简体   繁体   English

Android如何测试方向变化

[英]Android how to test orientation change

I have an issue regarding unit testing android orientation shifts. 我有一个关于单元测试android方向转换的问题。 I have both Portrait and Landscape supported in my application and I have to test if the views hierarchy is correctly drawn when the orientation changes. 我在我的应用程序中支持Portrait和Landscape,我必须测试在方向更改时是否正确绘制了视图层次结构。

I have created two test methods to check this, and I have something like this: 我创建了两个测试方法来检查这个,我有这样的事情:

public void testOnCreate() throws Exception {
    //Check all the activity components
    assertNotNull(activity);
    assertNotNull(application);

    //Check if the rights components are available on the screen
    assertNotNull(LayoutInflater.from(activity));
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    testOrientationPortrait();
}

On this particular case the tests pass, and the view hierarchy is drawn correctly. 在这种特殊情况下,测试通过,并正确绘制视图层次结构。 But when I try to test the landscape using: 但是当我尝试使用以下方法测试景观时:

public void testOrientationChange() throws Exception {
    assertNotNull(activity);
    activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    //Check if the rights components are available on the screen
    assertNotNull(LayoutInflater.from(activity));
    testOrientationLandscape();
}

The orientation changes, but the view hierarchy fails, because views have the attributes from portrait. 方向更改,但视图层次结构失败,因为视图具有纵向属性。

Any ideas how to fix this? 任何想法如何解决这一问题?

Thanks, Ark 谢谢,方舟

Override this method and do your changes in the method: 重写此方法并在方法中进行更改:

@Override
 public void onConfigurationChanged(Configuration newConfig) {
  // TODO Auto-generated method stub
  super.onConfigurationChanged(newConfig);


 }

Do not forget to add 别忘了添加

 <activity

        android:configChanges="orientation"
        >
    </activity>

in your menifest. 在你的清单中。

And this can be used just to check the orientation . 这可以用来检查方向。 getResources().getConfiguration().orientation getResources()。getConfiguration()。方向

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM