1 | // Copyright (c) 2012 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.android_webview.test; |
6 | |
7 | import android.app.Activity; |
8 | import android.content.pm.ActivityInfo; |
9 | import android.test.suitebuilder.annotation.SmallTest; |
10 | import android.view.View; |
11 | import android.view.ViewConfiguration; |
12 | |
13 | import org.chromium.android_webview.AwContents; |
14 | import org.chromium.android_webview.AwSettings; |
15 | import org.chromium.base.ThreadUtils; |
16 | import org.chromium.base.test.util.DisabledTest; |
17 | import org.chromium.base.test.util.Feature; |
18 | import org.chromium.content.browser.test.util.Criteria; |
19 | import org.chromium.content.browser.test.util.CriteriaHelper; |
20 | |
21 | import java.util.concurrent.Callable; |
22 | |
23 | /** |
24 | * A test suite for zooming-related methods and settings. |
25 | */ |
26 | public class AwZoomTest extends AwTestBase { |
27 | private static final long TEST_TIMEOUT_MS = 20000L; |
28 | private static final int CHECK_INTERVAL_MS = 100; |
29 | |
30 | private TestAwContentsClient mContentsClient; |
31 | private AwContents mAwContents; |
32 | |
33 | @Override |
34 | public void setUp() throws Exception { |
35 | super.setUp(); |
36 | mContentsClient = new TestAwContentsClient(); |
37 | final AwTestContainerView testContainerView = |
38 | createAwTestContainerViewOnMainSync(mContentsClient); |
39 | mAwContents = testContainerView.getAwContents(); |
40 | } |
41 | |
42 | private String getZoomableHtml() { |
43 | return "<html><head><meta name=\"viewport\" content=\"" + |
44 | "width=device-width, minimum-scale=0.5, maximum-scale=2.0, initial-scale=0.5" + |
45 | "\"/></head><body>Zoomable</body></html>"; |
46 | } |
47 | |
48 | private String getNonZoomableHtml() { |
49 | // This page can't be zoomed because its viewport fully occupies |
50 | // view area and is explicitly made non user-scalable. |
51 | return "<html><head>" + |
52 | "<meta name=\"viewport\" " + |
53 | "content=\"width=device-width,height=device-height," + |
54 | "initial-scale=1,maximum-scale=1,user-scalable=no\">" + |
55 | "</head><body>Non-zoomable</body></html>"; |
56 | } |
57 | |
58 | private boolean isMultiTouchZoomSupportedOnUiThread() throws Throwable { |
59 | return runTestOnUiThreadAndGetResult(new Callable<Boolean>() { |
60 | @Override |
61 | public Boolean call() throws Exception { |
62 | return mAwContents.isMultiTouchZoomSupported(); |
63 | } |
64 | }); |
65 | } |
66 | |
67 | private int getVisibilityOnUiThread(final View view) throws Throwable { |
68 | return runTestOnUiThreadAndGetResult(new Callable<Integer>() { |
69 | @Override |
70 | public Integer call() throws Exception { |
71 | return view.getVisibility(); |
72 | } |
73 | }); |
74 | } |
75 | |
76 | private boolean canZoomInOnUiThread() throws Throwable { |
77 | return runTestOnUiThreadAndGetResult(new Callable<Boolean>() { |
78 | @Override |
79 | public Boolean call() throws Exception { |
80 | return mAwContents.canZoomIn(); |
81 | } |
82 | }); |
83 | } |
84 | |
85 | private boolean canZoomOutOnUiThread() throws Throwable { |
86 | return runTestOnUiThreadAndGetResult(new Callable<Boolean>() { |
87 | @Override |
88 | public Boolean call() throws Exception { |
89 | return mAwContents.canZoomOut(); |
90 | } |
91 | }); |
92 | } |
93 | |
94 | private float getScaleOnUiThread() throws Throwable { |
95 | return runTestOnUiThreadAndGetResult(new Callable<Float>() { |
96 | @Override |
97 | public Float call() throws Exception { |
98 | return mAwContents.getScale(); |
99 | } |
100 | }); |
101 | } |
102 | |
103 | private View getZoomControlsOnUiThread() throws Throwable { |
104 | return runTestOnUiThreadAndGetResult(new Callable<View>() { |
105 | @Override |
106 | public View call() throws Exception { |
107 | return mAwContents.getZoomControlsForTest(); |
108 | } |
109 | }); |
110 | } |
111 | |
112 | private void invokeZoomPickerOnUiThread() throws Throwable { |
113 | ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
114 | @Override |
115 | public void run() { |
116 | mAwContents.invokeZoomPicker(); |
117 | } |
118 | }); |
119 | } |
120 | |
121 | private boolean zoomInOnUiThreadAndWait() throws Throwable { |
122 | final float previousScale = getScaleOnUiThread(); |
123 | if (!runTestOnUiThreadAndGetResult(new Callable<Boolean>() { |
124 | @Override |
125 | public Boolean call() throws Exception { |
126 | return mAwContents.zoomIn(); |
127 | } |
128 | })) |
129 | return false; |
130 | // The zoom level is updated asynchronously. |
131 | return waitForScaleChange(previousScale); |
132 | } |
133 | |
134 | private boolean zoomOutOnUiThreadAndWait() throws Throwable { |
135 | final float previousScale = getScaleOnUiThread(); |
136 | if (!runTestOnUiThreadAndGetResult(new Callable<Boolean>() { |
137 | @Override |
138 | public Boolean call() throws Exception { |
139 | return mAwContents.zoomOut(); |
140 | } |
141 | })) |
142 | return false; |
143 | // The zoom level is updated asynchronously. |
144 | return waitForScaleChange(previousScale); |
145 | } |
146 | |
147 | private boolean waitForScaleChange(final float previousScale) throws Throwable { |
148 | return CriteriaHelper.pollForCriteria(new Criteria() { |
149 | @Override |
150 | public boolean isSatisfied() { |
151 | try { |
152 | return previousScale != getScaleOnUiThread(); |
153 | } catch (Throwable t) { |
154 | t.printStackTrace(); |
155 | fail("Failed to getScaleOnUiThread: " + t.toString()); |
156 | return false; |
157 | } |
158 | } |
159 | }, TEST_TIMEOUT_MS, CHECK_INTERVAL_MS); |
160 | } |
161 | |
162 | private boolean waitUntilCanNotZoom() throws Throwable { |
163 | return CriteriaHelper.pollForCriteria(new Criteria() { |
164 | @Override |
165 | public boolean isSatisfied() { |
166 | try { |
167 | return !canZoomInOnUiThread() && !canZoomOutOnUiThread(); |
168 | } catch (Throwable t) { |
169 | t.printStackTrace(); |
170 | fail("Failed to query canZoomIn/Out: " + t.toString()); |
171 | return false; |
172 | } |
173 | } |
174 | }, TEST_TIMEOUT_MS, CHECK_INTERVAL_MS); |
175 | } |
176 | |
177 | private void runMagnificationTest(boolean supportZoom) throws Throwable { |
178 | int onScaleChangedCallCount = mContentsClient.getOnScaleChangedHelper().getCallCount(); |
179 | loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), |
180 | getZoomableHtml(), "text/html", false); |
181 | mContentsClient.getOnScaleChangedHelper().waitForCallback(onScaleChangedCallCount); |
182 | getAwSettingsOnUiThread(mAwContents).setSupportZoom(supportZoom); |
183 | assertTrue("Should be able to zoom in", canZoomInOnUiThread()); |
184 | assertFalse("Should not be able to zoom out", canZoomOutOnUiThread()); |
185 | |
186 | while (canZoomInOnUiThread()) { |
187 | assertTrue(zoomInOnUiThreadAndWait()); |
188 | } |
189 | assertTrue("Should be able to zoom out", canZoomOutOnUiThread()); |
190 | |
191 | while (canZoomOutOnUiThread()) { |
192 | assertTrue(zoomOutOnUiThreadAndWait()); |
193 | } |
194 | assertTrue("Should be able to zoom in", canZoomInOnUiThread()); |
195 | } |
196 | |
197 | /* |
198 | @SmallTest |
199 | @Feature({"AndroidWebView"}) |
200 | http://crbug.com/239144 |
201 | */ |
202 | @DisabledTest |
203 | public void testMagnification() throws Throwable { |
204 | runMagnificationTest(true); |
205 | } |
206 | |
207 | // According to Android CTS test, zoomIn/Out must work |
208 | // even if supportZoom is turned off. |
209 | /* |
210 | @SmallTest |
211 | @Feature({"AndroidWebView"}) |
212 | http://crbug.com/239144 |
213 | */ |
214 | @DisabledTest |
215 | public void testMagnificationWithZoomSupportOff() throws Throwable { |
216 | runMagnificationTest(false); |
217 | } |
218 | |
219 | @SmallTest |
220 | @Feature({"AndroidWebView"}) |
221 | public void testZoomUsingMultiTouch() throws Throwable { |
222 | AwSettings webSettings = getAwSettingsOnUiThread(mAwContents); |
223 | loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), |
224 | getZoomableHtml(), "text/html", false); |
225 | |
226 | assertTrue(webSettings.supportZoom()); |
227 | assertFalse(webSettings.getBuiltInZoomControls()); |
228 | assertFalse(isMultiTouchZoomSupportedOnUiThread()); |
229 | |
230 | webSettings.setBuiltInZoomControls(true); |
231 | assertTrue(isMultiTouchZoomSupportedOnUiThread()); |
232 | |
233 | webSettings.setSupportZoom(false); |
234 | assertFalse(isMultiTouchZoomSupportedOnUiThread()); |
235 | } |
236 | |
237 | /* |
238 | @SmallTest |
239 | @Feature({"AndroidWebView"}) |
240 | http://crbug.com/239144 |
241 | */ |
242 | @DisabledTest |
243 | public void testZoomControls() throws Throwable { |
244 | AwSettings webSettings = getAwSettingsOnUiThread(mAwContents); |
245 | int onScaleChangedCallCount = mContentsClient.getOnScaleChangedHelper().getCallCount(); |
246 | loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), |
247 | getZoomableHtml(), "text/html", false); |
248 | mContentsClient.getOnScaleChangedHelper().waitForCallback(onScaleChangedCallCount); |
249 | // It must be possible to zoom in (or zoom out) for zoom controls to be shown |
250 | assertTrue("Should be able to zoom in", canZoomInOnUiThread()); |
251 | |
252 | assertTrue(webSettings.supportZoom()); |
253 | webSettings.setBuiltInZoomControls(true); |
254 | webSettings.setDisplayZoomControls(false); |
255 | |
256 | // With DisplayZoomControls set to false, attempts to display zoom |
257 | // controls must be ignored. |
258 | assertNull(getZoomControlsOnUiThread()); |
259 | invokeZoomPickerOnUiThread(); |
260 | assertNull(getZoomControlsOnUiThread()); |
261 | |
262 | webSettings.setDisplayZoomControls(true); |
263 | assertNull(getZoomControlsOnUiThread()); |
264 | invokeZoomPickerOnUiThread(); |
265 | View zoomControls = getZoomControlsOnUiThread(); |
266 | assertEquals(View.VISIBLE, getVisibilityOnUiThread(zoomControls)); |
267 | } |
268 | |
269 | @SmallTest |
270 | @Feature({"AndroidWebView"}) |
271 | public void testZoomControlsOnNonZoomableContent() throws Throwable { |
272 | AwSettings webSettings = getAwSettingsOnUiThread(mAwContents); |
273 | loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), |
274 | getNonZoomableHtml(), "text/html", false); |
275 | |
276 | // ContentView must update itself according to the viewport setup. |
277 | waitUntilCanNotZoom(); |
278 | |
279 | assertTrue(webSettings.supportZoom()); |
280 | webSettings.setBuiltInZoomControls(true); |
281 | webSettings.setDisplayZoomControls(true); |
282 | assertNull(getZoomControlsOnUiThread()); |
283 | invokeZoomPickerOnUiThread(); |
284 | View zoomControls = getZoomControlsOnUiThread(); |
285 | assertEquals(View.GONE, getVisibilityOnUiThread(zoomControls)); |
286 | } |
287 | |
288 | @SmallTest |
289 | @Feature({"AndroidWebView"}) |
290 | public void testZoomControlsOnOrientationChange() throws Throwable { |
291 | AwSettings webSettings = getAwSettingsOnUiThread(mAwContents); |
292 | loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), |
293 | getZoomableHtml(), "text/html", false); |
294 | |
295 | assertTrue(webSettings.supportZoom()); |
296 | webSettings.setBuiltInZoomControls(true); |
297 | webSettings.setDisplayZoomControls(true); |
298 | invokeZoomPickerOnUiThread(); |
299 | |
300 | // Now force an orientation change, and try to display the zoom picker |
301 | // again. Make sure that we don't crash when the ZoomPicker registers |
302 | // it's receiver. |
303 | |
304 | Activity activity = getActivity(); |
305 | int orientation = activity.getRequestedOrientation(); |
306 | activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); |
307 | activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); |
308 | activity.setRequestedOrientation(orientation); |
309 | invokeZoomPickerOnUiThread(); |
310 | |
311 | // We may crash shortly (as the zoom picker has a short delay in it before |
312 | // it tries to register it's BroadcastReceiver), so sleep to verify we don't. |
313 | // The delay is encoded in ZoomButtonsController#ZOOM_CONTROLS_TIMEOUT, |
314 | // if that changes we may need to update this test. |
315 | Thread.sleep(ViewConfiguration.getZoomControlsTimeout()); |
316 | } |
317 | } |