Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
      5  * use this file except in compliance with the License. You may obtain a copy of
      6  * 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, WITHOUT
     12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     13  * License for the specific language governing permissions and limitations under
     14  * the License.
     15  */
     16 package android.app.cts;
     17 
     18 import android.app.UiAutomation;
     19 import android.content.Context;
     20 import android.content.pm.PackageManager;
     21 import android.content.res.Configuration;
     22 import android.support.test.filters.SmallTest;
     23 import android.test.ActivityInstrumentationTestCase2;
     24 
     25 import java.util.concurrent.Future;
     26 
     27 /**
     28  * Tests the {@link android.view.ContextThemeWrapper#applyOverrideConfiguration(Configuration)}
     29  * method and how it affects the Activity's resources and lifecycle callbacks.
     30  */
     31 public class ApplyOverrideConfigurationTest extends
     32         ActivityInstrumentationTestCase2<ApplyOverrideConfigurationActivity> {
     33     public ApplyOverrideConfigurationTest() {
     34         super(ApplyOverrideConfigurationActivity.class);
     35     }
     36 
     37     @Override
     38     public void setUp() throws Exception {
     39         super.setUp();
     40         getInstrumentation().getUiAutomation().setRotation(UiAutomation.ROTATION_FREEZE_0);
     41     }
     42 
     43     @Override
     44     public void tearDown() throws Exception {
     45         getInstrumentation().getUiAutomation().setRotation(UiAutomation.ROTATION_UNFREEZE);
     46         super.tearDown();
     47     }
     48 
     49     @SmallTest
     50     public void testOverriddenConfigurationIsPassedIntoCallback() throws Exception {
     51         // This test instruments display rotation; disable it on devices that do not
     52         // support auto-rotation.
     53         if (!isRotationSupported(getActivity())) {
     54             return;
     55         }
     56         final Configuration config = getActivity().getResources().getConfiguration();
     57         final int originalOrientation = config.orientation;
     58         assertEquals(ApplyOverrideConfigurationActivity.OVERRIDE_SMALLEST_WIDTH,
     59                 config.smallestScreenWidthDp);
     60 
     61         Future<Configuration> callback =
     62                 getActivity().watchForSingleOnConfigurationChangedCallback();
     63 
     64         getInstrumentation().getUiAutomation().setRotation(UiAutomation.ROTATION_FREEZE_90);
     65 
     66         final Configuration callbackConfig = callback.get();
     67         assertNotNull(callbackConfig);
     68 
     69         final Configuration newConfig = getActivity().getResources().getConfiguration();
     70         assertTrue(newConfig.orientation != originalOrientation);
     71         assertEquals(ApplyOverrideConfigurationActivity.OVERRIDE_SMALLEST_WIDTH,
     72                 newConfig.smallestScreenWidthDp);
     73 
     74         assertEquals(newConfig, callbackConfig);
     75     }
     76 
     77     /**
     78      * Gets whether the device supports rotation. In general such a
     79      * device has both portrait and landscape features.
     80      *
     81      * @param context Context for accessing system resources.
     82      * @return Whether the device supports rotation.
     83      */
     84     public static boolean isRotationSupported(Context context) {
     85         PackageManager pm = context.getPackageManager();
     86         return pm.hasSystemFeature(PackageManager.FEATURE_SCREEN_PORTRAIT)
     87                 && pm.hasSystemFeature(PackageManager.FEATURE_SCREEN_LANDSCAPE)
     88                 && !isVrHeadset(context);
     89     }
     90 
     91     /**
     92      * Gets whether the DUT is a vr headset.
     93      *
     94      * @param context Context for accessing system resources.
     95      * @return Whether the device is a vr headset.
     96      */
     97     public static boolean isVrHeadset(Context context) {
     98         return (context.getResources().getConfiguration().uiMode
     99                 & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_VR_HEADSET;
    100     }
    101 }
    102