Remove now obsolete checkdeps copy
[chromium-blink-merge.git] / ash / shelf / shelf_view_unittest.cc
blobd57e23eccb31a875d2cb862fb6342d424f6c5d1e
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 // Check that items in the overflow area are returning the overflow button as
642 // ideal bounds.
643 TEST_F(ShelfViewTest, OverflowButtonBounds) {
644 ShelfID first_id = AddPlatformApp();
645 ShelfID overflow_id = AddPlatformApp();
646 int items_added = 0;
647 while (!test_api_->IsOverflowButtonVisible()) {
648 // Added button is visible after animation while in this loop.
649 EXPECT_TRUE(GetButtonByID(overflow_id)->visible());
650 overflow_id = AddPlatformApp();
651 ++items_added;
652 ASSERT_LT(items_added, 10000);
654 ShelfID last_id = AddPlatformApp();
656 gfx::Rect first_bounds = shelf_view_->GetIdealBoundsOfItemIcon(first_id);
657 gfx::Rect overflow_bounds =
658 shelf_view_->GetIdealBoundsOfItemIcon(overflow_id);
659 gfx::Rect last_bounds = shelf_view_->GetIdealBoundsOfItemIcon(last_id);
661 // Check that all items have the same size and that the overflow items are
662 // identical whereas the first one does not match either of them.
663 EXPECT_EQ(first_bounds.size().ToString(), last_bounds.size().ToString());
664 EXPECT_NE(first_bounds.ToString(), last_bounds.ToString());
665 EXPECT_EQ(overflow_bounds.ToString(), last_bounds.ToString());
668 // Checks that shelf view contents are considered in the correct drag group.
669 TEST_F(ShelfViewTest, EnforceDragType) {
670 EXPECT_TRUE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_PLATFORM_APP));
671 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_SHORTCUT));
672 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP,
673 TYPE_BROWSER_SHORTCUT));
674 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_WINDOWED_APP));
675 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_LIST));
676 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_PANEL));
678 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_SHORTCUT));
679 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
680 TYPE_BROWSER_SHORTCUT));
681 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
682 TYPE_WINDOWED_APP));
683 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_LIST));
684 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_PANEL));
686 EXPECT_TRUE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
687 TYPE_BROWSER_SHORTCUT));
688 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
689 TYPE_WINDOWED_APP));
690 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_LIST));
691 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_PANEL));
693 EXPECT_TRUE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_WINDOWED_APP));
694 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_LIST));
695 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_PANEL));
697 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_LIST));
698 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_PANEL));
700 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_PANEL, TYPE_APP_PANEL));
703 // Adds platform app button until overflow and verifies that the last added
704 // platform app button is hidden.
705 TEST_F(ShelfViewTest, AddBrowserUntilOverflow) {
706 // All buttons should be visible.
707 ASSERT_EQ(test_api_->GetButtonCount(),
708 test_api_->GetLastVisibleIndex() + 1);
710 // Add platform app button until overflow.
711 int items_added = 0;
712 ShelfID last_added = AddPlatformApp();
713 while (!test_api_->IsOverflowButtonVisible()) {
714 // Added button is visible after animation while in this loop.
715 EXPECT_TRUE(GetButtonByID(last_added)->visible());
717 last_added = AddPlatformApp();
718 ++items_added;
719 ASSERT_LT(items_added, 10000);
722 // The last added button should be invisible.
723 EXPECT_FALSE(GetButtonByID(last_added)->visible());
726 // Adds one platform app button then adds app shortcut until overflow. Verifies
727 // that the browser button gets hidden on overflow and last added app shortcut
728 // is still visible.
729 TEST_F(ShelfViewTest, AddAppShortcutWithBrowserButtonUntilOverflow) {
730 // All buttons should be visible.
731 ASSERT_EQ(test_api_->GetButtonCount(),
732 test_api_->GetLastVisibleIndex() + 1);
734 ShelfID browser_button_id = AddPlatformApp();
736 // Add app shortcut until overflow.
737 int items_added = 0;
738 ShelfID last_added = AddAppShortcut();
739 while (!test_api_->IsOverflowButtonVisible()) {
740 // Added button is visible after animation while in this loop.
741 EXPECT_TRUE(GetButtonByID(last_added)->visible());
743 last_added = AddAppShortcut();
744 ++items_added;
745 ASSERT_LT(items_added, 10000);
748 // And the platform app button is invisible.
749 EXPECT_FALSE(GetButtonByID(browser_button_id)->visible());
752 TEST_F(ShelfViewTest, AddPanelHidesPlatformAppButton) {
753 // All buttons should be visible.
754 ASSERT_EQ(test_api_->GetButtonCount(),
755 test_api_->GetLastVisibleIndex() + 1);
757 // Add platform app button until overflow, remember last visible platform app
758 // button.
759 int items_added = 0;
760 ShelfID first_added = AddPlatformApp();
761 EXPECT_TRUE(GetButtonByID(first_added)->visible());
762 while (true) {
763 ShelfID added = AddPlatformApp();
764 if (test_api_->IsOverflowButtonVisible()) {
765 EXPECT_FALSE(GetButtonByID(added)->visible());
766 RemoveByID(added);
767 break;
769 ++items_added;
770 ASSERT_LT(items_added, 10000);
773 ShelfID panel = AddPanel();
774 EXPECT_TRUE(test_api_->IsOverflowButtonVisible());
776 RemoveByID(panel);
777 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
780 // When there are more panels then platform app buttons we should hide panels
781 // rather than platform apps.
782 TEST_F(ShelfViewTest, PlatformAppHidesExcessPanels) {
783 // All buttons should be visible.
784 ASSERT_EQ(test_api_->GetButtonCount(),
785 test_api_->GetLastVisibleIndex() + 1);
787 // Add platform app button.
788 ShelfID platform_app = AddPlatformApp();
789 ShelfID first_panel = AddPanel();
791 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
792 EXPECT_TRUE(GetButtonByID(first_panel)->visible());
794 // Add panels until there is an overflow.
795 ShelfID last_panel = first_panel;
796 int items_added = 0;
797 while (!test_api_->IsOverflowButtonVisible()) {
798 last_panel = AddPanel();
799 ++items_added;
800 ASSERT_LT(items_added, 10000);
803 // The first panel should now be hidden by the new platform apps needing
804 // space.
805 EXPECT_FALSE(GetButtonByID(first_panel)->visible());
806 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
807 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
809 // Adding platform apps should eventually begin to hide platform apps. We will
810 // add platform apps until either the last panel or platform app is hidden.
811 items_added = 0;
812 while (GetButtonByID(platform_app)->visible() &&
813 GetButtonByID(last_panel)->visible()) {
814 platform_app = AddPlatformApp();
815 ++items_added;
816 ASSERT_LT(items_added, 10000);
818 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
819 EXPECT_FALSE(GetButtonByID(platform_app)->visible());
822 // Adds button until overflow then removes first added one. Verifies that
823 // the last added one changes from invisible to visible and overflow
824 // chevron is gone.
825 TEST_F(ShelfViewTest, RemoveButtonRevealsOverflowed) {
826 // All buttons should be visible.
827 ASSERT_EQ(test_api_->GetButtonCount(),
828 test_api_->GetLastVisibleIndex() + 1);
830 // Add platform app buttons until overflow.
831 int items_added = 0;
832 ShelfID first_added = AddPlatformApp();
833 ShelfID last_added = first_added;
834 while (!test_api_->IsOverflowButtonVisible()) {
835 last_added = AddPlatformApp();
836 ++items_added;
837 ASSERT_LT(items_added, 10000);
840 // Expect add more than 1 button. First added is visible and last is not.
841 EXPECT_NE(first_added, last_added);
842 EXPECT_TRUE(GetButtonByID(first_added)->visible());
843 EXPECT_FALSE(GetButtonByID(last_added)->visible());
845 // Remove first added.
846 RemoveByID(first_added);
848 // Last added button becomes visible and overflow chevron is gone.
849 EXPECT_TRUE(GetButtonByID(last_added)->visible());
850 EXPECT_EQ(1.0f, GetButtonByID(last_added)->layer()->opacity());
851 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
854 // Verifies that remove last overflowed button should hide overflow chevron.
855 TEST_F(ShelfViewTest, RemoveLastOverflowed) {
856 // All buttons should be visible.
857 ASSERT_EQ(test_api_->GetButtonCount(),
858 test_api_->GetLastVisibleIndex() + 1);
860 // Add platform app button until overflow.
861 int items_added = 0;
862 ShelfID last_added = AddPlatformApp();
863 while (!test_api_->IsOverflowButtonVisible()) {
864 last_added = AddPlatformApp();
865 ++items_added;
866 ASSERT_LT(items_added, 10000);
869 RemoveByID(last_added);
870 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
873 // Adds platform app button without waiting for animation to finish and verifies
874 // that all added buttons are visible.
875 TEST_F(ShelfViewTest, AddButtonQuickly) {
876 // All buttons should be visible.
877 ASSERT_EQ(test_api_->GetButtonCount(),
878 test_api_->GetLastVisibleIndex() + 1);
880 // Add a few platform buttons quickly without wait for animation.
881 int added_count = 0;
882 while (!test_api_->IsOverflowButtonVisible()) {
883 AddPlatformAppNoWait();
884 ++added_count;
885 ASSERT_LT(added_count, 10000);
888 // ShelfView should be big enough to hold at least 3 new buttons.
889 ASSERT_GE(added_count, 3);
891 // Wait for the last animation to finish.
892 test_api_->RunMessageLoopUntilAnimationsDone();
894 // Verifies non-overflow buttons are visible.
895 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
896 ShelfButton* button = test_api_->GetButton(i);
897 if (button) {
898 EXPECT_TRUE(button->visible()) << "button index=" << i;
899 EXPECT_EQ(1.0f, button->layer()->opacity()) << "button index=" << i;
904 // Check that model changes are handled correctly while a shelf icon is being
905 // dragged.
906 TEST_F(ShelfViewTest, ModelChangesWhileDragging) {
907 ShelfButtonHost* button_host = shelf_view_;
909 std::vector<std::pair<ShelfID, views::View*> > id_map;
910 SetupForDragTest(&id_map);
912 // Dragging browser shortcut at index 1.
913 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
914 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
915 std::rotate(id_map.begin() + 1,
916 id_map.begin() + 2,
917 id_map.begin() + 4);
918 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
919 button_host->PointerReleasedOnButton(
920 dragged_button, ShelfButtonHost::MOUSE, false);
921 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
923 // Dragging changes model order.
924 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
925 std::rotate(id_map.begin() + 1,
926 id_map.begin() + 2,
927 id_map.begin() + 4);
928 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
930 // Cancelling the drag operation restores previous order.
931 button_host->PointerReleasedOnButton(
932 dragged_button, ShelfButtonHost::MOUSE, true);
933 std::rotate(id_map.begin() + 1,
934 id_map.begin() + 3,
935 id_map.begin() + 4);
936 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
938 // Deleting an item keeps the remaining intact.
939 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
940 model_->RemoveItemAt(1);
941 id_map.erase(id_map.begin() + 1);
942 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
943 button_host->PointerReleasedOnButton(
944 dragged_button, ShelfButtonHost::MOUSE, false);
946 // Adding a shelf item cancels the drag and respects the order.
947 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
948 ShelfID new_id = AddAppShortcut();
949 id_map.insert(id_map.begin() + 6,
950 std::make_pair(new_id, GetButtonByID(new_id)));
951 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
952 button_host->PointerReleasedOnButton(
953 dragged_button, ShelfButtonHost::MOUSE, false);
955 // Adding a shelf item at the end (i.e. a panel) canels drag and respects
956 // the order.
957 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
958 new_id = AddPanel();
959 id_map.insert(id_map.begin() + 7,
960 std::make_pair(new_id, GetButtonByID(new_id)));
961 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
962 button_host->PointerReleasedOnButton(
963 dragged_button, ShelfButtonHost::MOUSE, false);
966 // Check that 2nd drag from the other pointer would be ignored.
967 TEST_F(ShelfViewTest, SimultaneousDrag) {
968 ShelfButtonHost* button_host = shelf_view_;
970 std::vector<std::pair<ShelfID, views::View*> > id_map;
971 SetupForDragTest(&id_map);
973 // Start a mouse drag.
974 views::View* dragged_button_mouse =
975 SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
976 std::rotate(id_map.begin() + 1,
977 id_map.begin() + 2,
978 id_map.begin() + 4);
979 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
980 // Attempt a touch drag before the mouse drag finishes.
981 views::View* dragged_button_touch =
982 SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
984 // Nothing changes since 2nd drag is ignored.
985 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
987 // Finish the mouse drag.
988 button_host->PointerReleasedOnButton(
989 dragged_button_mouse, ShelfButtonHost::MOUSE, false);
990 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
992 // Now start a touch drag.
993 dragged_button_touch = SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
994 std::rotate(id_map.begin() + 3,
995 id_map.begin() + 4,
996 id_map.begin() + 5);
997 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
999 // And attempt a mouse drag before the touch drag finishes.
1000 dragged_button_mouse = SimulateDrag(ShelfButtonHost::MOUSE, 1, 2);
1002 // Nothing changes since 2nd drag is ignored.
1003 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1005 button_host->PointerReleasedOnButton(
1006 dragged_button_touch, ShelfButtonHost::TOUCH, false);
1007 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1010 // Check that clicking first on one item and then dragging another works as
1011 // expected.
1012 TEST_F(ShelfViewTest, ClickOneDragAnother) {
1013 ShelfButtonHost* button_host = shelf_view_;
1015 std::vector<std::pair<ShelfID, views::View*> > id_map;
1016 SetupForDragTest(&id_map);
1018 // A click on item 1 is simulated.
1019 SimulateClick(ShelfButtonHost::MOUSE, 1);
1021 // Dragging browser index at 0 should change the model order correctly.
1022 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
1023 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1024 std::rotate(id_map.begin() + 1,
1025 id_map.begin() + 2,
1026 id_map.begin() + 4);
1027 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1028 button_host->PointerReleasedOnButton(
1029 dragged_button, ShelfButtonHost::MOUSE, false);
1030 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
1033 // Confirm that item status changes are reflected in the buttons.
1034 TEST_F(ShelfViewTest, ShelfItemStatus) {
1035 // All buttons should be visible.
1036 ASSERT_EQ(test_api_->GetButtonCount(),
1037 test_api_->GetLastVisibleIndex() + 1);
1039 // Add platform app button.
1040 ShelfID last_added = AddPlatformApp();
1041 ShelfItem item = GetItemByID(last_added);
1042 int index = model_->ItemIndexByID(last_added);
1043 ShelfButton* button = GetButtonByID(last_added);
1044 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1045 item.status = STATUS_ACTIVE;
1046 model_->Set(index, item);
1047 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1048 item.status = STATUS_ATTENTION;
1049 model_->Set(index, item);
1050 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1053 // Confirm that item status changes are reflected in the buttons
1054 // for platform apps.
1055 TEST_F(ShelfViewTest, ShelfItemStatusPlatformApp) {
1056 // All buttons should be visible.
1057 ASSERT_EQ(test_api_->GetButtonCount(),
1058 test_api_->GetLastVisibleIndex() + 1);
1060 // Add platform app button.
1061 ShelfID last_added = AddPlatformApp();
1062 ShelfItem item = GetItemByID(last_added);
1063 int index = model_->ItemIndexByID(last_added);
1064 ShelfButton* button = GetButtonByID(last_added);
1065 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1066 item.status = STATUS_ACTIVE;
1067 model_->Set(index, item);
1068 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1069 item.status = STATUS_ATTENTION;
1070 model_->Set(index, item);
1071 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1074 // Confirm that shelf item bounds are correctly updated on shelf changes.
1075 TEST_F(ShelfViewTest, ShelfItemBoundsCheck) {
1076 VerifyShelfItemBoundsAreValid();
1077 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1078 SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
1079 test_api_->RunMessageLoopUntilAnimationsDone();
1080 VerifyShelfItemBoundsAreValid();
1081 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1082 SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
1083 test_api_->RunMessageLoopUntilAnimationsDone();
1084 VerifyShelfItemBoundsAreValid();
1087 TEST_F(ShelfViewTest, ShelfTooltipTest) {
1088 ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
1089 test_api_->GetButtonCount());
1091 // Prepare some items to the shelf.
1092 ShelfID app_button_id = AddAppShortcut();
1093 ShelfID platform_button_id = AddPlatformApp();
1095 ShelfButton* app_button = GetButtonByID(app_button_id);
1096 ShelfButton* platform_button = GetButtonByID(platform_button_id);
1098 ShelfButtonHost* button_host = shelf_view_;
1099 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1101 button_host->MouseEnteredButton(app_button);
1102 // There's a delay to show the tooltip, so it's not visible yet.
1103 EXPECT_FALSE(tooltip_manager->IsVisible());
1104 EXPECT_EQ(app_button, GetTooltipAnchorView());
1106 ShowTooltip();
1107 EXPECT_TRUE(tooltip_manager->IsVisible());
1109 // Once it's visible, it keeps visibility and is pointing to the same
1110 // item.
1111 button_host->MouseExitedButton(app_button);
1112 EXPECT_TRUE(tooltip_manager->IsVisible());
1113 EXPECT_EQ(app_button, GetTooltipAnchorView());
1115 // When entered to another item, it switches to the new item. There is no
1116 // delay for the visibility.
1117 button_host->MouseEnteredButton(platform_button);
1118 EXPECT_TRUE(tooltip_manager->IsVisible());
1119 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1121 button_host->MouseExitedButton(platform_button);
1122 tooltip_manager->Close();
1124 // Next time: enter app_button -> move immediately to tab_button.
1125 button_host->MouseEnteredButton(app_button);
1126 button_host->MouseExitedButton(app_button);
1127 button_host->MouseEnteredButton(platform_button);
1128 EXPECT_FALSE(tooltip_manager->IsVisible());
1129 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1132 // Verify a fix for crash caused by a tooltip update for a deletedshelf
1133 // button, see crbug.com/288838.
1134 TEST_F(ShelfViewTest, RemovingItemClosesTooltip) {
1135 ShelfButtonHost* button_host = shelf_view_;
1136 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1138 // Add an item to the shelf.
1139 ShelfID app_button_id = AddAppShortcut();
1140 ShelfButton* app_button = GetButtonByID(app_button_id);
1142 // Spawn a tooltip on that item.
1143 button_host->MouseEnteredButton(app_button);
1144 ShowTooltip();
1145 EXPECT_TRUE(tooltip_manager->IsVisible());
1147 // Remove the app shortcut while the tooltip is open. The tooltip should be
1148 // closed.
1149 RemoveByID(app_button_id);
1150 EXPECT_FALSE(tooltip_manager->IsVisible());
1152 // Change the shelf layout. This should not crash.
1153 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1154 Shell::GetPrimaryRootWindow());
1157 // Changing the shelf alignment closes any open tooltip.
1158 TEST_F(ShelfViewTest, ShelfAlignmentClosesTooltip) {
1159 ShelfButtonHost* button_host = shelf_view_;
1160 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1162 // Add an item to the shelf.
1163 ShelfID app_button_id = AddAppShortcut();
1164 ShelfButton* app_button = GetButtonByID(app_button_id);
1166 // Spawn a tooltip on the item.
1167 button_host->MouseEnteredButton(app_button);
1168 ShowTooltip();
1169 EXPECT_TRUE(tooltip_manager->IsVisible());
1171 // Changing shelf alignment hides the tooltip.
1172 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1173 Shell::GetPrimaryRootWindow());
1174 EXPECT_FALSE(tooltip_manager->IsVisible());
1177 TEST_F(ShelfViewTest, ShouldHideTooltipTest) {
1178 ShelfID app_button_id = AddAppShortcut();
1179 ShelfID platform_button_id = AddPlatformApp();
1181 // The tooltip shouldn't hide if the mouse is on normal buttons.
1182 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1183 ShelfButton* button = test_api_->GetButton(i);
1184 if (!button)
1185 continue;
1187 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1188 button->GetMirroredBounds().CenterPoint()))
1189 << "ShelfView tries to hide on button " << i;
1192 // The tooltip should not hide on the app-list button.
1193 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1194 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1195 app_list_button->GetMirroredBounds().CenterPoint()));
1197 // The tooltip shouldn't hide if the mouse is in the gap between two buttons.
1198 gfx::Rect app_button_rect = GetButtonByID(app_button_id)->GetMirroredBounds();
1199 gfx::Rect platform_button_rect =
1200 GetButtonByID(platform_button_id)->GetMirroredBounds();
1201 ASSERT_FALSE(app_button_rect.Intersects(platform_button_rect));
1202 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1203 gfx::UnionRects(app_button_rect, platform_button_rect).CenterPoint()));
1205 // The tooltip should hide if it's outside of all buttons.
1206 gfx::Rect all_area;
1207 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1208 ShelfButton* button = test_api_->GetButton(i);
1209 if (!button)
1210 continue;
1212 all_area.Union(button->GetMirroredBounds());
1214 all_area.Union(shelf_view_->GetAppListButtonView()->GetMirroredBounds());
1215 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(all_area.origin()));
1216 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1217 gfx::Point(all_area.right() - 1, all_area.bottom() - 1)));
1218 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1219 gfx::Point(all_area.right(), all_area.y())));
1220 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1221 gfx::Point(all_area.x() - 1, all_area.y())));
1222 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1223 gfx::Point(all_area.x(), all_area.y() - 1)));
1224 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1225 gfx::Point(all_area.x(), all_area.bottom())));
1228 TEST_F(ShelfViewTest, ShouldHideTooltipWithAppListWindowTest) {
1229 Shell::GetInstance()->ToggleAppList(NULL);
1230 ASSERT_TRUE(Shell::GetInstance()->GetAppListWindow());
1232 // The tooltip shouldn't hide if the mouse is on normal buttons.
1233 for (int i = 1; i < test_api_->GetButtonCount(); i++) {
1234 ShelfButton* button = test_api_->GetButton(i);
1235 if (!button)
1236 continue;
1238 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1239 button->GetMirroredBounds().CenterPoint()))
1240 << "ShelfView tries to hide on button " << i;
1243 // The tooltip should hide on the app-list button.
1244 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1245 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1246 app_list_button->GetMirroredBounds().CenterPoint()));
1249 // Test that by moving the mouse cursor off the button onto the bubble it closes
1250 // the bubble.
1251 TEST_F(ShelfViewTest, ShouldHideTooltipWhenHoveringOnTooltip) {
1252 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1253 tooltip_manager->CreateZeroDelayTimerForTest();
1254 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1256 // Move the mouse off any item and check that no tooltip is shown.
1257 generator.MoveMouseTo(gfx::Point(0, 0));
1258 EXPECT_FALSE(tooltip_manager->IsVisible());
1260 // Move the mouse over the button and check that it is visible.
1261 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1262 gfx::Rect bounds = app_list_button->GetBoundsInScreen();
1263 generator.MoveMouseTo(bounds.CenterPoint());
1264 // Wait for the timer to go off.
1265 RunAllPendingInMessageLoop();
1266 EXPECT_TRUE(tooltip_manager->IsVisible());
1268 // Move the mouse cursor slightly to the right of the item. The tooltip should
1269 // stay open.
1270 generator.MoveMouseBy(bounds.width() / 2 + 5, 0);
1271 // Make sure there is no delayed close.
1272 RunAllPendingInMessageLoop();
1273 EXPECT_TRUE(tooltip_manager->IsVisible());
1275 // Move back - it should still stay open.
1276 generator.MoveMouseBy(-(bounds.width() / 2 + 5), 0);
1277 // Make sure there is no delayed close.
1278 RunAllPendingInMessageLoop();
1279 EXPECT_TRUE(tooltip_manager->IsVisible());
1281 // Now move the mouse cursor slightly above the item - so that it is over the
1282 // tooltip bubble. Now it should disappear.
1283 generator.MoveMouseBy(0, -(bounds.height() / 2 + 5));
1284 // Wait until the delayed close kicked in.
1285 RunAllPendingInMessageLoop();
1286 EXPECT_FALSE(tooltip_manager->IsVisible());
1289 // Resizing shelf view while an add animation without fade-in is running,
1290 // which happens when overflow happens. App list button should end up in its
1291 // new ideal bounds.
1292 TEST_F(ShelfViewTest, ResizeDuringOverflowAddAnimation) {
1293 // All buttons should be visible.
1294 ASSERT_EQ(test_api_->GetButtonCount(),
1295 test_api_->GetLastVisibleIndex() + 1);
1297 // Add buttons until overflow. Let the non-overflow add animations finish but
1298 // leave the last running.
1299 int items_added = 0;
1300 AddPlatformAppNoWait();
1301 while (!test_api_->IsOverflowButtonVisible()) {
1302 test_api_->RunMessageLoopUntilAnimationsDone();
1303 AddPlatformAppNoWait();
1304 ++items_added;
1305 ASSERT_LT(items_added, 10000);
1308 // Resize shelf view with that animation running and stay overflown.
1309 gfx::Rect bounds = shelf_view_->bounds();
1310 bounds.set_width(bounds.width() - kShelfSize);
1311 shelf_view_->SetBoundsRect(bounds);
1312 ASSERT_TRUE(test_api_->IsOverflowButtonVisible());
1314 // Finish the animation.
1315 test_api_->RunMessageLoopUntilAnimationsDone();
1317 // App list button should ends up in its new ideal bounds.
1318 const int app_list_button_index = test_api_->GetButtonCount() - 1;
1319 const gfx::Rect& app_list_ideal_bounds =
1320 test_api_->GetIdealBoundsByIndex(app_list_button_index);
1321 const gfx::Rect& app_list_bounds =
1322 test_api_->GetBoundsByIndex(app_list_button_index);
1323 EXPECT_EQ(app_list_ideal_bounds, app_list_bounds);
1326 // Checks the overflow bubble size when an item is ripped off and re-inserted.
1327 TEST_F(ShelfViewTest, OverflowBubbleSize) {
1328 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1329 ReplaceShelfDelegateForRipOffTest();
1331 AddButtonsUntilOverflow();
1333 // Show overflow bubble.
1334 test_api_->ShowOverflowBubble();
1335 ASSERT_TRUE(test_api_->overflow_bubble() &&
1336 test_api_->overflow_bubble()->IsShowing());
1338 ShelfViewTestAPI test_for_overflow_view(
1339 test_api_->overflow_bubble()->shelf_view());
1341 int ripped_index = test_for_overflow_view.GetLastVisibleIndex();
1342 gfx::Size bubble_size = test_for_overflow_view.GetPreferredSize();
1343 int item_width = test_for_overflow_view.GetButtonSize() +
1344 test_for_overflow_view.GetButtonSpacing();
1346 aura::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
1347 gfx::Point());
1348 ShelfButton* button = test_for_overflow_view.GetButton(ripped_index);
1349 // Rip off the last visible item.
1350 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1351 gfx::Point rip_off_point(start_point.x(), 0);
1352 generator.MoveMouseTo(start_point.x(), start_point.y());
1353 base::MessageLoop::current()->RunUntilIdle();
1354 generator.PressLeftButton();
1355 base::MessageLoop::current()->RunUntilIdle();
1356 generator.MoveMouseTo(rip_off_point.x(), rip_off_point.y());
1357 base::MessageLoop::current()->RunUntilIdle();
1358 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1360 // Check the overflow bubble size when an item is ripped off.
1361 EXPECT_EQ(bubble_size.width() - item_width,
1362 test_for_overflow_view.GetPreferredSize().width());
1363 ASSERT_TRUE(test_api_->overflow_bubble() &&
1364 test_api_->overflow_bubble()->IsShowing());
1366 // Re-insert an item into the overflow bubble.
1367 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1368 button = test_for_overflow_view.GetButton(first_index);
1370 // Check the bubble size after an item is re-inserted.
1371 generator.MoveMouseTo(button->GetBoundsInScreen().CenterPoint());
1372 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1373 EXPECT_EQ(bubble_size.width(),
1374 test_for_overflow_view.GetPreferredSize().width());
1376 generator.ReleaseLeftButton();
1377 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1378 EXPECT_EQ(bubble_size.width(),
1379 test_for_overflow_view.GetPreferredSize().width());
1382 // Check the drag insertion bounds of scrolled overflow bubble.
1383 TEST_F(ShelfViewTest, CheckDragInsertBoundsOfScrolledOverflowBubble) {
1384 UpdateDisplay("400x300");
1386 EXPECT_EQ(2, model_->item_count());
1388 AddButtonsUntilOverflow();
1390 // Show overflow bubble.
1391 test_api_->ShowOverflowBubble();
1392 ASSERT_TRUE(test_api_->overflow_bubble() &&
1393 test_api_->overflow_bubble()->IsShowing());
1395 int item_width = test_api_->GetButtonSize() +
1396 test_api_->GetButtonSpacing();
1397 OverflowBubbleView* bubble_view = test_api_->overflow_bubble()->bubble_view();
1398 test::OverflowBubbleViewTestAPI bubble_view_api(bubble_view);
1400 // Add more buttons until OverflowBubble is scrollable and it has 3 invisible
1401 // items.
1402 while (bubble_view_api.GetContentsSize().width() <
1403 (bubble_view->GetContentsBounds().width() + 3 * item_width))
1404 AddAppShortcut();
1406 ASSERT_TRUE(test_api_->overflow_bubble() &&
1407 test_api_->overflow_bubble()->IsShowing());
1409 ShelfViewTestAPI test_for_overflow_view(
1410 test_api_->overflow_bubble()->shelf_view());
1411 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1412 int last_index = test_for_overflow_view.GetLastVisibleIndex();
1414 ShelfButton* first_button = test_for_overflow_view.GetButton(first_index);
1415 ShelfButton* last_button = test_for_overflow_view.GetButton(last_index);
1416 gfx::Point first_point = first_button->GetBoundsInScreen().CenterPoint();
1417 gfx::Point last_point = last_button->GetBoundsInScreen().CenterPoint();
1418 gfx::Rect drag_reinsert_bounds =
1419 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1420 EXPECT_TRUE(drag_reinsert_bounds.Contains(first_point));
1421 EXPECT_FALSE(drag_reinsert_bounds.Contains(last_point));
1423 // Scrolls sufficiently to show last item.
1424 bubble_view_api.ScrollByXOffset(3 * item_width);
1425 drag_reinsert_bounds =
1426 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1427 first_point = first_button->GetBoundsInScreen().CenterPoint();
1428 last_point = last_button->GetBoundsInScreen().CenterPoint();
1429 EXPECT_FALSE(drag_reinsert_bounds.Contains(first_point));
1430 EXPECT_TRUE(drag_reinsert_bounds.Contains(last_point));
1433 // Check the drag insertion bounds of shelf view in multi monitor environment.
1434 TEST_F(ShelfViewTest, CheckDragInsertBoundsWithMultiMonitor) {
1435 // win8-aura doesn't support multiple display.
1436 if (!SupportsMultipleDisplays())
1437 return;
1439 UpdateDisplay("800x600,800x600");
1440 Shelf* secondary_shelf = Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
1441 ShelfView* shelf_view_for_secondary =
1442 ShelfTestAPI(secondary_shelf).shelf_view();
1444 // The bounds should be big enough for 4 buttons + overflow chevron.
1445 shelf_view_for_secondary->SetBounds(0, 0, 500, kShelfSize);
1447 ShelfViewTestAPI test_api_for_secondary(shelf_view_for_secondary);
1448 // Speeds up animation for test.
1449 test_api_for_secondary.SetAnimationDuration(1);
1451 AddButtonsUntilOverflow();
1453 // Test #1: Test drag insertion bounds of primary shelf.
1454 // Show overflow bubble.
1455 test_api_->ShowOverflowBubble();
1456 ASSERT_TRUE(test_api_->overflow_bubble() &&
1457 test_api_->overflow_bubble()->IsShowing());
1459 ShelfViewTestAPI test_api_for_overflow_view(
1460 test_api_->overflow_bubble()->shelf_view());
1462 ShelfButton* button = test_api_for_overflow_view.GetButton(
1463 test_api_for_overflow_view.GetLastVisibleIndex());
1465 // Checks that a point in shelf is contained in drag insert bounds.
1466 gfx::Point point_in_shelf_view = button->GetBoundsInScreen().CenterPoint();
1467 gfx::Rect drag_reinsert_bounds =
1468 test_api_for_overflow_view.GetBoundsForDragInsertInScreen();
1469 EXPECT_TRUE(drag_reinsert_bounds.Contains(point_in_shelf_view));
1470 // Checks that a point out of shelf is not contained in drag insert bounds.
1471 EXPECT_FALSE(drag_reinsert_bounds.Contains(
1472 gfx::Point(point_in_shelf_view.x(), 0)));
1474 // Test #2: Test drag insertion bounds of secondary shelf.
1475 // Show overflow bubble.
1476 test_api_for_secondary.ShowOverflowBubble();
1477 ASSERT_TRUE(test_api_for_secondary.overflow_bubble() &&
1478 test_api_for_secondary.overflow_bubble()->IsShowing());
1480 ShelfViewTestAPI test_api_for_overflow_view_of_secondary(
1481 test_api_for_secondary.overflow_bubble()->shelf_view());
1483 ShelfButton* button_in_secondary =
1484 test_api_for_overflow_view_of_secondary.GetButton(
1485 test_api_for_overflow_view_of_secondary.GetLastVisibleIndex());
1487 // Checks that a point in shelf is contained in drag insert bounds.
1488 gfx::Point point_in_secondary_shelf_view =
1489 button_in_secondary->GetBoundsInScreen().CenterPoint();
1490 gfx::Rect drag_reinsert_bounds_in_secondary =
1491 test_api_for_overflow_view_of_secondary.GetBoundsForDragInsertInScreen();
1492 EXPECT_TRUE(drag_reinsert_bounds_in_secondary.Contains(
1493 point_in_secondary_shelf_view));
1494 // Checks that a point out of shelf is not contained in drag insert bounds.
1495 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(
1496 gfx::Point(point_in_secondary_shelf_view.x(), 0)));
1497 // Checks that a point of overflow bubble in primary shelf should not be
1498 // contained by insert bounds of secondary shelf.
1499 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(point_in_shelf_view));
1502 // Checks the rip an item off from left aligned shelf in secondary monitor.
1503 TEST_F(ShelfViewTest, CheckRipOffFromLeftShelfAlignmentWithMultiMonitor) {
1504 // win8-aura doesn't support multiple display.
1505 if (!SupportsMultipleDisplays())
1506 return;
1508 UpdateDisplay("800x600,800x600");
1509 ASSERT_EQ(2U, Shell::GetAllRootWindows().size());
1511 aura::Window* second_root = Shell::GetAllRootWindows()[1];
1513 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT, second_root);
1514 ASSERT_EQ(SHELF_ALIGNMENT_LEFT,
1515 Shell::GetInstance()->GetShelfAlignment(second_root));
1517 // Initially, app list and browser shortcut are added.
1518 EXPECT_EQ(2, model_->item_count());
1519 int browser_index = model_->GetItemIndexForType(TYPE_BROWSER_SHORTCUT);
1520 EXPECT_GT(browser_index, 0);
1522 Shelf* secondary_shelf = Shelf::ForWindow(second_root);
1523 ShelfView* shelf_view_for_secondary =
1524 ShelfTestAPI(secondary_shelf).shelf_view();
1526 ShelfViewTestAPI test_api_for_secondary_shelf_view(shelf_view_for_secondary);
1527 ShelfButton* button =
1528 test_api_for_secondary_shelf_view.GetButton(browser_index);
1530 // Fetch the start point of dragging.
1531 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1532 wm::ConvertPointFromScreen(second_root, &start_point);
1534 aura::test::EventGenerator generator(second_root, start_point);
1536 // Rip off the browser item.
1537 generator.PressLeftButton();
1538 generator.MoveMouseTo(start_point.x() + 400, start_point.y());
1539 test_api_for_secondary_shelf_view.RunMessageLoopUntilAnimationsDone();
1540 EXPECT_TRUE(test_api_for_secondary_shelf_view.IsRippedOffFromShelf());
1543 // Checks various drag and drop operations from OverflowBubble to Shelf.
1544 TEST_F(ShelfViewTest, CheckDragAndDropFromOverflowBubbleToShelf) {
1545 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1546 ReplaceShelfDelegateForRipOffTest();
1548 AddButtonsUntilOverflow();
1550 TestDraggingAnItemFromOverflowToShelf(false);
1551 TestDraggingAnItemFromOverflowToShelf(true);
1554 class ShelfViewVisibleBoundsTest : public ShelfViewTest,
1555 public testing::WithParamInterface<bool> {
1556 public:
1557 ShelfViewVisibleBoundsTest() : text_direction_change_(GetParam()) {}
1559 void CheckAllItemsAreInBounds() {
1560 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1561 gfx::Rect shelf_bounds = shelf_view_->GetBoundsInScreen();
1562 EXPECT_TRUE(shelf_bounds.Contains(visible_bounds));
1563 for (int i = 0; i < test_api_->GetButtonCount(); ++i)
1564 if (ShelfButton* button = test_api_->GetButton(i))
1565 EXPECT_TRUE(visible_bounds.Contains(button->GetBoundsInScreen()));
1566 CheckAppListButtonIsInBounds();
1569 void CheckAppListButtonIsInBounds() {
1570 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1571 gfx::Rect app_list_button_bounds = shelf_view_->GetAppListButtonView()->
1572 GetBoundsInScreen();
1573 EXPECT_TRUE(visible_bounds.Contains(app_list_button_bounds));
1576 private:
1577 ScopedTextDirectionChange text_direction_change_;
1579 DISALLOW_COPY_AND_ASSIGN(ShelfViewVisibleBoundsTest);
1582 TEST_P(ShelfViewVisibleBoundsTest, ItemsAreInBounds) {
1583 // Adding elements leaving some empty space.
1584 for (int i = 0; i < 3; i++) {
1585 AddAppShortcut();
1587 test_api_->RunMessageLoopUntilAnimationsDone();
1588 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
1589 CheckAllItemsAreInBounds();
1590 // Same for overflow case.
1591 while (!test_api_->IsOverflowButtonVisible()) {
1592 AddAppShortcut();
1594 test_api_->RunMessageLoopUntilAnimationsDone();
1595 CheckAllItemsAreInBounds();
1598 INSTANTIATE_TEST_CASE_P(LtrRtl, ShelfViewTextDirectionTest, testing::Bool());
1599 INSTANTIATE_TEST_CASE_P(VisibleBounds, ShelfViewVisibleBoundsTest,
1600 testing::Bool());
1602 } // namespace test
1603 } // namespace ash