-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add unit tests for NumberRangePreferenceCompat dialog behavior (#13283) #19957
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
base: main
Are you sure you want to change the base?
Add unit tests for NumberRangePreferenceCompat dialog behavior (#13283) #19957
Conversation
|
First PR! 🚀 We sincerely appreciate that you have taken the time to propose a change to AnkiDroid! Please have patience with us as we are all volunteers - we will get to this as soon as possible. |
david-allison
left a comment
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.
Looks good, thanks! Tests are great!
Feel free to resolve the nitpicks without changes, especially the mockk one, as that may not easily be feasible.
In addition: could you squash any new comments and change the commit message to something along the lines of:
test: add NumberRangePreferenceCompatTest
AnkiDroid/src/test/java/com/ichi2/preferences/NumberRangePreferenceCompatTest.kt
Outdated
Show resolved
Hide resolved
| class NumberRangePreferenceCompatTest { | ||
| private lateinit var context: Context | ||
| private lateinit var preference: TestableNumberRangePreferenceCompat | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| context = ApplicationProvider.getApplicationContext() | ||
| preference = TestableNumberRangePreferenceCompat(context) | ||
| } |
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.
class NumberRangePreferenceCompatTest: RobolectricTest() {
private val preference = TestableNumberRangePreferenceCompat(targetContext)
AnkiDroid/src/test/java/com/ichi2/preferences/NumberRangePreferenceCompatTest.kt
Outdated
Show resolved
Hide resolved
| /** | ||
| * Test subclass to expose onDialogClosed for testing and track setValue calls | ||
| */ | ||
| private class TestNumberRangeDialogFragmentCompat : NumberRangePreferenceCompat.NumberRangeDialogFragmentCompat() { |
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.
nit: You may be able to use mockk here instead: https://mockk.io/
8e2c1b7 to
581fdfe
Compare
|
Thanks for the review, I addressed the nitpicks by extracting the repeated dialog setup into a helper and switching the onDialogClosed assertions to MockK interaction verification (no functional changes). I also squashed the commits and updated the commit message. |
| // Keep the real preference for validation tests | ||
| private val preference = TestableNumberRangePreferenceCompat(targetContext) | ||
|
|
||
| // Use a mock for onDialogClosed interaction verification | ||
| private val preferenceMock: NumberRangePreferenceCompat = mockk(relaxed = true) | ||
|
|
||
| // Tests for getValidatedRangeFromInt |
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.
LLM? but I am not opposing using it just that the code before being pushed should be cleaned and understood, here you have left comments that are not needed
| // Test for onDialogClosed behavior | ||
| // This tests: @NeedsTest("value is set to preference previous value if text is blank") |
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.
Not needed
|
|
||
| // Inject the preference into the fragment (preference is held by the base class) | ||
| // We use reflection so we can unit-test onDialogClosed without running a full Preference/Fragment lifecycle. | ||
| val preferenceField = androidx.preference.PreferenceDialogFragmentCompat::class.java.getDeclaredField("mPreference") |
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.
Use import
| } | ||
|
|
||
| // Test for onDialogClosed behavior | ||
| // This tests: @NeedsTest("value is set to preference previous value if text is blank") |
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.
If you are resolving an annotation then remove that annotation from the original place so that we know what is being resolved
581fdfe to
b8eac7b
Compare
|
Thanks for the note, I did use LLM for guidance, but I made sure I understood the changes before pushing. I’ve now removed the unnecessary comments and made required changes, and will make sure future updates are clean and reviewed before I push them. |
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.
Looks good one last change
| private val preferenceMock: NumberRangePreferenceCompat = mockk(relaxed = true) | ||
|
|
||
| @Test | ||
| fun getValidatedRangeFromInt_withinRange_returnsInput() { |
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.
Use backticks or camel case
|
@pankaj0695 Please clean the commit message |
made changes make required changes update
b8eac7b to
8300a01
Compare
|
I have made the required updates |
Purpose / Description
This PR adds missing unit test coverage for NumberRangePreferenceCompat, specifically the behavior marked with @NeedsTest where the preference value should remain unchanged when the dialog is confirmed with blank input. It also adds coverage for range validation helpers to prevent regressions.
Fixes
@NeedsTest) #13283Approach
How Has This Been Tested?
Run the unit tests via Gradle
Checklist
Please, go through these checks before submitting the PR.