如何通过 Robolectric 获取当前活动?

发布于 2024-12-02 04:50:36 字数 98 浏览 0 评论 0原文

假设我有一个活动 A,它从其 onCreate() 方法中启动另一个活动 B,并期待结果。

如何使用 Robolectric 获取活动 B?

Suppose I have an activity A that launches another activity B from within its onCreate() method, expecting for results.

How do I get activity B using Robolectric?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

攒一口袋星星 2024-12-09 04:50:36

这个怎么样:

ActivityA activityA = setupActivity(ActivityA.class);
// Start other activity by e.g. pushing on a button
Intent intentForB = shadowOf(activityA).peekNextStartedActivity();
ActivityB activityB = buildActivity(ActivityB.class).withIntent(intentForB)
    .create().get();

How about this:

ActivityA activityA = setupActivity(ActivityA.class);
// Start other activity by e.g. pushing on a button
Intent intentForB = shadowOf(activityA).peekNextStartedActivity();
ActivityB activityB = buildActivity(ActivityB.class).withIntent(intentForB)
    .create().get();
忘羡 2024-12-09 04:50:36

也许您可以验证按钮启动的活动是否符合预期?

    button.performClick();
    assertThat(activity, new StartedMatcher( SecondActivity.class));

maybe you could verify that the activity that the button launched is as expected?

    button.performClick();
    assertThat(activity, new StartedMatcher( SecondActivity.class));
聊慰 2024-12-09 04:50:36

来自 Roboletric 文档

[...],Robolectric 只能验证第二个活动是否已启动,但不能验证它是否实际启动。

因此,您无法获取 Activity 本身,但您可以拦截正在传递的 Intent 并检查是否已启动正确的 Activity。

为此,您可以使用Shadows。下面是检查 ActivityB 是否在 ActivityAonCreate 方法期间启动的代码(注意:我使用的是 Kotlin,但 Java 代码看起来很漂亮大致相同)。

// The activity should be built using Roboletric's method
val activityA = Robolectric.buildActivity(ActivityA::class.java)
            .setup() // setup calls onCreate and onResume
            .get()

// now we need a Shadow (spooky!) to verify the next activity is started
val shadowOfA = Shadows.shadowOf(activityA)

// with the shadow it is easy if ActivityB was launched
assertThat(shadowOfA.getNextStartedActivity().getComponent())
            .isEqualTo(ComponentName(activityA, ActivityB::class.java))

ShadowActivity 中的 getNextStartedActivity 方法返回正在启动的 Intent。您可以检查其组件,看看它是否与您想要的相匹配,甚至可以检查 Bundle 内部,看看是否传递了您需要的所有内容。

getNextStartedActivity 的文档:http://robolectric.org/javadoc/3.0/org/robolectric/shadows/ShadowContextWrapper.html#getNextStartedActivity--

From the Roboletric documentation:

[...], Robolectric is only able to validate that the second activity would have been launched, but not that it is actually launched.

So you cannot get the Activity in itself, but you can intercept the Intent being passed and check if the right activity would have been launched.

For that you can use Shadows. Here is the code to check if ActivityB is launched during ActivityA's onCreate method (note: I am using Kotlin but the Java code looks pretty much the same).

// The activity should be built using Roboletric's method
val activityA = Robolectric.buildActivity(ActivityA::class.java)
            .setup() // setup calls onCreate and onResume
            .get()

// now we need a Shadow (spooky!) to verify the next activity is started
val shadowOfA = Shadows.shadowOf(activityA)

// with the shadow it is easy if ActivityB was launched
assertThat(shadowOfA.getNextStartedActivity().getComponent())
            .isEqualTo(ComponentName(activityA, ActivityB::class.java))

The getNextStartedActivity method from ShadowActivity returns the intent being started. You can check its components to see if it matches what you want and even check inside the Bundle to see if you are passing everything you need.

Documentation for getNextStartedActivity: http://robolectric.org/javadoc/3.0/org/robolectric/shadows/ShadowContextWrapper.html#getNextStartedActivity--

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文