I'm setting up Compose Preview Screenshot Testing,...
# compose
m
I'm setting up Compose Preview Screenshot Testing, I have a composable with a date that's formatted with
java.time.format.DateTimeFormatter
, more specifically using
Locale.getDefault()
. When I generate the reference screenshots on my local machine the locale formats dates in
DD.M.YYYY
. However, when I run tests on my CI machine it uses the
M/DD/YYYY
format. This causes the tests to fail, as now the screenshots look different. What would be a recommended way to go about this? Of course, providing a Locale as an argument to the Composable would be one option, but I'm wondering if there's a more graceful approach.
m
Locale.setDefault()
in the test before building the UI
Ideally, it would probably be best to do screen shot tests in different locals to be able to look at how the screen looks with different translations and time formatting.
m
Thanks! I'll give it a go!
Copy code
Write access not allowed during rendering (user.language)
        at com.android.tools.rendering.security.RenderSecurityException.create(RenderSecurityException.java:52)
        at com.android.tools.rendering.security.RenderSecurityManager.checkPermission(RenderSecurityManager.java:694)
        at java.base/java.util.Locale.setDefault(Locale.java:1110)
        at java.base/java.util.Locale.setDefault(Locale.java:1069)
Unfortunately that didn't quite work
I guess the way here would be to pass the Locale as an argument to the Composable and define a default value for the parameter (
Locale.getDefault()
)..
m
Could use a CompositionLocal instead of a parameter
m
Interesting! I wouldn't have thought of using
CompositionLocal
, but at the same time I've got very little experience with it so maybe that's why. I think the date is used in so few places, that the arg with a default parameter seems like an easier approach here.