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