Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_manager_browsertest / gallery / test_loader.js
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.
4
5 'use strict';
6
7 /**
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.
11  */
12 function loadScript(url) {
13   return new Promise(function(fulfill, reject) {
14     var script = document.createElement('script');
15     script.src = url;
16     // Ensure the scripts are loaded in the order of calling loadScript.
17     script.async = false;
18     script.onload = fulfill;
19     script.onerror = reject.bind(null, 'Faile to load ' + url);
20     document.documentElement.appendChild(script);
21   });
22 }
23
24 var testUtilPromise = loadScript(
25     'chrome-extension:///ejhcmmdhhpdhhgmifplfmjobgegbibkn/test_util.js');
26
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(
32       function(args) {
33         // Do nothing if the guest mode is different.
34         if (JSON.parse(args[0]) !== chrome.extension.inIncognitoContext)
35           return;
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]();
43             }).then(function() {
44               success();
45             }, function(error) {
46               chrome.test.fail(error.stack || error);
47             });
48           };
49           testCase.generatedName = testName;
50           chrome.test.runTests([testCase]);
51         });
52       });
53 }).catch(function(error) {
54   chrome.test.fail(error.stack || error);
55 });