blob: c1febfd25bf934f55d22bc29f15e2f2d2b99f8d0 [file] [log] [blame]
[email protected]5139f1e2012-01-18 23:58:021// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]a7eb0182011-12-21 12:33:312// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]e73bd7802012-02-17 20:10:345#include "ash/shell.h"
[email protected]1dd1c1b2012-02-17 22:04:476#include "ash/test/ash_test_base.h"
[email protected]2f744282011-12-23 22:40:527#include "ash/tooltips/tooltip_controller.h"
[email protected]a7eb0182011-12-21 12:33:318#include "base/utf_string_conversions.h"
9#include "ui/aura/client/tooltip_client.h"
[email protected]1aad3322012-06-06 06:37:0910#include "ui/aura/cursor_manager.h"
11#include "ui/aura/env.h"
[email protected]a7eb0182011-12-21 12:33:3112#include "ui/aura/root_window.h"
[email protected]68a8e062012-01-26 01:39:4913#include "ui/aura/test/event_generator.h"
[email protected]a7eb0182011-12-21 12:33:3114#include "ui/aura/window.h"
[email protected]1eea6142012-03-22 09:30:0115#include "ui/base/resource/resource_bundle.h"
16#include "ui/base/text/text_elider.h"
17#include "ui/gfx/font.h"
[email protected]a7eb0182011-12-21 12:33:3118#include "ui/gfx/point.h"
19#include "ui/views/view.h"
20#include "ui/views/widget/widget.h"
21
[email protected]55f593352011-12-24 05:42:4622namespace ash {
[email protected]a7eb0182011-12-21 12:33:3123namespace test {
24
25namespace {
26
27class 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
46views::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]42713f72012-05-25 00:41:5052 params.parent = Shell::GetPrimaryRootWindow();
[email protected]a7eb0182011-12-21 12:33:3153 params.child = true;
54 widget->Init(params);
55 widget->Show();
56 return widget;
57}
58
59void 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]55f593352011-12-24 05:42:4674ash::internal::TooltipController* GetController() {
75 return static_cast<ash::internal::TooltipController*>(
[email protected]42713f72012-05-25 00:41:5076 aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow()));
[email protected]a7eb0182011-12-21 12:33:3177}
78
[email protected]1eea6142012-03-22 09:30:0179gfx::Font GetDefaultFont() {
80 return ui::ResourceBundle::GetSharedInstance().GetFont(
81 ui::ResourceBundle::BaseFont);
82}
83
[email protected]a7eb0182011-12-21 12:33:3184} // namespace
85
[email protected]1dd1c1b2012-02-17 22:04:4786class TooltipControllerTest : public AshTestBase {
[email protected]a7eb0182011-12-21 12:33:3187 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]7109ce12012-07-24 01:20:33103 bool IsTooltipTimerRunning() {
104 return GetController()->tooltip_timer_.IsRunning();
105 }
106
107 void FireTooltipShownTimer() {
108 GetController()->tooltip_shown_timer_.Stop();
109 GetController()->TooltipShownTimerFired();
110 }
111
112 bool IsTooltipShownTimerRunning() {
113 return GetController()->tooltip_shown_timer_.IsRunning();
114 }
115
[email protected]5139f1e2012-01-18 23:58:02116 bool IsTooltipVisible() {
117 return GetController()->IsTooltipVisible();
118 }
119
[email protected]1eea6142012-03-22 09:30:01120 void TrimTooltipToFit(string16* text,
121 int* max_width,
122 int* line_count,
123 int x,
124 int y) {
125 ash::internal::TooltipController::TrimTooltipToFit(text, max_width,
126 line_count, x, y);
127 }
128
[email protected]a7eb0182011-12-21 12:33:31129 private:
130 DISALLOW_COPY_AND_ASSIGN(TooltipControllerTest);
131};
132
133TEST_F(TooltipControllerTest, NonNullTooltipClient) {
[email protected]42713f72012-05-25 00:41:50134 EXPECT_TRUE(aura::client::GetTooltipClient(Shell::GetPrimaryRootWindow())
135 != NULL);
[email protected]dc03f982012-05-04 21:12:49136 EXPECT_EQ(string16(), GetTooltipText());
[email protected]a7eb0182011-12-21 12:33:31137 EXPECT_EQ(NULL, GetTooltipWindow());
[email protected]5139f1e2012-01-18 23:58:02138 EXPECT_FALSE(IsTooltipVisible());
[email protected]a7eb0182011-12-21 12:33:31139}
140
141TEST_F(TooltipControllerTest, ViewTooltip) {
[email protected]4c616542012-02-07 23:37:10142 scoped_ptr<views::Widget> widget(CreateNewWidget());
[email protected]a7eb0182011-12-21 12:33:31143 TooltipTestView* view = new TooltipTestView;
[email protected]4c616542012-02-07 23:37:10144 AddViewToWidgetAndResize(widget.get(), view);
[email protected]a7eb0182011-12-21 12:33:31145 view->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
[email protected]dc03f982012-05-04 21:12:49146 EXPECT_EQ(string16(), GetTooltipText());
[email protected]a7eb0182011-12-21 12:33:31147 EXPECT_EQ(NULL, GetTooltipWindow());
[email protected]42713f72012-05-25 00:41:50148 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
[email protected]68a8e062012-01-26 01:39:49149 generator.MoveMouseToCenterOf(widget->GetNativeView());
[email protected]a7eb0182011-12-21 12:33:31150
[email protected]a7eb0182011-12-21 12:33:31151 aura::Window* window = widget->GetNativeView();
[email protected]42713f72012-05-25 00:41:50152 EXPECT_EQ(window, Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint(
[email protected]68a8e062012-01-26 01:39:49153 generator.current_location()));
[email protected]a7eb0182011-12-21 12:33:31154 string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
[email protected]6e9f6aa2012-02-09 04:16:20155 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
[email protected]dc03f982012-05-04 21:12:49156 EXPECT_EQ(string16(), GetTooltipText());
[email protected]a7eb0182011-12-21 12:33:31157 EXPECT_EQ(window, GetTooltipWindow());
158
159 // Fire tooltip timer so tooltip becomes visible.
160 FireTooltipTimer();
161
[email protected]5139f1e2012-01-18 23:58:02162 EXPECT_TRUE(IsTooltipVisible());
[email protected]68a8e062012-01-26 01:39:49163 generator.MoveMouseBy(1, 0);
[email protected]a7eb0182011-12-21 12:33:31164
[email protected]5139f1e2012-01-18 23:58:02165 EXPECT_TRUE(IsTooltipVisible());
[email protected]6e9f6aa2012-02-09 04:16:20166 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
[email protected]a7eb0182011-12-21 12:33:31167 EXPECT_EQ(expected_tooltip, GetTooltipText());
168 EXPECT_EQ(window, GetTooltipWindow());
169}
170
171TEST_F(TooltipControllerTest, TooltipsInMultipleViews) {
[email protected]4c616542012-02-07 23:37:10172 scoped_ptr<views::Widget> widget(CreateNewWidget());
[email protected]a7eb0182011-12-21 12:33:31173 TooltipTestView* view1 = new TooltipTestView;
[email protected]4c616542012-02-07 23:37:10174 AddViewToWidgetAndResize(widget.get(), view1);
[email protected]a7eb0182011-12-21 12:33:31175 view1->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
[email protected]dc03f982012-05-04 21:12:49176 EXPECT_EQ(string16(), GetTooltipText());
[email protected]a7eb0182011-12-21 12:33:31177 EXPECT_EQ(NULL, GetTooltipWindow());
178
179 TooltipTestView* view2 = new TooltipTestView;
[email protected]4c616542012-02-07 23:37:10180 AddViewToWidgetAndResize(widget.get(), view2);
[email protected]a7eb0182011-12-21 12:33:31181
182 aura::Window* window = widget->GetNativeView();
[email protected]a7eb0182011-12-21 12:33:31183
184 // Fire tooltip timer so tooltip becomes visible.
[email protected]42713f72012-05-25 00:41:50185 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
[email protected]68a8e062012-01-26 01:39:49186 generator.MoveMouseRelativeTo(window,
187 view1->bounds().CenterPoint());
[email protected]a7eb0182011-12-21 12:33:31188 FireTooltipTimer();
[email protected]5139f1e2012-01-18 23:58:02189 EXPECT_TRUE(IsTooltipVisible());
[email protected]a7eb0182011-12-21 12:33:31190 for (int i = 0; i < 50; i++) {
[email protected]68a8e062012-01-26 01:39:49191 generator.MoveMouseBy(1, 0);
[email protected]5139f1e2012-01-18 23:58:02192 EXPECT_TRUE(IsTooltipVisible());
[email protected]a7eb0182011-12-21 12:33:31193 EXPECT_EQ(window,
[email protected]42713f72012-05-25 00:41:50194 Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint(
[email protected]68a8e062012-01-26 01:39:49195 generator.current_location()));
[email protected]a7eb0182011-12-21 12:33:31196 string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
[email protected]6e9f6aa2012-02-09 04:16:20197 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
[email protected]a7eb0182011-12-21 12:33:31198 EXPECT_EQ(expected_tooltip, GetTooltipText());
199 EXPECT_EQ(window, GetTooltipWindow());
200 }
201 for (int i = 0; i < 50; i++) {
[email protected]68a8e062012-01-26 01:39:49202 generator.MoveMouseBy(1, 0);
[email protected]5139f1e2012-01-18 23:58:02203 EXPECT_FALSE(IsTooltipVisible());
[email protected]a7eb0182011-12-21 12:33:31204 EXPECT_EQ(window,
[email protected]42713f72012-05-25 00:41:50205 Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint(
[email protected]68a8e062012-01-26 01:39:49206 generator.current_location()));
[email protected]6e9f6aa2012-02-09 04:16:20207 string16 expected_tooltip; // = ""
208 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
[email protected]a7eb0182011-12-21 12:33:31209 EXPECT_EQ(expected_tooltip, GetTooltipText());
210 EXPECT_EQ(window, GetTooltipWindow());
211 }
212}
213
[email protected]5139f1e2012-01-18 23:58:02214TEST_F(TooltipControllerTest, EnableOrDisableTooltips) {
[email protected]4c616542012-02-07 23:37:10215 scoped_ptr<views::Widget> widget(CreateNewWidget());
[email protected]5139f1e2012-01-18 23:58:02216 TooltipTestView* view = new TooltipTestView;
[email protected]4c616542012-02-07 23:37:10217 AddViewToWidgetAndResize(widget.get(), view);
[email protected]5139f1e2012-01-18 23:58:02218 view->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
[email protected]dc03f982012-05-04 21:12:49219 EXPECT_EQ(string16(), GetTooltipText());
[email protected]5139f1e2012-01-18 23:58:02220 EXPECT_EQ(NULL, GetTooltipWindow());
221
[email protected]42713f72012-05-25 00:41:50222 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
[email protected]68a8e062012-01-26 01:39:49223 generator.MoveMouseRelativeTo(widget->GetNativeView(),
224 view->bounds().CenterPoint());
[email protected]5139f1e2012-01-18 23:58:02225 string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
226
227 // Fire tooltip timer so tooltip becomes visible.
228 FireTooltipTimer();
229 EXPECT_TRUE(IsTooltipVisible());
230
231 // Diable tooltips and check again.
232 GetController()->SetTooltipsEnabled(false);
233 EXPECT_FALSE(IsTooltipVisible());
234 FireTooltipTimer();
235 EXPECT_FALSE(IsTooltipVisible());
236
237 // Enable tooltips back and check again.
238 GetController()->SetTooltipsEnabled(true);
239 EXPECT_FALSE(IsTooltipVisible());
240 FireTooltipTimer();
241 EXPECT_TRUE(IsTooltipVisible());
242}
243
[email protected]3341c652012-04-24 02:02:28244TEST_F(TooltipControllerTest, HideTooltipWhenCursorHidden) {
245 scoped_ptr<views::Widget> widget(CreateNewWidget());
246 TooltipTestView* view = new TooltipTestView;
247 AddViewToWidgetAndResize(widget.get(), view);
248 view->set_tooltip_text(ASCIIToUTF16("Tooltip Text"));
[email protected]dc03f982012-05-04 21:12:49249 EXPECT_EQ(string16(), GetTooltipText());
[email protected]3341c652012-04-24 02:02:28250 EXPECT_EQ(NULL, GetTooltipWindow());
251
[email protected]42713f72012-05-25 00:41:50252 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
[email protected]3341c652012-04-24 02:02:28253 generator.MoveMouseRelativeTo(widget->GetNativeView(),
254 view->bounds().CenterPoint());
255 string16 expected_tooltip = ASCIIToUTF16("Tooltip Text");
256
257 // Fire tooltip timer so tooltip becomes visible.
258 FireTooltipTimer();
259 EXPECT_TRUE(IsTooltipVisible());
260
261 // Hide the cursor and check again.
[email protected]1aad3322012-06-06 06:37:09262 aura::Env::GetInstance()->cursor_manager()->ShowCursor(false);
[email protected]3341c652012-04-24 02:02:28263 FireTooltipTimer();
264 EXPECT_FALSE(IsTooltipVisible());
265
266 // Show the cursor and re-check.
[email protected]1aad3322012-06-06 06:37:09267 aura::Env::GetInstance()->cursor_manager()->ShowCursor(true);
[email protected]3341c652012-04-24 02:02:28268 FireTooltipTimer();
269 EXPECT_TRUE(IsTooltipVisible());
270}
271
[email protected]1eea6142012-03-22 09:30:01272TEST_F(TooltipControllerTest, TrimTooltipToFitTests) {
273 string16 tooltip;
274 int max_width, line_count, expect_lines;
275 int max_pixel_width = 400; // copied from constants in tooltip_controller.cc
276 int max_lines = 10; // copied from constants in tooltip_controller.cc
277 gfx::Font font = GetDefaultFont();
278 size_t tooltip_len;
279
280 // Error in computed size vs. expected size should not be greater than the
281 // size of the longest word.
282 int error_in_pixel_width = font.GetStringWidth(ASCIIToUTF16("tooltip"));
283
284 // Long tooltips should wrap to next line
285 tooltip.clear();
286 max_width = line_count = -1;
287 expect_lines = 3;
288 for (; font.GetStringWidth(tooltip) <= (expect_lines - 1) * max_pixel_width;)
289 tooltip.append(ASCIIToUTF16("This is part of the tooltip"));
290 tooltip_len = tooltip.length();
291 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
292 EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width);
293 EXPECT_EQ(expect_lines, line_count);
294 EXPECT_EQ(tooltip_len + expect_lines - 1, tooltip.length());
295
296 // More than |max_lines| lines should get truncated at 10 lines.
297 tooltip.clear();
298 max_width = line_count = -1;
299 expect_lines = 13;
300 for (; font.GetStringWidth(tooltip) <= (expect_lines - 1) * max_pixel_width;)
301 tooltip.append(ASCIIToUTF16("This is part of the tooltip"));
302 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
303 EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width);
304 EXPECT_EQ(max_lines, line_count);
305
306 // Long multi line tooltips should wrap individual lines.
307 tooltip.clear();
308 max_width = line_count = -1;
309 expect_lines = 4;
310 for (; font.GetStringWidth(tooltip) <= (expect_lines - 2) * max_pixel_width;)
311 tooltip.append(ASCIIToUTF16("This is part of the tooltip"));
312 tooltip.insert(tooltip.length() / 2, ASCIIToUTF16("\n"));
313 tooltip_len = tooltip.length();
314 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
315 EXPECT_NEAR(max_pixel_width, max_width, error_in_pixel_width);
316 EXPECT_EQ(expect_lines, line_count);
317 // We may have inserted the line break above near a space which will get
318 // trimmed. Hence we may be off by 1 in the final tooltip length calculation.
319 EXPECT_NEAR(tooltip_len + expect_lines - 2, tooltip.length(), 1);
320
[email protected]6c114e42012-03-22 10:02:38321#if !defined(OS_WIN)
[email protected]1eea6142012-03-22 09:30:01322 // Tooltip with really long word gets elided.
323 tooltip.clear();
324 max_width = line_count = -1;
325 tooltip = UTF8ToUTF16(std::string('a', max_pixel_width));
326 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
327 EXPECT_NEAR(max_pixel_width, max_width, 5);
328 EXPECT_EQ(1, line_count);
329 EXPECT_EQ(ui::ElideText(UTF8ToUTF16(std::string('a', max_pixel_width)), font,
330 max_pixel_width, ui::ELIDE_AT_END), tooltip);
[email protected]6c114e42012-03-22 10:02:38331#endif
[email protected]1eea6142012-03-22 09:30:01332
333 // Normal small tooltip should stay as is.
334 tooltip.clear();
335 max_width = line_count = -1;
336 tooltip = ASCIIToUTF16("Small Tooltip");
337 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
338 EXPECT_EQ(font.GetStringWidth(ASCIIToUTF16("Small Tooltip")), max_width);
339 EXPECT_EQ(1, line_count);
340 EXPECT_EQ(ASCIIToUTF16("Small Tooltip"), tooltip);
341
342 // Normal small multi-line tooltip should stay as is.
343 tooltip.clear();
344 max_width = line_count = -1;
345 tooltip = ASCIIToUTF16("Multi line\nTooltip");
346 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
347 int expected_width = font.GetStringWidth(ASCIIToUTF16("Multi line"));
348 expected_width = std::max(expected_width,
349 font.GetStringWidth(ASCIIToUTF16("Tooltip")));
350 EXPECT_EQ(expected_width, max_width);
351 EXPECT_EQ(2, line_count);
352 EXPECT_EQ(ASCIIToUTF16("Multi line\nTooltip"), tooltip);
353
354 // Whitespaces in tooltips are preserved.
355 tooltip.clear();
356 max_width = line_count = -1;
357 tooltip = ASCIIToUTF16("Small Tool t\tip");
358 TrimTooltipToFit(&tooltip, &max_width, &line_count, 0, 0);
359 EXPECT_EQ(font.GetStringWidth(ASCIIToUTF16("Small Tool t\tip")), max_width);
360 EXPECT_EQ(1, line_count);
361 EXPECT_EQ(ASCIIToUTF16("Small Tool t\tip"), tooltip);
362}
363
[email protected]7109ce12012-07-24 01:20:33364TEST_F(TooltipControllerTest, TooltipHidesOnKeyPressAndStaysHiddenUntilChange) {
365 scoped_ptr<views::Widget> widget(CreateNewWidget());
366 TooltipTestView* view1 = new TooltipTestView;
367 AddViewToWidgetAndResize(widget.get(), view1);
368 view1->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 1"));
369 EXPECT_EQ(string16(), GetTooltipText());
370 EXPECT_EQ(NULL, GetTooltipWindow());
371
372 TooltipTestView* view2 = new TooltipTestView;
373 AddViewToWidgetAndResize(widget.get(), view2);
374 view2->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 2"));
375
376 aura::Window* window = widget->GetNativeView();
377
378 // Fire tooltip timer so tooltip becomes visible.
379 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
380 generator.MoveMouseRelativeTo(window,
381 view1->bounds().CenterPoint());
382 FireTooltipTimer();
383 EXPECT_TRUE(IsTooltipVisible());
384 EXPECT_TRUE(IsTooltipShownTimerRunning());
385
386 generator.PressKey(ui::VKEY_1, 0);
387 EXPECT_FALSE(IsTooltipVisible());
388 EXPECT_FALSE(IsTooltipTimerRunning());
389 EXPECT_FALSE(IsTooltipShownTimerRunning());
390
391 // Moving the mouse inside |view1| should not change the state of the tooltip
392 // or the timers.
393 for (int i = 0; i < 50; i++) {
394 generator.MoveMouseBy(1, 0);
395 EXPECT_FALSE(IsTooltipVisible());
396 EXPECT_FALSE(IsTooltipTimerRunning());
397 EXPECT_FALSE(IsTooltipShownTimerRunning());
398 EXPECT_EQ(window,
399 Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint(
400 generator.current_location()));
401 string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 1");
402 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
403 EXPECT_EQ(expected_tooltip, GetTooltipText());
404 EXPECT_EQ(window, GetTooltipWindow());
405 }
406
407 // Now we move the mouse on to |view2|. It should re-start the tooltip timer.
408 generator.MoveMouseBy(1, 0);
409 EXPECT_TRUE(IsTooltipTimerRunning());
410 FireTooltipTimer();
411 EXPECT_TRUE(IsTooltipVisible());
412 EXPECT_TRUE(IsTooltipShownTimerRunning());
413 string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 2");
414 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
415 EXPECT_EQ(expected_tooltip, GetTooltipText());
416 EXPECT_EQ(window, GetTooltipWindow());
417}
418
419TEST_F(TooltipControllerTest, TooltipHidesOnTimeoutAndStaysHiddenUntilChange) {
420 scoped_ptr<views::Widget> widget(CreateNewWidget());
421 TooltipTestView* view1 = new TooltipTestView;
422 AddViewToWidgetAndResize(widget.get(), view1);
423 view1->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 1"));
424 EXPECT_EQ(string16(), GetTooltipText());
425 EXPECT_EQ(NULL, GetTooltipWindow());
426
427 TooltipTestView* view2 = new TooltipTestView;
428 AddViewToWidgetAndResize(widget.get(), view2);
429 view2->set_tooltip_text(ASCIIToUTF16("Tooltip Text for view 2"));
430
431 aura::Window* window = widget->GetNativeView();
432
433 // Fire tooltip timer so tooltip becomes visible.
434 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
435 generator.MoveMouseRelativeTo(window,
436 view1->bounds().CenterPoint());
437 FireTooltipTimer();
438 EXPECT_TRUE(IsTooltipVisible());
439 EXPECT_TRUE(IsTooltipShownTimerRunning());
440
441 FireTooltipShownTimer();
442 EXPECT_FALSE(IsTooltipVisible());
443 EXPECT_FALSE(IsTooltipTimerRunning());
444 EXPECT_FALSE(IsTooltipShownTimerRunning());
445
446 // Moving the mouse inside |view1| should not change the state of the tooltip
447 // or the timers.
448 for (int i = 0; i < 50; i++) {
449 generator.MoveMouseBy(1, 0);
450 EXPECT_FALSE(IsTooltipVisible());
451 EXPECT_FALSE(IsTooltipTimerRunning());
452 EXPECT_FALSE(IsTooltipShownTimerRunning());
453 EXPECT_EQ(window,
454 Shell::GetPrimaryRootWindow()->GetEventHandlerForPoint(
455 generator.current_location()));
456 string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 1");
457 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
458 EXPECT_EQ(expected_tooltip, GetTooltipText());
459 EXPECT_EQ(window, GetTooltipWindow());
460 }
461
462 // Now we move the mouse on to |view2|. It should re-start the tooltip timer.
463 generator.MoveMouseBy(1, 0);
464 EXPECT_TRUE(IsTooltipTimerRunning());
465 FireTooltipTimer();
466 EXPECT_TRUE(IsTooltipVisible());
467 EXPECT_TRUE(IsTooltipShownTimerRunning());
468 string16 expected_tooltip = ASCIIToUTF16("Tooltip Text for view 2");
469 EXPECT_EQ(expected_tooltip, aura::client::GetTooltipText(window));
470 EXPECT_EQ(expected_tooltip, GetTooltipText());
471 EXPECT_EQ(window, GetTooltipWindow());
472}
473
[email protected]a7eb0182011-12-21 12:33:31474} // namespace test
[email protected]55f593352011-12-24 05:42:46475} // namespace ash