gpt4 book ai didi

java - 从 @UiThreadTest 调用 waitForMonitorWithTimeout()

转载 作者:行者123 更新时间:2023-11-30 03:57:39 27 4
gpt4 key购买 nike

为了说明我在为我的 Android 应用程序编写 JUnit 测试时遇到的最新问题,我编写了一个包含两个 Activity 的简单示例,StartActivityForResultChildActivity。前者包含一个 TextView(用于显示目的)和一个 Button,而后者仅包含一个 ButtonStartActivityForResult 中按钮的 onClickListener 只是启动 ChildActivity 的一个实例。

private View.OnClickListener onStart = new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.d(TAG, "Start button clicked");
Intent intent = new Intent(StartActivityForResult.this, ChildActivity.class);

StartActivityForResult.this.startActivityForResult(intent, R.id.child_request);
}
};

现在我想使用 JUnit 测试这个方法。所以我写了下面的测试:

package codeguru.startactivityforresult;

import android.app.Activity;
import android.app.Instrumentation;
import android.content.Intent;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.util.Log;
import android.widget.Button;
import android.widget.TextView;
import junit.framework.Assert;

public class StartActivityForResultTest extends ActivityInstrumentationTestCase2<StartActivityForResult> {

public StartActivityForResultTest() {
super(StartActivityForResult.class);
}

@Override
public void setUp() throws Exception {
super.setUp();

Log.d(TAG, "setUp()");

this.setActivityInitialTouchMode(false);

this.activity = this.getActivity();
this.resultText = (TextView) this.activity.findViewById(R.id.result_text);
this.startButton = (Button) this.activity.findViewById(R.id.start_button);

Intent data = new Intent();
data.putExtra(this.activity.getString(R.string.result), RESULT);
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, data);

this.childMonitor = new Instrumentation.ActivityMonitor(ChildActivity.class.getName(), result, true);
this.getInstrumentation().addMonitor(this.childMonitor);
}

@Override
public void tearDown() throws Exception {
this.activity.finish();

super.tearDown();
}

@UiThreadTest
public void testStartButtonOnClick() {
Assert.assertTrue(this.startButton.performClick());

Activity childActivity = this.getInstrumentation().waitForMonitorWithTimeout(this.childMonitor, TIME_OUT);
Assert.assertNotNull(childActivity); // <------ Line 51

Button resultButton = (Button) childActivity.findViewById(R.id.result_button);
Assert.assertTrue(resultButton.performClick());

Assert.assertEquals(Integer.toString(RESULT), this.resultText.getText().toString());
}
private Activity activity = null;
private TextView resultText = null;
private Button startButton = null;
private Instrumentation.ActivityMonitor childMonitor = null;
private static final int TIME_OUT = 5 * 1000; // 5 seconds
private static final int RESULT = 69;
private static final String TAG = StartActivityForResultTest.class.getName();
}

运行此测试会产生以下输出:

codeguru@trolloc:~/src/java/stackoverflow/sscce/StartActivityForResult/test$ adb logcat -c
codeguru@trolloc:~/src/java/stackoverflow/sscce/StartActivityForResult/test$ adb shell am instrument -w -e class codeguru.startactivityforresult.StartActivityForResultTest codeguru.startactivityforresult.tests/android.test.InstrumentationTestRunner

codeguru.startactivityforresult.StartActivityForResultTest:
Failure in testStartButtonOnClick:
junit.framework.AssertionFailedError
at codeguru.startactivityforresult.StartActivityForResultTest.testStartButtonOnClick(StartActivityForResultTest.java:51)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:214)
at android.test.InstrumentationTestCase.access$000(InstrumentationTestCase.java:36)
at android.test.InstrumentationTestCase$2.run(InstrumentationTestCase.java:189)
at android.app.Instrumentation$SyncRunnable.run(Instrumentation.java:1602)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)

Test results for InstrumentationTestRunner=.F
Time: 3.248

FAILURES!!!
Tests run: 1, Failures: 1, Errors: 0


codeguru@trolloc:~/src/java/stackoverflow/sscce/StartActivityForResult/test$ adb logcat -d codeguru.startactivityforresult.StartActivityForResultTest:D codeguru.startactivityforresult.StartActivityForResult:D codeguru.startactivityforresult.ChildActivity:D *:S
D/codeguru.startactivityforresult.StartActivityForResultTest( 954): setUp()
D/codeguru.startactivityforresult.StartActivityForResult( 954): onCreate()
D/codeguru.startactivityforresult.StartActivityForResult( 954): Start button clicked
layne@trolloc:~/src/java/stackoverflow/sscce/StartActivityForResult/test$

据我所知,调用 startButton.performClick() 不会启动 ChildActivity 的实例。给了什么?

最佳答案

an answer to one of my related questions 中所示,问题是我在 UI 线程上调用 waitForMonitorWithTimeout()。意识到这一点后,它当然完全有意义,因为 waitForMonitorWithTimeout() 等待 UI 线程完成某些操作(即显示 Activity 的 UI)。但是,通过在 UI 线程上调用它,我延迟了该操作的发生。

关于java - 从 @UiThreadTest 调用 waitForMonitorWithTimeout(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13105202/

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