OLD | NEW |
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 Loading... |
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. |
| 127 class ExternallyConnectableMessagingTest : public ExtensionApiTest { |
| 128 protected: |
| 129 // Result codes from the test. These must match up with |results| in |
| 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_SENDER = 5, |
| 138 INCORRECT_RESPONSE_MESSAGE = 6, |
| 139 }; |
| 140 |
| 141 Result CanConnectAndSendMessages(const std::string& extension_id) { |
| 142 int result; |
| 143 CHECK(content::ExecuteScriptAndExtractInt( |
| 144 browser()->tab_strip_model()->GetActiveWebContents(), |
| 145 "window.assertions.canConnectAndSendMessages('" + extension_id + "')", |
| 146 &result)); |
| 147 return static_cast<Result>(result); |
| 148 } |
| 149 |
| 150 testing::AssertionResult AreAnyNonWebApisDefined() { |
| 151 // All runtime API methods are non-web except for sendRequest and connect. |
| 152 const std::string non_messaging_apis[] = { |
| 153 "getBackgroundPage", |
| 154 "getManifest", |
| 155 "getURL", |
| 156 "reload", |
| 157 "requestUpdateCheck", |
| 158 "connectNative", |
| 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 GURL GetURLForPath(const std::string& host, const std::string& path) { |
| 178 GURL path_url = embedded_test_server()->GetURL(path); |
| 179 GURL::Replacements replace_host; |
| 180 replace_host.SetHostStr(host); |
| 181 return path_url.ReplaceComponents(replace_host); |
| 182 } |
| 183 |
| 184 private: |
| 185 testing::AssertionResult AreAnyRuntimePropertiesDefined( |
| 186 const std::vector<std::string>& names) { |
| 187 for (size_t i = 0; i < names.size(); ++i) { |
| 188 if (IsRuntimePropertyDefined(names[i]) == OK) |
| 189 return testing::AssertionSuccess() << names[i] << " is defined"; |
| 190 } |
| 191 return testing::AssertionFailure() |
| 192 << "none of " << names.size() << " properties are defined"; |
| 193 } |
| 194 |
| 195 Result IsRuntimePropertyDefined(const std::string& name) { |
| 196 int result_int; |
| 197 CHECK(content::ExecuteScriptAndExtractInt( |
| 198 browser()->tab_strip_model()->GetActiveWebContents(), |
| 199 "window.assertions.isDefined('" + name + "')", |
| 200 &result_int)); |
| 201 return static_cast<Result>(result_int); |
| 202 } |
| 203 }; |
| 204 |
| 205 IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, |
| 206 ExternallyConnectableMessaging) { |
| 207 const std::string kExtensionDir = "messaging/externally_connectable"; |
| 208 |
| 209 // The extension allows connections from chromium.org but not google.com. |
| 210 const std::string kChromiumOrg = "www.chromium.org"; |
| 211 const std::string kGoogleCom = "www.google.com"; |
| 212 |
| 213 host_resolver()->AddRule(kChromiumOrg, "127.0.0.1"); |
| 214 host_resolver()->AddRule(kGoogleCom, "127.0.0.1"); |
| 215 |
| 216 embedded_test_server()->ServeFilesFromDirectory( |
| 217 base::FilePath(FILE_PATH_LITERAL( |
| 218 "chrome/test/data/extensions/api_test/" + kExtensionDir))); |
| 219 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| 220 |
| 221 const GURL kChromiumOrgUrl = |
| 222 GetURLForPath(kChromiumOrg, "/sites/chromium.org.html"); |
| 223 const GURL kGoogleComUrl = |
| 224 GetURLForPath(kGoogleCom, "/sites/google.com.html"); |
| 225 |
| 226 // When an extension isn't installed all attempts to connect to it should |
| 227 // fail. |
| 228 const std::string kFakeId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 229 ui_test_utils::NavigateToURL(browser(), kChromiumOrgUrl); |
| 230 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
| 231 CanConnectAndSendMessages(kFakeId)); |
| 232 EXPECT_FALSE(AreAnyNonWebApisDefined()); |
| 233 |
| 234 ui_test_utils::NavigateToURL(browser(), kGoogleComUrl); |
| 235 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
| 236 CanConnectAndSendMessages(kFakeId)); |
| 237 EXPECT_FALSE(AreAnyNonWebApisDefined()); |
| 238 |
| 239 // Install the web connectable extension. chromium.org can connect to it, |
| 240 // google.com can't. |
| 241 const extensions::Extension* web_connectable = LoadExtension( |
| 242 test_data_dir_.AppendASCII(kExtensionDir).AppendASCII("web_connectable")); |
| 243 |
| 244 ui_test_utils::NavigateToURL(browser(), kChromiumOrgUrl); |
| 245 EXPECT_EQ(OK, CanConnectAndSendMessages(web_connectable->id())); |
| 246 EXPECT_FALSE(AreAnyNonWebApisDefined()); |
| 247 |
| 248 ui_test_utils::NavigateToURL(browser(), kGoogleComUrl); |
| 249 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
| 250 CanConnectAndSendMessages(web_connectable->id())); |
| 251 EXPECT_FALSE(AreAnyNonWebApisDefined()); |
| 252 |
| 253 // Install the non-connectable extension. Nothing can connect to it. |
| 254 const extensions::Extension* not_connectable = LoadExtension( |
| 255 test_data_dir_.AppendASCII(kExtensionDir).AppendASCII("not_connectable")); |
| 256 |
| 257 ui_test_utils::NavigateToURL(browser(), kChromiumOrgUrl); |
| 258 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
| 259 CanConnectAndSendMessages(not_connectable->id())); |
| 260 EXPECT_FALSE(AreAnyNonWebApisDefined()); |
| 261 |
| 262 ui_test_utils::NavigateToURL(browser(), kGoogleComUrl); |
| 263 EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
| 264 CanConnectAndSendMessages(not_connectable->id())); |
| 265 EXPECT_FALSE(AreAnyNonWebApisDefined()); |
| 266 } |
OLD | NEW |