[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | e73bd780 | 2012-02-17 20:10:34 | [diff] [blame] | 5 | #include "ash/shell.h" |
[email protected] | 1dd1c1b | 2012-02-17 22:04:47 | [diff] [blame] | 6 | #include "ash/test/ash_test_base.h" |
[email protected] | 2f74428 | 2011-12-23 22:40:52 | [diff] [blame] | 7 | #include "ash/tooltips/tooltip_controller.h" |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 8 | #include "base/utf_string_conversions.h" |
| 9 | #include "ui/aura/client/tooltip_client.h" |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame^] | 10 | #include "ui/aura/cursor_manager.h" |
| 11 | #include "ui/aura/env.h" |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 12 | #include "ui/aura/root_window.h" |
[email protected] | 68a8e06 | 2012-01-26 01:39:49 | [diff] [blame] | 13 | #include "ui/aura/test/event_generator.h" |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 14 | #include "ui/aura/window.h" |
[email protected] | 1eea614 | 2012-03-22 09:30:01 | [diff] [blame] | 15 | #include "ui/base/resource/resource_bundle.h" |
| 16 | #include "ui/base/text/text_elider.h" |
| 17 | #include "ui/gfx/font.h" |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 18 | #include "ui/gfx/point.h" |
| 19 | #include "ui/views/view.h" |
| 20 | #include "ui/views/widget/widget.h" |
| 21 | |
[email protected] | 55f59335 | 2011-12-24 05:42:46 | [diff] [blame] | 22 | namespace ash { |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 23 | namespace test { |
| 24 | |
| 25 | namespace { |
| 26 | |
| 27 | class TooltipTestView : public views::View { |
| 28 | public: |
| 29 | TooltipTestView() : views::View() { |
| 30 | } |
| 31 | |
| 32 | void set_tooltip_text(string16 tooltip_text) { tooltip_text_ = tooltip_text; } |
| 33 | |
| 34 | // Overridden from views::View |
| 35 | bool GetTooltipText(const gfx::Point& p, string16* tooltip) const { |
| 36 | *tooltip = tooltip_text_; |
| 37 | return true; |
| 38 | } |
| 39 | |
| 40 | private: |
| 41 | string16 tooltip_text_; |
| 42 | |
| 43 | DISALLOW_COPY_AND_ASSIGN(TooltipTestView); |
| 44 | }; |
| 45 | |
| 46 | views::Widget* CreateNewWidget() { |
| 47 | views::Widget* widget = new views::Widget; |
| 48 | views::Widget::InitParams params; |
| 49 | params.type = views::Widget::InitParams::TYPE_WINDOW_FRAMELESS; |
| 50 | params.accept_events = true; |
| 51 | params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 52 | params.parent = Shell::GetPrimaryRootWindow(); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 53 | params.child = true; |
| 54 | widget->Init(params); |
| 55 | widget->Show(); |
| 56 | return widget; |
| 57 | } |
| 58 | |
| 59 | void AddViewToWidgetAndResize(views::Widget* widget, views::View* view) { |
| 60 | if (!widget->GetContentsView()) { |
| 61 | views::View* contents_view = new views::View; |
| 62 | widget->SetContentsView(contents_view); |
| 63 | } |
| 64 | |
| 65 | views::View* contents_view = widget->GetContentsView(); |
| 66 | contents_view->AddChildView(view); |
| 67 | view->SetBounds(contents_view->width(), 0, 100, 100); |
| 68 | gfx::Rect contents_view_bounds = contents_view->bounds(); |
| 69 | contents_view_bounds = contents_view_bounds.Union(view->bounds()); |
| 70 | contents_view->SetBoundsRect(contents_view_bounds); |
| 71 | widget->SetBounds(contents_view_bounds); |
| 72 | } |
| 73 | |
[email protected] | 55f59335 | 2011-12-24 05:42:46 | [diff] [blame] | 74 | ash::internal::TooltipController* GetController() { |
| 75 | return static_cast<ash::internal::TooltipController*>( |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 76 | aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow())); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 77 | } |
| 78 | |
[email protected] | 1eea614 | 2012-03-22 09:30:01 | [diff] [blame] | 79 | gfx::Font GetDefaultFont() { |
| 80 | return ui::ResourceBundle::GetSharedInstance().GetFont( |
| 81 | ui::ResourceBundle::BaseFont); |
| 82 | } |
| 83 | |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 84 | } // namespace |
| 85 | |
[email protected] | 1dd1c1b | 2012-02-17 22:04:47 | [diff] [blame] | 86 | class TooltipControllerTest : public AshTestBase { |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 87 | public: |
| 88 | TooltipControllerTest() {} |
| 89 | virtual ~TooltipControllerTest() {} |
| 90 | |
| 91 | string16 GetTooltipText() { |
| 92 | return GetController()->tooltip_text_; |
| 93 | } |
| 94 | |
| 95 | aura::Window* GetTooltipWindow() { |
| 96 | return GetController()->tooltip_window_; |
| 97 | } |
| 98 | |
| 99 | void FireTooltipTimer() { |
| 100 | GetController()->TooltipTimerFired(); |
| 101 | } |
| 102 | |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 103 | bool IsTooltipVisible() { |
| 104 | return GetController()->IsTooltipVisible(); |
| 105 | } |
| 106 | |
[email protected] | 1eea614 | 2012-03-22 09:30:01 | [diff] [blame] | 107 | void TrimTooltipToFit(string16* text, |
| 108 | int* max_width, |
| 109 | int* line_count, |
| 110 | int x, |
| 111 | int y) { |
| 112 | ash::internal::TooltipController::TrimTooltipToFit(text, max_width, |
| 113 | line_count, x, y); |
| 114 | } |
| 115 | |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 116 | private: |
| 117 | DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest); |
| 118 | }; |
| 119 | |
| 120 | TEST_F(TooltipControllerTest, NonNullTooltipClient) { |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 121 | EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow()) |
| 122 | != NULL); |
[email protected] | dc03f98 | 2012-05-04 21:12:49 | [diff] [blame] | 123 | EXPECT_EQ(string16(), GetTooltipText()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 124 | EXPECT_EQ(NULL, GetTooltipWindow()); |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 125 | EXPECT_FALSE(IsTooltipVisible()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | TEST_F(TooltipControllerTest, ViewTooltip) { |
[email protected] | 4c61654 | 2012-02-07 23:37:10 | [diff] [blame] | 129 | scoped_ptr<views::Widget> widget(CreateNewWidget()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 130 | TooltipTestView* view = new TooltipTestView; |
[email protected] | 4c61654 | 2012-02-07 23:37:10 | [diff] [blame] | 131 | AddViewToWidgetAndResize(widget.get(), view); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 132 | view->set_tooltip_text(ASCIIToUTF16("Tooltip Text")); |
[email protected] | dc03f98 | 2012-05-04 21:12:49 | [diff] [blame] | 133 | EXPECT_EQ(string16(), GetTooltipText()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 134 | EXPECT_EQ(NULL, GetTooltipWindow()); |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 135 | aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); |
[email protected] | 68a8e06 | 2012-01-26 01:39:49 | [diff] [blame] | 136 | generator.MoveMouseToCenterOf(widget->GetNativeView()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 137 | |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 138 | aura::Window* window = widget->GetNativeView(); |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 139 | EXPECT_EQ(window, Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint( |
[email protected] | 68a8e06 | 2012-01-26 01:39:49 | [diff] [blame] | 140 | generator.current_location())); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 141 | string16 expected_tooltip = ASCIIToUTF16("Tooltip Text"); |
[email protected] | 6e9f6aa | 2012-02-09 04:16:20 | [diff] [blame] | 142 | EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window)); |
[email protected] | dc03f98 | 2012-05-04 21:12:49 | [diff] [blame] | 143 | EXPECT_EQ(string16(), GetTooltipText()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 144 | EXPECT_EQ(window, GetTooltipWindow()); |
| 145 | |
| 146 | // Fire tooltip timer so tooltip becomes visible. |
| 147 | FireTooltipTimer(); |
| 148 | |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 149 | EXPECT_TRUE(IsTooltipVisible()); |
[email protected] | 68a8e06 | 2012-01-26 01:39:49 | [diff] [blame] | 150 | generator.MoveMouseBy(1, 0); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 151 | |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 152 | EXPECT_TRUE(IsTooltipVisible()); |
[email protected] | 6e9f6aa | 2012-02-09 04:16:20 | [diff] [blame] | 153 | EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window)); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 154 | EXPECT_EQ(expected_tooltip, GetTooltipText()); |
| 155 | EXPECT_EQ(window, GetTooltipWindow()); |
| 156 | } |
| 157 | |
| 158 | TEST_F(TooltipControllerTest, TooltipsInMultipleViews) { |
[email protected] | 4c61654 | 2012-02-07 23:37:10 | [diff] [blame] | 159 | scoped_ptr<views::Widget> widget(CreateNewWidget()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 160 | TooltipTestView* view1 = new TooltipTestView; |
[email protected] | 4c61654 | 2012-02-07 23:37:10 | [diff] [blame] | 161 | AddViewToWidgetAndResize(widget.get(), view1); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 162 | view1->set_tooltip_text(ASCIIToUTF16("Tooltip Text")); |
[email protected] | dc03f98 | 2012-05-04 21:12:49 | [diff] [blame] | 163 | EXPECT_EQ(string16(), GetTooltipText()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 164 | EXPECT_EQ(NULL, GetTooltipWindow()); |
| 165 | |
| 166 | TooltipTestView* view2 = new TooltipTestView; |
[email protected] | 4c61654 | 2012-02-07 23:37:10 | [diff] [blame] | 167 | AddViewToWidgetAndResize(widget.get(), view2); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 168 | |
| 169 | aura::Window* window = widget->GetNativeView(); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 170 | |
| 171 | // Fire tooltip timer so tooltip becomes visible. |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 172 | aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); |
[email protected] | 68a8e06 | 2012-01-26 01:39:49 | [diff] [blame] | 173 | generator.MoveMouseRelativeTo(window, |
| 174 | view1->bounds().CenterPoint()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 175 | FireTooltipTimer(); |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 176 | EXPECT_TRUE(IsTooltipVisible()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 177 | for (int i = 0; i < 50; i++) { |
[email protected] | 68a8e06 | 2012-01-26 01:39:49 | [diff] [blame] | 178 | generator.MoveMouseBy(1, 0); |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 179 | EXPECT_TRUE(IsTooltipVisible()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 180 | EXPECT_EQ(window, |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 181 | Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint( |
[email protected] | 68a8e06 | 2012-01-26 01:39:49 | [diff] [blame] | 182 | generator.current_location())); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 183 | string16 expected_tooltip = ASCIIToUTF16("Tooltip Text"); |
[email protected] | 6e9f6aa | 2012-02-09 04:16:20 | [diff] [blame] | 184 | EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window)); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 185 | EXPECT_EQ(expected_tooltip, GetTooltipText()); |
| 186 | EXPECT_EQ(window, GetTooltipWindow()); |
| 187 | } |
| 188 | for (int i = 0; i < 50; i++) { |
[email protected] | 68a8e06 | 2012-01-26 01:39:49 | [diff] [blame] | 189 | generator.MoveMouseBy(1, 0); |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 190 | EXPECT_FALSE(IsTooltipVisible()); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 191 | EXPECT_EQ(window, |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 192 | Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint( |
[email protected] | 68a8e06 | 2012-01-26 01:39:49 | [diff] [blame] | 193 | generator.current_location())); |
[email protected] | 6e9f6aa | 2012-02-09 04:16:20 | [diff] [blame] | 194 | string16 expected_tooltip; // = "" |
| 195 | EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window)); |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 196 | EXPECT_EQ(expected_tooltip, GetTooltipText()); |
| 197 | EXPECT_EQ(window, GetTooltipWindow()); |
| 198 | } |
| 199 | } |
| 200 | |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 201 | TEST_F(TooltipControllerTest, EnableOrDisableTooltips) { |
[email protected] | 4c61654 | 2012-02-07 23:37:10 | [diff] [blame] | 202 | scoped_ptr<views::Widget> widget(CreateNewWidget()); |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 203 | TooltipTestView* view = new TooltipTestView; |
[email protected] | 4c61654 | 2012-02-07 23:37:10 | [diff] [blame] | 204 | AddViewToWidgetAndResize(widget.get(), view); |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 205 | view->set_tooltip_text(ASCIIToUTF16("Tooltip Text")); |
[email protected] | dc03f98 | 2012-05-04 21:12:49 | [diff] [blame] | 206 | EXPECT_EQ(string16(), GetTooltipText()); |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 207 | EXPECT_EQ(NULL, GetTooltipWindow()); |
| 208 | |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 209 | aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); |
[email protected] | 68a8e06 | 2012-01-26 01:39:49 | [diff] [blame] | 210 | generator.MoveMouseRelativeTo(widget->GetNativeView(), |
| 211 | view->bounds().CenterPoint()); |
[email protected] | 5139f1e | 2012-01-18 23:58:02 | [diff] [blame] | 212 | string16 expected_tooltip = ASCIIToUTF16("Tooltip Text"); |
| 213 | |
| 214 | // Fire tooltip timer so tooltip becomes visible. |
| 215 | FireTooltipTimer(); |
| 216 | EXPECT_TRUE(IsTooltipVisible()); |
| 217 | |
| 218 | // Diable tooltips and check again. |
| 219 | GetController()->SetTooltipsEnabled(false); |
| 220 | EXPECT_FALSE(IsTooltipVisible()); |
| 221 | FireTooltipTimer(); |
| 222 | EXPECT_FALSE(IsTooltipVisible()); |
| 223 | |
| 224 | // Enable tooltips back and check again. |
| 225 | GetController()->SetTooltipsEnabled(true); |
| 226 | EXPECT_FALSE(IsTooltipVisible()); |
| 227 | FireTooltipTimer(); |
| 228 | EXPECT_TRUE(IsTooltipVisible()); |
| 229 | } |
| 230 | |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 231 | TEST_F(TooltipControllerTest, HideTooltipWhenCursorHidden) { |
| 232 | scoped_ptr<views::Widget> widget(CreateNewWidget()); |
| 233 | TooltipTestView* view = new TooltipTestView; |
| 234 | AddViewToWidgetAndResize(widget.get(), view); |
| 235 | view->set_tooltip_text(ASCIIToUTF16("Tooltip Text")); |
[email protected] | dc03f98 | 2012-05-04 21:12:49 | [diff] [blame] | 236 | EXPECT_EQ(string16(), GetTooltipText()); |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 237 | EXPECT_EQ(NULL, GetTooltipWindow()); |
| 238 | |
[email protected] | 42713f7 | 2012-05-25 00:41:50 | [diff] [blame] | 239 | aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow()); |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 240 | generator.MoveMouseRelativeTo(widget->GetNativeView(), |
| 241 | view->bounds().CenterPoint()); |
| 242 | string16 expected_tooltip = ASCIIToUTF16("Tooltip Text"); |
| 243 | |
| 244 | // Fire tooltip timer so tooltip becomes visible. |
| 245 | FireTooltipTimer(); |
| 246 | EXPECT_TRUE(IsTooltipVisible()); |
| 247 | |
| 248 | // Hide the cursor and check again. |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame^] | 249 | aura::Env::GetInstance()->cursor_manager()->ShowCursor(false); |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 250 | FireTooltipTimer(); |
| 251 | EXPECT_FALSE(IsTooltipVisible()); |
| 252 | |
| 253 | // Show the cursor and re-check. |
[email protected] | 1aad332 | 2012-06-06 06:37:09 | [diff] [blame^] | 254 | aura::Env::GetInstance()->cursor_manager()->ShowCursor(true); |
[email protected] | 3341c65 | 2012-04-24 02:02:28 | [diff] [blame] | 255 | FireTooltipTimer(); |
| 256 | EXPECT_TRUE(IsTooltipVisible()); |
| 257 | } |
| 258 | |
[email protected] | 1eea614 | 2012-03-22 09:30:01 | [diff] [blame] | 259 | TEST_F(TooltipControllerTest, TrimTooltipToFitTests) { |
| 260 | string16 tooltip; |
| 261 | int max_width, line_count, expect_lines; |
| 262 | int max_pixel_width = 400; // copied from constants in tooltip_controller.cc |
| 263 | int max_lines = 10; // copied from constants in tooltip_controller.cc |
| 264 | gfx::Font font = GetDefaultFont(); |
| 265 | size_t tooltip_len; |
| 266 | |
| 267 | // Error in computed size vs. expected size should not be greater than the |
| 268 | // size of the longest word. |
| 269 | int error_in_pixel_width = font.GetStringWidth(ASCIIToUTF16("tooltip")); |
| 270 | |
| 271 | // Long tooltips should wrap to next line |
| 272 | tooltip.clear(); |
| 273 | max_width = line_count = -1; |
| 274 | expect_lines = 3; |
| 275 | for (; font.GetStringWidth(tooltip) <= (expect_lines - 1) * max_pixel_width;) |
| 276 | tooltip.append(ASCIIToUTF16("This is part of the tooltip")); |
| 277 | tooltip_len = tooltip.length(); |
| 278 | TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 279 | EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width); |
| 280 | EXPECT_EQ(expect_lines, line_count); |
| 281 | EXPECT_EQ(tooltip_len + expect_lines - 1, tooltip.length()); |
| 282 | |
| 283 | // More than |max_lines| lines should get truncated at 10 lines. |
| 284 | tooltip.clear(); |
| 285 | max_width = line_count = -1; |
| 286 | expect_lines = 13; |
| 287 | for (; font.GetStringWidth(tooltip) <= (expect_lines - 1) * max_pixel_width;) |
| 288 | tooltip.append(ASCIIToUTF16("This is part of the tooltip")); |
| 289 | TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 290 | EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width); |
| 291 | EXPECT_EQ(max_lines, line_count); |
| 292 | |
| 293 | // Long multi line tooltips should wrap individual lines. |
| 294 | tooltip.clear(); |
| 295 | max_width = line_count = -1; |
| 296 | expect_lines = 4; |
| 297 | for (; font.GetStringWidth(tooltip) <= (expect_lines - 2) * max_pixel_width;) |
| 298 | tooltip.append(ASCIIToUTF16("This is part of the tooltip")); |
| 299 | tooltip.insert(tooltip.length() / 2, ASCIIToUTF16("\n")); |
| 300 | tooltip_len = tooltip.length(); |
| 301 | TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 302 | EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width); |
| 303 | EXPECT_EQ(expect_lines, line_count); |
| 304 | // We may have inserted the line break above near a space which will get |
| 305 | // trimmed. Hence we may be off by 1 in the final tooltip length calculation. |
| 306 | EXPECT_NEAR(tooltip_len + expect_lines - 2, tooltip.length(), 1); |
| 307 | |
[email protected] | 6c114e4 | 2012-03-22 10:02:38 | [diff] [blame] | 308 | #if !defined(OS_WIN) |
[email protected] | 1eea614 | 2012-03-22 09:30:01 | [diff] [blame] | 309 | // Tooltip with really long word gets elided. |
| 310 | tooltip.clear(); |
| 311 | max_width = line_count = -1; |
| 312 | tooltip = UTF8ToUTF16(std::string('a', max_pixel_width)); |
| 313 | TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 314 | EXPECT_NEAR(max_pixel_width, max_width, 5); |
| 315 | EXPECT_EQ(1, line_count); |
| 316 | EXPECT_EQ(ui::ElideText(UTF8ToUTF16(std::string('a', max_pixel_width)), font, |
| 317 | max_pixel_width, ui::ELIDE_AT_END), tooltip); |
[email protected] | 6c114e4 | 2012-03-22 10:02:38 | [diff] [blame] | 318 | #endif |
[email protected] | 1eea614 | 2012-03-22 09:30:01 | [diff] [blame] | 319 | |
| 320 | // Normal small tooltip should stay as is. |
| 321 | tooltip.clear(); |
| 322 | max_width = line_count = -1; |
| 323 | tooltip = ASCIIToUTF16("Small Tooltip"); |
| 324 | TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 325 | EXPECT_EQ(font.GetStringWidth(ASCIIToUTF16("Small Tooltip")), max_width); |
| 326 | EXPECT_EQ(1, line_count); |
| 327 | EXPECT_EQ(ASCIIToUTF16("Small Tooltip"), tooltip); |
| 328 | |
| 329 | // Normal small multi-line tooltip should stay as is. |
| 330 | tooltip.clear(); |
| 331 | max_width = line_count = -1; |
| 332 | tooltip = ASCIIToUTF16("Multi line\nTooltip"); |
| 333 | TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 334 | int expected_width = font.GetStringWidth(ASCIIToUTF16("Multi line")); |
| 335 | expected_width = std::max(expected_width, |
| 336 | font.GetStringWidth(ASCIIToUTF16("Tooltip"))); |
| 337 | EXPECT_EQ(expected_width, max_width); |
| 338 | EXPECT_EQ(2, line_count); |
| 339 | EXPECT_EQ(ASCIIToUTF16("Multi line\nTooltip"), tooltip); |
| 340 | |
| 341 | // Whitespaces in tooltips are preserved. |
| 342 | tooltip.clear(); |
| 343 | max_width = line_count = -1; |
| 344 | tooltip = ASCIIToUTF16("Small Tool t\tip"); |
| 345 | TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 346 | EXPECT_EQ(font.GetStringWidth(ASCIIToUTF16("Small Tool t\tip")), max_width); |
| 347 | EXPECT_EQ(1, line_count); |
| 348 | EXPECT_EQ(ASCIIToUTF16("Small Tool t\tip"), tooltip); |
| 349 | } |
| 350 | |
[email protected] | a7eb018 | 2011-12-21 12:33:31 | [diff] [blame] | 351 | } // namespace test |
[email protected] | 55f59335 | 2011-12-24 05:42:46 | [diff] [blame] | 352 | } // namespace ash |