android-testify icon indicating copy to clipboard operation
android-testify copied to clipboard

Throws UnexpectedDeviceException because of a mismatch of DisplaySize although it has the same value

Open sergio-sastre opened this issue 1 year ago • 3 comments

Describe the bug One trick I generally use to configure an Activity under different configurations (uiMode, fontSize, displaySize, locale...) is to launch an EmptyActivity with the desired configuration, and inflate the views in it with that Activity's context.

Regarding the Display Size, I basically modify the densityDpi accordingly. e.g.

val newDensityDpi = displaySize.value.toFloat() * newConfig.densityDpi
newConfig.densityDpi = newDensityDpi.toInt()

However, this seems to cause a problem when verifying the screenshots with Android-testify, where I get an UnexpectedDeviceException because of a mismatch of the displayDensity in the device key.

After some investigation, it seems the problem lies on the following code lines in the assertSame() method:

assertExpectedDevice(testContext, description.name, isRecordMode)

val destination = getDestination(activity, outputFileName)

One of them uses testContext, which is getInstrumentation().context, while the other one uses the Activity's context. Since what I'm doing is to launch an EmptyActivity with a context that simulates a different display size via context's configuration's densityDpi, both contexts' display size differ, and when not in record mode, the assertExpectedDevice() throws an UnexpectedDeviceException

I think it'd make sense to pass the same context to both, likely the Activity context in this case.

This issue relates to:

  • [X] The Kotlin library
  • [ ] The Gradle plugin
  • [ ] The IntelliJ Platform plugin
  • [ ] The sample code
  • [ ] The documentation

Link to the corresponding issue in my library: https://github.com/sergio-sastre/AndroidUiTestingUtils/issues/104

sergio-sastre avatar Feb 19 '24 22:02 sergio-sastre

@sergio-sastre Thanks for logging this issue. Do you have an example test that I can try this out with?

DanielJette avatar Feb 19 '24 22:02 DanielJette

@DanielJette Sure! You can find an executable example here: https://github.com/sergio-sastre/Android-screenshot-testing-playground/blob/master/recyclerviewscreen/android-testify/src/androidTest/java/com/example/road/to/effective/snapshot/testing/recyclerviewscreen/android_testify/viewholder/MemoriseViewHolderTest.kt

However that uses AndroidUiTestingUtils on top of Android-Testify

I can create a simpler example if it helps. Just let me know 😊

sergio-sastre avatar Feb 20 '24 20:02 sergio-sastre

Sure! You can find an executable example here:

Perfect!

I can create a simpler example if it helps. Just let me know 😊

No thank you. That example will do just fine.

@sergio-sastre

DanielJette avatar Feb 20 '24 20:02 DanielJette