Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: chrome/test/data/extensions/api_test/stubs/content_script.js

Issue 16174005: Implement externally_connectable! Web pages can now communicate directly with (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: absolute path... Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/test/data/extensions/api_test/messaging/externally_connectable/web_connectable/manifest.json ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Helper function to log message to both the local console and to the 5 // Helper function to log message to both the local console and to the
6 // background page, so that the latter can output the message via the 6 // background page, so that the latter can output the message via the
7 // chrome.test.log() function. 7 // chrome.test.log() function.
8 function logToConsoleAndStdout(msg) { 8 function logToConsoleAndStdout(msg) {
9 console.log(msg); 9 console.log(msg);
10 chrome.extension.sendRequest("log: " + msg); 10 chrome.extension.sendRequest("log: " + msg);
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 66
67 // Tests whether missing properties of the chrome object correctly throw an 67 // Tests whether missing properties of the chrome object correctly throw an
68 // error on access. The path is a namespace or function/property/event etc. 68 // error on access. The path is a namespace or function/property/event etc.
69 // within a namespace, and is dot-separated. 69 // within a namespace, and is dot-separated.
70 function testPath(path, expectError) { 70 function testPath(path, expectError) {
71 var parts = path.split('.'); 71 var parts = path.split('.');
72 72
73 var module = chrome; 73 var module = chrome;
74 for (var i = 0; i < parts.length; i++) { 74 for (var i = 0; i < parts.length; i++) {
75 if (i < parts.length - 1) { 75 if (i < parts.length - 1) {
76 // Not the last component. Should not throw an exception, but allowed to 76 // Not the last component. Allowed to be undefined because some paths are
77 // be undefined (because some paths are only defined on some platforms). 77 // only defined on some platforms.
78 try { 78 module = module[parts[i]];
79 module = module[parts[i]];
80 } catch (err) {
81 logToConsoleAndStdout("testPath failed on " +
82 parts.slice(0, i+1).join('.') + '(' + err + ')');
83 return false;
84 }
85 if (typeof(module) == "undefined") 79 if (typeof(module) == "undefined")
86 return true; 80 return true;
87 } else { 81 } else {
88 // This is the last component - we expect it to either be undefined or 82 // This is the last component - we expect it to either be undefined or
89 // to throw an error on access. 83 // to throw an error on access.
90 try { 84 if (typeof(module[parts[i]]) == "undefined" &&
91 if (typeof(module[parts[i]]) == "undefined" && 85 // lastError being defined depends on there being an error obviously.
92 path != "extension.lastError" && 86 path != "extension.lastError" &&
93 path != "runtime.lastError") { 87 path != "runtime.lastError") {
94 if (expectError) { 88 if (expectError) {
95 return true;
96 } else {
97 logToConsoleAndStdout(" fail (should not be undefined): " + path);
98 return false;
99 }
100 } else if (!expectError) {
101 return true; 89 return true;
102 } 90 } else {
103 } catch (err) { 91 logToConsoleAndStdout(" fail (should not be undefined): " + path);
104 if (!expectError) {
105 logToConsoleAndStdout(" fail (did not expect error): " + path);
106 return false; 92 return false;
107 } 93 }
108 var str = err.toString(); 94 } else if (!expectError) {
109 if (str.search("can only be used in extension processes.") != -1) { 95 return true;
110 return true;
111 } else {
112 logToConsoleAndStdout(
113 "fail: " + path + " (wrong error: '" + str + "')");
114 return false;
115 }
116 } 96 }
117 } 97 }
118 } 98 }
119 logToConsoleAndStdout(" fail (no error when we were expecting one): " + path); 99 logToConsoleAndStdout(" fail (no error when we were expecting one): " + path);
120 return false; 100 return false;
121 } 101 }
122 102
123 function displayResult(status) { 103 function displayResult(status) {
124 var div = document.createElement("div"); 104 var div = document.createElement("div");
125 div.innerHTML = "<h1>" + status + "</h2>"; 105 div.innerHTML = "<h1>" + status + "</h2>";
(...skipping 24 matching lines...) Expand all
150 reportFailure(); 130 reportFailure();
151 return; 131 return;
152 } 132 }
153 133
154 var failures = []; 134 var failures = [];
155 var success = true; 135 var success = true;
156 136
157 // Returns a function that will test a path and record any failures. 137 // Returns a function that will test a path and record any failures.
158 function makeTestFunction(expectError) { 138 function makeTestFunction(expectError) {
159 return function(path) { 139 return function(path) {
160 if (!testPath(path, expectError)) { 140 // runtime.connect and runtime.sendMessage are available in all contexts,
141 // unlike the runtime API in general.
142 var expectErrorForPath = expectError &&
143 path != 'runtime.connect' &&
144 path != 'runtime.sendMessage';
145 if (!testPath(path, expectErrorForPath)) {
161 success = false; 146 success = false;
162 failures.push(path); 147 failures.push(path);
163 } 148 }
164 }; 149 };
165 } 150 }
166 privilegedPaths.forEach(makeTestFunction(true)); 151 privilegedPaths.forEach(makeTestFunction(true));
167 unprivilegedPaths.forEach(makeTestFunction(false)); 152 unprivilegedPaths.forEach(makeTestFunction(false));
168 153
169 console.log(success ? "pass" : "fail"); 154 console.log(success ? "pass" : "fail");
170 if (success) { 155 if (success) {
171 reportSuccess(); 156 reportSuccess();
172 } else { 157 } else {
173 logToConsoleAndStdout("failures on:\n" + failures.join("\n") + 158 logToConsoleAndStdout("failures on:\n" + failures.join("\n") +
174 "\n\n\n>>> See comment in stubs_apitest.cc for a " + 159 "\n\n\n>>> See comment in stubs_apitest.cc for a " +
175 "hint about fixing this failure.\n\n"); 160 "hint about fixing this failure.\n\n");
176 reportFailure(); 161 reportFailure();
177 } 162 }
178 } 163 }
OLDNEW
« no previous file with comments | « chrome/test/data/extensions/api_test/messaging/externally_connectable/web_connectable/manifest.json ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698