summaryrefslogtreecommitdiffstats
path: root/ash/shelf/shelf_tooltip_manager_unittest.cc
blob: 3f2b2e884155399b0ed76938c1c737b54a04d9b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/shelf/shelf_tooltip_manager.h"

#include "ash/shelf/shelf_layout_manager.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/shelf_test_api.h"
#include "ash/test/shelf_view_test_api.h"
#include "ui/events/event_constants.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/events/test/event_generator.h"
#include "ui/views/bubble/bubble_delegate.h"
#include "ui/views/widget/widget.h"

namespace ash {
namespace test {

class ShelfTooltipManagerTest : public AshTestBase {
 public:
  ShelfTooltipManagerTest() {}
  ~ShelfTooltipManagerTest() override {}

  void SetUp() override {
    AshTestBase::SetUp();
    shelf_ = Shelf::ForPrimaryDisplay();
    ShelfView* shelf_view = test::ShelfTestAPI(shelf_).shelf_view();
    tooltip_manager_ = test::ShelfViewTestAPI(shelf_view).tooltip_manager();
  }

  void ShowDelayed() {
    CreateWidget();
    tooltip_manager_->ShowTooltipWithDelay(dummy_anchor_.get());
  }

  void ShowImmediately() {
    CreateWidget();
    tooltip_manager_->ShowTooltip(dummy_anchor_.get());
  }

  bool TooltipIsVisible() { return tooltip_manager_->IsVisible(); }
  bool IsTimerRunning() { return tooltip_manager_->timer_.IsRunning(); }

  aura::Window* GetTooltipWindow() {
    return tooltip_manager_->bubble_->GetWidget()->GetNativeWindow();
  }

 protected:
  scoped_ptr<views::Widget> widget_;
  scoped_ptr<views::View> dummy_anchor_;

  Shelf* shelf_;
  ShelfTooltipManager* tooltip_manager_;

 private:
  void CreateWidget() {
    dummy_anchor_.reset(new views::View);
    widget_.reset(new views::Widget);
    views::Widget::InitParams params(
        views::Widget::InitParams::TYPE_WINDOW_FRAMELESS);
    params.opacity = views::Widget::InitParams::TRANSLUCENT_WINDOW;
    params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
    params.parent = Shell::GetContainer(Shell::GetPrimaryRootWindow(),
                                        ash::kShellWindowId_ShelfContainer);
    widget_->Init(params);
    widget_->SetContentsView(dummy_anchor_.get());
  }

  DISALLOW_COPY_AND_ASSIGN(ShelfTooltipManagerTest);
};

TEST_F(ShelfTooltipManagerTest, ShowingBasics) {
  // ShowDelayed() should just start the timer instead of showing immediately.
  ShowDelayed();
  EXPECT_FALSE(TooltipIsVisible());
  EXPECT_TRUE(IsTimerRunning());

  ShowImmediately();
  EXPECT_TRUE(TooltipIsVisible());
  EXPECT_FALSE(IsTimerRunning());
}

TEST_F(ShelfTooltipManagerTest, HideWhenShelfIsHidden) {
  ShowImmediately();
  ASSERT_TRUE(TooltipIsVisible());

  // Create a full-screen window to hide the shelf.
  scoped_ptr<views::Widget> widget(new views::Widget);
  views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
  params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
  params.context = CurrentContext();
  widget->Init(params);
  widget->SetFullscreen(true);
  widget->Show();

  // Once the shelf is hidden, the tooltip should be invisible.
  ASSERT_EQ(SHELF_HIDDEN, shelf_->shelf_layout_manager()->visibility_state());
  EXPECT_FALSE(TooltipIsVisible());

  // Do not show the view if the shelf is hidden.
  ShowImmediately();
  EXPECT_FALSE(TooltipIsVisible());

  // ShowDelayed() doesn't even start the timer for the hidden shelf.
  ShowDelayed();
  EXPECT_FALSE(IsTimerRunning());
}

TEST_F(ShelfTooltipManagerTest, HideWhenShelfIsAutoHide) {
  // Create a visible window so auto-hide behavior is enforced.
  views::Widget* dummy = new views::Widget;
  views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
  params.bounds = gfx::Rect(0, 0, 200, 200);
  params.context = CurrentContext();
  dummy->Init(params);
  dummy->Show();

  ShowImmediately();
  ASSERT_TRUE(TooltipIsVisible());

  ShelfLayoutManager* shelf_layout_manager = shelf_->shelf_layout_manager();
  shelf_layout_manager->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
  shelf_layout_manager->UpdateAutoHideState();
  ASSERT_EQ(SHELF_AUTO_HIDE_HIDDEN, shelf_layout_manager->auto_hide_state());

  // Tooltip visibility change for auto hide may take time.
  EXPECT_TRUE(TooltipIsVisible());
  RunAllPendingInMessageLoop();
  EXPECT_FALSE(TooltipIsVisible());

  // Do not show the view if the shelf is hidden.
  ShowImmediately();
  EXPECT_FALSE(TooltipIsVisible());

  // ShowDelayed doesn't even run the timer for the hidden shelf.
  ShowDelayed();
  EXPECT_FALSE(IsTimerRunning());
}

TEST_F(ShelfTooltipManagerTest, HideForEvents) {
  ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());
  gfx::Rect shelf_bounds = shelf_->shelf_widget()->GetNativeWindow()->bounds();

  // Should hide if the mouse exits the shelf area.
  ShowImmediately();
  ASSERT_TRUE(TooltipIsVisible());
  generator.MoveMouseTo(shelf_bounds.CenterPoint());
  generator.SendMouseExit();
  EXPECT_FALSE(TooltipIsVisible());

  // Should hide if the mouse is pressed in the shelf area.
  ShowImmediately();
  ASSERT_TRUE(TooltipIsVisible());
  generator.MoveMouseTo(shelf_bounds.CenterPoint());
  generator.PressLeftButton();
  EXPECT_FALSE(TooltipIsVisible());

  // Should hide for touch events in the shelf.
  ShowImmediately();
  ASSERT_TRUE(TooltipIsVisible());
  generator.set_current_location(shelf_bounds.CenterPoint());
  generator.PressTouch();
  EXPECT_FALSE(TooltipIsVisible());

  // Should hide for gesture events in the shelf.
  ShowImmediately();
  ASSERT_TRUE(TooltipIsVisible());
  generator.GestureTapDownAndUp(shelf_bounds.CenterPoint());
  EXPECT_FALSE(TooltipIsVisible());
}

// TODO(msw): Hiding for touch and gesture events outside the shelf is broken.
TEST_F(ShelfTooltipManagerTest, HideForEventsBroken) {
  ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());

  ShowImmediately();
  ASSERT_TRUE(TooltipIsVisible());

  generator.set_current_location(gfx::Point());
  generator.PressTouch();
  EXPECT_TRUE(TooltipIsVisible());

  generator.GestureTapDownAndUp(gfx::Point());
  EXPECT_TRUE(TooltipIsVisible());
}

TEST_F(ShelfTooltipManagerTest, DoNotHideForEvents) {
  ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow());

  ShowImmediately();
  ASSERT_TRUE(TooltipIsVisible());

  // Should not hide for key events.
  generator.PressKey(ui::VKEY_A, ui::EF_NONE);
  EXPECT_TRUE(TooltipIsVisible());

  // Should not hide for touch events on the tooltip.
  generator.set_current_location(GetTooltipWindow()->bounds().CenterPoint());
  generator.PressTouch();
  EXPECT_TRUE(TooltipIsVisible());
}

}  // namespace test
}  // namespace ash