[Android] Allow multiple --install in bb_device_steps.py.
[chromium-blink-merge.git] / chrome / browser / referrer_policy_browsertest.cc
blobbfbc505105aed93d7aca33020934de83df47d6ec
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/bind.h"
6 #include "base/command_line.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/app/chrome_command_ids.h"
11 #include "chrome/browser/chrome_notification_types.h"
12 #include "chrome/browser/renderer_context_menu/render_view_context_menu.h"
13 #include "chrome/browser/renderer_context_menu/render_view_context_menu_browsertest_util.h"
14 #include "chrome/browser/ui/browser.h"
15 #include "chrome/browser/ui/browser_commands.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/pref_names.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/navigation_controller.h"
21 #include "content/public/browser/navigation_entry.h"
22 #include "content/public/browser/notification_service.h"
23 #include "content/public/browser/render_view_host.h"
24 #include "content/public/browser/web_contents.h"
25 #include "content/public/common/content_switches.h"
26 #include "content/public/test/browser_test_utils.h"
27 #include "net/test/spawned_test_server/spawned_test_server.h"
28 #include "third_party/WebKit/public/web/WebInputEvent.h"
30 namespace {
32 const base::FilePath::CharType kDocRoot[] =
33 FILE_PATH_LITERAL("chrome/test/data/referrer_policy");
35 } // namespace
37 class ReferrerPolicyTest : public InProcessBrowserTest {
38 public:
39 ReferrerPolicyTest() {}
40 ~ReferrerPolicyTest() override {}
42 void SetUp() override {
43 test_server_.reset(new net::SpawnedTestServer(
44 net::SpawnedTestServer::TYPE_HTTP,
45 net::SpawnedTestServer::kLocalhost,
46 base::FilePath(kDocRoot)));
47 ASSERT_TRUE(test_server_->Start());
48 ssl_test_server_.reset(new net::SpawnedTestServer(
49 net::SpawnedTestServer::TYPE_HTTPS,
50 net::SpawnedTestServer::kLocalhost,
51 base::FilePath(kDocRoot)));
52 ASSERT_TRUE(ssl_test_server_->Start());
54 InProcessBrowserTest::SetUp();
57 protected:
58 enum ExpectedReferrer {
59 EXPECT_EMPTY_REFERRER,
60 EXPECT_FULL_REFERRER,
61 EXPECT_ORIGIN_AS_REFERRER
64 // Returns the expected title for the tab with the given (full) referrer and
65 // the expected modification of it.
66 base::string16 GetExpectedTitle(const GURL& url,
67 ExpectedReferrer expected_referrer) {
68 std::string referrer;
69 switch (expected_referrer) {
70 case EXPECT_EMPTY_REFERRER:
71 referrer = "Referrer is empty";
72 break;
73 case EXPECT_FULL_REFERRER:
74 referrer = "Referrer is " + url.spec();
75 break;
76 case EXPECT_ORIGIN_AS_REFERRER:
77 referrer = "Referrer is " + url.GetWithEmptyPath().spec();
78 break;
80 return base::ASCIIToUTF16(referrer);
83 // Adds all possible titles to the TitleWatcher, so we don't time out
84 // waiting for the title if the test fails.
85 void AddAllPossibleTitles(const GURL& url,
86 content::TitleWatcher* title_watcher) {
87 title_watcher->AlsoWaitForTitle(
88 GetExpectedTitle(url, EXPECT_EMPTY_REFERRER));
89 title_watcher->AlsoWaitForTitle(
90 GetExpectedTitle(url, EXPECT_FULL_REFERRER));
91 title_watcher->AlsoWaitForTitle(
92 GetExpectedTitle(url, EXPECT_ORIGIN_AS_REFERRER));
95 // Returns a string representation of a given |referrer_policy|.
96 std::string ReferrerPolicyToString(blink::WebReferrerPolicy referrer_policy) {
97 switch (referrer_policy) {
98 case blink::WebReferrerPolicyDefault:
99 return "no-meta";
100 case blink::WebReferrerPolicyNoReferrerWhenDowngrade:
101 return "default";
102 case blink::WebReferrerPolicyOrigin:
103 return "origin";
104 case blink::WebReferrerPolicyOriginWhenCrossOrigin:
105 return "origin-when-crossorigin";
106 case blink::WebReferrerPolicyAlways:
107 return "always";
108 case blink::WebReferrerPolicyNever:
109 return "never";
110 default:
111 NOTREACHED();
112 return "";
116 enum StartOnProtocol { START_ON_HTTP, START_ON_HTTPS, };
118 enum LinkType { REGULAR_LINK, LINK_WITH_TARGET_BLANK, };
120 enum RedirectType {
121 NO_REDIRECT,
122 SERVER_REDIRECT_FROM_HTTPS_TO_HTTP,
123 SERVER_REDIRECT_FROM_HTTP_TO_HTTP,
124 SERVER_REDIRECT_FROM_HTTP_TO_HTTPS
127 std::string RedirectTypeToString(RedirectType redirect) {
128 switch (redirect) {
129 case NO_REDIRECT:
130 return "none";
131 case SERVER_REDIRECT_FROM_HTTPS_TO_HTTP:
132 return "https2http";
133 case SERVER_REDIRECT_FROM_HTTP_TO_HTTP:
134 return "http2http";
135 case SERVER_REDIRECT_FROM_HTTP_TO_HTTPS:
136 return "http2https";
138 NOTREACHED();
139 return "";
142 // Navigates from a page with a given |referrer_policy| and checks that the
143 // reported referrer matches the expectation.
144 // Parameters:
145 // referrer_policy: The referrer policy to test.
146 // start_protocol: The protocol the test should start on.
147 // link_type: The link type that is used to trigger the navigation.
148 // redirect: Whether the link target should redirect and how.
149 // disposition: The disposition for the navigation.
150 // button: If not WebMouseEvent::ButtonNone, click on the
151 // link with the specified mouse button.
152 // expected_referrer: The kind of referrer to expect.
154 // Returns:
155 // The URL of the first page navigated to.
156 GURL RunReferrerTest(const blink::WebReferrerPolicy referrer_policy,
157 StartOnProtocol start_protocol,
158 LinkType link_type,
159 RedirectType redirect,
160 WindowOpenDisposition disposition,
161 blink::WebMouseEvent::Button button,
162 ExpectedReferrer expected_referrer) {
163 GURL start_url;
164 net::SpawnedTestServer* start_server = start_protocol == START_ON_HTTPS
165 ? ssl_test_server_.get()
166 : test_server_.get();
167 start_url = start_server->GetURL(
168 std::string("files/referrer-policy-start.html?") + "policy=" +
169 ReferrerPolicyToString(referrer_policy) + "&port=" +
170 base::IntToString(test_server_->host_port_pair().port()) +
171 "&ssl_port=" +
172 base::IntToString(ssl_test_server_->host_port_pair().port()) +
173 "&redirect=" + RedirectTypeToString(redirect) + "&link=" +
174 (button == blink::WebMouseEvent::ButtonNone ? "false" : "true") +
175 "&target=" + (link_type == LINK_WITH_TARGET_BLANK ? "_blank" : ""));
177 ui_test_utils::WindowedTabAddedNotificationObserver tab_added_observer(
178 content::NotificationService::AllSources());
180 base::string16 expected_title =
181 GetExpectedTitle(start_url, expected_referrer);
182 content::WebContents* tab =
183 browser()->tab_strip_model()->GetActiveWebContents();
184 content::TitleWatcher title_watcher(tab, expected_title);
186 // Watch for all possible outcomes to avoid timeouts if something breaks.
187 AddAllPossibleTitles(start_url, &title_watcher);
189 ui_test_utils::NavigateToURL(browser(), start_url);
191 if (button != blink::WebMouseEvent::ButtonNone) {
192 blink::WebMouseEvent mouse_event;
193 mouse_event.type = blink::WebInputEvent::MouseDown;
194 mouse_event.button = button;
195 mouse_event.x = 15;
196 mouse_event.y = 15;
197 mouse_event.clickCount = 1;
198 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
199 mouse_event.type = blink::WebInputEvent::MouseUp;
200 tab->GetRenderViewHost()->ForwardMouseEvent(mouse_event);
203 if (disposition == CURRENT_TAB) {
204 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
205 } else {
206 tab_added_observer.Wait();
207 tab = tab_added_observer.GetTab();
208 EXPECT_TRUE(tab);
209 content::TitleWatcher title_watcher2(tab, expected_title);
211 // Watch for all possible outcomes to avoid timeouts if something breaks.
212 AddAllPossibleTitles(start_url, &title_watcher2);
214 EXPECT_EQ(expected_title, title_watcher2.WaitAndGetTitle());
217 EXPECT_EQ(referrer_policy,
218 tab->GetController().GetActiveEntry()->GetReferrer().policy);
220 return start_url;
223 scoped_ptr<net::SpawnedTestServer> test_server_;
224 scoped_ptr<net::SpawnedTestServer> ssl_test_server_;
227 // The basic behavior of referrer policies is covered by layout tests in
228 // http/tests/security/referrer-policy-*. These tests cover (hopefully) all
229 // code paths chrome uses to navigate. To keep the number of combinations down,
230 // we only test the "origin" policy here.
232 // Some tests are marked as FAILS, see http://crbug.com/124750
234 // Content initiated navigation, from HTTP to HTTP.
235 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, Origin) {
236 RunReferrerTest(blink::WebReferrerPolicyOrigin,
237 START_ON_HTTP,
238 REGULAR_LINK,
239 NO_REDIRECT,
240 CURRENT_TAB,
241 blink::WebMouseEvent::ButtonNone,
242 EXPECT_ORIGIN_AS_REFERRER);
245 // Content initiated navigation, from HTTPS to HTTP.
246 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpsDefault) {
247 RunReferrerTest(blink::WebReferrerPolicyOrigin,
248 START_ON_HTTPS,
249 REGULAR_LINK,
250 NO_REDIRECT,
251 CURRENT_TAB,
252 blink::WebMouseEvent::ButtonNone,
253 EXPECT_ORIGIN_AS_REFERRER);
256 // User initiated navigation, from HTTP to HTTP.
257 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, LeftClickOrigin) {
258 RunReferrerTest(blink::WebReferrerPolicyOrigin,
259 START_ON_HTTP,
260 REGULAR_LINK,
261 NO_REDIRECT,
262 CURRENT_TAB,
263 blink::WebMouseEvent::ButtonLeft,
264 EXPECT_ORIGIN_AS_REFERRER);
267 // User initiated navigation, from HTTPS to HTTP.
268 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpsLeftClickOrigin) {
269 RunReferrerTest(blink::WebReferrerPolicyOrigin,
270 START_ON_HTTPS,
271 REGULAR_LINK,
272 NO_REDIRECT,
273 CURRENT_TAB,
274 blink::WebMouseEvent::ButtonLeft,
275 EXPECT_ORIGIN_AS_REFERRER);
278 // User initiated navigation, middle click, from HTTP to HTTP.
279 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, MiddleClickOrigin) {
280 RunReferrerTest(blink::WebReferrerPolicyOrigin,
281 START_ON_HTTP,
282 REGULAR_LINK,
283 NO_REDIRECT,
284 NEW_BACKGROUND_TAB,
285 blink::WebMouseEvent::ButtonMiddle,
286 EXPECT_ORIGIN_AS_REFERRER);
289 // User initiated navigation, middle click, from HTTPS to HTTP.
290 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpsMiddleClickOrigin) {
291 RunReferrerTest(blink::WebReferrerPolicyOrigin,
292 START_ON_HTTPS,
293 REGULAR_LINK,
294 NO_REDIRECT,
295 NEW_BACKGROUND_TAB,
296 blink::WebMouseEvent::ButtonMiddle,
297 EXPECT_ORIGIN_AS_REFERRER);
300 // User initiated navigation, target blank, from HTTP to HTTP.
301 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, TargetBlankOrigin) {
302 RunReferrerTest(blink::WebReferrerPolicyOrigin,
303 START_ON_HTTP,
304 LINK_WITH_TARGET_BLANK,
305 NO_REDIRECT,
306 NEW_FOREGROUND_TAB,
307 blink::WebMouseEvent::ButtonLeft,
308 EXPECT_ORIGIN_AS_REFERRER);
311 // User initiated navigation, target blank, from HTTPS to HTTP.
312 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpsTargetBlankOrigin) {
313 RunReferrerTest(blink::WebReferrerPolicyOrigin,
314 START_ON_HTTPS,
315 LINK_WITH_TARGET_BLANK,
316 NO_REDIRECT,
317 NEW_FOREGROUND_TAB,
318 blink::WebMouseEvent::ButtonLeft,
319 EXPECT_ORIGIN_AS_REFERRER);
322 // User initiated navigation, middle click, target blank, from HTTP to HTTP.
323 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, MiddleClickTargetBlankOrigin) {
324 RunReferrerTest(blink::WebReferrerPolicyOrigin,
325 START_ON_HTTP,
326 LINK_WITH_TARGET_BLANK,
327 NO_REDIRECT,
328 NEW_FOREGROUND_TAB,
329 blink::WebMouseEvent::ButtonMiddle,
330 EXPECT_ORIGIN_AS_REFERRER);
333 // User initiated navigation, middle click, target blank, from HTTPS to HTTP.
334 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpsMiddleClickTargetBlankOrigin) {
335 RunReferrerTest(blink::WebReferrerPolicyOrigin,
336 START_ON_HTTPS,
337 LINK_WITH_TARGET_BLANK,
338 NO_REDIRECT,
339 NEW_FOREGROUND_TAB,
340 blink::WebMouseEvent::ButtonMiddle,
341 EXPECT_ORIGIN_AS_REFERRER);
344 // Context menu, from HTTP to HTTP.
345 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, ContextMenuOrigin) {
346 ContextMenuNotificationObserver context_menu_observer(
347 IDC_CONTENT_CONTEXT_OPENLINKNEWTAB);
348 RunReferrerTest(blink::WebReferrerPolicyOrigin,
349 START_ON_HTTP,
350 REGULAR_LINK,
351 NO_REDIRECT,
352 NEW_FOREGROUND_TAB,
353 blink::WebMouseEvent::ButtonRight,
354 EXPECT_ORIGIN_AS_REFERRER);
357 // Context menu, from HTTPS to HTTP.
358 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpsContextMenuOrigin) {
359 ContextMenuNotificationObserver context_menu_observer(
360 IDC_CONTENT_CONTEXT_OPENLINKNEWTAB);
361 RunReferrerTest(blink::WebReferrerPolicyOrigin,
362 START_ON_HTTPS,
363 REGULAR_LINK,
364 NO_REDIRECT,
365 NEW_FOREGROUND_TAB,
366 blink::WebMouseEvent::ButtonRight,
367 EXPECT_ORIGIN_AS_REFERRER);
370 // Content initiated navigation, from HTTP to HTTP via server redirect.
371 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, Redirect) {
372 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTP, REGULAR_LINK,
373 SERVER_REDIRECT_FROM_HTTPS_TO_HTTP, CURRENT_TAB,
374 blink::WebMouseEvent::ButtonNone, EXPECT_ORIGIN_AS_REFERRER);
377 // Content initiated navigation, from HTTPS to HTTP via server redirect.
378 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpsRedirect) {
379 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTPS, REGULAR_LINK,
380 SERVER_REDIRECT_FROM_HTTPS_TO_HTTP, CURRENT_TAB,
381 blink::WebMouseEvent::ButtonNone, EXPECT_ORIGIN_AS_REFERRER);
384 // User initiated navigation, from HTTP to HTTP via server redirect.
385 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, LeftClickRedirect) {
386 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTP, REGULAR_LINK,
387 SERVER_REDIRECT_FROM_HTTP_TO_HTTP, CURRENT_TAB,
388 blink::WebMouseEvent::ButtonLeft, EXPECT_ORIGIN_AS_REFERRER);
391 // User initiated navigation, from HTTPS to HTTP via server redirect.
392 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpsLeftClickRedirect) {
393 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTPS, REGULAR_LINK,
394 SERVER_REDIRECT_FROM_HTTPS_TO_HTTP, CURRENT_TAB,
395 blink::WebMouseEvent::ButtonLeft, EXPECT_ORIGIN_AS_REFERRER);
398 // User initiated navigation, middle click, from HTTP to HTTP via server
399 // redirect.
400 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, MiddleClickRedirect) {
401 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTP, REGULAR_LINK,
402 SERVER_REDIRECT_FROM_HTTPS_TO_HTTP, NEW_BACKGROUND_TAB,
403 blink::WebMouseEvent::ButtonMiddle,
404 EXPECT_ORIGIN_AS_REFERRER);
407 // User initiated navigation, middle click, from HTTPS to HTTP via server
408 // redirect.
409 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpsMiddleClickRedirect) {
410 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTPS, REGULAR_LINK,
411 SERVER_REDIRECT_FROM_HTTPS_TO_HTTP, NEW_BACKGROUND_TAB,
412 blink::WebMouseEvent::ButtonMiddle,
413 EXPECT_ORIGIN_AS_REFERRER);
416 // User initiated navigation, target blank, from HTTP to HTTP via server
417 // redirect.
418 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, TargetBlankRedirect) {
419 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTP,
420 LINK_WITH_TARGET_BLANK, SERVER_REDIRECT_FROM_HTTPS_TO_HTTP,
421 NEW_FOREGROUND_TAB, blink::WebMouseEvent::ButtonLeft,
422 EXPECT_ORIGIN_AS_REFERRER);
425 // User initiated navigation, target blank, from HTTPS to HTTP via server
426 // redirect.
427 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpsTargetBlankRedirect) {
428 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTPS,
429 LINK_WITH_TARGET_BLANK, SERVER_REDIRECT_FROM_HTTPS_TO_HTTP,
430 NEW_FOREGROUND_TAB, blink::WebMouseEvent::ButtonLeft,
431 EXPECT_ORIGIN_AS_REFERRER);
434 // User initiated navigation, middle click, target blank, from HTTP to HTTP via
435 // server redirect.
436 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, MiddleClickTargetBlankRedirect) {
437 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTP,
438 LINK_WITH_TARGET_BLANK, SERVER_REDIRECT_FROM_HTTPS_TO_HTTP,
439 NEW_FOREGROUND_TAB, blink::WebMouseEvent::ButtonMiddle,
440 EXPECT_ORIGIN_AS_REFERRER);
443 // User initiated navigation, middle click, target blank, from HTTPS to HTTP
444 // via server redirect.
445 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest,
446 HttpsMiddleClickTargetBlankRedirect) {
447 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTPS,
448 LINK_WITH_TARGET_BLANK, SERVER_REDIRECT_FROM_HTTPS_TO_HTTP,
449 NEW_FOREGROUND_TAB, blink::WebMouseEvent::ButtonMiddle,
450 EXPECT_ORIGIN_AS_REFERRER);
453 // Context menu, from HTTP to HTTP via server redirect.
454 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, ContextMenuRedirect) {
455 ContextMenuNotificationObserver context_menu_observer(
456 IDC_CONTENT_CONTEXT_OPENLINKNEWTAB);
457 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTP, REGULAR_LINK,
458 SERVER_REDIRECT_FROM_HTTPS_TO_HTTP, NEW_FOREGROUND_TAB,
459 blink::WebMouseEvent::ButtonRight, EXPECT_ORIGIN_AS_REFERRER);
462 // Context menu, from HTTPS to HTTP via server redirect.
463 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpsContextMenuRedirect) {
464 ContextMenuNotificationObserver context_menu_observer(
465 IDC_CONTENT_CONTEXT_OPENLINKNEWTAB);
466 RunReferrerTest(blink::WebReferrerPolicyOrigin, START_ON_HTTPS, REGULAR_LINK,
467 SERVER_REDIRECT_FROM_HTTPS_TO_HTTP, NEW_FOREGROUND_TAB,
468 blink::WebMouseEvent::ButtonRight, EXPECT_ORIGIN_AS_REFERRER);
471 // Tests history navigation actions: Navigate from A to B with a referrer
472 // policy, then navigate to C, back to B, and reload.
473 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, History) {
474 // Navigate from A to B.
475 GURL start_url = RunReferrerTest(
476 blink::WebReferrerPolicyOrigin, START_ON_HTTPS, REGULAR_LINK,
477 SERVER_REDIRECT_FROM_HTTPS_TO_HTTP, CURRENT_TAB,
478 blink::WebMouseEvent::ButtonLeft, EXPECT_ORIGIN_AS_REFERRER);
480 // Navigate to C.
481 ui_test_utils::NavigateToURL(browser(), test_server_->GetURL(std::string()));
483 base::string16 expected_title =
484 GetExpectedTitle(start_url, EXPECT_ORIGIN_AS_REFERRER);
485 content::WebContents* tab =
486 browser()->tab_strip_model()->GetActiveWebContents();
487 scoped_ptr<content::TitleWatcher> title_watcher(
488 new content::TitleWatcher(tab, expected_title));
490 // Watch for all possible outcomes to avoid timeouts if something breaks.
491 AddAllPossibleTitles(start_url, title_watcher.get());
493 // Go back to B.
494 chrome::GoBack(browser(), CURRENT_TAB);
495 EXPECT_EQ(expected_title, title_watcher->WaitAndGetTitle());
497 title_watcher.reset(new content::TitleWatcher(tab, expected_title));
498 AddAllPossibleTitles(start_url, title_watcher.get());
500 // Reload to B.
501 chrome::Reload(browser(), CURRENT_TAB);
502 EXPECT_EQ(expected_title, title_watcher->WaitAndGetTitle());
504 title_watcher.reset(new content::TitleWatcher(tab, expected_title));
505 AddAllPossibleTitles(start_url, title_watcher.get());
507 // Shift-reload to B.
508 chrome::ReloadIgnoringCache(browser(), CURRENT_TAB);
509 EXPECT_EQ(expected_title, title_watcher->WaitAndGetTitle());
512 // Tests that reloading a site for "request tablet version" correctly clears
513 // the referrer.
514 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, RequestTabletSite) {
515 GURL start_url = RunReferrerTest(
516 blink::WebReferrerPolicyOrigin, START_ON_HTTPS, REGULAR_LINK,
517 SERVER_REDIRECT_FROM_HTTP_TO_HTTP, CURRENT_TAB,
518 blink::WebMouseEvent::ButtonLeft, EXPECT_ORIGIN_AS_REFERRER);
520 base::string16 expected_title =
521 GetExpectedTitle(start_url, EXPECT_EMPTY_REFERRER);
522 content::WebContents* tab =
523 browser()->tab_strip_model()->GetActiveWebContents();
524 content::TitleWatcher title_watcher(tab, expected_title);
526 // Watch for all possible outcomes to avoid timeouts if something breaks.
527 AddAllPossibleTitles(start_url, &title_watcher);
529 // Request tablet version.
530 chrome::ToggleRequestTabletSite(browser());
531 EXPECT_EQ(expected_title, title_watcher.WaitAndGetTitle());
534 // Test that an iframes gets the parent frames referrer and referrer policy if
535 // the load was triggered by the parent, or from the iframe itself, if the
536 // navigations was started by the iframe.
537 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, IFrame) {
538 browser()->profile()->GetPrefs()->SetBoolean(
539 prefs::kWebKitAllowRunningInsecureContent, true);
540 content::WebContents* tab =
541 browser()->tab_strip_model()->GetActiveWebContents();
542 base::string16 expected_title(base::ASCIIToUTF16("loaded"));
543 scoped_ptr<content::TitleWatcher> title_watcher(
544 new content::TitleWatcher(tab, expected_title));
546 // Load a page that loads an iframe.
547 ui_test_utils::NavigateToURL(
548 browser(),
549 ssl_test_server_->GetURL(
550 std::string("files/referrer-policy-iframe.html?") +
551 base::IntToString(test_server_->host_port_pair().port())));
552 EXPECT_EQ(expected_title, title_watcher->WaitAndGetTitle());
554 // Verify that the referrer policy was honored and the main page's origin was
555 // send as referrer.
556 content::RenderFrameHost* frame = content::FrameMatchingPredicate(
557 tab, base::Bind(&content::FrameIsChildOfMainFrame));
558 std::string title;
559 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
560 frame,
561 "window.domAutomationController.send(document.title)",
562 &title));
563 EXPECT_EQ("Referrer is " + ssl_test_server_->GetURL(std::string()).spec(),
564 title);
566 // Reload the iframe.
567 expected_title = base::ASCIIToUTF16("reset");
568 title_watcher.reset(new content::TitleWatcher(tab, expected_title));
569 EXPECT_TRUE(content::ExecuteScript(tab, "document.title = 'reset'"));
570 EXPECT_EQ(expected_title, title_watcher->WaitAndGetTitle());
572 expected_title = base::ASCIIToUTF16("loaded");
573 title_watcher.reset(new content::TitleWatcher(tab, expected_title));
574 EXPECT_TRUE(content::ExecuteScript(frame, "location.reload()"));
575 EXPECT_EQ(expected_title, title_watcher->WaitAndGetTitle());
577 // Verify that the full url of the iframe was used as referrer.
578 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
579 frame,
580 "window.domAutomationController.send(document.title)",
581 &title));
582 EXPECT_EQ("Referrer is " +
583 test_server_->GetURL("files/referrer-policy-log.html").spec(),
584 title);
587 // Origin When Cross-Origin
589 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest,
590 HttpLeftClickHTTPSRedirectToHTTPOriginWhenCrossOrigin) {
591 RunReferrerTest(blink::WebReferrerPolicyOriginWhenCrossOrigin, START_ON_HTTPS,
592 REGULAR_LINK, SERVER_REDIRECT_FROM_HTTPS_TO_HTTP, CURRENT_TAB,
593 blink::WebMouseEvent::ButtonLeft, EXPECT_ORIGIN_AS_REFERRER);
596 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest,
597 HttpLeftClickRedirectToHTTPSOriginWhenCrossOrigin) {
598 RunReferrerTest(blink::WebReferrerPolicyOriginWhenCrossOrigin, START_ON_HTTP,
599 REGULAR_LINK, SERVER_REDIRECT_FROM_HTTP_TO_HTTPS, CURRENT_TAB,
600 blink::WebMouseEvent::ButtonLeft, EXPECT_ORIGIN_AS_REFERRER);
603 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest,
604 HttpLeftClickRedirectToHTTPOriginWhenCrossOrigin) {
605 RunReferrerTest(blink::WebReferrerPolicyOriginWhenCrossOrigin, START_ON_HTTP,
606 REGULAR_LINK, SERVER_REDIRECT_FROM_HTTP_TO_HTTP, CURRENT_TAB,
607 blink::WebMouseEvent::ButtonLeft, EXPECT_FULL_REFERRER);
610 // Reduced 'referer' granularity flag tests.
612 // User initiated navigation, from HTTP to HTTPS via server redirect.
613 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpLeftClickRedirectDefaultNoFlag) {
614 RunReferrerTest(blink::WebReferrerPolicyDefault, START_ON_HTTP, REGULAR_LINK,
615 SERVER_REDIRECT_FROM_HTTP_TO_HTTPS, CURRENT_TAB,
616 blink::WebMouseEvent::ButtonLeft, EXPECT_FULL_REFERRER);
619 IN_PROC_BROWSER_TEST_F(ReferrerPolicyTest, HttpLeftClickRedirectDefaultFlag) {
620 CommandLine::ForCurrentProcess()->AppendSwitch(
621 switches::kReducedReferrerGranularity);
622 RunReferrerTest(blink::WebReferrerPolicyDefault, START_ON_HTTP, REGULAR_LINK,
623 SERVER_REDIRECT_FROM_HTTP_TO_HTTPS, CURRENT_TAB,
624 blink::WebMouseEvent::ButtonLeft, EXPECT_ORIGIN_AS_REFERRER);