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
3 // found in the LICENSE file.
8 * Loads a test script from URL.
9 * @param {string} url URL of a script file.
10 * @return {Promise} Promise to be fulfilled when completing to load.
12 function loadScript(url) {
13 return new Promise(function(fulfill, reject) {
14 var script = document.createElement('script');
16 // Ensure the scripts are loaded in the order of calling loadScript.
18 script.onload = fulfill;
19 script.onerror = reject.bind(null, 'Faile to load ' + url);
20 document.documentElement.appendChild(script);
24 var testUtilPromise = loadScript(
25 'chrome-extension:///ejhcmmdhhpdhhgmifplfmjobgegbibkn/test_util.js');
27 testUtilPromise.then(function() {
28 var inGuestModePromise = sendTestMessage({name: 'isInGuestMode'});
29 var testNamePromise = sendTestMessage({name: 'getTestName'});
30 var scriptsPromise = sendTestMessage({name: 'getScripts'});
31 Promise.all([inGuestModePromise, testNamePromise, scriptsPromise]).then(
33 // Do nothing if the guest mode is different.
34 if (JSON.parse(args[0]) !== chrome.extension.inIncognitoContext)
36 var scripts = JSON.parse(args[2]);
37 return Promise.all(scripts.map(loadScript)).then(function() {
38 var testName = args[1];
39 var testCase = function() {
40 var success = chrome.test.callbackPass();
41 Promise.resolve().then(function() {
42 return window[testName]();
46 chrome.test.fail(error.stack || error);
49 testCase.generatedName = testName;
50 chrome.test.runTests([testCase]);
53 }).catch(function(error) {
54 chrome.test.fail(error.stack || error);