|
1 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
2 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
4 |
|
5 package org.mozilla.gecko.tests.helpers; |
|
6 |
|
7 import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertTrue; |
|
8 |
|
9 import org.mozilla.gecko.GeckoAppShell; |
|
10 import org.mozilla.gecko.tests.UITestContext; |
|
11 |
|
12 import android.app.Activity; |
|
13 import android.os.Build; |
|
14 import android.util.DisplayMetrics; |
|
15 |
|
16 import com.jayway.android.robotium.solo.Solo; |
|
17 |
|
18 /** |
|
19 * Provides general hardware (ex: configuration) and software (ex: version) information |
|
20 * about the current test device and allows changing its configuration. |
|
21 */ |
|
22 public final class DeviceHelper { |
|
23 public enum Type { |
|
24 PHONE, |
|
25 TABLET |
|
26 } |
|
27 |
|
28 public enum AndroidVersion { |
|
29 v2x, |
|
30 v3x, |
|
31 v4x |
|
32 } |
|
33 |
|
34 private static Activity sActivity; |
|
35 private static Solo sSolo; |
|
36 |
|
37 private static Type sDeviceType; |
|
38 private static AndroidVersion sAndroidVersion; |
|
39 |
|
40 private static int sScreenHeight; |
|
41 private static int sScreenWidth; |
|
42 |
|
43 private DeviceHelper() { /* To disallow instantiation. */ } |
|
44 |
|
45 public static void assertIsTablet() { |
|
46 fAssertTrue("The device is a tablet", isTablet()); |
|
47 } |
|
48 |
|
49 protected static void init(final UITestContext context) { |
|
50 sActivity = context.getActivity(); |
|
51 sSolo = context.getSolo(); |
|
52 |
|
53 setAndroidVersion(); |
|
54 setScreenDimensions(); |
|
55 setDeviceType(); |
|
56 } |
|
57 |
|
58 private static void setAndroidVersion() { |
|
59 int sdk = Build.VERSION.SDK_INT; |
|
60 if (sdk < Build.VERSION_CODES.HONEYCOMB) { |
|
61 sAndroidVersion = AndroidVersion.v2x; |
|
62 } else if (sdk > Build.VERSION_CODES.HONEYCOMB_MR2) { |
|
63 sAndroidVersion = AndroidVersion.v4x; |
|
64 } else { |
|
65 sAndroidVersion = AndroidVersion.v3x; |
|
66 } |
|
67 } |
|
68 |
|
69 private static void setScreenDimensions() { |
|
70 final DisplayMetrics dm = new DisplayMetrics(); |
|
71 sActivity.getWindowManager().getDefaultDisplay().getMetrics(dm); |
|
72 |
|
73 sScreenHeight = dm.heightPixels; |
|
74 sScreenWidth = dm.widthPixels; |
|
75 } |
|
76 |
|
77 private static void setDeviceType() { |
|
78 sDeviceType = (GeckoAppShell.isTablet() ? Type.TABLET : Type.PHONE); |
|
79 } |
|
80 |
|
81 public static int getScreenHeight() { |
|
82 return sScreenHeight; |
|
83 } |
|
84 |
|
85 public static int getScreenWidth() { |
|
86 return sScreenWidth; |
|
87 } |
|
88 |
|
89 public static AndroidVersion getAndroidVersion() { |
|
90 return sAndroidVersion; |
|
91 } |
|
92 |
|
93 public static boolean isPhone() { |
|
94 return (sDeviceType == Type.PHONE); |
|
95 } |
|
96 |
|
97 public static boolean isTablet() { |
|
98 return (sDeviceType == Type.TABLET); |
|
99 } |
|
100 |
|
101 public static void setLandscapeRotation() { |
|
102 sSolo.setActivityOrientation(Solo.LANDSCAPE); |
|
103 } |
|
104 |
|
105 public static void setPortraitOrientation() { |
|
106 sSolo.setActivityOrientation(Solo.LANDSCAPE); |
|
107 } |
|
108 } |