OLD | NEW |
1 | 1 |
2 // Helper function to log message to both the local console and to the | 2 // Helper function to log message to both the local console and to the |
3 // background page, so that the latter can output the message via the | 3 // background page, so that the latter can output the message via the |
4 // chrome.test.log() function. | 4 // chrome.test.log() function. |
5 function logToConsoleAndStdout(msg) { | 5 function logToConsoleAndStdout(msg) { |
6 console.log(msg); | 6 console.log(msg); |
7 chrome.extension.sendRequest("log: " + msg); | 7 chrome.extension.sendRequest("log: " + msg); |
8 } | 8 } |
9 | 9 |
10 // We ask the background page to get the extension API to test against. When it | 10 // We ask the background page to get the extension API to test against. When it |
11 // responds we start the test. | 11 // responds we start the test. |
12 console.log("asking for api ..."); | 12 console.log("asking for api ..."); |
13 chrome.extension.sendRequest("getApi", function(apis) { | 13 chrome.extension.sendRequest("getApi", function(apis) { |
14 console.log("got api response"); | 14 console.log("got api response"); |
15 var privilegedPaths = []; | 15 var privilegedPaths = []; |
16 var unprivilegedPaths = []; | 16 var unprivilegedPaths = []; |
17 apis.forEach(function(module) { | 17 apis.forEach(function(module) { |
18 var namespace = module.namespace; | 18 var namespace = module.namespace; |
19 if (!module.unprivileged) { | |
20 privilegedPaths.push(namespace); | |
21 return; | |
22 } | |
23 | 19 |
24 ["functions", "events"].forEach(function(section) { | 20 ["functions", "events"].forEach(function(section) { |
25 if (typeof(module[section]) == "undefined") | 21 if (typeof(module[section]) == "undefined") |
26 return; | 22 return; |
27 module[section].forEach(function(entry) { | 23 module[section].forEach(function(entry) { |
28 var path = namespace + "." + entry.name; | 24 var path = namespace + "." + entry.name; |
29 if (entry.unprivileged) { | 25 if (entry.unprivileged) { |
30 unprivilegedPaths.push(path); | 26 unprivilegedPaths.push(path); |
31 } else { | 27 } else { |
32 privilegedPaths.push(path); | 28 privilegedPaths.push(path); |
(...skipping 13 matching lines...) Expand all Loading... |
46 } | 42 } |
47 }); | 43 }); |
48 doTest(privilegedPaths, unprivilegedPaths); | 44 doTest(privilegedPaths, unprivilegedPaths); |
49 }); | 45 }); |
50 | 46 |
51 | 47 |
52 // Tests whether missing properties of the chrome object correctly throw an | 48 // Tests whether missing properties of the chrome object correctly throw an |
53 // error on access. The path is a namespace or function/property/event etc. | 49 // error on access. The path is a namespace or function/property/event etc. |
54 // within a namespace, and is dot-separated. | 50 // within a namespace, and is dot-separated. |
55 function testPath(path, expectError) { | 51 function testPath(path, expectError) { |
56 console.log("trying " + path); | |
57 var parts = path.split('.'); | 52 var parts = path.split('.'); |
58 | 53 |
59 // Iterate over each component of the path, making sure all but the last part | |
60 // is defined. The last part should either be defined or throw an error on | |
61 // attempted access. | |
62 var module = chrome; | 54 var module = chrome; |
63 for (var i = 0; i < parts.length; i++) { | 55 for (var i = 0; i < parts.length; i++) { |
64 if (i < parts.length - 1) { | 56 if (i < parts.length - 1) { |
65 // Not the last component, so expect non-null / no exception. | 57 // Not the last component. Should not throw an exception, but allowed to |
| 58 // be undefined (because some paths are only defined on some platforms). |
66 try { | 59 try { |
67 module = module[parts[i]]; | 60 module = module[parts[i]]; |
68 } catch (err) { | 61 } catch (err) { |
69 logToConsoleAndStdout("testPath failed on " + | 62 logToConsoleAndStdout("testPath failed on " + |
70 parts.slice(0, i+1).join('.') + '(' + err + ')'); | 63 parts.slice(0, i+1).join('.') + '(' + err + ')'); |
71 return false; | 64 return false; |
72 } | 65 } |
| 66 if (typeof(module) == "undefined") |
| 67 return true; |
73 } else { | 68 } else { |
74 // This is the last component - we expect it to either be defined or | 69 // This is the last component - we expect it to either be defined or |
75 // to throw an error on access. | 70 // to throw an error on access. |
76 try { | 71 try { |
77 if (typeof(module[parts[i]]) == "undefined" && | 72 if (typeof(module[parts[i]]) == "undefined" && |
78 path != "extension.lastError") { | 73 path != "extension.lastError") { |
79 logToConsoleAndStdout(" fail (undefined and not throwing error): " + | 74 logToConsoleAndStdout(" fail (undefined and not throwing error): " + |
80 path); | 75 path); |
81 return false; | 76 return false; |
82 } else if (!expectError) { | 77 } else if (!expectError) { |
83 console.log(" ok (defined): " + path); | |
84 return true; | 78 return true; |
85 } | 79 } |
86 } catch (err) { | 80 } catch (err) { |
87 if (!expectError) { | 81 if (!expectError) { |
88 logToConsoleAndStdout(" fail (did not expect error): " + path); | 82 logToConsoleAndStdout(" fail (did not expect error): " + path); |
89 return false; | 83 return false; |
90 } | 84 } |
91 var str = err.toString(); | 85 var str = err.toString(); |
92 if (str.search("can only be used in extension processes.") != -1) { | 86 if (str.search("can only be used in extension processes.") != -1) { |
93 console.log(" ok (correct error thrown): " + path); | |
94 return true; | 87 return true; |
95 } else { | 88 } else { |
96 logToConsoleAndStdout(" fail (wrong error: '" + str + "')"); | 89 logToConsoleAndStdout( |
| 90 "fail: " + path + " (wrong error: '" + str + "')"); |
97 return false; | 91 return false; |
98 } | 92 } |
99 } | 93 } |
100 } | 94 } |
101 } | 95 } |
102 logToConsoleAndStdout(" fail (no error when we were expecting one): " + path); | 96 logToConsoleAndStdout(" fail (no error when we were expecting one): " + path); |
103 return false; | 97 return false; |
104 } | 98 } |
105 | 99 |
106 function displayResult(status) { | 100 function displayResult(status) { |
(...skipping 16 matching lines...) Expand all Loading... |
123 }, 1000); | 117 }, 1000); |
124 } | 118 } |
125 | 119 |
126 // Runs over each string path in privilegedPaths and unprivilegedPaths, testing | 120 // Runs over each string path in privilegedPaths and unprivilegedPaths, testing |
127 // to ensure a proper error is thrown on access or the path is defined. | 121 // to ensure a proper error is thrown on access or the path is defined. |
128 function doTest(privilegedPaths, unprivilegedPaths) { | 122 function doTest(privilegedPaths, unprivilegedPaths) { |
129 console.log("starting"); | 123 console.log("starting"); |
130 | 124 |
131 if (!privilegedPaths || privilegedPaths.length < 1 || !unprivilegedPaths || | 125 if (!privilegedPaths || privilegedPaths.length < 1 || !unprivilegedPaths || |
132 unprivilegedPaths.length < 1) { | 126 unprivilegedPaths.length < 1) { |
133 port.postMessage("fail"); | 127 reportFailure(); |
134 return; | 128 return; |
135 } | 129 } |
136 | 130 |
137 var failures = []; | 131 var failures = []; |
138 var success = true; | 132 var success = true; |
139 | 133 |
140 // Returns a function that will test a path and record any failures. | 134 // Returns a function that will test a path and record any failures. |
141 function makeTestFunction(expectError) { | 135 function makeTestFunction(expectError) { |
142 return function(path) { | 136 return function(path) { |
143 if (!testPath(path, expectError)) { | 137 if (!testPath(path, expectError)) { |
144 success = false; | 138 success = false; |
145 failures.push(path); | 139 failures.push(path); |
146 } | 140 } |
147 }; | 141 }; |
148 } | 142 } |
149 privilegedPaths.forEach(makeTestFunction(true)); | 143 privilegedPaths.forEach(makeTestFunction(true)); |
150 unprivilegedPaths.forEach(makeTestFunction(false)); | 144 unprivilegedPaths.forEach(makeTestFunction(false)); |
151 | 145 |
152 console.log(success ? "pass" : "fail"); | 146 console.log(success ? "pass" : "fail"); |
153 if (success) { | 147 if (success) { |
154 reportSuccess(); | 148 reportSuccess(); |
155 } else { | 149 } else { |
156 logToConsoleAndStdout("failures on:\n" + failures.join("\n") + | 150 logToConsoleAndStdout("failures on:\n" + failures.join("\n") + |
157 "\n\n\n>>> See comment in stubs_apitest.cc for a " + | 151 "\n\n\n>>> See comment in stubs_apitest.cc for a " + |
158 "hint about fixing this failure.\n\n"); | 152 "hint about fixing this failure.\n\n"); |
159 reportFailure(); | 153 reportFailure(); |
160 } | 154 } |
161 } | 155 } |
OLD | NEW |