Allow only one bookmark to be added for multiple fast starring
[chromium-blink-merge.git] / chrome / browser / extensions / api / web_request / web_request_apitest.cc
blob7589d096c478dbd097e9d35848b8db1b229eac9e
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
3 // found in the LICENSE file.
5 #include "base/command_line.h"
6 #include "chrome/browser/chrome_notification_types.h"
7 #include "chrome/browser/extensions/extension_apitest.h"
8 #include "chrome/browser/extensions/extension_service.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/login/login_prompt.h"
12 #include "chrome/browser/ui/tabs/tab_strip_model.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "content/public/browser/notification_service.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "extensions/browser/api/web_request/web_request_api.h"
20 #include "extensions/browser/extension_system.h"
21 #include "extensions/common/features/feature.h"
22 #include "extensions/test/extension_test_message_listener.h"
23 #include "extensions/test/result_catcher.h"
24 #include "net/dns/mock_host_resolver.h"
25 #include "net/test/embedded_test_server/embedded_test_server.h"
26 #include "third_party/WebKit/public/web/WebInputEvent.h"
28 using content::WebContents;
29 using extensions::Feature;
30 using extensions::ResultCatcher;
32 namespace {
34 class CancelLoginDialog : public content::NotificationObserver {
35 public:
36 CancelLoginDialog() {
37 registrar_.Add(this,
38 chrome::NOTIFICATION_AUTH_NEEDED,
39 content::NotificationService::AllSources());
42 ~CancelLoginDialog() override {}
44 void Observe(int type,
45 const content::NotificationSource& source,
46 const content::NotificationDetails& details) override {
47 LoginHandler* handler =
48 content::Details<LoginNotificationDetails>(details).ptr()->handler();
49 handler->CancelAuth();
52 private:
53 content::NotificationRegistrar registrar_;
55 DISALLOW_COPY_AND_ASSIGN(CancelLoginDialog);
58 } // namespace
60 class ExtensionWebRequestApiTest : public ExtensionApiTest {
61 public:
62 void SetUpInProcessBrowserTestFixture() override {
63 ExtensionApiTest::SetUpInProcessBrowserTestFixture();
64 host_resolver()->AddRule("*", "127.0.0.1");
67 void RunPermissionTest(
68 const char* extension_directory,
69 bool load_extension_with_incognito_permission,
70 bool wait_for_extension_loaded_in_incognito,
71 const char* expected_content_regular_window,
72 const char* exptected_content_incognito_window);
75 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebRequestApi) {
76 ASSERT_TRUE(StartEmbeddedTestServer());
77 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_api.html")) << message_;
80 // Fails often on Windows dbg bots. http://crbug.com/177163
81 #if defined(OS_WIN)
82 #define MAYBE_WebRequestSimple DISABLED_WebRequestSimple
83 #else
84 #define MAYBE_WebRequestSimple WebRequestSimple
85 #endif // defined(OS_WIN)
86 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, MAYBE_WebRequestSimple) {
87 ASSERT_TRUE(StartEmbeddedTestServer());
88 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_simple.html")) <<
89 message_;
92 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebRequestComplex) {
93 ASSERT_TRUE(StartEmbeddedTestServer());
94 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_complex.html")) <<
95 message_;
98 // Flaky (sometimes crash): http://crbug.com/140976
99 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
100 DISABLED_WebRequestAuthRequired) {
101 CancelLoginDialog login_dialog_helper;
103 ASSERT_TRUE(StartEmbeddedTestServer());
104 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_auth_required.html")) <<
105 message_;
108 // This test times out regularly on win_rel trybots. See http://crbug.com/122178
109 #if defined(OS_WIN)
110 #define MAYBE_WebRequestBlocking DISABLED_WebRequestBlocking
111 #else
112 #define MAYBE_WebRequestBlocking WebRequestBlocking
113 #endif
114 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, MAYBE_WebRequestBlocking) {
115 ASSERT_TRUE(StartEmbeddedTestServer());
116 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_blocking.html")) <<
117 message_;
120 // Fails often on Windows dbg bots. http://crbug.com/177163
121 #if defined(OS_WIN)
122 #define MAYBE_WebRequestNewTab DISABLED_WebRequestNewTab
123 #else
124 #define MAYBE_WebRequestNewTab WebRequestNewTab
125 #endif // defined(OS_WIN)
126 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, MAYBE_WebRequestNewTab) {
127 ASSERT_TRUE(StartEmbeddedTestServer());
128 // Wait for the extension to set itself up and return control to us.
129 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_newTab.html"))
130 << message_;
132 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
133 content::WaitForLoadStop(tab);
135 ResultCatcher catcher;
137 ExtensionService* service = extensions::ExtensionSystem::Get(
138 browser()->profile())->extension_service();
139 const extensions::Extension* extension =
140 service->GetExtensionById(last_loaded_extension_id(), false);
141 GURL url = extension->GetResourceURL("newTab/a.html");
143 ui_test_utils::NavigateToURL(browser(), url);
145 // There's a link on a.html with target=_blank. Click on it to open it in a
146 // new tab.
147 blink::WebMouseEvent mouse_event;
148 mouse_event.type = blink::WebInputEvent::MouseDown;
149 mouse_event.button = blink::WebMouseEvent::ButtonLeft;
150 mouse_event.x = 7;
151 mouse_event.y = 7;
152 mouse_event.clickCount = 1;
153 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
154 mouse_event.type = blink::WebInputEvent::MouseUp;
155 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
157 ASSERT_TRUE(catcher.GetNextResult()) << catcher.message();
160 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, WebRequestDeclarative1) {
161 ASSERT_TRUE(StartEmbeddedTestServer());
162 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_declarative1.html"))
163 << message_;
166 // This test times out on XP. See http://crbug.com/178296
167 #if defined(OS_WIN)
168 #define MAYBE_WebRequestDeclarative2 DISABLED_WebRequestDeclarative2
169 #else
170 #define MAYBE_WebRequestDeclarative2 WebRequestDeclarative2
171 #endif
172 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
173 MAYBE_WebRequestDeclarative2) {
174 ASSERT_TRUE(StartEmbeddedTestServer());
175 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_declarative2.html"))
176 << message_;
179 void ExtensionWebRequestApiTest::RunPermissionTest(
180 const char* extension_directory,
181 bool load_extension_with_incognito_permission,
182 bool wait_for_extension_loaded_in_incognito,
183 const char* expected_content_regular_window,
184 const char* exptected_content_incognito_window) {
185 ResultCatcher catcher;
186 catcher.RestrictToBrowserContext(browser()->profile());
187 ResultCatcher catcher_incognito;
188 catcher_incognito.RestrictToBrowserContext(
189 browser()->profile()->GetOffTheRecordProfile());
191 ExtensionTestMessageListener listener("done", true);
192 ExtensionTestMessageListener listener_incognito("done_incognito", true);
194 int load_extension_flags = kFlagNone;
195 if (load_extension_with_incognito_permission)
196 load_extension_flags |= kFlagEnableIncognito;
197 ASSERT_TRUE(LoadExtensionWithFlags(
198 test_data_dir_.AppendASCII("webrequest_permissions")
199 .AppendASCII(extension_directory),
200 load_extension_flags));
202 // Test that navigation in regular window is properly redirected.
203 EXPECT_TRUE(listener.WaitUntilSatisfied());
205 // This navigation should be redirected.
206 ui_test_utils::NavigateToURL(
207 browser(),
208 embedded_test_server()->GetURL("/extensions/test_file.html"));
210 std::string body;
211 WebContents* tab = browser()->tab_strip_model()->GetActiveWebContents();
212 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
213 tab,
214 "window.domAutomationController.send(document.body.textContent)",
215 &body));
216 EXPECT_EQ(expected_content_regular_window, body);
218 // Test that navigation in OTR window is properly redirected.
219 Browser* otr_browser =
220 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
222 if (wait_for_extension_loaded_in_incognito)
223 EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
225 // This navigation should be redirected if
226 // load_extension_with_incognito_permission is true.
227 ui_test_utils::NavigateToURL(
228 otr_browser,
229 embedded_test_server()->GetURL("/extensions/test_file.html"));
231 body.clear();
232 WebContents* otr_tab = otr_browser->tab_strip_model()->GetActiveWebContents();
233 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
234 otr_tab,
235 "window.domAutomationController.send(document.body.textContent)",
236 &body));
237 EXPECT_EQ(exptected_content_incognito_window, body);
240 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
241 WebRequestDeclarativePermissionSpanning1) {
242 // Test spanning with incognito permission.
243 ASSERT_TRUE(StartEmbeddedTestServer());
244 RunPermissionTest("spanning", true, false, "redirected1", "redirected1");
247 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
248 WebRequestDeclarativePermissionSpanning2) {
249 // Test spanning without incognito permission.
250 ASSERT_TRUE(StartEmbeddedTestServer());
251 RunPermissionTest("spanning", false, false, "redirected1", "");
255 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
256 WebRequestDeclarativePermissionSplit1) {
257 // Test split with incognito permission.
258 ASSERT_TRUE(StartEmbeddedTestServer());
259 RunPermissionTest("split", true, true, "redirected1", "redirected2");
262 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
263 WebRequestDeclarativePermissionSplit2) {
264 // Test split without incognito permission.
265 ASSERT_TRUE(StartEmbeddedTestServer());
266 RunPermissionTest("split", false, false, "redirected1", "");
269 // TODO(vabr): Cure these flaky tests, http://crbug.com/238179.
270 #if !defined(NDEBUG)
271 #define MAYBE_PostData1 DISABLED_PostData1
272 #define MAYBE_PostData2 DISABLED_PostData2
273 #else
274 #define MAYBE_PostData1 PostData1
275 #define MAYBE_PostData2 PostData2
276 #endif
277 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, MAYBE_PostData1) {
278 // Test HTML form POST data access with the default and "url" encoding.
279 ASSERT_TRUE(StartEmbeddedTestServer());
280 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_post1.html")) <<
281 message_;
284 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, MAYBE_PostData2) {
285 // Test HTML form POST data access with the multipart and plaintext encoding.
286 ASSERT_TRUE(StartEmbeddedTestServer());
287 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_post2.html")) <<
288 message_;
291 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
292 DeclarativeSendMessage) {
293 ASSERT_TRUE(StartEmbeddedTestServer());
294 ASSERT_TRUE(RunExtensionTest("webrequest_sendmessage")) << message_;
297 // Check that reloading an extension that runs in incognito split mode and
298 // has two active background pages with registered events does not crash the
299 // browser. Regression test for http://crbug.com/224094
300 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, IncognitoSplitModeReload) {
301 ASSERT_TRUE(StartEmbeddedTestServer());
302 // Wait for rules to be set up.
303 ExtensionTestMessageListener listener("done", true);
304 ExtensionTestMessageListener listener_incognito("done_incognito", true);
306 const extensions::Extension* extension = LoadExtensionWithFlags(
307 test_data_dir_.AppendASCII("webrequest_reload"),
308 kFlagEnableIncognito);
309 ASSERT_TRUE(extension);
310 OpenURLOffTheRecord(browser()->profile(), GURL("about:blank"));
312 EXPECT_TRUE(listener.WaitUntilSatisfied());
313 EXPECT_TRUE(listener_incognito.WaitUntilSatisfied());
315 // Reload extension and wait for rules to be set up again. This should not
316 // crash the browser.
317 ExtensionTestMessageListener listener2("done", true);
318 ExtensionTestMessageListener listener_incognito2("done_incognito", true);
320 ReloadExtension(extension->id());
322 EXPECT_TRUE(listener2.WaitUntilSatisfied());
323 EXPECT_TRUE(listener_incognito2.WaitUntilSatisfied());
326 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest, ExtensionRequests) {
327 ASSERT_TRUE(StartEmbeddedTestServer());
328 ExtensionTestMessageListener listener_main1("web_request_status1", true);
329 ExtensionTestMessageListener listener_main2("web_request_status2", true);
331 ExtensionTestMessageListener listener_app("app_done", false);
332 ExtensionTestMessageListener listener_extension("extension_done", true);
334 // Set up webRequest listener
335 ASSERT_TRUE(LoadExtension(
336 test_data_dir_.AppendASCII("webrequest_extensions/main")));
337 EXPECT_TRUE(listener_main1.WaitUntilSatisfied());
338 EXPECT_TRUE(listener_main2.WaitUntilSatisfied());
340 // Perform some network activity in an app and another extension.
341 ASSERT_TRUE(LoadExtension(
342 test_data_dir_.AppendASCII("webrequest_extensions/app")));
343 ASSERT_TRUE(LoadExtension(
344 test_data_dir_.AppendASCII("webrequest_extensions/extension")));
346 EXPECT_TRUE(listener_app.WaitUntilSatisfied());
347 EXPECT_TRUE(listener_extension.WaitUntilSatisfied());
349 // Load a page, a content script will ping us when it is ready.
350 ExtensionTestMessageListener listener_pageready("contentscript_ready", true);
351 ui_test_utils::NavigateToURL(browser(), embedded_test_server()->GetURL(
352 "/extensions/test_file.html?match_webrequest_test"));
354 // The extension and app-generated requests should not have triggered any
355 // webRequest event filtered by type 'xmlhttprequest'.
356 // (check this here instead of before the navigation, in case the webRequest
357 // event routing is slow for some reason).
358 ExtensionTestMessageListener listener_result(false);
359 listener_main1.Reply("");
360 EXPECT_TRUE(listener_result.WaitUntilSatisfied());
361 EXPECT_EQ("Did not intercept any requests.", listener_result.message());
363 // Proceed with the final tests: Let the content script fire a request.
364 EXPECT_TRUE(listener_pageready.WaitUntilSatisfied());
365 listener_pageready.Reply("");
367 ExtensionTestMessageListener listener_contentscript("contentscript_done",
368 true);
369 ExtensionTestMessageListener listener_framescript("framescript_done", false);
370 EXPECT_TRUE(listener_contentscript.WaitUntilSatisfied());
371 listener_contentscript.Reply("");
372 EXPECT_TRUE(listener_framescript.WaitUntilSatisfied());
374 // Collect the visited URLs. The content script and subframe does not run in
375 // the extension's process, so the requests should be visible to the main
376 // extension.
377 listener_result.Reset();
378 listener_main2.Reply("");
379 EXPECT_TRUE(listener_result.WaitUntilSatisfied());
380 if (content::AreAllSitesIsolatedForTesting()) {
381 // With --site-per-process, the extension frame does run in the extension's
382 // process.
383 EXPECT_EQ("Intercepted requests: ?contentscript",
384 listener_result.message());
385 } else {
386 EXPECT_EQ("Intercepted requests: ?contentscript, ?framescript",
387 listener_result.message());