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.
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/profiles/profile.h"
9 #include "chrome/browser/ui/browser.h"
10 #include "chrome/browser/ui/browser_commands.h"
11 #include "chrome/browser/ui/browser_window.h"
12 #include "chrome/browser/ui/gtk/browser_toolbar_gtk.h"
13 #include "chrome/browser/ui/gtk/browser_window_gtk.h"
14 #include "chrome/browser/ui/gtk/location_bar_view_gtk.h"
15 #include "chrome/browser/ui/gtk/view_id_util.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/browser/ui/zoom/zoom_controller.h"
18 #include "chrome/test/base/in_process_browser_test.h"
19 #include "chrome/test/base/ui_test_utils.h"
20 #include "content/public/browser/host_zoom_map.h"
21 #include "content/public/browser/notification_service.h"
22 #include "content/public/browser/notification_types.h"
23 #include "content/public/browser/web_contents.h"
24 #include "content/public/common/page_zoom.h"
25 #include "grit/theme_resources.h"
26 #include "testing/gtest/include/gtest/gtest.h"
28 // TODO(dbeam): share some testing code with ZoomBubbleGtkTest.
32 int GetZoomPercent(content::WebContents
* contents
) {
34 return contents
->GetZoomPercent(&dummy
, &dummy
);
37 void ExpectZoomedIn(content::WebContents
* contents
) {
38 EXPECT_GT(GetZoomPercent(contents
), 100);
41 void ExpectZoomedOut(content::WebContents
* contents
) {
42 EXPECT_LT(GetZoomPercent(contents
), 100);
45 void ExpectAtDefaultZoom(content::WebContents
* contents
) {
46 EXPECT_EQ(GetZoomPercent(contents
), 100);
49 void OnZoomLevelChanged(const base::Closure
& callback
,
50 const content::HostZoomMap::ZoomLevelChange
& host
) {
56 class LocationBarViewGtkZoomTest
: public InProcessBrowserTest
{
58 LocationBarViewGtkZoomTest() {}
59 virtual ~LocationBarViewGtkZoomTest() {}
62 void ExpectTooltipContainsZoom() {
63 gchar
* text
= gtk_widget_get_tooltip_text(GetZoomWidget());
64 std::string
tooltip(text
);
66 content::WebContents
* contents
=
67 browser()->tab_strip_model()->GetActiveWebContents();
68 std::string zoom_percent
= base::IntToString(GetZoomPercent(contents
));
69 EXPECT_FALSE(tooltip
.find(zoom_percent
) == std::string::npos
);
72 bool ZoomIconIsShowing() {
73 return gtk_widget_get_visible(GetZoomWidget());
76 void ExpectIconIsResource(int resource_id
) {
77 // TODO(dbeam): actually compare the image bits with gfx::test::IsEqual?
78 content::WebContents
* contents
=
79 browser()->tab_strip_model()->GetActiveWebContents();
80 ZoomController
* zoom_controller
= ZoomController::FromWebContents(contents
);
81 EXPECT_EQ(resource_id
, zoom_controller
->GetResourceForZoomLevel());
85 WaitForZoom(content::PAGE_ZOOM_RESET
);
88 content::WebContents
* SetUpTest() {
89 content::WebContents
* contents
=
90 browser()->tab_strip_model()->GetActiveWebContents();
92 ExpectAtDefaultZoom(contents
);
97 WaitForZoom(content::PAGE_ZOOM_IN
);
101 WaitForZoom(content::PAGE_ZOOM_OUT
);
105 GtkWidget
* GetZoomWidget() {
106 gfx::NativeWindow window
= browser()->window()->GetNativeWindow();
107 return ViewIDUtil::GetWidget(GTK_WIDGET(window
), VIEW_ID_ZOOM_BUTTON
);
110 void WaitForZoom(content::PageZoom zoom_action
) {
111 scoped_refptr
<content::MessageLoopRunner
> loop_runner(
112 new content::MessageLoopRunner
);
113 content::HostZoomMap::ZoomLevelChangedCallback
callback(
114 base::Bind(&OnZoomLevelChanged
, loop_runner
->QuitClosure()));
115 scoped_ptr
<content::HostZoomMap::Subscription
> sub
=
116 content::HostZoomMap::GetForBrowserContext(
117 browser()->profile())->AddZoomLevelChangedCallback(callback
);
118 chrome::Zoom(browser(), zoom_action
);
122 DISALLOW_COPY_AND_ASSIGN(LocationBarViewGtkZoomTest
);
125 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest
, DefaultToZoomedInAndBack
) {
126 content::WebContents
* contents
= SetUpTest();
129 ExpectZoomedIn(contents
);
130 EXPECT_TRUE(ZoomIconIsShowing());
131 ExpectIconIsResource(IDR_ZOOM_PLUS
);
132 ExpectTooltipContainsZoom();
134 ZoomOut(); // Back to default, in theory.
135 ExpectAtDefaultZoom(contents
);
136 EXPECT_FALSE(ZoomIconIsShowing());
139 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest
, ZoomInTwiceAndReset
) {
140 content::WebContents
* contents
= SetUpTest();
143 int zoom_level
= GetZoomPercent(contents
);
145 DCHECK_GT(GetZoomPercent(contents
), zoom_level
);
147 ExpectZoomedIn(contents
);
148 EXPECT_TRUE(ZoomIconIsShowing());
149 ExpectIconIsResource(IDR_ZOOM_PLUS
);
150 ExpectTooltipContainsZoom();
153 ExpectAtDefaultZoom(contents
);
154 EXPECT_FALSE(ZoomIconIsShowing());
157 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest
, DefaultToZoomedOutAndBack
) {
158 content::WebContents
* contents
= SetUpTest();
161 ExpectZoomedOut(contents
);
162 EXPECT_TRUE(ZoomIconIsShowing());
163 ExpectIconIsResource(IDR_ZOOM_MINUS
);
164 ExpectTooltipContainsZoom();
167 ExpectAtDefaultZoom(contents
);
168 EXPECT_FALSE(ZoomIconIsShowing());
171 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest
, ZoomOutTwiceAndReset
) {
172 content::WebContents
* contents
= SetUpTest();
175 int zoom_level
= GetZoomPercent(contents
);
177 DCHECK_LT(GetZoomPercent(contents
), zoom_level
);
178 ExpectZoomedOut(contents
);
179 EXPECT_TRUE(ZoomIconIsShowing());
180 ExpectIconIsResource(IDR_ZOOM_MINUS
);
181 ExpectTooltipContainsZoom();
184 ExpectAtDefaultZoom(contents
);
185 EXPECT_FALSE(ZoomIconIsShowing());