OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ash/shell.h" | 5 #include "ash/shell.h" |
6 #include "ash/test/ash_test_base.h" | 6 #include "ash/test/ash_test_base.h" |
7 #include "ash/tooltips/tooltip_controller.h" | 7 #include "ash/tooltips/tooltip_controller.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "ui/aura/client/tooltip_client.h" | 9 #include "ui/aura/client/tooltip_client.h" |
10 #include "ui/aura/root_window.h" | 10 #include "ui/aura/root_window.h" |
11 #include "ui/aura/test/event_generator.h" | 11 #include "ui/aura/test/event_generator.h" |
12 #include "ui/aura/window.h" | 12 #include "ui/aura/window.h" |
| 13 #include "ui/base/resource/resource_bundle.h" |
| 14 #include "ui/base/text/text_elider.h" |
| 15 #include "ui/gfx/font.h" |
13 #include "ui/gfx/point.h" | 16 #include "ui/gfx/point.h" |
14 #include "ui/views/view.h" | 17 #include "ui/views/view.h" |
15 #include "ui/views/widget/widget.h" | 18 #include "ui/views/widget/widget.h" |
16 | 19 |
17 namespace ash { | 20 namespace ash { |
18 namespace test { | 21 namespace test { |
19 | 22 |
20 namespace { | 23 namespace { |
21 | 24 |
22 class TooltipTestView : public views::View { | 25 class TooltipTestView : public views::View { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 contents_view_bounds = contents_view_bounds.Union(view->bounds()); | 67 contents_view_bounds = contents_view_bounds.Union(view->bounds()); |
65 contents_view->SetBoundsRect(contents_view_bounds); | 68 contents_view->SetBoundsRect(contents_view_bounds); |
66 widget->SetBounds(contents_view_bounds); | 69 widget->SetBounds(contents_view_bounds); |
67 } | 70 } |
68 | 71 |
69 ash::internal::TooltipController* GetController() { | 72 ash::internal::TooltipController* GetController() { |
70 return static_cast<ash::internal::TooltipController*>( | 73 return static_cast<ash::internal::TooltipController*>( |
71 aura::client::GetTooltipClient(Shell::GetRootWindow())); | 74 aura::client::GetTooltipClient(Shell::GetRootWindow())); |
72 } | 75 } |
73 | 76 |
| 77 gfx::Font GetDefaultFont() { |
| 78 return ui::ResourceBundle::GetSharedInstance().GetFont( |
| 79 ui::ResourceBundle::BaseFont); |
| 80 } |
| 81 |
74 } // namespace | 82 } // namespace |
75 | 83 |
76 class TooltipControllerTest : public AshTestBase { | 84 class TooltipControllerTest : public AshTestBase { |
77 public: | 85 public: |
78 TooltipControllerTest() {} | 86 TooltipControllerTest() {} |
79 virtual ~TooltipControllerTest() {} | 87 virtual ~TooltipControllerTest() {} |
80 | 88 |
81 string16 GetTooltipText() { | 89 string16 GetTooltipText() { |
82 return GetController()->tooltip_text_; | 90 return GetController()->tooltip_text_; |
83 } | 91 } |
84 | 92 |
85 aura::Window* GetTooltipWindow() { | 93 aura::Window* GetTooltipWindow() { |
86 return GetController()->tooltip_window_; | 94 return GetController()->tooltip_window_; |
87 } | 95 } |
88 | 96 |
89 void FireTooltipTimer() { | 97 void FireTooltipTimer() { |
90 GetController()->TooltipTimerFired(); | 98 GetController()->TooltipTimerFired(); |
91 } | 99 } |
92 | 100 |
93 bool IsTooltipVisible() { | 101 bool IsTooltipVisible() { |
94 return GetController()->IsTooltipVisible(); | 102 return GetController()->IsTooltipVisible(); |
95 } | 103 } |
96 | 104 |
| 105 void TrimTooltipToFit(string16* text, |
| 106 int* max_width, |
| 107 int* line_count, |
| 108 int x, |
| 109 int y) { |
| 110 ash::internal::TooltipController::TrimTooltipToFit(text, max_width, |
| 111 line_count, x, y); |
| 112 } |
| 113 |
97 private: | 114 private: |
98 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest); | 115 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest); |
99 }; | 116 }; |
100 | 117 |
101 TEST_F(TooltipControllerTest, NonNullTooltipClient) { | 118 TEST_F(TooltipControllerTest, NonNullTooltipClient) { |
102 EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetRootWindow()) != NULL); | 119 EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetRootWindow()) != NULL); |
103 EXPECT_EQ(ASCIIToUTF16(""), GetTooltipText()); | 120 EXPECT_EQ(ASCIIToUTF16(""), GetTooltipText()); |
104 EXPECT_EQ(NULL, GetTooltipWindow()); | 121 EXPECT_EQ(NULL, GetTooltipWindow()); |
105 EXPECT_FALSE(IsTooltipVisible()); | 122 EXPECT_FALSE(IsTooltipVisible()); |
106 } | 123 } |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 FireTooltipTimer(); | 218 FireTooltipTimer(); |
202 EXPECT_FALSE(IsTooltipVisible()); | 219 EXPECT_FALSE(IsTooltipVisible()); |
203 | 220 |
204 // Enable tooltips back and check again. | 221 // Enable tooltips back and check again. |
205 GetController()->SetTooltipsEnabled(true); | 222 GetController()->SetTooltipsEnabled(true); |
206 EXPECT_FALSE(IsTooltipVisible()); | 223 EXPECT_FALSE(IsTooltipVisible()); |
207 FireTooltipTimer(); | 224 FireTooltipTimer(); |
208 EXPECT_TRUE(IsTooltipVisible()); | 225 EXPECT_TRUE(IsTooltipVisible()); |
209 } | 226 } |
210 | 227 |
| 228 TEST_F(TooltipControllerTest, TrimTooltipToFitTests) { |
| 229 string16 tooltip; |
| 230 int max_width, line_count, expect_lines; |
| 231 int max_pixel_width = 400; // copied from constants in tooltip_controller.cc |
| 232 int max_lines = 10; // copied from constants in tooltip_controller.cc |
| 233 gfx::Font font = GetDefaultFont(); |
| 234 size_t tooltip_len; |
| 235 |
| 236 // Error in computed size vs. expected size should not be greater than the |
| 237 // size of the longest word. |
| 238 int error_in_pixel_width = font.GetStringWidth(ASCIIToUTF16("tooltip")); |
| 239 |
| 240 // Long tooltips should wrap to next line |
| 241 tooltip.clear(); |
| 242 max_width = line_count = -1; |
| 243 expect_lines = 3; |
| 244 for (; font.GetStringWidth(tooltip) <= (expect_lines - 1) * max_pixel_width;) |
| 245 tooltip.append(ASCIIToUTF16("This is part of the tooltip")); |
| 246 tooltip_len = tooltip.length(); |
| 247 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 248 EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width); |
| 249 EXPECT_EQ(expect_lines, line_count); |
| 250 EXPECT_EQ(tooltip_len + expect_lines - 1, tooltip.length()); |
| 251 |
| 252 // More than |max_lines| lines should get truncated at 10 lines. |
| 253 tooltip.clear(); |
| 254 max_width = line_count = -1; |
| 255 expect_lines = 13; |
| 256 for (; font.GetStringWidth(tooltip) <= (expect_lines - 1) * max_pixel_width;) |
| 257 tooltip.append(ASCIIToUTF16("This is part of the tooltip")); |
| 258 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 259 EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width); |
| 260 EXPECT_EQ(max_lines, line_count); |
| 261 |
| 262 // Long multi line tooltips should wrap individual lines. |
| 263 tooltip.clear(); |
| 264 max_width = line_count = -1; |
| 265 expect_lines = 4; |
| 266 for (; font.GetStringWidth(tooltip) <= (expect_lines - 2) * max_pixel_width;) |
| 267 tooltip.append(ASCIIToUTF16("This is part of the tooltip")); |
| 268 tooltip.insert(tooltip.length() / 2, ASCIIToUTF16("\n")); |
| 269 tooltip_len = tooltip.length(); |
| 270 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 271 EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width); |
| 272 EXPECT_EQ(expect_lines, line_count); |
| 273 // We may have inserted the line break above near a space which will get |
| 274 // trimmed. Hence we may be off by 1 in the final tooltip length calculation. |
| 275 EXPECT_NEAR(tooltip_len + expect_lines - 2, tooltip.length(), 1); |
| 276 |
| 277 // Tooltip with really long word gets elided. |
| 278 tooltip.clear(); |
| 279 max_width = line_count = -1; |
| 280 tooltip = UTF8ToUTF16(std::string('a', max_pixel_width)); |
| 281 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 282 EXPECT_NEAR(max_pixel_width, max_width, 5); |
| 283 EXPECT_EQ(1, line_count); |
| 284 EXPECT_EQ(ui::ElideText(UTF8ToUTF16(std::string('a', max_pixel_width)), font, |
| 285 max_pixel_width, ui::ELIDE_AT_END), tooltip); |
| 286 |
| 287 // Normal small tooltip should stay as is. |
| 288 tooltip.clear(); |
| 289 max_width = line_count = -1; |
| 290 tooltip = ASCIIToUTF16("Small Tooltip"); |
| 291 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 292 EXPECT_EQ(font.GetStringWidth(ASCIIToUTF16("Small Tooltip")), max_width); |
| 293 EXPECT_EQ(1, line_count); |
| 294 EXPECT_EQ(ASCIIToUTF16("Small Tooltip"), tooltip); |
| 295 |
| 296 // Normal small multi-line tooltip should stay as is. |
| 297 tooltip.clear(); |
| 298 max_width = line_count = -1; |
| 299 tooltip = ASCIIToUTF16("Multi line\nTooltip"); |
| 300 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 301 int expected_width = font.GetStringWidth(ASCIIToUTF16("Multi line")); |
| 302 expected_width = std::max(expected_width, |
| 303 font.GetStringWidth(ASCIIToUTF16("Tooltip"))); |
| 304 EXPECT_EQ(expected_width, max_width); |
| 305 EXPECT_EQ(2, line_count); |
| 306 EXPECT_EQ(ASCIIToUTF16("Multi line\nTooltip"), tooltip); |
| 307 |
| 308 // Whitespaces in tooltips are preserved. |
| 309 tooltip.clear(); |
| 310 max_width = line_count = -1; |
| 311 tooltip = ASCIIToUTF16("Small Tool t\tip"); |
| 312 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0); |
| 313 EXPECT_EQ(font.GetStringWidth(ASCIIToUTF16("Small Tool t\tip")), max_width); |
| 314 EXPECT_EQ(1, line_count); |
| 315 EXPECT_EQ(ASCIIToUTF16("Small Tool t\tip"), tooltip); |
| 316 } |
| 317 |
211 } // namespace test | 318 } // namespace test |
212 } // namespace ash | 319 } // namespace ash |
OLD | NEW |