-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support @Preview param Wallpapers #486
Comments
Changing to app code seems only neccesary if the theme is embedded in the components you want to test. In practice my own code generally assumes the Theme is applied higher up, say in the Activity. This is generally safe for libraries if you assume someone else wants to configure say MaterialTheme. But it does seem to mean that the Previews in AndroidStudio won't match the theme, if this is only applied in RenderPreview. Is there a WallpaperThemeApplier that could be used in the Preview and therefore automatically in the roborazzi preview test, without needing to refer to previewInfo? |
The point is, AppTheme would work out of the box with Compose-Preview Screenshot Test tool and the theming applies properly in the Previews. However, that is not the case in Robolectric. To provide dynamic colors, Android uses a dynamic color engine called Monet, which is not available in Robolectric. Therefore it is not applied when running Robolectric. The trick I used is to intercept the theme and apply MaterialKolor’s DynamicColorTheme in tests, which emulates Monet. I doubt it is possible to apply Wallpapers with some kind of WallpaperThemeApplier |
After researching a bit, I think it could be possible by overriding the existing LocalColorScheme with the one that MaterialKolor provides and production code would not need any change to enable it. I’ll give it a try once I am back from holidays |
It seems that LocalColorScheme is something fancy that the AI tool I was using made up and it does not exist 😅 Therefore, I believe the best is to create an issue in Robolectric to support the dynamic colors engine Monet and wait for them to have it implemented. I’ll create the issue. That should make it work out of the box without needing any change in Roborazzi’s code. |
Would implementing a shadow that called MaterialKolor’s DynamicColorTheme be a usable solution? |
That’s indeed a pretty good idea! Not sure whether that suffices, but I’ll give it a try. |
And a more detailed blog about the classes involved here: |
Since this is the Preview param that I consider the hardest to support, because it is something Robolectric doesn't support, I decided to give it a try, and I succeded :)
What I'm not sure is how we could provide support out of the box, because this solution requires the use of
CompositionLocalProvider. That means, the user should configure his code in such a way that it allows to switch Themes dynamically, for instance, in tests.
Assuming the user has defined his AppTheme like this as stated in the Android docu:
He'll have to make the following changes
Then one can use MaterialKolor in Roborazzi tests if Wallpapers apply:
and use it in the generated Roborazzi Test
I have a PR in "Android Screenshot Testing Playground" where this works :)
sergio-sastre/Android-screenshot-testing-playground#69
As I mentioned, I am a bit uncertain on how we could add support for this since the user needs to adjust his production code to enable this.
The best I come up with is:
then you just need to add that import in the auto-generated test
Do you have any better idea maybe?
The text was updated successfully, but these errors were encountered: