1 // Copyright (c) 2012 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 "base/message_loop/message_loop.h"
6 #include "base/prefs/pref_service.h"
7 #include "chrome/browser/ui/browser.h"
8 #include "chrome/browser/ui/browser_finder.h"
9 #include "chrome/test/base/chrome_render_view_host_test_harness.h"
10 #include "chrome/test/base/testing_profile.h"
11 #include "components/ui/zoom/zoom_controller.h"
12 #include "components/ui/zoom/zoom_observer.h"
13 #include "content/public/browser/host_zoom_map.h"
14 #include "content/public/browser/navigation_details.h"
15 #include "content/public/common/frame_navigate_params.h"
16 #include "content/public/test/test_renderer_host.h"
17 #include "content/public/test/test_utils.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
21 using ui_zoom::ZoomController
;
23 bool operator==(const ZoomController::ZoomChangedEventData
& lhs
,
24 const ZoomController::ZoomChangedEventData
& rhs
) {
25 return lhs
.web_contents
== rhs
.web_contents
&&
26 lhs
.old_zoom_level
== rhs
.old_zoom_level
&&
27 lhs
.new_zoom_level
== rhs
.new_zoom_level
&&
28 lhs
.zoom_mode
== rhs
.zoom_mode
&&
29 lhs
.can_show_bubble
== rhs
.can_show_bubble
;
32 class ZoomChangedWatcher
: public ui_zoom::ZoomObserver
{
35 ZoomController
* zoom_controller
,
36 const ZoomController::ZoomChangedEventData
& expected_event_data
)
37 : zoom_controller_(zoom_controller
),
38 expected_event_data_(expected_event_data
),
39 message_loop_runner_(new content::MessageLoopRunner
) {
40 zoom_controller_
->AddObserver(this);
42 ~ZoomChangedWatcher() override
{ zoom_controller_
->RemoveObserver(this); }
44 void Wait() { message_loop_runner_
->Run(); }
47 const ZoomController::ZoomChangedEventData
& event_data
) override
{
48 if (event_data
== expected_event_data_
)
49 message_loop_runner_
->Quit();
53 ZoomController
* zoom_controller_
;
54 ZoomController::ZoomChangedEventData expected_event_data_
;
55 scoped_refptr
<content::MessageLoopRunner
> message_loop_runner_
;
57 DISALLOW_COPY_AND_ASSIGN(ZoomChangedWatcher
);
60 class ZoomControllerTest
: public ChromeRenderViewHostTestHarness
{
62 void SetUp() override
{
63 ChromeRenderViewHostTestHarness::SetUp();
64 zoom_controller_
.reset(new ZoomController(web_contents()));
66 // This call is needed so that the RenderViewHost reports being alive. This
67 // is only important for tests that call ZoomController::SetZoomLevel().
68 content::RenderViewHostTester::For(rvh())->CreateRenderView(
69 base::string16(), MSG_ROUTING_NONE
, MSG_ROUTING_NONE
, -1, false);
72 void TearDown() override
{
73 zoom_controller_
.reset();
74 ChromeRenderViewHostTestHarness::TearDown();
78 scoped_ptr
<ZoomController
> zoom_controller_
;
81 TEST_F(ZoomControllerTest
, DidNavigateMainFrame
) {
82 double zoom_level
= zoom_controller_
->GetZoomLevel();
83 ZoomController::ZoomChangedEventData
zoom_change_data(
87 ZoomController::ZOOM_MODE_DEFAULT
,
89 ZoomChangedWatcher
zoom_change_watcher(zoom_controller_
.get(),
91 zoom_controller_
->DidNavigateMainFrame(content::LoadCommittedDetails(),
92 content::FrameNavigateParams());
93 zoom_change_watcher
.Wait();
96 TEST_F(ZoomControllerTest
, Observe_ZoomController
) {
97 double old_zoom_level
= zoom_controller_
->GetZoomLevel();
98 double new_zoom_level
= 110.0;
100 NavigateAndCommit(GURL("about:blank"));
102 ZoomController::ZoomChangedEventData
zoom_change_data1(
106 ZoomController::ZOOM_MODE_ISOLATED
,
107 true /* can_show_bubble */);
110 ZoomChangedWatcher
zoom_change_watcher1(zoom_controller_
.get(),
112 zoom_controller_
->SetZoomMode(ZoomController::ZOOM_MODE_ISOLATED
);
113 zoom_change_watcher1
.Wait();
116 ZoomController::ZoomChangedEventData
zoom_change_data2(
120 ZoomController::ZOOM_MODE_ISOLATED
,
121 true /* can_show_bubble */);
124 ZoomChangedWatcher
zoom_change_watcher2(zoom_controller_
.get(),
126 zoom_controller_
->SetZoomLevel(new_zoom_level
);
127 zoom_change_watcher2
.Wait();
131 TEST_F(ZoomControllerTest
, ObserveManualZoomCanShowBubble
) {
132 NavigateAndCommit(GURL("about:blank"));
133 double old_zoom_level
= zoom_controller_
->GetZoomLevel();
134 double new_zoom_level1
= old_zoom_level
+ 0.5;
135 double new_zoom_level2
= old_zoom_level
+ 1.0;
137 zoom_controller_
->SetZoomMode(ui_zoom::ZoomController::ZOOM_MODE_MANUAL
);
138 // By default, the zoom controller will send 'true' for can_show_bubble.
139 ZoomController::ZoomChangedEventData
zoom_change_data1(
143 ZoomController::ZOOM_MODE_MANUAL
,
144 true /* can_show_bubble */);
146 ZoomChangedWatcher
zoom_change_watcher1(zoom_controller_
.get(),
148 zoom_controller_
->SetZoomLevel(new_zoom_level1
);
149 zoom_change_watcher1
.Wait();
152 // Override default and verify the subsequent event reflects this change.
153 zoom_controller_
->SetShowsNotificationBubble(false);
154 ZoomController::ZoomChangedEventData
zoom_change_data2(
158 ZoomController::ZOOM_MODE_MANUAL
,
159 false /* can_show_bubble */);
161 ZoomChangedWatcher
zoom_change_watcher2(zoom_controller_
.get(),
163 zoom_controller_
->SetZoomLevel(new_zoom_level2
);
164 zoom_change_watcher2
.Wait();