| 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/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" | |
| 27 | |
| 28 // TODO(dbeam): share some testing code with ZoomBubbleGtkTest. | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 int GetZoomPercent(content::WebContents* contents) { | |
| 33 bool dummy; | |
| 34 return contents->GetZoomPercent(&dummy, &dummy); | |
| 35 } | |
| 36 | |
| 37 void ExpectZoomedIn(content::WebContents* contents) { | |
| 38 EXPECT_GT(GetZoomPercent(contents), 100); | |
| 39 } | |
| 40 | |
| 41 void ExpectZoomedOut(content::WebContents* contents) { | |
| 42 EXPECT_LT(GetZoomPercent(contents), 100); | |
| 43 } | |
| 44 | |
| 45 void ExpectAtDefaultZoom(content::WebContents* contents) { | |
| 46 EXPECT_EQ(GetZoomPercent(contents), 100); | |
| 47 } | |
| 48 | |
| 49 void OnZoomLevelChanged(const base::Closure& callback, | |
| 50 const content::HostZoomMap::ZoomLevelChange& host) { | |
| 51 callback.Run(); | |
| 52 } | |
| 53 | |
| 54 } // namespace | |
| 55 | |
| 56 class LocationBarViewGtkZoomTest : public InProcessBrowserTest { | |
| 57 public: | |
| 58 LocationBarViewGtkZoomTest() {} | |
| 59 virtual ~LocationBarViewGtkZoomTest() {} | |
| 60 | |
| 61 protected: | |
| 62 void ExpectTooltipContainsZoom() { | |
| 63 gchar* text = gtk_widget_get_tooltip_text(GetZoomWidget()); | |
| 64 std::string tooltip(text); | |
| 65 g_free(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); | |
| 70 } | |
| 71 | |
| 72 bool ZoomIconIsShowing() { | |
| 73 return gtk_widget_get_visible(GetZoomWidget()); | |
| 74 } | |
| 75 | |
| 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()); | |
| 82 } | |
| 83 | |
| 84 void ResetZoom() { | |
| 85 WaitForZoom(content::PAGE_ZOOM_RESET); | |
| 86 } | |
| 87 | |
| 88 content::WebContents* SetUpTest() { | |
| 89 content::WebContents* contents = | |
| 90 browser()->tab_strip_model()->GetActiveWebContents(); | |
| 91 ResetZoom(); | |
| 92 ExpectAtDefaultZoom(contents); | |
| 93 return contents; | |
| 94 } | |
| 95 | |
| 96 void ZoomIn() { | |
| 97 WaitForZoom(content::PAGE_ZOOM_IN); | |
| 98 } | |
| 99 | |
| 100 void ZoomOut() { | |
| 101 WaitForZoom(content::PAGE_ZOOM_OUT); | |
| 102 } | |
| 103 | |
| 104 private: | |
| 105 GtkWidget* GetZoomWidget() { | |
| 106 gfx::NativeWindow window = browser()->window()->GetNativeWindow(); | |
| 107 return ViewIDUtil::GetWidget(GTK_WIDGET(window), VIEW_ID_ZOOM_BUTTON); | |
| 108 } | |
| 109 | |
| 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); | |
| 119 loop_runner->Run(); | |
| 120 } | |
| 121 | |
| 122 DISALLOW_COPY_AND_ASSIGN(LocationBarViewGtkZoomTest); | |
| 123 }; | |
| 124 | |
| 125 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest, DefaultToZoomedInAndBack) { | |
| 126 content::WebContents* contents = SetUpTest(); | |
| 127 | |
| 128 ZoomIn(); | |
| 129 ExpectZoomedIn(contents); | |
| 130 EXPECT_TRUE(ZoomIconIsShowing()); | |
| 131 ExpectIconIsResource(IDR_ZOOM_PLUS); | |
| 132 ExpectTooltipContainsZoom(); | |
| 133 | |
| 134 ZoomOut(); // Back to default, in theory. | |
| 135 ExpectAtDefaultZoom(contents); | |
| 136 EXPECT_FALSE(ZoomIconIsShowing()); | |
| 137 } | |
| 138 | |
| 139 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest, ZoomInTwiceAndReset) { | |
| 140 content::WebContents* contents = SetUpTest(); | |
| 141 | |
| 142 ZoomIn(); | |
| 143 int zoom_level = GetZoomPercent(contents); | |
| 144 ZoomIn(); | |
| 145 DCHECK_GT(GetZoomPercent(contents), zoom_level); | |
| 146 | |
| 147 ExpectZoomedIn(contents); | |
| 148 EXPECT_TRUE(ZoomIconIsShowing()); | |
| 149 ExpectIconIsResource(IDR_ZOOM_PLUS); | |
| 150 ExpectTooltipContainsZoom(); | |
| 151 | |
| 152 ResetZoom(); | |
| 153 ExpectAtDefaultZoom(contents); | |
| 154 EXPECT_FALSE(ZoomIconIsShowing()); | |
| 155 } | |
| 156 | |
| 157 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest, DefaultToZoomedOutAndBack) { | |
| 158 content::WebContents* contents = SetUpTest(); | |
| 159 | |
| 160 ZoomOut(); | |
| 161 ExpectZoomedOut(contents); | |
| 162 EXPECT_TRUE(ZoomIconIsShowing()); | |
| 163 ExpectIconIsResource(IDR_ZOOM_MINUS); | |
| 164 ExpectTooltipContainsZoom(); | |
| 165 | |
| 166 ZoomIn(); | |
| 167 ExpectAtDefaultZoom(contents); | |
| 168 EXPECT_FALSE(ZoomIconIsShowing()); | |
| 169 } | |
| 170 | |
| 171 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest, ZoomOutTwiceAndReset) { | |
| 172 content::WebContents* contents = SetUpTest(); | |
| 173 | |
| 174 ZoomOut(); | |
| 175 int zoom_level = GetZoomPercent(contents); | |
| 176 ZoomOut(); | |
| 177 DCHECK_LT(GetZoomPercent(contents), zoom_level); | |
| 178 ExpectZoomedOut(contents); | |
| 179 EXPECT_TRUE(ZoomIconIsShowing()); | |
| 180 ExpectIconIsResource(IDR_ZOOM_MINUS); | |
| 181 ExpectTooltipContainsZoom(); | |
| 182 | |
| 183 ResetZoom(); | |
| 184 ExpectAtDefaultZoom(contents); | |
| 185 EXPECT_FALSE(ZoomIconIsShowing()); | |
| 186 } | |
| OLD | NEW |