OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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.chrome.browser; | |
6 | |
7 import android.graphics.Rect; | |
8 import android.test.suitebuilder.annotation.MediumTest; | |
9 | |
10 import junit.framework.Assert; | |
11 | |
12 import org.chromium.base.ThreadUtils; | |
13 import org.chromium.base.test.util.CommandLineFlags; | |
14 import org.chromium.base.test.util.UrlUtils; | |
15 import org.chromium.chrome.test.ChromeActivityTestCaseBase; | |
16 import org.chromium.content.browser.ContentViewCore; | |
17 import org.chromium.content.browser.test.util.Criteria; | |
18 import org.chromium.content.browser.test.util.CriteriaHelper; | |
19 import org.chromium.content.browser.test.util.DOMUtils; | |
20 import org.chromium.content.browser.test.util.JavaScriptUtils; | |
21 import org.chromium.content_public.browser.WebContents; | |
22 import org.chromium.ui.UiUtils; | |
23 | |
24 import java.util.concurrent.ExecutionException; | |
25 import java.util.concurrent.TimeoutException; | |
26 import java.util.concurrent.atomic.AtomicReference; | |
27 | |
28 | |
29 /** | |
30 * Integration test to ensure that OSK resizes only the visual viewport. | |
31 */ | |
32 | |
33 public class OSKOverscrollTest extends ChromeActivityTestCaseBase<ChromeActivity > { | |
34 private static final String FIXED_FOOTER_PAGE = UrlUtils.encodeHtmlDataUri(" " | |
35 + "<html>" | |
36 + "<head>" | |
37 + " <meta name=\"viewport\" " | |
38 + " content=\"width=device-width, initial-scale=1.0, maximum-scal e=1.0\" />" | |
39 + " <style>" | |
40 + " body {" | |
41 + " height:1500px;" | |
42 + " margin:0px;" | |
43 + " }" | |
44 + " #footer {" | |
45 + " position:fixed;" | |
46 + " left:0px;" | |
47 + " bottom:0px;" | |
48 + " height:50px;" | |
49 + " width:100%;" | |
50 + " background:#FFFF00;" | |
51 + " }" | |
52 + " </style>" | |
53 + "</head>" | |
54 + "<body>" | |
55 + " <form method=\"POST\">" | |
56 + " <input type=\"text\" id=\"fn\"/><br>" | |
57 + " <div id=\"footer\"></div>" | |
58 + " </form>" | |
59 + "</body>" | |
60 + "</html>"); | |
61 | |
62 // We convert CSS pixels into device pixels and compare the viewport size | |
63 // before and after the keyboard show. window.innerHeight returns an | |
64 // integer and the actual height is a floating point. Need some buffer for | |
65 // error. | |
66 private static final int ERROR_EPS_PIX = 1; | |
67 | |
68 public OSKOverscrollTest() { | |
69 super(ChromeActivity.class); | |
70 } | |
71 | |
72 @Override | |
73 public void startMainActivity() throws InterruptedException { | |
74 // Don't launch activity automatically. | |
75 } | |
76 | |
77 private void waitForKeyboard() throws InterruptedException { | |
78 // Wait until the keyboard is showing. | |
79 CriteriaHelper.pollForUIThreadCriteria(new Criteria("Keyboard was never shown.") { | |
80 @Override | |
81 public boolean isSatisfied() { | |
82 return UiUtils.isKeyboardShowing( | |
83 getActivity(), | |
84 getActivity().getCurrentContentViewCore().getContainerVi ew()); | |
85 } | |
86 }); | |
87 } | |
88 | |
89 private int getViewportHeight(WebContents webContents) { | |
90 try { | |
91 String jsonText = JavaScriptUtils.executeJavaScriptAndWaitForResult( | |
92 webContents, "window.innerHeight"); | |
93 Assert.assertFalse("Failed to retrieve bounds for element: fn", | |
Ted C
2016/03/12 00:01:44
you should be able to just call assertFalse here w
ymalik
2016/03/14 22:19:01
Done.
| |
94 jsonText.trim().equalsIgnoreCase("null")); | |
95 return Integer.parseInt(jsonText); | |
96 } catch (Exception ex) { | |
97 assert false : "Unexpected Exception"; | |
Ted C
2016/03/12 00:01:44
Use the fail(...) method
ymalik
2016/03/14 22:19:01
Done.
| |
98 } | |
99 return -1; | |
100 } | |
101 | |
102 private boolean almostEqual(int a, int b) { | |
Ted C
2016/03/12 00:01:44
i like this name...that is all
ymalik
2016/03/14 22:19:01
Acknowledged :)
| |
103 return Math.abs(a - b) <= ERROR_EPS_PIX; | |
104 } | |
105 | |
106 /** | |
107 * Verify that OSK show only resizes the visual viewport when the | |
108 * ENABLE_OSK_OVERSCROLL flag is set. | |
109 * | |
110 * @throws InterruptedException | |
111 * @throws TimeoutException | |
112 * @throws ExecutionException | |
113 */ | |
114 @MediumTest | |
115 @CommandLineFlags.Add({ChromeSwitches.ENABLE_OSK_OVERSCROLL}) | |
116 public void testOnlyVisualViewportResizes() | |
117 throws InterruptedException, TimeoutException, ExecutionException { | |
118 startMainActivityWithURL(FIXED_FOOTER_PAGE); | |
119 | |
120 final AtomicReference<ContentViewCore> viewCoreRef = new AtomicReference <ContentViewCore>(); | |
121 final AtomicReference<WebContents> webContentsRef = new AtomicReference< WebContents>(); | |
122 ThreadUtils.runOnUiThreadBlocking(new Runnable() { | |
123 @Override | |
124 public void run() { | |
125 viewCoreRef.set(getActivity().getCurrentContentViewCore()); | |
126 webContentsRef.set(viewCoreRef.get().getWebContents()); | |
127 } | |
128 }); | |
129 | |
130 DOMUtils.waitForNonZeroNodeBounds(webContentsRef.get(), "fn"); | |
131 | |
132 // Get the position of the footer and the viewport height before | |
133 // bringing up the OSK. | |
134 Rect footerPositionBefore = DOMUtils.getNodeBounds(webContentsRef.get(), "footer"); | |
135 int viewportHeightBeforeCss = getViewportHeight(webContentsRef.get()); | |
136 float cssToDevicePixFactor = viewCoreRef.get().getPageScaleFactor() | |
137 * viewCoreRef.get().getDeviceScaleFactor(); | |
138 | |
139 // Click on the unfocused input element for the first time to focus on | |
140 // it. This brings up the OSK. | |
141 DOMUtils.clickNode(this, viewCoreRef.get(), "fn"); | |
142 | |
143 waitForKeyboard(); | |
144 | |
145 // Get the position of the footer after bringing up the OSK. This should | |
146 // be the same as the position before because only the visual viewport | |
147 // should have resized. | |
148 Rect footerPositionAfter = DOMUtils.getNodeBounds(webContentsRef.get(), "footer"); | |
149 assertEquals(footerPositionBefore, footerPositionAfter); | |
150 | |
151 // Verify that the size of the viewport before the OSK show is equal to | |
152 // the size of the viewport after the OSK show plus the size of the | |
153 // keyboard. | |
154 int viewportHeightAfterCss = getViewportHeight(webContentsRef.get()); | |
155 int keyboardHeight = viewCoreRef.get().getContentViewClient().getSystemW indowInsetBottom(); | |
156 assertTrue(almostEqual( | |
157 (int) (viewportHeightBeforeCss * cssToDevicePixFactor), | |
158 (int) (viewportHeightAfterCss * cssToDevicePixFactor) + keyboard Height)); | |
159 } | |
160 } | |
OLD | NEW |