Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ash / shelf / shelf_view_unittest.cc
blobe4a79f70b6b8fb8684bebcb5e8240c508c5ac58c
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 "base/basictypes.h"
33 #include "base/command_line.h"
34 #include "base/compiler_specific.h"
35 #include "base/memory/scoped_ptr.h"
36 #include "base/strings/string_number_conversions.h"
37 #include "grit/ash_resources.h"
38 #include "ui/aura/test/aura_test_base.h"
39 #include "ui/aura/window.h"
40 #include "ui/aura/window_event_dispatcher.h"
41 #include "ui/base/l10n/l10n_util.h"
42 #include "ui/compositor/layer.h"
43 #include "ui/events/event.h"
44 #include "ui/events/event_constants.h"
45 #include "ui/events/test/event_generator.h"
46 #include "ui/views/view_model.h"
47 #include "ui/views/widget/widget.h"
48 #include "ui/views/widget/widget_delegate.h"
49 #include "ui/wm/core/coordinate_conversion.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 // TestShelfItemDelegate which tracks whether it gets selected.
124 class ShelfItemSelectionTracker : public TestShelfItemDelegate {
125 public:
126 ShelfItemSelectionTracker() : TestShelfItemDelegate(NULL), selected_(false) {
129 virtual ~ShelfItemSelectionTracker() {
132 // Resets to the initial state.
133 void Reset() { selected_ = false; }
135 // Returns true if the delegate was selected.
136 bool WasSelected() {
137 return selected_;
140 // TestShelfItemDelegate:
141 virtual bool ItemSelected(const ui::Event& event) OVERRIDE {
142 selected_ = true;
143 return false;
146 private:
147 bool selected_;
149 DISALLOW_COPY_AND_ASSIGN(ShelfItemSelectionTracker);
152 TEST_F(ShelfViewIconObserverTest, AddRemove) {
153 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
154 ASSERT_TRUE(shelf_delegate);
156 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
157 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
158 params.bounds = gfx::Rect(0, 0, 200, 200);
159 params.context = CurrentContext();
161 scoped_ptr<views::Widget> widget(new views::Widget());
162 widget->Init(params);
163 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
164 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
165 EXPECT_TRUE(observer()->change_notified());
166 observer()->Reset();
168 widget->Show();
169 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
170 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
171 EXPECT_TRUE(observer()->change_notified());
172 observer()->Reset();
175 // Sometimes fails on trybots on win7_aura. http://crbug.com/177135
176 #if defined(OS_WIN)
177 #define MAYBE_AddRemoveWithMultipleDisplays \
178 DISABLED_AddRemoveWithMultipleDisplays
179 #else
180 #define MAYBE_AddRemoveWithMultipleDisplays \
181 AddRemoveWithMultipleDisplays
182 #endif
183 // Make sure creating/deleting an window on one displays notifies a
184 // shelf on external display as well as one on primary.
185 TEST_F(ShelfViewIconObserverTest, MAYBE_AddRemoveWithMultipleDisplays) {
186 UpdateDisplay("400x400,400x400");
187 TestShelfIconObserver second_observer(ShelfForSecondaryDisplay());
189 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
190 ASSERT_TRUE(shelf_delegate);
192 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
193 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
194 params.bounds = gfx::Rect(0, 0, 200, 200);
195 params.context = CurrentContext();
197 scoped_ptr<views::Widget> widget(new views::Widget());
198 widget->Init(params);
199 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
200 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
201 EXPECT_TRUE(observer()->change_notified());
202 EXPECT_TRUE(second_observer.change_notified());
203 observer()->Reset();
204 second_observer.Reset();
206 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
207 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
208 EXPECT_TRUE(observer()->change_notified());
209 EXPECT_TRUE(second_observer.change_notified());
211 observer()->Reset();
212 second_observer.Reset();
215 TEST_F(ShelfViewIconObserverTest, BoundsChanged) {
216 ShelfWidget* widget = Shell::GetPrimaryRootWindowController()->shelf();
217 Shelf* shelf = Shelf::ForPrimaryDisplay();
218 gfx::Size shelf_size = widget->GetWindowBoundsInScreen().size();
219 shelf_size.set_width(shelf_size.width() / 2);
220 ASSERT_GT(shelf_size.width(), 0);
221 shelf->SetShelfViewBounds(gfx::Rect(shelf_size));
222 // No animation happens for ShelfView bounds change.
223 EXPECT_TRUE(observer()->change_notified());
224 observer()->Reset();
227 ////////////////////////////////////////////////////////////////////////////////
228 // ShelfView tests.
230 // Simple ShelfDelegate implmentation for ShelfViewTest.OverflowBubbleSize
231 // and CheckDragAndDropFromOverflowBubbleToShelf
232 class TestShelfDelegateForShelfView : public ShelfDelegate {
233 public:
234 explicit TestShelfDelegateForShelfView(ShelfModel* model)
235 : model_(model) {}
236 virtual ~TestShelfDelegateForShelfView() {}
238 // ShelfDelegate overrides:
239 virtual void OnShelfCreated(Shelf* shelf) OVERRIDE {}
241 virtual void OnShelfDestroyed(Shelf* shelf) OVERRIDE {}
243 virtual ShelfID GetShelfIDForAppID(const std::string& app_id) OVERRIDE {
244 ShelfID id = 0;
245 EXPECT_TRUE(base::StringToInt(app_id, &id));
246 return id;
249 virtual const std::string& GetAppIDForShelfID(ShelfID id) OVERRIDE {
250 // Use |app_id_| member variable because returning a reference to local
251 // variable is not allowed.
252 app_id_ = base::IntToString(id);
253 return app_id_;
256 virtual void PinAppWithID(const std::string& app_id) OVERRIDE {
259 virtual bool IsAppPinned(const std::string& app_id) OVERRIDE {
260 // Returns true for ShelfViewTest.OverflowBubbleSize. To test ripping off in
261 // that test, an item is already pinned state.
262 return true;
265 virtual bool CanPin() const OVERRIDE {
266 return true;
269 virtual void UnpinAppWithID(const std::string& app_id) OVERRIDE {
270 ShelfID id = 0;
271 EXPECT_TRUE(base::StringToInt(app_id, &id));
272 ASSERT_GT(id, 0);
273 int index = model_->ItemIndexByID(id);
274 ASSERT_GE(index, 0);
276 model_->RemoveItemAt(index);
279 private:
280 ShelfModel* model_;
282 // Temp member variable for returning a value. See the comment in the
283 // GetAppIDForShelfID().
284 std::string app_id_;
286 DISALLOW_COPY_AND_ASSIGN(TestShelfDelegateForShelfView);
289 class ShelfViewTest : public AshTestBase {
290 public:
291 ShelfViewTest()
292 : model_(NULL),
293 shelf_view_(NULL),
294 browser_index_(1),
295 item_manager_(NULL) {}
296 virtual ~ShelfViewTest() {}
298 virtual void SetUp() OVERRIDE {
299 AshTestBase::SetUp();
300 test::ShellTestApi test_api(Shell::GetInstance());
301 model_ = test_api.shelf_model();
302 Shelf* shelf = Shelf::ForPrimaryDisplay();
303 shelf_view_ = ShelfTestAPI(shelf).shelf_view();
305 // The bounds should be big enough for 4 buttons + overflow chevron.
306 shelf_view_->SetBounds(0, 0, 500, kShelfSize);
308 test_api_.reset(new ShelfViewTestAPI(shelf_view_));
309 test_api_->SetAnimationDuration(1); // Speeds up animation for test.
311 item_manager_ = Shell::GetInstance()->shelf_item_delegate_manager();
312 DCHECK(item_manager_);
314 // Add browser shortcut shelf item at index 0 for test.
315 AddBrowserShortcut();
318 virtual void TearDown() OVERRIDE {
319 test_api_.reset();
320 AshTestBase::TearDown();
323 protected:
324 void CreateAndSetShelfItemDelegateForID(ShelfID id) {
325 scoped_ptr<ShelfItemDelegate> delegate(new TestShelfItemDelegate(NULL));
326 item_manager_->SetShelfItemDelegate(id, delegate.Pass());
329 ShelfID AddBrowserShortcut() {
330 ShelfItem browser_shortcut;
331 browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
333 ShelfID id = model_->next_id();
334 model_->AddAt(browser_index_, browser_shortcut);
335 CreateAndSetShelfItemDelegateForID(id);
336 test_api_->RunMessageLoopUntilAnimationsDone();
337 return id;
340 ShelfID AddAppShortcut() {
341 ShelfItem item;
342 item.type = TYPE_APP_SHORTCUT;
343 item.status = STATUS_CLOSED;
345 ShelfID id = model_->next_id();
346 model_->Add(item);
347 CreateAndSetShelfItemDelegateForID(id);
348 test_api_->RunMessageLoopUntilAnimationsDone();
349 return id;
352 ShelfID AddPanel() {
353 ShelfID id = AddPanelNoWait();
354 test_api_->RunMessageLoopUntilAnimationsDone();
355 return id;
358 ShelfID AddPlatformAppNoWait() {
359 ShelfItem item;
360 item.type = TYPE_PLATFORM_APP;
361 item.status = STATUS_RUNNING;
363 ShelfID id = model_->next_id();
364 model_->Add(item);
365 CreateAndSetShelfItemDelegateForID(id);
366 return id;
369 ShelfID AddPanelNoWait() {
370 ShelfItem item;
371 item.type = TYPE_APP_PANEL;
372 item.status = STATUS_RUNNING;
374 ShelfID id = model_->next_id();
375 model_->Add(item);
376 CreateAndSetShelfItemDelegateForID(id);
377 return id;
380 ShelfID AddPlatformApp() {
381 ShelfID id = AddPlatformAppNoWait();
382 test_api_->RunMessageLoopUntilAnimationsDone();
383 return id;
386 void RemoveByID(ShelfID id) {
387 model_->RemoveItemAt(model_->ItemIndexByID(id));
388 test_api_->RunMessageLoopUntilAnimationsDone();
391 ShelfButton* GetButtonByID(ShelfID id) {
392 int index = model_->ItemIndexByID(id);
393 return test_api_->GetButton(index);
396 ShelfItem GetItemByID(ShelfID id) {
397 ShelfItems::const_iterator items = model_->ItemByID(id);
398 return *items;
401 void CheckModelIDs(
402 const std::vector<std::pair<ShelfID, views::View*> >& id_map) {
403 size_t map_index = 0;
404 for (size_t model_index = 0;
405 model_index < model_->items().size();
406 ++model_index) {
407 ShelfItem item = model_->items()[model_index];
408 ShelfID id = item.id;
409 EXPECT_EQ(id_map[map_index].first, id);
410 EXPECT_EQ(id_map[map_index].second, GetButtonByID(id));
411 ++map_index;
413 ASSERT_EQ(map_index, id_map.size());
416 void VerifyShelfItemBoundsAreValid() {
417 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
418 if (test_api_->GetButton(i)) {
419 gfx::Rect shelf_view_bounds = shelf_view_->GetLocalBounds();
420 gfx::Rect item_bounds = test_api_->GetBoundsByIndex(i);
421 EXPECT_GE(item_bounds.x(), 0);
422 EXPECT_GE(item_bounds.y(), 0);
423 EXPECT_LE(item_bounds.right(), shelf_view_bounds.width());
424 EXPECT_LE(item_bounds.bottom(), shelf_view_bounds.height());
429 ShelfButton* SimulateButtonPressed(ShelfButtonHost::Pointer pointer,
430 int button_index) {
431 ShelfButtonHost* button_host = shelf_view_;
432 ShelfButton* button = test_api_->GetButton(button_index);
433 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED,
434 gfx::Point(),
435 button->GetBoundsInScreen().origin(), 0, 0);
436 button_host->PointerPressedOnButton(button, pointer, click_event);
437 return button;
440 // Simulates a single mouse click.
441 void SimulateClick(int button_index) {
442 ShelfButtonHost* button_host = shelf_view_;
443 ShelfButton* button =
444 SimulateButtonPressed(ShelfButtonHost::MOUSE, button_index);
445 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
446 gfx::Point(),
447 button->GetBoundsInScreen().origin(),
450 test_api_->ButtonPressed(button, release_event);
451 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false);
454 // Simulates the second click of a double click.
455 void SimulateDoubleClick(int button_index) {
456 ShelfButtonHost* button_host = shelf_view_;
457 ShelfButton* button =
458 SimulateButtonPressed(ShelfButtonHost::MOUSE, button_index);
459 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
460 gfx::Point(),
461 button->GetBoundsInScreen().origin(),
462 ui::EF_IS_DOUBLE_CLICK,
464 test_api_->ButtonPressed(button, release_event);
465 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false);
468 views::View* SimulateDrag(ShelfButtonHost::Pointer pointer,
469 int button_index,
470 int destination_index) {
471 ShelfButtonHost* button_host = shelf_view_;
472 views::View* button = SimulateButtonPressed(pointer, button_index);
474 // Drag.
475 views::View* destination = test_api_->GetButton(destination_index);
476 ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED,
477 gfx::Point(destination->x() - button->x(),
478 destination->y() - button->y()),
479 destination->GetBoundsInScreen().origin(), 0, 0);
480 button_host->PointerDraggedOnButton(button, pointer, drag_event);
481 return button;
484 void SetupForDragTest(
485 std::vector<std::pair<ShelfID, views::View*> >* id_map) {
486 // Initialize |id_map| with the automatically-created shelf buttons.
487 for (size_t i = 0; i < model_->items().size(); ++i) {
488 ShelfButton* button = test_api_->GetButton(i);
489 id_map->push_back(std::make_pair(model_->items()[i].id, button));
491 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
493 // Add 5 app shelf buttons for testing.
494 for (int i = 0; i < 5; ++i) {
495 ShelfID id = AddAppShortcut();
496 // App Icon is located at index 0, and browser shortcut is located at
497 // index 1. So we should start to add app shortcut at index 2.
498 id_map->insert(id_map->begin() + (i + browser_index_ + 1),
499 std::make_pair(id, GetButtonByID(id)));
501 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
504 views::View* GetTooltipAnchorView() {
505 return shelf_view_->tooltip_manager()->anchor_;
508 void AddButtonsUntilOverflow() {
509 int items_added = 0;
510 while (!test_api_->IsOverflowButtonVisible()) {
511 AddAppShortcut();
512 ++items_added;
513 ASSERT_LT(items_added, 10000);
517 void ShowTooltip() {
518 shelf_view_->tooltip_manager()->ShowInternal();
521 void TestDraggingAnItemFromOverflowToShelf(bool cancel) {
522 test_api_->ShowOverflowBubble();
523 ASSERT_TRUE(test_api_->overflow_bubble() &&
524 test_api_->overflow_bubble()->IsShowing());
526 ash::test::ShelfViewTestAPI test_api_for_overflow(
527 test_api_->overflow_bubble()->shelf_view());
529 int total_item_count = model_->item_count();
531 int last_visible_item_id_in_shelf =
532 GetItemId(test_api_->GetLastVisibleIndex());
533 int second_last_visible_item_id_in_shelf =
534 GetItemId(test_api_->GetLastVisibleIndex() - 1);
535 int first_visible_item_id_in_overflow =
536 GetItemId(test_api_for_overflow.GetFirstVisibleIndex());
537 int second_last_visible_item_id_in_overflow =
538 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1);
540 int drag_item_index =
541 test_api_for_overflow.GetLastVisibleIndex();
542 ShelfID drag_item_id = GetItemId(drag_item_index);
543 ShelfButton* drag_button = test_api_for_overflow.GetButton(drag_item_index);
544 gfx::Point center_point_of_drag_item =
545 drag_button->GetBoundsInScreen().CenterPoint();
547 ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow(),
548 center_point_of_drag_item);
549 // Rip an item off to OverflowBubble.
550 generator.PressLeftButton();
551 gfx::Point rip_off_point(center_point_of_drag_item.x(), 0);
552 generator.MoveMouseTo(rip_off_point);
553 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
554 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
555 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
557 // Move a dragged item into Shelf at |drop_index|.
558 int drop_index = 1;
559 gfx::Point drop_point =
560 test_api_->GetButton(drop_index)->GetBoundsInScreen().CenterPoint();
561 int item_width = test_api_for_overflow.GetButtonSize();
562 // To insert at |drop_index|, more smaller x-axis value of |drop_point|
563 // should be used.
564 gfx::Point modified_drop_point(drop_point.x() - item_width / 4,
565 drop_point.y());
566 generator.MoveMouseTo(modified_drop_point);
567 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
568 test_api_->RunMessageLoopUntilAnimationsDone();
569 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
570 ASSERT_TRUE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
572 if (cancel)
573 drag_button->OnMouseCaptureLost();
574 else
575 generator.ReleaseLeftButton();
577 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
578 test_api_->RunMessageLoopUntilAnimationsDone();
579 ASSERT_FALSE(test_api_for_overflow.IsRippedOffFromShelf());
580 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
582 // Compare pre-stored items' id with newly positioned items' after dragging
583 // is canceled or finished.
584 if (cancel) {
585 EXPECT_EQ(last_visible_item_id_in_shelf,
586 GetItemId(test_api_->GetLastVisibleIndex()));
587 EXPECT_EQ(second_last_visible_item_id_in_shelf,
588 GetItemId(test_api_->GetLastVisibleIndex() - 1));
589 EXPECT_EQ(first_visible_item_id_in_overflow,
590 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
591 EXPECT_EQ(second_last_visible_item_id_in_overflow,
592 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1));
593 } else {
594 EXPECT_EQ(drag_item_id, GetItemId(drop_index));
595 EXPECT_EQ(total_item_count, model_->item_count());
596 EXPECT_EQ(last_visible_item_id_in_shelf,
597 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
598 EXPECT_EQ(second_last_visible_item_id_in_shelf,
599 GetItemId(test_api_->GetLastVisibleIndex()));
600 EXPECT_EQ(first_visible_item_id_in_overflow,
601 GetItemId(test_api_for_overflow.GetFirstVisibleIndex() + 1));
602 EXPECT_EQ(second_last_visible_item_id_in_overflow,
603 GetItemId(test_api_for_overflow.GetLastVisibleIndex()));
607 // Returns the item's ShelfID at |index|.
608 ShelfID GetItemId(int index) {
609 DCHECK_GE(index, 0);
610 return model_->items()[index].id;
613 void ReplaceShelfDelegateForRipOffTest() {
614 // Replace ShelfDelegate.
615 test::ShellTestApi test_api(Shell::GetInstance());
616 test_api.SetShelfDelegate(NULL);
617 ShelfDelegate* delegate = new TestShelfDelegateForShelfView(model_);
618 test_api.SetShelfDelegate(delegate);
619 test::ShelfTestAPI(Shelf::ForPrimaryDisplay()).SetShelfDelegate(delegate);
620 test_api_->SetShelfDelegate(delegate);
623 ShelfModel* model_;
624 ShelfView* shelf_view_;
625 int browser_index_;
626 ShelfItemDelegateManager* item_manager_;
628 scoped_ptr<ShelfViewTestAPI> test_api_;
630 private:
631 DISALLOW_COPY_AND_ASSIGN(ShelfViewTest);
634 class ScopedTextDirectionChange {
635 public:
636 explicit ScopedTextDirectionChange(bool is_rtl) : is_rtl_(is_rtl) {
637 original_locale_ = l10n_util::GetApplicationLocale(std::string());
638 if (is_rtl_)
639 base::i18n::SetICUDefaultLocale("he");
640 CheckTextDirectionIsCorrect();
643 ~ScopedTextDirectionChange() {
644 if (is_rtl_)
645 base::i18n::SetICUDefaultLocale(original_locale_);
648 private:
649 void CheckTextDirectionIsCorrect() {
650 ASSERT_EQ(is_rtl_, base::i18n::IsRTL());
653 bool is_rtl_;
654 std::string original_locale_;
657 class ShelfViewTextDirectionTest
658 : public ShelfViewTest,
659 public testing::WithParamInterface<bool> {
660 public:
661 ShelfViewTextDirectionTest() : text_direction_change_(GetParam()) {}
662 virtual ~ShelfViewTextDirectionTest() {}
664 virtual void SetUp() OVERRIDE {
665 ShelfViewTest::SetUp();
668 virtual void TearDown() OVERRIDE {
669 ShelfViewTest::TearDown();
672 private:
673 ScopedTextDirectionChange text_direction_change_;
675 DISALLOW_COPY_AND_ASSIGN(ShelfViewTextDirectionTest);
678 // Checks that the ideal item icon bounds match the view's bounds in the screen
679 // in both LTR and RTL.
680 TEST_P(ShelfViewTextDirectionTest, IdealBoundsOfItemIcon) {
681 ShelfID id = AddPlatformApp();
682 ShelfButton* button = GetButtonByID(id);
683 gfx::Rect item_bounds = button->GetBoundsInScreen();
684 gfx::Point icon_offset = button->GetIconBounds().origin();
685 item_bounds.Offset(icon_offset.OffsetFromOrigin());
686 gfx::Rect ideal_bounds = shelf_view_->GetIdealBoundsOfItemIcon(id);
687 gfx::Point screen_origin;
688 views::View::ConvertPointToScreen(shelf_view_, &screen_origin);
689 ideal_bounds.Offset(screen_origin.x(), screen_origin.y());
690 EXPECT_EQ(item_bounds.x(), ideal_bounds.x());
691 EXPECT_EQ(item_bounds.y(), ideal_bounds.y());
694 // Check that items in the overflow area are returning the overflow button as
695 // ideal bounds.
696 TEST_F(ShelfViewTest, OverflowButtonBounds) {
697 ShelfID first_id = AddPlatformApp();
698 ShelfID overflow_id = AddPlatformApp();
699 int items_added = 0;
700 while (!test_api_->IsOverflowButtonVisible()) {
701 // Added button is visible after animation while in this loop.
702 EXPECT_TRUE(GetButtonByID(overflow_id)->visible());
703 overflow_id = AddPlatformApp();
704 ++items_added;
705 ASSERT_LT(items_added, 10000);
707 ShelfID last_id = AddPlatformApp();
709 gfx::Rect first_bounds = shelf_view_->GetIdealBoundsOfItemIcon(first_id);
710 gfx::Rect overflow_bounds =
711 shelf_view_->GetIdealBoundsOfItemIcon(overflow_id);
712 gfx::Rect last_bounds = shelf_view_->GetIdealBoundsOfItemIcon(last_id);
714 // Check that all items have the same size and that the overflow items are
715 // identical whereas the first one does not match either of them.
716 EXPECT_EQ(first_bounds.size().ToString(), last_bounds.size().ToString());
717 EXPECT_NE(first_bounds.ToString(), last_bounds.ToString());
718 EXPECT_EQ(overflow_bounds.ToString(), last_bounds.ToString());
721 // Checks that shelf view contents are considered in the correct drag group.
722 TEST_F(ShelfViewTest, EnforceDragType) {
723 EXPECT_TRUE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_PLATFORM_APP));
724 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_SHORTCUT));
725 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP,
726 TYPE_BROWSER_SHORTCUT));
727 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_WINDOWED_APP));
728 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_LIST));
729 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_PANEL));
731 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_SHORTCUT));
732 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
733 TYPE_BROWSER_SHORTCUT));
734 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
735 TYPE_WINDOWED_APP));
736 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_LIST));
737 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_PANEL));
739 EXPECT_TRUE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
740 TYPE_BROWSER_SHORTCUT));
741 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
742 TYPE_WINDOWED_APP));
743 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_LIST));
744 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_PANEL));
746 EXPECT_TRUE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_WINDOWED_APP));
747 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_LIST));
748 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_PANEL));
750 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_LIST));
751 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_PANEL));
753 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_PANEL, TYPE_APP_PANEL));
756 // Adds platform app button until overflow and verifies that the last added
757 // platform app button is hidden.
758 TEST_F(ShelfViewTest, AddBrowserUntilOverflow) {
759 // All buttons should be visible.
760 ASSERT_EQ(test_api_->GetButtonCount(),
761 test_api_->GetLastVisibleIndex() + 1);
763 // Add platform app button until overflow.
764 int items_added = 0;
765 ShelfID last_added = AddPlatformApp();
766 while (!test_api_->IsOverflowButtonVisible()) {
767 // Added button is visible after animation while in this loop.
768 EXPECT_TRUE(GetButtonByID(last_added)->visible());
770 last_added = AddPlatformApp();
771 ++items_added;
772 ASSERT_LT(items_added, 10000);
775 // The last added button should be invisible.
776 EXPECT_FALSE(GetButtonByID(last_added)->visible());
779 // Adds one platform app button then adds app shortcut until overflow. Verifies
780 // that the browser button gets hidden on overflow and last added app shortcut
781 // is still visible.
782 TEST_F(ShelfViewTest, AddAppShortcutWithBrowserButtonUntilOverflow) {
783 // All buttons should be visible.
784 ASSERT_EQ(test_api_->GetButtonCount(),
785 test_api_->GetLastVisibleIndex() + 1);
787 ShelfID browser_button_id = AddPlatformApp();
789 // Add app shortcut until overflow.
790 int items_added = 0;
791 ShelfID last_added = AddAppShortcut();
792 while (!test_api_->IsOverflowButtonVisible()) {
793 // Added button is visible after animation while in this loop.
794 EXPECT_TRUE(GetButtonByID(last_added)->visible());
796 last_added = AddAppShortcut();
797 ++items_added;
798 ASSERT_LT(items_added, 10000);
801 // And the platform app button is invisible.
802 EXPECT_FALSE(GetButtonByID(browser_button_id)->visible());
805 TEST_F(ShelfViewTest, AddPanelHidesPlatformAppButton) {
806 // All buttons should be visible.
807 ASSERT_EQ(test_api_->GetButtonCount(),
808 test_api_->GetLastVisibleIndex() + 1);
810 // Add platform app button until overflow, remember last visible platform app
811 // button.
812 int items_added = 0;
813 ShelfID first_added = AddPlatformApp();
814 EXPECT_TRUE(GetButtonByID(first_added)->visible());
815 while (true) {
816 ShelfID added = AddPlatformApp();
817 if (test_api_->IsOverflowButtonVisible()) {
818 EXPECT_FALSE(GetButtonByID(added)->visible());
819 RemoveByID(added);
820 break;
822 ++items_added;
823 ASSERT_LT(items_added, 10000);
826 ShelfID panel = AddPanel();
827 EXPECT_TRUE(test_api_->IsOverflowButtonVisible());
829 RemoveByID(panel);
830 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
833 // When there are more panels then platform app buttons we should hide panels
834 // rather than platform apps.
835 TEST_F(ShelfViewTest, PlatformAppHidesExcessPanels) {
836 // All buttons should be visible.
837 ASSERT_EQ(test_api_->GetButtonCount(),
838 test_api_->GetLastVisibleIndex() + 1);
840 // Add platform app button.
841 ShelfID platform_app = AddPlatformApp();
842 ShelfID first_panel = AddPanel();
844 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
845 EXPECT_TRUE(GetButtonByID(first_panel)->visible());
847 // Add panels until there is an overflow.
848 ShelfID last_panel = first_panel;
849 int items_added = 0;
850 while (!test_api_->IsOverflowButtonVisible()) {
851 last_panel = AddPanel();
852 ++items_added;
853 ASSERT_LT(items_added, 10000);
856 // The first panel should now be hidden by the new platform apps needing
857 // space.
858 EXPECT_FALSE(GetButtonByID(first_panel)->visible());
859 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
860 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
862 // Adding platform apps should eventually begin to hide platform apps. We will
863 // add platform apps until either the last panel or platform app is hidden.
864 items_added = 0;
865 while (GetButtonByID(platform_app)->visible() &&
866 GetButtonByID(last_panel)->visible()) {
867 platform_app = AddPlatformApp();
868 ++items_added;
869 ASSERT_LT(items_added, 10000);
871 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
872 EXPECT_FALSE(GetButtonByID(platform_app)->visible());
875 // Adds button until overflow then removes first added one. Verifies that
876 // the last added one changes from invisible to visible and overflow
877 // chevron is gone.
878 TEST_F(ShelfViewTest, RemoveButtonRevealsOverflowed) {
879 // All buttons should be visible.
880 ASSERT_EQ(test_api_->GetButtonCount(),
881 test_api_->GetLastVisibleIndex() + 1);
883 // Add platform app buttons until overflow.
884 int items_added = 0;
885 ShelfID first_added = AddPlatformApp();
886 ShelfID last_added = first_added;
887 while (!test_api_->IsOverflowButtonVisible()) {
888 last_added = AddPlatformApp();
889 ++items_added;
890 ASSERT_LT(items_added, 10000);
893 // Expect add more than 1 button. First added is visible and last is not.
894 EXPECT_NE(first_added, last_added);
895 EXPECT_TRUE(GetButtonByID(first_added)->visible());
896 EXPECT_FALSE(GetButtonByID(last_added)->visible());
898 // Remove first added.
899 RemoveByID(first_added);
901 // Last added button becomes visible and overflow chevron is gone.
902 EXPECT_TRUE(GetButtonByID(last_added)->visible());
903 EXPECT_EQ(1.0f, GetButtonByID(last_added)->layer()->opacity());
904 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
907 // Verifies that remove last overflowed button should hide overflow chevron.
908 TEST_F(ShelfViewTest, RemoveLastOverflowed) {
909 // All buttons should be visible.
910 ASSERT_EQ(test_api_->GetButtonCount(),
911 test_api_->GetLastVisibleIndex() + 1);
913 // Add platform app button until overflow.
914 int items_added = 0;
915 ShelfID last_added = AddPlatformApp();
916 while (!test_api_->IsOverflowButtonVisible()) {
917 last_added = AddPlatformApp();
918 ++items_added;
919 ASSERT_LT(items_added, 10000);
922 RemoveByID(last_added);
923 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
926 // Adds platform app button without waiting for animation to finish and verifies
927 // that all added buttons are visible.
928 TEST_F(ShelfViewTest, AddButtonQuickly) {
929 // All buttons should be visible.
930 ASSERT_EQ(test_api_->GetButtonCount(),
931 test_api_->GetLastVisibleIndex() + 1);
933 // Add a few platform buttons quickly without wait for animation.
934 int added_count = 0;
935 while (!test_api_->IsOverflowButtonVisible()) {
936 AddPlatformAppNoWait();
937 ++added_count;
938 ASSERT_LT(added_count, 10000);
941 // ShelfView should be big enough to hold at least 3 new buttons.
942 ASSERT_GE(added_count, 3);
944 // Wait for the last animation to finish.
945 test_api_->RunMessageLoopUntilAnimationsDone();
947 // Verifies non-overflow buttons are visible.
948 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
949 ShelfButton* button = test_api_->GetButton(i);
950 if (button) {
951 EXPECT_TRUE(button->visible()) << "button index=" << i;
952 EXPECT_EQ(1.0f, button->layer()->opacity()) << "button index=" << i;
957 // Check that model changes are handled correctly while a shelf icon is being
958 // dragged.
959 TEST_F(ShelfViewTest, ModelChangesWhileDragging) {
960 ShelfButtonHost* button_host = shelf_view_;
962 std::vector<std::pair<ShelfID, views::View*> > id_map;
963 SetupForDragTest(&id_map);
965 // Dragging browser shortcut at index 1.
966 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
967 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
968 std::rotate(id_map.begin() + 1,
969 id_map.begin() + 2,
970 id_map.begin() + 4);
971 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
972 button_host->PointerReleasedOnButton(
973 dragged_button, ShelfButtonHost::MOUSE, false);
974 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
976 // Dragging changes model order.
977 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
978 std::rotate(id_map.begin() + 1,
979 id_map.begin() + 2,
980 id_map.begin() + 4);
981 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
983 // Cancelling the drag operation restores previous order.
984 button_host->PointerReleasedOnButton(
985 dragged_button, ShelfButtonHost::MOUSE, true);
986 std::rotate(id_map.begin() + 1,
987 id_map.begin() + 3,
988 id_map.begin() + 4);
989 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
991 // Deleting an item keeps the remaining intact.
992 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
993 model_->RemoveItemAt(1);
994 id_map.erase(id_map.begin() + 1);
995 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
996 button_host->PointerReleasedOnButton(
997 dragged_button, ShelfButtonHost::MOUSE, false);
999 // Adding a shelf item cancels the drag and respects the order.
1000 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1001 ShelfID new_id = AddAppShortcut();
1002 id_map.insert(id_map.begin() + 6,
1003 std::make_pair(new_id, GetButtonByID(new_id)));
1004 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1005 button_host->PointerReleasedOnButton(
1006 dragged_button, ShelfButtonHost::MOUSE, false);
1008 // Adding a shelf item at the end (i.e. a panel) canels drag and respects
1009 // the order.
1010 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1011 new_id = AddPanel();
1012 id_map.insert(id_map.begin() + 7,
1013 std::make_pair(new_id, GetButtonByID(new_id)));
1014 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1015 button_host->PointerReleasedOnButton(
1016 dragged_button, ShelfButtonHost::MOUSE, false);
1019 // Check that 2nd drag from the other pointer would be ignored.
1020 TEST_F(ShelfViewTest, SimultaneousDrag) {
1021 ShelfButtonHost* button_host = shelf_view_;
1023 std::vector<std::pair<ShelfID, views::View*> > id_map;
1024 SetupForDragTest(&id_map);
1026 // Start a mouse drag.
1027 views::View* dragged_button_mouse =
1028 SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1029 std::rotate(id_map.begin() + 1,
1030 id_map.begin() + 2,
1031 id_map.begin() + 4);
1032 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1033 // Attempt a touch drag before the mouse drag finishes.
1034 views::View* dragged_button_touch =
1035 SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
1037 // Nothing changes since 2nd drag is ignored.
1038 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1040 // Finish the mouse drag.
1041 button_host->PointerReleasedOnButton(
1042 dragged_button_mouse, ShelfButtonHost::MOUSE, false);
1043 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1045 // Now start a touch drag.
1046 dragged_button_touch = SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
1047 std::rotate(id_map.begin() + 3,
1048 id_map.begin() + 4,
1049 id_map.begin() + 5);
1050 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1052 // And attempt a mouse drag before the touch drag finishes.
1053 dragged_button_mouse = SimulateDrag(ShelfButtonHost::MOUSE, 1, 2);
1055 // Nothing changes since 2nd drag is ignored.
1056 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1058 button_host->PointerReleasedOnButton(
1059 dragged_button_touch, ShelfButtonHost::TOUCH, false);
1060 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1063 // Check that clicking first on one item and then dragging another works as
1064 // expected.
1065 TEST_F(ShelfViewTest, ClickOneDragAnother) {
1066 ShelfButtonHost* button_host = shelf_view_;
1068 std::vector<std::pair<ShelfID, views::View*> > id_map;
1069 SetupForDragTest(&id_map);
1071 // A click on item 1 is simulated.
1072 SimulateClick(1);
1074 // Dragging browser index at 0 should change the model order correctly.
1075 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
1076 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1077 std::rotate(id_map.begin() + 1,
1078 id_map.begin() + 2,
1079 id_map.begin() + 4);
1080 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1081 button_host->PointerReleasedOnButton(
1082 dragged_button, ShelfButtonHost::MOUSE, false);
1083 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
1086 // Tests that double-clicking an item does not activate it twice.
1087 TEST_F(ShelfViewTest, ClickingTwiceActivatesOnce) {
1088 // Watch for selection of the browser shortcut.
1089 ShelfID browser_shelf_id = model_->items()[browser_index_].id;
1090 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1091 item_manager_->SetShelfItemDelegate(
1092 browser_shelf_id,
1093 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1095 // A single click selects the item.
1096 SimulateClick(browser_index_);
1097 EXPECT_TRUE(selection_tracker->WasSelected());
1099 // A double-click does not select the item.
1100 selection_tracker->Reset();
1101 SimulateDoubleClick(browser_index_);
1102 EXPECT_FALSE(selection_tracker->WasSelected());
1105 // Check that clicking an item and jittering the mouse a bit still selects the
1106 // item.
1107 TEST_F(ShelfViewTest, ClickAndMoveSlightly) {
1108 std::vector<std::pair<ShelfID, views::View*> > id_map;
1109 SetupForDragTest(&id_map);
1111 ShelfID shelf_id = (id_map.begin() + 1)->first;
1112 views::View* button = (id_map.begin() + 1)->second;
1114 // Replace the ShelfItemDelegate for |shelf_id| with one which tracks whether
1115 // the shelf item gets selected.
1116 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1117 item_manager_->SetShelfItemDelegate(
1118 shelf_id,
1119 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1121 gfx::Vector2d press_offset(5, 30);
1122 gfx::Point press_location = gfx::Point() + press_offset;
1123 gfx::Point press_location_in_screen =
1124 button->GetBoundsInScreen().origin() + press_offset;
1126 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED,
1127 press_location,
1128 press_location_in_screen,
1129 ui::EF_LEFT_MOUSE_BUTTON, 0);
1130 button->OnMousePressed(click_event);
1132 ui::MouseEvent drag_event1(ui::ET_MOUSE_DRAGGED,
1133 press_location + gfx::Vector2d(0, 1),
1134 press_location_in_screen + gfx::Vector2d(0, 1),
1135 ui::EF_LEFT_MOUSE_BUTTON, 0);
1136 button->OnMouseDragged(drag_event1);
1138 ui::MouseEvent drag_event2(ui::ET_MOUSE_DRAGGED,
1139 press_location + gfx::Vector2d(-1, 0),
1140 press_location_in_screen + gfx::Vector2d(-1, 0),
1141 ui::EF_LEFT_MOUSE_BUTTON, 0);
1142 button->OnMouseDragged(drag_event2);
1144 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
1145 press_location + gfx::Vector2d(-1, 0),
1146 press_location_in_screen + gfx::Vector2d(-1, 0),
1147 ui::EF_LEFT_MOUSE_BUTTON, 0);
1148 button->OnMouseReleased(release_event);
1150 EXPECT_TRUE(selection_tracker->WasSelected());
1153 // Confirm that item status changes are reflected in the buttons.
1154 TEST_F(ShelfViewTest, ShelfItemStatus) {
1155 // All buttons should be visible.
1156 ASSERT_EQ(test_api_->GetButtonCount(),
1157 test_api_->GetLastVisibleIndex() + 1);
1159 // Add platform app button.
1160 ShelfID last_added = AddPlatformApp();
1161 ShelfItem item = GetItemByID(last_added);
1162 int index = model_->ItemIndexByID(last_added);
1163 ShelfButton* button = GetButtonByID(last_added);
1164 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1165 item.status = STATUS_ACTIVE;
1166 model_->Set(index, item);
1167 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1168 item.status = STATUS_ATTENTION;
1169 model_->Set(index, item);
1170 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1173 // Confirm that item status changes are reflected in the buttons
1174 // for platform apps.
1175 TEST_F(ShelfViewTest, ShelfItemStatusPlatformApp) {
1176 // All buttons should be visible.
1177 ASSERT_EQ(test_api_->GetButtonCount(),
1178 test_api_->GetLastVisibleIndex() + 1);
1180 // Add platform app button.
1181 ShelfID last_added = AddPlatformApp();
1182 ShelfItem item = GetItemByID(last_added);
1183 int index = model_->ItemIndexByID(last_added);
1184 ShelfButton* button = GetButtonByID(last_added);
1185 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1186 item.status = STATUS_ACTIVE;
1187 model_->Set(index, item);
1188 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1189 item.status = STATUS_ATTENTION;
1190 model_->Set(index, item);
1191 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1194 // Confirm that shelf item bounds are correctly updated on shelf changes.
1195 TEST_F(ShelfViewTest, ShelfItemBoundsCheck) {
1196 VerifyShelfItemBoundsAreValid();
1197 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1198 SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
1199 test_api_->RunMessageLoopUntilAnimationsDone();
1200 VerifyShelfItemBoundsAreValid();
1201 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1202 SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
1203 test_api_->RunMessageLoopUntilAnimationsDone();
1204 VerifyShelfItemBoundsAreValid();
1207 TEST_F(ShelfViewTest, ShelfTooltipTest) {
1208 ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
1209 test_api_->GetButtonCount());
1211 // Prepare some items to the shelf.
1212 ShelfID app_button_id = AddAppShortcut();
1213 ShelfID platform_button_id = AddPlatformApp();
1215 ShelfButton* app_button = GetButtonByID(app_button_id);
1216 ShelfButton* platform_button = GetButtonByID(platform_button_id);
1218 ShelfButtonHost* button_host = shelf_view_;
1219 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1221 button_host->MouseEnteredButton(app_button);
1222 // There's a delay to show the tooltip, so it's not visible yet.
1223 EXPECT_FALSE(tooltip_manager->IsVisible());
1224 EXPECT_EQ(app_button, GetTooltipAnchorView());
1226 ShowTooltip();
1227 EXPECT_TRUE(tooltip_manager->IsVisible());
1229 // Once it's visible, it keeps visibility and is pointing to the same
1230 // item.
1231 button_host->MouseExitedButton(app_button);
1232 EXPECT_TRUE(tooltip_manager->IsVisible());
1233 EXPECT_EQ(app_button, GetTooltipAnchorView());
1235 // When entered to another item, it switches to the new item. There is no
1236 // delay for the visibility.
1237 button_host->MouseEnteredButton(platform_button);
1238 EXPECT_TRUE(tooltip_manager->IsVisible());
1239 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1241 button_host->MouseExitedButton(platform_button);
1242 tooltip_manager->Close();
1244 // Next time: enter app_button -> move immediately to tab_button.
1245 button_host->MouseEnteredButton(app_button);
1246 button_host->MouseExitedButton(app_button);
1247 button_host->MouseEnteredButton(platform_button);
1248 EXPECT_FALSE(tooltip_manager->IsVisible());
1249 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1252 // Verify a fix for crash caused by a tooltip update for a deletedshelf
1253 // button, see crbug.com/288838.
1254 TEST_F(ShelfViewTest, RemovingItemClosesTooltip) {
1255 ShelfButtonHost* button_host = shelf_view_;
1256 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1258 // Add an item to the shelf.
1259 ShelfID app_button_id = AddAppShortcut();
1260 ShelfButton* app_button = GetButtonByID(app_button_id);
1262 // Spawn a tooltip on that item.
1263 button_host->MouseEnteredButton(app_button);
1264 ShowTooltip();
1265 EXPECT_TRUE(tooltip_manager->IsVisible());
1267 // Remove the app shortcut while the tooltip is open. The tooltip should be
1268 // closed.
1269 RemoveByID(app_button_id);
1270 EXPECT_FALSE(tooltip_manager->IsVisible());
1272 // Change the shelf layout. This should not crash.
1273 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1274 Shell::GetPrimaryRootWindow());
1277 // Changing the shelf alignment closes any open tooltip.
1278 TEST_F(ShelfViewTest, ShelfAlignmentClosesTooltip) {
1279 ShelfButtonHost* button_host = shelf_view_;
1280 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1282 // Add an item to the shelf.
1283 ShelfID app_button_id = AddAppShortcut();
1284 ShelfButton* app_button = GetButtonByID(app_button_id);
1286 // Spawn a tooltip on the item.
1287 button_host->MouseEnteredButton(app_button);
1288 ShowTooltip();
1289 EXPECT_TRUE(tooltip_manager->IsVisible());
1291 // Changing shelf alignment hides the tooltip.
1292 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1293 Shell::GetPrimaryRootWindow());
1294 EXPECT_FALSE(tooltip_manager->IsVisible());
1297 TEST_F(ShelfViewTest, ShouldHideTooltipTest) {
1298 ShelfID app_button_id = AddAppShortcut();
1299 ShelfID platform_button_id = AddPlatformApp();
1301 // The tooltip shouldn't hide if the mouse is on normal buttons.
1302 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1303 ShelfButton* button = test_api_->GetButton(i);
1304 if (!button)
1305 continue;
1307 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1308 button->GetMirroredBounds().CenterPoint()))
1309 << "ShelfView tries to hide on button " << i;
1312 // The tooltip should not hide on the app-list button.
1313 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1314 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1315 app_list_button->GetMirroredBounds().CenterPoint()));
1317 // The tooltip shouldn't hide if the mouse is in the gap between two buttons.
1318 gfx::Rect app_button_rect = GetButtonByID(app_button_id)->GetMirroredBounds();
1319 gfx::Rect platform_button_rect =
1320 GetButtonByID(platform_button_id)->GetMirroredBounds();
1321 ASSERT_FALSE(app_button_rect.Intersects(platform_button_rect));
1322 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1323 gfx::UnionRects(app_button_rect, platform_button_rect).CenterPoint()));
1325 // The tooltip should hide if it's outside of all buttons.
1326 gfx::Rect all_area;
1327 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1328 ShelfButton* button = test_api_->GetButton(i);
1329 if (!button)
1330 continue;
1332 all_area.Union(button->GetMirroredBounds());
1334 all_area.Union(shelf_view_->GetAppListButtonView()->GetMirroredBounds());
1335 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(all_area.origin()));
1336 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1337 gfx::Point(all_area.right() - 1, all_area.bottom() - 1)));
1338 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1339 gfx::Point(all_area.right(), all_area.y())));
1340 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1341 gfx::Point(all_area.x() - 1, all_area.y())));
1342 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1343 gfx::Point(all_area.x(), all_area.y() - 1)));
1344 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1345 gfx::Point(all_area.x(), all_area.bottom())));
1348 TEST_F(ShelfViewTest, ShouldHideTooltipWithAppListWindowTest) {
1349 Shell::GetInstance()->ShowAppList(NULL);
1350 ASSERT_TRUE(Shell::GetInstance()->GetAppListWindow());
1352 // The tooltip shouldn't hide if the mouse is on normal buttons.
1353 for (int i = 1; i < test_api_->GetButtonCount(); i++) {
1354 ShelfButton* button = test_api_->GetButton(i);
1355 if (!button)
1356 continue;
1358 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1359 button->GetMirroredBounds().CenterPoint()))
1360 << "ShelfView tries to hide on button " << i;
1363 // The tooltip should hide on the app-list button.
1364 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1365 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1366 app_list_button->GetMirroredBounds().CenterPoint()));
1369 // Test that by moving the mouse cursor off the button onto the bubble it closes
1370 // the bubble.
1371 TEST_F(ShelfViewTest, ShouldHideTooltipWhenHoveringOnTooltip) {
1372 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1373 tooltip_manager->CreateZeroDelayTimerForTest();
1374 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1376 // Move the mouse off any item and check that no tooltip is shown.
1377 generator.MoveMouseTo(gfx::Point(0, 0));
1378 EXPECT_FALSE(tooltip_manager->IsVisible());
1380 // Move the mouse over the button and check that it is visible.
1381 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1382 gfx::Rect bounds = app_list_button->GetBoundsInScreen();
1383 generator.MoveMouseTo(bounds.CenterPoint());
1384 // Wait for the timer to go off.
1385 RunAllPendingInMessageLoop();
1386 EXPECT_TRUE(tooltip_manager->IsVisible());
1388 // Move the mouse cursor slightly to the right of the item. The tooltip should
1389 // stay open.
1390 generator.MoveMouseBy(bounds.width() / 2 + 5, 0);
1391 // Make sure there is no delayed close.
1392 RunAllPendingInMessageLoop();
1393 EXPECT_TRUE(tooltip_manager->IsVisible());
1395 // Move back - it should still stay open.
1396 generator.MoveMouseBy(-(bounds.width() / 2 + 5), 0);
1397 // Make sure there is no delayed close.
1398 RunAllPendingInMessageLoop();
1399 EXPECT_TRUE(tooltip_manager->IsVisible());
1401 // Now move the mouse cursor slightly above the item - so that it is over the
1402 // tooltip bubble. Now it should disappear.
1403 generator.MoveMouseBy(0, -(bounds.height() / 2 + 5));
1404 // Wait until the delayed close kicked in.
1405 RunAllPendingInMessageLoop();
1406 EXPECT_FALSE(tooltip_manager->IsVisible());
1409 // Resizing shelf view while an add animation without fade-in is running,
1410 // which happens when overflow happens. App list button should end up in its
1411 // new ideal bounds.
1412 TEST_F(ShelfViewTest, ResizeDuringOverflowAddAnimation) {
1413 // All buttons should be visible.
1414 ASSERT_EQ(test_api_->GetButtonCount(),
1415 test_api_->GetLastVisibleIndex() + 1);
1417 // Add buttons until overflow. Let the non-overflow add animations finish but
1418 // leave the last running.
1419 int items_added = 0;
1420 AddPlatformAppNoWait();
1421 while (!test_api_->IsOverflowButtonVisible()) {
1422 test_api_->RunMessageLoopUntilAnimationsDone();
1423 AddPlatformAppNoWait();
1424 ++items_added;
1425 ASSERT_LT(items_added, 10000);
1428 // Resize shelf view with that animation running and stay overflown.
1429 gfx::Rect bounds = shelf_view_->bounds();
1430 bounds.set_width(bounds.width() - kShelfSize);
1431 shelf_view_->SetBoundsRect(bounds);
1432 ASSERT_TRUE(test_api_->IsOverflowButtonVisible());
1434 // Finish the animation.
1435 test_api_->RunMessageLoopUntilAnimationsDone();
1437 // App list button should ends up in its new ideal bounds.
1438 const int app_list_button_index = test_api_->GetButtonCount() - 1;
1439 const gfx::Rect& app_list_ideal_bounds =
1440 test_api_->GetIdealBoundsByIndex(app_list_button_index);
1441 const gfx::Rect& app_list_bounds =
1442 test_api_->GetBoundsByIndex(app_list_button_index);
1443 EXPECT_EQ(app_list_ideal_bounds, app_list_bounds);
1446 // Checks the overflow bubble size when an item is ripped off and re-inserted.
1447 TEST_F(ShelfViewTest, OverflowBubbleSize) {
1448 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1449 ReplaceShelfDelegateForRipOffTest();
1451 AddButtonsUntilOverflow();
1453 // Show overflow bubble.
1454 test_api_->ShowOverflowBubble();
1455 ASSERT_TRUE(test_api_->overflow_bubble() &&
1456 test_api_->overflow_bubble()->IsShowing());
1458 ShelfViewTestAPI test_for_overflow_view(
1459 test_api_->overflow_bubble()->shelf_view());
1461 int ripped_index = test_for_overflow_view.GetLastVisibleIndex();
1462 gfx::Size bubble_size = test_for_overflow_view.GetPreferredSize();
1463 int item_width = test_for_overflow_view.GetButtonSize() +
1464 test_for_overflow_view.GetButtonSpacing();
1466 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
1467 gfx::Point());
1468 ShelfButton* button = test_for_overflow_view.GetButton(ripped_index);
1469 // Rip off the last visible item.
1470 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1471 gfx::Point rip_off_point(start_point.x(), 0);
1472 generator.MoveMouseTo(start_point.x(), start_point.y());
1473 base::MessageLoop::current()->RunUntilIdle();
1474 generator.PressLeftButton();
1475 base::MessageLoop::current()->RunUntilIdle();
1476 generator.MoveMouseTo(rip_off_point.x(), rip_off_point.y());
1477 base::MessageLoop::current()->RunUntilIdle();
1478 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1480 // Check the overflow bubble size when an item is ripped off.
1481 EXPECT_EQ(bubble_size.width() - item_width,
1482 test_for_overflow_view.GetPreferredSize().width());
1483 ASSERT_TRUE(test_api_->overflow_bubble() &&
1484 test_api_->overflow_bubble()->IsShowing());
1486 // Re-insert an item into the overflow bubble.
1487 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1488 button = test_for_overflow_view.GetButton(first_index);
1490 // Check the bubble size after an item is re-inserted.
1491 generator.MoveMouseTo(button->GetBoundsInScreen().CenterPoint());
1492 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1493 EXPECT_EQ(bubble_size.width(),
1494 test_for_overflow_view.GetPreferredSize().width());
1496 generator.ReleaseLeftButton();
1497 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1498 EXPECT_EQ(bubble_size.width(),
1499 test_for_overflow_view.GetPreferredSize().width());
1502 // Check the drag insertion bounds of scrolled overflow bubble.
1503 TEST_F(ShelfViewTest, CheckDragInsertBoundsOfScrolledOverflowBubble) {
1504 UpdateDisplay("400x300");
1506 EXPECT_EQ(2, model_->item_count());
1508 AddButtonsUntilOverflow();
1510 // Show overflow bubble.
1511 test_api_->ShowOverflowBubble();
1512 ASSERT_TRUE(test_api_->overflow_bubble() &&
1513 test_api_->overflow_bubble()->IsShowing());
1515 int item_width = test_api_->GetButtonSize() +
1516 test_api_->GetButtonSpacing();
1517 OverflowBubbleView* bubble_view = test_api_->overflow_bubble()->bubble_view();
1518 test::OverflowBubbleViewTestAPI bubble_view_api(bubble_view);
1520 // Add more buttons until OverflowBubble is scrollable and it has 3 invisible
1521 // items.
1522 while (bubble_view_api.GetContentsSize().width() <
1523 (bubble_view->GetContentsBounds().width() + 3 * item_width))
1524 AddAppShortcut();
1526 ASSERT_TRUE(test_api_->overflow_bubble() &&
1527 test_api_->overflow_bubble()->IsShowing());
1529 ShelfViewTestAPI test_for_overflow_view(
1530 test_api_->overflow_bubble()->shelf_view());
1531 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1532 int last_index = test_for_overflow_view.GetLastVisibleIndex();
1534 ShelfButton* first_button = test_for_overflow_view.GetButton(first_index);
1535 ShelfButton* last_button = test_for_overflow_view.GetButton(last_index);
1536 gfx::Point first_point = first_button->GetBoundsInScreen().CenterPoint();
1537 gfx::Point last_point = last_button->GetBoundsInScreen().CenterPoint();
1538 gfx::Rect drag_reinsert_bounds =
1539 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1540 EXPECT_TRUE(drag_reinsert_bounds.Contains(first_point));
1541 EXPECT_FALSE(drag_reinsert_bounds.Contains(last_point));
1543 // Scrolls sufficiently to show last item.
1544 bubble_view_api.ScrollByXOffset(3 * item_width);
1545 drag_reinsert_bounds =
1546 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1547 first_point = first_button->GetBoundsInScreen().CenterPoint();
1548 last_point = last_button->GetBoundsInScreen().CenterPoint();
1549 EXPECT_FALSE(drag_reinsert_bounds.Contains(first_point));
1550 EXPECT_TRUE(drag_reinsert_bounds.Contains(last_point));
1553 // Check the drag insertion bounds of shelf view in multi monitor environment.
1554 TEST_F(ShelfViewTest, CheckDragInsertBoundsWithMultiMonitor) {
1555 // win8-aura doesn't support multiple display.
1556 if (!SupportsMultipleDisplays())
1557 return;
1559 UpdateDisplay("800x600,800x600");
1560 Shelf* secondary_shelf = Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
1561 ShelfView* shelf_view_for_secondary =
1562 ShelfTestAPI(secondary_shelf).shelf_view();
1564 // The bounds should be big enough for 4 buttons + overflow chevron.
1565 shelf_view_for_secondary->SetBounds(0, 0, 500, kShelfSize);
1567 ShelfViewTestAPI test_api_for_secondary(shelf_view_for_secondary);
1568 // Speeds up animation for test.
1569 test_api_for_secondary.SetAnimationDuration(1);
1571 AddButtonsUntilOverflow();
1573 // Test #1: Test drag insertion bounds of primary shelf.
1574 // Show overflow bubble.
1575 test_api_->ShowOverflowBubble();
1576 ASSERT_TRUE(test_api_->overflow_bubble() &&
1577 test_api_->overflow_bubble()->IsShowing());
1579 ShelfViewTestAPI test_api_for_overflow_view(
1580 test_api_->overflow_bubble()->shelf_view());
1582 ShelfButton* button = test_api_for_overflow_view.GetButton(
1583 test_api_for_overflow_view.GetLastVisibleIndex());
1585 // Checks that a point in shelf is contained in drag insert bounds.
1586 gfx::Point point_in_shelf_view = button->GetBoundsInScreen().CenterPoint();
1587 gfx::Rect drag_reinsert_bounds =
1588 test_api_for_overflow_view.GetBoundsForDragInsertInScreen();
1589 EXPECT_TRUE(drag_reinsert_bounds.Contains(point_in_shelf_view));
1590 // Checks that a point out of shelf is not contained in drag insert bounds.
1591 EXPECT_FALSE(drag_reinsert_bounds.Contains(
1592 gfx::Point(point_in_shelf_view.x(), 0)));
1594 // Test #2: Test drag insertion bounds of secondary shelf.
1595 // Show overflow bubble.
1596 test_api_for_secondary.ShowOverflowBubble();
1597 ASSERT_TRUE(test_api_for_secondary.overflow_bubble() &&
1598 test_api_for_secondary.overflow_bubble()->IsShowing());
1600 ShelfViewTestAPI test_api_for_overflow_view_of_secondary(
1601 test_api_for_secondary.overflow_bubble()->shelf_view());
1603 ShelfButton* button_in_secondary =
1604 test_api_for_overflow_view_of_secondary.GetButton(
1605 test_api_for_overflow_view_of_secondary.GetLastVisibleIndex());
1607 // Checks that a point in shelf is contained in drag insert bounds.
1608 gfx::Point point_in_secondary_shelf_view =
1609 button_in_secondary->GetBoundsInScreen().CenterPoint();
1610 gfx::Rect drag_reinsert_bounds_in_secondary =
1611 test_api_for_overflow_view_of_secondary.GetBoundsForDragInsertInScreen();
1612 EXPECT_TRUE(drag_reinsert_bounds_in_secondary.Contains(
1613 point_in_secondary_shelf_view));
1614 // Checks that a point out of shelf is not contained in drag insert bounds.
1615 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(
1616 gfx::Point(point_in_secondary_shelf_view.x(), 0)));
1617 // Checks that a point of overflow bubble in primary shelf should not be
1618 // contained by insert bounds of secondary shelf.
1619 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(point_in_shelf_view));
1622 // Checks the rip an item off from left aligned shelf in secondary monitor.
1623 TEST_F(ShelfViewTest, CheckRipOffFromLeftShelfAlignmentWithMultiMonitor) {
1624 // win8-aura doesn't support multiple display.
1625 if (!SupportsMultipleDisplays())
1626 return;
1628 UpdateDisplay("800x600,800x600");
1629 ASSERT_EQ(2U, Shell::GetAllRootWindows().size());
1631 aura::Window* second_root = Shell::GetAllRootWindows()[1];
1633 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT, second_root);
1634 ASSERT_EQ(SHELF_ALIGNMENT_LEFT,
1635 Shell::GetInstance()->GetShelfAlignment(second_root));
1637 // Initially, app list and browser shortcut are added.
1638 EXPECT_EQ(2, model_->item_count());
1639 int browser_index = model_->GetItemIndexForType(TYPE_BROWSER_SHORTCUT);
1640 EXPECT_GT(browser_index, 0);
1642 Shelf* secondary_shelf = Shelf::ForWindow(second_root);
1643 ShelfView* shelf_view_for_secondary =
1644 ShelfTestAPI(secondary_shelf).shelf_view();
1646 ShelfViewTestAPI test_api_for_secondary_shelf_view(shelf_view_for_secondary);
1647 ShelfButton* button =
1648 test_api_for_secondary_shelf_view.GetButton(browser_index);
1650 // Fetch the start point of dragging.
1651 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1652 ::wm::ConvertPointFromScreen(second_root, &start_point);
1654 ui::test::EventGenerator generator(second_root, start_point);
1656 // Rip off the browser item.
1657 generator.PressLeftButton();
1658 generator.MoveMouseTo(start_point.x() + 400, start_point.y());
1659 test_api_for_secondary_shelf_view.RunMessageLoopUntilAnimationsDone();
1660 EXPECT_TRUE(test_api_for_secondary_shelf_view.IsRippedOffFromShelf());
1663 // Checks various drag and drop operations from OverflowBubble to Shelf.
1664 TEST_F(ShelfViewTest, CheckDragAndDropFromOverflowBubbleToShelf) {
1665 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1666 ReplaceShelfDelegateForRipOffTest();
1668 AddButtonsUntilOverflow();
1670 TestDraggingAnItemFromOverflowToShelf(false);
1671 TestDraggingAnItemFromOverflowToShelf(true);
1674 class ShelfViewVisibleBoundsTest : public ShelfViewTest,
1675 public testing::WithParamInterface<bool> {
1676 public:
1677 ShelfViewVisibleBoundsTest() : text_direction_change_(GetParam()) {}
1679 void CheckAllItemsAreInBounds() {
1680 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1681 gfx::Rect shelf_bounds = shelf_view_->GetBoundsInScreen();
1682 EXPECT_TRUE(shelf_bounds.Contains(visible_bounds));
1683 for (int i = 0; i < test_api_->GetButtonCount(); ++i)
1684 if (ShelfButton* button = test_api_->GetButton(i))
1685 EXPECT_TRUE(visible_bounds.Contains(button->GetBoundsInScreen()));
1686 CheckAppListButtonIsInBounds();
1689 void CheckAppListButtonIsInBounds() {
1690 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1691 gfx::Rect app_list_button_bounds = shelf_view_->GetAppListButtonView()->
1692 GetBoundsInScreen();
1693 EXPECT_TRUE(visible_bounds.Contains(app_list_button_bounds));
1696 private:
1697 ScopedTextDirectionChange text_direction_change_;
1699 DISALLOW_COPY_AND_ASSIGN(ShelfViewVisibleBoundsTest);
1702 TEST_P(ShelfViewVisibleBoundsTest, ItemsAreInBounds) {
1703 // Adding elements leaving some empty space.
1704 for (int i = 0; i < 3; i++) {
1705 AddAppShortcut();
1707 test_api_->RunMessageLoopUntilAnimationsDone();
1708 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
1709 CheckAllItemsAreInBounds();
1710 // Same for overflow case.
1711 while (!test_api_->IsOverflowButtonVisible()) {
1712 AddAppShortcut();
1714 test_api_->RunMessageLoopUntilAnimationsDone();
1715 CheckAllItemsAreInBounds();
1718 INSTANTIATE_TEST_CASE_P(LtrRtl, ShelfViewTextDirectionTest, testing::Bool());
1719 INSTANTIATE_TEST_CASE_P(VisibleBounds, ShelfViewVisibleBoundsTest,
1720 testing::Bool());
1722 } // namespace test
1723 } // namespace ash