我正在尝试测试,如果新创建的Activity(在方向更改后)正确地重新初始化。下面的代码显示从getActivity()返回的活动是在setUp()中构造的活动,而不是新创建的活动。
测试:
public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity>{
private static final String TAG = "RAMPS";
private MyActivity mActivity;
public MyActivityTest() {
super("com.ramps", MyActivity.class);
}
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
Log.v(TAG, "setUp; activity=" + mActivity);
}
public void testOrienationChange(){
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getInstrumentation().waitForIdleSync();
MyActivity newActivity = getActivity(); //should be new, but it's not
Log.v(TAG, "testOrienationChange; activity=" + newActivity);
}
}
Activiy:
public class MyActivity extends Activity {
private static final String TAG = "RAMPS";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(TAG, "onCreate; activity=" + this);
setContentView(new TextView(this));
}
//...rest of stuff like onSaveInstanceState() etc.
}
和日志:
06-11 14:16:52.431: V/RAMPS(367): onCreate; activity=MyActivity@44eb8690
06-11 14:16:52.891: V/RAMPS(367): setUp; activity=MyActivity@44eb8690
06-11 14:16:52.971: V/RAMPS(367): onCreate; activity=MyActivity@44ee5178
06-11 14:16:53.131: V/RAMPS(367): testOrienationChange; activity=MyActivity@44eb8690
如前所述,日志显示确实创建了新活动(MyActivity @ 44ee5178),但getActivity()返回在setUp()(MyActivity @ 44eb8690)中创建的旧活动。是否可以访问新创建的?
答案 0 :(得分:5)
好吧,我想我终于设法解决了它 - 使用robotium框架。如果有人遇到同样的问题,我会附上解决方案。
测试:
public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity>{
private static final String TAG = "RAMPS";
private MyActivity mActivity;
private Solo mSolo;
public MyActivityTest() {
super("com.ramps", MyActivity.class);
}
@Override
protected void setUp() throws Exception {
super.setUp();
mActivity = getActivity();
mSolo = new Solo(getInstrumentation(), getActivity());
Log.v(TAG, "setUp; activity=" + mActivity);
}
public void testOrienationChange(){
mSolo.setActivityOrientation(Solo.LANDSCAPE);
getInstrumentation().waitForIdleSync();
MyActivity newActivity = getActivity(); //should be new, but it's not
Activity newActivity2 = mSolo.getCurrentActivity(); //this will return new activity
Log.v(TAG, "testOrienationChange; activity=" + newActivity);
Log.v(TAG, "testOrienationChange; activity2=" + newActivity2);
}
}
并记录消息 - 以进行确认:
06-11 18:47:02.631: V/RAMPS(716): onCreate; activity=MyActivity@44c326a8
06-11 18:47:03.061: V/RAMPS(716): setUp; activity=MyActivity@44c326a8
06-11 18:47:03.781: V/RAMPS(716): onCreate; activity=MyActivity@44c481e0
06-11 18:47:04.482: V/RAMPS(716): testOrienationChange; activity=MyActivity@44c326a8
06-11 18:47:04.482: V/RAMPS(716): testOrienationChange; activity2=MyActivity@44c481e0
如您所见,从mSolo.getCurrentActivity()返回的活动与在方向更改后创建的活动相同。我真的推荐Robotium - 来自Jayway的另一个很棒的代码!
答案 1 :(得分:4)
我的UI测试中遇到了同样的问题。我不使用Robotium,但是我创建了一个新的基础测试类,getActivity()
始终返回当前活动。我们的想法是在方向更改之前添加ActivityMonitor
,并在监视器等待时更新当前活动:
public class UiTest<T extends Activity> extends ActivityInstrumentationTestCase2<T> {
private final Class<T> activityClass;
protected T activity;
public UiTest(Class<T> activityClass) {
super(activityClass);
this.activityClass = activityClass;
}
@Override
protected void setUp() throws Exception {
super.setUp();
setActivityInitialTouchMode(false); // Depends on your needs.
activity = super.getActivity();
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
/**
* Rotates the test device and updates current activity.
*/
protected final void rotate() {
int nextOrientation =
activity.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
: ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
Instrumentation.ActivityMonitor monitor =
new Instrumentation.ActivityMonitor(activityClass.getName(), null, false);
getInstrumentation().addMonitor(monitor);
activity.setRequestedOrientation(nextOrientation);
getInstrumentation().waitForIdleSync();
this.activity = (T) getInstrumentation().waitForMonitor(monitor);
}
@Override
public T getActivity() {
return activity;
}
当然,如果活动类在测试期间发生变化,或者方向通过其他方式而不是rotate()
方法更改,则无效。我希望这有助于某人。
答案 2 :(得分:0)
而不是
MyActivity newActivity = getActivity(); //should be new, but it's not
尝试
MyActivity newActivity = instrumentation.startActivitySync(intent);
我认为这种方法可能有助于你
答案 3 :(得分:0)
取了@Smok的答案并将其更新为始终旋转:
@SuppressWarnings("unchecked") // it's fine
public static <T extends Activity> T rotate(ActivityInstrumentationTestCase2<T> testCase) {
T activity = testCase.getActivity();
int orientation = activity.getWindowManager().getDefaultDisplay().getRotation();
int nextOrientation = orientation == Configuration.ORIENTATION_LANDSCAPE ? ActivityInfo.SCREEN_ORIENTATION_PORTRAIT : ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
Instrumentation.ActivityMonitor monitor = new Instrumentation.ActivityMonitor(activity.getClass().getName(), null, false);
testCase.getInstrumentation().addMonitor(monitor);
activity.setRequestedOrientation(nextOrientation);
testCase.getInstrumentation().waitForIdleSync();
return (T) testCase.getInstrumentation().waitForMonitor(monitor);
}
这解决了返回UNSPECIFIED
答案 4 :(得分:0)
这解决了我的问题,仅使用Android测试框架:
mActivity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
mActivity.finish();
setActivity(null);
mActivity = getActivity();
getInstrumentation().waitForIdleSync();
AFAIK,getActivity()
确实创建了一个新活动,如果当前的活动为空,则通过设置setActivity(null)
来完成。此外,新活动的方向也会设置为Activity.setRequestedOrientation(int)
。
如果您需要检查方向更改时是否正确保存状态,则应从UI线程中调用getInstrumentation().callActivityOnCreate(mActivity, Bundle)
。