OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/public/browser/host_zoom_map.h" | 5 #include "content/public/browser/host_zoom_map.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
68 }; | 68 }; |
69 | 69 |
70 } // namespace | 70 } // namespace |
71 | 71 |
72 class HostZoomMapBrowserTest : public InProcessBrowserTest { | 72 class HostZoomMapBrowserTest : public InProcessBrowserTest { |
73 public: | 73 public: |
74 HostZoomMapBrowserTest() {} | 74 HostZoomMapBrowserTest() {} |
75 | 75 |
76 protected: | 76 protected: |
77 void SetDefaultZoomLevel(double level) { | 77 void SetDefaultZoomLevel(double level) { |
78 browser()->profile()->GetPrefs()->SetDouble( | 78 browser()->profile()->GetZoomLevelPrefs()->SetDouble( |
79 prefs::kDefaultZoomLevel, level); | 79 prefs::kDefaultZoomLevel, level); |
80 } | 80 } |
81 | 81 |
82 double GetZoomLevel(const GURL& url) { | 82 double GetZoomLevel(const GURL& url) { |
83 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>( | 83 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>( |
84 content::HostZoomMap::GetDefaultForBrowserContext( | 84 content::HostZoomMap::GetDefaultForBrowserContext( |
85 browser()->profile())); | 85 browser()->profile())); |
86 return host_zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), | 86 return host_zoom_map->GetZoomLevelForHostAndScheme(url.scheme(), |
87 url.host()); | 87 url.host()); |
88 } | 88 } |
89 | 89 |
90 std::vector<std::string> GetHostsWithZoomLevels() { | 90 std::vector<std::string> GetHostsWithZoomLevels() { |
91 typedef content::HostZoomMap::ZoomLevelVector ZoomLevelVector; | 91 typedef content::HostZoomMap::ZoomLevelVector ZoomLevelVector; |
92 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>( | 92 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>( |
93 content::HostZoomMap::GetDefaultForBrowserContext( | 93 content::HostZoomMap::GetDefaultForBrowserContext( |
94 browser()->profile())); | 94 browser()->profile())); |
95 content::HostZoomMap::ZoomLevelVector zoom_levels = | 95 content::HostZoomMap::ZoomLevelVector zoom_levels = |
96 host_zoom_map->GetAllZoomLevels(); | 96 host_zoom_map->GetAllZoomLevels(); |
97 std::vector<std::string> results; | 97 std::vector<std::string> results; |
98 for (ZoomLevelVector::const_iterator it = zoom_levels.begin(); | 98 for (ZoomLevelVector::const_iterator it = zoom_levels.begin(); |
99 it != zoom_levels.end(); ++it) | 99 it != zoom_levels.end(); ++it) |
100 results.push_back(it->host); | 100 results.push_back(it->host); |
101 return results; | 101 return results; |
102 } | 102 } |
103 | 103 |
104 std::vector<std::string> GetHostsWithZoomLevelsFromPrefs() { | 104 std::vector<std::string> GetHostsWithZoomLevelsFromPrefs() { |
105 PrefService* prefs = browser()->profile()->GetPrefs(); | 105 PrefService* prefs = browser()->profile()->GetZoomLevelPrefs(); |
106 const base::DictionaryValue* values = | 106 const base::DictionaryValue* values = |
107 prefs->GetDictionary(prefs::kPerHostZoomLevels); | 107 prefs->GetDictionary(prefs::kPerHostZoomLevels); |
108 std::vector<std::string> results; | 108 std::vector<std::string> results; |
109 if (values) { | 109 if (values) { |
110 for (base::DictionaryValue::Iterator it(*values); | 110 for (base::DictionaryValue::Iterator it(*values); |
111 !it.IsAtEnd(); it.Advance()) | 111 !it.IsAtEnd(); it.Advance()) |
112 results.push_back(it.key()); | 112 results.push_back(it.key()); |
113 } | 113 } |
114 return results; | 114 return results; |
115 } | 115 } |
(...skipping 14 matching lines...) Expand all Loading... | |
130 virtual void SetUpOnMainThread() OVERRIDE { | 130 virtual void SetUpOnMainThread() OVERRIDE { |
131 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); | 131 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); |
132 embedded_test_server()->RegisterRequestHandler(base::Bind( | 132 embedded_test_server()->RegisterRequestHandler(base::Bind( |
133 &HostZoomMapBrowserTest::HandleRequest, base::Unretained(this))); | 133 &HostZoomMapBrowserTest::HandleRequest, base::Unretained(this))); |
134 host_resolver()->AddRule("*", "127.0.0.1"); | 134 host_resolver()->AddRule("*", "127.0.0.1"); |
135 } | 135 } |
136 | 136 |
137 DISALLOW_COPY_AND_ASSIGN(HostZoomMapBrowserTest); | 137 DISALLOW_COPY_AND_ASSIGN(HostZoomMapBrowserTest); |
138 }; | 138 }; |
139 | 139 |
140 class HostZoomMapSanitizationBrowserTest : public HostZoomMapBrowserTest { | 140 class HostZoomMapBrowserTestWithPrefs : public HostZoomMapBrowserTest { |
141 public: | 141 public: |
142 HostZoomMapSanitizationBrowserTest() {} | 142 HostZoomMapBrowserTestWithPrefs(std::string prefs_data, |
Bernhard Bauer
2014/09/12 10:28:49
Pass the strings by (const)reference?
wjmaclean
2014/09/16 18:44:44
Done.
| |
143 std::string prefs_filename) | |
144 : prefs_data_(prefs_data), prefs_filename_(prefs_filename) {} | |
143 | 145 |
144 private: | 146 private: |
145 // InProcessBrowserTest: | 147 // InProcessBrowserTest: |
146 virtual bool SetUpUserDataDirectory() OVERRIDE { | 148 virtual bool SetUpUserDataDirectory() OVERRIDE { |
147 // Zoom-related preferences demonstrating the two problems that could be | 149 std::replace(prefs_data_.begin(), prefs_data_.end(), '\'', '\"'); |
148 // caused by the bug. They incorrectly contain a per-host zoom level for the | |
149 // empty host; and a value for 'host1' that only differs from the default by | |
150 // epsilon. Neither should have been persisted. | |
151 const char kBrokenPrefs[] = | |
152 "{'profile': {" | |
153 " 'default_zoom_level': 1.2," | |
154 " 'per_host_zoom_levels': {'': 1.1, 'host1': 1.20001, 'host2': 1.3}" | |
155 "}}"; | |
156 std::string broken_prefs(kBrokenPrefs); | |
157 std::replace(broken_prefs.begin(), broken_prefs.end(), '\'', '\"'); | |
158 | 150 |
159 base::FilePath user_data_directory, path_to_prefs; | 151 base::FilePath user_data_directory, path_to_prefs; |
160 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); | 152 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); |
161 path_to_prefs = user_data_directory | 153 path_to_prefs = user_data_directory |
162 .AppendASCII(TestingProfile::kTestUserProfileDir) | 154 .AppendASCII(TestingProfile::kTestUserProfileDir) |
163 .Append(chrome::kPreferencesFilename); | 155 .Append(prefs_filename_); |
164 base::CreateDirectory(path_to_prefs.DirName()); | 156 base::CreateDirectory(path_to_prefs.DirName()); |
165 base::WriteFile(path_to_prefs, broken_prefs.c_str(), broken_prefs.size()); | 157 base::WriteFile( |
158 path_to_prefs, prefs_data_.c_str(), prefs_data_.size()); | |
166 return true; | 159 return true; |
167 } | 160 } |
168 | 161 |
162 std::string prefs_data_; | |
163 std::string prefs_filename_; | |
164 | |
165 DISALLOW_COPY_AND_ASSIGN(HostZoomMapBrowserTestWithPrefs); | |
166 }; | |
167 | |
168 class HostZoomMapSanitizationBrowserTest | |
169 : public HostZoomMapBrowserTestWithPrefs { | |
170 public: | |
171 HostZoomMapSanitizationBrowserTest() | |
172 : // Zoom-related preferences demonstrating the two problems that could | |
Bernhard Bauer
2014/09/12 10:28:49
Indentation / line breaks are off here. Also, do y
wjmaclean
2014/09/16 18:44:44
Done.
| |
173 // be | |
174 // caused by the bug. They incorrectly contain a per-host zoom level for | |
175 // the | |
176 // empty host; and a value for 'host1' that only differs from the | |
177 // default by | |
178 // epsilon. Neither should have been persisted. | |
179 HostZoomMapBrowserTestWithPrefs( | |
180 "{'partition': {" | |
181 " 'default_zoom_level': 1.2," | |
182 " 'per_host_zoom_levels': {'': 1.1, 'host1': 1.20001, 'host2': " | |
183 "1.3}" | |
184 "}}", | |
185 chrome::kZoomLevelPreferencesFilename) {} | |
186 | |
187 private: | |
169 DISALLOW_COPY_AND_ASSIGN(HostZoomMapSanitizationBrowserTest); | 188 DISALLOW_COPY_AND_ASSIGN(HostZoomMapSanitizationBrowserTest); |
170 }; | 189 }; |
171 | 190 |
172 // Regression test for crbug.com/364399. | 191 // Regression test for crbug.com/364399. |
173 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest, ToggleDefaultZoomLevel) { | 192 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest, ToggleDefaultZoomLevel) { |
174 const double default_zoom_level = content::ZoomFactorToZoomLevel(1.5); | 193 const double default_zoom_level = content::ZoomFactorToZoomLevel(1.5); |
175 | 194 |
176 const char kTestURLTemplate1[] = "http://host1:%d/"; | 195 const char kTestURLTemplate1[] = "http://host1:%d/"; |
177 const char kTestURLTemplate2[] = "http://host2:%d/"; | 196 const char kTestURLTemplate2[] = "http://host2:%d/"; |
178 | 197 |
(...skipping 30 matching lines...) Expand all Loading... | |
209 // any per-host values saved either to Pref, or internally in HostZoomMap. | 228 // any per-host values saved either to Pref, or internally in HostZoomMap. |
210 EXPECT_TRUE(GetHostsWithZoomLevels().empty()); | 229 EXPECT_TRUE(GetHostsWithZoomLevels().empty()); |
211 EXPECT_TRUE(GetHostsWithZoomLevelsFromPrefs().empty()); | 230 EXPECT_TRUE(GetHostsWithZoomLevelsFromPrefs().empty()); |
212 } | 231 } |
213 | 232 |
214 // Test that garbage data from crbug.com/364399 is cleared up on startup. | 233 // Test that garbage data from crbug.com/364399 is cleared up on startup. |
215 IN_PROC_BROWSER_TEST_F(HostZoomMapSanitizationBrowserTest, ClearOnStartup) { | 234 IN_PROC_BROWSER_TEST_F(HostZoomMapSanitizationBrowserTest, ClearOnStartup) { |
216 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2")); | 235 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2")); |
217 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2")); | 236 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2")); |
218 } | 237 } |
238 | |
239 class HostZoomMapMigrationBrowserTest : public HostZoomMapBrowserTestWithPrefs { | |
240 public: | |
241 HostZoomMapMigrationBrowserTest() | |
242 : // In this case we migrate the zoom level data from the profile prefs. | |
243 HostZoomMapBrowserTestWithPrefs( | |
244 "{'profile': {" | |
245 " 'default_zoom_level': 1.2," | |
246 " 'per_host_zoom_levels': {'': 1.1, 'host1': 1.20001, 'host2': " | |
247 "1.3}" | |
248 "}}", | |
249 chrome::kPreferencesFilename) {} | |
250 | |
251 static const double kOriginalDefaultZoomLevel; | |
252 | |
253 private: | |
254 DISALLOW_COPY_AND_ASSIGN(HostZoomMapMigrationBrowserTest); | |
255 }; | |
256 | |
257 const double HostZoomMapMigrationBrowserTest::kOriginalDefaultZoomLevel = 1.2; | |
258 | |
259 // This test is the same as HostZoomMapSanitizationBrowserTest, except that the | |
260 // zoom level data is loaded from the profile prefs, transfered to the | |
261 // zoom-level prefs, and we verify that the profile zoom level prefs are | |
262 // erased in the process. We also test that changes to the host zoom map and the | |
263 // default zoom level don't propagate back to the profile prefs. | |
264 IN_PROC_BROWSER_TEST_F(HostZoomMapMigrationBrowserTest, | |
265 MigrateProfileZoomPreferences) { | |
266 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2")); | |
267 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2")); | |
268 | |
269 PrefService* profile_prefs = | |
270 browser()->profile()->GetPrefs(); | |
271 PrefService* zoom_level_prefs = browser()->profile()->GetZoomLevelPrefs(); | |
272 // Make sure that the profile pref for default zoom level has been set to | |
273 // its default value of 0.0. | |
274 EXPECT_EQ(0.0, profile_prefs->GetDouble(prefs::kProfileDefaultZoomLevel)); | |
275 EXPECT_EQ(kOriginalDefaultZoomLevel, | |
276 zoom_level_prefs->GetDouble(prefs::kDefaultZoomLevel)); | |
277 | |
278 // Make sure that the profile prefs for per-host zoom levels are erased. | |
279 { | |
280 const base::DictionaryValue* profile_host_zoom_dictionary = | |
281 profile_prefs->GetDictionary(prefs::kProfilePerHostZoomLevels); | |
282 EXPECT_EQ(0UL, profile_host_zoom_dictionary->size()); | |
283 } | |
284 | |
285 ZoomLevelChangeObserver observer(browser()->profile()); | |
286 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>( | |
287 content::HostZoomMap::GetDefaultForBrowserContext( | |
288 browser()->profile())); | |
289 | |
290 // Make sure that a change to a host zoom level doesn't propagate to the | |
291 // profile prefs. | |
292 std::string host3("host3"); | |
293 host_zoom_map->SetZoomLevelForHost(host3, 1.3); | |
294 observer.BlockUntilZoomLevelForHostHasChanged(host3); | |
295 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), | |
296 testing::ElementsAre("host2", host3)); | |
297 { | |
298 const base::DictionaryValue* profile_host_zoom_dictionary = | |
299 profile_prefs->GetDictionary(prefs::kProfilePerHostZoomLevels); | |
300 EXPECT_EQ(0UL, profile_host_zoom_dictionary->size()); | |
301 } | |
302 | |
303 // Make sure a change to the default zoom level doesn't propagate to the | |
304 // profile prefs. | |
305 | |
306 // First, we need a host at the default zoom level to respond when the | |
307 // default zoom level changes. | |
308 const double kNewDefaultZoomLevel = 1.5; | |
309 GURL test_url = ConstructTestServerURL("http://host4:%d/"); | |
310 ui_test_utils::NavigateToURL(browser(), test_url); | |
311 EXPECT_TRUE(content::ZoomValuesEqual(kOriginalDefaultZoomLevel, | |
312 GetZoomLevel(test_url))); | |
313 | |
314 // Change the default zoom level and observe. | |
315 zoom_level_prefs->SetDouble(prefs::kDefaultZoomLevel, kNewDefaultZoomLevel); | |
316 observer.BlockUntilZoomLevelForHostHasChanged(test_url.host()); | |
317 EXPECT_TRUE( | |
318 content::ZoomValuesEqual(kNewDefaultZoomLevel, GetZoomLevel(test_url))); | |
319 EXPECT_EQ(kNewDefaultZoomLevel, | |
320 zoom_level_prefs->GetDouble(prefs::kDefaultZoomLevel)); | |
321 EXPECT_EQ(0.0, profile_prefs->GetDouble(prefs::kProfileDefaultZoomLevel)); | |
322 } | |
323 | |
OLD | NEW |