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
In Robolectric 3.0, they updated this API. The actual version:
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
Usage example:
public class MainActivity extends AppCompatActivity {
........
public void finishGame() {
Handler h = new Handler();
h.postDelayed(new Runnable() {
@Override
public void run() {
finish();
}
}, 5000);
}
}
Test arount it:
@RunWith(RobolectricTestRunner.class)
@Config(constants = BuildConfig.class, sdk=21)
public class MainActivityTest {
@Test
public void testFinishing() throws InterruptedException {
MainActivity mainActivity = new MainActivity();
assertFalse(mainActivity.isFinishing());
mainActivity.finishGame();
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
assertTrue(mainActivity.isFinishing());
}
}
In Robolectric 3.0, IuriiO's answer would translate to-
ShadowLooper.pauseMainLooper();
Robolectric.getForegroundThreadScheduler().advanceBy(intervalMs);
ShadowLooper.unPauseMainLooper();
You can try:
Robolectric.pauseMainLooper();
Robolectric.getUiThreadScheduler().advanceBy(intervalMs);
Robolectric.unPauseMainLooper();
or, if you just was the runnable to be called:
Robolectric.runUiThreadTasksIncludingDelayedTasks();