How can I shadow the PackageManager with Robolectric

后端 未结 6 836
有刺的猬
有刺的猬 2020-12-15 22:16

My Android application has a simple method to fire off an intent to display a URL.

protected void launchBrowser(int id)
{
    Uri uri = Uri.parse( getStrin         


        
6条回答
  •  太阳男子
    2020-12-15 23:16

    I upgraded to the latest Robolectric (version 2.1.1) and the PackageManager doesn't come up null anymore.

    The code below verifies that a browser intent is fired (and configured with a url). I haven't tested this on 1.x, but I think it works there too:

    @Test
    public void recipeTitleShouldOpenBrowserOnClick() throws Exception
    {
        title.performClick();
        ShadowActivity shadowActivity = shadowOf( detailFragment.getActivity() );
        ShadowActivity.IntentForResult intent = shadowActivity.peekNextStartedActivityForResult();
    
        // or
        // ShadowActivity.IntentForResult intent = shadowActivity.getNextStartedActivityForResult();
    
        assertThat( intent.intent,
                    equalTo( createBrowserIntentWithURL( "url" ) ) );
    }
    

    Note: I'm invoking start activity from a fragment here.

提交回复
热议问题