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