Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(445)

Side by Side Diff: ash/common/shelf/shelf_tooltip_manager_unittest.cc

Issue 2196933002: mash: Migrate shelf tooltip manager tests to ash/common. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mash: Migrate shelf tooltip manager tests to ash/common. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « ash/common/shelf/DEPS ('k') | ash/shelf/shelf_tooltip_manager_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 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 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/common/shelf/shelf_tooltip_manager.h" 5 #include "ash/common/shelf/shelf_tooltip_manager.h"
6 6
7 #include "ash/common/shelf/app_list_button.h" 7 #include "ash/common/shelf/app_list_button.h"
8 #include "ash/common/shelf/shelf_model.h" 8 #include "ash/common/shelf/shelf_model.h"
9 #include "ash/common/shelf/wm_shelf.h" 9 #include "ash/common/shelf/wm_shelf.h"
10 #include "ash/common/wm_shell.h" 10 #include "ash/common/shell_window_ids.h"
James Cook 2016/08/01 16:58:03 nit: remove
msw 2016/08/01 17:25:46 Done.
11 #include "ash/shelf/shelf_view.h" 11 #include "ash/shelf/shelf_view.h"
12 #include "ash/shell.h"
13 #include "ash/test/ash_test_base.h" 12 #include "ash/test/ash_test_base.h"
14 #include "ash/test/shelf_test_api.h"
15 #include "ash/test/shelf_view_test_api.h" 13 #include "ash/test/shelf_view_test_api.h"
16 #include "ash/test/test_shelf_item_delegate.h" 14 #include "ash/test/test_shelf_item_delegate.h"
17 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
18 #include "ui/events/event_constants.h" 16 #include "ui/events/event_constants.h"
19 #include "ui/events/test/event_generator.h" 17 #include "ui/events/test/event_generator.h"
20 #include "ui/views/bubble/bubble_dialog_delegate.h" 18 #include "ui/views/bubble/bubble_dialog_delegate.h"
21 #include "ui/views/widget/widget.h" 19 #include "ui/views/widget/widget.h"
22 20
23 namespace ash { 21 namespace ash {
24 namespace test { 22 namespace test {
25 23
26 class ShelfTooltipManagerTest : public AshTestBase { 24 class ShelfTooltipManagerTest : public AshTestBase {
27 public: 25 public:
28 ShelfTooltipManagerTest() {} 26 ShelfTooltipManagerTest() {}
29 ~ShelfTooltipManagerTest() override {} 27 ~ShelfTooltipManagerTest() override {}
30 28
31 void SetUp() override { 29 void SetUp() override {
32 AshTestBase::SetUp(); 30 AshTestBase::SetUp();
33 shelf_ = Shelf::ForPrimaryDisplay(); 31 shelf_view_ = GetPrimaryShelf()->GetShelfViewForTesting();
34 shelf_view_ = test::ShelfTestAPI(shelf_).shelf_view();
35 tooltip_manager_ = test::ShelfViewTestAPI(shelf_view_).tooltip_manager(); 32 tooltip_manager_ = test::ShelfViewTestAPI(shelf_view_).tooltip_manager();
36 tooltip_manager_->set_timer_delay_for_test(0); 33 tooltip_manager_->set_timer_delay_for_test(0);
37 } 34 }
38 35
39 bool IsTimerRunning() { return tooltip_manager_->timer_.IsRunning(); } 36 bool IsTimerRunning() { return tooltip_manager_->timer_.IsRunning(); }
40 views::Widget* GetTooltip() { return tooltip_manager_->bubble_->GetWidget(); } 37 views::Widget* GetTooltip() { return tooltip_manager_->bubble_->GetWidget(); }
41 38
39 std::unique_ptr<views::Widget> CreateTestWidget() {
James Cook 2016/08/01 16:58:03 Odd that AshTestBase::CreateTestWidget doesn't wor
msw 2016/08/01 17:25:46 Acknowledged. I can look separately.
40 std::unique_ptr<views::Widget> widget = base::MakeUnique<views::Widget>();
41 views::Widget::InitParams params;
42 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
43 params.context = CurrentContext();
44 widget->Init(params);
45 widget->Show();
46 return widget;
47 }
48
42 protected: 49 protected:
43 Shelf* shelf_;
44 ShelfView* shelf_view_; 50 ShelfView* shelf_view_;
45 ShelfTooltipManager* tooltip_manager_; 51 ShelfTooltipManager* tooltip_manager_;
46 52
47 private: 53 private:
48 DISALLOW_COPY_AND_ASSIGN(ShelfTooltipManagerTest); 54 DISALLOW_COPY_AND_ASSIGN(ShelfTooltipManagerTest);
49 }; 55 };
50 56
51 TEST_F(ShelfTooltipManagerTest, ShowTooltip) { 57 TEST_F(ShelfTooltipManagerTest, ShowTooltip) {
52 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 58 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
53 EXPECT_TRUE(tooltip_manager_->IsVisible()); 59 EXPECT_TRUE(tooltip_manager_->IsVisible());
54 EXPECT_FALSE(IsTimerRunning()); 60 EXPECT_FALSE(IsTimerRunning());
55 } 61 }
56 62
57 TEST_F(ShelfTooltipManagerTest, ShowTooltipWithDelay) { 63 TEST_F(ShelfTooltipManagerTest, ShowTooltipWithDelay) {
58 // ShowTooltipWithDelay should start the timer instead of showing immediately. 64 // ShowTooltipWithDelay should start the timer instead of showing immediately.
59 tooltip_manager_->ShowTooltipWithDelay(shelf_->GetAppListButton()); 65 tooltip_manager_->ShowTooltipWithDelay(shelf_view_->GetAppListButton());
60 EXPECT_FALSE(tooltip_manager_->IsVisible()); 66 EXPECT_FALSE(tooltip_manager_->IsVisible());
61 EXPECT_TRUE(IsTimerRunning()); 67 EXPECT_TRUE(IsTimerRunning());
62 // TODO: Test that the delayed tooltip is shown, without flaky failures. 68 // TODO: Test that the delayed tooltip is shown, without flaky failures.
63 } 69 }
64 70
65 TEST_F(ShelfTooltipManagerTest, DoNotShowForInvalidView) { 71 TEST_F(ShelfTooltipManagerTest, DoNotShowForInvalidView) {
66 // The manager should not show or start the timer for a null view. 72 // The manager should not show or start the timer for a null view.
67 tooltip_manager_->ShowTooltip(nullptr); 73 tooltip_manager_->ShowTooltip(nullptr);
68 EXPECT_FALSE(tooltip_manager_->IsVisible()); 74 EXPECT_FALSE(tooltip_manager_->IsVisible());
69 tooltip_manager_->ShowTooltipWithDelay(nullptr); 75 tooltip_manager_->ShowTooltipWithDelay(nullptr);
70 EXPECT_FALSE(IsTimerRunning()); 76 EXPECT_FALSE(IsTimerRunning());
71 77
72 // The manager should not show or start the timer for a non-shelf view. 78 // The manager should not show or start the timer for a non-shelf view.
73 views::View view; 79 views::View view;
74 tooltip_manager_->ShowTooltip(&view); 80 tooltip_manager_->ShowTooltip(&view);
75 EXPECT_FALSE(tooltip_manager_->IsVisible()); 81 EXPECT_FALSE(tooltip_manager_->IsVisible());
76 tooltip_manager_->ShowTooltipWithDelay(&view); 82 tooltip_manager_->ShowTooltipWithDelay(&view);
77 EXPECT_FALSE(IsTimerRunning()); 83 EXPECT_FALSE(IsTimerRunning());
78 84
79 // The manager should start the timer for a view on the shelf. 85 // The manager should start the timer for a view on the shelf.
80 ShelfModel* model = WmShell::Get()->shelf_model(); 86 ShelfModel* model = shelf_view_->model();
81 ShelfItem item; 87 ShelfItem item;
82 item.type = TYPE_APP_SHORTCUT; 88 item.type = TYPE_APP_SHORTCUT;
83 const int index = model->Add(item); 89 const int index = model->Add(item);
84 const ShelfID id = model->items()[index].id; 90 const ShelfID id = model->items()[index].id;
85 model->SetShelfItemDelegate( 91 model->SetShelfItemDelegate(id,
86 id, base::WrapUnique(new TestShelfItemDelegate(nullptr))); 92 base::MakeUnique<TestShelfItemDelegate>(nullptr));
87 // Note: There's no easy way to correlate shelf a model index/id to its view. 93 // Note: There's no easy way to correlate shelf a model index/id to its view.
88 tooltip_manager_->ShowTooltipWithDelay( 94 tooltip_manager_->ShowTooltipWithDelay(
89 shelf_view_->child_at(shelf_view_->child_count() - 1)); 95 shelf_view_->child_at(shelf_view_->child_count() - 1));
90 EXPECT_TRUE(IsTimerRunning()); 96 EXPECT_TRUE(IsTimerRunning());
91 97
92 // Removing the view won't stop the timer, but the tooltip shouldn't be shown. 98 // Removing the view won't stop the timer, but the tooltip shouldn't be shown.
93 model->RemoveItemAt(index); 99 model->RemoveItemAt(index);
94 EXPECT_TRUE(IsTimerRunning()); 100 EXPECT_TRUE(IsTimerRunning());
95 RunAllPendingInMessageLoop(); 101 RunAllPendingInMessageLoop();
96 EXPECT_FALSE(IsTimerRunning()); 102 EXPECT_FALSE(IsTimerRunning());
97 EXPECT_FALSE(tooltip_manager_->IsVisible()); 103 EXPECT_FALSE(tooltip_manager_->IsVisible());
98 } 104 }
99 105
100 TEST_F(ShelfTooltipManagerTest, HideWhenShelfIsHidden) { 106 TEST_F(ShelfTooltipManagerTest, HideWhenShelfIsHidden) {
101 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 107 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
102 ASSERT_TRUE(tooltip_manager_->IsVisible()); 108 ASSERT_TRUE(tooltip_manager_->IsVisible());
103 109
104 // Create a full-screen window to hide the shelf. 110 // Create a full-screen window to hide the shelf.
105 std::unique_ptr<views::Widget> widget(new views::Widget); 111 std::unique_ptr<views::Widget> widget = CreateTestWidget();
106 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
107 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
108 params.context = CurrentContext();
109 widget->Init(params);
110 widget->SetFullscreen(true); 112 widget->SetFullscreen(true);
111 widget->Show();
112 113
113 // Once the shelf is hidden, the tooltip should be invisible. 114 // Once the shelf is hidden, the tooltip should be invisible.
114 ASSERT_EQ(SHELF_HIDDEN, GetPrimaryShelf()->GetVisibilityState()); 115 ASSERT_EQ(SHELF_HIDDEN, GetPrimaryShelf()->GetVisibilityState());
115 EXPECT_FALSE(tooltip_manager_->IsVisible()); 116 EXPECT_FALSE(tooltip_manager_->IsVisible());
116 117
117 // Do not show the view if the shelf is hidden. 118 // Do not show the view if the shelf is hidden.
118 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 119 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
119 EXPECT_FALSE(tooltip_manager_->IsVisible()); 120 EXPECT_FALSE(tooltip_manager_->IsVisible());
120 121
121 // ShowTooltipWithDelay doesn't even start the timer for the hidden shelf. 122 // ShowTooltipWithDelay doesn't even start the timer for the hidden shelf.
122 tooltip_manager_->ShowTooltipWithDelay(shelf_->GetAppListButton()); 123 tooltip_manager_->ShowTooltipWithDelay(shelf_view_->GetAppListButton());
123 EXPECT_FALSE(IsTimerRunning()); 124 EXPECT_FALSE(IsTimerRunning());
124 } 125 }
125 126
126 TEST_F(ShelfTooltipManagerTest, HideWhenShelfIsAutoHide) { 127 TEST_F(ShelfTooltipManagerTest, HideWhenShelfIsAutoHide) {
127 // Create a visible window so auto-hide behavior is enforced. 128 // Create a visible window so auto-hide behavior is enforced.
128 views::Widget* dummy = new views::Widget; 129 std::unique_ptr<views::Widget> widget = CreateTestWidget();
129 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW); 130 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
130 params.bounds = gfx::Rect(0, 0, 200, 200);
131 params.context = CurrentContext();
132 dummy->Init(params);
133 dummy->Show();
134
135 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton());
136 ASSERT_TRUE(tooltip_manager_->IsVisible()); 131 ASSERT_TRUE(tooltip_manager_->IsVisible());
137 132
138 GetPrimaryShelf()->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS); 133 GetPrimaryShelf()->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
139 GetPrimaryShelf()->UpdateAutoHideState(); 134 GetPrimaryShelf()->UpdateAutoHideState();
140 ASSERT_EQ(SHELF_AUTO_HIDE_HIDDEN, GetPrimaryShelf()->GetAutoHideState()); 135 ASSERT_EQ(SHELF_AUTO_HIDE_HIDDEN, GetPrimaryShelf()->GetAutoHideState());
141 136
142 // Tooltip visibility change for auto hide may take time. 137 // Tooltip visibility change for auto hide may take time.
143 EXPECT_TRUE(tooltip_manager_->IsVisible()); 138 EXPECT_TRUE(tooltip_manager_->IsVisible());
144 RunAllPendingInMessageLoop(); 139 RunAllPendingInMessageLoop();
145 EXPECT_FALSE(tooltip_manager_->IsVisible()); 140 EXPECT_FALSE(tooltip_manager_->IsVisible());
146 141
147 // Do not show the view if the shelf is hidden. 142 // Do not show the view if the shelf is hidden.
148 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 143 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
149 EXPECT_FALSE(tooltip_manager_->IsVisible()); 144 EXPECT_FALSE(tooltip_manager_->IsVisible());
150 145
151 // ShowTooltipWithDelay doesn't even run the timer for the hidden shelf. 146 // ShowTooltipWithDelay doesn't even run the timer for the hidden shelf.
152 tooltip_manager_->ShowTooltipWithDelay(shelf_->GetAppListButton()); 147 tooltip_manager_->ShowTooltipWithDelay(shelf_view_->GetAppListButton());
153 EXPECT_FALSE(IsTimerRunning()); 148 EXPECT_FALSE(IsTimerRunning());
154 } 149 }
155 150
156 TEST_F(ShelfTooltipManagerTest, HideForEvents) { 151 TEST_F(ShelfTooltipManagerTest, HideForEvents) {
157 ui::test::EventGenerator& generator = GetEventGenerator(); 152 ui::test::EventGenerator& generator = GetEventGenerator();
158 gfx::Rect shelf_bounds = shelf_view_->GetBoundsInScreen(); 153 gfx::Rect shelf_bounds = shelf_view_->GetBoundsInScreen();
159 154
160 // Should hide if the mouse exits the shelf area. 155 // Should hide if the mouse exits the shelf area.
161 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 156 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
162 ASSERT_TRUE(tooltip_manager_->IsVisible()); 157 ASSERT_TRUE(tooltip_manager_->IsVisible());
163 generator.MoveMouseTo(shelf_bounds.CenterPoint()); 158 generator.MoveMouseTo(shelf_bounds.CenterPoint());
164 generator.SendMouseExit(); 159 generator.SendMouseExit();
165 EXPECT_FALSE(tooltip_manager_->IsVisible()); 160 EXPECT_FALSE(tooltip_manager_->IsVisible());
166 161
167 // Should hide if the mouse is pressed in the shelf area. 162 // Should hide if the mouse is pressed in the shelf area.
168 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 163 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
169 ASSERT_TRUE(tooltip_manager_->IsVisible()); 164 ASSERT_TRUE(tooltip_manager_->IsVisible());
170 generator.MoveMouseTo(shelf_bounds.CenterPoint()); 165 generator.MoveMouseTo(shelf_bounds.CenterPoint());
171 generator.PressLeftButton(); 166 generator.PressLeftButton();
172 EXPECT_FALSE(tooltip_manager_->IsVisible()); 167 EXPECT_FALSE(tooltip_manager_->IsVisible());
173 168
174 // Should hide for touch events in the shelf. 169 // Should hide for touch events in the shelf.
175 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 170 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
176 ASSERT_TRUE(tooltip_manager_->IsVisible()); 171 ASSERT_TRUE(tooltip_manager_->IsVisible());
177 generator.set_current_location(shelf_bounds.CenterPoint()); 172 generator.set_current_location(shelf_bounds.CenterPoint());
178 generator.PressTouch(); 173 generator.PressTouch();
179 EXPECT_FALSE(tooltip_manager_->IsVisible()); 174 EXPECT_FALSE(tooltip_manager_->IsVisible());
180 175
181 // Should hide for gesture events in the shelf. 176 // Should hide for gesture events in the shelf.
182 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 177 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
183 ASSERT_TRUE(tooltip_manager_->IsVisible()); 178 ASSERT_TRUE(tooltip_manager_->IsVisible());
184 generator.GestureTapDownAndUp(shelf_bounds.CenterPoint()); 179 generator.GestureTapDownAndUp(shelf_bounds.CenterPoint());
185 EXPECT_FALSE(tooltip_manager_->IsVisible()); 180 EXPECT_FALSE(tooltip_manager_->IsVisible());
186 } 181 }
187 182
188 TEST_F(ShelfTooltipManagerTest, HideForExternalEvents) { 183 TEST_F(ShelfTooltipManagerTest, HideForExternalEvents) {
189 ui::test::EventGenerator& generator = GetEventGenerator(); 184 ui::test::EventGenerator& generator = GetEventGenerator();
190 185
191 // Should hide for touches outside the shelf. 186 // Should hide for touches outside the shelf.
192 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 187 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
193 ASSERT_TRUE(tooltip_manager_->IsVisible()); 188 ASSERT_TRUE(tooltip_manager_->IsVisible());
194 generator.set_current_location(gfx::Point()); 189 generator.set_current_location(gfx::Point());
195 generator.PressTouch(); 190 generator.PressTouch();
196 EXPECT_FALSE(tooltip_manager_->IsVisible()); 191 EXPECT_FALSE(tooltip_manager_->IsVisible());
197 generator.ReleaseTouch(); 192 generator.ReleaseTouch();
198 193
199 // Should hide for touch events on the tooltip. 194 // Should hide for touch events on the tooltip.
200 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 195 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
201 ASSERT_TRUE(tooltip_manager_->IsVisible()); 196 ASSERT_TRUE(tooltip_manager_->IsVisible());
202 generator.set_current_location( 197 generator.set_current_location(
203 GetTooltip()->GetWindowBoundsInScreen().CenterPoint()); 198 GetTooltip()->GetWindowBoundsInScreen().CenterPoint());
204 generator.PressTouch(); 199 generator.PressTouch();
205 EXPECT_FALSE(tooltip_manager_->IsVisible()); 200 EXPECT_FALSE(tooltip_manager_->IsVisible());
206 generator.ReleaseTouch(); 201 generator.ReleaseTouch();
207 202
208 // Should hide for gestures outside the shelf. 203 // Should hide for gestures outside the shelf.
209 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 204 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
210 ASSERT_TRUE(tooltip_manager_->IsVisible()); 205 ASSERT_TRUE(tooltip_manager_->IsVisible());
211 generator.GestureTapDownAndUp(gfx::Point()); 206 generator.GestureTapDownAndUp(gfx::Point());
212 EXPECT_FALSE(tooltip_manager_->IsVisible()); 207 EXPECT_FALSE(tooltip_manager_->IsVisible());
213 } 208 }
214 209
215 TEST_F(ShelfTooltipManagerTest, DoNotHideForKeyEvents) { 210 TEST_F(ShelfTooltipManagerTest, DoNotHideForKeyEvents) {
216 ui::test::EventGenerator& generator = GetEventGenerator(); 211 ui::test::EventGenerator& generator = GetEventGenerator();
217 212
218 // Should not hide for key events. 213 // Should not hide for key events.
219 tooltip_manager_->ShowTooltip(shelf_->GetAppListButton()); 214 tooltip_manager_->ShowTooltip(shelf_view_->GetAppListButton());
220 ASSERT_TRUE(tooltip_manager_->IsVisible()); 215 ASSERT_TRUE(tooltip_manager_->IsVisible());
221 generator.PressKey(ui::VKEY_A, ui::EF_NONE); 216 generator.PressKey(ui::VKEY_A, ui::EF_NONE);
222 EXPECT_TRUE(tooltip_manager_->IsVisible()); 217 EXPECT_TRUE(tooltip_manager_->IsVisible());
223 } 218 }
224 219
225 } // namespace test 220 } // namespace test
226 } // namespace ash 221 } // namespace ash
OLDNEW
« no previous file with comments | « ash/common/shelf/DEPS ('k') | ash/shelf/shelf_tooltip_manager_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698