- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / file_manager_browsertest / background.js
1 // Copyright (c) 2013 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 /**
6  * Extension ID of Files.app.
7  * @type {string}
8  * @const
9  */
10 var FILE_MANAGER_EXTENSIONS_ID = 'hhaomjibdihmijegdhdafkllkbggdgoj';
11
12 /**
13  * Calls a remote test util in Files.app's extension. See: test_util.js.
14  *
15  * @param {string} func Function name.
16  * @param {?string} appId Target window's App ID or null for functions
17  *     not requiring a window.
18  * @param {Array.<*>} args Array of arguments.
19  * @param {function(*)} callback Callback handling the function's result.
20  */
21 function callRemoteTestUtil(func, appId, args, callback) {
22   chrome.runtime.sendMessage(
23       FILE_MANAGER_EXTENSIONS_ID, {
24         func: func,
25         appId: appId,
26         args: args
27       },
28       callback);
29 }
30
31 /**
32  * Executes a sequence of test steps.
33  * @constructor
34  */
35 function StepsRunner() {
36   /**
37    * List of steps.
38    * @type {Array.<function>}
39    * @private
40    */
41   this.steps_ = [];
42 }
43
44 /**
45  * Creates a StepsRunner instance and runs the passed steps.
46  */
47 StepsRunner.run = function(steps) {
48   var stepsRunner = new StepsRunner();
49   stepsRunner.run_(steps);
50 };
51
52 StepsRunner.prototype = {
53   /**
54    * @return {function} The next closure.
55    */
56   get next() {
57     return this.steps_[0];
58   }
59 };
60
61 /**
62  * Runs a sequence of the added test steps.
63  * @type {Array.<function>} List of the sequential steps.
64  */
65 StepsRunner.prototype.run_ = function(steps) {
66   this.steps_ = steps.slice(0);
67
68   // An extra step which acts as an empty callback for optional asynchronous
69   // calls in the last provided step.
70   this.steps_.push(function() {});
71
72   this.steps_ = this.steps_.map(function(f) {
73     return chrome.test.callbackPass(function() {
74       this.steps_.shift();
75       f.apply(this, arguments);
76     }.bind(this));
77   }.bind(this));
78
79   this.next();
80 };
81
82 /**
83  * Adds the givin entries to the target volume(s).
84  * @param {Array.<string>} volumeNames Names of target volumes.
85  * @param {Array.<TestEntryInfo>} entries List of entries to be added.
86  * @param {function(boolean)} callback Callback function to be passed the result
87  *     of function. The argument is true on success.
88  */
89 function addEntries(volumeNames, entries, callback) {
90   if (volumeNames.length == 0) {
91     callback(true);
92     return;
93   }
94   chrome.test.sendMessage(JSON.stringify({
95     name: 'addEntries',
96     volume: volumeNames.shift(),
97     entries: entries
98   }), chrome.test.callbackPass(function(result) {
99     if (result == "onEntryAdded")
100       addEntries(volumeNames, entries, callback);
101     else
102       callback(false);
103   }));
104 };
105
106 var steps = [
107   // Check for the guest mode.
108   function() {
109     chrome.test.sendMessage(
110         JSON.stringify({name: 'isInGuestMode'}), steps.shift());
111   },
112   // Obtain the test case name.
113   function(result) {
114     if (JSON.parse(result) != chrome.extension.inIncognitoContext)
115       return;
116     chrome.test.sendMessage(
117         JSON.stringify({name: 'getTestName'}), steps.shift());
118   },
119   // Run the test case.
120   function(testCaseName) {
121     if (!testcase[testCaseName])
122       return;
123     chrome.test.runTests([testcase[testCaseName]]);
124   }
125 ];
126
127 steps.shift()();