Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(586)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/vr_shell/VrTestRule.java

Issue 2857583005: Convert Vr tests to JUnit4 (Closed)
Patch Set: Add VrTestRule Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.chrome.browser.vr_shell; 5 package org.chromium.chrome.browser.vr_shell;
6 6
7 import static org.chromium.chrome.browser.vr_shell.VrUtils.POLL_CHECK_INTERVAL_L ONG_MS; 7 import static org.chromium.chrome.browser.vr_shell.VrUtils.POLL_CHECK_INTERVAL_L ONG_MS;
8 import static org.chromium.chrome.browser.vr_shell.VrUtils.POLL_CHECK_INTERVAL_S HORT_MS; 8 import static org.chromium.chrome.browser.vr_shell.VrUtils.POLL_CHECK_INTERVAL_S HORT_MS;
9 import static org.chromium.chrome.browser.vr_shell.VrUtils.POLL_TIMEOUT_LONG_MS; 9 import static org.chromium.chrome.browser.vr_shell.VrUtils.POLL_TIMEOUT_LONG_MS;
10 import static org.chromium.chrome.browser.vr_shell.VrUtils.POLL_TIMEOUT_SHORT_MS ; 10 import static org.chromium.chrome.browser.vr_shell.VrUtils.POLL_TIMEOUT_SHORT_MS ;
11 11
12 import android.support.test.InstrumentationRegistry;
13
14 import org.junit.Assert;
15 import org.junit.runner.Description;
16 import org.junit.runners.model.Statement;
17
12 import org.chromium.base.Log; 18 import org.chromium.base.Log;
13 import org.chromium.base.test.util.UrlUtils; 19 import org.chromium.base.test.util.UrlUtils;
14 import org.chromium.chrome.test.ChromeTabbedActivityTestBase; 20 import org.chromium.chrome.test.ChromeTabbedActivityTestRule;
15 import org.chromium.content.browser.test.util.ClickUtils; 21 import org.chromium.content.browser.test.util.ClickUtils;
16 import org.chromium.content.browser.test.util.Criteria; 22 import org.chromium.content.browser.test.util.Criteria;
17 import org.chromium.content.browser.test.util.CriteriaHelper; 23 import org.chromium.content.browser.test.util.CriteriaHelper;
18 import org.chromium.content.browser.test.util.JavaScriptUtils; 24 import org.chromium.content.browser.test.util.JavaScriptUtils;
19 import org.chromium.content_public.browser.WebContents; 25 import org.chromium.content_public.browser.WebContents;
20 26
21 import java.util.concurrent.Callable; 27 import java.util.concurrent.Callable;
22 import java.util.concurrent.TimeUnit; 28 import java.util.concurrent.TimeUnit;
23 import java.util.concurrent.TimeoutException; 29 import java.util.concurrent.TimeoutException;
24 30
(...skipping 10 matching lines...) Expand all
35 * match what we expect for that test 41 * match what we expect for that test
36 * - Repeat: 42 * - Repeat:
37 * - Run any necessary Java-side code, e.g. trigger a user action 43 * - Run any necessary Java-side code, e.g. trigger a user action
38 * - Trigger the next JavaScript test step and wait for it to finish 44 * - Trigger the next JavaScript test step and wait for it to finish
39 * 45 *
40 * The JavaScript code will automatically process test results once all 46 * The JavaScript code will automatically process test results once all
41 * testharness.js tests are done, just like in layout tests. Once the results 47 * testharness.js tests are done, just like in layout tests. Once the results
42 * are processed, the JavaScript code will automatically signal the Java code, 48 * are processed, the JavaScript code will automatically signal the Java code,
43 * which can then grab the results and pass/fail the instrumentation test. 49 * which can then grab the results and pass/fail the instrumentation test.
44 */ 50 */
45 public class VrTestBase extends ChromeTabbedActivityTestBase { 51 public class VrTestRule extends ChromeTabbedActivityTestRule {
jbudorick 2017/05/09 23:52:41 Given how little this class interacts with its sup
the real yoland 2017/05/10 00:56:51 Done
46 private static final String TAG = "VrTestBase"; 52 private static final String TAG = "VrTestBase";
47 protected static final String TEST_DIR = "chrome/test/data/android/webvr_ins trumentation"; 53 static final String TEST_DIR = "chrome/test/data/android/webvr_instrumentati on";
48 protected static final int PAGE_LOAD_TIMEOUT_S = 10; 54 static final int PAGE_LOAD_TIMEOUT_S = 10;
49 55
50 protected WebContents mWebContents; 56 private WebContents mWebContents;
51 57
52 @Override 58 @Override
53 protected void setUp() throws Exception { 59 public Statement apply(final Statement base, Description desc) {
54 super.setUp(); 60 return super.apply(new Statement() {
55 mWebContents = getActivity().getActivityTab().getWebContents(); 61 @Override
62 public void evaluate() throws Throwable {
63 startMainActivityOnBlankPage();
64 mWebContents = getActivity().getActivityTab().getWebContents();
65 base.evaluate();
66 }
67 }, desc);
56 } 68 }
57 69
58 @Override 70 public WebContents getWebContents() {
59 public void startMainActivity() throws InterruptedException { 71 return mWebContents;
60 startMainActivityOnBlankPage();
61 } 72 }
62 73
63 /** 74 /**
64 * Gets the file:// URL to the test file 75 * Gets the file:// URL to the test file
65 * @param testName The name of the test whose file will be retrieved 76 * @param testName The name of the test whose file will be retrieved
66 * @return The file:// URL to the specified test file 77 * @return The file:// URL to the specified test file
67 */ 78 */
68 protected static String getHtmlTestFile(String testName) { 79 public static String getHtmlTestFile(String testName) {
69 return "file://" + UrlUtils.getIsolatedTestFilePath(TEST_DIR) + "/html/" + testName 80 return "file://" + UrlUtils.getIsolatedTestFilePath(TEST_DIR) + "/html/" + testName
70 + ".html"; 81 + ".html";
71 } 82 }
72 83
73 /** 84 /**
74 * Blocks until the promise returned by nagivator.getVRDisplays() resolves, 85 * Blocks until the promise returned by nagivator.getVRDisplays() resolves,
75 * then checks whether a VRDisplay was actually found. 86 * then checks whether a VRDisplay was actually found.
76 * @param webContents The WebContents to run the JavaScript through 87 * @param webContents The WebContents to run the JavaScript through
77 * @return Whether a VRDisplay was found 88 * @return Whether a VRDisplay was found
78 */ 89 */
79 protected boolean vrDisplayFound(WebContents webContents) { 90 public boolean vrDisplayFound(WebContents webContents) {
80 pollJavaScriptBoolean("vrDisplayPromiseDone", POLL_TIMEOUT_SHORT_MS, web Contents); 91 pollJavaScriptBoolean("vrDisplayPromiseDone", POLL_TIMEOUT_SHORT_MS, web Contents);
81 return !runJavaScriptOrFail("vrDisplay", POLL_TIMEOUT_SHORT_MS, webConte nts).equals("null"); 92 return !runJavaScriptOrFail("vrDisplay", POLL_TIMEOUT_SHORT_MS, webConte nts).equals("null");
82 } 93 }
83 94
84 /** 95 /**
85 * Use to tap in the middle of the screen, triggering the canvas' onclick 96 * Use to tap in the middle of the screen, triggering the canvas' onclick
86 * to fulfil WebVR's gesture requirement for presenting. 97 * to fulfil WebVR's gesture requirement for presenting.
87 */ 98 */
88 protected void enterVrTap() { 99 public void enterVrTap() {
89 ClickUtils.mouseSingleClickView( 100 ClickUtils.mouseSingleClickView(InstrumentationRegistry.getInstrumentati on(),
90 getInstrumentation(), getActivity().getWindow().getDecorView().g etRootView()); 101 getActivity().getWindow().getDecorView().getRootView());
91 } 102 }
92 103
93 /** 104 /**
94 * Taps in the middle of the screen then waits for the JavaScript step to fi nish. 105 * Taps in the middle of the screen then waits for the JavaScript step to fi nish.
95 * @param webContents The WebContents for the tab the JavaScript step is in 106 * @param webContents The WebContents for the tab the JavaScript step is in
96 */ 107 */
97 protected void enterVrTapAndWait(WebContents webContents) { 108 public void enterVrTapAndWait(WebContents webContents) {
98 enterVrTap(); 109 enterVrTap();
99 waitOnJavaScriptStep(webContents); 110 waitOnJavaScriptStep(webContents);
100 } 111 }
101 112
102 /** 113 /**
103 * Use to simulate a Daydream View NFC scan without blocking afterwards 114 * Use to simulate a Daydream View NFC scan without blocking afterwards
104 */ 115 */
105 protected void simNfcScan() { 116 public void simNfcScan() {
106 VrUtils.simNfc(getActivity()); 117 VrUtils.simNfc(getActivity());
107 } 118 }
108 119
109 /** 120 /**
110 * Simulate an NFC scan and wait for the JavaScript code in the given 121 * Simulate an NFC scan and wait for the JavaScript code in the given
111 * WebContents to signal that it is done with the step. 122 * WebContents to signal that it is done with the step.
112 * @param webContents The WebContents for the JavaScript that will be polled 123 * @param webContents The WebContents for the JavaScript that will be polled
113 */ 124 */
114 protected void simNfcScanAndWait(WebContents webContents) { 125 public void simNfcScanAndWait(WebContents webContents) {
115 simNfcScan(); 126 simNfcScan();
116 waitOnJavaScriptStep(webContents); 127 waitOnJavaScriptStep(webContents);
117 } 128 }
118 129
119 /** 130 /**
120 * Helper function to run the given JavaScript, return the return value, 131 * Helper function to run the given JavaScript, return the return value,
121 * and fail if a timeout/interrupt occurs so we don't have to catch or 132 * and fail if a timeout/interrupt occurs so we don't have to catch or
122 * declare exceptions all the time. 133 * declare exceptions all the time.
123 * @param js The JavaScript to run 134 * @param js The JavaScript to run
124 * @param timeout The timeout in milliseconds before a failure 135 * @param timeout The timeout in milliseconds before a failure
125 * @param webContents The WebContents object to run the JavaScript in 136 * @param webContents The WebContents object to run the JavaScript in
126 * @return The return value of the JavaScript 137 * @return The return value of the JavaScript
127 */ 138 */
128 protected String runJavaScriptOrFail(String js, int timeout, WebContents web Contents) { 139 public String runJavaScriptOrFail(String js, int timeout, WebContents webCon tents) {
129 try { 140 try {
130 return JavaScriptUtils.executeJavaScriptAndWaitForResult( 141 return JavaScriptUtils.executeJavaScriptAndWaitForResult(
131 webContents, js, timeout, TimeUnit.MILLISECONDS); 142 webContents, js, timeout, TimeUnit.MILLISECONDS);
132 } catch (InterruptedException | TimeoutException e) { 143 } catch (InterruptedException | TimeoutException e) {
133 fail("Fatal interruption or timeout running JavaScript: " + js); 144 Assert.fail("Fatal interruption or timeout running JavaScript: " + j s);
134 } 145 }
135 return "Not reached"; 146 return "Not reached";
136 } 147 }
137 148
138 /** 149 /**
139 * Ends the test harness test and checks whether there it passed 150 * Ends the test harness test and checks whether there it passed
140 * @param webContents The WebContents for the tab to check results in 151 * @param webContents The WebContents for the tab to check results in
141 * @return "Passed" if test passed, String with failure reason otherwise 152 * @return "Passed" if test passed, String with failure reason otherwise
142 */ 153 */
143 protected String checkResults(WebContents webContents) { 154 public String checkResults(WebContents webContents) {
144 if (runJavaScriptOrFail("testPassed", POLL_TIMEOUT_SHORT_MS, webContents ).equals("true")) { 155 if (runJavaScriptOrFail("testPassed", POLL_TIMEOUT_SHORT_MS, webContents ).equals("true")) {
145 return "Passed"; 156 return "Passed";
146 } 157 }
147 return runJavaScriptOrFail("resultString", POLL_TIMEOUT_SHORT_MS, webCon tents); 158 return runJavaScriptOrFail("resultString", POLL_TIMEOUT_SHORT_MS, webCon tents);
148 } 159 }
149 160
150 /** 161 /**
151 * Helper function to end the test harness test and assert that it passed, 162 * Helper function to end the test harness test and assert that it passed,
152 * setting the failure reason as the description if it didn't. 163 * setting the failure reason as the description if it didn't.
153 * @param webContents The WebContents for the tab to check test results in 164 * @param webContents The WebContents for the tab to check test results in
154 */ 165 */
155 protected void endTest(WebContents webContents) { 166 public void endTest(WebContents webContents) {
156 assertEquals("Passed", checkResults(webContents)); 167 Assert.assertEquals("Passed", checkResults(webContents));
157 } 168 }
158 169
159 /** 170 /**
160 * Polls the provided JavaScript boolean until the timeout is reached or 171 * Polls the provided JavaScript boolean until the timeout is reached or
161 * the boolean is true. 172 * the boolean is true.
162 * @param boolName The name of the JavaScript boolean or expression to poll 173 * @param boolName The name of the JavaScript boolean or expression to poll
163 * @param timeoutMs The polling timeout in milliseconds 174 * @param timeoutMs The polling timeout in milliseconds
164 * @param webContents The WebContents to run the JavaScript through 175 * @param webContents The WebContents to run the JavaScript through
165 * @return True if the boolean evaluated to true, false if timed out 176 * @return True if the boolean evaluated to true, false if timed out
166 */ 177 */
167 protected boolean pollJavaScriptBoolean( 178 public boolean pollJavaScriptBoolean(
168 final String boolName, int timeoutMs, final WebContents webContents) { 179 final String boolName, int timeoutMs, final WebContents webContents) {
169 try { 180 try {
170 CriteriaHelper.pollInstrumentationThread(Criteria.equals(true, new C allable<Boolean>() { 181 CriteriaHelper.pollInstrumentationThread(Criteria.equals(true, new C allable<Boolean>() {
171 @Override 182 @Override
172 public Boolean call() { 183 public Boolean call() {
173 String result = "false"; 184 String result = "false";
174 try { 185 try {
175 result = JavaScriptUtils.executeJavaScriptAndWaitForResu lt(webContents, 186 result = JavaScriptUtils.executeJavaScriptAndWaitForResu lt(webContents,
176 boolName, POLL_CHECK_INTERVAL_SHORT_MS, TimeUnit .MILLISECONDS); 187 boolName, POLL_CHECK_INTERVAL_SHORT_MS, TimeUnit .MILLISECONDS);
177 } catch (InterruptedException | TimeoutException e) { 188 } catch (InterruptedException | TimeoutException e) {
178 // Expected to happen regularly, do nothing 189 // Expected to happen regularly, do nothing
179 } 190 }
180 return Boolean.parseBoolean(result); 191 return Boolean.parseBoolean(result);
181 } 192 }
182 }), timeoutMs, POLL_CHECK_INTERVAL_LONG_MS); 193 }), timeoutMs, POLL_CHECK_INTERVAL_LONG_MS);
183 } catch (AssertionError e) { 194 } catch (AssertionError e) {
184 Log.d(TAG, "pollJavaScriptBoolean() timed out"); 195 Log.d(TAG, "pollJavaScriptBoolean() timed out");
185 return false; 196 return false;
186 } 197 }
187 return true; 198 return true;
188 } 199 }
189 200
190 /** 201 /**
191 * Waits for a JavaScript step to finish, asserting that the step finished 202 * Waits for a JavaScript step to finish, asserting that the step finished
192 * instead of timing out. 203 * instead of timing out.
193 * @param webContents The WebContents for the tab the JavaScript step is in 204 * @param webContents The WebContents for the tab the JavaScript step is in
194 */ 205 */
195 protected void waitOnJavaScriptStep(WebContents webContents) { 206 public void waitOnJavaScriptStep(WebContents webContents) {
196 assertTrue("Polling JavaScript boolean javascriptDone timed out", 207 Assert.assertTrue("Polling JavaScript boolean javascriptDone timed out",
197 pollJavaScriptBoolean("javascriptDone", POLL_TIMEOUT_LONG_MS, we bContents)); 208 pollJavaScriptBoolean("javascriptDone", POLL_TIMEOUT_LONG_MS, we bContents));
198 // Reset the synchronization boolean 209 // Reset the synchronization boolean
199 runJavaScriptOrFail("javascriptDone = false", POLL_TIMEOUT_SHORT_MS, web Contents); 210 runJavaScriptOrFail("javascriptDone = false", POLL_TIMEOUT_SHORT_MS, web Contents);
200 } 211 }
201 212
202 /** 213 /**
203 * Executes a JavaScript step function using the given WebContents. 214 * Executes a JavaScript step function using the given WebContents.
204 * @param stepFunction The JavaScript step function to call 215 * @param stepFunction The JavaScript step function to call
205 * @param webContents The WebContents for the tab the JavaScript is in 216 * @param webContents The WebContents for the tab the JavaScript is in
206 */ 217 */
207 protected void executeStepAndWait(String stepFunction, WebContents webConten ts) { 218 public void executeStepAndWait(String stepFunction, WebContents webContents) {
208 // Run the step and block 219 // Run the step and block
209 JavaScriptUtils.executeJavaScript(webContents, stepFunction); 220 JavaScriptUtils.executeJavaScript(webContents, stepFunction);
210 waitOnJavaScriptStep(webContents); 221 waitOnJavaScriptStep(webContents);
211 } 222 }
212 } 223 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698