Cast: Stop logging kVideoFrameSentToEncoder and rename a couple events.
[chromium-blink-merge.git] / ash / shelf / shelf_view_unittest.cc
blob91e54fede75beca29bd5777d8bc02050f8241489
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
3 // found in the LICENSE file.
5 #include "ash/shelf/shelf_view.h"
7 #include <algorithm>
8 #include <vector>
10 #include "ash/ash_switches.h"
11 #include "ash/root_window_controller.h"
12 #include "ash/shelf/overflow_bubble.h"
13 #include "ash/shelf/overflow_bubble_view.h"
14 #include "ash/shelf/shelf.h"
15 #include "ash/shelf/shelf_button.h"
16 #include "ash/shelf/shelf_constants.h"
17 #include "ash/shelf/shelf_icon_observer.h"
18 #include "ash/shelf/shelf_item_delegate_manager.h"
19 #include "ash/shelf/shelf_layout_manager.h"
20 #include "ash/shelf/shelf_model.h"
21 #include "ash/shelf/shelf_tooltip_manager.h"
22 #include "ash/shelf/shelf_widget.h"
23 #include "ash/shell.h"
24 #include "ash/shell_window_ids.h"
25 #include "ash/test/ash_test_base.h"
26 #include "ash/test/overflow_bubble_view_test_api.h"
27 #include "ash/test/shelf_test_api.h"
28 #include "ash/test/shelf_view_test_api.h"
29 #include "ash/test/shell_test_api.h"
30 #include "ash/test/test_shelf_delegate.h"
31 #include "ash/test/test_shelf_item_delegate.h"
32 #include "ash/wm/coordinate_conversion.h"
33 #include "base/basictypes.h"
34 #include "base/command_line.h"
35 #include "base/compiler_specific.h"
36 #include "base/memory/scoped_ptr.h"
37 #include "base/strings/string_number_conversions.h"
38 #include "grit/ash_resources.h"
39 #include "ui/aura/test/aura_test_base.h"
40 #include "ui/aura/test/event_generator.h"
41 #include "ui/aura/window.h"
42 #include "ui/aura/window_event_dispatcher.h"
43 #include "ui/base/l10n/l10n_util.h"
44 #include "ui/compositor/layer.h"
45 #include "ui/events/event.h"
46 #include "ui/events/event_constants.h"
47 #include "ui/views/view_model.h"
48 #include "ui/views/widget/widget.h"
49 #include "ui/views/widget/widget_delegate.h"
51 namespace ash {
52 namespace test {
54 ////////////////////////////////////////////////////////////////////////////////
55 // ShelfIconObserver tests.
57 class TestShelfIconObserver : public ShelfIconObserver {
58 public:
59 explicit TestShelfIconObserver(Shelf* shelf)
60 : shelf_(shelf),
61 change_notified_(false) {
62 if (shelf_)
63 shelf_->AddIconObserver(this);
66 virtual ~TestShelfIconObserver() {
67 if (shelf_)
68 shelf_->RemoveIconObserver(this);
71 // ShelfIconObserver implementation.
72 virtual void OnShelfIconPositionsChanged() OVERRIDE {
73 change_notified_ = true;
76 int change_notified() const { return change_notified_; }
77 void Reset() { change_notified_ = false; }
79 private:
80 Shelf* shelf_;
81 bool change_notified_;
83 DISALLOW_COPY_AND_ASSIGN(TestShelfIconObserver);
86 class ShelfViewIconObserverTest : public AshTestBase {
87 public:
88 ShelfViewIconObserverTest() {}
89 virtual ~ShelfViewIconObserverTest() {}
91 virtual void SetUp() OVERRIDE {
92 AshTestBase::SetUp();
93 Shelf* shelf = Shelf::ForPrimaryDisplay();
94 observer_.reset(new TestShelfIconObserver(shelf));
96 shelf_view_test_.reset(
97 new ShelfViewTestAPI(ShelfTestAPI(shelf).shelf_view()));
98 shelf_view_test_->SetAnimationDuration(1);
101 virtual void TearDown() OVERRIDE {
102 observer_.reset();
103 AshTestBase::TearDown();
106 TestShelfIconObserver* observer() { return observer_.get(); }
108 ShelfViewTestAPI* shelf_view_test() {
109 return shelf_view_test_.get();
112 Shelf* ShelfForSecondaryDisplay() {
113 return Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
116 private:
117 scoped_ptr<TestShelfIconObserver> observer_;
118 scoped_ptr<ShelfViewTestAPI> shelf_view_test_;
120 DISALLOW_COPY_AND_ASSIGN(ShelfViewIconObserverTest);
123 TEST_F(ShelfViewIconObserverTest, AddRemove) {
124 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
125 ASSERT_TRUE(shelf_delegate);
127 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
128 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
129 params.bounds = gfx::Rect(0, 0, 200, 200);
130 params.context = CurrentContext();
132 scoped_ptr<views::Widget> widget(new views::Widget());
133 widget->Init(params);
134 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
135 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
136 EXPECT_TRUE(observer()->change_notified());
137 observer()->Reset();
139 widget->Show();
140 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
141 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
142 EXPECT_TRUE(observer()->change_notified());
143 observer()->Reset();
146 // Sometimes fails on trybots on win7_aura. http://crbug.com/177135
147 #if defined(OS_WIN)
148 #define MAYBE_AddRemoveWithMultipleDisplays \
149 DISABLED_AddRemoveWithMultipleDisplays
150 #else
151 #define MAYBE_AddRemoveWithMultipleDisplays \
152 AddRemoveWithMultipleDisplays
153 #endif
154 // Make sure creating/deleting an window on one displays notifies a
155 // shelf on external display as well as one on primary.
156 TEST_F(ShelfViewIconObserverTest, MAYBE_AddRemoveWithMultipleDisplays) {
157 UpdateDisplay("400x400,400x400");
158 TestShelfIconObserver second_observer(ShelfForSecondaryDisplay());
160 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
161 ASSERT_TRUE(shelf_delegate);
163 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
164 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
165 params.bounds = gfx::Rect(0, 0, 200, 200);
166 params.context = CurrentContext();
168 scoped_ptr<views::Widget> widget(new views::Widget());
169 widget->Init(params);
170 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
171 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
172 EXPECT_TRUE(observer()->change_notified());
173 EXPECT_TRUE(second_observer.change_notified());
174 observer()->Reset();
175 second_observer.Reset();
177 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
178 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
179 EXPECT_TRUE(observer()->change_notified());
180 EXPECT_TRUE(second_observer.change_notified());
182 observer()->Reset();
183 second_observer.Reset();
186 TEST_F(ShelfViewIconObserverTest, BoundsChanged) {
187 ShelfWidget* widget = Shell::GetPrimaryRootWindowController()->shelf();
188 Shelf* shelf = Shelf::ForPrimaryDisplay();
189 gfx::Size shelf_size = widget->GetWindowBoundsInScreen().size();
190 shelf_size.set_width(shelf_size.width() / 2);
191 ASSERT_GT(shelf_size.width(), 0);
192 shelf->SetShelfViewBounds(gfx::Rect(shelf_size));
193 // No animation happens for ShelfView bounds change.
194 EXPECT_TRUE(observer()->change_notified());
195 observer()->Reset();
198 ////////////////////////////////////////////////////////////////////////////////
199 // ShelfView tests.
201 // Simple ShelfDelegate implmentation for ShelfViewTest.OverflowBubbleSize
202 // and CheckDragAndDropFromOverflowBubbleToShelf
203 class TestShelfDelegateForShelfView : public ShelfDelegate {
204 public:
205 explicit TestShelfDelegateForShelfView(ShelfModel* model)
206 : model_(model) {}
207 virtual ~TestShelfDelegateForShelfView() {}
209 // ShelfDelegate overrides:
210 virtual void OnShelfCreated(Shelf* shelf) OVERRIDE {}
212 virtual void OnShelfDestroyed(Shelf* shelf) OVERRIDE {}
214 virtual ShelfID GetShelfIDForAppID(const std::string& app_id) OVERRIDE {
215 ShelfID id = 0;
216 EXPECT_TRUE(base::StringToInt(app_id, &id));
217 return id;
220 virtual const std::string& GetAppIDForShelfID(ShelfID id) OVERRIDE {
221 // Use |app_id_| member variable because returning a reference to local
222 // variable is not allowed.
223 app_id_ = base::IntToString(id);
224 return app_id_;
227 virtual void PinAppWithID(const std::string& app_id) OVERRIDE {
230 virtual bool IsAppPinned(const std::string& app_id) OVERRIDE {
231 // Returns true for ShelfViewTest.OverflowBubbleSize. To test ripping off in
232 // that test, an item is already pinned state.
233 return true;
236 virtual bool CanPin() const OVERRIDE {
237 return true;
240 virtual void UnpinAppWithID(const std::string& app_id) OVERRIDE {
241 ShelfID id = 0;
242 EXPECT_TRUE(base::StringToInt(app_id, &id));
243 ASSERT_GT(id, 0);
244 int index = model_->ItemIndexByID(id);
245 ASSERT_GE(index, 0);
247 model_->RemoveItemAt(index);
250 private:
251 ShelfModel* model_;
253 // Temp member variable for returning a value. See the comment in the
254 // GetAppIDForShelfID().
255 std::string app_id_;
257 DISALLOW_COPY_AND_ASSIGN(TestShelfDelegateForShelfView);
260 class ShelfViewTest : public AshTestBase {
261 public:
262 ShelfViewTest() : model_(NULL), shelf_view_(NULL), browser_index_(1) {}
263 virtual ~ShelfViewTest() {}
265 virtual void SetUp() OVERRIDE {
266 AshTestBase::SetUp();
267 test::ShellTestApi test_api(Shell::GetInstance());
268 model_ = test_api.shelf_model();
269 Shelf* shelf = Shelf::ForPrimaryDisplay();
270 shelf_view_ = ShelfTestAPI(shelf).shelf_view();
272 // The bounds should be big enough for 4 buttons + overflow chevron.
273 shelf_view_->SetBounds(0, 0, 500, kShelfSize);
275 test_api_.reset(new ShelfViewTestAPI(shelf_view_));
276 test_api_->SetAnimationDuration(1); // Speeds up animation for test.
278 item_manager_ = Shell::GetInstance()->shelf_item_delegate_manager();
279 DCHECK(item_manager_);
281 // Add browser shortcut shelf item at index 0 for test.
282 AddBrowserShortcut();
285 virtual void TearDown() OVERRIDE {
286 test_api_.reset();
287 AshTestBase::TearDown();
290 protected:
291 void CreateAndSetShelfItemDelegateForID(ShelfID id) {
292 scoped_ptr<ShelfItemDelegate> delegate(new TestShelfItemDelegate(NULL));
293 item_manager_->SetShelfItemDelegate(id, delegate.Pass());
296 ShelfID AddBrowserShortcut() {
297 ShelfItem browser_shortcut;
298 browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
300 ShelfID id = model_->next_id();
301 model_->AddAt(browser_index_, browser_shortcut);
302 CreateAndSetShelfItemDelegateForID(id);
303 test_api_->RunMessageLoopUntilAnimationsDone();
304 return id;
307 ShelfID AddAppShortcut() {
308 ShelfItem item;
309 item.type = TYPE_APP_SHORTCUT;
310 item.status = STATUS_CLOSED;
312 ShelfID id = model_->next_id();
313 model_->Add(item);
314 CreateAndSetShelfItemDelegateForID(id);
315 test_api_->RunMessageLoopUntilAnimationsDone();
316 return id;
319 ShelfID AddPanel() {
320 ShelfID id = AddPanelNoWait();
321 test_api_->RunMessageLoopUntilAnimationsDone();
322 return id;
325 ShelfID AddPlatformAppNoWait() {
326 ShelfItem item;
327 item.type = TYPE_PLATFORM_APP;
328 item.status = STATUS_RUNNING;
330 ShelfID id = model_->next_id();
331 model_->Add(item);
332 CreateAndSetShelfItemDelegateForID(id);
333 return id;
336 ShelfID AddPanelNoWait() {
337 ShelfItem item;
338 item.type = TYPE_APP_PANEL;
339 item.status = STATUS_RUNNING;
341 ShelfID id = model_->next_id();
342 model_->Add(item);
343 CreateAndSetShelfItemDelegateForID(id);
344 return id;
347 ShelfID AddPlatformApp() {
348 ShelfID id = AddPlatformAppNoWait();
349 test_api_->RunMessageLoopUntilAnimationsDone();
350 return id;
353 void RemoveByID(ShelfID id) {
354 model_->RemoveItemAt(model_->ItemIndexByID(id));
355 test_api_->RunMessageLoopUntilAnimationsDone();
358 ShelfButton* GetButtonByID(ShelfID id) {
359 int index = model_->ItemIndexByID(id);
360 return test_api_->GetButton(index);
363 ShelfItem GetItemByID(ShelfID id) {
364 ShelfItems::const_iterator items = model_->ItemByID(id);
365 return *items;
368 void CheckModelIDs(
369 const std::vector<std::pair<ShelfID, views::View*> >& id_map) {
370 size_t map_index = 0;
371 for (size_t model_index = 0;
372 model_index < model_->items().size();
373 ++model_index) {
374 ShelfItem item = model_->items()[model_index];
375 ShelfID id = item.id;
376 EXPECT_EQ(id_map[map_index].first, id);
377 EXPECT_EQ(id_map[map_index].second, GetButtonByID(id));
378 ++map_index;
380 ASSERT_EQ(map_index, id_map.size());
383 void VerifyShelfItemBoundsAreValid() {
384 for (int i=0;i <= test_api_->GetLastVisibleIndex(); ++i) {
385 if (test_api_->GetButton(i)) {
386 gfx::Rect shelf_view_bounds = shelf_view_->GetLocalBounds();
387 gfx::Rect item_bounds = test_api_->GetBoundsByIndex(i);
388 EXPECT_GE(item_bounds.x(), 0);
389 EXPECT_GE(item_bounds.y(), 0);
390 EXPECT_LE(item_bounds.right(), shelf_view_bounds.width());
391 EXPECT_LE(item_bounds.bottom(), shelf_view_bounds.height());
396 views::View* SimulateButtonPressed(ShelfButtonHost::Pointer pointer,
397 int button_index) {
398 ShelfButtonHost* button_host = shelf_view_;
399 views::View* button = test_api_->GetButton(button_index);
400 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED,
401 button->bounds().origin(),
402 button->GetBoundsInScreen().origin(), 0, 0);
403 button_host->PointerPressedOnButton(button, pointer, click_event);
404 return button;
407 views::View* SimulateClick(ShelfButtonHost::Pointer pointer,
408 int button_index) {
409 ShelfButtonHost* button_host = shelf_view_;
410 views::View* button = SimulateButtonPressed(pointer, button_index);
411 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false);
412 return button;
415 views::View* SimulateDrag(ShelfButtonHost::Pointer pointer,
416 int button_index,
417 int destination_index) {
418 ShelfButtonHost* button_host = shelf_view_;
419 views::View* button = SimulateButtonPressed(pointer, button_index);
421 // Drag.
422 views::View* destination = test_api_->GetButton(destination_index);
423 ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED,
424 destination->bounds().origin(),
425 destination->GetBoundsInScreen().origin(), 0, 0);
426 button_host->PointerDraggedOnButton(button, pointer, drag_event);
427 return button;
430 void SetupForDragTest(
431 std::vector<std::pair<ShelfID, views::View*> >* id_map) {
432 // Initialize |id_map| with the automatically-created shelf buttons.
433 for (size_t i = 0; i < model_->items().size(); ++i) {
434 ShelfButton* button = test_api_->GetButton(i);
435 id_map->push_back(std::make_pair(model_->items()[i].id, button));
437 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
439 // Add 5 app shelf buttons for testing.
440 for (int i = 0; i < 5; ++i) {
441 ShelfID id = AddAppShortcut();
442 // App Icon is located at index 0, and browser shortcut is located at
443 // index 1. So we should start to add app shortcut at index 2.
444 id_map->insert(id_map->begin() + (i + browser_index_ + 1),
445 std::make_pair(id, GetButtonByID(id)));
447 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
450 views::View* GetTooltipAnchorView() {
451 return shelf_view_->tooltip_manager()->anchor_;
454 void AddButtonsUntilOverflow() {
455 int items_added = 0;
456 while (!test_api_->IsOverflowButtonVisible()) {
457 AddAppShortcut();
458 ++items_added;
459 ASSERT_LT(items_added, 10000);
463 void ShowTooltip() {
464 shelf_view_->tooltip_manager()->ShowInternal();
467 void TestDraggingAnItemFromOverflowToShelf(bool cancel) {
468 test_api_->ShowOverflowBubble();
469 ASSERT_TRUE(test_api_->overflow_bubble() &&
470 test_api_->overflow_bubble()->IsShowing());
472 ash::test::ShelfViewTestAPI test_api_for_overflow(
473 test_api_->overflow_bubble()->shelf_view());
475 int total_item_count = model_->item_count();
477 int last_visible_item_id_in_shelf =
478 GetItemId(test_api_->GetLastVisibleIndex());
479 int second_last_visible_item_id_in_shelf =
480 GetItemId(test_api_->GetLastVisibleIndex() - 1);
481 int first_visible_item_id_in_overflow =
482 GetItemId(test_api_for_overflow.GetFirstVisibleIndex());
483 int second_last_visible_item_id_in_overflow =
484 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1);
486 int drag_item_index =
487 test_api_for_overflow.GetLastVisibleIndex();
488 ShelfID drag_item_id = GetItemId(drag_item_index);
489 ShelfButton* drag_button = test_api_for_overflow.GetButton(drag_item_index);
490 gfx::Point center_point_of_drag_item =
491 drag_button->GetBoundsInScreen().CenterPoint();
493 aura::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow(),
494 center_point_of_drag_item);
495 // Rip an item off to OverflowBubble.
496 generator.PressLeftButton();
497 gfx::Point rip_off_point(center_point_of_drag_item.x(), 0);
498 generator.MoveMouseTo(rip_off_point);
499 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
500 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
501 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
503 // Move a dragged item into Shelf at |drop_index|.
504 int drop_index = 1;
505 gfx::Point drop_point =
506 test_api_->GetButton(drop_index)->GetBoundsInScreen().CenterPoint();
507 int item_width = test_api_for_overflow.GetButtonSize();
508 // To insert at |drop_index|, more smaller x-axis value of |drop_point|
509 // should be used.
510 gfx::Point modified_drop_point(drop_point.x() - item_width / 4,
511 drop_point.y());
512 generator.MoveMouseTo(modified_drop_point);
513 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
514 test_api_->RunMessageLoopUntilAnimationsDone();
515 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
516 ASSERT_TRUE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
518 if (cancel)
519 drag_button->OnMouseCaptureLost();
520 else
521 generator.ReleaseLeftButton();
523 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
524 test_api_->RunMessageLoopUntilAnimationsDone();
525 ASSERT_FALSE(test_api_for_overflow.IsRippedOffFromShelf());
526 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
528 // Compare pre-stored items' id with newly positioned items' after dragging
529 // is canceled or finished.
530 if (cancel) {
531 EXPECT_EQ(last_visible_item_id_in_shelf,
532 GetItemId(test_api_->GetLastVisibleIndex()));
533 EXPECT_EQ(second_last_visible_item_id_in_shelf,
534 GetItemId(test_api_->GetLastVisibleIndex() - 1));
535 EXPECT_EQ(first_visible_item_id_in_overflow,
536 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
537 EXPECT_EQ(second_last_visible_item_id_in_overflow,
538 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1));
539 } else {
540 EXPECT_EQ(drag_item_id, GetItemId(drop_index));
541 EXPECT_EQ(total_item_count, model_->item_count());
542 EXPECT_EQ(last_visible_item_id_in_shelf,
543 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
544 EXPECT_EQ(second_last_visible_item_id_in_shelf,
545 GetItemId(test_api_->GetLastVisibleIndex()));
546 EXPECT_EQ(first_visible_item_id_in_overflow,
547 GetItemId(test_api_for_overflow.GetFirstVisibleIndex() + 1));
548 EXPECT_EQ(second_last_visible_item_id_in_overflow,
549 GetItemId(test_api_for_overflow.GetLastVisibleIndex()));
553 // Returns the item's ShelfID at |index|.
554 ShelfID GetItemId(int index) {
555 DCHECK_GE(index, 0);
556 return model_->items()[index].id;
559 void ReplaceShelfDelegateForRipOffTest() {
560 // Replace ShelfDelegate.
561 test::ShellTestApi test_api(Shell::GetInstance());
562 test_api.SetShelfDelegate(NULL);
563 ShelfDelegate* delegate = new TestShelfDelegateForShelfView(model_);
564 test_api.SetShelfDelegate(delegate);
565 test::ShelfTestAPI(Shelf::ForPrimaryDisplay()).SetShelfDelegate(delegate);
566 test_api_->SetShelfDelegate(delegate);
569 ShelfModel* model_;
570 ShelfView* shelf_view_;
571 int browser_index_;
572 ShelfItemDelegateManager* item_manager_;
574 scoped_ptr<ShelfViewTestAPI> test_api_;
576 private:
577 DISALLOW_COPY_AND_ASSIGN(ShelfViewTest);
580 class ScopedTextDirectionChange {
581 public:
582 ScopedTextDirectionChange(bool is_rtl)
583 : is_rtl_(is_rtl) {
584 original_locale_ = l10n_util::GetApplicationLocale(std::string());
585 if (is_rtl_)
586 base::i18n::SetICUDefaultLocale("he");
587 CheckTextDirectionIsCorrect();
590 ~ScopedTextDirectionChange() {
591 if (is_rtl_)
592 base::i18n::SetICUDefaultLocale(original_locale_);
595 private:
596 void CheckTextDirectionIsCorrect() {
597 ASSERT_EQ(is_rtl_, base::i18n::IsRTL());
600 bool is_rtl_;
601 std::string original_locale_;
604 class ShelfViewTextDirectionTest
605 : public ShelfViewTest,
606 public testing::WithParamInterface<bool> {
607 public:
608 ShelfViewTextDirectionTest() : text_direction_change_(GetParam()) {}
609 virtual ~ShelfViewTextDirectionTest() {}
611 virtual void SetUp() OVERRIDE {
612 ShelfViewTest::SetUp();
615 virtual void TearDown() OVERRIDE {
616 ShelfViewTest::TearDown();
619 private:
620 ScopedTextDirectionChange text_direction_change_;
622 DISALLOW_COPY_AND_ASSIGN(ShelfViewTextDirectionTest);
625 // Checks that the ideal item icon bounds match the view's bounds in the screen
626 // in both LTR and RTL.
627 TEST_P(ShelfViewTextDirectionTest, IdealBoundsOfItemIcon) {
628 ShelfID id = AddPlatformApp();
629 ShelfButton* button = GetButtonByID(id);
630 gfx::Rect item_bounds = button->GetBoundsInScreen();
631 gfx::Point icon_offset = button->GetIconBounds().origin();
632 item_bounds.Offset(icon_offset.OffsetFromOrigin());
633 gfx::Rect ideal_bounds = shelf_view_->GetIdealBoundsOfItemIcon(id);
634 gfx::Point screen_origin;
635 views::View::ConvertPointToScreen(shelf_view_, &screen_origin);
636 ideal_bounds.Offset(screen_origin.x(), screen_origin.y());
637 EXPECT_EQ(item_bounds.x(), ideal_bounds.x());
638 EXPECT_EQ(item_bounds.y(), ideal_bounds.y());
641 // Checks that shelf view contents are considered in the correct drag group.
642 TEST_F(ShelfViewTest, EnforceDragType) {
643 EXPECT_TRUE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_PLATFORM_APP));
644 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_SHORTCUT));
645 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP,
646 TYPE_BROWSER_SHORTCUT));
647 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_WINDOWED_APP));
648 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_LIST));
649 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_PANEL));
651 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_SHORTCUT));
652 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
653 TYPE_BROWSER_SHORTCUT));
654 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
655 TYPE_WINDOWED_APP));
656 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_LIST));
657 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_PANEL));
659 EXPECT_TRUE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
660 TYPE_BROWSER_SHORTCUT));
661 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
662 TYPE_WINDOWED_APP));
663 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_LIST));
664 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_PANEL));
666 EXPECT_TRUE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_WINDOWED_APP));
667 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_LIST));
668 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_PANEL));
670 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_LIST));
671 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_PANEL));
673 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_PANEL, TYPE_APP_PANEL));
676 // Adds platform app button until overflow and verifies that the last added
677 // platform app button is hidden.
678 TEST_F(ShelfViewTest, AddBrowserUntilOverflow) {
679 // All buttons should be visible.
680 ASSERT_EQ(test_api_->GetButtonCount(),
681 test_api_->GetLastVisibleIndex() + 1);
683 // Add platform app button until overflow.
684 int items_added = 0;
685 ShelfID last_added = AddPlatformApp();
686 while (!test_api_->IsOverflowButtonVisible()) {
687 // Added button is visible after animation while in this loop.
688 EXPECT_TRUE(GetButtonByID(last_added)->visible());
690 last_added = AddPlatformApp();
691 ++items_added;
692 ASSERT_LT(items_added, 10000);
695 // The last added button should be invisible.
696 EXPECT_FALSE(GetButtonByID(last_added)->visible());
699 // Adds one platform app button then adds app shortcut until overflow. Verifies
700 // that the browser button gets hidden on overflow and last added app shortcut
701 // is still visible.
702 TEST_F(ShelfViewTest, AddAppShortcutWithBrowserButtonUntilOverflow) {
703 // All buttons should be visible.
704 ASSERT_EQ(test_api_->GetButtonCount(),
705 test_api_->GetLastVisibleIndex() + 1);
707 ShelfID browser_button_id = AddPlatformApp();
709 // Add app shortcut until overflow.
710 int items_added = 0;
711 ShelfID last_added = AddAppShortcut();
712 while (!test_api_->IsOverflowButtonVisible()) {
713 // Added button is visible after animation while in this loop.
714 EXPECT_TRUE(GetButtonByID(last_added)->visible());
716 last_added = AddAppShortcut();
717 ++items_added;
718 ASSERT_LT(items_added, 10000);
721 // And the platform app button is invisible.
722 EXPECT_FALSE(GetButtonByID(browser_button_id)->visible());
725 TEST_F(ShelfViewTest, AddPanelHidesPlatformAppButton) {
726 // All buttons should be visible.
727 ASSERT_EQ(test_api_->GetButtonCount(),
728 test_api_->GetLastVisibleIndex() + 1);
730 // Add platform app button until overflow, remember last visible platform app
731 // button.
732 int items_added = 0;
733 ShelfID first_added = AddPlatformApp();
734 EXPECT_TRUE(GetButtonByID(first_added)->visible());
735 while (true) {
736 ShelfID added = AddPlatformApp();
737 if (test_api_->IsOverflowButtonVisible()) {
738 EXPECT_FALSE(GetButtonByID(added)->visible());
739 RemoveByID(added);
740 break;
742 ++items_added;
743 ASSERT_LT(items_added, 10000);
746 ShelfID panel = AddPanel();
747 EXPECT_TRUE(test_api_->IsOverflowButtonVisible());
749 RemoveByID(panel);
750 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
753 // When there are more panels then platform app buttons we should hide panels
754 // rather than platform apps.
755 TEST_F(ShelfViewTest, PlatformAppHidesExcessPanels) {
756 // All buttons should be visible.
757 ASSERT_EQ(test_api_->GetButtonCount(),
758 test_api_->GetLastVisibleIndex() + 1);
760 // Add platform app button.
761 ShelfID platform_app = AddPlatformApp();
762 ShelfID first_panel = AddPanel();
764 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
765 EXPECT_TRUE(GetButtonByID(first_panel)->visible());
767 // Add panels until there is an overflow.
768 ShelfID last_panel = first_panel;
769 int items_added = 0;
770 while (!test_api_->IsOverflowButtonVisible()) {
771 last_panel = AddPanel();
772 ++items_added;
773 ASSERT_LT(items_added, 10000);
776 // The first panel should now be hidden by the new platform apps needing
777 // space.
778 EXPECT_FALSE(GetButtonByID(first_panel)->visible());
779 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
780 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
782 // Adding platform apps should eventually begin to hide platform apps. We will
783 // add platform apps until either the last panel or platform app is hidden.
784 items_added = 0;
785 while (GetButtonByID(platform_app)->visible() &&
786 GetButtonByID(last_panel)->visible()) {
787 platform_app = AddPlatformApp();
788 ++items_added;
789 ASSERT_LT(items_added, 10000);
791 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
792 EXPECT_FALSE(GetButtonByID(platform_app)->visible());
795 // Adds button until overflow then removes first added one. Verifies that
796 // the last added one changes from invisible to visible and overflow
797 // chevron is gone.
798 TEST_F(ShelfViewTest, RemoveButtonRevealsOverflowed) {
799 // All buttons should be visible.
800 ASSERT_EQ(test_api_->GetButtonCount(),
801 test_api_->GetLastVisibleIndex() + 1);
803 // Add platform app buttons until overflow.
804 int items_added = 0;
805 ShelfID first_added = AddPlatformApp();
806 ShelfID last_added = first_added;
807 while (!test_api_->IsOverflowButtonVisible()) {
808 last_added = AddPlatformApp();
809 ++items_added;
810 ASSERT_LT(items_added, 10000);
813 // Expect add more than 1 button. First added is visible and last is not.
814 EXPECT_NE(first_added, last_added);
815 EXPECT_TRUE(GetButtonByID(first_added)->visible());
816 EXPECT_FALSE(GetButtonByID(last_added)->visible());
818 // Remove first added.
819 RemoveByID(first_added);
821 // Last added button becomes visible and overflow chevron is gone.
822 EXPECT_TRUE(GetButtonByID(last_added)->visible());
823 EXPECT_EQ(1.0f, GetButtonByID(last_added)->layer()->opacity());
824 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
827 // Verifies that remove last overflowed button should hide overflow chevron.
828 TEST_F(ShelfViewTest, RemoveLastOverflowed) {
829 // All buttons should be visible.
830 ASSERT_EQ(test_api_->GetButtonCount(),
831 test_api_->GetLastVisibleIndex() + 1);
833 // Add platform app button until overflow.
834 int items_added = 0;
835 ShelfID last_added = AddPlatformApp();
836 while (!test_api_->IsOverflowButtonVisible()) {
837 last_added = AddPlatformApp();
838 ++items_added;
839 ASSERT_LT(items_added, 10000);
842 RemoveByID(last_added);
843 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
846 // Adds platform app button without waiting for animation to finish and verifies
847 // that all added buttons are visible.
848 TEST_F(ShelfViewTest, AddButtonQuickly) {
849 // All buttons should be visible.
850 ASSERT_EQ(test_api_->GetButtonCount(),
851 test_api_->GetLastVisibleIndex() + 1);
853 // Add a few platform buttons quickly without wait for animation.
854 int added_count = 0;
855 while (!test_api_->IsOverflowButtonVisible()) {
856 AddPlatformAppNoWait();
857 ++added_count;
858 ASSERT_LT(added_count, 10000);
861 // ShelfView should be big enough to hold at least 3 new buttons.
862 ASSERT_GE(added_count, 3);
864 // Wait for the last animation to finish.
865 test_api_->RunMessageLoopUntilAnimationsDone();
867 // Verifies non-overflow buttons are visible.
868 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
869 ShelfButton* button = test_api_->GetButton(i);
870 if (button) {
871 EXPECT_TRUE(button->visible()) << "button index=" << i;
872 EXPECT_EQ(1.0f, button->layer()->opacity()) << "button index=" << i;
877 // Check that model changes are handled correctly while a shelf icon is being
878 // dragged.
879 TEST_F(ShelfViewTest, ModelChangesWhileDragging) {
880 ShelfButtonHost* button_host = shelf_view_;
882 std::vector<std::pair<ShelfID, views::View*> > id_map;
883 SetupForDragTest(&id_map);
885 // Dragging browser shortcut at index 1.
886 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
887 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
888 std::rotate(id_map.begin() + 1,
889 id_map.begin() + 2,
890 id_map.begin() + 4);
891 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
892 button_host->PointerReleasedOnButton(
893 dragged_button, ShelfButtonHost::MOUSE, false);
894 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
896 // Dragging changes model order.
897 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
898 std::rotate(id_map.begin() + 1,
899 id_map.begin() + 2,
900 id_map.begin() + 4);
901 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
903 // Cancelling the drag operation restores previous order.
904 button_host->PointerReleasedOnButton(
905 dragged_button, ShelfButtonHost::MOUSE, true);
906 std::rotate(id_map.begin() + 1,
907 id_map.begin() + 3,
908 id_map.begin() + 4);
909 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
911 // Deleting an item keeps the remaining intact.
912 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
913 model_->RemoveItemAt(1);
914 id_map.erase(id_map.begin() + 1);
915 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
916 button_host->PointerReleasedOnButton(
917 dragged_button, ShelfButtonHost::MOUSE, false);
919 // Adding a shelf item cancels the drag and respects the order.
920 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
921 ShelfID new_id = AddAppShortcut();
922 id_map.insert(id_map.begin() + 6,
923 std::make_pair(new_id, GetButtonByID(new_id)));
924 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
925 button_host->PointerReleasedOnButton(
926 dragged_button, ShelfButtonHost::MOUSE, false);
928 // Adding a shelf item at the end (i.e. a panel) canels drag and respects
929 // the order.
930 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
931 new_id = AddPanel();
932 id_map.insert(id_map.begin() + 7,
933 std::make_pair(new_id, GetButtonByID(new_id)));
934 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
935 button_host->PointerReleasedOnButton(
936 dragged_button, ShelfButtonHost::MOUSE, false);
939 // Check that 2nd drag from the other pointer would be ignored.
940 TEST_F(ShelfViewTest, SimultaneousDrag) {
941 ShelfButtonHost* button_host = shelf_view_;
943 std::vector<std::pair<ShelfID, views::View*> > id_map;
944 SetupForDragTest(&id_map);
946 // Start a mouse drag.
947 views::View* dragged_button_mouse =
948 SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
949 std::rotate(id_map.begin() + 1,
950 id_map.begin() + 2,
951 id_map.begin() + 4);
952 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
953 // Attempt a touch drag before the mouse drag finishes.
954 views::View* dragged_button_touch =
955 SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
957 // Nothing changes since 2nd drag is ignored.
958 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
960 // Finish the mouse drag.
961 button_host->PointerReleasedOnButton(
962 dragged_button_mouse, ShelfButtonHost::MOUSE, false);
963 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
965 // Now start a touch drag.
966 dragged_button_touch = SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
967 std::rotate(id_map.begin() + 3,
968 id_map.begin() + 4,
969 id_map.begin() + 5);
970 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
972 // And attempt a mouse drag before the touch drag finishes.
973 dragged_button_mouse = SimulateDrag(ShelfButtonHost::MOUSE, 1, 2);
975 // Nothing changes since 2nd drag is ignored.
976 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
978 button_host->PointerReleasedOnButton(
979 dragged_button_touch, ShelfButtonHost::TOUCH, false);
980 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
983 // Check that clicking first on one item and then dragging another works as
984 // expected.
985 TEST_F(ShelfViewTest, ClickOneDragAnother) {
986 ShelfButtonHost* button_host = shelf_view_;
988 std::vector<std::pair<ShelfID, views::View*> > id_map;
989 SetupForDragTest(&id_map);
991 // A click on item 1 is simulated.
992 SimulateClick(ShelfButtonHost::MOUSE, 1);
994 // Dragging browser index at 0 should change the model order correctly.
995 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
996 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
997 std::rotate(id_map.begin() + 1,
998 id_map.begin() + 2,
999 id_map.begin() + 4);
1000 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1001 button_host->PointerReleasedOnButton(
1002 dragged_button, ShelfButtonHost::MOUSE, false);
1003 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
1006 // Confirm that item status changes are reflected in the buttons.
1007 TEST_F(ShelfViewTest, ShelfItemStatus) {
1008 // All buttons should be visible.
1009 ASSERT_EQ(test_api_->GetButtonCount(),
1010 test_api_->GetLastVisibleIndex() + 1);
1012 // Add platform app button.
1013 ShelfID last_added = AddPlatformApp();
1014 ShelfItem item = GetItemByID(last_added);
1015 int index = model_->ItemIndexByID(last_added);
1016 ShelfButton* button = GetButtonByID(last_added);
1017 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1018 item.status = STATUS_ACTIVE;
1019 model_->Set(index, item);
1020 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1021 item.status = STATUS_ATTENTION;
1022 model_->Set(index, item);
1023 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1026 // Confirm that item status changes are reflected in the buttons
1027 // for platform apps.
1028 TEST_F(ShelfViewTest, ShelfItemStatusPlatformApp) {
1029 // All buttons should be visible.
1030 ASSERT_EQ(test_api_->GetButtonCount(),
1031 test_api_->GetLastVisibleIndex() + 1);
1033 // Add platform app button.
1034 ShelfID last_added = AddPlatformApp();
1035 ShelfItem item = GetItemByID(last_added);
1036 int index = model_->ItemIndexByID(last_added);
1037 ShelfButton* button = GetButtonByID(last_added);
1038 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1039 item.status = STATUS_ACTIVE;
1040 model_->Set(index, item);
1041 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1042 item.status = STATUS_ATTENTION;
1043 model_->Set(index, item);
1044 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1047 // Confirm that shelf item bounds are correctly updated on shelf changes.
1048 TEST_F(ShelfViewTest, ShelfItemBoundsCheck) {
1049 VerifyShelfItemBoundsAreValid();
1050 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1051 SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
1052 test_api_->RunMessageLoopUntilAnimationsDone();
1053 VerifyShelfItemBoundsAreValid();
1054 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1055 SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
1056 test_api_->RunMessageLoopUntilAnimationsDone();
1057 VerifyShelfItemBoundsAreValid();
1060 TEST_F(ShelfViewTest, ShelfTooltipTest) {
1061 ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
1062 test_api_->GetButtonCount());
1064 // Prepare some items to the shelf.
1065 ShelfID app_button_id = AddAppShortcut();
1066 ShelfID platform_button_id = AddPlatformApp();
1068 ShelfButton* app_button = GetButtonByID(app_button_id);
1069 ShelfButton* platform_button = GetButtonByID(platform_button_id);
1071 ShelfButtonHost* button_host = shelf_view_;
1072 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1074 button_host->MouseEnteredButton(app_button);
1075 // There's a delay to show the tooltip, so it's not visible yet.
1076 EXPECT_FALSE(tooltip_manager->IsVisible());
1077 EXPECT_EQ(app_button, GetTooltipAnchorView());
1079 ShowTooltip();
1080 EXPECT_TRUE(tooltip_manager->IsVisible());
1082 // Once it's visible, it keeps visibility and is pointing to the same
1083 // item.
1084 button_host->MouseExitedButton(app_button);
1085 EXPECT_TRUE(tooltip_manager->IsVisible());
1086 EXPECT_EQ(app_button, GetTooltipAnchorView());
1088 // When entered to another item, it switches to the new item. There is no
1089 // delay for the visibility.
1090 button_host->MouseEnteredButton(platform_button);
1091 EXPECT_TRUE(tooltip_manager->IsVisible());
1092 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1094 button_host->MouseExitedButton(platform_button);
1095 tooltip_manager->Close();
1097 // Next time: enter app_button -> move immediately to tab_button.
1098 button_host->MouseEnteredButton(app_button);
1099 button_host->MouseExitedButton(app_button);
1100 button_host->MouseEnteredButton(platform_button);
1101 EXPECT_FALSE(tooltip_manager->IsVisible());
1102 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1105 // Verify a fix for crash caused by a tooltip update for a deletedshelf
1106 // button, see crbug.com/288838.
1107 TEST_F(ShelfViewTest, RemovingItemClosesTooltip) {
1108 ShelfButtonHost* button_host = shelf_view_;
1109 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1111 // Add an item to the shelf.
1112 ShelfID app_button_id = AddAppShortcut();
1113 ShelfButton* app_button = GetButtonByID(app_button_id);
1115 // Spawn a tooltip on that item.
1116 button_host->MouseEnteredButton(app_button);
1117 ShowTooltip();
1118 EXPECT_TRUE(tooltip_manager->IsVisible());
1120 // Remove the app shortcut while the tooltip is open. The tooltip should be
1121 // closed.
1122 RemoveByID(app_button_id);
1123 EXPECT_FALSE(tooltip_manager->IsVisible());
1125 // Change the shelf layout. This should not crash.
1126 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1127 Shell::GetPrimaryRootWindow());
1130 // Changing the shelf alignment closes any open tooltip.
1131 TEST_F(ShelfViewTest, ShelfAlignmentClosesTooltip) {
1132 ShelfButtonHost* button_host = shelf_view_;
1133 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1135 // Add an item to the shelf.
1136 ShelfID app_button_id = AddAppShortcut();
1137 ShelfButton* app_button = GetButtonByID(app_button_id);
1139 // Spawn a tooltip on the item.
1140 button_host->MouseEnteredButton(app_button);
1141 ShowTooltip();
1142 EXPECT_TRUE(tooltip_manager->IsVisible());
1144 // Changing shelf alignment hides the tooltip.
1145 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1146 Shell::GetPrimaryRootWindow());
1147 EXPECT_FALSE(tooltip_manager->IsVisible());
1150 TEST_F(ShelfViewTest, ShouldHideTooltipTest) {
1151 ShelfID app_button_id = AddAppShortcut();
1152 ShelfID platform_button_id = AddPlatformApp();
1154 // The tooltip shouldn't hide if the mouse is on normal buttons.
1155 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1156 ShelfButton* button = test_api_->GetButton(i);
1157 if (!button)
1158 continue;
1160 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1161 button->GetMirroredBounds().CenterPoint()))
1162 << "ShelfView tries to hide on button " << i;
1165 // The tooltip should not hide on the app-list button.
1166 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1167 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1168 app_list_button->GetMirroredBounds().CenterPoint()));
1170 // The tooltip shouldn't hide if the mouse is in the gap between two buttons.
1171 gfx::Rect app_button_rect = GetButtonByID(app_button_id)->GetMirroredBounds();
1172 gfx::Rect platform_button_rect =
1173 GetButtonByID(platform_button_id)->GetMirroredBounds();
1174 ASSERT_FALSE(app_button_rect.Intersects(platform_button_rect));
1175 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1176 gfx::UnionRects(app_button_rect, platform_button_rect).CenterPoint()));
1178 // The tooltip should hide if it's outside of all buttons.
1179 gfx::Rect all_area;
1180 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1181 ShelfButton* button = test_api_->GetButton(i);
1182 if (!button)
1183 continue;
1185 all_area.Union(button->GetMirroredBounds());
1187 all_area.Union(shelf_view_->GetAppListButtonView()->GetMirroredBounds());
1188 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(all_area.origin()));
1189 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1190 gfx::Point(all_area.right() - 1, all_area.bottom() - 1)));
1191 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1192 gfx::Point(all_area.right(), all_area.y())));
1193 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1194 gfx::Point(all_area.x() - 1, all_area.y())));
1195 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1196 gfx::Point(all_area.x(), all_area.y() - 1)));
1197 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1198 gfx::Point(all_area.x(), all_area.bottom())));
1201 TEST_F(ShelfViewTest, ShouldHideTooltipWithAppListWindowTest) {
1202 Shell::GetInstance()->ToggleAppList(NULL);
1203 ASSERT_TRUE(Shell::GetInstance()->GetAppListWindow());
1205 // The tooltip shouldn't hide if the mouse is on normal buttons.
1206 for (int i = 1; i < test_api_->GetButtonCount(); i++) {
1207 ShelfButton* button = test_api_->GetButton(i);
1208 if (!button)
1209 continue;
1211 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1212 button->GetMirroredBounds().CenterPoint()))
1213 << "ShelfView tries to hide on button " << i;
1216 // The tooltip should hide on the app-list button.
1217 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1218 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1219 app_list_button->GetMirroredBounds().CenterPoint()));
1222 // Test that by moving the mouse cursor off the button onto the bubble it closes
1223 // the bubble.
1224 TEST_F(ShelfViewTest, ShouldHideTooltipWhenHoveringOnTooltip) {
1225 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1226 tooltip_manager->CreateZeroDelayTimerForTest();
1227 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1229 // Move the mouse off any item and check that no tooltip is shown.
1230 generator.MoveMouseTo(gfx::Point(0, 0));
1231 EXPECT_FALSE(tooltip_manager->IsVisible());
1233 // Move the mouse over the button and check that it is visible.
1234 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1235 gfx::Rect bounds = app_list_button->GetBoundsInScreen();
1236 generator.MoveMouseTo(bounds.CenterPoint());
1237 // Wait for the timer to go off.
1238 RunAllPendingInMessageLoop();
1239 EXPECT_TRUE(tooltip_manager->IsVisible());
1241 // Move the mouse cursor slightly to the right of the item. The tooltip should
1242 // stay open.
1243 generator.MoveMouseBy(bounds.width() / 2 + 5, 0);
1244 // Make sure there is no delayed close.
1245 RunAllPendingInMessageLoop();
1246 EXPECT_TRUE(tooltip_manager->IsVisible());
1248 // Move back - it should still stay open.
1249 generator.MoveMouseBy(-(bounds.width() / 2 + 5), 0);
1250 // Make sure there is no delayed close.
1251 RunAllPendingInMessageLoop();
1252 EXPECT_TRUE(tooltip_manager->IsVisible());
1254 // Now move the mouse cursor slightly above the item - so that it is over the
1255 // tooltip bubble. Now it should disappear.
1256 generator.MoveMouseBy(0, -(bounds.height() / 2 + 5));
1257 // Wait until the delayed close kicked in.
1258 RunAllPendingInMessageLoop();
1259 EXPECT_FALSE(tooltip_manager->IsVisible());
1262 // Resizing shelf view while an add animation without fade-in is running,
1263 // which happens when overflow happens. App list button should end up in its
1264 // new ideal bounds.
1265 TEST_F(ShelfViewTest, ResizeDuringOverflowAddAnimation) {
1266 // All buttons should be visible.
1267 ASSERT_EQ(test_api_->GetButtonCount(),
1268 test_api_->GetLastVisibleIndex() + 1);
1270 // Add buttons until overflow. Let the non-overflow add animations finish but
1271 // leave the last running.
1272 int items_added = 0;
1273 AddPlatformAppNoWait();
1274 while (!test_api_->IsOverflowButtonVisible()) {
1275 test_api_->RunMessageLoopUntilAnimationsDone();
1276 AddPlatformAppNoWait();
1277 ++items_added;
1278 ASSERT_LT(items_added, 10000);
1281 // Resize shelf view with that animation running and stay overflown.
1282 gfx::Rect bounds = shelf_view_->bounds();
1283 bounds.set_width(bounds.width() - kShelfSize);
1284 shelf_view_->SetBoundsRect(bounds);
1285 ASSERT_TRUE(test_api_->IsOverflowButtonVisible());
1287 // Finish the animation.
1288 test_api_->RunMessageLoopUntilAnimationsDone();
1290 // App list button should ends up in its new ideal bounds.
1291 const int app_list_button_index = test_api_->GetButtonCount() - 1;
1292 const gfx::Rect& app_list_ideal_bounds =
1293 test_api_->GetIdealBoundsByIndex(app_list_button_index);
1294 const gfx::Rect& app_list_bounds =
1295 test_api_->GetBoundsByIndex(app_list_button_index);
1296 EXPECT_EQ(app_list_ideal_bounds, app_list_bounds);
1299 // Checks the overflow bubble size when an item is ripped off and re-inserted.
1300 TEST_F(ShelfViewTest, OverflowBubbleSize) {
1301 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1302 ReplaceShelfDelegateForRipOffTest();
1304 AddButtonsUntilOverflow();
1306 // Show overflow bubble.
1307 test_api_->ShowOverflowBubble();
1308 ASSERT_TRUE(test_api_->overflow_bubble() &&
1309 test_api_->overflow_bubble()->IsShowing());
1311 ShelfViewTestAPI test_for_overflow_view(
1312 test_api_->overflow_bubble()->shelf_view());
1314 int ripped_index = test_for_overflow_view.GetLastVisibleIndex();
1315 gfx::Size bubble_size = test_for_overflow_view.GetPreferredSize();
1316 int item_width = test_for_overflow_view.GetButtonSize() +
1317 test_for_overflow_view.GetButtonSpacing();
1319 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
1320 gfx::Point());
1321 ShelfButton* button = test_for_overflow_view.GetButton(ripped_index);
1322 // Rip off the last visible item.
1323 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1324 gfx::Point rip_off_point(start_point.x(), 0);
1325 generator.MoveMouseTo(start_point.x(), start_point.y());
1326 base::MessageLoop::current()->RunUntilIdle();
1327 generator.PressLeftButton();
1328 base::MessageLoop::current()->RunUntilIdle();
1329 generator.MoveMouseTo(rip_off_point.x(), rip_off_point.y());
1330 base::MessageLoop::current()->RunUntilIdle();
1331 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1333 // Check the overflow bubble size when an item is ripped off.
1334 EXPECT_EQ(bubble_size.width() - item_width,
1335 test_for_overflow_view.GetPreferredSize().width());
1336 ASSERT_TRUE(test_api_->overflow_bubble() &&
1337 test_api_->overflow_bubble()->IsShowing());
1339 // Re-insert an item into the overflow bubble.
1340 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1341 button = test_for_overflow_view.GetButton(first_index);
1343 // Check the bubble size after an item is re-inserted.
1344 generator.MoveMouseTo(button->GetBoundsInScreen().CenterPoint());
1345 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1346 EXPECT_EQ(bubble_size.width(),
1347 test_for_overflow_view.GetPreferredSize().width());
1349 generator.ReleaseLeftButton();
1350 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1351 EXPECT_EQ(bubble_size.width(),
1352 test_for_overflow_view.GetPreferredSize().width());
1355 // Check the drag insertion bounds of scrolled overflow bubble.
1356 TEST_F(ShelfViewTest, CheckDragInsertBoundsOfScrolledOverflowBubble) {
1357 UpdateDisplay("400x300");
1359 EXPECT_EQ(2, model_->item_count());
1361 AddButtonsUntilOverflow();
1363 // Show overflow bubble.
1364 test_api_->ShowOverflowBubble();
1365 ASSERT_TRUE(test_api_->overflow_bubble() &&
1366 test_api_->overflow_bubble()->IsShowing());
1368 int item_width = test_api_->GetButtonSize() +
1369 test_api_->GetButtonSpacing();
1370 OverflowBubbleView* bubble_view = test_api_->overflow_bubble()->bubble_view();
1371 test::OverflowBubbleViewTestAPI bubble_view_api(bubble_view);
1373 // Add more buttons until OverflowBubble is scrollable and it has 3 invisible
1374 // items.
1375 while (bubble_view_api.GetContentsSize().width() <
1376 (bubble_view->GetContentsBounds().width() + 3 * item_width))
1377 AddAppShortcut();
1379 ASSERT_TRUE(test_api_->overflow_bubble() &&
1380 test_api_->overflow_bubble()->IsShowing());
1382 ShelfViewTestAPI test_for_overflow_view(
1383 test_api_->overflow_bubble()->shelf_view());
1384 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1385 int last_index = test_for_overflow_view.GetLastVisibleIndex();
1387 ShelfButton* first_button = test_for_overflow_view.GetButton(first_index);
1388 ShelfButton* last_button = test_for_overflow_view.GetButton(last_index);
1389 gfx::Point first_point = first_button->GetBoundsInScreen().CenterPoint();
1390 gfx::Point last_point = last_button->GetBoundsInScreen().CenterPoint();
1391 gfx::Rect drag_reinsert_bounds =
1392 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1393 EXPECT_TRUE(drag_reinsert_bounds.Contains(first_point));
1394 EXPECT_FALSE(drag_reinsert_bounds.Contains(last_point));
1396 // Scrolls sufficiently to show last item.
1397 bubble_view_api.ScrollByXOffset(3 * item_width);
1398 drag_reinsert_bounds =
1399 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1400 first_point = first_button->GetBoundsInScreen().CenterPoint();
1401 last_point = last_button->GetBoundsInScreen().CenterPoint();
1402 EXPECT_FALSE(drag_reinsert_bounds.Contains(first_point));
1403 EXPECT_TRUE(drag_reinsert_bounds.Contains(last_point));
1406 // Check the drag insertion bounds of shelf view in multi monitor environment.
1407 TEST_F(ShelfViewTest, CheckDragInsertBoundsWithMultiMonitor) {
1408 // win8-aura doesn't support multiple display.
1409 if (!SupportsMultipleDisplays())
1410 return;
1412 UpdateDisplay("800x600,800x600");
1413 Shelf* secondary_shelf = Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
1414 ShelfView* shelf_view_for_secondary =
1415 ShelfTestAPI(secondary_shelf).shelf_view();
1417 // The bounds should be big enough for 4 buttons + overflow chevron.
1418 shelf_view_for_secondary->SetBounds(0, 0, 500, kShelfSize);
1420 ShelfViewTestAPI test_api_for_secondary(shelf_view_for_secondary);
1421 // Speeds up animation for test.
1422 test_api_for_secondary.SetAnimationDuration(1);
1424 AddButtonsUntilOverflow();
1426 // Test #1: Test drag insertion bounds of primary shelf.
1427 // Show overflow bubble.
1428 test_api_->ShowOverflowBubble();
1429 ASSERT_TRUE(test_api_->overflow_bubble() &&
1430 test_api_->overflow_bubble()->IsShowing());
1432 ShelfViewTestAPI test_api_for_overflow_view(
1433 test_api_->overflow_bubble()->shelf_view());
1435 ShelfButton* button = test_api_for_overflow_view.GetButton(
1436 test_api_for_overflow_view.GetLastVisibleIndex());
1438 // Checks that a point in shelf is contained in drag insert bounds.
1439 gfx::Point point_in_shelf_view = button->GetBoundsInScreen().CenterPoint();
1440 gfx::Rect drag_reinsert_bounds =
1441 test_api_for_overflow_view.GetBoundsForDragInsertInScreen();
1442 EXPECT_TRUE(drag_reinsert_bounds.Contains(point_in_shelf_view));
1443 // Checks that a point out of shelf is not contained in drag insert bounds.
1444 EXPECT_FALSE(drag_reinsert_bounds.Contains(
1445 gfx::Point(point_in_shelf_view.x(), 0)));
1447 // Test #2: Test drag insertion bounds of secondary shelf.
1448 // Show overflow bubble.
1449 test_api_for_secondary.ShowOverflowBubble();
1450 ASSERT_TRUE(test_api_for_secondary.overflow_bubble() &&
1451 test_api_for_secondary.overflow_bubble()->IsShowing());
1453 ShelfViewTestAPI test_api_for_overflow_view_of_secondary(
1454 test_api_for_secondary.overflow_bubble()->shelf_view());
1456 ShelfButton* button_in_secondary =
1457 test_api_for_overflow_view_of_secondary.GetButton(
1458 test_api_for_overflow_view_of_secondary.GetLastVisibleIndex());
1460 // Checks that a point in shelf is contained in drag insert bounds.
1461 gfx::Point point_in_secondary_shelf_view =
1462 button_in_secondary->GetBoundsInScreen().CenterPoint();
1463 gfx::Rect drag_reinsert_bounds_in_secondary =
1464 test_api_for_overflow_view_of_secondary.GetBoundsForDragInsertInScreen();
1465 EXPECT_TRUE(drag_reinsert_bounds_in_secondary.Contains(
1466 point_in_secondary_shelf_view));
1467 // Checks that a point out of shelf is not contained in drag insert bounds.
1468 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(
1469 gfx::Point(point_in_secondary_shelf_view.x(), 0)));
1470 // Checks that a point of overflow bubble in primary shelf should not be
1471 // contained by insert bounds of secondary shelf.
1472 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(point_in_shelf_view));
1475 // Checks the rip an item off from left aligned shelf in secondary monitor.
1476 TEST_F(ShelfViewTest, CheckRipOffFromLeftShelfAlignmentWithMultiMonitor) {
1477 // win8-aura doesn't support multiple display.
1478 if (!SupportsMultipleDisplays())
1479 return;
1481 UpdateDisplay("800x600,800x600");
1482 ASSERT_EQ(2U, Shell::GetAllRootWindows().size());
1484 aura::Window* second_root = Shell::GetAllRootWindows()[1];
1486 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT, second_root);
1487 ASSERT_EQ(SHELF_ALIGNMENT_LEFT,
1488 Shell::GetInstance()->GetShelfAlignment(second_root));
1490 // Initially, app list and browser shortcut are added.
1491 EXPECT_EQ(2, model_->item_count());
1492 int browser_index = model_->GetItemIndexForType(TYPE_BROWSER_SHORTCUT);
1493 EXPECT_GT(browser_index, 0);
1495 Shelf* secondary_shelf = Shelf::ForWindow(second_root);
1496 ShelfView* shelf_view_for_secondary =
1497 ShelfTestAPI(secondary_shelf).shelf_view();
1499 ShelfViewTestAPI test_api_for_secondary_shelf_view(shelf_view_for_secondary);
1500 ShelfButton* button =
1501 test_api_for_secondary_shelf_view.GetButton(browser_index);
1503 // Fetch the start point of dragging.
1504 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1505 wm::ConvertPointFromScreen(second_root, &start_point);
1507 aura::test::EventGenerator generator(second_root, start_point);
1509 // Rip off the browser item.
1510 generator.PressLeftButton();
1511 generator.MoveMouseTo(start_point.x() + 400, start_point.y());
1512 test_api_for_secondary_shelf_view.RunMessageLoopUntilAnimationsDone();
1513 EXPECT_TRUE(test_api_for_secondary_shelf_view.IsRippedOffFromShelf());
1516 // Checks various drag and drop operations from OverflowBubble to Shelf.
1517 TEST_F(ShelfViewTest, CheckDragAndDropFromOverflowBubbleToShelf) {
1518 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1519 ReplaceShelfDelegateForRipOffTest();
1521 AddButtonsUntilOverflow();
1523 TestDraggingAnItemFromOverflowToShelf(false);
1524 TestDraggingAnItemFromOverflowToShelf(true);
1527 class ShelfViewVisibleBoundsTest : public ShelfViewTest,
1528 public testing::WithParamInterface<bool> {
1529 public:
1530 ShelfViewVisibleBoundsTest() : text_direction_change_(GetParam()) {}
1532 void CheckAllItemsAreInBounds() {
1533 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1534 gfx::Rect shelf_bounds = shelf_view_->GetBoundsInScreen();
1535 EXPECT_TRUE(shelf_bounds.Contains(visible_bounds));
1536 for (int i = 0; i < test_api_->GetButtonCount(); ++i)
1537 if (ShelfButton* button = test_api_->GetButton(i))
1538 EXPECT_TRUE(visible_bounds.Contains(button->GetBoundsInScreen()));
1539 CheckAppListButtonIsInBounds();
1542 void CheckAppListButtonIsInBounds() {
1543 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1544 gfx::Rect app_list_button_bounds = shelf_view_->GetAppListButtonView()->
1545 GetBoundsInScreen();
1546 EXPECT_TRUE(visible_bounds.Contains(app_list_button_bounds));
1549 private:
1550 ScopedTextDirectionChange text_direction_change_;
1552 DISALLOW_COPY_AND_ASSIGN(ShelfViewVisibleBoundsTest);
1555 TEST_P(ShelfViewVisibleBoundsTest, ItemsAreInBounds) {
1556 // Adding elements leaving some empty space.
1557 for (int i = 0; i < 3; i++) {
1558 AddAppShortcut();
1560 test_api_->RunMessageLoopUntilAnimationsDone();
1561 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
1562 CheckAllItemsAreInBounds();
1563 // Same for overflow case.
1564 while (!test_api_->IsOverflowButtonVisible()) {
1565 AddAppShortcut();
1567 test_api_->RunMessageLoopUntilAnimationsDone();
1568 CheckAllItemsAreInBounds();
1571 INSTANTIATE_TEST_CASE_P(LtrRtl, ShelfViewTextDirectionTest, testing::Bool());
1572 INSTANTIATE_TEST_CASE_P(VisibleBounds, ShelfViewVisibleBoundsTest,
1573 testing::Bool());
1575 } // namespace test
1576 } // namespace ash