ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / browser / profiles / host_zoom_map_browsertest.cc
blobc4f5730d211b808742e8998a85e12f5a1d2cc0c9
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
3 // found in the LICENSE file.
5 #include "content/public/browser/host_zoom_map.h"
7 #include <algorithm>
8 #include <string>
9 #include <vector>
11 #include "base/bind.h"
12 #include "base/files/file_path.h"
13 #include "base/files/file_util.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/path_service.h"
17 #include "base/prefs/pref_service.h"
18 #include "base/strings/stringprintf.h"
19 #include "base/values.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile_impl.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/tabs/tab_strip_model.h"
24 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h"
25 #include "chrome/common/chrome_constants.h"
26 #include "chrome/common/chrome_paths.h"
27 #include "chrome/common/pref_names.h"
28 #include "chrome/common/url_constants.h"
29 #include "chrome/test/base/in_process_browser_test.h"
30 #include "chrome/test/base/testing_profile.h"
31 #include "chrome/test/base/ui_test_utils.h"
32 #include "components/signin/core/common/profile_management_switches.h"
33 #include "components/ui/zoom/page_zoom.h"
34 #include "components/ui/zoom/zoom_event_manager.h"
35 #include "content/public/test/test_utils.h"
36 #include "net/dns/mock_host_resolver.h"
37 #include "net/test/embedded_test_server/embedded_test_server.h"
38 #include "net/test/embedded_test_server/http_response.h"
39 #include "testing/gmock/include/gmock/gmock.h"
40 #include "url/gurl.h"
42 using content::HostZoomMap;
44 namespace {
46 class ZoomLevelChangeObserver {
47 public:
48 explicit ZoomLevelChangeObserver(content::BrowserContext* context)
49 : message_loop_runner_(new content::MessageLoopRunner) {
50 subscription_ = ui_zoom::ZoomEventManager::GetForBrowserContext(context)
51 ->AddZoomLevelChangedCallback(base::Bind(
52 &ZoomLevelChangeObserver::OnZoomLevelChanged,
53 base::Unretained(this)));
56 void BlockUntilZoomLevelForHostHasChanged(const std::string& host) {
57 while (!std::count(changed_hosts_.begin(), changed_hosts_.end(), host)) {
58 message_loop_runner_->Run();
59 message_loop_runner_ = new content::MessageLoopRunner;
61 changed_hosts_.clear();
64 private:
65 void OnZoomLevelChanged(const content::HostZoomMap::ZoomLevelChange& change) {
66 changed_hosts_.push_back(change.host);
67 message_loop_runner_->Quit();
70 scoped_refptr<content::MessageLoopRunner> message_loop_runner_;
71 std::vector<std::string> changed_hosts_;
72 scoped_ptr<content::HostZoomMap::Subscription> subscription_;
74 DISALLOW_COPY_AND_ASSIGN(ZoomLevelChangeObserver);
77 } // namespace
79 class HostZoomMapBrowserTest : public InProcessBrowserTest {
80 public:
81 HostZoomMapBrowserTest() {}
83 protected:
84 void SetDefaultZoomLevel(double level) {
85 browser()->profile()->GetZoomLevelPrefs()->SetDefaultZoomLevelPref(level);
88 double GetZoomLevel(const GURL& url) {
89 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>(
90 content::HostZoomMap::GetDefaultForBrowserContext(
91 browser()->profile()));
92 return host_zoom_map->GetZoomLevelForHostAndScheme(url.scheme(),
93 url.host());
96 std::vector<std::string> GetHostsWithZoomLevels() {
97 typedef content::HostZoomMap::ZoomLevelVector ZoomLevelVector;
98 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>(
99 content::HostZoomMap::GetDefaultForBrowserContext(
100 browser()->profile()));
101 content::HostZoomMap::ZoomLevelVector zoom_levels =
102 host_zoom_map->GetAllZoomLevels();
103 std::vector<std::string> results;
104 for (ZoomLevelVector::const_iterator it = zoom_levels.begin();
105 it != zoom_levels.end(); ++it)
106 results.push_back(it->host);
107 return results;
110 std::vector<std::string> GetHostsWithZoomLevelsFromPrefs() {
111 PrefService* prefs = browser()->profile()->GetPrefs();
112 const base::DictionaryValue* dictionaries =
113 prefs->GetDictionary(prefs::kPartitionPerHostZoomLevels);
114 const base::DictionaryValue* values = NULL;
115 std::string partition_key =
116 chrome::ChromeZoomLevelPrefs::GetHashForTesting(base::FilePath());
117 dictionaries->GetDictionary(partition_key, &values);
118 std::vector<std::string> results;
119 if (values) {
120 for (base::DictionaryValue::Iterator it(*values);
121 !it.IsAtEnd(); it.Advance())
122 results.push_back(it.key());
124 return results;
127 GURL ConstructTestServerURL(const char* url_template) {
128 return GURL(base::StringPrintf(
129 url_template, embedded_test_server()->port()));
132 private:
133 scoped_ptr<net::test_server::HttpResponse> HandleRequest(
134 const net::test_server::HttpRequest& request) {
135 return scoped_ptr<net::test_server::HttpResponse>(
136 new net::test_server::BasicHttpResponse);
139 // BrowserTestBase:
140 void SetUpOnMainThread() override {
141 ASSERT_TRUE(embedded_test_server()->InitializeAndWaitUntilReady());
142 embedded_test_server()->RegisterRequestHandler(base::Bind(
143 &HostZoomMapBrowserTest::HandleRequest, base::Unretained(this)));
144 host_resolver()->AddRule("*", "127.0.0.1");
147 DISALLOW_COPY_AND_ASSIGN(HostZoomMapBrowserTest);
150 #define PARTITION_KEY_PLACEHOLDER "NNN"
152 class HostZoomMapBrowserTestWithPrefs : public HostZoomMapBrowserTest {
153 public:
154 explicit HostZoomMapBrowserTestWithPrefs(const std::string& prefs_data)
155 : prefs_data_(prefs_data) {}
157 private:
158 // InProcessBrowserTest:
159 bool SetUpUserDataDirectory() override {
160 std::replace(prefs_data_.begin(), prefs_data_.end(), '\'', '\"');
161 // It seems the hash functions on different platforms can return different
162 // values for the same input, so make sure we test with the hash appropriate
163 // for the platform.
164 std::string hash_string =
165 chrome::ChromeZoomLevelPrefs::GetHashForTesting(base::FilePath());
166 std::string partition_key_placeholder(PARTITION_KEY_PLACEHOLDER);
167 size_t start_index;
168 while ((start_index = prefs_data_.find(partition_key_placeholder)) !=
169 std::string::npos) {
170 prefs_data_.replace(
171 start_index, partition_key_placeholder.size(), hash_string);
174 base::FilePath user_data_directory, path_to_prefs;
175 PathService::Get(chrome::DIR_USER_DATA, &user_data_directory);
176 path_to_prefs = user_data_directory
177 .AppendASCII(TestingProfile::kTestUserProfileDir)
178 .Append(chrome::kPreferencesFilename);
179 base::CreateDirectory(path_to_prefs.DirName());
180 base::WriteFile(
181 path_to_prefs, prefs_data_.c_str(), prefs_data_.size());
182 return true;
185 std::string prefs_data_;
187 DISALLOW_COPY_AND_ASSIGN(HostZoomMapBrowserTestWithPrefs);
190 // Zoom-related preferences demonstrating the two problems that
191 // could be caused by the bug. They incorrectly contain a per-host
192 // zoom level for the empty host; and a value for 'host1' that only
193 // differs from the default by epsilon. Neither should have been
194 // persisted.
195 const char kSanitizationTestPrefs[] =
196 "{'partition': {"
197 " 'default_zoom_level': { '" PARTITION_KEY_PLACEHOLDER "': 1.2 },"
198 " 'per_host_zoom_levels': {"
199 " '" PARTITION_KEY_PLACEHOLDER "': {"
200 " '': 1.1, 'host1': 1.20001, 'host2': 1.3 }"
201 " }"
202 "}}";
204 #undef PARTITION_KEY_PLACEHOLDER
206 class HostZoomMapSanitizationBrowserTest
207 : public HostZoomMapBrowserTestWithPrefs {
208 public:
209 HostZoomMapSanitizationBrowserTest()
210 : HostZoomMapBrowserTestWithPrefs(kSanitizationTestPrefs) {}
212 private:
213 DISALLOW_COPY_AND_ASSIGN(HostZoomMapSanitizationBrowserTest);
216 // Regression test for crbug.com/437392
217 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest, ZoomEventsWorkForOffTheRecord) {
218 GURL test_url(url::kAboutBlankURL);
219 std::string test_host(test_url.host());
220 std::string test_scheme(test_url.scheme());
221 Browser* incognito_browser =
222 ui_test_utils::OpenURLOffTheRecord(browser()->profile(), test_url);
224 content::WebContents* web_contents =
225 incognito_browser->tab_strip_model()->GetActiveWebContents();
227 content::BrowserContext* context = web_contents->GetBrowserContext();
228 EXPECT_TRUE(context->IsOffTheRecord());
229 ZoomLevelChangeObserver observer(context);
230 HostZoomMap* host_zoom_map = HostZoomMap::GetForWebContents(web_contents);
232 double new_zoom_level =
233 host_zoom_map->GetZoomLevelForHostAndScheme(test_scheme, test_host) + 0.5;
234 host_zoom_map->SetZoomLevelForHostAndScheme(test_scheme, test_host,
235 new_zoom_level);
236 observer.BlockUntilZoomLevelForHostHasChanged(test_host);
237 EXPECT_EQ(new_zoom_level, host_zoom_map->GetZoomLevelForHostAndScheme(
238 test_scheme, test_host));
241 // Regression test for crbug.com/435017.
242 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest,
243 EventsForNonDefaultStoragePartition) {
244 ZoomLevelChangeObserver observer(browser()->profile());
245 // TODO(wjmaclean): Make this test more general by implementing a way to
246 // force a generic URL to be loaded in a non-default storage partition. This
247 // test currently relies on the signin page being loaded into a non-default
248 // storage partition (and verifies this is the case), but ultimately it would
249 // be better not to rely on what the signin page is doing.
250 GURL test_url = ConstructTestServerURL(chrome::kChromeUIChromeSigninURL);
251 std::string test_host(test_url.host());
252 std::string test_scheme(test_url.scheme());
253 ui_test_utils::NavigateToURL(browser(), test_url);
255 content::WebContents* web_contents =
256 browser()->tab_strip_model()->GetActiveWebContents();
258 // Verify that our loaded page is using a HostZoomMap different from the
259 // one for the default StoragePartition.
260 HostZoomMap* host_zoom_map = HostZoomMap::GetForWebContents(web_contents);
262 // For the webview based sign-in code, the sign in page uses the default host
263 // zoom map.
264 if (!switches::IsEnableWebviewBasedSignin()) {
265 HostZoomMap* default_profile_host_zoom_map =
266 HostZoomMap::GetDefaultForBrowserContext(browser()->profile());
267 EXPECT_NE(host_zoom_map, default_profile_host_zoom_map);
270 double new_zoom_level =
271 host_zoom_map->GetZoomLevelForHostAndScheme(test_scheme, test_host) + 0.5;
272 host_zoom_map->SetZoomLevelForHostAndScheme(test_scheme, test_host,
273 new_zoom_level);
274 observer.BlockUntilZoomLevelForHostHasChanged(test_host);
275 EXPECT_EQ(new_zoom_level, host_zoom_map->GetZoomLevelForHostAndScheme(
276 test_scheme, test_host));
279 // Regression test for crbug.com/364399.
280 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest, ToggleDefaultZoomLevel) {
281 const double default_zoom_level = content::ZoomFactorToZoomLevel(1.5);
283 const char kTestURLTemplate1[] = "http://host1:%u/";
284 const char kTestURLTemplate2[] = "http://host2:%u/";
286 ZoomLevelChangeObserver observer(browser()->profile());
288 GURL test_url1 = ConstructTestServerURL(kTestURLTemplate1);
289 ui_test_utils::NavigateToURL(browser(), test_url1);
291 SetDefaultZoomLevel(default_zoom_level);
292 observer.BlockUntilZoomLevelForHostHasChanged(test_url1.host());
293 EXPECT_TRUE(
294 content::ZoomValuesEqual(default_zoom_level, GetZoomLevel(test_url1)));
296 GURL test_url2 = ConstructTestServerURL(kTestURLTemplate2);
297 ui_test_utils::NavigateToURLWithDisposition(
298 browser(), test_url2, NEW_FOREGROUND_TAB,
299 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION);
300 EXPECT_TRUE(
301 content::ZoomValuesEqual(default_zoom_level, GetZoomLevel(test_url2)));
303 content::WebContents* web_contents =
304 browser()->tab_strip_model()->GetActiveWebContents();
305 ui_zoom::PageZoom::Zoom(web_contents, content::PAGE_ZOOM_OUT);
306 observer.BlockUntilZoomLevelForHostHasChanged(test_url2.host());
307 EXPECT_FALSE(
308 content::ZoomValuesEqual(default_zoom_level, GetZoomLevel(test_url2)));
310 ui_zoom::PageZoom::Zoom(web_contents, content::PAGE_ZOOM_IN);
311 observer.BlockUntilZoomLevelForHostHasChanged(test_url2.host());
312 EXPECT_TRUE(
313 content::ZoomValuesEqual(default_zoom_level, GetZoomLevel(test_url2)));
315 // Now both tabs should be at the default zoom level, so there should not be
316 // any per-host values saved either to Pref, or internally in HostZoomMap.
317 EXPECT_TRUE(GetHostsWithZoomLevels().empty());
318 EXPECT_TRUE(GetHostsWithZoomLevelsFromPrefs().empty());
321 // Test that garbage data from crbug.com/364399 is cleared up on startup.
322 IN_PROC_BROWSER_TEST_F(HostZoomMapSanitizationBrowserTest, ClearOnStartup) {
323 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2"));
324 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2"));
327 // In this case we migrate the zoom level data from the profile prefs.
328 const char kMigrationTestPrefs[] =
329 "{'profile': {"
330 " 'default_zoom_level': 1.2,"
331 " 'per_host_zoom_levels': {'': 1.1, 'host1': 1.20001, 'host2': "
332 "1.3}"
333 "}}";
335 class HostZoomMapMigrationBrowserTest : public HostZoomMapBrowserTestWithPrefs {
336 public:
337 HostZoomMapMigrationBrowserTest()
338 : HostZoomMapBrowserTestWithPrefs(kMigrationTestPrefs) {}
340 static const double kOriginalDefaultZoomLevel;
342 private:
343 DISALLOW_COPY_AND_ASSIGN(HostZoomMapMigrationBrowserTest);
346 const double HostZoomMapMigrationBrowserTest::kOriginalDefaultZoomLevel = 1.2;
348 // This test is the same as HostZoomMapSanitizationBrowserTest, except that the
349 // zoom level data is loaded from the profile prefs, transfered to the
350 // zoom-level prefs, and we verify that the profile zoom level prefs are
351 // erased in the process. We also test that changes to the host zoom map and the
352 // default zoom level don't propagate back to the profile prefs.
353 IN_PROC_BROWSER_TEST_F(HostZoomMapMigrationBrowserTest,
354 MigrateProfileZoomPreferences) {
355 EXPECT_THAT(GetHostsWithZoomLevels(), testing::ElementsAre("host2"));
356 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(), testing::ElementsAre("host2"));
358 PrefService* profile_prefs =
359 browser()->profile()->GetPrefs();
360 chrome::ChromeZoomLevelPrefs* zoom_level_prefs =
361 browser()->profile()->GetZoomLevelPrefs();
362 // Make sure that the profile pref for default zoom level has been set to
363 // its default value of 0.0.
364 EXPECT_EQ(0.0, profile_prefs->GetDouble(prefs::kDefaultZoomLevelDeprecated));
365 EXPECT_EQ(kOriginalDefaultZoomLevel,
366 zoom_level_prefs->GetDefaultZoomLevelPref());
368 // Make sure that the profile prefs for per-host zoom levels are erased.
370 const base::DictionaryValue* profile_host_zoom_dictionary =
371 profile_prefs->GetDictionary(prefs::kPerHostZoomLevelsDeprecated);
372 EXPECT_EQ(0UL, profile_host_zoom_dictionary->size());
375 ZoomLevelChangeObserver observer(browser()->profile());
376 content::HostZoomMap* host_zoom_map = static_cast<content::HostZoomMap*>(
377 content::HostZoomMap::GetDefaultForBrowserContext(
378 browser()->profile()));
380 // Make sure that a change to a host zoom level doesn't propagate to the
381 // profile prefs.
382 std::string host3("host3");
383 host_zoom_map->SetZoomLevelForHost(host3, 1.3);
384 observer.BlockUntilZoomLevelForHostHasChanged(host3);
385 EXPECT_THAT(GetHostsWithZoomLevelsFromPrefs(),
386 testing::ElementsAre("host2", host3));
388 const base::DictionaryValue* profile_host_zoom_dictionary =
389 profile_prefs->GetDictionary(prefs::kPerHostZoomLevelsDeprecated);
390 EXPECT_EQ(0UL, profile_host_zoom_dictionary->size());
393 // Make sure a change to the default zoom level doesn't propagate to the
394 // profile prefs.
396 // First, we need a host at the default zoom level to respond when the
397 // default zoom level changes.
398 const double kNewDefaultZoomLevel = 1.5;
399 GURL test_url = ConstructTestServerURL("http://host4:%u/");
400 ui_test_utils::NavigateToURL(browser(), test_url);
401 EXPECT_TRUE(content::ZoomValuesEqual(kOriginalDefaultZoomLevel,
402 GetZoomLevel(test_url)));
404 // Change the default zoom level and observe.
405 SetDefaultZoomLevel(kNewDefaultZoomLevel);
406 observer.BlockUntilZoomLevelForHostHasChanged(test_url.host());
407 EXPECT_TRUE(
408 content::ZoomValuesEqual(kNewDefaultZoomLevel, GetZoomLevel(test_url)));
409 EXPECT_EQ(kNewDefaultZoomLevel, zoom_level_prefs->GetDefaultZoomLevelPref());
410 EXPECT_EQ(0.0, profile_prefs->GetDouble(prefs::kDefaultZoomLevelDeprecated));
413 // Test four things:
414 // 1. Host zoom maps of parent profile and child profile are different.
415 // 2. Child host zoom map inherits zoom level at construction.
416 // 3. Change of zoom level doesn't propagate from child to parent.
417 // 4. Change of zoom level propagates from parent to child.
418 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest,
419 OffTheRecordProfileHostZoomMap) {
420 // Constants for test case.
421 const std::string host("example.com");
422 const double zoom_level_25 = 2.5;
423 const double zoom_level_30 = 3.0;
424 const double zoom_level_40 = 4.0;
426 Profile* parent_profile = browser()->profile();
427 Profile* child_profile =
428 static_cast<ProfileImpl*>(parent_profile)->GetOffTheRecordProfile();
429 HostZoomMap* parent_zoom_map =
430 HostZoomMap::GetDefaultForBrowserContext(parent_profile);
431 ASSERT_TRUE(parent_zoom_map);
433 parent_zoom_map->SetZoomLevelForHost(host, zoom_level_25);
434 ASSERT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
435 zoom_level_25);
437 // Prepare child host zoom map.
438 HostZoomMap* child_zoom_map =
439 HostZoomMap::GetDefaultForBrowserContext(child_profile);
440 ASSERT_TRUE(child_zoom_map);
442 // Verify.
443 EXPECT_NE(parent_zoom_map, child_zoom_map);
445 EXPECT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
446 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
447 "Child must inherit from parent.";
449 child_zoom_map->SetZoomLevelForHost(host, zoom_level_30);
450 ASSERT_EQ(
451 child_zoom_map->GetZoomLevelForHostAndScheme("http", host),
452 zoom_level_30);
454 EXPECT_NE(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
455 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
456 "Child change must not propagate to parent.";
458 parent_zoom_map->SetZoomLevelForHost(host, zoom_level_40);
459 ASSERT_EQ(
460 parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
461 zoom_level_40);
463 EXPECT_EQ(parent_zoom_map->GetZoomLevelForHostAndScheme("http", host),
464 child_zoom_map->GetZoomLevelForHostAndScheme("http", host)) <<
465 "Parent change should propagate to child.";
466 base::RunLoop().RunUntilIdle();
469 IN_PROC_BROWSER_TEST_F(HostZoomMapBrowserTest,
470 ParentDefaultZoomPropagatesToIncognitoChild) {
471 Profile* parent_profile = browser()->profile();
472 Profile* child_profile =
473 static_cast<ProfileImpl*>(parent_profile)->GetOffTheRecordProfile();
475 double new_default_zoom_level =
476 parent_profile->GetZoomLevelPrefs()->GetDefaultZoomLevelPref() + 1.f;
477 HostZoomMap* parent_host_zoom_map =
478 HostZoomMap::GetDefaultForBrowserContext(parent_profile);
479 HostZoomMap* child_host_zoom_map =
480 HostZoomMap::GetDefaultForBrowserContext(child_profile);
481 ASSERT_TRUE(parent_host_zoom_map);
482 ASSERT_TRUE(child_host_zoom_map);
483 EXPECT_NE(parent_host_zoom_map, child_host_zoom_map);
484 EXPECT_NE(new_default_zoom_level, child_host_zoom_map->GetDefaultZoomLevel());
486 parent_profile->GetZoomLevelPrefs()->SetDefaultZoomLevelPref(
487 new_default_zoom_level);
488 EXPECT_EQ(new_default_zoom_level, child_host_zoom_map->GetDefaultZoomLevel());