Skip to content

Conversation

@pankaj0695
Copy link

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

Approach

  • Added a new Robolectric test class at AnkiDroid/src/test/java/com/ichi2/preferences/NumberRangePreferenceCompatTest.kt.
  • Implemented focused unit tests for:
    • getValidatedRangeFromInt: values within range and below min clamping.
    • getValidatedRangeFromString: empty string, valid numeric string and invalid string handling.
    • onDialogClosed: ensures blank input and cancelled dialog do not trigger value updates, while valid input does.

How Has This Been Tested?

Run the unit tests via Gradle

./gradlew :AnkiDroid:testDebugUnitTest --tests "com.ichi2.preferences.NumberRangePreferenceCompatTest"
Screenshot 2025-12-28 at 6 31 34 PM

Checklist

Please, go through these checks before submitting the PR.

  • You have a descriptive commit message with a short title (first line, max 50 chars).
  • You have commented your code, particularly in hard-to-understand areas
  • You have performed a self-review of your own code
  • UI changes: include screenshots of all affected screens (in particular showing any new or changed strings)
  • UI Changes: You have tested your change using the Google Accessibility Scanner

@welcome
Copy link

welcome bot commented Dec 28, 2025

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.

Copy link
Member

@david-allison david-allison left a 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

Comment on lines 29 to 37
class NumberRangePreferenceCompatTest {
private lateinit var context: Context
private lateinit var preference: TestableNumberRangePreferenceCompat

@Before
fun setUp() {
context = ApplicationProvider.getApplicationContext()
preference = TestableNumberRangePreferenceCompat(context)
}
Copy link
Member

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)

/**
* Test subclass to expose onDialogClosed for testing and track setValue calls
*/
private class TestNumberRangeDialogFragmentCompat : NumberRangePreferenceCompat.NumberRangeDialogFragmentCompat() {
Copy link
Member

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/

@david-allison david-allison added the Needs Author Reply Waiting for a reply from the original author label Dec 28, 2025
@pankaj0695 pankaj0695 force-pushed the created_NumberRangePreferenceCompatTest.kt branch from 8e2c1b7 to 581fdfe Compare December 28, 2025 19:05
@pankaj0695
Copy link
Author

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.

@sanjaysargam sanjaysargam added Needs Review and removed Needs Author Reply Waiting for a reply from the original author labels Dec 29, 2025
Comment on lines 33 to 39
// 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
Copy link
Contributor

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

Comment on lines 73 to 74
// Test for onDialogClosed behavior
// This tests: @NeedsTest("value is set to preference previous value if text is blank")
Copy link
Contributor

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")
Copy link
Contributor

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")
Copy link
Contributor

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

@criticalAY criticalAY added the Needs Author Reply Waiting for a reply from the original author label Dec 29, 2025
@pankaj0695 pankaj0695 force-pushed the created_NumberRangePreferenceCompatTest.kt branch from 581fdfe to b8eac7b Compare December 29, 2025 10:01
@pankaj0695
Copy link
Author

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.

Copy link
Contributor

@criticalAY criticalAY left a 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() {
Copy link
Contributor

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

@criticalAY
Copy link
Contributor

@pankaj0695 Please clean the commit message

made changes

make required changes

update
@pankaj0695 pankaj0695 force-pushed the created_NumberRangePreferenceCompatTest.kt branch from b8eac7b to 8300a01 Compare December 29, 2025 10:37
@pankaj0695
Copy link
Author

I have made the required updates

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs Author Reply Waiting for a reply from the original author Needs Review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Cleanup]: Add Tests to the code (@NeedsTest)

4 participants