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
;
34 class CancelLoginDialog
: public content::NotificationObserver
{
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();
53 content::NotificationRegistrar registrar_
;
55 DISALLOW_COPY_AND_ASSIGN(CancelLoginDialog
);
60 class ExtensionWebRequestApiTest
: public ExtensionApiTest
{
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
82 #define MAYBE_WebRequestSimple DISABLED_WebRequestSimple
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")) <<
92 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest
, WebRequestComplex
) {
93 ASSERT_TRUE(StartEmbeddedTestServer());
94 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_complex.html")) <<
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")) <<
108 // This test times out regularly on win_rel trybots. See http://crbug.com/122178
110 #define MAYBE_WebRequestBlocking DISABLED_WebRequestBlocking
112 #define MAYBE_WebRequestBlocking WebRequestBlocking
114 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest
, MAYBE_WebRequestBlocking
) {
115 ASSERT_TRUE(StartEmbeddedTestServer());
116 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_blocking.html")) <<
120 // Fails often on Windows dbg bots. http://crbug.com/177163
122 #define MAYBE_WebRequestNewTab DISABLED_WebRequestNewTab
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"))
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
147 blink::WebMouseEvent mouse_event
;
148 mouse_event
.type
= blink::WebInputEvent::MouseDown
;
149 mouse_event
.button
= blink::WebMouseEvent::ButtonLeft
;
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"))
166 // This test times out on XP. See http://crbug.com/178296
168 #define MAYBE_WebRequestDeclarative2 DISABLED_WebRequestDeclarative2
170 #define MAYBE_WebRequestDeclarative2 WebRequestDeclarative2
172 IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest
,
173 MAYBE_WebRequestDeclarative2
) {
174 ASSERT_TRUE(StartEmbeddedTestServer());
175 ASSERT_TRUE(RunExtensionSubtest("webrequest", "test_declarative2.html"))
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(
208 embedded_test_server()->GetURL("/extensions/test_file.html"));
211 WebContents
* tab
= browser()->tab_strip_model()->GetActiveWebContents();
212 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
214 "window.domAutomationController.send(document.body.textContent)",
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(
229 embedded_test_server()->GetURL("/extensions/test_file.html"));
232 WebContents
* otr_tab
= otr_browser
->tab_strip_model()->GetActiveWebContents();
233 ASSERT_TRUE(content::ExecuteScriptAndExtractString(
235 "window.domAutomationController.send(document.body.textContent)",
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.
271 #define MAYBE_PostData1 DISABLED_PostData1
272 #define MAYBE_PostData2 DISABLED_PostData2
274 #define MAYBE_PostData1 PostData1
275 #define MAYBE_PostData2 PostData2
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")) <<
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")) <<
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());