简体   繁体   中英

Android how to test orientation change

I have an issue regarding unit testing android orientation shifts. 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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