| 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_ = null; |
| 48 |
| 49 /** |
| 50 * @return {void} |
| 51 * @suppress {checkTypes|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(uiModeChanged, onUIModeChanged); |
| 139 reject('Timeout waiting for ' + expectedMode); | 167 reject('Timeout waiting for ' + expectedMode); |
| 140 } | 168 } |
| 141 | 169 |
| 170 /** @param {remoting.AppMode} mode */ |
| 142 function onUIModeChanged(mode) { | 171 function onUIModeChanged(mode) { |
| 143 if (mode == expectedMode) { | 172 if (mode == expectedMode) { |
| 144 remoting.testEvents.removeEventListener(uiModeChanged, onUIModeChanged); | 173 remoting.testEvents.removeEventListener(uiModeChanged, onUIModeChanged); |
| 145 window.clearTimeout(timerId); | 174 window.clearTimeout(timerId); |
| 146 timerId = null; | 175 timerId = null; |
| 147 fulfill(); | 176 fulfill(true); |
| 148 } | 177 } |
| 149 } | 178 } |
| 150 | 179 |
| 151 if (opt_timeout != browserTest.Timeout.NONE) { | 180 if (opt_timeout != browserTest.Timeout.NONE) { |
| 152 timerId = window.setTimeout(onTimeout, opt_timeout); | 181 timerId = window.setTimeout(onTimeout, |
| 182 /** @type {number} */ (opt_timeout)); |
| 153 } | 183 } |
| 154 remoting.testEvents.addEventListener(uiModeChanged, onUIModeChanged); | 184 remoting.testEvents.addEventListener(uiModeChanged, onUIModeChanged); |
| 155 }); | 185 }); |
| 156 }; | 186 }; |
| 157 | 187 |
| 188 /** |
| 189 * @return {Promise} |
| 190 */ |
| 158 browserTest.connectMe2Me = function() { | 191 browserTest.connectMe2Me = function() { |
| 159 var AppMode = remoting.AppMode; | 192 var AppMode = remoting.AppMode; |
| 160 browserTest.clickOnControl('this-host-connect'); | 193 browserTest.clickOnControl('this-host-connect'); |
| 161 return browserTest.onUIMode(AppMode.CLIENT_HOST_NEEDS_UPGRADE).then( | 194 return browserTest.onUIMode(AppMode.CLIENT_HOST_NEEDS_UPGRADE).then( |
| 162 function() { | 195 function() { |
| 163 // On fulfilled. | 196 // On fulfilled. |
| 164 browserTest.clickOnControl('host-needs-update-connect-button'); | 197 browserTest.clickOnControl('host-needs-update-connect-button'); |
| 165 }, function() { | 198 }, function() { |
| 166 // On time out. | 199 // On time out. |
| 167 return Promise.resolve(); | 200 return Promise.resolve(); |
| 168 }).then(function() { | 201 }).then(function() { |
| 169 return browserTest.onUIMode(AppMode.CLIENT_PIN_PROMPT, 10000); | 202 return browserTest.onUIMode(AppMode.CLIENT_PIN_PROMPT, 10000); |
| 170 }); | 203 }); |
| 171 }; | 204 }; |
| 172 | 205 |
| 206 /** |
| 207 * @return {Promise} |
| 208 */ |
| 173 browserTest.disconnect = function() { | 209 browserTest.disconnect = function() { |
| 174 var AppMode = remoting.AppMode; | 210 var AppMode = remoting.AppMode; |
| 175 var finishedMode = AppMode.CLIENT_SESSION_FINISHED_ME2ME; | 211 var finishedMode = AppMode.CLIENT_SESSION_FINISHED_ME2ME; |
| 176 var finishedButton = 'client-finished-me2me-button'; | 212 var finishedButton = 'client-finished-me2me-button'; |
| 177 if (remoting.clientSession.getMode() == remoting.ClientSession.Mode.IT2ME) { | 213 if (remoting.desktopConnectedView.getMode() == |
| 214 remoting.DesktopConnectedView.Mode.IT2ME) { |
| 178 finishedMode = AppMode.CLIENT_SESSION_FINISHED_IT2ME; | 215 finishedMode = AppMode.CLIENT_SESSION_FINISHED_IT2ME; |
| 179 finishedButton = 'client-finished-it2me-button'; | 216 finishedButton = 'client-finished-it2me-button'; |
| 180 } | 217 } |
| 181 | 218 |
| 182 remoting.disconnect(); | 219 remoting.disconnect(); |
| 183 | 220 |
| 184 return browserTest.onUIMode(finishedMode).then(function() { | 221 return browserTest.onUIMode(finishedMode).then(function() { |
| 185 browserTest.clickOnControl(finishedButton); | 222 browserTest.clickOnControl(finishedButton); |
| 186 return browserTest.onUIMode(AppMode.HOME); | 223 return browserTest.onUIMode(AppMode.HOME); |
| 187 }); | 224 }); |
| 188 }; | 225 }; |
| 189 | 226 |
| 227 /** |
| 228 * @param {string} pin |
| 229 * @param {?boolean} opt_expectError |
| 230 * @return {Promise} |
| 231 */ |
| 190 browserTest.enterPIN = function(pin, opt_expectError) { | 232 browserTest.enterPIN = function(pin, opt_expectError) { |
| 191 // Wait for 500ms before hitting the PIN button. From experiment, sometimes | 233 // Wait for 500ms before hitting the PIN button. From experiment, sometimes |
| 192 // the PIN prompt does not dismiss without the timeout. | 234 // the PIN prompt does not dismiss without the timeout. |
| 193 var CONNECT_PIN_WAIT = 500; | 235 var CONNECT_PIN_WAIT = 500; |
| 194 | 236 |
| 195 document.getElementById('pin-entry').value = pin; | 237 document.getElementById('pin-entry').value = pin; |
| 196 | 238 |
| 197 return base.Promise.sleep(CONNECT_PIN_WAIT).then(function() { | 239 return base.Promise.sleep(CONNECT_PIN_WAIT).then(function() { |
| 198 browserTest.clickOnControl('pin-connect-button'); | 240 browserTest.clickOnControl('pin-connect-button'); |
| 199 }).then(function() { | 241 }).then(function() { |
| 200 if (opt_expectError) { | 242 if (opt_expectError) { |
| 201 return browserTest.expectConnectionError( | 243 return browserTest.expectConnectionError( |
| 202 remoting.ClientSession.Mode.ME2ME, | 244 remoting.DesktopConnectedView.Mode.ME2ME, |
| 203 remoting.Error.INVALID_ACCESS_CODE); | 245 remoting.Error.INVALID_ACCESS_CODE); |
| 204 } else { | 246 } else { |
| 205 return browserTest.expectConnected(); | 247 return browserTest.expectConnected(); |
| 206 } | 248 } |
| 207 }); | 249 }); |
| 208 }; | 250 }; |
| 209 | 251 |
| 252 /** |
| 253 * @param {remoting.DesktopConnectedView.Mode} connectionMode |
| 254 * @param {string} errorTag |
| 255 * @return {Promise} |
| 256 */ |
| 210 browserTest.expectConnectionError = function(connectionMode, errorTag) { | 257 browserTest.expectConnectionError = function(connectionMode, errorTag) { |
| 211 var AppMode = remoting.AppMode; | 258 var AppMode = remoting.AppMode; |
| 212 var Timeout = browserTest.Timeout; | 259 var Timeout = browserTest.Timeout; |
| 213 | 260 |
| 214 var finishButton = 'client-finished-me2me-button'; | 261 var finishButton = 'client-finished-me2me-button'; |
| 215 var failureMode = AppMode.CLIENT_CONNECT_FAILED_ME2ME; | 262 var failureMode = AppMode.CLIENT_CONNECT_FAILED_ME2ME; |
| 216 | 263 |
| 217 if (connectionMode == remoting.ClientSession.Mode.IT2ME) { | 264 if (connectionMode == remoting.DesktopConnectedView.Mode.IT2ME) { |
| 218 failureMode = AppMode.CLIENT_CONNECT_FAILED_IT2ME; | 265 failureMode = AppMode.CLIENT_CONNECT_FAILED_IT2ME; |
| 219 finishButton = 'client-finished-it2me-button'; | 266 finishButton = 'client-finished-it2me-button'; |
| 220 } | 267 } |
| 221 | 268 |
| 222 var onConnected = browserTest.onUIMode(AppMode.IN_SESSION, Timeout.NONE); | 269 var onConnected = browserTest.onUIMode(AppMode.IN_SESSION, Timeout.NONE); |
| 223 var onFailure = browserTest.onUIMode(failureMode); | 270 var onFailure = browserTest.onUIMode(failureMode); |
| 224 | 271 |
| 225 onConnected = onConnected.then(function() { | 272 onConnected = onConnected.then(function() { |
| 226 return Promise.reject( | 273 return Promise.reject( |
| 227 'Expected the connection to fail.'); | 274 'Expected the connection to fail.'); |
| 228 }); | 275 }); |
| 229 | 276 |
| 230 onFailure = onFailure.then(function() { | 277 onFailure = onFailure.then(function() { |
| 278 /** @type {Element} */ |
| 231 var errorDiv = document.getElementById('connect-error-message'); | 279 var errorDiv = document.getElementById('connect-error-message'); |
| 232 var actual = errorDiv.innerText; | 280 var actual = errorDiv.innerText; |
| 233 var expected = l10n.getTranslationOrError(errorTag); | 281 var expected = l10n.getTranslationOrError(errorTag); |
| 234 browserTest.clickOnControl(finishButton); | 282 browserTest.clickOnControl(finishButton); |
| 235 | 283 |
| 236 if (actual != expected) { | 284 if (actual != expected) { |
| 237 return Promise.reject('Unexpected failure. actual:' + actual + | 285 return Promise.reject('Unexpected failure. actual:' + actual + |
| 238 ' expected:' + expected); | 286 ' expected:' + expected); |
| 239 } | 287 } |
| 240 }); | 288 }); |
| 241 | 289 |
| 242 return Promise.race([onConnected, onFailure]); | 290 return Promise.race([onConnected, onFailure]); |
| 243 }; | 291 }; |
| 244 | 292 |
| 293 /** |
| 294 * @return {Promise} |
| 295 */ |
| 245 browserTest.expectConnected = function() { | 296 browserTest.expectConnected = function() { |
| 246 var AppMode = remoting.AppMode; | 297 var AppMode = remoting.AppMode; |
| 247 // Timeout if the session is not connected within 30 seconds. | 298 // Timeout if the session is not connected within 30 seconds. |
| 248 var SESSION_CONNECTION_TIMEOUT = 30000; | 299 var SESSION_CONNECTION_TIMEOUT = 30000; |
| 249 var onConnected = browserTest.onUIMode(AppMode.IN_SESSION, | 300 var onConnected = browserTest.onUIMode(AppMode.IN_SESSION, |
| 250 SESSION_CONNECTION_TIMEOUT); | 301 SESSION_CONNECTION_TIMEOUT); |
| 251 var onFailure = browserTest.onUIMode(AppMode.CLIENT_CONNECT_FAILED_ME2ME, | 302 var onFailure = browserTest.onUIMode(AppMode.CLIENT_CONNECT_FAILED_ME2ME, |
| 252 browserTest.Timeout.NONE); | 303 browserTest.Timeout.NONE); |
| 253 onFailure = onFailure.then(function() { | 304 onFailure = onFailure.then(function() { |
| 254 var errorDiv = document.getElementById('connect-error-message'); | 305 var errorDiv = document.getElementById('connect-error-message'); |
| 255 var errorMsg = errorDiv.innerText; | 306 var errorMsg = errorDiv.innerText; |
| 256 return Promise.reject('Unexpected error - ' + errorMsg); | 307 return Promise.reject('Unexpected error - ' + errorMsg); |
| 257 }); | 308 }); |
| 258 return Promise.race([onConnected, onFailure]); | 309 return Promise.race([onConnected, onFailure]); |
| 259 }; | 310 }; |
| 260 | 311 |
| 312 /** |
| 313 * @param {base.EventSource} eventSource |
| 314 * @param {string} event |
| 315 * @param {number} timeoutMs |
| 316 * @param {?string} opt_expectedData |
| 317 * @return {Promise} |
| 318 */ |
| 261 browserTest.expectEvent = function(eventSource, event, timeoutMs, | 319 browserTest.expectEvent = function(eventSource, event, timeoutMs, |
| 262 opt_expectedData) { | 320 opt_expectedData) { |
| 263 return new Promise(function(fullfil, reject) { | 321 return new Promise(function(fullfil, reject) { |
| 322 /** @param {string=} actualData */ |
| 264 var verifyEventParameters = function(actualData) { | 323 var verifyEventParameters = function(actualData) { |
| 265 if (opt_expectedData === undefined || opt_expectedData === actualData) { | 324 if (opt_expectedData === undefined || opt_expectedData === actualData) { |
| 266 fullfil(); | 325 fullfil(true); |
| 267 } else { | 326 } else { |
| 268 reject('Bad event data; expected ' + opt_expectedData + | 327 reject('Bad event data; expected ' + opt_expectedData + |
| 269 '; got ' + actualData); | 328 '; got ' + actualData); |
| 270 } | 329 } |
| 271 }; | 330 }; |
| 272 eventSource.addEventListener(event, verifyEventParameters); | 331 eventSource.addEventListener(event, verifyEventParameters); |
| 273 base.Promise.sleep(timeoutMs).then(function() { | 332 base.Promise.sleep(timeoutMs).then(function() { |
| 274 reject(Error('Event ' + event + ' not received after ' + | 333 reject(Error('Event ' + event + ' not received after ' + |
| 275 timeoutMs + 'ms.')); | 334 timeoutMs + 'ms.')); |
| 276 }); | 335 }); |
| 277 }); | 336 }); |
| 278 }; | 337 }; |
| 279 | 338 |
| 339 /** |
| 340 * @param {Function} testClass |
| 341 * @param {*} data |
| 342 * @return {void} |
| 343 * @suppress {checkTypes|checkVars|reportUnknownTypes} |
| 344 */ |
| 280 browserTest.runTest = function(testClass, data) { | 345 browserTest.runTest = function(testClass, data) { |
| 281 try { | 346 try { |
| 282 var test = new testClass(); | 347 var test = new testClass(); |
| 283 browserTest.expect(typeof test.run == 'function'); | 348 browserTest.expect(typeof test.run == 'function'); |
| 284 test.run(data); | 349 test.run(data); |
| 285 } catch (e) { | 350 } catch (/** @type {Error} */ e) { |
| 286 browserTest.fail(e); | 351 browserTest.fail(e); |
| 287 } | 352 } |
| 288 }; | 353 }; |
| 289 | 354 |
| 355 /** |
| 356 * @param {string} newPin |
| 357 * @return {Promise} |
| 358 */ |
| 290 browserTest.setupPIN = function(newPin) { | 359 browserTest.setupPIN = function(newPin) { |
| 291 var AppMode = remoting.AppMode; | 360 var AppMode = remoting.AppMode; |
| 292 var HOST_SETUP_WAIT = 10000; | 361 var HOST_SETUP_WAIT = 10000; |
| 293 var Timeout = browserTest.Timeout; | 362 var Timeout = browserTest.Timeout; |
| 294 | 363 |
| 295 return browserTest.onUIMode(AppMode.HOST_SETUP_ASK_PIN).then(function() { | 364 return browserTest.onUIMode(AppMode.HOST_SETUP_ASK_PIN).then(function() { |
| 296 document.getElementById('daemon-pin-entry').value = newPin; | 365 document.getElementById('daemon-pin-entry').value = newPin; |
| 297 document.getElementById('daemon-pin-confirm').value = newPin; | 366 document.getElementById('daemon-pin-confirm').value = newPin; |
| 298 browserTest.clickOnControl('daemon-pin-ok'); | 367 browserTest.clickOnControl('daemon-pin-ok'); |
| 299 | 368 |
| 300 var success = browserTest.onUIMode(AppMode.HOST_SETUP_DONE, Timeout.NONE); | 369 var success = browserTest.onUIMode(AppMode.HOST_SETUP_DONE, Timeout.NONE); |
| 301 var failure = browserTest.onUIMode(AppMode.HOST_SETUP_ERROR, Timeout.NONE); | 370 var failure = browserTest.onUIMode(AppMode.HOST_SETUP_ERROR, Timeout.NONE); |
| 302 failure = failure.then(function(){ | 371 failure = failure.then(function(){ |
| 303 return Promise.reject('Unexpected host setup failure'); | 372 return Promise.reject('Unexpected host setup failure'); |
| 304 }); | 373 }); |
| 305 return Promise.race([success, failure]); | 374 return Promise.race([success, failure]); |
| 306 }).then(function() { | 375 }).then(function() { |
| 307 console.log('browserTest: PIN Setup is done.'); | 376 console.log('browserTest: PIN Setup is done.'); |
| 308 browserTest.clickOnControl('host-config-done-dismiss'); | 377 browserTest.clickOnControl('host-config-done-dismiss'); |
| 309 | 378 |
| 310 // On Linux, we restart the host after changing the PIN, need to sleep | 379 // On Linux, we restart the host after changing the PIN, need to sleep |
| 311 // for ten seconds before the host is ready for connection. | 380 // for ten seconds before the host is ready for connection. |
| 312 return base.Promise.sleep(HOST_SETUP_WAIT); | 381 return base.Promise.sleep(HOST_SETUP_WAIT); |
| 313 }); | 382 }); |
| 314 }; | 383 }; |
| 315 | 384 |
| 385 /** |
| 386 * @return {Promise} |
| 387 */ |
| 316 browserTest.isLocalHostStarted = function() { | 388 browserTest.isLocalHostStarted = function() { |
| 317 return new Promise(function(resolve) { | 389 return new Promise(function(resolve) { |
| 318 remoting.hostController.getLocalHostState(function(state) { | 390 remoting.hostController.getLocalHostState(function(state) { |
| 319 resolve(remoting.HostController.State.STARTED == state); | 391 resolve(remoting.HostController.State.STARTED == state); |
| 320 }); | 392 }); |
| 321 }); | 393 }); |
| 322 }; | 394 }; |
| 323 | 395 |
| 396 /** |
| 397 * @param {string} pin |
| 398 * @return {Promise} |
| 399 */ |
| 324 browserTest.ensureHostStartedWithPIN = function(pin) { | 400 browserTest.ensureHostStartedWithPIN = function(pin) { |
| 325 // Return if host is already | 401 // Return if host is already |
| 326 return browserTest.isLocalHostStarted().then(function(started){ | 402 return browserTest.isLocalHostStarted().then( |
| 327 if (!started) { | 403 /** @param {boolean} started */ |
| 328 console.log('browserTest: Enabling remote connection.'); | 404 function(started){ |
| 329 browserTest.clickOnControl('start-daemon'); | 405 if (!started) { |
| 330 } else { | 406 console.log('browserTest: Enabling remote connection.'); |
| 331 console.log('browserTest: Changing the PIN of the host to: ' + pin + '.'); | 407 browserTest.clickOnControl('start-daemon'); |
| 332 browserTest.clickOnControl('change-daemon-pin'); | 408 } else { |
| 333 } | 409 console.log('browserTest: Changing the PIN of the host to: ' + |
| 334 return browserTest.setupPIN(pin); | 410 pin + '.'); |
| 411 browserTest.clickOnControl('change-daemon-pin'); |
| 412 } |
| 413 return browserTest.setupPIN(pin); |
| 414 }); |
| 415 }; |
| 416 |
| 417 /** |
| 418 * Called by Browser Test in C++ |
| 419 * @param {string} pin |
| 420 * @suppress {checkTypes} |
| 421 */ |
| 422 browserTest.ensureRemoteConnectionEnabled = function(pin) { |
| 423 browserTest.ensureHostStartedWithPIN(pin).then(function() { |
| 424 browserTest.pass(); |
| 425 }, function(reason) { |
| 426 browserTest.fail(reason); |
| 335 }); | 427 }); |
| 336 }; | 428 }; |
| 337 | 429 |
| 338 // Called by Browser Test in C++ | |
| 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 }; | |
| 347 | |
| 348 browserTest.init(); | 430 browserTest.init(); |
| OLD | NEW |