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 "components/zoom/zoom_controller.h" | 5 #include "components/zoom/zoom_controller.h" |
6 | 6 |
7 #include "base/macros.h" | 7 #include "base/macros.h" |
8 #include "base/process/kill.h" | 8 #include "base/process/kill.h" |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/browser_commands.h" | 12 #include "chrome/browser/ui/browser_commands.h" |
13 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 13 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
14 #include "chrome/browser/ui/webui/signin/login_ui_test_utils.h" | 14 #include "chrome/browser/ui/webui/signin/login_ui_test_utils.h" |
15 #include "chrome/browser/ui/zoom/chrome_zoom_level_otr_delegate.h" | |
15 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" | 16 #include "chrome/browser/ui/zoom/chrome_zoom_level_prefs.h" |
16 #include "chrome/common/url_constants.h" | 17 #include "chrome/common/url_constants.h" |
17 #include "chrome/test/base/in_process_browser_test.h" | 18 #include "chrome/test/base/in_process_browser_test.h" |
18 #include "chrome/test/base/ui_test_utils.h" | 19 #include "chrome/test/base/ui_test_utils.h" |
19 #include "components/prefs/pref_service.h" | 20 #include "components/prefs/pref_service.h" |
20 #include "components/signin/core/common/profile_management_switches.h" | 21 #include "components/signin/core/common/profile_management_switches.h" |
21 #include "content/public/browser/host_zoom_map.h" | 22 #include "content/public/browser/host_zoom_map.h" |
22 #include "content/public/browser/navigation_entry.h" | 23 #include "content/public/browser/navigation_entry.h" |
23 #include "content/public/browser/render_process_host.h" | 24 #include "content/public/browser/render_process_host.h" |
24 #include "content/public/browser/render_view_host.h" | 25 #include "content/public/browser/render_view_host.h" |
26 #include "content/public/browser/storage_partition.h" | |
25 #include "content/public/browser/web_contents.h" | 27 #include "content/public/browser/web_contents.h" |
26 #include "content/public/common/page_type.h" | 28 #include "content/public/common/page_type.h" |
27 #include "content/public/test/browser_test_utils.h" | 29 #include "content/public/test/browser_test_utils.h" |
28 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
29 | 31 |
30 using zoom::ZoomController; | 32 using zoom::ZoomController; |
31 using zoom::ZoomObserver; | 33 using zoom::ZoomObserver; |
32 | 34 |
33 bool operator==(const ZoomController::ZoomChangedEventData& lhs, | 35 bool operator==(const ZoomController::ZoomChangedEventData& lhs, |
34 const ZoomController::ZoomChangedEventData& rhs) { | 36 const ZoomController::ZoomChangedEventData& rhs) { |
(...skipping 28 matching lines...) Expand all Loading... | |
63 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; | 65 scoped_refptr<content::MessageLoopRunner> message_loop_runner_; |
64 | 66 |
65 DISALLOW_COPY_AND_ASSIGN(ZoomChangedWatcher); | 67 DISALLOW_COPY_AND_ASSIGN(ZoomChangedWatcher); |
66 }; | 68 }; |
67 | 69 |
68 class ZoomControllerBrowserTest : public InProcessBrowserTest { | 70 class ZoomControllerBrowserTest : public InProcessBrowserTest { |
69 public: | 71 public: |
70 ZoomControllerBrowserTest() {} | 72 ZoomControllerBrowserTest() {} |
71 ~ZoomControllerBrowserTest() override {} | 73 ~ZoomControllerBrowserTest() override {} |
72 | 74 |
75 void SetUpOnMainThread() override { | |
76 // These tests assume we are per-origin by default. | |
77 content::WebContents* web_contents = | |
78 browser()->tab_strip_model()->GetActiveWebContents(); | |
79 ZoomController* zoom_controller = | |
80 ZoomController::FromWebContents(web_contents); | |
81 ASSERT_EQ(ZoomController::ZOOM_MODE_DEFAULT, zoom_controller->zoom_mode()); | |
82 } | |
83 | |
73 void TestResetOnNavigation(ZoomController::ZoomMode zoom_mode) { | 84 void TestResetOnNavigation(ZoomController::ZoomMode zoom_mode) { |
74 DCHECK(zoom_mode == ZoomController::ZOOM_MODE_ISOLATED || | 85 DCHECK(zoom_mode == ZoomController::ZOOM_MODE_ISOLATED || |
75 zoom_mode == ZoomController::ZOOM_MODE_MANUAL); | 86 zoom_mode == ZoomController::ZOOM_MODE_MANUAL); |
76 content::WebContents* web_contents = | 87 content::WebContents* web_contents = |
77 browser()->tab_strip_model()->GetActiveWebContents(); | 88 browser()->tab_strip_model()->GetActiveWebContents(); |
78 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( | 89 ui_test_utils::NavigateToURLBlockUntilNavigationsComplete( |
79 browser(), GURL("about:blank"), 1); | 90 browser(), GURL("about:blank"), 1); |
80 ZoomController* zoom_controller = | 91 ZoomController* zoom_controller = |
81 ZoomController::FromWebContents(web_contents); | 92 ZoomController::FromWebContents(web_contents); |
82 double zoom_level = zoom_controller->GetDefaultZoomLevel(); | 93 double zoom_level = zoom_controller->GetDefaultZoomLevel(); |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 web_contents, | 333 web_contents, |
323 old_zoom_level, | 334 old_zoom_level, |
324 new_zoom_level, | 335 new_zoom_level, |
325 ZoomController::ZOOM_MODE_DEFAULT, | 336 ZoomController::ZOOM_MODE_DEFAULT, |
326 true); // We have a non-empty host, so this will be 'true'. | 337 true); // We have a non-empty host, so this will be 'true'. |
327 ZoomChangedWatcher zoom_change_watcher(web_contents, zoom_change_data); | 338 ZoomChangedWatcher zoom_change_watcher(web_contents, zoom_change_data); |
328 zoom_controller->SetZoomLevel(new_zoom_level); | 339 zoom_controller->SetZoomLevel(new_zoom_level); |
329 zoom_change_watcher.Wait(); | 340 zoom_change_watcher.Wait(); |
330 } | 341 } |
331 #endif // !defined(OS_CHROMEOS) | 342 #endif // !defined(OS_CHROMEOS) |
343 | |
344 | |
345 class ZoomControllerPerTabBrowserTest : public ZoomControllerBrowserTest { | |
346 public: | |
347 void SetUpOnMainThread() override { | |
348 // These tests assume we are per-tab by default. | |
349 ChromeZoomLevelPrefs* zoom_prefs = | |
350 browser()->profile()->GetZoomLevelPrefs(); | |
Kevin McNee
2017/02/03 23:17:20
It looks like Profile::GetZoomLevelPrefs is to be
wjmaclean
2017/02/06 16:09:07
Are ZoomLevelPrefs going away? (I haven't heard of
Kevin McNee
2017/02/06 18:26:12
See https://cs.chromium.org/chromium/src/chrome/br
wjmaclean
2017/02/06 18:48:38
Assume it will stick around for now.
| |
351 ASSERT_TRUE(zoom_prefs); | |
352 zoom_prefs->SetZoomScopeIsPerOriginPref(false); | |
353 | |
354 content::WebContents* web_contents = | |
355 browser()->tab_strip_model()->GetActiveWebContents(); | |
356 ZoomController* zoom_controller = | |
357 ZoomController::FromWebContents(web_contents); | |
358 // Since this zoom controller was initialized before SetUpOnMainThread() | |
359 // was called, we need to verify that this zoom controller's mode was | |
360 // updated when we set the zoom scope preference. | |
361 ASSERT_EQ(ZoomController::ZOOM_MODE_ISOLATED, zoom_controller->zoom_mode()); | |
362 } | |
363 }; // ZoomControllerPerTabBrowserTest | |
364 | |
365 IN_PROC_BROWSER_TEST_F(ZoomControllerPerTabBrowserTest, IsolatedZoom) { | |
366 content::WebContents* web_contents1 = | |
367 browser()->tab_strip_model()->GetActiveWebContents(); | |
368 ZoomController* zoom_controller1 = | |
369 ZoomController::FromWebContents(web_contents1); | |
370 double old_zoom_level = zoom_controller1->GetZoomLevel(); | |
371 | |
372 double new_zoom_level1 = old_zoom_level + 0.5; | |
373 ZoomController::ZoomChangedEventData zoom_change_data1( | |
374 web_contents1, | |
375 old_zoom_level, | |
376 new_zoom_level1, | |
377 ZoomController::ZOOM_MODE_ISOLATED, | |
378 true); | |
379 ZoomChangedWatcher zoom_change_watcher1(web_contents1, zoom_change_data1); | |
380 zoom_controller1->SetZoomLevel(new_zoom_level1); | |
381 zoom_change_watcher1.Wait(); | |
382 | |
383 // The new tab should start with the default zoom, and not be affected by | |
384 // the zoom level set in the other tab. | |
385 ui_test_utils::NavigateToURLWithDisposition( | |
386 browser(), GURL("about:blank"), | |
387 WindowOpenDisposition::NEW_FOREGROUND_TAB, | |
388 ui_test_utils::BROWSER_TEST_WAIT_FOR_NAVIGATION); | |
389 content::WebContents* web_contents2 = | |
390 browser()->tab_strip_model()->GetActiveWebContents(); | |
391 ZoomController* zoom_controller2 = | |
392 ZoomController::FromWebContents(web_contents2); | |
393 EXPECT_TRUE(zoom_controller2->IsAtDefaultZoom()); | |
394 | |
395 double new_zoom_level2 = old_zoom_level + 1.0; | |
396 ZoomController::ZoomChangedEventData zoom_change_data2( | |
397 web_contents2, | |
398 old_zoom_level, | |
399 new_zoom_level2, | |
400 ZoomController::ZOOM_MODE_ISOLATED, | |
401 true); | |
402 ZoomChangedWatcher zoom_change_watcher2(web_contents2, zoom_change_data2); | |
403 zoom_controller2->SetZoomLevel(new_zoom_level2); | |
404 zoom_change_watcher2.Wait(); | |
405 | |
406 // Now when we zoom the second tab, the first should not be affected. | |
407 EXPECT_TRUE(content::ZoomValuesEqual(new_zoom_level1, | |
408 zoom_controller1->GetZoomLevel())); | |
409 } | |
410 | |
411 // Since we are zooming on a per-tab basis, the navigation of a tab does not | |
412 // reset the zoom level. | |
413 IN_PROC_BROWSER_TEST_F(ZoomControllerPerTabBrowserTest, | |
414 NavigationPreservesZoom) { | |
415 ui_test_utils::NavigateToURL(browser(), GURL("about:blank")); | |
416 content::WebContents* web_contents = | |
417 browser()->tab_strip_model()->GetActiveWebContents(); | |
418 ZoomController* zoom_controller = | |
419 ZoomController::FromWebContents(web_contents); | |
420 | |
421 double old_zoom_level = zoom_controller->GetZoomLevel(); | |
422 double new_zoom_level = old_zoom_level + 0.5; | |
423 ZoomController::ZoomChangedEventData zoom_change_data( | |
424 web_contents, | |
425 old_zoom_level, | |
426 new_zoom_level, | |
427 ZoomController::ZOOM_MODE_ISOLATED, | |
428 true); | |
429 ZoomChangedWatcher zoom_change_watcher(web_contents, zoom_change_data); | |
430 zoom_controller->SetZoomLevel(new_zoom_level); | |
431 zoom_change_watcher.Wait(); | |
432 | |
433 ui_test_utils::NavigateToURL(browser(), GURL(chrome::kChromeUISettingsURL)); | |
434 EXPECT_TRUE(content::ZoomValuesEqual(new_zoom_level, | |
435 zoom_controller->GetZoomLevel())); | |
436 } | |
437 | |
438 // Test that duplicated tabs start with the temporary zoom level of the | |
439 // original tab. | |
440 IN_PROC_BROWSER_TEST_F(ZoomControllerPerTabBrowserTest, | |
441 TabDuplicationPreservesZoom) { | |
442 TabStripModel* strip = browser()->tab_strip_model(); | |
443 content::WebContents* web_contents1 = strip->GetActiveWebContents(); | |
444 ZoomController* zoom_controller1 = | |
445 ZoomController::FromWebContents(web_contents1); | |
446 | |
447 double old_zoom_level = zoom_controller1->GetZoomLevel(); | |
448 double new_zoom_level = old_zoom_level + 0.5; | |
449 | |
450 ZoomController::ZoomChangedEventData zoom_change_data1( | |
451 web_contents1, | |
452 old_zoom_level, | |
453 new_zoom_level, | |
454 ZoomController::ZOOM_MODE_ISOLATED, | |
455 true); | |
456 ZoomChangedWatcher zoom_change_watcher1(web_contents1, zoom_change_data1); | |
457 zoom_controller1->SetZoomLevel(new_zoom_level); | |
458 zoom_change_watcher1.Wait(); | |
459 | |
460 content::WebContents* web_contents2 = chrome::DuplicateTabAt( | |
461 browser(), strip->GetIndexOfWebContents(web_contents1)); | |
462 ZoomController* zoom_controller2 = | |
463 ZoomController::FromWebContents(web_contents2); | |
464 | |
465 EXPECT_TRUE(content::ZoomValuesEqual(new_zoom_level, | |
466 zoom_controller2->GetZoomLevel())); | |
467 } | |
468 | |
469 // Test that swapping the WebContents for a tab (e.g. due to prerendering) | |
470 // preserves the temporary zoom level. | |
471 IN_PROC_BROWSER_TEST_F(ZoomControllerPerTabBrowserTest, | |
472 TabStripReplacementPreservesZoom) { | |
473 TabStripModel* strip = browser()->tab_strip_model(); | |
474 content::WebContents* web_contents1 = strip->GetActiveWebContents(); | |
475 ZoomController* zoom_controller1 = | |
476 ZoomController::FromWebContents(web_contents1); | |
477 | |
478 // Setup a new WebContents that will be swapped for the old one. | |
479 std::unique_ptr<content::WebContents> web_contents2(web_contents1->Clone()); | |
480 ZoomController::CreateForWebContents(web_contents2.get()); | |
481 | |
482 double old_zoom_level = zoom_controller1->GetZoomLevel(); | |
483 double new_zoom_level = old_zoom_level + 0.5; | |
484 | |
485 ZoomController::ZoomChangedEventData zoom_change_data1( | |
486 web_contents1, | |
487 old_zoom_level, | |
488 new_zoom_level, | |
489 ZoomController::ZOOM_MODE_ISOLATED, | |
490 true); | |
491 ZoomChangedWatcher zoom_change_watcher1(web_contents1, zoom_change_data1); | |
492 zoom_controller1->SetZoomLevel(new_zoom_level); | |
493 zoom_change_watcher1.Wait(); | |
494 | |
495 // Verify that the temporary zoom level is set on the new WebContents. | |
496 ZoomController::ZoomChangedEventData zoom_change_data2( | |
497 web_contents2.get(), | |
498 new_zoom_level, | |
499 new_zoom_level, | |
500 ZoomController::ZOOM_MODE_ISOLATED, | |
501 false); | |
502 ZoomChangedWatcher zoom_change_watcher2(web_contents2.get(), | |
503 zoom_change_data2); | |
504 delete strip->ReplaceWebContentsAt( | |
505 strip->GetIndexOfWebContents(web_contents1), web_contents2.get()); | |
506 zoom_change_watcher2.Wait(); | |
507 } | |
508 | |
509 // Test that an Incognito profile inherits its parent's default zoom scope and | |
510 // that changes to the default zoom scope in the parent propogate to the child. | |
511 IN_PROC_BROWSER_TEST_F(ZoomControllerPerTabBrowserTest, | |
512 OffTheRecordProfileDefaultZoomScope) { | |
513 Browser* incognito_browser = CreateIncognitoBrowser(); | |
514 content::WebContents* web_contents = | |
515 incognito_browser->tab_strip_model()->GetActiveWebContents(); | |
516 ZoomController* zoom_controller = | |
517 ZoomController::FromWebContents(web_contents); | |
518 content::BrowserContext* context = web_contents->GetBrowserContext(); | |
519 ASSERT_TRUE(context->IsOffTheRecord()); | |
520 | |
521 ChromeZoomLevelPrefs* parent_zoom_prefs = | |
522 browser()->profile()->GetZoomLevelPrefs(); | |
523 ChromeZoomLevelOTRDelegate* child_zoom_prefs = | |
524 static_cast<ChromeZoomLevelOTRDelegate*>( | |
525 content::BrowserContext::GetDefaultStoragePartition(context) | |
526 ->GetZoomLevelDelegate()); | |
527 ASSERT_TRUE(parent_zoom_prefs); | |
528 ASSERT_TRUE(child_zoom_prefs); | |
529 | |
530 EXPECT_FALSE(child_zoom_prefs->GetZoomScopeIsPerOriginPref()); | |
531 EXPECT_EQ(ZoomController::ZOOM_MODE_ISOLATED, zoom_controller->zoom_mode()); | |
532 | |
533 parent_zoom_prefs->SetZoomScopeIsPerOriginPref(true); | |
534 | |
535 EXPECT_TRUE(child_zoom_prefs->GetZoomScopeIsPerOriginPref()); | |
536 EXPECT_EQ(ZoomController::ZOOM_MODE_DEFAULT, zoom_controller->zoom_mode()); | |
537 } | |
OLD | NEW |