| OLD | NEW |
| (Empty) | |
| 1 |
| 2 // We ask the background page to get the extension API to test against. When it |
| 3 // responds we start the test. |
| 4 console.log("asking for api ..."); |
| 5 chrome.extension.sendRequest("getApi", function(apis) { |
| 6 console.log("got api response"); |
| 7 var privilegedPaths = []; |
| 8 var unprivilegedPaths = []; |
| 9 apis.forEach(function(module) { |
| 10 var namespace = module.namespace; |
| 11 if (!module.unprivileged) { |
| 12 privilegedPaths.push(namespace); |
| 13 return; |
| 14 } |
| 15 |
| 16 ["functions", "events"].forEach(function(section) { |
| 17 if (typeof(module[section]) == "undefined") |
| 18 return; |
| 19 module[section].forEach(function(entry) { |
| 20 var path = namespace + "." + entry.name; |
| 21 if (entry.unprivileged) { |
| 22 unprivilegedPaths.push(path); |
| 23 } else { |
| 24 privilegedPaths.push(path); |
| 25 } |
| 26 }); |
| 27 }); |
| 28 |
| 29 if (module.properties) { |
| 30 for (var propName in module.properties) { |
| 31 var path = namespace + "." + propName; |
| 32 if (module.properties[propName].unprivileged) { |
| 33 unprivilegedPaths.push(path); |
| 34 } else { |
| 35 privilegedPaths.push(path); |
| 36 } |
| 37 } |
| 38 } |
| 39 }); |
| 40 doTest(privilegedPaths, unprivilegedPaths); |
| 41 }); |
| 42 |
| 43 |
| 44 // Tests whether missing properties of the chrome object correctly throw an |
| 45 // error on access. The path is a namespace or function/property/event etc. |
| 46 // within a namespace, and is dot-separated. |
| 47 function testPath(path, expectError) { |
| 48 console.log("trying " + path); |
| 49 var parts = path.split('.'); |
| 50 |
| 51 // Iterate over each component of the path, making sure all but the last part |
| 52 // is defined. The last part should either be defined or throw an error on |
| 53 // attempted access. |
| 54 var module = chrome; |
| 55 for (var i = 0; i < parts.length; i++) { |
| 56 if (i < parts.length - 1) { |
| 57 // Not the last component, so expect non-null / no exception. |
| 58 try { |
| 59 module = module[parts[i]]; |
| 60 } catch (err) { |
| 61 console.log("testPath failed on subcomponent of " + path); |
| 62 return false; |
| 63 } |
| 64 } else { |
| 65 // This is the last component - we expect it to either be defined or |
| 66 // to throw an error on access. |
| 67 try { |
| 68 if (typeof(module[parts[i]]) == "undefined") { |
| 69 console.log(" fail (undefined and not throwing error): " + |
| 70 path); |
| 71 return false; |
| 72 } else if (!expectError) { |
| 73 console.log(" ok (defined): " + path); |
| 74 return true; |
| 75 } |
| 76 } catch (err) { |
| 77 if (!expectError) { |
| 78 console.log(" fail (did not expect error): " + path); |
| 79 return false; |
| 80 } |
| 81 var str = err.toString(); |
| 82 if (str.search("is not supported in content scripts") != -1) { |
| 83 console.log(" ok (correct error thrown): " + path); |
| 84 return true; |
| 85 } else { |
| 86 console.log(" fail (wrong error: '" + str + "')"); |
| 87 return false; |
| 88 } |
| 89 } |
| 90 } |
| 91 } |
| 92 console.log(" fail (no error when we were expecting one): " + path); |
| 93 return false; |
| 94 } |
| 95 |
| 96 function displayResult(status) { |
| 97 var div = document.createElement("div"); |
| 98 div.innerHTML = "<h1>" + status + "</h2>"; |
| 99 document.body.appendChild(div); |
| 100 } |
| 101 |
| 102 function reportSuccess() { |
| 103 displayResult("pass"); |
| 104 chrome.extension.sendRequest("pass"); |
| 105 } |
| 106 |
| 107 function reportFailure() { |
| 108 displayResult("fail"); |
| 109 // Let the "fail" show for a little while so you can see it when running |
| 110 // browser_tests in the debugger. |
| 111 setTimeout(function() { |
| 112 chrome.extension.sendRequest("fail"); |
| 113 }, 1000); |
| 114 } |
| 115 |
| 116 // Runs over each string path in privilegedPaths and unprivilegedPaths, testing |
| 117 // to ensure a proper error is thrown on access or the path is defined. |
| 118 function doTest(privilegedPaths, unprivilegedPaths) { |
| 119 console.log("starting"); |
| 120 |
| 121 if (!privilegedPaths || privilegedPaths.length < 1 || !unprivilegedPaths || |
| 122 unprivilegedPaths.length < 1) { |
| 123 port.postMessage("fail"); |
| 124 return; |
| 125 } |
| 126 |
| 127 var failures = []; |
| 128 var success = true; |
| 129 |
| 130 // Returns a function that will test a path and record any failures. |
| 131 function makeTestFunction(expectError) { |
| 132 return function(path) { |
| 133 if (!testPath(path, expectError)) { |
| 134 success = false; |
| 135 failures.push(path); |
| 136 } |
| 137 }; |
| 138 } |
| 139 privilegedPaths.forEach(makeTestFunction(true)); |
| 140 unprivilegedPaths.forEach(makeTestFunction(false)); |
| 141 |
| 142 console.log(success ? "pass" : "fail"); |
| 143 if (success) { |
| 144 reportSuccess(); |
| 145 } else { |
| 146 console.log("failures on:\n" + failures.join("\n")); |
| 147 reportFailure(); |
| 148 } |
| 149 } |
| 150 |
| OLD | NEW |