Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / public / android / javatests / src / org / chromium / content / browser / ScreenOrientationTest.java
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.
4
5 package org.chromium.content.browser;
6
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;
15
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;
25
26 import java.util.concurrent.TimeoutException;
27
28 /**
29  * Integration tests for Screen Orientation API (and legacy API).
30  */
31 public class ScreenOrientationTest extends ContentShellTestBase {
32
33     private static final String DEFAULT_URL =
34             UrlUtils.encodeHtmlDataUri("<html><body>foo</body></html>");
35
36     /**
37      * Class that is used to get notified when the activity configuration
38      * changes.
39      */
40     private static class ConfigurationChangeAccessDelegate
41             implements InternalAccessDelegate {
42
43         private interface ConfigurationChangedListener {
44             public void onConfigurationChanged();
45         }
46
47         private ConfigurationChangedListener mListener = null;
48
49         private void setListener(ConfigurationChangedListener listener) {
50             mListener = listener;
51         }
52
53         @Override
54         public boolean drawChild(Canvas canvas, View child, long drawingTime) {
55             return false;
56         }
57
58         @Override
59         public boolean super_onKeyUp(int keyCode, KeyEvent event) {
60             return false;
61         }
62
63         @Override
64         public boolean super_dispatchKeyEventPreIme(KeyEvent event) {
65             return false;
66         }
67
68         @Override
69         public boolean super_dispatchKeyEvent(KeyEvent event) {
70             return false;
71         }
72
73         @Override
74         public boolean super_onGenericMotionEvent(MotionEvent event) {
75             return false;
76         }
77
78         @Override
79         public void super_onConfigurationChanged(Configuration newConfig) {
80             if (mListener != null)
81                 mListener.onConfigurationChanged();
82         }
83
84         @Override
85         public void onScrollChanged(int lPix, int tPix, int oldlPix, int oldtPix) {
86         }
87
88         @Override
89         public boolean awakenScrollBars() {
90             return false;
91         }
92
93         @Override
94         public boolean super_awakenScrollBars(int startDelay, boolean invalidate) {
95             return false;
96         }
97     }
98
99     /**
100      * Criteria used to know when an orientation change happens.
101      */
102     private static class OrientationChangeListenerCriteria implements Criteria {
103
104         private boolean mSatisfied = false;
105
106         private ConfigurationChangeAccessDelegate mAccessDelegate;
107
108         private OrientationChangeListenerCriteria(
109                 ConfigurationChangeAccessDelegate accessDelegate) {
110             mAccessDelegate = accessDelegate;
111             mAccessDelegate.setListener(
112                 new ConfigurationChangeAccessDelegate.ConfigurationChangedListener() {
113                     @Override
114                     public void onConfigurationChanged() {
115                         mSatisfied = true;
116                     }
117                 });
118         }
119
120         @Override
121         public boolean isSatisfied() {
122             if (mSatisfied) {
123                 mAccessDelegate.setListener(null);
124                 return true;
125             }
126
127             return false;
128         }
129     }
130
131     private ContentView mContentView;
132
133     private ConfigurationChangeAccessDelegate mConfChangeAccessDelegate;
134
135     /**
136      * Locks the screen orientation to the predefined orientation type.
137      */
138     private void lockOrientation(int orientation) {
139         getActivity().setRequestedOrientation(orientation);
140     }
141
142     /**
143      * Unlock the screen orientation. Equivalent to locking to unspecified.
144      */
145     private void unlockOrientation() {
146         getActivity().setRequestedOrientation(
147                 ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
148     }
149
150     /**
151      * Locks the screen orientation to the predefined orientation type then wait
152      * for the orientation change to happen.
153      */
154     private boolean lockOrientationAndWait(int orientation)
155             throws InterruptedException {
156         OrientationChangeListenerCriteria listenerCriteria =
157             new OrientationChangeListenerCriteria(mConfChangeAccessDelegate);
158
159         lockOrientation(orientation);
160
161         return CriteriaHelper.pollForCriteria(listenerCriteria);
162     }
163
164     /**
165      * Returns the screen orientation as seen by |window.orientation|.
166      */
167     private int getWindowOrientation()
168             throws InterruptedException, TimeoutException {
169         return Integer.parseInt(
170             JavaScriptUtils.executeJavaScriptAndWaitForResult(
171                     mContentView,
172                     new TestCallbackHelperContainer(mContentView),
173                     "window.orientation"));
174     }
175
176     @Override
177     public void setUp() throws Exception {
178         super.setUp();
179
180         ContentShellActivity activity = launchContentShellWithUrl(DEFAULT_URL);
181         waitForActiveShellToBeDoneLoading();
182         mContentView = activity.getActiveContentView();
183
184         mConfChangeAccessDelegate = new ConfigurationChangeAccessDelegate();
185         getContentViewCore().
186                 setContainerViewInternals(mConfChangeAccessDelegate);
187     }
188
189     @Override
190     public void tearDown() throws Exception {
191         unlockOrientation();
192         super.tearDown();
193     }
194
195     @SmallTest
196     @Feature({"ScreenOrientation", "Main"})
197     public void testDefault() throws Throwable {
198         assertEquals(0, getWindowOrientation());
199     }
200
201     @MediumTest
202     @Feature({"ScreenOrientation", "Main"})
203     public void testChanges() throws Throwable {
204         lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
205         assertEquals(90, getWindowOrientation());
206
207         lockOrientationAndWait(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
208         assertEquals(0, getWindowOrientation());
209
210         lockOrientationAndWait(
211             ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
212         assertEquals(-90, getWindowOrientation());
213
214         lockOrientationAndWait(
215             ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
216         int orientation = getWindowOrientation();
217         assertTrue(orientation == 0 || orientation == 180);
218     }
219 }