Android Espresso:意图匹配器和 androidx PreferenceFragments
Android Espresso: Intent matchers and androidx PreferenceFragments
我最近迁移了我的首选项实现以使用 androidx.preference
s。这导致我的一个仪器测试失败,我还没有找到合理的解决方法来让它通过。
在测试中,如果满足应用首选项中的某些条件,我有一个对话框显示错误。在此测试中,禁用了特定首选项。用户可以单击错误以打开特定的首选项,然后进入并更改它。问题是我不知道如何断言显示了正确的 PreferenceFragment。
失败的测试如下所示:
@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
)
)
)
}
但是当您不再使用 PreferenceActivity 实现时,如何重写它以匹配特定的 PreferenceFragment?
查看失败测试的堆栈跟踪后找到答案:
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}]])
解决方法:
intended(
allOf<Intent>(
IntentMatchers.hasComponent(
ComponentName(
InstrumentationRegistry.getInstrumentation().targetContext,
SettingsActivity::class.java
)
),
IntentMatchers.hasExtra(
EXTRA_SHOW_WORKLIST_FRAGMENT,
WorklistFragment::class.java
)
)
)
我最近迁移了我的首选项实现以使用 androidx.preference
s。这导致我的一个仪器测试失败,我还没有找到合理的解决方法来让它通过。
在测试中,如果满足应用首选项中的某些条件,我有一个对话框显示错误。在此测试中,禁用了特定首选项。用户可以单击错误以打开特定的首选项,然后进入并更改它。问题是我不知道如何断言显示了正确的 PreferenceFragment。
失败的测试如下所示:
@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
)
)
)
}
但是当您不再使用 PreferenceActivity 实现时,如何重写它以匹配特定的 PreferenceFragment?
查看失败测试的堆栈跟踪后找到答案:
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}]])
解决方法:
intended(
allOf<Intent>(
IntentMatchers.hasComponent(
ComponentName(
InstrumentationRegistry.getInstrumentation().targetContext,
SettingsActivity::class.java
)
),
IntentMatchers.hasExtra(
EXTRA_SHOW_WORKLIST_FRAGMENT,
WorklistFragment::class.java
)
)
)