【问题标题】:Android Testing Splash ScreenAndroid 测试启动画面
【发布时间】:2015-08-18 11:17:52
【问题描述】:

所以我有这个运行良好的启动画面,但我想测试“处理程序是否已经为下一个活动提供午餐”。 类:

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

机器人电动: 测试,在
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());
}

谁能告诉我,我该如何测试,处理程序是否触发了我的下一个活动? 非常感谢!

【问题讨论】:

    标签: android functional-testing robolectric


    【解决方案1】:

    这可以通过一些流行的单元测试框架来实现,您可以使用它来检查以启动正确的意图。请参阅下面的 Robolectric 文档。

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

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

    【讨论】:

    • 嘿 Durgesh,我已经更新了我的答案,请回顾一下。谢谢
    【解决方案2】:

    通过执行以下操作,我能够成功地在我的应用中测试启动画面:

    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() 行。它是运行 Handler.postDelayed 中的代码所必需的,如 this StackOverflow post 中所述。

    【讨论】:

      猜你喜欢
      • 2019-07-19
      • 1970-01-01
      • 2021-12-26
      • 1970-01-01
      • 2015-06-21
      • 2020-06-15
      • 2020-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多