Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(604)

Side by Side Diff: chrome/browser/profiles/host_zoom_map_browsertest.cc

Issue 541103002: Introduce ChromeZoomLevelPref, make zoom level prefs independent of profile prefs. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Revise histogram collection. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/files/file_path.h" 12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h" 13 #include "base/files/file_util.h"
14 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h" 15 #include "base/memory/scoped_ptr.h"
16 #include "base/path_service.h" 16 #include "base/path_service.h"
17 #include "base/prefs/pref_service.h" 17 #include "base/prefs/pref_service.h"
18 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
19 #include "base/values.h" 19 #include "base/values.h"
20 #include "chrome/browser/chrome_page_zoom.h" 20 #include "chrome/browser/chrome_page_zoom.h"
21 #include "chrome/browser/profiles/profile.h" 21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/browser.h" 22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h" 23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
24 #include "chrome/common/chrome_constants.h" 25 #include "chrome/common/chrome_constants.h"
25 #include "chrome/common/chrome_paths.h" 26 #include "chrome/common/chrome_paths.h"
26 #include "chrome/common/pref_names.h" 27 #include "chrome/common/pref_names.h"
27 #include "chrome/test/base/in_process_browser_test.h" 28 #include "chrome/test/base/in_process_browser_test.h"
28 #include "chrome/test/base/testing_profile.h" 29 #include "chrome/test/base/testing_profile.h"
29 #include "chrome/test/base/ui_test_utils.h" 30 #include "chrome/test/base/ui_test_utils.h"
30 #include "content/public/test/test_utils.h" 31 #include "content/public/test/test_utils.h"
31 #include "net/dns/mock_host_resolver.h" 32 #include "net/dns/mock_host_resolver.h"
32 #include "net/test/embedded_test_server/embedded_test_server.h" 33 #include "net/test/embedded_test_server/embedded_test_server.h"
33 #include "net/test/embedded_test_server/http_response.h" 34 #include "net/test/embedded_test_server/http_response.h"
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 }; 69 };
69 70
70 } // namespace 71 } // namespace
71 72
72 class HostZoomMapBrowserTest : public InProcessBrowserTest { 73 class HostZoomMapBrowserTest : public InProcessBrowserTest {
73 public: 74 public:
74 HostZoomMapBrowserTest() {} 75 HostZoomMapBrowserTest() {}
75 76
76 protected: 77 protected:
77 void SetDefaultZoomLevel(double level) { 78 void SetDefaultZoomLevel(double level) {
78 browser()->profile()->GetPrefs()->SetDouble( 79 browser()->profile()->GetZoomLevelPrefs()->SetDefaultZoomLevelPref(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()->GetPrefs();
106 const base::DictionaryValue* values = 106 const base::DictionaryValue* dictionaries =
107 prefs->GetDictionary(prefs::kPerHostZoomLevels); 107 prefs->GetDictionary(prefs::kPerHostZoomLevels);
108 const base::DictionaryValue* values = NULL;
109 dictionaries->GetDictionary("0", &values);
108 std::vector<std::string> results; 110 std::vector<std::string> results;
109 if (values) { 111 if (values) {
110 for (base::DictionaryValue::Iterator it(*values); 112 for (base::DictionaryValue::Iterator it(*values);
111 !it.IsAtEnd(); it.Advance()) 113 !it.IsAtEnd(); it.Advance())
112 results.push_back(it.key()); 114 results.push_back(it.key());
113 } 115 }
114 return results; 116 return results;
115 } 117 }
116 118
117 GURL ConstructTestServerURL(const char* url_template) { 119 GURL ConstructTestServerURL(const char* url_template) {
(...skipping 12 matching lines...) Expand all
130 virtual void SetUpOnMainThread() OVERRIDE { 132 virtual void SetUpOnMainThread() OVERRIDE {
131 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady()); 133 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
132 embedded_test_server()->RegisterRequestHandler(base::Bind( 134 embedded_test_server()->RegisterRequestHandler(base::Bind(
133 &HostZoomMapBrowserTest::HandleRequest, base::Unretained(this))); 135 &HostZoomMapBrowserTest::HandleRequest, base::Unretained(this)));
134 host_resolver()->AddRule("*", "127.0.0.1"); 136 host_resolver()->AddRule("*", "127.0.0.1");
135 } 137 }
136 138
137 DISALLOW_COPY_AND_ASSIGN(HostZoomMapBrowserTest); 139 DISALLOW_COPY_AND_ASSIGN(HostZoomMapBrowserTest);
138 }; 140 };
139 141
140 class HostZoomMapSanitizationBrowserTest : public HostZoomMapBrowserTest { 142 #define PARTITION_KEY_PLACEHOLDER "NNN"
143
144 class HostZoomMapBrowserTestWithPrefs : public HostZoomMapBrowserTest {
141 public: 145 public:
142 HostZoomMapSanitizationBrowserTest() {} 146 explicit HostZoomMapBrowserTestWithPrefs(const std::string& prefs_data)
147 : prefs_data_(prefs_data) {}
143 148
144 private: 149 private:
145 // InProcessBrowserTest: 150 // InProcessBrowserTest:
146 virtual bool SetUpUserDataDirectory() OVERRIDE { 151 virtual bool SetUpUserDataDirectory() OVERRIDE {
147 // Zoom-related preferences demonstrating the two problems that could be 152 std::replace(prefs_data_.begin(), prefs_data_.end(), '\'', '\"');
148 // caused by the bug. They incorrectly contain a per-host zoom level for the 153 // It seems the hash functions on different platforms can return different
149 // empty host; and a value for 'host1' that only differs from the default by 154 // values for the same input, so make sure we test with the hash appropriate
150 // epsilon. Neither should have been persisted. 155 // for the platform.
151 const char kBrokenPrefs[] = 156 std::string hash_string =
152 "{'profile': {" 157 chrome::ChromeZoomLevelPrefs::GetHash(base::FilePath());
153 " 'default_zoom_level': 1.2," 158 std::string partition_key_placeholder(PARTITION_KEY_PLACEHOLDER);
154 " 'per_host_zoom_levels': {'': 1.1, 'host1': 1.20001, 'host2': 1.3}" 159 size_t start_index;
155 "}}"; 160 while ((start_index = prefs_data_.find(partition_key_placeholder)) !=
156 std::string broken_prefs(kBrokenPrefs); 161 std::string::npos) {
157 std::replace(broken_prefs.begin(), broken_prefs.end(), '\'', '\"'); 162 prefs_data_.replace(
163 start_index, partition_key_placeholder.size(), hash_string);
164 }
158 165
159 base::FilePath user_data_directory, path_to_prefs; 166 base::FilePath user_data_directory, path_to_prefs;
160 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory); 167 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory);
161 path_to_prefs = user_data_directory 168 path_to_prefs = user_data_directory
162 .AppendASCII(TestingProfile::kTestUserProfileDir) 169 .AppendASCII(TestingProfile::kTestUserProfileDir)
163 .Append(chrome::kPreferencesFilename); 170 .Append(chrome::kPreferencesFilename);
164 base::CreateDirectory(path_to_prefs.DirName()); 171 base::CreateDirectory(path_to_prefs.DirName());
165 base::WriteFile(path_to_prefs, broken_prefs.c_str(), broken_prefs.size()); 172 base::WriteFile(
173 path_to_prefs, prefs_data_.c_str(), prefs_data_.size());
166 return true; 174 return true;
167 } 175 }
168 176
177 std::string prefs_data_;
178
179 DISALLOW_COPY_AND_ASSIGN(HostZoomMapBrowserTestWithPrefs);
180 };
181
182 // Zoom-related preferences demonstrating the two problems that
183 // could be caused by the bug. They incorrectly contain a per-host
184 // zoom level for the empty host; and a value for 'host1' that only
185 // differs from the default by epsilon. Neither should have been
186 // persisted.
187 const char kSanitizationTestPrefs[] =
188 "{'partition': {"
189 " 'default_zoom_level': { '" PARTITION_KEY_PLACEHOLDER "': 1.2 },"
190 " 'per_host_zoom_levels': {"
191 " '" PARTITION_KEY_PLACEHOLDER "': {"
192 " '': 1.1, 'host1': 1.20001, 'host2': 1.3 }"
193 " }"
194 "}}";
195
196 #undef PARTITION_KEY_PLACEHOLDER
197
198 class HostZoomMapSanitizationBrowserTest
199 : public HostZoomMapBrowserTestWithPrefs {
200 public:
201 HostZoomMapSanitizationBrowserTest()
202 : HostZoomMapBrowserTestWithPrefs(kSanitizationTestPrefs) {}
203
204 private:
169 DISALLOW_COPY_AND_ASSIGN(HostZoomMapSanitizationBrowserTest); 205 DISALLOW_COPY_AND_ASSIGN(HostZoomMapSanitizationBrowserTest);
170 }; 206 };
171 207
172 // Regression test for crbug.com/364399. 208 // Regression test for crbug.com/364399.
173 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest, ToggleDefaultZoomLevel) { 209 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest, ToggleDefaultZoomLevel) {
174 const double default_zoom_level = content::ZoomFactorToZoomLevel(1.5); 210 const double default_zoom_level = content::ZoomFactorToZoomLevel(1.5);
175 211
176 const char kTestURLTemplate1[] = "http://host1:%d/"; 212 const char kTestURLTemplate1[] = "http://host1:%d/";
177 const char kTestURLTemplate2[] = "http://host2:%d/"; 213 const char kTestURLTemplate2[] = "http://host2:%d/";
178 214
(...skipping 30 matching lines...) Expand all
209 // any per-host values saved either to Pref, or internally in HostZoomMap. 245 // any per-host values saved either to Pref, or internally in HostZoomMap.
210 EXPECT_TRUE(GetHostsWithZoomLevels().empty()); 246 EXPECT_TRUE(GetHostsWithZoomLevels().empty());
211 EXPECT_TRUE(GetHostsWithZoomLevelsFromPrefs().empty()); 247 EXPECT_TRUE(GetHostsWithZoomLevelsFromPrefs().empty());
212 } 248 }
213 249
214 // Test that garbage data from crbug.com/364399 is cleared up on startup. 250 // Test that garbage data from crbug.com/364399 is cleared up on startup.
215 IN_PROC_BROWSER_TEST_F(HostZoomMapSanitizationBrowserTest, ClearOnStartup) { 251 IN_PROC_BROWSER_TEST_F(HostZoomMapSanitizationBrowserTest, ClearOnStartup) {
216 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2")); 252 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2"));
217 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2")); 253 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2"));
218 } 254 }
255
256 // In this case we migrate the zoom level data from the profile prefs.
257 const char kMigrationTestPrefs[] =
258 "{'profile': {"
259 " 'default_zoom_level': 1.2,"
260 " 'per_host_zoom_levels': {'': 1.1, 'host1': 1.20001, 'host2': "
261 "1.3}"
262 "}}";
263
264 class HostZoomMapMigrationBrowserTest : public HostZoomMapBrowserTestWithPrefs {
265 public:
266 HostZoomMapMigrationBrowserTest()
267 : HostZoomMapBrowserTestWithPrefs(kMigrationTestPrefs) {}
268
269 static const double kOriginalDefaultZoomLevel;
270
271 private:
272 DISALLOW_COPY_AND_ASSIGN(HostZoomMapMigrationBrowserTest);
273 };
274
275 const double HostZoomMapMigrationBrowserTest::kOriginalDefaultZoomLevel = 1.2;
276
277 // This test is the same as HostZoomMapSanitizationBrowserTest, except that the
278 // zoom level data is loaded from the profile prefs, transfered to the
279 // zoom-level prefs, and we verify that the profile zoom level prefs are
280 // erased in the process. We also test that changes to the host zoom map and the
281 // default zoom level don't propagate back to the profile prefs.
282 IN_PROC_BROWSER_TEST_F(HostZoomMapMigrationBrowserTest,
283 MigrateProfileZoomPreferences) {
284 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2"));
285 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2"));
286
287 PrefService* profile_prefs =
288 browser()->profile()->GetPrefs();
289 chrome::ChromeZoomLevelPrefs* zoom_level_prefs =
290 browser()->profile()->GetZoomLevelPrefs();
291 // Make sure that the profile pref for default zoom level has been set to
292 // its default value of 0.0.
293 EXPECT_EQ(0.0, profile_prefs->GetDouble(prefs::kDefaultZoomLevelInProfile));
294 EXPECT_EQ(kOriginalDefaultZoomLevel,
295 zoom_level_prefs->GetDefaultZoomLevelPref());
296
297 // Make sure that the profile prefs for per-host zoom levels are erased.
298 {
299 const base::DictionaryValue* profile_host_zoom_dictionary =
300 profile_prefs->GetDictionary(prefs::kPerHostZoomLevelsInProfile);
301 EXPECT_EQ(0UL, profile_host_zoom_dictionary->size());
302 }
303
304 ZoomLevelChangeObserver observer(browser()->profile());
305 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>(
306 content::HostZoomMap::GetDefaultForBrowserContext(
307 browser()->profile()));
308
309 // Make sure that a change to a host zoom level doesn't propagate to the
310 // profile prefs.
311 std::string host3("host3");
312 host_zoom_map->SetZoomLevelForHost(host3, 1.3);
313 observer.BlockUntilZoomLevelForHostHasChanged(host3);
314 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(),
315 testing::ElementsAre("host2", host3));
316 {
317 const base::DictionaryValue* profile_host_zoom_dictionary =
318 profile_prefs->GetDictionary(prefs::kPerHostZoomLevelsInProfile);
319 EXPECT_EQ(0UL, profile_host_zoom_dictionary->size());
320 }
321
322 // Make sure a change to the default zoom level doesn't propagate to the
323 // profile prefs.
324
325 // First, we need a host at the default zoom level to respond when the
326 // default zoom level changes.
327 const double kNewDefaultZoomLevel = 1.5;
328 GURL test_url = ConstructTestServerURL("http://host4:%d/");
329 ui_test_utils::NavigateToURL(browser(), test_url);
330 EXPECT_TRUE(content::ZoomValuesEqual(kOriginalDefaultZoomLevel,
331 GetZoomLevel(test_url)));
332
333 // Change the default zoom level and observe.
334 SetDefaultZoomLevel(kNewDefaultZoomLevel);
335 observer.BlockUntilZoomLevelForHostHasChanged(test_url.host());
336 EXPECT_TRUE(
337 content::ZoomValuesEqual(kNewDefaultZoomLevel, GetZoomLevel(test_url)));
338 EXPECT_EQ(kNewDefaultZoomLevel, zoom_level_prefs->GetDefaultZoomLevelPref());
339 EXPECT_EQ(0.0, profile_prefs->GetDouble(prefs::kDefaultZoomLevelInProfile));
340 }
341
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698