-
Notifications
You must be signed in to change notification settings - Fork 340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add PendingIntentCompat to enforce mutability flag #37
base: master
Are you sure you want to change the base?
Conversation
There are some errors in this PR, I am fixing it. Please don't merge it immediately, thanks. |
@adaext FYI, you could change this PR to a "draft" while you're still working on it: This way it's clear to everyone that it's not ready for merge yet. |
Thank you very much, you are so kind. I've converted it to draft. |
@adaext Thanks for this PR! Before diving into the details, I had a question about the broader reason for this pull request. It looks like it replaces around 6 lines of code with 393, and the majority of that new code is unused. Is there a reason for that new code to exist? Is there an immediate plan to use it in the project? From a maintenance perspective I'd prefer to keep smaller simpler solutions if that's all that's needed. |
Thanks for the question. Firstly, Everytime we use |
@adaext I appreciate your effort on this PR, but I think Android X would be a better home for this class. IMHO, looking closer, there is just too much extra code here that's not currently needed to justify merging into this app - as a result, it adds a maintenance burden without any benefit. Only one of the Please keep us posted on when this code gets released into Android X, and at that time we can switch over to using it if the benefits outweigh the costs. For better coordination, I'd suggest opening issues in the future to discuss code changes before implementing them. I know issues aren't currently enabled on this GitHub repo, so I'm going to check with the team to see what the best path for coordination is going forward. Thanks again for your contributions! |
I agree with @barbeau that we should not add code that is not used to the App. This will increase the code size and bring maintenance burden. You can keep the methods that are used and needed. |
…tidy PendingIntentCompat specific for this project
Thanks for your advice. I've learned a lot from that. I've made a tidy PendingIntentCompat specific for this project in the last commit which deleted the useless test and uncalled function. Also fix the problem that the test can't run. I agree with your opinions on opening issues. Although this project can create issue here https://developer.android.com/guide/topics/sensors/gnss, it seems that we could just receive feedback and couldn't discuss here. So, it's not convenient. Looking forward to your good news! |
Thanks for your advice, I've commited a new version as you requested. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @adaext for reducing the scope of the PR to code that will be used in this project.
Please see my comments in-line below.
GNSSLogger/app/src/main/java/com/google/android/apps/location/gps/gnsslogger/MainActivity.java
Show resolved
Hide resolved
import org.robolectric.shadows.ShadowPendingIntent; | ||
|
||
/** Unit test for {@link PendingIntentCompat}. */ | ||
@RunWith(RobolectricTestRunner.class) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use Expresso instead of Robolectric?
https://developer.android.com/training/testing/espresso/intents
I'd prefer to avoid external testing libraries unless absolutely necessary. I've opened PR #38 to add CI with multiple API levels that we can test on going forward. After that PR is merged, please merge master into this branch so the tests run on CI.
Also, my preference would be that all new classes be implemented in Kotlin instead of Java. See other comment for a way to do a quick conversion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you use Expresso instead of Robolectric?
https://developer.android.com/training/testing/espresso/intents
I'd prefer to avoid external testing libraries unless absolutely necessary. I've opened PR #38 to add CI with multiple API levels that we can test on going forward. After that PR is merged, please merge master into this branch so the tests run on CI.
Also, my preference would be that all new classes be implemented in Kotlin instead of Java. See other comment for a way to do a quick conversion.
Thanks for your advice. Robolectric is very lightweight compared to Expresso. My class is simple enough and does not require too complicated tests. I think it may easier to use Robolectric.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My issue with Robolectric is that it doesn't actually run on Android platform code, so you're not testing on an actual Android implementation. As a result I've preferred instrumented tests and have used Robolectric only as a method of last resort (e.g., when performance is critical, instrumented tests can't run on emulators/CI). GnssLogger will eventually have other instrumented tests so adding one more here for the PendingIntent doesn't add significant overhead.
It seems like the AndroidX library also prefers instrumented tests to Robolectric.
If you take a look, most of the unit tests for the AndroidX core library are here in androidTest
:
https://github.com/androidx/androidx/tree/androidx-main/core/core/src/androidTest/java/androidx/core
...not in test
using Robolectric on the JVM:
https://github.com/androidx/androidx/tree/androidx-main/core/core/src/test/java/androidx/core
If you look at the Robolectric tests, they are annotated with @DoNotInstrument
, which means they exist there for a reason.
I also don't think you need full blown Expresso here - if you take a look at some of the Android X instrumented tests you'll get an idea of how you could mirror that approach with the PendingIntent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My issue with Robolectric is that it doesn't actually run on Android platform code, so you're not testing on an actual Android implementation. As a result I've preferred instrumented tests and have used Robolectric only as a method of last resort (e.g., when performance is critical, instrumented tests can't run on emulators/CI). GnssLogger will eventually have other instrumented tests so adding one more here for the PendingIntent doesn't add significant overhead.
It seems like the AndroidX library also prefers instrumented tests to Robolectric.
If you take a look, most of the unit tests for the AndroidX core library are here in
androidTest
: https://github.com/androidx/androidx/tree/androidx-main/core/core/src/androidTest/java/androidx/core...not in
test
using Robolectric on the JVM: https://github.com/androidx/androidx/tree/androidx-main/core/core/src/test/java/androidx/coreIf you look at the Robolectric tests, they are annotated with
@DoNotInstrument
, which means they exist there for a reason.I also don't think you need full blown Expresso here - if you take a look at some of the Android X instrumented tests you'll get an idea of how you could mirror that approach with the PendingIntent.
I agree with that instrumented tests are needed in this project. However, I don't know how to apply it on PendingIntentCompat
. Because we can't get the parameters information directly through creating PendingIntent
object. https://github.com/androidx/androidx/blob/androidx-main/core/core/src/androidTest/java/androidx/core/app/DialogCompatTest.java. However, shadow of Robolectirc could help us get those information.
import androidx.annotation.NonNull; | ||
|
||
/** Helper for accessing features in {@link PendingIntent}. */ | ||
public class PendingIntentCompat { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please move this class to a new .util
package and convert it to Kotlin? You can do this easily within Android Studio by right-clicking on the file and selecting "Convert Java file to Kotlin file". I'd prefer all new classes be written in Kotlin.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please move this class to a new
.util
package and convert it to Kotlin? You can do this easily within Android Studio by right-clicking on the file and selecting "Convert Java file to Kotlin file". I'd prefer all new classes be written in Kotlin.
Ok, thanks
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please move this class to a new
.util
package and convert it to Kotlin? You can do this easily within Android Studio by right-clicking on the file and selecting "Convert Java file to Kotlin file". I'd prefer all new classes be written in Kotlin.Ok, thanks
I've converted this class to Kotlin. But I found that the previous integrated google-java-format
tool can't format Kotlin file. I think it's better to integrate Facebook Kotlin formatter
into gradle, which is based on google-java-format
. https://github.com/facebookincubator/ktfmt.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, we'll need a separate agreed-upon formatter for a Kotlin style.
The canonical style references seem to be:
- Android team - https://developer.android.com/kotlin/style-guide
- Kotlin language - https://kotlinlang.org/docs/coding-conventions.html
From a brief search, though, Google and Jetbrains don't seem to have their own standalone Kotlin formatting tool (they only have IntelilJ/Android Studio itself).
These seem to be the top two 3rd party contenders:
Both seem to be actively maintained.
Let's leave this for a separate PR - I'll do some asking around to see what's preferred.
...r/app/src/main/java/com/google/android/apps/location/gps/gnsslogger/PendingIntentCompat.java
Outdated
Show resolved
Hide resolved
...p/src/test/java/com/google/android/apps/location/gps/gnsslogger/PendingIntentCompatTest.java
Outdated
Show resolved
Hide resolved
...p/src/test/java/com/google/android/apps/location/gps/gnsslogger/PendingIntentCompatTest.java
Outdated
Show resolved
Hide resolved
...p/src/test/java/com/google/android/apps/location/gps/gnsslogger/PendingIntentCompatTest.java
Outdated
Show resolved
Hide resolved
For future reference, here's the PR for this same feature on Android X: |
I still feel it might be too much to have a separate class within GnssLogger just for the purpose of handing PendingIntent mutability. On the other hand, I totally agree on moving the class to AndroidX and definitely would like to help in that. |
Ok, thanks! |
你好,邮件已收到。
|
PendingIntentCompat combines mutability flag when necessary.