gpt4 book ai didi

Android 测试启动画面

转载 作者:行者123 更新时间:2023-11-29 20:30:17 25 4
gpt4 key购买 nike

所以我有这个运行良好的初始屏幕,但我想测试“处理程序是否已启动下一个 Activity ”。类:

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

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

最佳答案

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

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

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

关于Android 测试启动画面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32070916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com