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/zoom/zoom_controller.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 "grit/theme_resources.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 |
| 26 // TODO(dbeam): share some testing code with ZoomBubbleGtkTest. |
| 27 |
| 28 namespace { |
| 29 |
| 30 int GetZoomPercent(content::WebContents* contents) { |
| 31 bool dummy; |
| 32 return contents->GetZoomPercent(&dummy, &dummy); |
| 33 } |
| 34 |
| 35 void ExpectZoomedIn(content::WebContents* contents) { |
| 36 EXPECT_GT(GetZoomPercent(contents), 100); |
| 37 } |
| 38 |
| 39 void ExpectZoomedOut(content::WebContents* contents) { |
| 40 EXPECT_LT(GetZoomPercent(contents), 100); |
| 41 } |
| 42 |
| 43 void ExpectAtDefaultZoom(content::WebContents* contents) { |
| 44 EXPECT_EQ(GetZoomPercent(contents), 100); |
| 45 } |
| 46 |
| 47 } |
| 48 |
| 49 class LocationBarViewGtkZoomTest : public InProcessBrowserTest { |
| 50 public: |
| 51 LocationBarViewGtkZoomTest() {} |
| 52 virtual ~LocationBarViewGtkZoomTest() {} |
| 53 |
| 54 protected: |
| 55 void ExpectTooltipContainsZoom() { |
| 56 gchar* text = gtk_widget_get_tooltip_text(GetZoomWidget()); |
| 57 std::string tooltip(text); |
| 58 g_free(text); |
| 59 content::WebContents* contents = chrome::GetActiveWebContents(browser()); |
| 60 std::string zoom_percent = base::IntToString(GetZoomPercent(contents)); |
| 61 EXPECT_FALSE(tooltip.find(zoom_percent) == std::string::npos); |
| 62 } |
| 63 |
| 64 bool ZoomIconIsShowing() { |
| 65 return gtk_widget_get_visible(GetZoomWidget()); |
| 66 } |
| 67 |
| 68 void ExpectIconIsResource(int resource_id) { |
| 69 // TODO(dbeam): actually compare the image bits with gfx::test::IsEqual? |
| 70 content::WebContents* contents = chrome::GetActiveWebContents(browser()); |
| 71 ZoomController* zoom_controller = ZoomController::FromWebContents(contents); |
| 72 EXPECT_EQ(resource_id, zoom_controller->GetResourceForZoomLevel()); |
| 73 } |
| 74 |
| 75 void ResetZoom() { |
| 76 WaitForZoom(content::PAGE_ZOOM_RESET); |
| 77 } |
| 78 |
| 79 content::WebContents* SetUpTest() { |
| 80 content::WebContents* contents = chrome::GetActiveWebContents(browser()); |
| 81 ResetZoom(); |
| 82 ExpectAtDefaultZoom(contents); |
| 83 return contents; |
| 84 } |
| 85 |
| 86 void ZoomIn() { |
| 87 WaitForZoom(content::PAGE_ZOOM_IN); |
| 88 } |
| 89 |
| 90 void ZoomOut() { |
| 91 WaitForZoom(content::PAGE_ZOOM_OUT); |
| 92 } |
| 93 |
| 94 private: |
| 95 GtkWidget* GetZoomWidget() { |
| 96 gfx::NativeWindow window = browser()->window()->GetNativeWindow(); |
| 97 return ViewIDUtil::GetWidget(GTK_WIDGET(window), VIEW_ID_ZOOM_BUTTON); |
| 98 } |
| 99 |
| 100 void WaitForZoom(content::PageZoom zoom_action) { |
| 101 content::WindowedNotificationObserver zoom_observer( |
| 102 content::NOTIFICATION_ZOOM_LEVEL_CHANGED, |
| 103 content::NotificationService::AllSources()); |
| 104 chrome::Zoom(browser(), zoom_action); |
| 105 zoom_observer.Wait(); |
| 106 } |
| 107 |
| 108 DISALLOW_COPY_AND_ASSIGN(LocationBarViewGtkZoomTest); |
| 109 }; |
| 110 |
| 111 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest, DefaultToZoomedInAndBack) { |
| 112 content::WebContents* contents = SetUpTest(); |
| 113 |
| 114 ZoomIn(); |
| 115 ExpectZoomedIn(contents); |
| 116 EXPECT_TRUE(ZoomIconIsShowing()); |
| 117 ExpectIconIsResource(IDR_ZOOM_PLUS); |
| 118 ExpectTooltipContainsZoom(); |
| 119 |
| 120 ZoomOut(); // Back to default, in theory. |
| 121 ExpectAtDefaultZoom(contents); |
| 122 EXPECT_FALSE(ZoomIconIsShowing()); |
| 123 } |
| 124 |
| 125 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest, ZoomInTwiceAndReset) { |
| 126 content::WebContents* contents = SetUpTest(); |
| 127 |
| 128 ZoomIn(); |
| 129 int zoom_level = GetZoomPercent(contents); |
| 130 ZoomIn(); |
| 131 DCHECK_GT(GetZoomPercent(contents), zoom_level); |
| 132 |
| 133 ExpectZoomedIn(contents); |
| 134 EXPECT_TRUE(ZoomIconIsShowing()); |
| 135 ExpectIconIsResource(IDR_ZOOM_PLUS); |
| 136 ExpectTooltipContainsZoom(); |
| 137 |
| 138 ResetZoom(); |
| 139 ExpectAtDefaultZoom(contents); |
| 140 EXPECT_FALSE(ZoomIconIsShowing()); |
| 141 } |
| 142 |
| 143 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest, DefaultToZoomedOutAndBack) { |
| 144 content::WebContents* contents = SetUpTest(); |
| 145 |
| 146 ZoomOut(); |
| 147 ExpectZoomedOut(contents); |
| 148 EXPECT_TRUE(ZoomIconIsShowing()); |
| 149 ExpectIconIsResource(IDR_ZOOM_MINUS); |
| 150 ExpectTooltipContainsZoom(); |
| 151 |
| 152 ZoomIn(); |
| 153 ExpectAtDefaultZoom(contents); |
| 154 EXPECT_FALSE(ZoomIconIsShowing()); |
| 155 } |
| 156 |
| 157 IN_PROC_BROWSER_TEST_F(LocationBarViewGtkZoomTest, ZoomOutTwiceAndReset) { |
| 158 content::WebContents* contents = SetUpTest(); |
| 159 |
| 160 ZoomOut(); |
| 161 int zoom_level = GetZoomPercent(contents); |
| 162 ZoomOut(); |
| 163 DCHECK_LT(GetZoomPercent(contents), zoom_level); |
| 164 ExpectZoomedOut(contents); |
| 165 EXPECT_TRUE(ZoomIconIsShowing()); |
| 166 ExpectIconIsResource(IDR_ZOOM_MINUS); |
| 167 ExpectTooltipContainsZoom(); |
| 168 |
| 169 ResetZoom(); |
| 170 ExpectAtDefaultZoom(contents); |
| 171 EXPECT_FALSE(ZoomIconIsShowing()); |
| 172 } |
OLD | NEW |