- add sources.
[platform/framework/web/crosswalk.git] / src / ash / shelf / shelf_tooltip_manager_unittest.cc
1 // Copyright 2013 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 "ash/shelf/shelf_tooltip_manager.h"
6
7 #include "ash/root_window_controller.h"
8 #include "ash/shelf/shelf_layout_manager.h"
9 #include "ash/shelf/shelf_widget.h"
10 #include "ash/shell.h"
11 #include "ash/shell_window_ids.h"
12 #include "ash/test/ash_test_base.h"
13 #include "ash/test/launcher_test_api.h"
14 #include "ash/wm/window_util.h"
15 #include "base/strings/string16.h"
16 #include "base/time/time.h"
17 #include "ui/aura/root_window.h"
18 #include "ui/events/event.h"
19 #include "ui/events/event_constants.h"
20 #include "ui/events/event_handler.h"
21 #include "ui/events/keycodes/keyboard_codes.h"
22 #include "ui/views/widget/widget.h"
23
24 namespace {
25
26 void SetEventTarget(ui::EventTarget* target,
27                     ui::Event* event) {
28   ui::Event::DispatcherApi dispatch_helper(event);
29   dispatch_helper.set_target(target);
30 }
31
32 }
33
34 namespace ash {
35 namespace test {
36
37 class ShelfTooltipManagerTest : public AshTestBase {
38  public:
39   ShelfTooltipManagerTest() {}
40   virtual ~ShelfTooltipManagerTest() {}
41
42   virtual void SetUp() OVERRIDE {
43     AshTestBase::SetUp();
44     internal::RootWindowController* controller =
45         Shell::GetPrimaryRootWindowController();
46     tooltip_manager_.reset(new internal::ShelfTooltipManager(
47         controller->GetShelfLayoutManager(),
48         LauncherTestAPI(controller->shelf()->launcher()).shelf_view()));
49   }
50
51   virtual void TearDown() OVERRIDE {
52     tooltip_manager_.reset();
53     AshTestBase::TearDown();
54   }
55
56   void ShowDelayed() {
57     CreateWidget();
58     tooltip_manager_->ShowDelayed(dummy_anchor_.get(), base::string16());
59   }
60
61   void ShowImmediately() {
62     CreateWidget();
63     tooltip_manager_->ShowImmediately(dummy_anchor_.get(), base::string16());
64   }
65
66   bool TooltipIsVisible() {
67     return tooltip_manager_->IsVisible();
68   }
69
70   bool IsTimerRunning() {
71     return tooltip_manager_->timer_.get() != NULL;
72   }
73
74   ui::EventHandler* GetEventHandler() {
75     return tooltip_manager_.get();
76   }
77
78   views::Widget* GetTooltipWidget() {
79     return tooltip_manager_->widget_;
80   }
81
82  protected:
83   scoped_ptr<views::Widget> widget_;
84   scoped_ptr<views::View> dummy_anchor_;
85   scoped_ptr<internal::ShelfTooltipManager> tooltip_manager_;
86
87  private:
88   void CreateWidget() {
89     dummy_anchor_.reset(new views::View);
90
91     widget_.reset(new views::Widget);
92     views::Widget::InitParams params(
93         views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
94     params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
95     params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
96     params.parent = Shell::GetContainer(
97         Shell::GetPrimaryRootWindow(),
98         ash::internal::kShellWindowId_ShelfContainer);
99
100     widget_->Init(params);
101     widget_->SetContentsView(dummy_anchor_.get());
102   }
103
104   DISALLOW_COPY_AND_ASSIGN(ShelfTooltipManagerTest);
105 };
106
107 TEST_F(ShelfTooltipManagerTest, ShowingBasics) {
108   // ShowDelayed() should just start the timer instead of showing immediately.
109   ShowDelayed();
110   EXPECT_FALSE(TooltipIsVisible());
111   EXPECT_TRUE(IsTimerRunning());
112
113   ShowImmediately();
114   EXPECT_TRUE(TooltipIsVisible());
115   EXPECT_FALSE(IsTimerRunning());
116 }
117
118 TEST_F(ShelfTooltipManagerTest, HideWhenShelfIsHidden) {
119   ShowImmediately();
120   ASSERT_TRUE(TooltipIsVisible());
121
122   // Create a full-screen window to hide the shelf.
123   scoped_ptr<views::Widget> widget(new views::Widget);
124   views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
125   params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
126   params.context = CurrentContext();
127   widget->Init(params);
128   widget->SetFullscreen(true);
129   widget->Show();
130
131   // Once the shelf is hidden, the tooltip should be invisible.
132   ASSERT_EQ(
133       SHELF_HIDDEN,
134       Shell::GetPrimaryRootWindowController()->
135           GetShelfLayoutManager()->visibility_state());
136   EXPECT_FALSE(TooltipIsVisible());
137
138   // Do not show the view if the shelf is hidden.
139   ShowImmediately();
140   EXPECT_FALSE(TooltipIsVisible());
141
142   // ShowDelayed() doesn't even start the timer for the hidden shelf.
143   ShowDelayed();
144   EXPECT_FALSE(IsTimerRunning());
145 }
146
147 TEST_F(ShelfTooltipManagerTest, HideWhenShelfIsAutoHide) {
148   // Create a visible window so auto-hide behavior is enforced.
149   views::Widget* dummy = new views::Widget;
150   views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
151   params.bounds = gfx::Rect(0, 0, 200, 200);
152   params.context = CurrentContext();
153   dummy->Init(params);
154   dummy->Show();
155
156   ShowImmediately();
157   ASSERT_TRUE(TooltipIsVisible());
158
159   internal::ShelfLayoutManager* shelf =
160       Shell::GetPrimaryRootWindowController()->GetShelfLayoutManager();
161   shelf->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
162   shelf->UpdateAutoHideState();
163   ASSERT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf->auto_hide_state());
164
165   // Tooltip visibility change for auto hide may take time.
166   EXPECT_TRUE(TooltipIsVisible());
167   RunAllPendingInMessageLoop();
168   EXPECT_FALSE(TooltipIsVisible());
169
170   // Do not show the view if the shelf is hidden.
171   ShowImmediately();
172   EXPECT_FALSE(TooltipIsVisible());
173
174   // ShowDelayed doesn't even run the timer for the hidden shelf.
175   ShowDelayed();
176   EXPECT_FALSE(IsTimerRunning());
177 }
178
179 TEST_F(ShelfTooltipManagerTest, ShouldHideForEvents) {
180   ShowImmediately();
181   ASSERT_TRUE(TooltipIsVisible());
182
183   aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
184   ui::EventHandler* event_handler = GetEventHandler();
185
186   // Should not hide for key events.
187   ui::KeyEvent key_event(ui::ET_KEY_PRESSED, ui::VKEY_A, ui::EF_NONE, false);
188   SetEventTarget(root_window, &key_event);
189   event_handler->OnKeyEvent(&key_event);
190   EXPECT_FALSE(key_event.handled());
191   EXPECT_TRUE(TooltipIsVisible());
192
193   // Should hide for touch events.
194   ui::TouchEvent touch_event(
195       ui::ET_TOUCH_PRESSED, gfx::Point(), 0, base::TimeDelta());
196   SetEventTarget(root_window, &touch_event);
197   event_handler->OnTouchEvent(&touch_event);
198   EXPECT_FALSE(touch_event.handled());
199   EXPECT_FALSE(TooltipIsVisible());
200
201   // Shouldn't hide if the touch happens on the tooltip.
202   ShowImmediately();
203   views::Widget* tooltip_widget = GetTooltipWidget();
204   SetEventTarget(tooltip_widget->GetNativeWindow(), &touch_event);
205   event_handler->OnTouchEvent(&touch_event);
206   EXPECT_FALSE(touch_event.handled());
207   EXPECT_TRUE(TooltipIsVisible());
208
209   // Should hide for gesture events.
210   ui::GestureEvent gesture_event(
211       ui::ET_GESTURE_BEGIN, 0, 0, ui::EF_NONE,
212       base::TimeDelta::FromMilliseconds(base::Time::Now().ToDoubleT() * 1000),
213       ui::GestureEventDetails(ui::ET_GESTURE_BEGIN, 0.0f, 0.0f), 0);
214   SetEventTarget(tooltip_widget->GetNativeWindow(), &gesture_event);
215   event_handler->OnGestureEvent(&gesture_event);
216   EXPECT_FALSE(gesture_event.handled());
217   RunAllPendingInMessageLoop();
218   EXPECT_FALSE(TooltipIsVisible());
219 }
220
221 TEST_F(ShelfTooltipManagerTest, HideForMouseMoveEvent) {
222   ShowImmediately();
223   ASSERT_TRUE(TooltipIsVisible());
224
225   aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
226   ui::EventHandler* event_handler = GetEventHandler();
227
228   gfx::Rect tooltip_rect = GetTooltipWidget()->GetNativeWindow()->bounds();
229   ASSERT_FALSE(tooltip_rect.IsEmpty());
230
231   // Shouldn't hide if the mouse is in the tooltip.
232   ui::MouseEvent mouse_event(ui::ET_MOUSE_MOVED, tooltip_rect.CenterPoint(),
233                              tooltip_rect.CenterPoint(), ui::EF_NONE);
234   ui::LocatedEvent::TestApi test_api(&mouse_event);
235
236   SetEventTarget(root_window, &mouse_event);
237   event_handler->OnMouseEvent(&mouse_event);
238   EXPECT_FALSE(mouse_event.handled());
239   EXPECT_TRUE(TooltipIsVisible());
240
241   // Should hide if the mouse is out of the tooltip.
242   test_api.set_location(tooltip_rect.origin() + gfx::Vector2d(-1, -1));
243   event_handler->OnMouseEvent(&mouse_event);
244   EXPECT_FALSE(mouse_event.handled());
245   RunAllPendingInMessageLoop();
246   EXPECT_FALSE(TooltipIsVisible());
247 }
248
249 // Checks that tooltip is hidden when mouse is pressed in anywhere.
250 TEST_F(ShelfTooltipManagerTest, HideForMouseClickEvent) {
251   ShowImmediately();
252   ASSERT_TRUE(TooltipIsVisible());
253
254   aura::Window* root_window = Shell::GetInstance()->GetPrimaryRootWindow();
255   ui::EventHandler* event_handler = GetEventHandler();
256
257   gfx::Rect tooltip_rect = GetTooltipWidget()->GetNativeWindow()->bounds();
258   ASSERT_FALSE(tooltip_rect.IsEmpty());
259
260   // Should hide if the mouse is pressed in the tooltip.
261   ui::MouseEvent mouse_event(ui::ET_MOUSE_PRESSED, tooltip_rect.CenterPoint(),
262                              tooltip_rect.CenterPoint(), ui::EF_NONE);
263
264   SetEventTarget(root_window, &mouse_event);
265   event_handler->OnMouseEvent(&mouse_event);
266   EXPECT_FALSE(mouse_event.handled());
267   RunAllPendingInMessageLoop();
268   EXPECT_FALSE(TooltipIsVisible());
269
270   // Should hide if the mouse is pressed outside of the tooltip.
271   ShowImmediately();
272   ASSERT_TRUE(TooltipIsVisible());
273
274   ui::LocatedEvent::TestApi test_api(&mouse_event);
275   test_api.set_location(tooltip_rect.origin() + gfx::Vector2d(-1, -1));
276
277   SetEventTarget(root_window, &mouse_event);
278   event_handler->OnMouseEvent(&mouse_event);
279   EXPECT_FALSE(mouse_event.handled());
280   RunAllPendingInMessageLoop();
281   EXPECT_FALSE(TooltipIsVisible());
282 }
283
284 }  // namespace test
285 }  // namespace ash