Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_usages_state_unittest.cc
blob22da47382fed689b6c254d9b879cca4fb12a46c3
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/browser/content_settings/host_content_settings_map.h"
11 #include "chrome/test/base/testing_profile.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, type);
32 GURL url_0("http://www.example.com");
34 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());
35 entry->SetURL(url_0);
36 content::LoadCommittedDetails load_committed_details;
37 load_committed_details.entry = entry.get();
38 state.DidNavigate(load_committed_details);
40 profile.GetHostContentSettingsMap()->SetContentSetting(
41 ContentSettingsPattern::FromURLNoWildcard(url_0),
42 ContentSettingsPattern::FromURLNoWildcard(url_0),
43 type,
44 std::string(),
45 CONTENT_SETTING_ALLOW);
46 state.OnPermissionSet(url_0, true);
48 GURL url_1("http://www.example1.com");
49 profile.GetHostContentSettingsMap()->SetContentSetting(
50 ContentSettingsPattern::FromURLNoWildcard(url_1),
51 ContentSettingsPattern::FromURLNoWildcard(url_0),
52 type,
53 std::string(),
54 CONTENT_SETTING_BLOCK);
55 state.OnPermissionSet(url_1, false);
57 ContentSettingsUsagesState::StateMap state_map =
58 state.state_map();
59 EXPECT_EQ(2U, state_map.size());
61 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state;
62 unsigned int tab_state_flags = 0;
63 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
64 EXPECT_TRUE(tab_state_flags &
65 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED)
66 << tab_state_flags;
67 EXPECT_TRUE(tab_state_flags &
68 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION)
69 << tab_state_flags;
70 EXPECT_FALSE(tab_state_flags &
71 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED)
72 << tab_state_flags;
73 EXPECT_TRUE(tab_state_flags &
74 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON)
75 << tab_state_flags;
76 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
77 EXPECT_EQ(1U,
78 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
79 url_0.host()));
81 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
82 EXPECT_EQ(1U,
83 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
84 url_1.host()));
86 state.OnPermissionSet(url_0, false);
88 formatted_host_per_state.clear();
89 tab_state_flags = 0;
90 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
91 EXPECT_FALSE(tab_state_flags &
92 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED)
93 << tab_state_flags;
94 EXPECT_TRUE(tab_state_flags &
95 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION)
96 << tab_state_flags;
97 EXPECT_TRUE(tab_state_flags &
98 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED)
99 << tab_state_flags;
100 EXPECT_TRUE(tab_state_flags &
101 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON)
102 << tab_state_flags;
103 EXPECT_EQ(0U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
104 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
105 EXPECT_EQ(1U,
106 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
107 url_0.host()));
108 EXPECT_EQ(1U,
109 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
110 url_1.host()));
112 state.OnPermissionSet(url_0, true);
114 load_committed_details.previous_url = url_0;
115 state.DidNavigate(load_committed_details);
117 ContentSettingsUsagesState::StateMap new_state_map =
118 state.state_map();
119 EXPECT_EQ(state_map.size(), new_state_map.size());
121 GURL different_url("http://foo.com");
122 entry->SetURL(different_url);
123 state.DidNavigate(load_committed_details);
125 EXPECT_TRUE(state.state_map().empty());
127 formatted_host_per_state.clear();
128 tab_state_flags = 0;
129 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
130 EXPECT_TRUE(formatted_host_per_state.empty());
131 EXPECT_EQ(0U, tab_state_flags);
134 void ShowPortOnSameHost(ContentSettingsType type) {
135 TestingProfile profile;
136 ContentSettingsUsagesState state(&profile, type);
137 GURL url_0("http://www.example.com");
139 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create());
140 entry->SetURL(url_0);
141 content::LoadCommittedDetails load_committed_details;
142 load_committed_details.entry = entry.get();
143 state.DidNavigate(load_committed_details);
145 profile.GetHostContentSettingsMap()->SetContentSetting(
146 ContentSettingsPattern::FromURLNoWildcard(url_0),
147 ContentSettingsPattern::FromURLNoWildcard(url_0),
148 type,
149 std::string(),
150 CONTENT_SETTING_ALLOW);
151 state.OnPermissionSet(url_0, true);
153 GURL url_1("https://www.example.com");
154 profile.GetHostContentSettingsMap()->SetContentSetting(
155 ContentSettingsPattern::FromURLNoWildcard(url_1),
156 ContentSettingsPattern::FromURLNoWildcard(url_0),
157 type,
158 std::string(),
159 CONTENT_SETTING_ALLOW);
160 state.OnPermissionSet(url_1, true);
162 GURL url_2("http://www.example1.com");
163 profile.GetHostContentSettingsMap()->SetContentSetting(
164 ContentSettingsPattern::FromURLNoWildcard(url_2),
165 ContentSettingsPattern::FromURLNoWildcard(url_0),
166 type,
167 std::string(),
168 CONTENT_SETTING_ALLOW);
169 state.OnPermissionSet(url_2, true);
171 ContentSettingsUsagesState::StateMap state_map =
172 state.state_map();
173 EXPECT_EQ(3U, state_map.size());
175 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state;
176 unsigned int tab_state_flags = 0;
177 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
179 EXPECT_EQ(3U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
180 EXPECT_EQ(1U,
181 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
182 url_0.spec()));
183 EXPECT_EQ(1U,
184 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
185 url_1.spec()));
186 EXPECT_EQ(1U,
187 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
188 url_2.host()));
190 state.OnPermissionSet(url_1, false);
191 formatted_host_per_state.clear();
192 tab_state_flags = 0;
193 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
195 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
196 EXPECT_EQ(1U,
197 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
198 url_0.spec()));
199 EXPECT_EQ(1U,
200 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
201 url_2.host()));
202 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
203 EXPECT_EQ(1U,
204 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
205 url_1.spec()));
208 protected:
209 base::MessageLoop message_loop_;
210 content::TestBrowserThread ui_thread_;
213 TEST_F(ContentSettingsUsagesStateTests, ClearOnNewOriginForGeolocation) {
214 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_GEOLOCATION);
217 TEST_F(ContentSettingsUsagesStateTests, ClearOnNewOriginForMIDI) {
218 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
221 TEST_F(ContentSettingsUsagesStateTests, ShowPortOnSameHostForGeolocation) {
222 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_GEOLOCATION);
225 TEST_F(ContentSettingsUsagesStateTests, ShowPortOnSameHostForMIDI) {
226 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
229 } // namespace