Use content::Referrer to pass around referrers in the plugin code
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_usages_state_unittest.cc
blob94f8a50558ca4418f105a6201ec12fc2d9f43e9c
1 // Copyright 2013 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 "chrome/browser/content_settings/content_settings_usages_state.h"
7 #include <string>
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "components/content_settings/core/browser/host_content_settings_map.h"
12 #include "content/public/browser/navigation_details.h"
13 #include "content/public/browser/navigation_entry.h"
14 #include "content/public/test/test_browser_thread.h"
15 #include "testing/gtest/include/gtest/gtest.h"
17 using content::BrowserThread;
18 using content::NavigationEntry;
20 namespace {
22 class ContentSettingsUsagesStateTests : public testing::Test {
23 public:
24 ContentSettingsUsagesStateTests()
25 : ui_thread_(BrowserThread::UI, &message_loop_) {
28 protected:
29 void ClearOnNewOrigin(ContentSettingsType type) {
30 TestingProfile profile;
31 ContentSettingsUsagesState state(profile.GetHostContentSettingsMap(),
32 profile.GetPrefs(), type);
33 GURL url_0("http://www.example.com");
35 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());
36 entry->SetURL(url_0);
37 content::LoadCommittedDetails load_committed_details;
38 load_committed_details.entry = entry.get();
39 state.DidNavigate(load_committed_details);
41 profile.GetHostContentSettingsMap()->SetContentSetting(
42 ContentSettingsPattern::FromURLNoWildcard(url_0),
43 ContentSettingsPattern::FromURLNoWildcard(url_0),
44 type,
45 std::string(),
46 CONTENT_SETTING_ALLOW);
47 state.OnPermissionSet(url_0, true);
49 GURL url_1("http://www.example1.com");
50 profile.GetHostContentSettingsMap()->SetContentSetting(
51 ContentSettingsPattern::FromURLNoWildcard(url_1),
52 ContentSettingsPattern::FromURLNoWildcard(url_0),
53 type,
54 std::string(),
55 CONTENT_SETTING_BLOCK);
56 state.OnPermissionSet(url_1, false);
58 ContentSettingsUsagesState::StateMap state_map =
59 state.state_map();
60 EXPECT_EQ(2U, state_map.size());
62 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state;
63 unsigned int tab_state_flags = 0;
64 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
65 EXPECT_TRUE(tab_state_flags &
66 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED)
67 << tab_state_flags;
68 EXPECT_TRUE(tab_state_flags &
69 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION)
70 << tab_state_flags;
71 EXPECT_FALSE(tab_state_flags &
72 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED)
73 << tab_state_flags;
74 EXPECT_TRUE(tab_state_flags &
75 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON)
76 << tab_state_flags;
77 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
78 EXPECT_EQ(1U,
79 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
80 url_0.host()));
82 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
83 EXPECT_EQ(1U,
84 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
85 url_1.host()));
87 state.OnPermissionSet(url_0, false);
89 formatted_host_per_state.clear();
90 tab_state_flags = 0;
91 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
92 EXPECT_FALSE(tab_state_flags &
93 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED)
94 << tab_state_flags;
95 EXPECT_TRUE(tab_state_flags &
96 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION)
97 << tab_state_flags;
98 EXPECT_TRUE(tab_state_flags &
99 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED)
100 << tab_state_flags;
101 EXPECT_TRUE(tab_state_flags &
102 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON)
103 << tab_state_flags;
104 EXPECT_EQ(0U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
105 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
106 EXPECT_EQ(1U,
107 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
108 url_0.host()));
109 EXPECT_EQ(1U,
110 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
111 url_1.host()));
113 state.OnPermissionSet(url_0, true);
115 load_committed_details.previous_url = url_0;
116 state.DidNavigate(load_committed_details);
118 ContentSettingsUsagesState::StateMap new_state_map =
119 state.state_map();
120 EXPECT_EQ(state_map.size(), new_state_map.size());
122 GURL different_url("http://foo.com");
123 entry->SetURL(different_url);
124 state.DidNavigate(load_committed_details);
126 EXPECT_TRUE(state.state_map().empty());
128 formatted_host_per_state.clear();
129 tab_state_flags = 0;
130 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
131 EXPECT_TRUE(formatted_host_per_state.empty());
132 EXPECT_EQ(0U, tab_state_flags);
135 void ShowPortOnSameHost(ContentSettingsType type) {
136 TestingProfile profile;
137 ContentSettingsUsagesState state(profile.GetHostContentSettingsMap(),
138 profile.GetPrefs(), type);
139 GURL url_0("http://www.example.com");
141 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());
142 entry->SetURL(url_0);
143 content::LoadCommittedDetails load_committed_details;
144 load_committed_details.entry = entry.get();
145 state.DidNavigate(load_committed_details);
147 profile.GetHostContentSettingsMap()->SetContentSetting(
148 ContentSettingsPattern::FromURLNoWildcard(url_0),
149 ContentSettingsPattern::FromURLNoWildcard(url_0),
150 type,
151 std::string(),
152 CONTENT_SETTING_ALLOW);
153 state.OnPermissionSet(url_0, true);
155 GURL url_1("https://www.example.com");
156 profile.GetHostContentSettingsMap()->SetContentSetting(
157 ContentSettingsPattern::FromURLNoWildcard(url_1),
158 ContentSettingsPattern::FromURLNoWildcard(url_0),
159 type,
160 std::string(),
161 CONTENT_SETTING_ALLOW);
162 state.OnPermissionSet(url_1, true);
164 GURL url_2("http://www.example1.com");
165 profile.GetHostContentSettingsMap()->SetContentSetting(
166 ContentSettingsPattern::FromURLNoWildcard(url_2),
167 ContentSettingsPattern::FromURLNoWildcard(url_0),
168 type,
169 std::string(),
170 CONTENT_SETTING_ALLOW);
171 state.OnPermissionSet(url_2, true);
173 ContentSettingsUsagesState::StateMap state_map =
174 state.state_map();
175 EXPECT_EQ(3U, state_map.size());
177 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state;
178 unsigned int tab_state_flags = 0;
179 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
181 EXPECT_EQ(3U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
182 EXPECT_EQ(1U,
183 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
184 url_0.spec()));
185 EXPECT_EQ(1U,
186 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
187 url_1.spec()));
188 EXPECT_EQ(1U,
189 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
190 url_2.host()));
192 state.OnPermissionSet(url_1, false);
193 formatted_host_per_state.clear();
194 tab_state_flags = 0;
195 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
197 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
198 EXPECT_EQ(1U,
199 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
200 url_0.spec()));
201 EXPECT_EQ(1U,
202 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
203 url_2.host()));
204 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
205 EXPECT_EQ(1U,
206 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
207 url_1.spec()));
210 protected:
211 base::MessageLoop message_loop_;
212 content::TestBrowserThread ui_thread_;
215 TEST_F(ContentSettingsUsagesStateTests, ClearOnNewOriginForGeolocation) {
216 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_GEOLOCATION);
219 TEST_F(ContentSettingsUsagesStateTests, ClearOnNewOriginForMidi) {
220 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
223 TEST_F(ContentSettingsUsagesStateTests, ShowPortOnSameHostForGeolocation) {
224 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_GEOLOCATION);
227 TEST_F(ContentSettingsUsagesStateTests, ShowPortOnSameHostForMidi) {
228 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
231 } // namespace