summaryrefslogtreecommitdiffstats
path: root/chrome/browser/do_not_track_browsertest.cc
blob: acaafc71e960170dad87d41de1f466bc7079c815 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/string16.h"
#include "base/utf_string_conversions.h"
#include "chrome/browser/prefs/pref_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/common/pref_names.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_test_utils.h"

typedef InProcessBrowserTest DoNotTrackTest;

// Check that the DNT header is sent when the corresponding preference is set.
IN_PROC_BROWSER_TEST_F(DoNotTrackTest, Simple) {
  ASSERT_TRUE(test_server()->Start());

  PrefService* prefs = browser()->profile()->GetPrefs();
  prefs->SetBoolean(prefs::kEnableDoNotTrack, true);

  GURL url = test_server()->GetURL("echoheader?DNT");
  ui_test_utils::NavigateToURL(browser(), url);

  int matches = ui_test_utils::FindInPage(
      chrome::GetActiveTabContents(browser()),
      string16(ASCIIToUTF16("1")),
      true /* forward */, false /* match case */, NULL /* ordinal */,
      NULL /* selection_rect */);

  EXPECT_EQ(1, matches);
}

// Check that the DNT header is preserved during redirects.
IN_PROC_BROWSER_TEST_F(DoNotTrackTest, Redirect) {
  ASSERT_TRUE(test_server()->Start());

  PrefService* prefs = browser()->profile()->GetPrefs();
  prefs->SetBoolean(prefs::kEnableDoNotTrack, true);

  GURL final_url = test_server()->GetURL("echoheader?DNT");
  GURL url = test_server()->GetURL(
      std::string("server-redirect?") + final_url.spec());
  ui_test_utils::NavigateToURL(browser(), url);

  int matches = ui_test_utils::FindInPage(
      chrome::GetActiveTabContents(browser()),
      string16(ASCIIToUTF16("1")),
      true /* forward */, false /* match case */, NULL /* ordinal */,
      NULL /* selection_rect */);

  EXPECT_EQ(1, matches);
}

// Check that the DOM property is set when the corresponding preference is set.
IN_PROC_BROWSER_TEST_F(DoNotTrackTest, DOMProperty) {
  PrefService* prefs = browser()->profile()->GetPrefs();
  prefs->SetBoolean(prefs::kEnableDoNotTrack, true);

  ASSERT_NO_FATAL_FAILURE(content::WaitForLoadStop(
      chrome::GetActiveWebContents(browser())));

  std::string do_not_track;
  EXPECT_TRUE(content::ExecuteJavaScriptAndExtractString(
      chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
      std::wstring(),
      L"window.domAutomationController.send(navigator.doNotTrack)",
      &do_not_track));
  EXPECT_EQ("1", do_not_track);

  // Reset flag and check that the changed value is propagated to the existing
  // renderer.
  prefs->SetBoolean(prefs::kEnableDoNotTrack, false);

  EXPECT_TRUE(content::ExecuteJavaScriptAndExtractString(
      chrome::GetActiveWebContents(browser())->GetRenderViewHost(),
      std::wstring(),
      L"window.domAutomationController.send("
      L"navigator.doNotTrack === null ? '0' : '1')",
      &do_not_track));
  EXPECT_EQ("0", do_not_track);
}