Issue
I've recently migrated my preference implementation to use androidx.preferences. This caused one of my instrumentation tests to fail and I haven't found a reasonable workaround to make it pass.
In the test I have a dialog that displays an error if some condition in the app preferences is met. In this test a specific preference is disabled. The user can click on the error to open the specific preference and that way go in and change it. The problem is I can't figure out how to assert that the correct PreferenceFragment is displayed.
The tests that fails looks like this:
@Test
fun whenWorklistNotEnabled_shouldDisplayWorklistNotEnabledMessage() {
val manager = PreferenceManagerImpl(InstrumentationRegistry.getInstrumentation().targetContext)
whenever(preferenceManagerSpy.worklistEnabled).thenReturn(false)
whenever(preferenceManagerSpy.openDicomSettings()).thenAnswer { manager.openWorklistSettings() }
launchWorklistDialog()
onView(withErrorMessageView(R.id.dialog_worklist_errorview))
.check(
matches(
allOf(
isDisplayed(),
withPrimaryErrorText(R.string.global_worklist_disabled_error),
withSecondaryErrorText(R.string.dialog_worklist_worklist_disabled_error_secondary_text)
)
)
)
.perform(ErrorMessageViewActions.actionOnSecondaryTextContainer(click()))
intended(
allOf<Intent>(
IntentMatchers.hasComponent(
ComponentName(
InstrumentationRegistry.getInstrumentation().targetContext,
SettingsActivity::class.java
)
),
IntentMatchers.hasExtra(
PreferenceActivity.EXTRA_SHOW_FRAGMENT,
WorklistFragment::class.java
)
)
)
}
But how can you rewrite it to match a specific PreferenceFragment when you no longer use a PreferenceActivity implementation?
Solution
Found the answer after looking at the stacktrace of the failing test:
Recorded intents:
-Intent { cmp=com.example/.preferences.SettingsActivity (has extras) } handling packages:[[com.example]], extras:[Bundle[{EXTRA_SHOW_DICOM_FRAGMENT=class com.example.preferences.WorklistFragment}]])
The solution:
intended(
allOf<Intent>(
IntentMatchers.hasComponent(
ComponentName(
InstrumentationRegistry.getInstrumentation().targetContext,
SettingsActivity::class.java
)
),
IntentMatchers.hasExtra(
EXTRA_SHOW_WORKLIST_FRAGMENT,
WorklistFragment::class.java
)
)
)
Answered By - Bohsen
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.