Skip to content
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

[Test Harness] Add custom ContextThemeWrapper to improve test harness behavior in previews #1414

Merged
merged 2 commits into from
Nov 30, 2022

Conversation

alexvanyo
Copy link
Collaborator

Adds a custom ContextThemeWrapper to work around the issue that currently causes TestHarness to fail in previews.

The cause of the crash is that Context.createConfigurationContext returns null for BridgeContext in layoutlib: https://cs.android.com/android/platform/superproject/+/master:frameworks/layoutlib/bridge/src/com/android/layoutlib/bridge/android/BridgeContext.java;l=1380;drc=58bd418e161b5d4fdde39b46523d3e49269117e3

ContextThemeWrapper uses this Context.createConfigurationContext internally and assumes it is not null, which causes the crash.

The androidx.appcompat.view.ContextThemeWrapper has a fallback mechanism for older API versions using a deprecated Resources constructor, so this PR adds a custom ContextThemeWrapper based on the appcompat one that uses that fallback mechanism in case Context.createConfigurationContext is null.

This improves behavior, preventing the crash, but doesn't remove all of the limitations. In particular, alternate resources are not loaded (such as the string not translated in the below image), which is probably related to how layoutlib manages resources.

Screen Shot 2022-11-22 at 12 05 11 PM

@alexvanyo alexvanyo marked this pull request as ready for review November 29, 2022 18:29
* This is a modification of androidx.appcompat.view.ContextThemeWrapper to allow the
* TestHarness to work in previews.
*/
class ContextThemeWrapper extends ContextWrapper {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I kept this class in .java, to be closer to the appcompat ContextThemeWrapper. I could convert and simplify, if desired.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you are happy to maintain Java code, fine by me!

@alexvanyo alexvanyo merged commit 6b1b669 into main Nov 30, 2022
@alexvanyo alexvanyo deleted the av/test-harness-custom-context-theme-wrapper branch November 30, 2022 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants