1package com.android.launcher3.ui;
2
3import android.content.SharedPreferences;
4import android.graphics.Rect;
5import android.support.test.uiautomator.UiDevice;
6import android.support.test.uiautomator.UiObject;
7import android.support.test.uiautomator.UiSelector;
8import android.test.suitebuilder.annotation.MediumTest;
9
10import com.android.launcher3.R;
11import com.android.launcher3.Utilities;
12
13/**
14 * Test for auto rotate preference.
15 */
16@MediumTest
17public class RotationPreferenceTest extends LauncherInstrumentationTestCase {
18
19    private SharedPreferences mPrefs;
20    private boolean mOriginalRotationValue;
21
22    @Override
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
34    protected void tearDown() throws Exception {
35        setRotationEnabled(mOriginalRotationValue);
36        super.tearDown();
37    }
38
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
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