Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / content_settings / content_settings_usages_state_unittest.cc
blobc6e3e46bae90111f6c08abfd8af5ad13063e4ab8
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 "components/content_settings/core/browser/content_settings_usages_state.h"
7 #include <string>
9 #include "base/message_loop/message_loop.h"
10 #include "chrome/common/pref_names.h"
11 #include "chrome/test/base/testing_profile.h"
12 #include "components/content_settings/core/browser/host_content_settings_map.h"
13 #include "content/public/test/test_browser_thread.h"
14 #include "testing/gtest/include/gtest/gtest.h"
16 using content::BrowserThread;
18 namespace {
20 ContentSettingsUsagesState::CommittedDetails CreateDetailsWithURL(
21 const GURL& url) {
22 ContentSettingsUsagesState::CommittedDetails details;
23 details.current_url_valid = true;
24 details.current_url = url;
25 return details;
28 class ContentSettingsUsagesStateTests : public testing::Test {
29 public:
30 ContentSettingsUsagesStateTests()
31 : ui_thread_(BrowserThread::UI, &message_loop_) {
34 protected:
35 void ClearOnNewOrigin(ContentSettingsType type) {
36 TestingProfile profile;
37 ContentSettingsUsagesState state(profile.GetHostContentSettingsMap(), type,
38 prefs::kAcceptLanguages,
39 profile.GetPrefs());
40 GURL url_0("http://www.example.com");
42 ContentSettingsUsagesState::CommittedDetails details =
43 CreateDetailsWithURL(url_0);
44 state.DidNavigate(details);
46 profile.GetHostContentSettingsMap()->SetContentSetting(
47 ContentSettingsPattern::FromURLNoWildcard(url_0),
48 ContentSettingsPattern::FromURLNoWildcard(url_0),
49 type,
50 std::string(),
51 CONTENT_SETTING_ALLOW);
52 state.OnPermissionSet(url_0, true);
54 GURL url_1("http://www.example1.com");
55 profile.GetHostContentSettingsMap()->SetContentSetting(
56 ContentSettingsPattern::FromURLNoWildcard(url_1),
57 ContentSettingsPattern::FromURLNoWildcard(url_0),
58 type,
59 std::string(),
60 CONTENT_SETTING_BLOCK);
61 state.OnPermissionSet(url_1, false);
63 ContentSettingsUsagesState::StateMap state_map =
64 state.state_map();
65 EXPECT_EQ(2U, state_map.size());
67 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state;
68 unsigned int tab_state_flags = 0;
69 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
70 EXPECT_TRUE(tab_state_flags &
71 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED)
72 << tab_state_flags;
73 EXPECT_TRUE(tab_state_flags &
74 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION)
75 << tab_state_flags;
76 EXPECT_FALSE(tab_state_flags &
77 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED)
78 << tab_state_flags;
79 EXPECT_TRUE(tab_state_flags &
80 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON)
81 << tab_state_flags;
82 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
83 EXPECT_EQ(1U,
84 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
85 url_0.host()));
87 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
88 EXPECT_EQ(1U,
89 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
90 url_1.host()));
92 state.OnPermissionSet(url_0, false);
94 formatted_host_per_state.clear();
95 tab_state_flags = 0;
96 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
97 EXPECT_FALSE(tab_state_flags &
98 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED)
99 << tab_state_flags;
100 EXPECT_TRUE(tab_state_flags &
101 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION)
102 << tab_state_flags;
103 EXPECT_TRUE(tab_state_flags &
104 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED)
105 << tab_state_flags;
106 EXPECT_TRUE(tab_state_flags &
107 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON)
108 << tab_state_flags;
109 EXPECT_EQ(0U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
110 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
111 EXPECT_EQ(1U,
112 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
113 url_0.host()));
114 EXPECT_EQ(1U,
115 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
116 url_1.host()));
118 state.OnPermissionSet(url_0, true);
120 details.previous_url = url_0;
121 state.DidNavigate(details);
123 ContentSettingsUsagesState::StateMap new_state_map =
124 state.state_map();
125 EXPECT_EQ(state_map.size(), new_state_map.size());
127 details.current_url = GURL("http://foo.com");
128 state.DidNavigate(details);
130 EXPECT_TRUE(state.state_map().empty());
132 formatted_host_per_state.clear();
133 tab_state_flags = 0;
134 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
135 EXPECT_TRUE(formatted_host_per_state.empty());
136 EXPECT_EQ(0U, tab_state_flags);
139 void ShowPortOnSameHost(ContentSettingsType type) {
140 TestingProfile profile;
141 ContentSettingsUsagesState state(profile.GetHostContentSettingsMap(), type,
142 prefs::kAcceptLanguages,
143 profile.GetPrefs());
144 GURL url_0("http://www.example.com");
146 ContentSettingsUsagesState::CommittedDetails details =
147 CreateDetailsWithURL(url_0);
148 state.DidNavigate(details);
150 profile.GetHostContentSettingsMap()->SetContentSetting(
151 ContentSettingsPattern::FromURLNoWildcard(url_0),
152 ContentSettingsPattern::FromURLNoWildcard(url_0),
153 type,
154 std::string(),
155 CONTENT_SETTING_ALLOW);
156 state.OnPermissionSet(url_0, true);
158 GURL url_1("https://www.example.com");
159 profile.GetHostContentSettingsMap()->SetContentSetting(
160 ContentSettingsPattern::FromURLNoWildcard(url_1),
161 ContentSettingsPattern::FromURLNoWildcard(url_0),
162 type,
163 std::string(),
164 CONTENT_SETTING_ALLOW);
165 state.OnPermissionSet(url_1, true);
167 GURL url_2("http://www.example1.com");
168 profile.GetHostContentSettingsMap()->SetContentSetting(
169 ContentSettingsPattern::FromURLNoWildcard(url_2),
170 ContentSettingsPattern::FromURLNoWildcard(url_0),
171 type,
172 std::string(),
173 CONTENT_SETTING_ALLOW);
174 state.OnPermissionSet(url_2, true);
176 ContentSettingsUsagesState::StateMap state_map =
177 state.state_map();
178 EXPECT_EQ(3U, state_map.size());
180 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state;
181 unsigned int tab_state_flags = 0;
182 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
184 EXPECT_EQ(3U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
185 EXPECT_EQ(1U,
186 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
187 url_0.spec()));
188 EXPECT_EQ(1U,
189 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
190 url_1.spec()));
191 EXPECT_EQ(1U,
192 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
193 url_2.host()));
195 state.OnPermissionSet(url_1, false);
196 formatted_host_per_state.clear();
197 tab_state_flags = 0;
198 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
200 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
201 EXPECT_EQ(1U,
202 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
203 url_0.spec()));
204 EXPECT_EQ(1U,
205 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
206 url_2.host()));
207 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
208 EXPECT_EQ(1U,
209 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
210 url_1.spec()));
213 protected:
214 base::MessageLoop message_loop_;
215 content::TestBrowserThread ui_thread_;
218 TEST_F(ContentSettingsUsagesStateTests, ClearOnNewOriginForGeolocation) {
219 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_GEOLOCATION);
222 TEST_F(ContentSettingsUsagesStateTests, ClearOnNewOriginForMidi) {
223 ClearOnNewOrigin(CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
226 TEST_F(ContentSettingsUsagesStateTests, ShowPortOnSameHostForGeolocation) {
227 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_GEOLOCATION);
230 TEST_F(ContentSettingsUsagesStateTests, ShowPortOnSameHostForMidi) {
231 ShowPortOnSameHost(CONTENT_SETTINGS_TYPE_MIDI_SYSEX);
234 } // namespace