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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/base/tests/helpers/DeviceHelper.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,108 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +package org.mozilla.gecko.tests.helpers;
     1.9 +
    1.10 +import static org.mozilla.gecko.tests.helpers.AssertionHelper.fAssertTrue;
    1.11 +
    1.12 +import org.mozilla.gecko.GeckoAppShell;
    1.13 +import org.mozilla.gecko.tests.UITestContext;
    1.14 +
    1.15 +import android.app.Activity;
    1.16 +import android.os.Build;
    1.17 +import android.util.DisplayMetrics;
    1.18 +
    1.19 +import com.jayway.android.robotium.solo.Solo;
    1.20 +
    1.21 +/**
    1.22 + * Provides general hardware (ex: configuration) and software (ex: version) information
    1.23 + * about the current test device and allows changing its configuration.
    1.24 + */
    1.25 +public final class DeviceHelper {
    1.26 +    public enum Type {
    1.27 +        PHONE,
    1.28 +        TABLET
    1.29 +    }
    1.30 +
    1.31 +    public enum AndroidVersion {
    1.32 +        v2x,
    1.33 +        v3x,
    1.34 +        v4x
    1.35 +    }
    1.36 +
    1.37 +    private static Activity sActivity;
    1.38 +    private static Solo sSolo;
    1.39 +
    1.40 +    private static Type sDeviceType;
    1.41 +    private static AndroidVersion sAndroidVersion;
    1.42 +
    1.43 +    private static int sScreenHeight;
    1.44 +    private static int sScreenWidth;
    1.45 +
    1.46 +    private DeviceHelper() { /* To disallow instantiation. */ }
    1.47 +
    1.48 +    public static void assertIsTablet() {
    1.49 +        fAssertTrue("The device is a tablet", isTablet());
    1.50 +    }
    1.51 +
    1.52 +    protected static void init(final UITestContext context) {
    1.53 +        sActivity = context.getActivity();
    1.54 +        sSolo = context.getSolo();
    1.55 +
    1.56 +        setAndroidVersion();
    1.57 +        setScreenDimensions();
    1.58 +        setDeviceType();
    1.59 +    }
    1.60 +
    1.61 +    private static void setAndroidVersion() {
    1.62 +        int sdk = Build.VERSION.SDK_INT;
    1.63 +        if (sdk < Build.VERSION_CODES.HONEYCOMB) {
    1.64 +            sAndroidVersion = AndroidVersion.v2x;
    1.65 +        } else if (sdk > Build.VERSION_CODES.HONEYCOMB_MR2) {
    1.66 +            sAndroidVersion = AndroidVersion.v4x;
    1.67 +        } else {
    1.68 +            sAndroidVersion = AndroidVersion.v3x;
    1.69 +        }
    1.70 +    }
    1.71 +
    1.72 +    private static void setScreenDimensions() {
    1.73 +        final DisplayMetrics dm = new DisplayMetrics();
    1.74 +        sActivity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    1.75 +
    1.76 +        sScreenHeight = dm.heightPixels;
    1.77 +        sScreenWidth = dm.widthPixels;
    1.78 +    }
    1.79 +
    1.80 +    private static void setDeviceType() {
    1.81 +        sDeviceType = (GeckoAppShell.isTablet() ? Type.TABLET : Type.PHONE);
    1.82 +    }
    1.83 +
    1.84 +    public static int getScreenHeight() {
    1.85 +        return sScreenHeight;
    1.86 +    }
    1.87 +
    1.88 +    public static int getScreenWidth() {
    1.89 +        return sScreenWidth;
    1.90 +    }
    1.91 +
    1.92 +    public static AndroidVersion getAndroidVersion() {
    1.93 +        return sAndroidVersion;
    1.94 +    }
    1.95 +
    1.96 +    public static boolean isPhone() {
    1.97 +        return (sDeviceType == Type.PHONE);
    1.98 +    }
    1.99 +
   1.100 +    public static boolean isTablet() {
   1.101 +        return (sDeviceType == Type.TABLET);
   1.102 +    }
   1.103 +
   1.104 +    public static void setLandscapeRotation() {
   1.105 +        sSolo.setActivityOrientation(Solo.LANDSCAPE);
   1.106 +    }
   1.107 +
   1.108 +    public static void setPortraitOrientation() {
   1.109 +        sSolo.setActivityOrientation(Solo.LANDSCAPE);
   1.110 +    }
   1.111 +}

mercurial