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.

     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/. */
     5 package org.mozilla.gecko.tests.helpers;
     7 import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertTrue;
     9 import org.mozilla.gecko.GeckoAppShell;
    10 import org.mozilla.gecko.tests.UITestContext;
    12 import android.app.Activity;
    13 import android.os.Build;
    14 import android.util.DisplayMetrics;
    16 import com.jayway.android.robotium.solo.Solo;
    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     }
    28     public enum AndroidVersion {
    29         v2x,
    30         v3x,
    31         v4x
    32     }
    34     private static Activity sActivity;
    35     private static Solo sSolo;
    37     private static Type sDeviceType;
    38     private static AndroidVersion sAndroidVersion;
    40     private static int sScreenHeight;
    41     private static int sScreenWidth;
    43     private DeviceHelper() { /* To disallow instantiation. */ }
    45     public static void assertIsTablet() {
    46         fAssertTrue("The device is a tablet", isTablet());
    47     }
    49     protected static void init(final UITestContext context) {
    50         sActivity = context.getActivity();
    51         sSolo = context.getSolo();
    53         setAndroidVersion();
    54         setScreenDimensions();
    55         setDeviceType();
    56     }
    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     }
    69     private static void setScreenDimensions() {
    70         final DisplayMetrics dm = new DisplayMetrics();
    71         sActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    73         sScreenHeight = dm.heightPixels;
    74         sScreenWidth = dm.widthPixels;
    75     }
    77     private static void setDeviceType() {
    78         sDeviceType = (GeckoAppShell.isTablet() ? Type.TABLET : Type.PHONE);
    79     }
    81     public static int getScreenHeight() {
    82         return sScreenHeight;
    83     }
    85     public static int getScreenWidth() {
    86         return sScreenWidth;
    87     }
    89     public static AndroidVersion getAndroidVersion() {
    90         return sAndroidVersion;
    91     }
    93     public static boolean isPhone() {
    94         return (sDeviceType == Type.PHONE);
    95     }
    97     public static boolean isTablet() {
    98         return (sDeviceType == Type.TABLET);
    99     }
   101     public static void setLandscapeRotation() {
   102         sSolo.setActivityOrientation(Solo.LANDSCAPE);
   103     }
   105     public static void setPortraitOrientation() {
   106         sSolo.setActivityOrientation(Solo.LANDSCAPE);
   107     }
   108 }

mercurial