ndtp / android-testify

Add screenshots to your Android tests
Other
99 stars 5 forks source link

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

Open sergio-sastre opened 4 months ago

sergio-sastre commented 4 months ago

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:

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

DanielJette commented 4 months ago

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

sergio-sastre commented 4 months ago

@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 😊

DanielJette commented 4 months ago

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