Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/content_settings/content_settings_usages_state.h" | |
| 6 | |
| 5 #include <string> | 7 #include <string> |
| 6 | 8 |
| 7 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 8 #include "chrome/browser/content_settings/host_content_settings_map.h" | 10 #include "chrome/browser/content_settings/host_content_settings_map.h" |
| 9 #include "chrome/browser/geolocation/geolocation_settings_state.h" | |
| 10 #include "chrome/test/base/testing_profile.h" | 11 #include "chrome/test/base/testing_profile.h" |
| 11 #include "content/public/browser/navigation_details.h" | 12 #include "content/public/browser/navigation_details.h" |
| 12 #include "content/public/browser/navigation_entry.h" | 13 #include "content/public/browser/navigation_entry.h" |
| 13 #include "content/public/test/test_browser_thread.h" | 14 #include "content/public/test/test_browser_thread.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 using content::BrowserThread; | 17 using content::BrowserThread; |
| 17 using content::NavigationEntry; | 18 using content::NavigationEntry; |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 class GeolocationSettingsStateTests : public testing::Test { | 22 // ContentSettingsUsagesState class should work for any ContentSettingsType. |
| 23 // Here, following tests use |kTypeForTesting| as representation. | |
| 24 // TODO(toyoshim): Add at least one test to verify the class works with other | |
|
markusheintz_
2013/07/22 19:41:51
Can we file a bug for this please and add the issu
Takashi Toyoshima
2013/07/23 05:17:47
Done.
| |
| 25 // types. | |
| 26 ContentSettingsType kTypeForTesting = CONTENT_SETTINGS_TYPE_GEOLOCATION; | |
| 27 | |
| 28 class ContentSettingsUsagesStateTests : public testing::Test { | |
| 22 public: | 29 public: |
| 23 GeolocationSettingsStateTests() | 30 ContentSettingsUsagesStateTests() |
| 24 : ui_thread_(BrowserThread::UI, &message_loop_) { | 31 : ui_thread_(BrowserThread::UI, &message_loop_) { |
| 25 } | 32 } |
| 26 | 33 |
| 27 protected: | 34 protected: |
| 28 base::MessageLoop message_loop_; | 35 base::MessageLoop message_loop_; |
| 29 content::TestBrowserThread ui_thread_; | 36 content::TestBrowserThread ui_thread_; |
| 30 }; | 37 }; |
| 31 | 38 |
| 32 TEST_F(GeolocationSettingsStateTests, ClearOnNewOrigin) { | 39 TEST_F(ContentSettingsUsagesStateTests, ClearOnNewOrigin) { |
| 33 TestingProfile profile; | 40 TestingProfile profile; |
| 34 GeolocationSettingsState state(&profile); | 41 ContentSettingsUsagesState state(&profile, kTypeForTesting); |
| 35 GURL url_0("http://www.example.com"); | 42 GURL url_0("http://www.example.com"); |
| 36 | 43 |
| 37 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create()); | 44 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create()); |
| 38 entry->SetURL(url_0); | 45 entry->SetURL(url_0); |
| 39 content::LoadCommittedDetails load_committed_details; | 46 content::LoadCommittedDetails load_committed_details; |
| 40 load_committed_details.entry = entry.get(); | 47 load_committed_details.entry = entry.get(); |
| 41 state.DidNavigate(load_committed_details); | 48 state.DidNavigate(load_committed_details); |
| 42 | 49 |
| 43 profile.GetHostContentSettingsMap()->SetContentSetting( | 50 profile.GetHostContentSettingsMap()->SetContentSetting( |
| 44 ContentSettingsPattern::FromURLNoWildcard(url_0), | 51 ContentSettingsPattern::FromURLNoWildcard(url_0), |
| 45 ContentSettingsPattern::FromURLNoWildcard(url_0), | 52 ContentSettingsPattern::FromURLNoWildcard(url_0), |
| 46 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 53 kTypeForTesting, |
| 47 std::string(), | 54 std::string(), |
| 48 CONTENT_SETTING_ALLOW); | 55 CONTENT_SETTING_ALLOW); |
| 49 state.OnGeolocationPermissionSet(url_0, true); | 56 state.OnPermissionSet(url_0, true); |
| 50 | 57 |
| 51 GURL url_1("http://www.example1.com"); | 58 GURL url_1("http://www.example1.com"); |
| 52 profile.GetHostContentSettingsMap()->SetContentSetting( | 59 profile.GetHostContentSettingsMap()->SetContentSetting( |
| 53 ContentSettingsPattern::FromURLNoWildcard(url_1), | 60 ContentSettingsPattern::FromURLNoWildcard(url_1), |
| 54 ContentSettingsPattern::FromURLNoWildcard(url_0), | 61 ContentSettingsPattern::FromURLNoWildcard(url_0), |
| 55 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 62 kTypeForTesting, |
| 56 std::string(), | 63 std::string(), |
| 57 CONTENT_SETTING_BLOCK); | 64 CONTENT_SETTING_BLOCK); |
| 58 state.OnGeolocationPermissionSet(url_1, false); | 65 state.OnPermissionSet(url_1, false); |
| 59 | 66 |
| 60 GeolocationSettingsState::StateMap state_map = | 67 ContentSettingsUsagesState::StateMap state_map = |
| 61 state.state_map(); | 68 state.state_map(); |
| 62 EXPECT_EQ(2U, state_map.size()); | 69 EXPECT_EQ(2U, state_map.size()); |
| 63 | 70 |
| 64 GeolocationSettingsState::FormattedHostsPerState formatted_host_per_state; | 71 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state; |
| 65 unsigned int tab_state_flags = 0; | 72 unsigned int tab_state_flags = 0; |
| 66 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); | 73 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); |
| 67 EXPECT_TRUE(tab_state_flags & | 74 EXPECT_TRUE(tab_state_flags & |
| 68 GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED) | 75 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED) |
| 69 << tab_state_flags; | 76 << tab_state_flags; |
| 70 EXPECT_TRUE(tab_state_flags & | 77 EXPECT_TRUE(tab_state_flags & |
| 71 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION) | 78 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION) |
| 72 << tab_state_flags; | 79 << tab_state_flags; |
| 73 EXPECT_FALSE(tab_state_flags & | 80 EXPECT_FALSE(tab_state_flags & |
| 74 GeolocationSettingsState::TABSTATE_HAS_CHANGED) | 81 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED) |
| 75 << tab_state_flags; | 82 << tab_state_flags; |
| 76 EXPECT_TRUE(tab_state_flags & | 83 EXPECT_TRUE(tab_state_flags & |
| 77 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON) | 84 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON) |
| 78 << tab_state_flags; | 85 << tab_state_flags; |
| 79 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); | 86 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); |
| 80 EXPECT_EQ(1U, | 87 EXPECT_EQ(1U, |
| 81 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | 88 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( |
| 82 url_0.host())); | 89 url_0.host())); |
| 83 | 90 |
| 84 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size()); | 91 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size()); |
| 85 EXPECT_EQ(1U, | 92 EXPECT_EQ(1U, |
| 86 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( | 93 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( |
| 87 url_1.host())); | 94 url_1.host())); |
| 88 | 95 |
| 89 state.OnGeolocationPermissionSet(url_0, false); | 96 state.OnPermissionSet(url_0, false); |
| 90 | 97 |
| 91 formatted_host_per_state.clear(); | 98 formatted_host_per_state.clear(); |
| 92 tab_state_flags = 0; | 99 tab_state_flags = 0; |
| 93 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); | 100 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); |
| 94 EXPECT_FALSE(tab_state_flags & | 101 EXPECT_FALSE(tab_state_flags & |
| 95 GeolocationSettingsState::TABSTATE_HAS_ANY_ALLOWED) | 102 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ALLOWED) |
| 96 << tab_state_flags; | 103 << tab_state_flags; |
| 97 EXPECT_TRUE(tab_state_flags & | 104 EXPECT_TRUE(tab_state_flags & |
| 98 GeolocationSettingsState::TABSTATE_HAS_EXCEPTION) | 105 ContentSettingsUsagesState::TABSTATE_HAS_EXCEPTION) |
| 99 << tab_state_flags; | 106 << tab_state_flags; |
| 100 EXPECT_TRUE(tab_state_flags & | 107 EXPECT_TRUE(tab_state_flags & |
| 101 GeolocationSettingsState::TABSTATE_HAS_CHANGED) | 108 ContentSettingsUsagesState::TABSTATE_HAS_CHANGED) |
| 102 << tab_state_flags; | 109 << tab_state_flags; |
| 103 EXPECT_TRUE(tab_state_flags & | 110 EXPECT_TRUE(tab_state_flags & |
| 104 GeolocationSettingsState::TABSTATE_HAS_ANY_ICON) | 111 ContentSettingsUsagesState::TABSTATE_HAS_ANY_ICON) |
| 105 << tab_state_flags; | 112 << tab_state_flags; |
| 106 EXPECT_EQ(0U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); | 113 EXPECT_EQ(0U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); |
| 107 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size()); | 114 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size()); |
| 108 EXPECT_EQ(1U, | 115 EXPECT_EQ(1U, |
| 109 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( | 116 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( |
| 110 url_0.host())); | 117 url_0.host())); |
| 111 EXPECT_EQ(1U, | 118 EXPECT_EQ(1U, |
| 112 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( | 119 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( |
| 113 url_1.host())); | 120 url_1.host())); |
| 114 | 121 |
| 115 state.OnGeolocationPermissionSet(url_0, true); | 122 state.OnPermissionSet(url_0, true); |
| 116 | 123 |
| 117 load_committed_details.previous_url = url_0; | 124 load_committed_details.previous_url = url_0; |
| 118 state.DidNavigate(load_committed_details); | 125 state.DidNavigate(load_committed_details); |
| 119 | 126 |
| 120 GeolocationSettingsState::StateMap new_state_map = | 127 ContentSettingsUsagesState::StateMap new_state_map = |
| 121 state.state_map(); | 128 state.state_map(); |
| 122 EXPECT_EQ(state_map.size(), new_state_map.size()); | 129 EXPECT_EQ(state_map.size(), new_state_map.size()); |
| 123 | 130 |
| 124 GURL different_url("http://foo.com"); | 131 GURL different_url("http://foo.com"); |
| 125 entry->SetURL(different_url); | 132 entry->SetURL(different_url); |
| 126 state.DidNavigate(load_committed_details); | 133 state.DidNavigate(load_committed_details); |
| 127 | 134 |
| 128 EXPECT_TRUE(state.state_map().empty()); | 135 EXPECT_TRUE(state.state_map().empty()); |
| 129 | 136 |
| 130 formatted_host_per_state.clear(); | 137 formatted_host_per_state.clear(); |
| 131 tab_state_flags = 0; | 138 tab_state_flags = 0; |
| 132 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); | 139 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); |
| 133 EXPECT_TRUE(formatted_host_per_state.empty()); | 140 EXPECT_TRUE(formatted_host_per_state.empty()); |
| 134 EXPECT_EQ(0U, tab_state_flags); | 141 EXPECT_EQ(0U, tab_state_flags); |
| 135 } | 142 } |
| 136 | 143 |
| 137 TEST_F(GeolocationSettingsStateTests, ShowPortOnSameHost) { | 144 TEST_F(ContentSettingsUsagesStateTests, ShowPortOnSameHost) { |
| 138 TestingProfile profile; | 145 TestingProfile profile; |
| 139 GeolocationSettingsState state(&profile); | 146 ContentSettingsUsagesState state(&profile, kTypeForTesting); |
| 140 GURL url_0("http://www.example.com"); | 147 GURL url_0("http://www.example.com"); |
| 141 | 148 |
| 142 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create()); | 149 scoped_ptr<NavigationEntry> entry(NavigationEntry::Create()); |
| 143 entry->SetURL(url_0); | 150 entry->SetURL(url_0); |
| 144 content::LoadCommittedDetails load_committed_details; | 151 content::LoadCommittedDetails load_committed_details; |
| 145 load_committed_details.entry = entry.get(); | 152 load_committed_details.entry = entry.get(); |
| 146 state.DidNavigate(load_committed_details); | 153 state.DidNavigate(load_committed_details); |
| 147 | 154 |
| 148 profile.GetHostContentSettingsMap()->SetContentSetting( | 155 profile.GetHostContentSettingsMap()->SetContentSetting( |
| 149 ContentSettingsPattern::FromURLNoWildcard(url_0), | 156 ContentSettingsPattern::FromURLNoWildcard(url_0), |
| 150 ContentSettingsPattern::FromURLNoWildcard(url_0), | 157 ContentSettingsPattern::FromURLNoWildcard(url_0), |
| 151 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 158 kTypeForTesting, |
| 152 std::string(), | 159 std::string(), |
| 153 CONTENT_SETTING_ALLOW); | 160 CONTENT_SETTING_ALLOW); |
| 154 state.OnGeolocationPermissionSet(url_0, true); | 161 state.OnPermissionSet(url_0, true); |
| 155 | 162 |
| 156 GURL url_1("https://www.example.com"); | 163 GURL url_1("https://www.example.com"); |
| 157 profile.GetHostContentSettingsMap()->SetContentSetting( | 164 profile.GetHostContentSettingsMap()->SetContentSetting( |
| 158 ContentSettingsPattern::FromURLNoWildcard(url_1), | 165 ContentSettingsPattern::FromURLNoWildcard(url_1), |
| 159 ContentSettingsPattern::FromURLNoWildcard(url_0), | 166 ContentSettingsPattern::FromURLNoWildcard(url_0), |
| 160 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 167 kTypeForTesting, |
| 161 std::string(), | 168 std::string(), |
| 162 CONTENT_SETTING_ALLOW); | 169 CONTENT_SETTING_ALLOW); |
| 163 state.OnGeolocationPermissionSet(url_1, true); | 170 state.OnPermissionSet(url_1, true); |
| 164 | 171 |
| 165 GURL url_2("http://www.example1.com"); | 172 GURL url_2("http://www.example1.com"); |
| 166 profile.GetHostContentSettingsMap()->SetContentSetting( | 173 profile.GetHostContentSettingsMap()->SetContentSetting( |
| 167 ContentSettingsPattern::FromURLNoWildcard(url_2), | 174 ContentSettingsPattern::FromURLNoWildcard(url_2), |
| 168 ContentSettingsPattern::FromURLNoWildcard(url_0), | 175 ContentSettingsPattern::FromURLNoWildcard(url_0), |
| 169 CONTENT_SETTINGS_TYPE_GEOLOCATION, | 176 kTypeForTesting, |
| 170 std::string(), | 177 std::string(), |
| 171 CONTENT_SETTING_ALLOW); | 178 CONTENT_SETTING_ALLOW); |
| 172 state.OnGeolocationPermissionSet(url_2, true); | 179 state.OnPermissionSet(url_2, true); |
| 173 | 180 |
| 174 GeolocationSettingsState::StateMap state_map = | 181 ContentSettingsUsagesState::StateMap state_map = |
| 175 state.state_map(); | 182 state.state_map(); |
| 176 EXPECT_EQ(3U, state_map.size()); | 183 EXPECT_EQ(3U, state_map.size()); |
| 177 | 184 |
| 178 GeolocationSettingsState::FormattedHostsPerState formatted_host_per_state; | 185 ContentSettingsUsagesState::FormattedHostsPerState formatted_host_per_state; |
| 179 unsigned int tab_state_flags = 0; | 186 unsigned int tab_state_flags = 0; |
| 180 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); | 187 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); |
| 181 | 188 |
| 182 EXPECT_EQ(3U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); | 189 EXPECT_EQ(3U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); |
| 183 EXPECT_EQ(1U, | 190 EXPECT_EQ(1U, |
| 184 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | 191 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( |
| 185 url_0.spec())); | 192 url_0.spec())); |
| 186 EXPECT_EQ(1U, | 193 EXPECT_EQ(1U, |
| 187 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | 194 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( |
| 188 url_1.spec())); | 195 url_1.spec())); |
| 189 EXPECT_EQ(1U, | 196 EXPECT_EQ(1U, |
| 190 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | 197 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( |
| 191 url_2.host())); | 198 url_2.host())); |
| 192 | 199 |
| 193 state.OnGeolocationPermissionSet(url_1, false); | 200 state.OnPermissionSet(url_1, false); |
| 194 formatted_host_per_state.clear(); | 201 formatted_host_per_state.clear(); |
| 195 tab_state_flags = 0; | 202 tab_state_flags = 0; |
| 196 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); | 203 state.GetDetailedInfo(&formatted_host_per_state, &tab_state_flags); |
| 197 | 204 |
| 198 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); | 205 EXPECT_EQ(2U, formatted_host_per_state[CONTENT_SETTING_ALLOW].size()); |
| 199 EXPECT_EQ(1U, | 206 EXPECT_EQ(1U, |
| 200 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | 207 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( |
| 201 url_0.spec())); | 208 url_0.spec())); |
| 202 EXPECT_EQ(1U, | 209 EXPECT_EQ(1U, |
| 203 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( | 210 formatted_host_per_state[CONTENT_SETTING_ALLOW].count( |
| 204 url_2.host())); | 211 url_2.host())); |
| 205 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size()); | 212 EXPECT_EQ(1U, formatted_host_per_state[CONTENT_SETTING_BLOCK].size()); |
| 206 EXPECT_EQ(1U, | 213 EXPECT_EQ(1U, |
| 207 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( | 214 formatted_host_per_state[CONTENT_SETTING_BLOCK].count( |
| 208 url_1.spec())); | 215 url_1.spec())); |
| 209 } | 216 } |
| 210 | 217 |
| 211 | 218 |
| 212 } // namespace | 219 } // namespace |
| OLD | NEW |