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"
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;
22 class ContentSettingsUsagesStateTests : public testing::Test {
24 ContentSettingsUsagesStateTests()
25 : ui_thread_(BrowserThread::UI, &message_loop_) {
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());
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),
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),
55 CONTENT_SETTING_BLOCK);
56 state.OnPermissionSet(url_1, false);
58 ContentSettingsUsagesState::StateMap 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)
68 EXPECT_TRUE(tab_state_flags &
69 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION)
71 EXPECT_FALSE(tab_state_flags &
72 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED)
74 EXPECT_TRUE(tab_state_flags &
75 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON)
77 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
79 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
82 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
84 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
87 state.OnPermissionSet(url_0, false);
89 formatted_host_per_state.clear();
91 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
92 EXPECT_FALSE(tab_state_flags &
93 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED)
95 EXPECT_TRUE(tab_state_flags &
96 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION)
98 EXPECT_TRUE(tab_state_flags &
99 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED)
101 EXPECT_TRUE(tab_state_flags &
102 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON)
104 EXPECT_EQ(0U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
105 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
107 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
110 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
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 =
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();
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),
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),
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),
170 CONTENT_SETTING_ALLOW);
171 state.OnPermissionSet(url_2, true);
173 ContentSettingsUsagesState::StateMap 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());
183 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
186 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
189 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
192 state.OnPermissionSet(url_1, false);
193 formatted_host_per_state.clear();
195 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags);
197 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size());
199 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
202 formatted_host_per_state[CONTENT_SETTING_ALLOW].count(
204 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size());
206 formatted_host_per_state[CONTENT_SETTING_BLOCK].count(
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);