Android的仪器测试 - 如何定位变更后接收新的活动? [英] Instrumentation test for Android - How to receive new Activity after orientation change?

查看:117
本文介绍了Android的仪器测试 - 如何定位变更后接收新的活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果
我想测试一下,如果新创建的活动(方向变更后)正确初始化。下面的code则显示,从活动getActivity(返回)是一个在设置(构造),而不是新创建的。

测试:


I'm trying to test, if newly created Activity (after orientation change) is properly reinitialized. The code below shows that activity returned from getActivity() is the one constructed in setUp(), not the newly created one.

Test:

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:


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()返回旧活动,在安装程序创建()(MyActivity @ 44eb8690)。是否有可能访问新创建的?


As mentioned before, logs shows that new activity is indeed created (MyActivity@44ee5178), but getActivity() return the old activity, created in setUp() (MyActivity@44eb8690). Is it possible to access newly created one?

推荐答案

好吧,我想我终于设法解决这个问题 - 与 robotium 框架。我附上万一有人解决了同样的问题。结果

Ok, I think I finally managed to solve it - with robotium framework. I'm attaching the solution in case someone had the same problem.

测试:

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

}

和日志信息 - 确认:​​

And log messages - for confirmation:

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 - 另一个伟大的一块code从Jayway

As you can see, activity returned from mSolo.getCurrentActivity() is the same that was created after orientation change. I really recommend Robotium - another great piece of code from Jayway!

这篇关于Android的仪器测试 - 如何定位变更后接收新的活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆