如何验证IntentService启动 [英] How to verify IntentService start

查看:107
本文介绍了如何验证IntentService启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Espresso-2.2测试我的应用行为

I'm trying to test my app behavior with Espresso-2.2

在主要活动中,当同时按下服务按钮和另一个活动时:

On main activity, when button pressed both service and another activity is being started:

public class MainActivity extends Activity {

    public void onButtonClicked() {
        startActivity(SecondActivity.getStartIntent());
        startService(MyIntentService.getStartIntent());
    }
}

我正在测试是否正在启动预期的组件:

I'm testing if intended components are being started:

public class MainActivityTest {
    @Rule
    public final IntentsTestRule<MainActivity> intentsRule = new IntentsTestRule<>(MainActivity.class, true);

    @Test
    public void shouldStartServiceOnButtonClicked() {
        onView(withId(R.id.button)).perform(click());
        intended(hasComponent(hasClassName(SecondActivity.class.getName())));
        intended(hasComponent(hasClassName(MyIntentService.class.getName())));
    }
}

但是我遇到了错误:

Caused by: junit.framework.AssertionFailedError: Wanted to match 1 intents. Actually matched 0 intents.

IntentMatcher: has component: has component with: class name: is "com.example.MyIntentService" package name: an instance of java.lang.String short class name: an instance of java.lang.String

Matched intents:[]

Recorded intents:
-Intent { cmp=com.example/.SecondActivity (has extras) } handling packages:[[com.example]], extras:[Bundle[...]])
at junit.framework.Assert.fail(Assert.java:50)

SecondActivity的开始已注册.为什么我的IntentService的启动未注册(我检查它是否已启动)?

Start of SecondActivity is registered. Why start of my IntentService is not registered (I checked it is started)?

推荐答案

tl; dr鉴于当前提供的意式浓缩咖啡所提供的功能,看来似乎不可能实现

tl;dr it doesn't appear as if this is possible given the current espresso-intents provided functionality

由于要进行类似的操作,因此在源代码中进行了挖掘,这是我发现的:

I dug around in the source code since I'm trying to do something similar and this is what I found:

预期"匹配器通过查看已记录的Intent列表来工作.通过调用Intents.init()时已向IntentMonitor实例注册的回调跟踪这些事件. IntentMonitor实例由当前的Instrumentation定义.每当调用IntentMonitor的signalIntent()时,都会触发Intent中的回调.

the 'intended' matcher works by looking at a list of recorded Intents. These are tracked by a callback that gets registered with the IntentMonitor instance when Intents.init() is called. The IntentMonitor instance is defined by the current Instrumentation. Any time the IntentMonitor's signalIntent() is called, the callback in Intents will be fired.

问题在于这样的事实:在您以意图调用activity.startService()的情况下,signalIntent()从未真正被调用过.这是因为AndroidJunitRunner案例中的signalIntent()仅由ExposedInstrumentationApi上公开的方法调用(碰巧仅与各种startActivity()方法相关).为了将意式浓缩咖啡与startService()结合使用,似乎需要有一个startService()的工具挂钩,以允许在IntentMonitor上调用signalIntent().

The problem lies in the fact that signalIntent() never actually gets called in the case where you call activity.startService() with an intent. This is because signalIntent() in the AndroidJunitRunner case only gets called by methods that are exposed on ExposedInstrumentationApi (which happen to correlate only to the various startActivity() methods). In order to use espresso-intents with startService() it would appear that an instrumentation hook for startService() would need to be available that would allow for calling signalIntent() on the IntentMonitor.

我没有将自定义工具添加到测试apk的经验,但这将是我的下一个调查途径.如果发现任何有效的方法,我将更新答案.

I don't have experience with adding custom Instrumentation to my test apks, but this is going to be my next avenue of investigation. I'll update my answer if I discover anything that works.

这篇关于如何验证IntentService启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆