Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.app.cts;
     18 
     19 import android.app.Activity;
     20 import android.app.Instrumentation;
     21 import android.content.pm.ActivityInfo;
     22 
     23 public class OrientationTestUtils {
     24 
     25     /**
     26      * Change the activity's orientation to something different and then switch back. This is used
     27      * to trigger {@link Activity#onConfigurationChanged(android.content.res.Configuration)}.
     28      *
     29      * @param activity whose orientation will be changed and restored
     30      */
     31     public static void toggleOrientation(Activity activity) {
     32         toggleOrientationSync(activity, null);
     33     }
     34 
     35     /**
     36      * Same as {@link #toggleOrientation(Activity)} except {@link Instrumentation#waitForIdleSync()}
     37      * is called after each orientation change.
     38      *
     39      * @param activity whose orientation will be changed and restored
     40      * @param instrumentation use for idle syncing
     41      */
     42     public static void toggleOrientationSync(final Activity activity,
     43             final Instrumentation instrumentation) {
     44         final int originalOrientation = activity.getResources().getConfiguration().orientation;
     45         final int newOrientation = originalOrientation == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
     46                 ? ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
     47                 : ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
     48         changeOrientation(activity, instrumentation, newOrientation);
     49         changeOrientation(activity, instrumentation, originalOrientation);
     50     }
     51 
     52     private static void changeOrientation(final Activity activity,
     53             Instrumentation instrumentation, final int orientation) {
     54         activity.setRequestedOrientation(orientation);
     55         if (instrumentation != null) {
     56             instrumentation.waitForIdleSync();
     57         }
     58     }
     59 }
     60