OLD | NEW |
---|---|
(Empty) | |
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. | |
4 | |
5 #include <gtk/gtk.h> | |
6 | |
7 #include "base/string_number_conversions.h" | |
8 #include "chrome/browser/ui/browser.h" | |
9 #include "chrome/browser/ui/browser_commands.h" | |
10 #include "chrome/browser/ui/browser_tabstrip.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/gtk/zoom_bubble_gtk.h" | |
17 #include "chrome/test/base/in_process_browser_test.h" | |
18 #include "chrome/test/base/ui_test_utils.h" | |
19 #include "content/public/browser/notification_service.h" | |
20 #include "content/public/browser/notification_types.h" | |
21 #include "content/public/browser/web_contents.h" | |
22 #include "content/public/common/page_zoom.h" | |
23 #include "testing/gtest/include/gtest/gtest.h" | |
24 | |
25 // TODO(dbeam): share some testing code with LocationBarViewGtkZoomTest. | |
26 | |
27 namespace { | |
28 | |
29 int GetZoomPercent(content::WebContents* contents) { | |
30 bool dummy; | |
31 return contents->GetZoomPercent(&dummy, &dummy); | |
32 } | |
33 | |
34 void AssertZoomedIn(content::WebContents* contents) { | |
35 ASSERT_GT(GetZoomPercent(contents), 100); | |
36 } | |
37 | |
38 void AssertZoomedOut(content::WebContents* contents) { | |
39 ASSERT_LT(GetZoomPercent(contents), 100); | |
40 } | |
41 | |
42 void AssertAtDefaultZoom(content::WebContents* contents) { | |
43 ASSERT_EQ(GetZoomPercent(contents), 100); | |
44 } | |
45 | |
46 } | |
47 | |
48 class ZoomBubbleGtkTest : public InProcessBrowserTest { | |
49 public: | |
50 ZoomBubbleGtkTest() { | |
51 } | |
52 | |
53 virtual ~ZoomBubbleGtkTest() { | |
54 } | |
55 | |
56 protected: | |
57 ZoomBubbleGtk* GetZoomBubble() { | |
58 return ZoomBubbleGtk::g_bubble; | |
59 } | |
60 | |
61 bool ZoomBubbleIsShowing() { | |
62 return ZoomBubbleGtk::IsShowing(); | |
63 } | |
64 | |
65 void ExpectLabelTextContains(int percent) { | |
66 std::string label(gtk_label_get_text(GTK_LABEL(GetZoomBubble()->label_))); | |
67 EXPECT_FALSE(label.find(base::IntToString(percent)) == std::string::npos); | |
68 } | |
69 | |
70 void ResetZoom() { | |
71 WaitForZoom(content::PAGE_ZOOM_RESET); | |
72 } | |
73 | |
74 content::WebContents* SetUpTest() { | |
75 content::WebContents* contents = chrome::GetActiveWebContents(browser()); | |
76 ResetZoom(); | |
77 AssertAtDefaultZoom(contents); | |
78 return contents; | |
79 } | |
80 | |
81 void ZoomIn() { | |
82 WaitForZoom(content::PAGE_ZOOM_IN); | |
83 MakeTestLessFlakey(); | |
84 } | |
85 | |
86 void ZoomOut() { | |
87 WaitForZoom(content::PAGE_ZOOM_OUT); | |
88 MakeTestLessFlakey(); | |
89 } | |
90 | |
91 private: | |
92 void MakeTestLessFlakey() { | |
93 // Stopping the close timer makes this test less able to accidentally fail | |
94 // if a bot hangs or IPC takes forever. | |
95 if (GetZoomBubble()) | |
96 GetZoomBubble()->StopTimerIfNecessary(); | |
97 } | |
98 | |
99 void WaitForZoom(content::PageZoom zoom_action) { | |
100 content::WindowedNotificationObserver zoom_observer( | |
101 content::NOTIFICATION_ZOOM_LEVEL_CHANGED, | |
102 content::NotificationService::AllSources()); | |
103 chrome::Zoom(browser(), zoom_action); | |
104 zoom_observer.Wait(); | |
105 } | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(ZoomBubbleGtkTest); | |
108 }; | |
109 | |
110 IN_PROC_BROWSER_TEST_F(ZoomBubbleGtkTest, BubbleSanityTest) { | |
111 ResetZoom(); | |
112 | |
113 // The bubble assumes it shows only at non-default zoom levels. | |
114 ZoomIn(); | |
115 | |
116 ZoomBubbleGtk::Close(); | |
117 DCHECK(!GetZoomBubble()); | |
118 EXPECT_FALSE(ZoomBubbleIsShowing()); | |
119 | |
120 GtkWidget* window = GTK_WIDGET(browser()->window()->GetNativeWindow()); | |
121 GtkWidget* zoom_icon = ViewIDUtil::GetWidget(window, VIEW_ID_ZOOM_BUTTON); | |
122 DCHECK(zoom_icon); | |
123 | |
124 TabContents* tab_contents = chrome::GetActiveTabContents(browser()); | |
125 DCHECK(tab_contents); | |
126 | |
127 // Force show a bubble. | |
128 ZoomBubbleGtk::Show(zoom_icon, tab_contents, true); | |
129 DCHECK(GetZoomBubble()); | |
130 EXPECT_TRUE(ZoomBubbleIsShowing()); | |
131 } | |
132 | |
133 IN_PROC_BROWSER_TEST_F(ZoomBubbleGtkTest, DefaultToZoomedInAndBack) { | |
134 content::WebContents* contents = SetUpTest(); | |
135 | |
136 ZoomIn(); | |
137 AssertZoomedIn(contents); | |
138 EXPECT_TRUE(ZoomBubbleIsShowing()); | |
139 ExpectLabelTextContains(110); | |
140 | |
141 ZoomOut(); | |
142 AssertAtDefaultZoom(contents); | |
143 EXPECT_FALSE(ZoomBubbleIsShowing()); | |
144 } | |
145 | |
146 IN_PROC_BROWSER_TEST_F(ZoomBubbleGtkTest, ZoomInTwiceAndReset) { | |
147 content::WebContents* contents = SetUpTest(); | |
148 | |
149 ZoomIn(); | |
150 ZoomIn(); | |
151 AssertZoomedIn(contents); | |
152 EXPECT_TRUE(ZoomBubbleIsShowing()); | |
153 ExpectLabelTextContains(125); | |
154 | |
155 ResetZoom(); | |
156 AssertAtDefaultZoom(contents); | |
157 EXPECT_FALSE(ZoomBubbleIsShowing()); | |
158 } | |
159 | |
160 IN_PROC_BROWSER_TEST_F(ZoomBubbleGtkTest, DefaultToZoomedOutAndBack) { | |
161 content::WebContents* contents = SetUpTest(); | |
162 | |
163 ZoomOut(); | |
164 AssertZoomedOut(contents); | |
165 EXPECT_TRUE(ZoomBubbleIsShowing()); | |
166 ExpectLabelTextContains(90); | |
Evan Stade
2012/10/02 18:35:59
I suggested you get rid of these hard-coded values
Dan Beam
2012/10/02 19:34:14
Ah, OK. Done.
| |
167 | |
168 ZoomIn(); | |
169 AssertAtDefaultZoom(contents); | |
170 EXPECT_FALSE(ZoomBubbleIsShowing()); | |
171 } | |
172 | |
173 IN_PROC_BROWSER_TEST_F(ZoomBubbleGtkTest, ZoomOutTwiceAndReset) { | |
174 content::WebContents* contents = SetUpTest(); | |
175 | |
176 ZoomOut(); | |
177 ZoomOut(); | |
178 AssertZoomedOut(contents); | |
179 EXPECT_TRUE(ZoomBubbleIsShowing()); | |
180 ExpectLabelTextContains(75); | |
181 | |
182 ResetZoom(); | |
183 AssertAtDefaultZoom(contents); | |
184 EXPECT_FALSE(ZoomBubbleIsShowing()); | |
185 } | |
OLD | NEW |