This source file includes following definitions.
- checkOrientationForLock
- lockOrientation
- lockOrientationAndWait
- unlockOrientation
- setUp
- tearDown
- Feature
- testBasicValues
- Feature
- testPortrait
- Feature
- testLandscape
package org.chromium.content.browser;
import android.os.Build;
import android.test.FlakyTest;
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.common.ScreenOrientationValues;
import org.chromium.content_shell_apk.ContentShellActivity;
import org.chromium.content_shell_apk.ContentShellTestBase;
public class ScreenOrientationProviderTest extends ContentShellTestBase {
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();
private boolean checkOrientationForLock(int orientations) {
switch (orientations) {
case ScreenOrientationValues.PORTRAIT_PRIMARY:
return mObserver.mOrientation == 0;
case ScreenOrientationValues.PORTRAIT_SECONDARY:
return mObserver.mOrientation == 180 ||
(ALLOW_0_FOR_180 && mObserver.mOrientation == 0);
case ScreenOrientationValues.LANDSCAPE_PRIMARY:
return mObserver.mOrientation == 90;
case ScreenOrientationValues.LANDSCAPE_SECONDARY:
return mObserver.mOrientation == -90;
case ScreenOrientationValues.PORTRAIT_PRIMARY |
ScreenOrientationValues.PORTRAIT_SECONDARY:
return mObserver.mOrientation == 0 || mObserver.mOrientation == 180;
case ScreenOrientationValues.LANDSCAPE_PRIMARY |
ScreenOrientationValues.LANDSCAPE_SECONDARY:
return mObserver.mOrientation == 90 || mObserver.mOrientation == -90;
case ScreenOrientationValues.PORTRAIT_PRIMARY |
ScreenOrientationValues.PORTRAIT_SECONDARY |
ScreenOrientationValues.LANDSCAPE_PRIMARY |
ScreenOrientationValues.LANDSCAPE_SECONDARY:
return true;
default:
return mObserver.mHasChanged == false;
}
}
private void lockOrientation(int orientations) {
mProvider.lockOrientation((byte)orientations);
}
private boolean lockOrientationAndWait(int orientations)
throws InterruptedException {
OrientationChangeObserverCriteria criteria =
new OrientationChangeObserverCriteria(mObserver);
lockOrientation(orientations);
return CriteriaHelper.pollForCriteria(criteria);
}
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());
OrientationChangeObserverCriteria criteria =
new OrientationChangeObserverCriteria(mObserver);
CriteriaHelper.pollForCriteria(criteria);
}
@Override
public void tearDown() throws Exception {
unlockOrientation();
mObserver = null;
super.tearDown();
}
@FlakyTest
@Feature({"ScreenOrientation"})
public void testBasicValues() throws Exception {
lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY);
assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY));
lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY);
assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY));
lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_SECONDARY);
assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_SECONDARY));
lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_SECONDARY);
assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_SECONDARY));
}
@FlakyTest
@Feature({"ScreenOrientation"})
public void testPortrait() throws Exception {
lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY);
assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY));
lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY |
ScreenOrientationValues.PORTRAIT_SECONDARY);
assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY |
ScreenOrientationValues.PORTRAIT_SECONDARY));
lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_SECONDARY);
assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_SECONDARY));
lockOrientationAndWait(ScreenOrientationValues.PORTRAIT_PRIMARY |
ScreenOrientationValues.PORTRAIT_SECONDARY);
assertTrue(checkOrientationForLock(ScreenOrientationValues.PORTRAIT_PRIMARY |
ScreenOrientationValues.PORTRAIT_SECONDARY));
}
@FlakyTest
@Feature({"ScreenOrientation"})
public void testLandscape() throws Exception {
int initialOrientation = mObserver.mOrientation;
lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
boolean result = checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY);
if (initialOrientation != -90)
assertTrue(result);
} else {
assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY));
}
lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY |
ScreenOrientationValues.LANDSCAPE_SECONDARY);
assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY |
ScreenOrientationValues.LANDSCAPE_SECONDARY));
lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_SECONDARY);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
boolean result = checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_SECONDARY);
if (initialOrientation == -90)
assertTrue(result);
} else {
assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_SECONDARY));
}
lockOrientationAndWait(ScreenOrientationValues.LANDSCAPE_PRIMARY |
ScreenOrientationValues.LANDSCAPE_SECONDARY);
assertTrue(checkOrientationForLock(ScreenOrientationValues.LANDSCAPE_PRIMARY |
ScreenOrientationValues.LANDSCAPE_SECONDARY));
}
}