- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / test / data / extensions / api_test / chromeos_info_private / background.js
1 // Copyright (c) 2011 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 var pass = chrome.test.callbackPass;
6 var fail = chrome.test.callbackFail;
7
8 function getTestFunctionFor(keys, fails) {
9   return function generatedTest () {
10     // Debug.
11     console.warn("keys: " + keys + "; fails: " + fails);
12
13     chrome.chromeosInfoPrivate.get(
14         keys,
15         pass(
16           function(values) {
17             for (var i = 0; i < keys.length; ++i) {
18               // Debug
19               if (keys[i] in values) {
20                 console.log("  values['" + keys[i] + "'] = " +
21                             values[keys[i]]);
22               } else {
23                 console.log("  " + keys[i] + " is missing in values");
24               }
25
26               chrome.test.assertEq(fails.indexOf(keys[i]) == -1,
27                                    keys[i] in values);
28             }
29           }
30         )
31     );
32   }
33 }
34
35 // Automatically generates tests for the given possible keys. Note, this
36 // tests do not check return value, only the fact that it is presented.
37 function generateTestsForKeys(keys) {
38   var tests = [];
39   // Test with all the keys at one.
40   tests.push(getTestFunctionFor(keys, []));
41   // Tests with key which hasn't corresponding value.
42   var noValueKey = "noValueForThisKey";
43   tests.push(getTestFunctionFor([noValueKey], [noValueKey]));
44
45   if (keys.length > 1) {
46     // Tests with the separate keys.
47     for (var i = 0; i < keys.length; ++i) {
48       tests.push(getTestFunctionFor([keys[i]], []));
49     }
50   }
51   if (keys.length >= 2) {
52     tests.push(getTestFunctionFor([keys[0], keys[1]], []));
53     tests.push(getTestFunctionFor([keys[0], noValueKey, keys[1]],
54                                   [noValueKey]));
55   }
56   return tests;
57 }
58
59 function timezoneSetTest() {
60   chrome.chromeosInfoPrivate.set('timezone', 'Pacific/Kiritimati');
61   chrome.chromeosInfoPrivate.get(
62       ["timezone"],
63       pass(
64         function(values) {
65           chrome.test.assertEq(values['timezone'],
66                                'Pacific/Kiritimati');
67         }
68       ));
69 }
70
71 // Run generated chrome.chromeosInfoPrivate.get() tests.
72 var tests = generateTestsForKeys(["hwid",
73                                   "homeProvider",
74                                   "initialLocale",
75                                   "board",
76                                   "isOwner",
77                                   "timezone",
78                                   "supportedTimezones"])
79
80 // Add chrome.chromeosInfoPrivate.set() test.
81 tests.push(timezoneSetTest);
82
83 chrome.test.runTests(tests);