| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * @suppress {checkTypes} By default, JSCompile is not run on test files. | |
| 8 * However, you can modify |remoting_webapp_files.gypi| locally to include | |
| 9 * the test in the package to expedite local development. This suppress | |
| 10 * is here so that JSCompile won't complain. | |
| 11 * | 7 * |
| 12 * Provides basic functionality for JavaScript based browser test. | 8 * Provides basic functionality for JavaScript based browser test. |
| 13 * | 9 * |
| 14 * To define a browser test, create a class under the browserTest namespace. | 10 * To define a browser test, create a class under the browserTest namespace. |
| 15 * You can pass arbitrary object literals to the browser test from the C++ test | 11 * You can pass arbitrary object literals to the browser test from the C++ test |
| 16 * harness as the test data. Each browser test class should implement the run | 12 * harness as the test data. Each browser test class should implement the run |
| 17 * method. | 13 * method. |
| 18 * For example: | 14 * For example: |
| 19 * | 15 * |
| 20 * browserTest.My_Test = function() {}; | 16 * browserTest.My_Test = function() {}; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 37 * | 33 * |
| 38 * You will then invoke the test in C++ by calling: | 34 * You will then invoke the test in C++ by calling: |
| 39 * | 35 * |
| 40 * RunJavaScriptTest(web_content, "My_Test", "{" | 36 * RunJavaScriptTest(web_content, "My_Test", "{" |
| 41 * "pin: '123123'" | 37 * "pin: '123123'" |
| 42 * "}"); | 38 * "}"); |
| 43 */ | 39 */ |
| 44 | 40 |
| 45 'use strict'; | 41 'use strict'; |
| 46 | 42 |
| 43 /** @suppress {duplicate} */ |
| 47 var browserTest = browserTest || {}; | 44 var browserTest = browserTest || {}; |
| 48 | 45 |
| 46 /** @type {window.DomAutomationController} */ |
| 47 browserTest.automationController_ = new window.DomAutomationController(); |
| 48 |
| 49 /** |
| 50 * @return {void} |
| 51 * @suppress {checkTypes|checkVars|reportUnknownTypes} |
| 52 */ |
| 49 browserTest.init = function() { | 53 browserTest.init = function() { |
| 50 // The domAutomationController is used to communicate progress back to the | 54 // The domAutomationController is used to communicate progress back to the |
| 51 // C++ calling code. It will only exist if chrome is run with the flag | 55 // C++ calling code. It will only exist if chrome is run with the flag |
| 52 // --dom-automation. It is stubbed out here so that browser test can be run | 56 // --dom-automation. It is stubbed out here so that browser test can be run |
| 53 // under the regular app. | 57 // under the regular app. |
| 54 browserTest.automationController_ = window.domAutomationController || { | 58 if (window.domAutomationController) { |
| 55 send: function(json) { | 59 /** @type {window.DomAutomationController} */ |
| 56 var result = JSON.parse(json); | 60 browserTest.automationController_ = window.domAutomationController; |
| 57 if (result.succeeded) { | 61 } else { |
| 58 console.log('Test Passed.'); | 62 browserTest.automationController_ = { |
| 59 } else { | 63 send: function(json) { |
| 60 console.error('Test Failed.\n' + | 64 var result = JSON.parse(json); |
| 61 result.error_message + '\n' + result.stack_trace); | 65 if (result.succeeded) { |
| 66 console.log('Test Passed.'); |
| 67 } else { |
| 68 console.error('Test Failed.\n' + |
| 69 result.error_message + '\n' + result.stack_trace); |
| 70 } |
| 62 } | 71 } |
| 63 } | 72 }; |
| 64 }; | 73 }; |
| 65 }; | 74 }; |
| 66 | 75 |
| 67 /** | 76 /** |
| 68 * Fails the C++ calling browser test with |message| if |expr| is false. | 77 * Fails the C++ calling browser test with |message| if |expr| is false. |
| 69 * @param {boolean} expr | 78 * @param {*} expr |
| 70 * @param {string} message | 79 * @param {string} message |
| 80 * @return {void} |
| 71 */ | 81 */ |
| 72 browserTest.expect = function(expr, message) { | 82 browserTest.expect = function(expr, message) { |
| 73 if (!expr) { | 83 if (!expr) { |
| 74 message = (message) ? '<' + message + '>' : ''; | 84 message = (message) ? '<' + message + '>' : ''; |
| 75 browserTest.fail('Expectation failed.' + message); | 85 browserTest.fail('Expectation failed.' + message); |
| 76 } | 86 } |
| 77 }; | 87 }; |
| 78 | 88 |
| 89 /** |
| 90 * @param {string|Error} error |
| 91 * @return {void} |
| 92 */ |
| 79 browserTest.fail = function(error) { | 93 browserTest.fail = function(error) { |
| 80 var error_message = error; | 94 var error_message = error; |
| 81 var stack_trace = base.debug.callstack(); | 95 var stack_trace = base.debug.callstack(); |
| 82 | 96 |
| 83 if (error instanceof Error) { | 97 if (error instanceof Error) { |
| 84 error_message = error.toString(); | 98 error_message = error.toString(); |
| 85 stack_trace = error.stack; | 99 stack_trace = error.stack; |
| 86 } | 100 } |
| 87 | 101 |
| 102 console.error(error_message); |
| 103 |
| 88 // To run browserTest locally: | 104 // To run browserTest locally: |
| 89 // 1. Go to |remoting_webapp_files| and look for | 105 // 1. Go to |remoting_webapp_files| and look for |
| 90 // |remoting_webapp_js_browser_test_files| and uncomment it | 106 // |remoting_webapp_js_browser_test_files| and uncomment it |
| 91 // 2. gclient runhooks | 107 // 2. gclient runhooks |
| 92 // 3. rebuild the webapp | 108 // 3. rebuild the webapp |
| 93 // 4. Run it in the console browserTest.runTest(browserTest.MyTest, {}); | 109 // 4. Run it in the console browserTest.runTest(browserTest.MyTest, {}); |
| 94 // 5. The line below will trap the test in the debugger in case of | 110 // 5. The line below will trap the test in the debugger in case of |
| 95 // failure. | 111 // failure. |
| 96 debugger; | 112 debugger; |
| 97 | 113 |
| 98 browserTest.automationController_.send(JSON.stringify({ | 114 browserTest.automationController_.send(JSON.stringify({ |
| 99 succeeded: false, | 115 succeeded: false, |
| 100 error_message: error_message, | 116 error_message: error_message, |
| 101 stack_trace: stack_trace | 117 stack_trace: stack_trace |
| 102 })); | 118 })); |
| 103 }; | 119 }; |
| 104 | 120 |
| 121 /** |
| 122 * @return {void} |
| 123 */ |
| 105 browserTest.pass = function() { | 124 browserTest.pass = function() { |
| 106 browserTest.automationController_.send(JSON.stringify({ | 125 browserTest.automationController_.send(JSON.stringify({ |
| 107 succeeded: true, | 126 succeeded: true, |
| 108 error_message: '', | 127 error_message: '', |
| 109 stack_trace: '' | 128 stack_trace: '' |
| 110 })); | 129 })); |
| 111 }; | 130 }; |
| 112 | 131 |
| 132 /** |
| 133 * @param {string} id |
| 134 * @return {void} |
| 135 */ |
| 113 browserTest.clickOnControl = function(id) { | 136 browserTest.clickOnControl = function(id) { |
| 114 var element = document.getElementById(id); | 137 var element = document.getElementById(id); |
| 115 browserTest.expect(element, 'No such element: ' + id); | 138 browserTest.expect(element, 'No such element: ' + id); |
| 116 element.click(); | 139 element.click(); |
| 117 }; | 140 }; |
| 118 | 141 |
| 142 /** |
| 143 * @param {remoting.AppMode} expectedMode |
| 144 * @param {number=} opt_timeout |
| 145 * @return {Promise} |
| 146 */ |
| 119 browserTest.onUIMode = function(expectedMode, opt_timeout) { | 147 browserTest.onUIMode = function(expectedMode, opt_timeout) { |
| 120 if (expectedMode == remoting.currentMode) { | 148 if (expectedMode == remoting.currentMode) { |
| 121 // If the current mode is the same as the expected mode, return a fulfilled | 149 // If the current mode is the same as the expected mode, return a fulfilled |
| 122 // promise. For some reason, if we fulfill the promise in the same | 150 // promise. For some reason, if we fulfill the promise in the same |
| 123 // callstack, V8 will assert at V8RecursionScope.h(66) with | 151 // callstack, V8 will assert at V8RecursionScope.h(66) with |
| 124 // ASSERT(!ScriptForbiddenScope::isScriptForbidden()). | 152 // ASSERT(!ScriptForbiddenScope::isScriptForbidden()). |
| 125 // To avoid the assert, execute the callback in a different callstack. | 153 // To avoid the assert, execute the callback in a different callstack. |
| 126 return base.Promise.sleep(0); | 154 return base.Promise.sleep(0); |
| 127 } | 155 } |
| 128 | 156 |
| 129 return new Promise (function(fulfill, reject) { | 157 return new Promise (function(fulfill, reject) { |
| 130 var uiModeChanged = remoting.testEvents.Names.uiModeChanged; | 158 var uiModeChanged = remoting.testEvents.Names.uiModeChanged; |
| 131 var timerId = null; | 159 var timerId = null; |
| 132 | 160 |
| 133 if (opt_timeout === undefined) { | 161 if (opt_timeout === undefined) { |
| 134 opt_timeout = browserTest.Timeout.DEFAULT; | 162 opt_timeout = browserTest.Timeout.DEFAULT; |
| 135 } | 163 } |
| 136 | 164 |
| 137 function onTimeout() { | 165 function onTimeout() { |
| 138 remoting.testEvents.removeEventListener(uiModeChanged, onUIModeChanged); | 166 remoting.testEvents.removeEventListener( |
| 167 uiModeChanged, /** @type {function(?=)} */ (onUIModeChanged)); |
| 139 reject('Timeout waiting for ' + expectedMode); | 168 reject('Timeout waiting for ' + expectedMode); |
| 140 } | 169 } |
| 141 | 170 |
| 171 /** @param {remoting.AppMode} mode */ |
| 142 function onUIModeChanged(mode) { | 172 function onUIModeChanged(mode) { |
| 143 if (mode == expectedMode) { | 173 if (mode == expectedMode) { |
| 144 remoting.testEvents.removeEventListener(uiModeChanged, onUIModeChanged); | 174 remoting.testEvents.removeEventListener( |
| 175 uiModeChanged, /** @type {function(?=)} */ (onUIModeChanged)); |
| 145 window.clearTimeout(timerId); | 176 window.clearTimeout(timerId); |
| 146 timerId = null; | 177 timerId = null; |
| 147 fulfill(); | 178 fulfill(true); |
| 148 } | 179 } |
| 149 } | 180 } |
| 150 | 181 |
| 151 if (opt_timeout != browserTest.Timeout.NONE) { | 182 if (opt_timeout != browserTest.Timeout.NONE) { |
| 152 timerId = window.setTimeout(onTimeout, opt_timeout); | 183 timerId = window.setTimeout(onTimeout, |
| 184 /** @type {number} */ (opt_timeout)); |
| 153 } | 185 } |
| 154 remoting.testEvents.addEventListener(uiModeChanged, onUIModeChanged); | 186 remoting.testEvents.addEventListener( |
| 187 uiModeChanged, /** @type {function(?=)} */ (onUIModeChanged)); |
| 155 }); | 188 }); |
| 156 }; | 189 }; |
| 157 | 190 |
| 191 /** |
| 192 * @return {Promise} |
| 193 */ |
| 158 browserTest.connectMe2Me = function() { | 194 browserTest.connectMe2Me = function() { |
| 159 var AppMode = remoting.AppMode; | 195 var AppMode = remoting.AppMode; |
| 160 browserTest.clickOnControl('this-host-connect'); | 196 browserTest.clickOnControl('this-host-connect'); |
| 161 return browserTest.onUIMode(AppMode.CLIENT_HOST_NEEDS_UPGRADE).then( | 197 return browserTest.onUIMode(AppMode.CLIENT_HOST_NEEDS_UPGRADE).then( |
| 162 function() { | 198 function() { |
| 163 // On fulfilled. | 199 // On fulfilled. |
| 164 browserTest.clickOnControl('host-needs-update-connect-button'); | 200 browserTest.clickOnControl('host-needs-update-connect-button'); |
| 165 }, function() { | 201 }, function() { |
| 166 // On time out. | 202 // On time out. |
| 167 return Promise.resolve(); | 203 return Promise.resolve(); |
| 168 }).then(function() { | 204 }).then(function() { |
| 169 return browserTest.onUIMode(AppMode.CLIENT_PIN_PROMPT, 10000); | 205 return browserTest.onUIMode(AppMode.CLIENT_PIN_PROMPT, 10000); |
| 170 }); | 206 }); |
| 171 }; | 207 }; |
| 172 | 208 |
| 209 /** |
| 210 * @return {Promise} |
| 211 */ |
| 173 browserTest.disconnect = function() { | 212 browserTest.disconnect = function() { |
| 174 var AppMode = remoting.AppMode; | 213 var AppMode = remoting.AppMode; |
| 175 var finishedMode = AppMode.CLIENT_SESSION_FINISHED_ME2ME; | 214 var finishedMode = AppMode.CLIENT_SESSION_FINISHED_ME2ME; |
| 176 var finishedButton = 'client-finished-me2me-button'; | 215 var finishedButton = 'client-finished-me2me-button'; |
| 177 if (remoting.clientSession.getMode() == remoting.ClientSession.Mode.IT2ME) { | 216 if (remoting.desktopConnectedView.getMode() == |
| 217 remoting.DesktopConnectedView.Mode.IT2ME) { |
| 178 finishedMode = AppMode.CLIENT_SESSION_FINISHED_IT2ME; | 218 finishedMode = AppMode.CLIENT_SESSION_FINISHED_IT2ME; |
| 179 finishedButton = 'client-finished-it2me-button'; | 219 finishedButton = 'client-finished-it2me-button'; |
| 180 } | 220 } |
| 181 | 221 |
| 182 remoting.disconnect(); | 222 remoting.disconnect(); |
| 183 | 223 |
| 184 return browserTest.onUIMode(finishedMode).then(function() { | 224 return browserTest.onUIMode(finishedMode).then(function() { |
| 185 browserTest.clickOnControl(finishedButton); | 225 browserTest.clickOnControl(finishedButton); |
| 186 return browserTest.onUIMode(AppMode.HOME); | 226 return browserTest.onUIMode(AppMode.HOME); |
| 187 }); | 227 }); |
| 188 }; | 228 }; |
| 189 | 229 |
| 230 /** |
| 231 * @param {string} pin |
| 232 * @param {?boolean} opt_expectError |
| 233 * @return {Promise} |
| 234 */ |
| 190 browserTest.enterPIN = function(pin, opt_expectError) { | 235 browserTest.enterPIN = function(pin, opt_expectError) { |
| 191 // Wait for 500ms before hitting the PIN button. From experiment, sometimes | 236 // Wait for 500ms before hitting the PIN button. From experiment, sometimes |
| 192 // the PIN prompt does not dismiss without the timeout. | 237 // the PIN prompt does not dismiss without the timeout. |
| 193 var CONNECT_PIN_WAIT = 500; | 238 var CONNECT_PIN_WAIT = 500; |
| 194 | 239 |
| 195 document.getElementById('pin-entry').value = pin; | 240 document.getElementById('pin-entry').value = pin; |
| 196 | 241 |
| 197 return base.Promise.sleep(CONNECT_PIN_WAIT).then(function() { | 242 return base.Promise.sleep(CONNECT_PIN_WAIT).then(function() { |
| 198 browserTest.clickOnControl('pin-connect-button'); | 243 browserTest.clickOnControl('pin-connect-button'); |
| 199 }).then(function() { | 244 }).then(function() { |
| 200 if (opt_expectError) { | 245 if (opt_expectError) { |
| 201 return browserTest.expectConnectionError( | 246 return browserTest.expectConnectionError( |
| 202 remoting.ClientSession.Mode.ME2ME, | 247 remoting.DesktopConnectedView.Mode.ME2ME, |
| 203 remoting.Error.INVALID_ACCESS_CODE); | 248 remoting.Error.INVALID_ACCESS_CODE); |
| 204 } else { | 249 } else { |
| 205 return browserTest.expectConnected(); | 250 return browserTest.expectConnected(); |
| 206 } | 251 } |
| 207 }); | 252 }); |
| 208 }; | 253 }; |
| 209 | 254 |
| 255 /** |
| 256 * @param {remoting.DesktopConnectedView.Mode} connectionMode |
| 257 * @param {string} errorTag |
| 258 * @return {Promise} |
| 259 */ |
| 210 browserTest.expectConnectionError = function(connectionMode, errorTag) { | 260 browserTest.expectConnectionError = function(connectionMode, errorTag) { |
| 211 var AppMode = remoting.AppMode; | 261 var AppMode = remoting.AppMode; |
| 212 var Timeout = browserTest.Timeout; | 262 var Timeout = browserTest.Timeout; |
| 213 | 263 |
| 214 var finishButton = 'client-finished-me2me-button'; | 264 var finishButton = 'client-finished-me2me-button'; |
| 215 var failureMode = AppMode.CLIENT_CONNECT_FAILED_ME2ME; | 265 var failureMode = AppMode.CLIENT_CONNECT_FAILED_ME2ME; |
| 216 | 266 |
| 217 if (connectionMode == remoting.ClientSession.Mode.IT2ME) { | 267 if (connectionMode == remoting.DesktopConnectedView.Mode.IT2ME) { |
| 218 failureMode = AppMode.CLIENT_CONNECT_FAILED_IT2ME; | 268 failureMode = AppMode.CLIENT_CONNECT_FAILED_IT2ME; |
| 219 finishButton = 'client-finished-it2me-button'; | 269 finishButton = 'client-finished-it2me-button'; |
| 220 } | 270 } |
| 221 | 271 |
| 222 var onConnected = browserTest.onUIMode(AppMode.IN_SESSION, Timeout.NONE); | 272 var onConnected = browserTest.onUIMode(AppMode.IN_SESSION, Timeout.NONE); |
| 223 var onFailure = browserTest.onUIMode(failureMode); | 273 var onFailure = browserTest.onUIMode(failureMode); |
| 224 | 274 |
| 225 onConnected = onConnected.then(function() { | 275 onConnected = onConnected.then(function() { |
| 226 return Promise.reject( | 276 return Promise.reject( |
| 227 'Expected the connection to fail.'); | 277 'Expected the connection to fail.'); |
| 228 }); | 278 }); |
| 229 | 279 |
| 230 onFailure = onFailure.then(function() { | 280 onFailure = onFailure.then(function() { |
| 281 /** @type {Element} */ |
| 231 var errorDiv = document.getElementById('connect-error-message'); | 282 var errorDiv = document.getElementById('connect-error-message'); |
| 232 var actual = errorDiv.innerText; | 283 var actual = errorDiv.innerText; |
| 233 var expected = l10n.getTranslationOrError(errorTag); | 284 var expected = l10n.getTranslationOrError(errorTag); |
| 234 browserTest.clickOnControl(finishButton); | 285 browserTest.clickOnControl(finishButton); |
| 235 | 286 |
| 236 if (actual != expected) { | 287 if (actual != expected) { |
| 237 return Promise.reject('Unexpected failure. actual:' + actual + | 288 return Promise.reject('Unexpected failure. actual:' + actual + |
| 238 ' expected:' + expected); | 289 ' expected:' + expected); |
| 239 } | 290 } |
| 240 }); | 291 }); |
| 241 | 292 |
| 242 return Promise.race([onConnected, onFailure]); | 293 return Promise.race([onConnected, onFailure]); |
| 243 }; | 294 }; |
| 244 | 295 |
| 296 /** |
| 297 * @return {Promise} |
| 298 */ |
| 245 browserTest.expectConnected = function() { | 299 browserTest.expectConnected = function() { |
| 246 var AppMode = remoting.AppMode; | 300 var AppMode = remoting.AppMode; |
| 247 // Timeout if the session is not connected within 30 seconds. | 301 // Timeout if the session is not connected within 30 seconds. |
| 248 var SESSION_CONNECTION_TIMEOUT = 30000; | 302 var SESSION_CONNECTION_TIMEOUT = 30000; |
| 249 var onConnected = browserTest.onUIMode(AppMode.IN_SESSION, | 303 var onConnected = browserTest.onUIMode(AppMode.IN_SESSION, |
| 250 SESSION_CONNECTION_TIMEOUT); | 304 SESSION_CONNECTION_TIMEOUT); |
| 251 var onFailure = browserTest.onUIMode(AppMode.CLIENT_CONNECT_FAILED_ME2ME, | 305 var onFailure = browserTest.onUIMode(AppMode.CLIENT_CONNECT_FAILED_ME2ME, |
| 252 browserTest.Timeout.NONE); | 306 browserTest.Timeout.NONE); |
| 253 onFailure = onFailure.then(function() { | 307 onFailure = onFailure.then(function() { |
| 254 var errorDiv = document.getElementById('connect-error-message'); | 308 var errorDiv = document.getElementById('connect-error-message'); |
| 255 var errorMsg = errorDiv.innerText; | 309 var errorMsg = errorDiv.innerText; |
| 256 return Promise.reject('Unexpected error - ' + errorMsg); | 310 return Promise.reject('Unexpected error - ' + errorMsg); |
| 257 }); | 311 }); |
| 258 return Promise.race([onConnected, onFailure]); | 312 return Promise.race([onConnected, onFailure]); |
| 259 }; | 313 }; |
| 260 | 314 |
| 315 /** |
| 316 * @param {base.EventSource} eventSource |
| 317 * @param {string} event |
| 318 * @param {number} timeoutMs |
| 319 * @param {?string} opt_expectedData |
| 320 * @return {Promise} |
| 321 */ |
| 261 browserTest.expectEvent = function(eventSource, event, timeoutMs, | 322 browserTest.expectEvent = function(eventSource, event, timeoutMs, |
| 262 opt_expectedData) { | 323 opt_expectedData) { |
| 263 return new Promise(function(fullfil, reject) { | 324 return new Promise(function(fullfil, reject) { |
| 325 /** @param {string=} actualData */ |
| 264 var verifyEventParameters = function(actualData) { | 326 var verifyEventParameters = function(actualData) { |
| 265 if (opt_expectedData === undefined || opt_expectedData === actualData) { | 327 if (opt_expectedData === undefined || opt_expectedData === actualData) { |
| 266 fullfil(); | 328 fullfil(true); |
| 267 } else { | 329 } else { |
| 268 reject('Bad event data; expected ' + opt_expectedData + | 330 reject('Bad event data; expected ' + opt_expectedData + |
| 269 '; got ' + actualData); | 331 '; got ' + actualData); |
| 270 } | 332 } |
| 271 }; | 333 }; |
| 272 eventSource.addEventListener(event, verifyEventParameters); | 334 eventSource.addEventListener(event, verifyEventParameters); |
| 273 base.Promise.sleep(timeoutMs).then(function() { | 335 base.Promise.sleep(timeoutMs).then(function() { |
| 274 reject(Error('Event ' + event + ' not received after ' + | 336 reject(Error('Event ' + event + ' not received after ' + |
| 275 timeoutMs + 'ms.')); | 337 timeoutMs + 'ms.')); |
| 276 }); | 338 }); |
| 277 }); | 339 }); |
| 278 }; | 340 }; |
| 279 | 341 |
| 342 /** |
| 343 * @param {browserTest.TestableClass} testClass |
| 344 * @param {*} data |
| 345 * @return {void} |
| 346 * @suppress {checkTypes|checkVars|reportUnknownTypes} |
| 347 */ |
| 280 browserTest.runTest = function(testClass, data) { | 348 browserTest.runTest = function(testClass, data) { |
| 281 try { | 349 try { |
| 282 var test = new testClass(); | 350 var test = new testClass(); |
| 283 browserTest.expect(typeof test.run == 'function'); | 351 browserTest.expect(typeof test.run == 'function'); |
| 284 test.run(data); | 352 test.run(data); |
| 285 } catch (e) { | 353 } catch (/** @type {Error} */ e) { |
| 286 browserTest.fail(e); | 354 browserTest.fail(e); |
| 287 } | 355 } |
| 288 }; | 356 }; |
| 289 | 357 |
| 358 /** |
| 359 * @param {string} newPin |
| 360 * @return {Promise} |
| 361 */ |
| 290 browserTest.setupPIN = function(newPin) { | 362 browserTest.setupPIN = function(newPin) { |
| 291 var AppMode = remoting.AppMode; | 363 var AppMode = remoting.AppMode; |
| 292 var HOST_SETUP_WAIT = 10000; | 364 var HOST_SETUP_WAIT = 10000; |
| 293 var Timeout = browserTest.Timeout; | 365 var Timeout = browserTest.Timeout; |
| 294 | 366 |
| 295 return browserTest.onUIMode(AppMode.HOST_SETUP_ASK_PIN).then(function() { | 367 return browserTest.onUIMode(AppMode.HOST_SETUP_ASK_PIN).then(function() { |
| 296 document.getElementById('daemon-pin-entry').value = newPin; | 368 document.getElementById('daemon-pin-entry').value = newPin; |
| 297 document.getElementById('daemon-pin-confirm').value = newPin; | 369 document.getElementById('daemon-pin-confirm').value = newPin; |
| 298 browserTest.clickOnControl('daemon-pin-ok'); | 370 browserTest.clickOnControl('daemon-pin-ok'); |
| 299 | 371 |
| 300 var success = browserTest.onUIMode(AppMode.HOST_SETUP_DONE, Timeout.NONE); | 372 var success = browserTest.onUIMode(AppMode.HOST_SETUP_DONE, Timeout.NONE); |
| 301 var failure = browserTest.onUIMode(AppMode.HOST_SETUP_ERROR, Timeout.NONE); | 373 var failure = browserTest.onUIMode(AppMode.HOST_SETUP_ERROR, Timeout.NONE); |
| 302 failure = failure.then(function(){ | 374 failure = failure.then(function(){ |
| 303 return Promise.reject('Unexpected host setup failure'); | 375 return Promise.reject('Unexpected host setup failure'); |
| 304 }); | 376 }); |
| 305 return Promise.race([success, failure]); | 377 return Promise.race([success, failure]); |
| 306 }).then(function() { | 378 }).then(function() { |
| 307 console.log('browserTest: PIN Setup is done.'); | 379 console.log('browserTest: PIN Setup is done.'); |
| 308 browserTest.clickOnControl('host-config-done-dismiss'); | 380 browserTest.clickOnControl('host-config-done-dismiss'); |
| 309 | 381 |
| 310 // On Linux, we restart the host after changing the PIN, need to sleep | 382 // On Linux, we restart the host after changing the PIN, need to sleep |
| 311 // for ten seconds before the host is ready for connection. | 383 // for ten seconds before the host is ready for connection. |
| 312 return base.Promise.sleep(HOST_SETUP_WAIT); | 384 return base.Promise.sleep(HOST_SETUP_WAIT); |
| 313 }); | 385 }); |
| 314 }; | 386 }; |
| 315 | 387 |
| 388 /** |
| 389 * @return {Promise} |
| 390 */ |
| 316 browserTest.isLocalHostStarted = function() { | 391 browserTest.isLocalHostStarted = function() { |
| 317 return new Promise(function(resolve) { | 392 return new Promise(function(resolve) { |
| 318 remoting.hostController.getLocalHostState(function(state) { | 393 remoting.hostController.getLocalHostState(function(state) { |
| 319 resolve(remoting.HostController.State.STARTED == state); | 394 resolve(remoting.HostController.State.STARTED == state); |
| 320 }); | 395 }); |
| 321 }); | 396 }); |
| 322 }; | 397 }; |
| 323 | 398 |
| 399 /** |
| 400 * @param {string} pin |
| 401 * @return {Promise} |
| 402 */ |
| 324 browserTest.ensureHostStartedWithPIN = function(pin) { | 403 browserTest.ensureHostStartedWithPIN = function(pin) { |
| 325 // Return if host is already | 404 // Return if host is already |
| 326 return browserTest.isLocalHostStarted().then(function(started){ | 405 return browserTest.isLocalHostStarted().then( |
| 327 if (!started) { | 406 /** @param {boolean} started */ |
| 328 console.log('browserTest: Enabling remote connection.'); | 407 function(started){ |
| 329 browserTest.clickOnControl('start-daemon'); | 408 if (!started) { |
| 330 } else { | 409 console.log('browserTest: Enabling remote connection.'); |
| 331 console.log('browserTest: Changing the PIN of the host to: ' + pin + '.'); | 410 browserTest.clickOnControl('start-daemon'); |
| 332 browserTest.clickOnControl('change-daemon-pin'); | 411 } else { |
| 333 } | 412 console.log('browserTest: Changing the PIN of the host to: ' + |
| 334 return browserTest.setupPIN(pin); | 413 pin + '.'); |
| 335 }); | 414 browserTest.clickOnControl('change-daemon-pin'); |
| 336 }; | 415 } |
| 337 | 416 return browserTest.setupPIN(pin); |
| 338 // Called by Browser Test in C++ | 417 }); |
| 339 browserTest.ensureRemoteConnectionEnabled = function(pin) { | |
| 340 browserTest.ensureHostStartedWithPIN(pin).then(function(){ | |
| 341 browserTest.automationController_.send(true); | |
| 342 }, function(errorMessage){ | |
| 343 console.error(errorMessage); | |
| 344 browserTest.automationController_.send(false); | |
| 345 }); | |
| 346 }; | 418 }; |
| 347 | 419 |
| 348 browserTest.init(); | 420 browserTest.init(); |
| OLD | NEW |