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