Android测试启动画面

Android测试启动画面

本文介绍了Android测试启动画面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个运行良好的启动屏幕,但我想测试处理程序是否在下一个活动中吃过午餐".课:

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:测试,在
失败 assertEquals(expectedIntent,shadowOf(activity).getNextStartedActivity());

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!

推荐答案

通过执行以下操作,我可以在我的应用程序中成功测试启动屏幕:

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());}

在我看来,您错过了ShadowLooper.runUiThreadTasksIncludingDelayedTasks()行.如在此StackOverflow帖子中所述,需要在Handler.postDelayed内部运行代码.

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.

这篇关于Android测试启动画面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 00:18