Android Testing Splash Screen

天大地大妈咪最大 提交于 2019-12-11 10:42:55

问题


So I have this Splash screen which is working well, but I would like to test "is the handler has lunched the next activity". class:

public class SplashActivity extends Activity {
private final int SPLASH_DISPLAY_LENGTH = 3000;
private TextView quote_text;

private int[] quote_id = {R.string.quote_1, R.string.quote_2, R.string.quote_3, R.string.quote_4, R.string.quote_5};


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen);

    quote_text = (TextView) findViewById(R.id.quote_text);

    int idx = new Random().nextInt(quote_id.length);
    int selectedID = (quote_id[idx]);
    quote_text.setText(getResources().getText(selectedID));

    new Handler().postDelayed(new Runnable() {
         @Override
         public void run() {
             Intent mainIntent = new Intent(SplashActivity.this, MainActivity.class);
             SplashActivity.this.startActivity(mainIntent);
             SplashActivity.this.finish();
         }
     }, SPLASH_DISPLAY_LENGTH);
}  }

Robolectric: Test, which is fail at the
assertEquals(expectedIntent, shadowOf(activity).getNextStartedActivity());

  @Test
    public void testNextActivityWasLaunchedWithIntent() {
        SplashActivity activity =        Robolectric.buildActivity(SplashActivity.class).create().start().resume().get();
      assertNotNull("MainActivity is not instantiated", activity);

    synchronized (this)
    {
        try {
            this.wait(3200);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    Intent expectedIntent = new Intent(activity, MainActivity.class);
    assertNotNull(expectedIntent);

    assertEquals(expectedIntent,shadowOf(activity).getNextStartedActivity());
}

Can anyone tell me please, how can I test, is the handler fired my next activity ? Thank you very much!


回答1:


This is possible by some popular unit test framework that you can use to check to start the correct intent. Please see below documentation of Robolectric.

http://robolectric.org/writing-a-test/

Intent expectedIntent = new Intent(activity, WelcomeActivity.class);  
assertThat(shadowOf(activity).getNextStartedActivity()).isEqualTo(expectedIntent);



回答2:


I'm able to successfully test the splash screen in my app by doing the following:

public void test(){
ActivityController<SplashScreen> controller = Robolectric.buildActivity(SplashScreen.class).create().start();
ShadowLooper.runUiThreadTasksIncludingDelayedTasks();

SplashScreen splashScreenActivity = controller.get();
Intent expectedIntent = new Intent(splashScreenActivity, PrimeiroAcessoActivity.class);

assertEquals(expectedIntent,shadowOf(splashScreenActivity).getNextStartedActivity());}

It seems to me that you're missing the ShadowLooper.runUiThreadTasksIncludingDelayedTasks() line. It's needed in order to run the code inside Handler.postDelayed, as explained in this StackOverflow post.



来源:https://stackoverflow.com/questions/32070916/android-testing-splash-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!