• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.launcher3;
2 
3 import android.content.Context;
4 import android.content.Intent;
5 import android.content.SharedPreferences;
6 import android.graphics.Rect;
7 import android.support.test.uiautomator.UiDevice;
8 import android.support.test.uiautomator.UiObject;
9 import android.support.test.uiautomator.UiSelector;
10 import android.test.InstrumentationTestCase;
11 
12 /**
13  * Test for auto rotate preference.
14  */
15 public class RotationPreferenceTest extends InstrumentationTestCase {
16 
17     private UiDevice mDevice;
18     private Context mTargetContext;
19     private String mTargetPackage;
20 
21     private SharedPreferences mPrefs;
22     private boolean mOriginalRotationValue;
23 
24     @Override
setUp()25     protected void setUp() throws Exception {
26         super.setUp();
27 
28         mDevice = UiDevice.getInstance(getInstrumentation());
29         mTargetContext = getInstrumentation().getTargetContext();
30         mTargetPackage = mTargetContext.getPackageName();
31         mPrefs = Utilities.getPrefs(mTargetContext);
32         mOriginalRotationValue = mPrefs.getBoolean(Utilities.ALLOW_ROTATION_PREFERENCE_KEY, false);
33     }
34 
35     @Override
tearDown()36     protected void tearDown() throws Exception {
37         setRotationEnabled(mOriginalRotationValue);
38         super.tearDown();
39     }
40 
testRotation_disabled()41     public void testRotation_disabled() throws Exception {
42         if (mTargetContext.getResources().getBoolean(R.bool.allow_rotation)) {
43             // This is a tablet. The test is only valid to mobile devices.
44             return;
45         }
46 
47         setRotationEnabled(false);
48         mDevice.setOrientationRight();
49         goToLauncher();
50 
51         Rect hotseat = getHotseatBounds();
52         assertTrue(hotseat.width() > hotseat.height());
53     }
54 
testRotation_enabled()55     public void testRotation_enabled() throws Exception {
56         if (mTargetContext.getResources().getBoolean(R.bool.allow_rotation)) {
57             // This is a tablet. The test is only valid to mobile devices.
58             return;
59         }
60 
61         setRotationEnabled(true);
62         mDevice.setOrientationRight();
63         goToLauncher();
64 
65         Rect hotseat = getHotseatBounds();
66         assertTrue(hotseat.width() < hotseat.height());
67     }
68 
69     private void goToLauncher() {
70         Intent homeIntent = new Intent(Intent.ACTION_MAIN)
71                 .addCategory(Intent.CATEGORY_HOME)
72                 .setPackage(mTargetPackage)
73                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
74         getInstrumentation().getContext().startActivity(homeIntent);
75         mDevice.findObject(new UiSelector().packageName(mTargetPackage)).waitForExists(6000);
76     }
77 
78     private void setRotationEnabled(boolean enabled) {
79         mPrefs.edit().putBoolean(Utilities.ALLOW_ROTATION_PREFERENCE_KEY, enabled).commit();
80     }
81 
82     private Rect getHotseatBounds() throws Exception {
83         UiObject hotseat = mDevice.findObject(
84                 new UiSelector().resourceId(mTargetPackage + ":id/hotseat"));
85         hotseat.waitForExists(6000);
86         return hotseat.getVisibleBounds();
87     }
88 }
89