1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 package org.chromium.content.browser;
7 import android.content.pm.ActivityInfo;
8 import android.content.res.Configuration;
9 import android.graphics.Canvas;
10 import android.test.suitebuilder.annotation.MediumTest;
11 import android.test.suitebuilder.annotation.SmallTest;
12 import android.view.KeyEvent;
13 import android.view.MotionEvent;
14 import android.view.View;
16 import org.chromium.base.test.util.Feature;
17 import org.chromium.base.test.util.UrlUtils;
18 import org.chromium.content.browser.ContentViewCore.InternalAccessDelegate;
19 import org.chromium.content.browser.test.util.Criteria;
20 import org.chromium.content.browser.test.util.CriteriaHelper;
21 import org.chromium.content.browser.test.util.JavaScriptUtils;
22 import org.chromium.content.browser.test.util.TestCallbackHelperContainer;
23 import org.chromium.content_shell_apk.ContentShellActivity;
24 import org.chromium.content_shell_apk.ContentShellTestBase;
26 import java.util.concurrent.TimeoutException;
29 * Integration tests for Screen Orientation API (and legacy API).
31 public class ScreenOrientationTest extends ContentShellTestBase {
33 private static final String DEFAULT_URL =
34 UrlUtils.encodeHtmlDataUri("<html><body>foo</body></html>");
37 * Class that is used to get notified when the activity configuration
40 private static class ConfigurationChangeAccessDelegate
41 implements InternalAccessDelegate {
43 private interface ConfigurationChangedListener {
44 public void onConfigurationChanged();
47 private ConfigurationChangedListener mListener = null;
49 private void setListener(ConfigurationChangedListener listener) {
54 public boolean drawChild(Canvas canvas, View child, long drawingTime) {
59 public boolean super_onKeyUp(int keyCode, KeyEvent event) {
64 public boolean super_dispatchKeyEventPreIme(KeyEvent event) {
69 public boolean super_dispatchKeyEvent(KeyEvent event) {
74 public boolean super_onGenericMotionEvent(MotionEvent event) {
79 public void super_onConfigurationChanged(Configuration newConfig) {
80 if (mListener != null)
81 mListener.onConfigurationChanged();
85 public void onScrollChanged(int lPix, int tPix, int oldlPix, int oldtPix) {
89 public boolean awakenScrollBars() {
94 public boolean super_awakenScrollBars(int startDelay, boolean invalidate) {
100 * Criteria used to know when an orientation change happens.
102 private static class OrientationChangeListenerCriteria implements Criteria {
104 private boolean mSatisfied = false;
106 private ConfigurationChangeAccessDelegate mAccessDelegate;
108 private OrientationChangeListenerCriteria(
109 ConfigurationChangeAccessDelegate accessDelegate) {
110 mAccessDelegate = accessDelegate;
111 mAccessDelegate.setListener(
112 new ConfigurationChangeAccessDelegate.ConfigurationChangedListener() {
114 public void onConfigurationChanged() {
121 public boolean isSatisfied() {
123 mAccessDelegate.setListener(null);
131 private ContentView mContentView;
133 private ConfigurationChangeAccessDelegate mConfChangeAccessDelegate;
136 * Locks the screen orientation to the predefined orientation type.
138 private void lockOrientation(int orientation) {
139 getActivity().setRequestedOrientation(orientation);
143 * Unlock the screen orientation. Equivalent to locking to unspecified.
145 private void unlockOrientation() {
146 getActivity().setRequestedOrientation(
147 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
151 * Locks the screen orientation to the predefined orientation type then wait
152 * for the orientation change to happen.
154 private boolean lockOrientationAndWait(int orientation)
155 throws InterruptedException {
156 OrientationChangeListenerCriteria listenerCriteria =
157 new OrientationChangeListenerCriteria(mConfChangeAccessDelegate);
159 lockOrientation(orientation);
161 return CriteriaHelper.pollForCriteria(listenerCriteria);
165 * Returns the screen orientation as seen by |window.orientation|.
167 private int getWindowOrientation()
168 throws InterruptedException, TimeoutException {
169 return Integer.parseInt(
170 JavaScriptUtils.executeJavaScriptAndWaitForResult(
172 new TestCallbackHelperContainer(mContentView),
173 "window.orientation"));
177 public void setUp() throws Exception {
180 ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL);
181 waitForActiveShellToBeDoneLoading();
182 mContentView = activity.getActiveContentView();
184 mConfChangeAccessDelegate = new ConfigurationChangeAccessDelegate();
185 getContentViewCore().
186 setContainerViewInternals(mConfChangeAccessDelegate);
190 public void tearDown() throws Exception {
196 @Feature({"ScreenOrientation", "Main"})
197 public void testDefault() throws Throwable {
198 assertEquals(0, getWindowOrientation());
202 @Feature({"ScreenOrientation", "Main"})
203 public void testChanges() throws Throwable {
204 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
205 assertEquals(90, getWindowOrientation());
207 lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
208 assertEquals(0, getWindowOrientation());
210 lockOrientationAndWait(
211 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
212 assertEquals(-90, getWindowOrientation());
214 lockOrientationAndWait(
215 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
216 int orientation = getWindowOrientation();
217 assertTrue(orientation == 0 || orientation == 180);