本文介绍了测试onActivityResult()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下活动:
package codeguru.startactivityforresult;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class StartActivityForResult extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.startButton = (Button) this.findViewById(R.id.start_button);
this.startButton.setOnClickListener(onStart);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
int result = data.getIntExtra(StartActivityForResult.this.getString(R.string.result), -1);
String msg = "requestCode=" + requestCode + ", resultCode=" + resultCode + ", result=" + result;
Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
}
private View.OnClickListener onStart = new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(StartActivityForResult.this, ChildActivity.class);
StartActivityForResult.this.startActivityForResult(intent, R.id.child_request);
}
};
private Button startButton = null;
}
和下面的JUnit测试:
And the following JUnit test:
package codeguru.startactivityforresult;
import android.app.Activity;
import android.app.Instrumentation;
import android.test.ActivityInstrumentationTestCase2;
import android.test.UiThreadTest;
import android.widget.Button;
import junit.framework.Assert;
public class StartActivityForResultTest extends ActivityInstrumentationTestCase2<StartActivityForResult> {
public StartActivityForResultTest() {
super(StartActivityForResult.class);
}
@Override
public void setUp() throws Exception {
super.setUp();
this.setActivityInitialTouchMode(false);
this.activity = this.getActivity();
this.startButton = (Button) this.activity.findViewById(R.id.start_button);
}
@Override
public void tearDown() throws Exception {
this.activity.finish();
super.tearDown();
}
@UiThreadTest
public void testStartButtonOnClick() {
Assert.assertTrue(this.startButton.performClick());
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK, null);
Assert.assertNotNull(result);
Instrumentation.ActivityMonitor am = new Instrumentation.ActivityMonitor(ChildActivity.class.getName(), result, true);
Assert.assertNotNull(am);
Activity childActivity = this.getInstrumentation().waitForMonitorWithTimeout(am, TIME_OUT);
Assert.assertNotNull(childActivity);
Assert.fail("How do I check that StartActivityForResult correctly handles the returned result?");
}
private Activity activity = null;
private Button startButton = null;
private static final int TIME_OUT = 5 * 1000; // 5 seconds
}
正如你所看到的,我想通了,如何使用小样结果 Instrumentation.ActivityResult
和 Instrumentation.ActivityMonitor
。我该如何检查 StartActivityForResult.onActivityResult()
正确处理这样的结果?
As you can see, I figured out how to mock up a result using Instrumentation.ActivityResult
and Instrumentation.ActivityMonitor
. How do I check that StartActivityForResult.onActivityResult()
properly handles this result?
推荐答案
有关测试onActivityResult()在你的测试类,所有你需要做的是:
For testing onActivityResult() in your test class, all you need to do is:
- 创建它捕捉ChildActivity的创建和重新调整模拟ActivityResult的活动监视器。
- 模拟按钮,点击它启动ChildActivity的结果。
- 请在状态和模拟ActivityResult一些说法。
样品StartActivityForResult:
Sample StartActivityForResult:
public class StartActivityForResult extends Activity {
private boolean activityResultIsReturned = false;
private String activityResult = null;
... ...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
activityResultIsReturned = true;
activityResult = data.getStringExtra("result");
... ...
}
... ...
}
样品StartActivityForResultTest:
Sample StartActivityForResultTest:
public class StartActivityForResultTest extends ActivityInstrumentationTestCase2<StartActivityForResult> {
... ...
public void testOnActivityResult() {
// Get current Activity and check initial status:
StartActivityForResult myActivity = getActivity();
assertFalse(myActivity.getActivityResultIsReturned());
assertNull(myActivity.getActiityResult());
// Mock up an ActivityResult:
Intent returnIntent = new Intent();
returnIntent.putExtra("result", "This is the result");
Instrumentation.ActivityResult activityResult = new Instrumentation.ActivityResult(Activity.RESULT_OK, returnIntent);
// Create an ActivityMonitor that catch ChildActivity and return mock ActivityResult:
Instrumentation.ActivityMonitor activityMonitor = getInstrumentation().addMonitor(ChildActivity.class.getName(), activityResult , true);
// Simulate a button click that start ChildActivity for result:
final Button button = (Button) myActivity.findViewById(com.company.R.id.open_next_activity);
myActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
// click button and open next activity.
button.performClick();
}
});
// Wait for the ActivityMonitor to be hit, Instrumentation will then return the mock ActivityResult:
ChildActivity childActivity = getInstrumentation().waitForMonitorWithTimeout(activityMonitor, 5);
// How do I check that StartActivityForResult correctly handles the returned result?
assertTrue(myActivity.getActivityResultIsReturned());
assertEqual(myActivity.getActiityResult(), "This is the result");
}
... ...
}
这篇关于测试onActivityResult()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!