Chromium Code Reviews| Index: chrome/browser/extensions/extension_messages_apitest.cc |
| diff --git a/chrome/browser/extensions/extension_messages_apitest.cc b/chrome/browser/extensions/extension_messages_apitest.cc |
| index e8de072ca66969eeac17218eb20c4c51322adfa8..197dc509a8e28fb55a436bbfe21ada21419ce49b 100644 |
| --- a/chrome/browser/extensions/extension_messages_apitest.cc |
| +++ b/chrome/browser/extensions/extension_messages_apitest.cc |
| @@ -7,11 +7,17 @@ |
| #include "chrome/browser/extensions/extension_apitest.h" |
| #include "chrome/browser/extensions/extension_system.h" |
| #include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/tabs/tab_strip_model.h" |
| #include "chrome/common/chrome_notification_types.h" |
| #include "chrome/common/chrome_switches.h" |
| +#include "chrome/test/base/ui_test_utils.h" |
| #include "content/public/browser/notification_registrar.h" |
| #include "content/public/browser/notification_service.h" |
| +#include "content/public/test/browser_test_utils.h" |
| #include "googleurl/src/gurl.h" |
| +#include "net/dns/mock_host_resolver.h" |
| +#include "net/test/embedded_test_server/embedded_test_server.h" |
| namespace { |
| @@ -111,3 +117,158 @@ class PanelMessagingTest : public ExtensionApiTest { |
| IN_PROC_BROWSER_TEST_F(PanelMessagingTest, MessagingPanel) { |
| ASSERT_TRUE(RunExtensionTest("messaging/connect_panel")) << message_; |
| } |
| + |
| +// Tests externally_connectable between a web page and an extension. |
| +// |
| +// TODO(kalman): test between extensions. This is already tested in this file, |
| +// but not with externally_connectable set in the manifest. |
| +// |
| +// 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.
|
| +class ExternallyConnectableMessagingTest : public ExtensionApiTest { |
| + protected: |
| + // 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.
|
| + // c/t/d/extensions/api_test/externally_connectable/sites/assertions.json. |
| + enum Result { |
| + OK = 0, |
| + NAMESPACE_NOT_DEFINED = 1, |
| + FUNCTION_NOT_DEFINED = 2, |
| + COULD_NOT_ESTABLISH_CONNECTION_ERROR = 3, |
| + OTHER_ERROR = 4, |
| + INCORRECT_RESPONSE = 5, |
| + }; |
| + |
| + Result CanConnectAndSendMessages(const std::string& extension_id) { |
| + int result; |
| + CHECK(content::ExecuteScriptAndExtractInt( |
| + browser()->tab_strip_model()->GetActiveWebContents(), |
| + "window.assertions.canConnectAndSendMessages('" + extension_id + "')", |
| + &result)); |
| + return static_cast<Result>(result); |
| + } |
| + |
| + testing::AssertionResult AreAnyNonMessagingApisDefined() { |
| + const std::string non_messaging_apis[] = { |
| + "getBackgroundPage" |
| + "getManifest" |
| + "getURL" |
| + "reload" |
| + "requestUpdateCheck" |
| + "connect" |
| + "connectNative" |
| + "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.
|
| + "sendNativeMessage" |
| + "onStartup" |
| + "onInstalled" |
| + "onSuspend" |
| + "onSuspendCanceled" |
| + "onUpdateAvailable" |
| + "onBrowserUpdateAvailable" |
| + "onConnect" |
| + "onConnectExternal" |
| + "onMessage" |
| + "onMessageExternal" |
| + "id" |
| + }; |
| + return AreAnyRuntimePropertiesDefined(std::vector<std::string>( |
| + non_messaging_apis, |
| + non_messaging_apis + arraysize(non_messaging_apis))); |
| + } |
| + |
| + private: |
| + testing::AssertionResult AreAnyRuntimePropertiesDefined( |
| + const std::vector<std::string>& names) { |
| + for (size_t i = 0; i < names.size(); ++i) { |
| + if (IsRuntimePropertyDefined(names[i]) == OK) |
| + return testing::AssertionSuccess() << names[i] << " is defined"; |
| + } |
| + return testing::AssertionFailure() |
| + << "none of " << names.size() << " properties are defined"; |
| + } |
| + |
| + Result IsRuntimePropertyDefined(const std::string& name) { |
| + int result_int; |
| + CHECK(content::ExecuteScriptAndExtractInt( |
| + browser()->tab_strip_model()->GetActiveWebContents(), |
| + "window.assertions.isDefined('" + name + "')", |
| + &result_int)); |
| + return static_cast<Result>(result_int); |
| + } |
| +}; |
| + |
| +IN_PROC_BROWSER_TEST_F(ExternallyConnectableMessagingTest, |
| + ExternallyConnectableMessaging) { |
| + const std::string kExtensionDir = "messaging/externally_connectable"; |
| + |
| + // The extension allows connections from chromium.org but not google.com. |
| + const std::string kChromiumOrg = "www.chromium.org"; |
| + const std::string kGoogleCom = "www.google.com"; |
| + |
| + host_resolver()->AddRule(kChromiumOrg, "127.0.0.1"); |
| + host_resolver()->AddRule(kGoogleCom, "127.0.0.1"); |
| + |
| + embedded_test_server()->ServeFilesFromDirectory( |
| + base::FilePath(FILE_PATH_LITERAL( |
| + "chrome/test/data/extensions/api_test/" + kExtensionDir))); |
| + ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
| + |
| + GURL kChromiumOrgUrl; |
| + GURL kGoogleComUrl; |
| + |
| + struct { |
| + std::string host; |
| + std::string path; |
| + GURL* target; |
| + } replacements[] = { |
| + { kChromiumOrg, "/sites/chromium.org.html", &kChromiumOrgUrl }, |
| + { kGoogleCom, "/sites/google.com.html", &kGoogleComUrl } |
| + }; |
| + |
| + for (size_t i = 0; i < arraysize(replacements); ++i) { |
| + 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.
|
| + GURL::Replacements replace_host; |
| + replace_host.SetHostStr(replacements[i].host); |
| + GURL* target = replacements[i].target; |
| + if (target) |
| + *target = path_url.ReplaceComponents(replace_host); |
| + } |
| + |
| + // 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.
|
| + const std::string kFakeId = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| + ui_test_utils::NavigateToURL(browser(), kChromiumOrgUrl); |
| + EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
| + CanConnectAndSendMessages(kFakeId)); |
| + EXPECT_FALSE(AreAnyNonMessagingApisDefined()); |
| + |
| + ui_test_utils::NavigateToURL(browser(), kGoogleComUrl); |
| + EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
| + CanConnectAndSendMessages(kFakeId)); |
| + EXPECT_FALSE(AreAnyNonMessagingApisDefined()); |
| + |
| + // Install the web connectable extension. chromium.org will work, google.com |
| + // won't. |
| + const extensions::Extension* web_connectable = LoadExtension( |
| + test_data_dir_.AppendASCII(kExtensionDir).AppendASCII("web_connectable")); |
| + |
| + ui_test_utils::NavigateToURL(browser(), kChromiumOrgUrl); |
| + EXPECT_EQ(OK, CanConnectAndSendMessages(web_connectable->id())); |
| + EXPECT_FALSE(AreAnyNonMessagingApisDefined()); |
| + |
| + ui_test_utils::NavigateToURL(browser(), kGoogleComUrl); |
| + EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
| + CanConnectAndSendMessages(web_connectable->id())); |
| + EXPECT_FALSE(AreAnyNonMessagingApisDefined()); |
| + |
| + // Install the non-connectable extension. Nothing will work. |
| + const extensions::Extension* not_connectable = LoadExtension( |
| + test_data_dir_.AppendASCII(kExtensionDir).AppendASCII("not_connectable")); |
| + |
| + ui_test_utils::NavigateToURL(browser(), kChromiumOrgUrl); |
| + EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
| + CanConnectAndSendMessages(not_connectable->id())); |
| + EXPECT_FALSE(AreAnyNonMessagingApisDefined()); |
| + |
| + ui_test_utils::NavigateToURL(browser(), kGoogleComUrl); |
| + EXPECT_EQ(COULD_NOT_ESTABLISH_CONNECTION_ERROR, |
| + CanConnectAndSendMessages(not_connectable->id())); |
| + EXPECT_FALSE(AreAnyNonMessagingApisDefined()); |
| +} |