Java Code Examples for org.robolectric.shadows.ShadowLooper#pauseMainLooper()

The following examples show how to use org.robolectric.shadows.ShadowLooper#pauseMainLooper() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ChoreographerCompatTest.java    From litho with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreationFromMainThread() {
  ShadowLooper.pauseMainLooper();

  final AtomicBoolean firstCallback = new AtomicBoolean(false);
  final ChoreographerCompatImpl choreographerCompat = new ChoreographerCompatImpl();
  assertThat(choreographerCompat.isUsingChoreographer()).isTrue();
  new ChoreographerCompatImpl()
      .postFrameCallback(
          new FrameCallback() {
            @Override
            public void doFrame(long frameTimeNanos) {
              firstCallback.set(true);
            }
          });

  assertThat(firstCallback.get()).isFalse();

  ShadowLooper.runUiThreadTasks();

  assertThat(firstCallback.get()).isTrue();
}
 
Example 2
Source File: HandlerExecutorServiceImplTest.java    From fresco with MIT License 5 votes vote down vote up
@Test
public void testSimpleExecute() {
  ShadowLooper.pauseMainLooper();
  mExecutorService.execute(mIncrementCounterRunnable);
  Assert.assertEquals(0, mCounter.get());
  ShadowLooper.unPauseMainLooper();
  Assert.assertEquals(1, mCounter.get());
}
 
Example 3
Source File: ItemActivityTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@SuppressLint("NewApi")
@Test
public void testOptionExternal() {
    ShadowPackageManager packageManager = shadowOf(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 = Robolectric.buildActivity(ItemActivity.class, intent);
    controller.create().start().resume();
    activity = controller.get();

    // 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);
}
 
Example 4
Source File: ItemActivityTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@SuppressLint("NewApi")
@Test
public void testShare() {
    TestApplication.addResolver(new Intent(Intent.ACTION_SEND));
    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 = Robolectric.buildActivity(ItemActivity.class, intent);
    controller.create().start().resume();
    activity = controller.get();

    // inflate menu, see https://github.com/robolectric/robolectric/issues/1326
    ShadowLooper.pauseMainLooper();
    controller.visible();
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();

    // share article
    shadowOf(activity).clickMenuItem(R.id.menu_share);
    shadowOf(ShadowPopupMenu.getLatestPopupMenu())
            .getOnMenuItemClickListener()
            .onMenuItemClick(new RoboMenuItem(R.id.menu_article));
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    Intent actual = shadowOf(activity).getNextStartedActivity();
    assertThat(actual)
            .hasAction(Intent.ACTION_SEND);

    // share item
    shadowOf(activity).clickMenuItem(R.id.menu_share);
    shadowOf(ShadowPopupMenu.getLatestPopupMenu())
            .getOnMenuItemClickListener()
            .onMenuItemClick(new RoboMenuItem(R.id.menu_comments));
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    actual = shadowOf(activity).getNextStartedActivity();
    assertThat(actual)
            .hasAction(Intent.ACTION_SEND);
}
 
Example 5
Source File: ItemActivityTest.java    From materialistic with Apache License 2.0 4 votes vote down vote up
@Config(shadows = ShadowRecyclerView.class)
@Test
public void testScrollToTop() {
    Intent intent = new Intent();
    intent.putExtra(ItemActivity.EXTRA_ITEM, new TestItem() {
        @NonNull
        @Override
        public String getType() {
            return STORY_TYPE;
        }

        @Override
        public String getId() {
            return "1";
        }

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

        @Override
        public int getKidCount() {
            return 10;
        }

        @Override
        public String getUrl() {
            return "http://example.com";
        }
    });
    controller = Robolectric.buildActivity(ItemActivity.class, intent);
    controller.create().start().resume();
    activity = controller.get();
    // see https://github.com/robolectric/robolectric/issues/1326
    ShadowLooper.pauseMainLooper();
    controller.visible();
    ShadowApplication.getInstance().getForegroundThreadScheduler().advanceToLastPostedRunnable();
    RecyclerView recyclerView = activity.findViewById(R.id.recycler_view);
    recyclerView.smoothScrollToPosition(1);
    assertThat(customShadowOf(recyclerView).getScrollPosition()).isEqualTo(1);
    TabLayout tabLayout = activity.findViewById(R.id.tab_layout);
    assertThat(tabLayout.getTabCount()).isEqualTo(2);
    tabLayout.getTabAt(1).select();
    tabLayout.getTabAt(0).select();
    tabLayout.getTabAt(0).select();
    assertThat(customShadowOf(recyclerView).getScrollPosition()).isEqualTo(0);
}