base::Time multiplicative operator overloading
[chromium-blink-merge.git] / chrome / browser / extensions / extension_webui_apitest.cc
blob66333ccb4b5cef9bd58e7d2e5cca7c6d2438e476
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/files/file_path.h"
6 #include "base/files/file_util.h"
7 #include "base/path_service.h"
8 #include "base/values.h"
9 #include "chrome/browser/extensions/extension_apitest.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/common/chrome_paths.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/render_frame_host.h"
15 #include "content/public/browser/web_contents.h"
16 #include "content/public/test/browser_test_utils.h"
17 #include "extensions/browser/event_router.h"
18 #include "extensions/common/api/test.h"
19 #include "extensions/common/extension.h"
20 #include "extensions/test/extension_test_message_listener.h"
21 #include "testing/gtest/include/gtest/gtest.h"
23 namespace extensions {
25 namespace OnMessage = core_api::test::OnMessage;
27 namespace {
29 // Tests running extension APIs on WebUI.
30 class ExtensionWebUITest : public ExtensionApiTest {
31 protected:
32 testing::AssertionResult RunTest(const char* name,
33 const GURL& page_url,
34 const GURL& frame_url,
35 bool expected_result) {
36 // Tests are located in chrome/test/data/extensions/webui/$(name).
37 base::FilePath path;
38 PathService::Get(chrome::DIR_TEST_DATA, &path);
39 path =
40 path.AppendASCII("extensions").AppendASCII("webui").AppendASCII(name);
42 // Read the test.
43 if (!base::PathExists(path))
44 return testing::AssertionFailure() << "Couldn't find " << path.value();
45 std::string script;
46 base::ReadFileToString(path, &script);
47 script = "(function(){'use strict';" + script + "}());";
49 // Run the test.
50 bool actual_result = false;
51 content::RenderFrameHost* webui = NavigateToWebUI(page_url, frame_url);
52 if (!webui)
53 return testing::AssertionFailure() << "Failed to navigate to WebUI";
54 CHECK(content::ExecuteScriptAndExtractBool(webui, script, &actual_result));
55 return (expected_result == actual_result)
56 ? testing::AssertionSuccess()
57 : (testing::AssertionFailure() << "Check console output");
60 testing::AssertionResult RunTestOnExtensionsFrame(const char* name) {
61 // In the current extension WebUI design, the content is actually hosted in
62 // an iframe at chrome://extensions-frame.
63 return RunTest(name,
64 GURL("chrome://extensions"),
65 GURL("chrome://extensions-frame"),
66 true); // tests on chrome://extensions-frame should succeed
69 testing::AssertionResult RunTestOnChromeExtensionsFrame(const char* name) {
70 // Like RunTestOnExtensionsFrame, but chrome://extensions is an alias for
71 // chrome://chrome/extensions so test it explicitly.
72 return RunTest(name,
73 GURL("chrome://chrome/extensions"),
74 GURL("chrome://extensions-frame"),
75 true); // tests on chrome://extensions-frame should succeed
78 testing::AssertionResult RunTestOnChromeExtensions(const char* name) {
79 // Despite the extensions page being hosted in an iframe, also test the
80 // top-level chrome://extensions page (which actually loads
81 // chrome://chrome/extensions). In the past there was a bug where top-level
82 // extension WebUI bindings weren't correctly set up.
83 return RunTest(name,
84 GURL("chrome://chrome/extensions"),
85 GURL("chrome://chrome/extensions"),
86 true); // tests on chrome://chrome/extensions should succeed
89 testing::AssertionResult RunTestOnAbout(const char* name) {
90 // chrome://about is an innocuous page that doesn't have any bindings.
91 // Tests should fail.
92 return RunTest(name,
93 GURL("chrome://about"),
94 GURL("chrome://about"),
95 false); // tests on chrome://about should fail
98 private:
99 // Navigates the browser to a WebUI page and returns the RenderFrameHost for
100 // that page.
101 content::RenderFrameHost* NavigateToWebUI(const GURL& page_url,
102 const GURL& frame_url) {
103 ui_test_utils::NavigateToURL(browser(), page_url);
105 content::WebContents* active_web_contents =
106 browser()->tab_strip_model()->GetActiveWebContents();
108 if (active_web_contents->GetLastCommittedURL() == frame_url)
109 return active_web_contents->GetMainFrame();
111 return FrameMatchingPredicate(
112 active_web_contents,
113 base::Bind(&content::FrameHasSourceUrl, frame_url));
117 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SanityCheckAvailableAPIsInFrame) {
118 ASSERT_TRUE(RunTestOnExtensionsFrame("sanity_check_available_apis.js"));
121 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest,
122 SanityCheckAvailableAPIsInChromeFrame) {
123 ASSERT_TRUE(RunTestOnChromeExtensionsFrame("sanity_check_available_apis.js"));
126 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SanityCheckAvailableAPIsInToplevel) {
127 ASSERT_TRUE(RunTestOnChromeExtensions("sanity_check_available_apis.js"));
130 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SanityCheckUnavailableAPIs) {
131 ASSERT_TRUE(RunTestOnAbout("sanity_check_available_apis.js"));
134 // Tests chrome.test.sendMessage, which exercises WebUI making a
135 // function call and receiving a response.
136 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, SendMessage) {
137 scoped_ptr<ExtensionTestMessageListener> listener(
138 new ExtensionTestMessageListener("ping", true));
140 ASSERT_TRUE(RunTestOnExtensionsFrame("send_message.js"));
142 ASSERT_TRUE(listener->WaitUntilSatisfied());
143 listener->Reply("pong");
145 listener.reset(new ExtensionTestMessageListener(false));
146 ASSERT_TRUE(listener->WaitUntilSatisfied());
147 EXPECT_EQ("true", listener->message());
150 // Tests chrome.runtime.onMessage, which exercises WebUI registering and
151 // receiving an event.
152 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, OnMessage) {
153 ASSERT_TRUE(RunTestOnExtensionsFrame("on_message.js"));
155 OnMessage::Info info;
156 info.data = "hi";
157 info.last_message = true;
158 EventRouter::Get(profile())->BroadcastEvent(make_scoped_ptr(
159 new Event(OnMessage::kEventName, OnMessage::Create(info))));
161 scoped_ptr<ExtensionTestMessageListener> listener(
162 new ExtensionTestMessageListener(false));
163 ASSERT_TRUE(listener->WaitUntilSatisfied());
164 EXPECT_EQ("true", listener->message());
167 // Tests chrome.runtime.lastError, which exercises WebUI accessing a property
168 // on an API which it doesn't actually have access to. A bindings test really.
169 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, RuntimeLastError) {
170 scoped_ptr<ExtensionTestMessageListener> listener(
171 new ExtensionTestMessageListener("ping", true));
173 ASSERT_TRUE(RunTestOnExtensionsFrame("runtime_last_error.js"));
175 ASSERT_TRUE(listener->WaitUntilSatisfied());
176 listener->ReplyWithError("unknown host");
178 listener.reset(new ExtensionTestMessageListener(false));
179 ASSERT_TRUE(listener->WaitUntilSatisfied());
180 EXPECT_EQ("true", listener->message());
183 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, CanEmbedExtensionOptions) {
184 scoped_ptr<ExtensionTestMessageListener> listener(
185 new ExtensionTestMessageListener("ready", true));
187 const Extension* extension =
188 LoadExtension(test_data_dir_.AppendASCII("extension_options")
189 .AppendASCII("embed_self"));
190 ASSERT_TRUE(extension);
192 ASSERT_TRUE(RunTestOnExtensionsFrame("can_embed_extension_options.js"));
194 ASSERT_TRUE(listener->WaitUntilSatisfied());
195 listener->Reply(extension->id());
196 listener.reset(new ExtensionTestMessageListener("load", false));
197 ASSERT_TRUE(listener->WaitUntilSatisfied());
200 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, ReceivesExtensionOptionsOnClose) {
201 scoped_ptr<ExtensionTestMessageListener> listener(
202 new ExtensionTestMessageListener("ready", true));
204 const Extension* extension =
205 InstallExtension(test_data_dir_.AppendASCII("extension_options")
206 .AppendASCII("close_self"), 1);
207 ASSERT_TRUE(extension);
209 ASSERT_TRUE(
210 RunTestOnExtensionsFrame("receives_extension_options_on_close.js"));
212 ASSERT_TRUE(listener->WaitUntilSatisfied());
213 listener->Reply(extension->id());
214 listener.reset(new ExtensionTestMessageListener("onclose received", false));
215 ASSERT_TRUE(listener->WaitUntilSatisfied());
218 // Regression test for crbug.com/414526.
220 // Same setup as CanEmbedExtensionOptions but disable the extension before
221 // embedding.
222 IN_PROC_BROWSER_TEST_F(ExtensionWebUITest, EmbedDisabledExtension) {
223 scoped_ptr<ExtensionTestMessageListener> listener(
224 new ExtensionTestMessageListener("ready", true));
226 std::string extension_id;
228 const Extension* extension =
229 LoadExtension(test_data_dir_.AppendASCII("extension_options")
230 .AppendASCII("embed_self"));
231 ASSERT_TRUE(extension);
232 extension_id = extension->id();
233 DisableExtension(extension_id);
236 ASSERT_TRUE(RunTestOnExtensionsFrame("can_embed_extension_options.js"));
238 ASSERT_TRUE(listener->WaitUntilSatisfied());
239 listener->Reply(extension_id);
240 listener.reset(new ExtensionTestMessageListener("createfailed", false));
241 ASSERT_TRUE(listener->WaitUntilSatisfied());
244 } // namespace
246 } // namespace extensions