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

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

Powered by Google App Engine
This is Rietveld 408576698