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

Robolectric Emulator(Roborazzi Emulator) #176

Open
takahirom opened this issue Oct 8, 2023 · 8 comments · May be fixed by #181
Open

Robolectric Emulator(Roborazzi Emulator) #176

takahirom opened this issue Oct 8, 2023 · 8 comments · May be fixed by #181

Comments

@takahirom
Copy link
Owner

I've realized that we can create an emulator using Robolectric Native Graphics. This is just a demo, but it works. I'm wondering if there are any use cases for this.

screencast.2023-10-08.14-49-52.mp4
@JoseAlcerreca
Copy link

I want to know more :) I guess it takes a screenshot on idle and displays it on the UI?

@jeprubio
Copy link
Contributor

jeprubio commented Oct 9, 2023

Could that be useful to run the tests on multiple devices/configurations?

Something like what Test Harness does for compose.

I mean without having to create multiple classes with annotations like @Config(qualifiers = RobolectricDeviceQualifiers.*), which is the only way I can think of to achieve that for now (in tests without compose).

@sergio-sastre
Copy link

sergio-sastre commented Oct 10, 2023

Probably for debugging purposes if you can see what is displayed before the screenshot is taken, and you give an option to “stop/sleep” for x seconds

@takahirom
Copy link
Owner Author

takahirom commented Oct 11, 2023

Thank you for your feedback. I think it will be useful for debugging. Here's what I'm thinking:
The reason I use a function is that we might want to debug a specific function rather than running all tests.

  @Test
  fun emulator() {
    onView(ViewMatchers.isRoot())
      .roboNativeGraphicsEmulator()
  }

(when you click Dump)
image

@sergio-sastre
Copy link

Thank you for your feedback. I think it will be useful for debugging. Here's what I'm thinking: The reason I use a function is that we might want to debug a specific function rather than running all tests.

  @Test
  fun emulator() {
    onView(ViewMatchers.isRoot())
      .roboNativeGraphicsEmulator()
  }

(when you click Dump) image

Thinking carefully, this makes sense not only for Roborazzi tests but also for Robolectric UI tests with RNG.

Sometimes they fail and I resort to run them on the emulator (shared test pattern) to see why they fail. This is definitely sth that would help debugging such tests without the need to making them “shared” between instrumentation & unit tests

@takahirom takahirom linked a pull request Oct 12, 2023 that will close this issue
@sergio-sastre
Copy link

sergio-sastre commented Nov 10, 2023

Could that be useful to run the tests on multiple devices/configurations?

Something like what Test Harness does for compose.

I mean without having to create multiple classes with annotations like @Config(qualifiers = RobolectricDeviceQualifiers.*), which is the only way I can think of to achieve that for now (in tests without compose).

@jeprubio
It's not the only way indeed. The easiest is to use cumulative qualifiers and set them dynamically by calling e.g. RuntimeEnvironment.setQualifiers("+night")

I've just written a blog post about that:
Efficient testing with robolectric roborazzi across many ui states devices and configurations

Moreover, you can also achieve that by using an ActivityScenario (which is compatible with Robolectric), launching an EmptyActivity whose context has the desired configuration, and inflating your View in it. That has the extra advantage of allowing you to set a custom xml theme and use pseudolocales, which are not supported via Robolectric qualifiers.
That's how AndroidUiTestingUtils supports it via RobolectricActivityScenarioForView/Composable

@jeprubio
Copy link
Contributor

jeprubio commented Nov 11, 2023

@sergio-sastre Thanks! That's really helpful. I did know and use the cumulative qualifiers annotations but I didn't know about RuntimeEnvironment.setQualifiers() for example. For xml I was launching a DummyActivity with a dummy layout and inflating the xml in it.

These days I'm a bit busy with my new home but this week I'll find a moment to read more in detail all that article, I'm sure I'll learn from that. Thanks a lot!

@yschimke
Copy link
Contributor

yschimke commented Apr 12, 2024

What about if instead of running it's own UI, it exposed the same hooks that Android Emulators do for returning images, and processing input.

Would be pretty useful tool for interactive compose previews, similar to the Android Studio previews, but based on a more open approach. A good base to build features like a VS Code preview support.

(possibly relevent? - https://github.com/google/android-emulator-webrtc)

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 a pull request may close this issue.

5 participants