How to check expected intent sent without actually launching activity in Espresso?

前端 未结 3 1588
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-14 01:42

I have a UI test which clicks a button, and then launch a new Activity in its onClickListener. The test checks whether expected intent is sent or not.

My problem is, I w

3条回答
  •  天命终不由人
    2021-02-14 02:11

    Actually, you can block any intent to launch an external or your own activity but still use the rich Espresso Intents API:

        Instrumentation.ActivityMonitor soloMonitor = solo.getActivityMonitor();
        instrumentation.removeMonitor(soloMonitor);
        IntentFilter filter = null;
        // Block any intent
        Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(filter, null, true);
        instrumentation.addMonitor(soloMonitor);
    
        // User action that results in an external browser activity being launched.
        user.clickOnView(system.getView(R.id.callButton));
        instrumentation.waitForIdleSync();
    
        Intents.intended(Matchers.allOf(
                IntentMatchers.hasAction(Matchers.equalTo(Intent.ACTION_VIEW)),
                IntentMatchers.hasData(Matchers.equalTo(Uri.parse(url))),
                IntentMatchers.toPackage(chromePackage)));
    
        instrumentation.removeMonitor(monitor);
    

    You able to do that because Espresso Intents still records every Intent with IntentMonitor callback even if you block them. Look at the source code of Espresso Intents on how they do that.

    If you use Robotium Solo framework you need to move your own ActivityMonitor before their one. Otherwise just skip the lines related to this.

提交回复
热议问题