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

Side by Side Diff: chrome/browser/extensions/extension_messages_apitest.cc

Issue 16174005: Implement externally_connectable! Web pages can now communicate directly with (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: no find copies 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
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 #include "base/values.h" 5 #include "base/values.h"
6 #include "chrome/browser/extensions/event_router.h" 6 #include "chrome/browser/extensions/event_router.h"
7 #include "chrome/browser/extensions/extension_apitest.h" 7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/extensions/extension_system.h" 8 #include "chrome/browser/extensions/extension_system.h"
9 #include "chrome/browser/profiles/profile.h" 9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
10 #include "chrome/common/chrome_notification_types.h" 12 #include "chrome/common/chrome_notification_types.h"
11 #include "chrome/common/chrome_switches.h" 13 #include "chrome/common/chrome_switches.h"
14 #include "chrome/test/base/ui_test_utils.h"
12 #include "content/public/browser/notification_registrar.h" 15 #include "content/public/browser/notification_registrar.h"
13 #include "content/public/browser/notification_service.h" 16 #include "content/public/browser/notification_service.h"
17 #include "content/public/test/browser_test_utils.h"
14 #include "googleurl/src/gurl.h" 18 #include "googleurl/src/gurl.h"
19 #include "net/dns/mock_host_resolver.h"
20 #include "net/test/embedded_test_server/embedded_test_server.h"
15 21
16 namespace { 22 namespace {
17 23
18 class MessageSender : public content::NotificationObserver { 24 class MessageSender : public content::NotificationObserver {
19 public: 25 public:
20 MessageSender() { 26 MessageSender() {
21 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING, 27 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_DID_STOP_LOADING,
22 content::NotificationService::AllSources()); 28 content::NotificationService::AllSources());
23 } 29 }
24 30
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 class PanelMessagingTest : public ExtensionApiTest { 110 class PanelMessagingTest : public ExtensionApiTest {
105 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE { 111 virtual void SetUpCommandLine(CommandLine* command_line) OVERRIDE {
106 ExtensionApiTest::SetUpCommandLine(command_line); 112 ExtensionApiTest::SetUpCommandLine(command_line);
107 command_line->AppendSwitch(switches::kEnablePanels); 113 command_line->AppendSwitch(switches::kEnablePanels);
108 } 114 }
109 }; 115 };
110 116
111 IN_PROC_BROWSER_TEST_F(PanelMessagingTest, MessagingPanel) { 117 IN_PROC_BROWSER_TEST_F(PanelMessagingTest, MessagingPanel) {
112 ASSERT_TRUE(RunExtensionTest("messaging/connect_panel")) << message_; 118 ASSERT_TRUE(RunExtensionTest("messaging/connect_panel")) << message_;
113 } 119 }
120
121 // Tests externally_connectable between a web page and an extension.
122 //
123 // TODO(kalman): test between extensions. This is already tested in this file,
124 // but not with externally_connectable set in the manifest.
125 //
126 // TODO(kalman): test with host permissions.
Jeffrey Yasskin 2013/06/08 00:28:09 nit: Capitalize comment sentences.
not at google - send to devlin 2013/06/08 02:02:33 Done.
127 class ExternallyConnectableMessagingTest : public ExtensionApiTest {
128 protected:
129 // Result codes from the test. These must match up |results| in
Jeffrey Yasskin 2013/06/08 00:28:09 s/match up |results|//match up with |results|/ ?
not at google - send to devlin 2013/06/08 02:02:33 Done.
130 // c/t/d/extensions/api_test/externally_connectable/sites/assertions.json.
131 enum Result {
132 OK = 0,
133 NAMESPACE_NOT_DEFINED = 1,
134 FUNCTION_NOT_DEFINED = 2,
135 COULD_NOT_ESTABLISH_CONNECTION_ERROR = 3,
136 OTHER_ERROR = 4,
137 INCORRECT_RESPONSE = 5,
138 };
139
140 Result CanConnectAndSendMessages(const std::string& extension_id) {
141 int result;
142 CHECK(content::ExecuteScriptAndExtractInt(
143 browser()->tab_strip_model()->GetActiveWebContents(),
144 "window.assertions.canConnectAndSendMessages('" + extension_id + "')",
145 &result));
146 return static_cast<Result>(result);
147 }
148
149 testing::AssertionResult AreAnyNonMessagingApisDefined() {
150 const std::string non_messaging_apis[] = {
151 "getBackgroundPage"
152 "getManifest"
153 "getURL"
154 "reload"
155 "requestUpdateCheck"
156 "connect"
157 "connectNative"
158 "sendMessage"
Jeffrey Yasskin 2013/06/08 00:28:09 It's odd to see "sendMessage" in a list of non-mes
not at google - send to devlin 2013/06/08 02:02:33 Oops. this test should not pass.
159 "sendNativeMessage"
160 "onStartup"
161 "onInstalled"
162 "onSuspend"
163 "onSuspendCanceled"
164 "onUpdateAvailable"
165 "onBrowserUpdateAvailable"
166 "onConnect"
167 "onConnectExternal"
168 "onMessage"
169 "onMessageExternal"
170 "id"
171 };
172 return AreAnyRuntimePropertiesDefined(std::vector<std::string>(
173 non_messaging_apis,
174 non_messaging_apis + arraysize(non_messaging_apis)));
175 }
176
177 private:
178 testing::AssertionResult AreAnyRuntimePropertiesDefined(
179 const std::vector<std::string>& names) {
180 for (size_t i = 0; i < names.size(); ++i) {
181 if (IsRuntimePropertyDefined(names[i]) == OK)
182 return testing::AssertionSuccess() << names[i] << " is defined";
183 }
184 return testing::AssertionFailure()
185 << "none of " << names.size() << " properties are defined";
186 }
187
188 Result IsRuntimePropertyDefined(const std::string& name) {
189 int result_int;
190 CHECK(content::ExecuteScriptAndExtractInt(
191 browser()->tab_strip_model()->GetActiveWebContents(),
192 "window.assertions.isDefined('" + name + "')",
193 &result_int));
194 return static_cast<Result>(result_int);
195 }
196 };
197
198 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest,
199 ExternallyConnectableMessaging) {
200 const std::string kExtensionDir = "messaging/externally_connectable";
201
202 // The extension allows connections from chromium.org but not google.com.
203 const std::string kChromiumOrg = "www.chromium.org";
204 const std::string kGoogleCom = "www.google.com";
205
206 host_resolver()->AddRule(kChromiumOrg, "127.0.0.1");
207 host_resolver()->AddRule(kGoogleCom, "127.0.0.1");
208
209 embedded_test_server()->ServeFilesFromDirectory(
210 base::FilePath(FILE_PATH_LITERAL(
211 "chrome/test/data/extensions/api_test/" + kExtensionDir)));
212 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
213
214 GURL kChromiumOrgUrl;
215 GURL kGoogleComUrl;
216
217 struct {
218 std::string host;
219 std::string path;
220 GURL* target;
221 } replacements[] = {
222 { kChromiumOrg, "/sites/chromium.org.html", &kChromiumOrgUrl },
223 { kGoogleCom, "/sites/google.com.html", &kGoogleComUrl }
224 };
225
226 for (size_t i = 0; i < arraysize(replacements); ++i) {
227 GURL path_url = embedded_test_server()->GetURL(replacements[i].path);
Jeffrey Yasskin 2013/06/08 00:28:09 Do this as a helper function (returning the GURL)
not at google - send to devlin 2013/06/08 02:02:33 Done.
228 GURL::Replacements replace_host;
229 replace_host.SetHostStr(replacements[i].host);
230 GURL* target = replacements[i].target;
231 if (target)
232 *target = path_url.ReplaceComponents(replace_host);
233 }
234
235 // When the extension isn't installed either should fail.
Jeffrey Yasskin 2013/06/08 00:28:09 "either"? Maybe "neither domain should be able to
not at google - send to devlin 2013/06/08 02:02:33 Done.
not at google - send to devlin 2013/06/08 02:02:33 Done.
236 const std::string kFakeId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
237 ui_test_utils::NavigateToURL(browser(), kChromiumOrgUrl);
238 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
239 CanConnectAndSendMessages(kFakeId));
240 EXPECT_FALSE(AreAnyNonMessagingApisDefined());
241
242 ui_test_utils::NavigateToURL(browser(), kGoogleComUrl);
243 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
244 CanConnectAndSendMessages(kFakeId));
245 EXPECT_FALSE(AreAnyNonMessagingApisDefined());
246
247 // Install the web connectable extension. chromium.org will work, google.com
248 // won't.
249 const extensions::Extension* web_connectable = LoadExtension(
250 test_data_dir_.AppendASCII(kExtensionDir).AppendASCII("web_connectable"));
251
252 ui_test_utils::NavigateToURL(browser(), kChromiumOrgUrl);
253 EXPECT_EQ(OK, CanConnectAndSendMessages(web_connectable->id()));
254 EXPECT_FALSE(AreAnyNonMessagingApisDefined());
255
256 ui_test_utils::NavigateToURL(browser(), kGoogleComUrl);
257 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
258 CanConnectAndSendMessages(web_connectable->id()));
259 EXPECT_FALSE(AreAnyNonMessagingApisDefined());
260
261 // Install the non-connectable extension. Nothing will work.
262 const extensions::Extension* not_connectable = LoadExtension(
263 test_data_dir_.AppendASCII(kExtensionDir).AppendASCII("not_connectable"));
264
265 ui_test_utils::NavigateToURL(browser(), kChromiumOrgUrl);
266 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
267 CanConnectAndSendMessages(not_connectable->id()));
268 EXPECT_FALSE(AreAnyNonMessagingApisDefined());
269
270 ui_test_utils::NavigateToURL(browser(), kGoogleComUrl);
271 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR,
272 CanConnectAndSendMessages(not_connectable->id()));
273 EXPECT_FALSE(AreAnyNonMessagingApisDefined());
274 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698