Android Testing Handler.postDelayed

456 views
Skip to first unread message

M. Rabie Hayoun Ben Maimoun

unread,
Jul 8, 2014, 6:34:40 PM7/8/14
to robol...@googlegroups.com

I'm newbie on Android and get stuck on testing a SplashScreen, basically what I'm doing is trying to test that the splash screen stays on for 3s. this is the code for the splashScreen

@Override
protected void onStart() {
    super.onStart();

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent i = new Intent(SplashScreenActivity.this, MainActivity.class);
            startActivity(i);
            finish();
        }
    }, SPLASH_TIME_OUT);
}

And this is my approach for the test:

@Test
public void splashScreenTimerTest() throws InterruptedException {
    StopWatch timer = new StopWatch();
    timer.start();

    splashScreenActivityController.start();

    timer.stop();
    long expected = 3000L;
    assertThat(String.format("The spalash screen run for longer than expected. It should stay on for [%d] but it run for [%d]",expected,timer.getTime()),timer.getTime(),equalTo(expected));
}

I'm using Android+Robolectric for testing.

I've been googling for a couple days, I tried many things but no result at all. I try to get the UI Thread and wait for it. I tried to you scheduler to get the queue and see if there is any task there. But no result at all.

Any suggestion how can I make my test wait until the new Activity is triggered?

Thanks

Thomas Keller

unread,
Jul 9, 2014, 4:13:33 PM7/9/14
to robol...@googlegroups.com
I suspect ShadowLooper.idleConstantly(true); could help here. Whenever you post something then via a Handler that runs on the main thread, it should get executed immediately.

Thomas.

M. Rabie Hayoun Ben Maimoun

unread,
Jul 10, 2014, 2:39:02 PM7/10/14
to robol...@googlegroups.com
Thanks for the tip but I'd already tried it. Didn't work.
Reply all
Reply to author
Forward
0 new messages