blob: a3eef7aae43b01c8812a81c980785892dc3b5ea3 [file] [log] [blame]
[email protected]9d8cfb682012-09-13 16:48:041// 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.
4
[email protected]3853a4c2013-02-11 17:15:575#include "base/prefs/pref_service.h"
[email protected]340f55d7c2013-06-10 20:49:116#include "base/strings/string16.h"
[email protected]135cb802013-06-09 16:44:207#include "base/strings/utf_string_conversions.h"
[email protected]9d8cfb682012-09-13 16:48:048#include "chrome/browser/profiles/profile.h"
9#include "chrome/browser/ui/browser.h"
[email protected]cc872372013-01-28 21:57:0710#include "chrome/browser/ui/tabs/tab_strip_model.h"
[email protected]9d8cfb682012-09-13 16:48:0411#include "chrome/common/pref_names.h"
12#include "chrome/test/base/in_process_browser_test.h"
13#include "chrome/test/base/ui_test_utils.h"
[email protected]498bc512012-09-21 21:50:0814#include "content/public/browser/web_contents.h"
15#include "content/public/test/browser_test_utils.h"
[email protected]9d8cfb682012-09-13 16:48:0416
17typedef InProcessBrowserTest DoNotTrackTest;
18
19// Check that the DNT header is sent when the corresponding preference is set.
20IN_PROC_BROWSER_TEST_F(DoNotTrackTest, Simple) {
21 ASSERT_TRUE(test_server()->Start());
22
23 PrefService* prefs = browser()->profile()->GetPrefs();
24 prefs->SetBoolean(prefs::kEnableDoNotTrack, true);
25
26 GURL url = test_server()->GetURL("echoheader?DNT");
27 ui_test_utils::NavigateToURL(browser(), url);
28
29 int matches = ui_test_utils::FindInPage(
[email protected]cc872372013-01-28 21:57:0730 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]04338722013-12-24 23:18:0531 base::ASCIIToUTF16("1"),
[email protected]9d8cfb682012-09-13 16:48:0432 true /* forward */, false /* match case */, NULL /* ordinal */,
33 NULL /* selection_rect */);
34
35 EXPECT_EQ(1, matches);
36}
37
38// Check that the DNT header is preserved during redirects.
39IN_PROC_BROWSER_TEST_F(DoNotTrackTest, Redirect) {
40 ASSERT_TRUE(test_server()->Start());
41
42 PrefService* prefs = browser()->profile()->GetPrefs();
43 prefs->SetBoolean(prefs::kEnableDoNotTrack, true);
44
45 GURL final_url = test_server()->GetURL("echoheader?DNT");
46 GURL url = test_server()->GetURL(
47 std::string("server-redirect?") + final_url.spec());
48 ui_test_utils::NavigateToURL(browser(), url);
49
50 int matches = ui_test_utils::FindInPage(
[email protected]cc872372013-01-28 21:57:0751 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]04338722013-12-24 23:18:0552 base::ASCIIToUTF16("1"),
[email protected]9d8cfb682012-09-13 16:48:0453 true /* forward */, false /* match case */, NULL /* ordinal */,
54 NULL /* selection_rect */);
55
56 EXPECT_EQ(1, matches);
57}
[email protected]498bc512012-09-21 21:50:0858
59// Check that the DOM property is set when the corresponding preference is set.
60IN_PROC_BROWSER_TEST_F(DoNotTrackTest, DOMProperty) {
61 PrefService* prefs = browser()->profile()->GetPrefs();
62 prefs->SetBoolean(prefs::kEnableDoNotTrack, true);
63
64 ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
[email protected]cc872372013-01-28 21:57:0765 browser()->tab_strip_model()->GetActiveWebContents()));
[email protected]498bc512012-09-21 21:50:0866
67 std::string do_not_track;
[email protected]b6987e02013-01-04 18:30:4368 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]cc872372013-01-28 21:57:0769 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:1370 "window.domAutomationController.send(navigator.doNotTrack)",
[email protected]498bc512012-09-21 21:50:0871 &do_not_track));
72 EXPECT_EQ("1", do_not_track);
73
74 // Reset flag and check that the changed value is propagated to the existing
75 // renderer.
76 prefs->SetBoolean(prefs::kEnableDoNotTrack, false);
77
[email protected]b6987e02013-01-04 18:30:4378 EXPECT_TRUE(content::ExecuteScriptAndExtractString(
[email protected]cc872372013-01-28 21:57:0779 browser()->tab_strip_model()->GetActiveWebContents(),
[email protected]06bc5d92013-01-02 22:44:1380 "window.domAutomationController.send("
81 " navigator.doNotTrack === null ? '0' : '1')",
[email protected]498bc512012-09-21 21:50:0882 &do_not_track));
83 EXPECT_EQ("0", do_not_track);
84}