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 // Utils provide logging functions and other JS functions commonly used by the | 5 // Utils provide logging functions and other JS functions commonly used by the |
6 // app and media players. | 6 // app and media players. |
7 var Utils = new function() { | 7 var Utils = new function() { |
8 this.titleChanged = false; | 8 this.titleChanged = false; |
9 }; | 9 }; |
10 | 10 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
207 }; | 207 }; |
208 | 208 |
209 Utils.resetTitleChange = function() { | 209 Utils.resetTitleChange = function() { |
210 this.titleChanged = false; | 210 this.titleChanged = false; |
211 document.title = ''; | 211 document.title = ''; |
212 }; | 212 }; |
213 | 213 |
214 Utils.sendRequest = function(requestType, responseType, message, serverURL, | 214 Utils.sendRequest = function(requestType, responseType, message, serverURL, |
215 onSuccessCallbackFn, forceInvalidResponse) { | 215 onSuccessCallbackFn, forceInvalidResponse) { |
216 var requestAttemptCount = 0; | 216 var requestAttemptCount = 0; |
217 var MAXIMUM_REQUEST_ATTEMPTS = 3; | |
218 var REQUEST_RETRY_DELAY_MS = 3000; | 217 var REQUEST_RETRY_DELAY_MS = 3000; |
| 218 var REQUEST_TIMEOUT_MS = 1000; |
219 | 219 |
220 function sendRequestAttempt() { | 220 function sendRequestAttempt() { |
| 221 // No limit on the number of retries. This will retry on failures |
| 222 // until the test framework stops the test. |
221 requestAttemptCount++; | 223 requestAttemptCount++; |
222 if (requestAttemptCount == MAXIMUM_REQUEST_ATTEMPTS) { | |
223 Utils.failTest('FAILED: Exceeded maximum license request attempts.'); | |
224 return; | |
225 } | |
226 var xmlhttp = new XMLHttpRequest(); | 224 var xmlhttp = new XMLHttpRequest(); |
227 xmlhttp.responseType = responseType; | 225 xmlhttp.responseType = responseType; |
228 xmlhttp.open(requestType, serverURL, true); | 226 xmlhttp.open(requestType, serverURL, true); |
229 xmlhttp.onerror = function(e) { | 227 xmlhttp.onerror = function(e) { |
230 Utils.timeLog('Request status: ' + this.statusText); | 228 Utils.timeLog('Request status: ' + this.statusText); |
231 Utils.failTest('FAILED: License request XHR failed with network error.'); | 229 Utils.timeLog('FAILED: License request XHR failed with network error.'); |
| 230 Utils.timeLog('Retrying request in ' + REQUEST_RETRY_DELAY_MS + 'ms'); |
| 231 setTimeout(sendRequestAttempt, REQUEST_RETRY_DELAY_MS); |
232 }; | 232 }; |
233 xmlhttp.onload = function(e) { | 233 xmlhttp.onload = function(e) { |
234 if (this.status == 200) { | 234 if (this.status == 200) { |
235 if (onSuccessCallbackFn) | 235 if (onSuccessCallbackFn) |
236 onSuccessCallbackFn(this.response); | 236 onSuccessCallbackFn(this.response); |
237 } else { | 237 } else { |
238 Utils.timeLog('Bad response status: ' + this.status); | 238 Utils.timeLog('Bad response status: ' + this.status); |
239 Utils.timeLog('Bad response: ' + this.response); | 239 Utils.timeLog('Bad response: ' + this.response); |
240 Utils.timeLog('Retrying request if possible in ' + | 240 Utils.timeLog('Retrying request in ' + REQUEST_RETRY_DELAY_MS + 'ms'); |
241 REQUEST_RETRY_DELAY_MS + 'ms'); | |
242 setTimeout(sendRequestAttempt, REQUEST_RETRY_DELAY_MS); | 241 setTimeout(sendRequestAttempt, REQUEST_RETRY_DELAY_MS); |
243 } | 242 } |
244 }; | 243 }; |
| 244 xmlhttp.timeout = REQUEST_TIMEOUT_MS; |
| 245 xmlhttp.ontimeout = function(e) { |
| 246 Utils.timeLog('Request timeout'); |
| 247 Utils.timeLog('Retrying request in ' + REQUEST_RETRY_DELAY_MS + 'ms'); |
| 248 setTimeout(sendRequestAttempt, REQUEST_RETRY_DELAY_MS); |
| 249 } |
245 Utils.timeLog('Attempt (' + requestAttemptCount + | 250 Utils.timeLog('Attempt (' + requestAttemptCount + |
246 '): sending request to server: ' + serverURL); | 251 '): sending request to server: ' + serverURL); |
247 xmlhttp.send(message); | 252 xmlhttp.send(message); |
248 } | 253 } |
249 | 254 |
250 if (forceInvalidResponse) { | 255 if (forceInvalidResponse) { |
251 Utils.timeLog('Not sending request - forcing an invalid response.'); | 256 Utils.timeLog('Not sending request - forcing an invalid response.'); |
252 return onSuccessCallbackFn([0xAA]); | 257 return onSuccessCallbackFn([0xAA]); |
253 } | 258 } |
254 sendRequestAttempt(); | 259 sendRequestAttempt(); |
(...skipping 15 matching lines...) Expand all Loading... |
270 var time = Utils.getCurrentTimeString(); | 275 var time = Utils.getCurrentTimeString(); |
271 // Log to document. | 276 // Log to document. |
272 Utils.documentLog(arguments[0], time); | 277 Utils.documentLog(arguments[0], time); |
273 // Log to JS console. | 278 // Log to JS console. |
274 var logString = time + ' - '; | 279 var logString = time + ' - '; |
275 for (var i = 0; i < arguments.length; i++) { | 280 for (var i = 0; i < arguments.length; i++) { |
276 logString += ' ' + arguments[i]; | 281 logString += ' ' + arguments[i]; |
277 } | 282 } |
278 console.log(logString); | 283 console.log(logString); |
279 }; | 284 }; |
OLD | NEW |