Search in sources :

Example 1 with RobolectricPackageManager

use of org.robolectric.res.builder.RobolectricPackageManager in project materialistic by hidroh.

the class ItemActivityTest method testOptionExternal.

@SuppressLint("NewApi")
@Test
public void testOptionExternal() {
    RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager();
    packageManager.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com")), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity"));
    packageManager.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(String.format(HackerNewsClient.WEB_ITEM_PATH, "1"))), ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity"));
    Intent intent = new Intent();
    intent.putExtra(ItemActivity.EXTRA_ITEM, new TestItem() {

        @NonNull
        @Override
        public String getType() {
            return STORY_TYPE;
        }

        @Override
        public String getUrl() {
            return "http://example.com";
        }

        @Override
        public boolean isStoryType() {
            return true;
        }

        @Override
        public String getId() {
            return "1";
        }
    });
    controller.withIntent(intent).create().start().resume();
    // inflate menu, see https://github.com/robolectric/robolectric/issues/1326
    ShadowLooper.pauseMainLooper();
    controller.visible();
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    // open article
    shadowOf(activity).clickMenuItem(R.id.menu_external);
    shadowOf(ShadowPopupMenu.getLatestPopupMenu()).getOnMenuItemClickListener().onMenuItemClick(new RoboMenuItem(R.id.menu_article));
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW);
    // open item
    shadowOf(activity).clickMenuItem(R.id.menu_external);
    shadowOf(ShadowPopupMenu.getLatestPopupMenu()).getOnMenuItemClickListener().onMenuItemClick(new RoboMenuItem(R.id.menu_comments));
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    assertThat(shadowOf(activity).getNextStartedActivity()).hasAction(Intent.ACTION_VIEW);
}
Also used : NonNull(android.support.annotation.NonNull) Intent(android.content.Intent) RobolectricPackageManager(org.robolectric.res.builder.RobolectricPackageManager) RoboMenuItem(org.robolectric.fakes.RoboMenuItem) TestItem(io.github.hidroh.materialistic.test.TestItem) SlowTest(io.github.hidroh.materialistic.test.suite.SlowTest) Test(org.junit.Test) SuppressLint(android.annotation.SuppressLint)

Example 2 with RobolectricPackageManager

use of org.robolectric.res.builder.RobolectricPackageManager in project robolectric by robolectric.

the class RobolectricTest method reset_shouldResetShadows_beforeClearingPackageManager.

@Test
public void reset_shouldResetShadows_beforeClearingPackageManager() {
    Iterable<ShadowProvider> oldProviders = ReflectionHelpers.getStaticField(Robolectric.class, "providers");
    ;
    ShadowProvider mockProvider = new MockProvider();
    List<ShadowProvider> mockProviders = Collections.singletonList(mockProvider);
    ReflectionHelpers.setStaticField(Robolectric.class, "providers", mockProviders);
    RobolectricPackageManager mockManager = mock(RobolectricPackageManager.class);
    doAnswer(new Answer<Void>() {

        public Void answer(InvocationOnMock invocation) {
            order.add("packageManager");
            return null;
        }
    }).when(mockManager).reset();
    RuntimeEnvironment.setRobolectricPackageManager(mockManager);
    try {
        Robolectric.reset();
    } finally {
        // Make sure we clean up after ourselves
        ReflectionHelpers.setStaticField(Robolectric.class, "providers", oldProviders);
    }
    assertThat(order).as("reset order").containsExactly("shadowProvider", "packageManager");
    assertThat(RuntimeEnvironment.application).as("app after reset").isNull();
    assertThat(RuntimeEnvironment.getRobolectricPackageManager()).as("packageManager after reset").isNull();
    assertThat(RuntimeEnvironment.getActivityThread()).as("activityThread after reset").isNull();
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) RobolectricPackageManager(org.robolectric.res.builder.RobolectricPackageManager) ShadowProvider(org.robolectric.internal.ShadowProvider) Test(org.junit.Test)

Example 3 with RobolectricPackageManager

use of org.robolectric.res.builder.RobolectricPackageManager in project glide by bumptech.

the class GlideTest method setUp.

@Before
public void setUp() throws Exception {
    Glide.tearDown();
    RobolectricPackageManager pm = RuntimeEnvironment.getRobolectricPackageManager();
    ApplicationInfo info = pm.getApplicationInfo(RuntimeEnvironment.application.getPackageName(), 0);
    info.metaData = new Bundle();
    info.metaData.putString(SetupModule.class.getName(), "GlideModule");
    // Ensure that target's size ready callback will be called synchronously.
    target = mock(Target.class);
    imageView = new ImageView(RuntimeEnvironment.application);
    imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
    doAnswer(new CallSizeReady()).when(target).getSize(isA(SizeReadyCallback.class));
    Handler bgHandler = mock(Handler.class);
    when(bgHandler.post(isA(Runnable.class))).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            Runnable runnable = (Runnable) invocation.getArguments()[0];
            runnable.run();
            return true;
        }
    });
    Lifecycle lifecycle = mock(Lifecycle.class);
    RequestManagerTreeNode treeNode = mock(RequestManagerTreeNode.class);
    requestManager = new RequestManager(Glide.get(getContext()), lifecycle, treeNode);
    requestManager.resumeRequests();
}
Also used : Bundle(android.os.Bundle) ViewGroup(android.view.ViewGroup) Lifecycle(com.bumptech.glide.manager.Lifecycle) ApplicationInfo(android.content.pm.ApplicationInfo) Handler(android.os.Handler) SizeReadyCallback(com.bumptech.glide.request.target.SizeReadyCallback) Target(com.bumptech.glide.request.target.Target) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ImageView(android.widget.ImageView) RequestManagerTreeNode(com.bumptech.glide.manager.RequestManagerTreeNode) RobolectricPackageManager(org.robolectric.res.builder.RobolectricPackageManager) Before(org.junit.Before)

Example 4 with RobolectricPackageManager

use of org.robolectric.res.builder.RobolectricPackageManager in project materialistic by hidroh.

the class WebFragmentTest method testDownloadPDF.

@Test
public void testDownloadPDF() {
    ResolveInfo resolverInfo = new ResolveInfo();
    resolverInfo.activityInfo = new ActivityInfo();
    resolverInfo.activityInfo.applicationInfo = new ApplicationInfo();
    resolverInfo.activityInfo.applicationInfo.packageName = ListActivity.class.getPackage().getName();
    resolverInfo.activityInfo.name = ListActivity.class.getName();
    RobolectricPackageManager rpm = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager();
    rpm.addResolveInfoForIntent(new Intent(Intent.ACTION_VIEW, Uri.parse("http://example.com/file.pdf")), resolverInfo);
    WebView webView = (WebView) activity.findViewById(R.id.web_view);
    ShadowWebView shadowWebView = (ShadowWebView) ShadowExtractor.extract(webView);
    shadowWebView.getDownloadListener().onDownloadStart("http://example.com/file.pdf", "", "", "", 0l);
    assertThat(activity.findViewById(R.id.empty)).isVisible();
    activity.findViewById(R.id.download_button).performClick();
    assertNotNull(shadowOf(activity).getNextStartedActivity());
}
Also used : ResolveInfo(android.content.pm.ResolveInfo) ActivityInfo(android.content.pm.ActivityInfo) ShadowWebView(io.github.hidroh.materialistic.test.shadow.ShadowWebView) ApplicationInfo(android.content.pm.ApplicationInfo) Intent(android.content.Intent) RobolectricPackageManager(org.robolectric.res.builder.RobolectricPackageManager) WebView(android.webkit.WebView) ShadowWebView(io.github.hidroh.materialistic.test.shadow.ShadowWebView) Test(org.junit.Test)

Example 5 with RobolectricPackageManager

use of org.robolectric.res.builder.RobolectricPackageManager in project materialistic by hidroh.

the class TestApplication method addResolver.

public static void addResolver(Intent intent) {
    RobolectricPackageManager packageManager = (RobolectricPackageManager) RuntimeEnvironment.application.getPackageManager();
    packageManager.addResolveInfoForIntent(intent, ShadowResolveInfo.newResolveInfo("label", "com.android.chrome", "DefaultActivity"));
}
Also used : RobolectricPackageManager(org.robolectric.res.builder.RobolectricPackageManager)

Aggregations

RobolectricPackageManager (org.robolectric.res.builder.RobolectricPackageManager)11 Test (org.junit.Test)7 Intent (android.content.Intent)5 SlowTest (io.github.hidroh.materialistic.test.suite.SlowTest)4 ApplicationInfo (android.content.pm.ApplicationInfo)3 TestWebItem (io.github.hidroh.materialistic.test.TestWebItem)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Before (org.junit.Before)2 SuppressLint (android.annotation.SuppressLint)1 ActivityInfo (android.content.pm.ActivityInfo)1 ResolveInfo (android.content.pm.ResolveInfo)1 Bundle (android.os.Bundle)1 Handler (android.os.Handler)1 NonNull (android.support.annotation.NonNull)1 ViewGroup (android.view.ViewGroup)1 WebView (android.webkit.WebView)1 ImageView (android.widget.ImageView)1 Lifecycle (com.bumptech.glide.manager.Lifecycle)1 RequestManagerTreeNode (com.bumptech.glide.manager.RequestManagerTreeNode)1 SizeReadyCallback (com.bumptech.glide.request.target.SizeReadyCallback)1