| Index: content/public/android/javatests/src/org/chromium/content/browser/ScreenOrientationProviderTest.java
|
| diff --git a/content/public/android/javatests/src/org/chromium/content/browser/ScreenOrientationProviderTest.java b/content/public/android/javatests/src/org/chromium/content/browser/ScreenOrientationProviderTest.java
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..1c3391665fd1d5a3a0e73e1eba5b5a524f82959b
|
| --- /dev/null
|
| +++ b/content/public/android/javatests/src/org/chromium/content/browser/ScreenOrientationProviderTest.java
|
| @@ -0,0 +1,165 @@
|
| +// Copyright 2014 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +package org.chromium.content.browser;
|
| +
|
| +import android.test.suitebuilder.annotation.MediumTest;
|
| +import android.test.suitebuilder.annotation.SmallTest;
|
| +
|
| +import org.chromium.base.test.util.Feature;
|
| +import org.chromium.base.test.util.UrlUtils;
|
| +import org.chromium.content.browser.test.util.CriteriaHelper;
|
| +import org.chromium.content.browser.test.util.MockOrientationObserver;
|
| +import org.chromium.content.browser.test.util.OrientationChangeObserverCriteria;
|
| +import org.chromium.content_shell_apk.ContentShellActivity;
|
| +import org.chromium.content_shell_apk.ContentShellTestBase;
|
| +
|
| +/**
|
| + * Tests for ScreenOrientationListener and its implementations.
|
| + */
|
| +public class ScreenOrientationProviderTest extends ContentShellTestBase {
|
| +
|
| + // For some reasons build bots are not able to lock to 180 degrees. This
|
| + // boolean is here to make the false negative go away in that situation.
|
| + private static final boolean ALLOW_0_FOR_180 = true;
|
| +
|
| + private static final String DEFAULT_URL =
|
| + UrlUtils.encodeHtmlDataUri("<html><body>foo</body></html>");
|
| +
|
| + private MockOrientationObserver mObserver;
|
| + private final ScreenOrientationProvider mProvider = ScreenOrientationProvider.create();
|
| +
|
| + // Has to be kept in sync with:
|
| + // third_party/WebKit/public/platform/WebScreenOrientation.h
|
| + private static final byte PORTRAIT_PRIMARY = 1 << 0;
|
| + private static final byte LANDSCAPE_PRIMARY = 1 << 1;
|
| + private static final byte PORTRAIT_SECONDARY = 1 << 2;
|
| + private static final byte LANDSCAPE_SECONDARY = 1 << 3;
|
| +
|
| + private boolean checkOrientationForLock(int orientations) {
|
| + switch (orientations) {
|
| + case PORTRAIT_PRIMARY:
|
| + return mObserver.mOrientation == 0;
|
| + case PORTRAIT_SECONDARY:
|
| + return mObserver.mOrientation == 180 ||
|
| + (ALLOW_0_FOR_180 && mObserver.mOrientation == 0);
|
| + case LANDSCAPE_PRIMARY:
|
| + return mObserver.mOrientation == 90;
|
| + case LANDSCAPE_SECONDARY:
|
| + return mObserver.mOrientation == -90;
|
| + case PORTRAIT_PRIMARY | PORTRAIT_SECONDARY:
|
| + return mObserver.mOrientation == 0 || mObserver.mOrientation == 180;
|
| + case LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY:
|
| + return mObserver.mOrientation == 90 || mObserver.mOrientation == -90;
|
| + case PORTRAIT_PRIMARY | PORTRAIT_SECONDARY | LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY:
|
| + // The orientation should not change but might and the value could be anything.
|
| + return true;
|
| + default:
|
| + return mObserver.mHasChanged == false;
|
| + }
|
| + }
|
| +
|
| + /**
|
| + * Locks the screen orientation to |orientations| using ScreenOrientationProvider.
|
| + */
|
| + private void lockOrientation(int orientations) {
|
| + mProvider.lockOrientation((byte)orientations);
|
| + }
|
| +
|
| + /**
|
| + * Call |lockOrientation| and wait for an orientation change.
|
| + */
|
| + private boolean lockOrientationAndWait(int orientations)
|
| + throws InterruptedException {
|
| + OrientationChangeObserverCriteria criteria =
|
| + new OrientationChangeObserverCriteria(mObserver);
|
| +
|
| + lockOrientation(orientations);
|
| +
|
| + return CriteriaHelper.pollForCriteria(criteria);
|
| + }
|
| +
|
| + /**
|
| + * Unlock the screen orientation using |ScreenOrientationProvider|.
|
| + */
|
| + private void unlockOrientation() {
|
| + mProvider.unlockOrientation();
|
| + }
|
| +
|
| + @Override
|
| + public void setUp() throws Exception {
|
| + super.setUp();
|
| +
|
| + ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL);
|
| + waitForActiveShellToBeDoneLoading();
|
| +
|
| + mObserver = new MockOrientationObserver();
|
| + ScreenOrientationListener.getInstance().addObserver(
|
| + mObserver, getInstrumentation().getTargetContext());
|
| + }
|
| +
|
| + @Override
|
| + public void tearDown() throws Exception {
|
| + unlockOrientation();
|
| +
|
| + mObserver = null;
|
| + super.tearDown();
|
| + }
|
| +
|
| + @SmallTest
|
| + @Feature({"ScreenOrientation"})
|
| + public void testBasicValues() throws Exception {
|
| + lockOrientationAndWait(PORTRAIT_PRIMARY);
|
| + assertTrue(checkOrientationForLock(PORTRAIT_PRIMARY));
|
| +
|
| + lockOrientationAndWait(LANDSCAPE_PRIMARY);
|
| + assertTrue(checkOrientationForLock(LANDSCAPE_PRIMARY));
|
| +
|
| + lockOrientationAndWait(PORTRAIT_SECONDARY);
|
| + assertTrue(checkOrientationForLock(PORTRAIT_SECONDARY));
|
| +
|
| + lockOrientationAndWait(LANDSCAPE_SECONDARY);
|
| + assertTrue(checkOrientationForLock(LANDSCAPE_SECONDARY));
|
| + }
|
| +
|
| + @MediumTest
|
| + @Feature({"ScreenOrientation"})
|
| + public void testPortrait() throws Exception {
|
| + lockOrientationAndWait(PORTRAIT_PRIMARY);
|
| + assertTrue(checkOrientationForLock(PORTRAIT_PRIMARY));
|
| +
|
| + lockOrientationAndWait(PORTRAIT_PRIMARY | PORTRAIT_SECONDARY);
|
| + assertTrue(checkOrientationForLock(PORTRAIT_PRIMARY | PORTRAIT_SECONDARY));
|
| +
|
| + lockOrientationAndWait(PORTRAIT_SECONDARY);
|
| + assertTrue(checkOrientationForLock(PORTRAIT_SECONDARY));
|
| +
|
| + lockOrientationAndWait(PORTRAIT_PRIMARY | PORTRAIT_SECONDARY);
|
| + assertTrue(checkOrientationForLock(PORTRAIT_PRIMARY | PORTRAIT_SECONDARY));
|
| + }
|
| +
|
| + @MediumTest
|
| + @Feature({"ScreenOrientation"})
|
| + public void testLandscape() throws Exception {
|
| + lockOrientationAndWait(LANDSCAPE_PRIMARY);
|
| + assertTrue(checkOrientationForLock(LANDSCAPE_PRIMARY));
|
| +
|
| + lockOrientationAndWait(LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY);
|
| + assertTrue(checkOrientationForLock(LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY));
|
| +
|
| + lockOrientationAndWait(LANDSCAPE_SECONDARY);
|
| + assertTrue(checkOrientationForLock(LANDSCAPE_SECONDARY));
|
| +
|
| + lockOrientationAndWait(LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY);
|
| + assertTrue(checkOrientationForLock(LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY));
|
| + }
|
| +
|
| + // There is no point in testing the case where we try to lock to
|
| + // PORTRAIT_PRIMARY | PORTRAIT_SECONDARY | LANDSCAPE_PRIMARY | LANDSCAPE_SECONDARY
|
| + // because with the device being likely flat during the test, locking to that
|
| + // will be a no-op.
|
| +
|
| + // We can't test unlock because the device is likely flat during the test
|
| + // and in that situation unlocking is a no-op.
|
| +}
|
|
|