mobile/android/base/tests/helpers/DeviceHelper.java

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial