0

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

Aurelian Cotuna
  • 3,076
  • 3
  • 29
  • 49

1 Answers1

2

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

Payal
  • 903
  • 1
  • 9
  • 28
  • `configChanges` is used when you have the same layout for both of the orientation, because views don't need to be redrawn, but I have separate layouts for land and protrait, so I don't used `configChanges` at all. So this doesn't help me at all. – Aurelian Cotuna Jan 28 '13 at 13:35
  • 1
    This gives the orientation if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE){ Log.e("On Config Change","LANDSCAPE"); }else{ Log.e("On Config Change","PORTRAIT"); } – Payal Jan 28 '13 at 13:50
  • Yes I know. But as I said before, the orientation is changed, but the views have wrong attributes, because they are not redrawn for the new orientation. For instance I run the tests for the portrait, everything is ok, I run the tests for landscape, the orientation changes, but the views attributes remain the same. – Aurelian Cotuna Jan 28 '13 at 14:41