Roll src/third_party/WebKit f36d5e0:68b67cd (svn 193299:193303)
[chromium-blink-merge.git] / ash / shelf / shelf_view_unittest.cc
blob177dc0059c9e13515d2deb91030efc74e8502465
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/root_window_controller.h"
11 #include "ash/shelf/app_list_button.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/compiler_specific.h"
34 #include "base/memory/scoped_ptr.h"
35 #include "base/strings/string_number_conversions.h"
36 #include "ui/aura/test/aura_test_base.h"
37 #include "ui/aura/window.h"
38 #include "ui/aura/window_event_dispatcher.h"
39 #include "ui/base/l10n/l10n_util.h"
40 #include "ui/compositor/layer.h"
41 #include "ui/events/event.h"
42 #include "ui/events/event_constants.h"
43 #include "ui/events/event_utils.h"
44 #include "ui/events/test/event_generator.h"
45 #include "ui/views/view_model.h"
46 #include "ui/views/widget/widget.h"
47 #include "ui/views/widget/widget_delegate.h"
48 #include "ui/wm/core/coordinate_conversion.h"
50 namespace ash {
51 namespace test {
53 ////////////////////////////////////////////////////////////////////////////////
54 // ShelfIconObserver tests.
56 class TestShelfIconObserver : public ShelfIconObserver {
57 public:
58 explicit TestShelfIconObserver(Shelf* shelf)
59 : shelf_(shelf),
60 change_notified_(false) {
61 if (shelf_)
62 shelf_->AddIconObserver(this);
65 ~TestShelfIconObserver() override {
66 if (shelf_)
67 shelf_->RemoveIconObserver(this);
70 // ShelfIconObserver implementation.
71 void OnShelfIconPositionsChanged() override { change_notified_ = true; }
73 int change_notified() const { return change_notified_; }
74 void Reset() { change_notified_ = false; }
76 private:
77 Shelf* shelf_;
78 bool change_notified_;
80 DISALLOW_COPY_AND_ASSIGN(TestShelfIconObserver);
83 class ShelfViewIconObserverTest : public AshTestBase {
84 public:
85 ShelfViewIconObserverTest() {}
86 ~ShelfViewIconObserverTest() override {}
88 void SetUp() override {
89 AshTestBase::SetUp();
90 Shelf* shelf = Shelf::ForPrimaryDisplay();
91 observer_.reset(new TestShelfIconObserver(shelf));
93 shelf_view_test_.reset(
94 new ShelfViewTestAPI(ShelfTestAPI(shelf).shelf_view()));
95 shelf_view_test_->SetAnimationDuration(1);
98 void TearDown() override {
99 observer_.reset();
100 AshTestBase::TearDown();
103 TestShelfIconObserver* observer() { return observer_.get(); }
105 ShelfViewTestAPI* shelf_view_test() {
106 return shelf_view_test_.get();
109 Shelf* ShelfForSecondaryDisplay() {
110 return Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
113 private:
114 scoped_ptr<TestShelfIconObserver> observer_;
115 scoped_ptr<ShelfViewTestAPI> shelf_view_test_;
117 DISALLOW_COPY_AND_ASSIGN(ShelfViewIconObserverTest);
120 // TestShelfItemDelegate which tracks whether it gets selected.
121 class ShelfItemSelectionTracker : public TestShelfItemDelegate {
122 public:
123 ShelfItemSelectionTracker() : TestShelfItemDelegate(NULL), selected_(false) {
126 ~ShelfItemSelectionTracker() override {}
128 // Resets to the initial state.
129 void Reset() { selected_ = false; }
131 // Returns true if the delegate was selected.
132 bool WasSelected() {
133 return selected_;
136 // TestShelfItemDelegate:
137 bool ItemSelected(const ui::Event& event) override {
138 selected_ = true;
139 return false;
142 private:
143 bool selected_;
145 DISALLOW_COPY_AND_ASSIGN(ShelfItemSelectionTracker);
148 TEST_F(ShelfViewIconObserverTest, AddRemove) {
149 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
150 ASSERT_TRUE(shelf_delegate);
152 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
153 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
154 params.bounds = gfx::Rect(0, 0, 200, 200);
155 params.context = CurrentContext();
157 scoped_ptr<views::Widget> widget(new views::Widget());
158 widget->Init(params);
159 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
160 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
161 EXPECT_TRUE(observer()->change_notified());
162 observer()->Reset();
164 widget->Show();
165 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
166 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
167 EXPECT_TRUE(observer()->change_notified());
168 observer()->Reset();
171 // Sometimes fails on trybots on win7_aura. http://crbug.com/177135
172 #if defined(OS_WIN)
173 #define MAYBE_AddRemoveWithMultipleDisplays \
174 DISABLED_AddRemoveWithMultipleDisplays
175 #else
176 #define MAYBE_AddRemoveWithMultipleDisplays \
177 AddRemoveWithMultipleDisplays
178 #endif
179 // Make sure creating/deleting an window on one displays notifies a
180 // shelf on external display as well as one on primary.
181 TEST_F(ShelfViewIconObserverTest, MAYBE_AddRemoveWithMultipleDisplays) {
182 UpdateDisplay("400x400,400x400");
183 TestShelfIconObserver second_observer(ShelfForSecondaryDisplay());
185 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
186 ASSERT_TRUE(shelf_delegate);
188 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
189 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
190 params.bounds = gfx::Rect(0, 0, 200, 200);
191 params.context = CurrentContext();
193 scoped_ptr<views::Widget> widget(new views::Widget());
194 widget->Init(params);
195 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
196 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
197 EXPECT_TRUE(observer()->change_notified());
198 EXPECT_TRUE(second_observer.change_notified());
199 observer()->Reset();
200 second_observer.Reset();
202 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
203 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
204 EXPECT_TRUE(observer()->change_notified());
205 EXPECT_TRUE(second_observer.change_notified());
207 observer()->Reset();
208 second_observer.Reset();
211 TEST_F(ShelfViewIconObserverTest, BoundsChanged) {
212 ShelfWidget* widget = Shell::GetPrimaryRootWindowController()->shelf();
213 Shelf* shelf = Shelf::ForPrimaryDisplay();
214 gfx::Size shelf_size = widget->GetWindowBoundsInScreen().size();
215 shelf_size.set_width(shelf_size.width() / 2);
216 ASSERT_GT(shelf_size.width(), 0);
217 shelf->SetShelfViewBounds(gfx::Rect(shelf_size));
218 // No animation happens for ShelfView bounds change.
219 EXPECT_TRUE(observer()->change_notified());
220 observer()->Reset();
223 ////////////////////////////////////////////////////////////////////////////////
224 // ShelfView tests.
226 // Simple ShelfDelegate implmentation for ShelfViewTest.OverflowBubbleSize
227 // and CheckDragAndDropFromOverflowBubbleToShelf
228 class TestShelfDelegateForShelfView : public ShelfDelegate {
229 public:
230 explicit TestShelfDelegateForShelfView(ShelfModel* model)
231 : model_(model) {}
232 ~TestShelfDelegateForShelfView() override {}
234 // ShelfDelegate overrides:
235 void OnShelfCreated(Shelf* shelf) override {}
237 void OnShelfDestroyed(Shelf* shelf) override {}
239 ShelfID GetShelfIDForAppID(const std::string& app_id) override {
240 ShelfID id = 0;
241 EXPECT_TRUE(base::StringToInt(app_id, &id));
242 return id;
245 const std::string& GetAppIDForShelfID(ShelfID id) override {
246 // Use |app_id_| member variable because returning a reference to local
247 // variable is not allowed.
248 app_id_ = base::IntToString(id);
249 return app_id_;
252 void PinAppWithID(const std::string& app_id) override {}
254 bool IsAppPinned(const std::string& app_id) override {
255 // Returns true for ShelfViewTest.OverflowBubbleSize. To test ripping off in
256 // that test, an item is already pinned state.
257 return true;
260 bool CanPin() const override { return true; }
262 void UnpinAppWithID(const std::string& app_id) override {
263 ShelfID id = 0;
264 EXPECT_TRUE(base::StringToInt(app_id, &id));
265 ASSERT_GT(id, 0);
266 int index = model_->ItemIndexByID(id);
267 ASSERT_GE(index, 0);
269 model_->RemoveItemAt(index);
272 private:
273 ShelfModel* model_;
275 // Temp member variable for returning a value. See the comment in the
276 // GetAppIDForShelfID().
277 std::string app_id_;
279 DISALLOW_COPY_AND_ASSIGN(TestShelfDelegateForShelfView);
282 class ShelfViewTest : public AshTestBase {
283 public:
284 ShelfViewTest()
285 : model_(NULL),
286 shelf_view_(NULL),
287 browser_index_(1),
288 item_manager_(NULL) {}
289 ~ShelfViewTest() override {}
291 void SetUp() override {
292 AshTestBase::SetUp();
293 test::ShellTestApi test_api(Shell::GetInstance());
294 model_ = test_api.shelf_model();
295 Shelf* shelf = Shelf::ForPrimaryDisplay();
296 shelf_view_ = ShelfTestAPI(shelf).shelf_view();
298 // The bounds should be big enough for 4 buttons + overflow chevron.
299 shelf_view_->SetBounds(0, 0, 500, kShelfSize);
301 test_api_.reset(new ShelfViewTestAPI(shelf_view_));
302 test_api_->SetAnimationDuration(1); // Speeds up animation for test.
304 item_manager_ = Shell::GetInstance()->shelf_item_delegate_manager();
305 DCHECK(item_manager_);
307 // Add browser shortcut shelf item at index 0 for test.
308 AddBrowserShortcut();
311 void TearDown() override {
312 test_api_.reset();
313 AshTestBase::TearDown();
316 protected:
317 void CreateAndSetShelfItemDelegateForID(ShelfID id) {
318 scoped_ptr<ShelfItemDelegate> delegate(new TestShelfItemDelegate(NULL));
319 item_manager_->SetShelfItemDelegate(id, delegate.Pass());
322 ShelfID AddBrowserShortcut() {
323 ShelfItem browser_shortcut;
324 browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
326 ShelfID id = model_->next_id();
327 model_->AddAt(browser_index_, browser_shortcut);
328 CreateAndSetShelfItemDelegateForID(id);
329 test_api_->RunMessageLoopUntilAnimationsDone();
330 return id;
333 ShelfID AddAppShortcut() {
334 ShelfItem item;
335 item.type = TYPE_APP_SHORTCUT;
336 item.status = STATUS_CLOSED;
338 ShelfID id = model_->next_id();
339 model_->Add(item);
340 CreateAndSetShelfItemDelegateForID(id);
341 test_api_->RunMessageLoopUntilAnimationsDone();
342 return id;
345 ShelfID AddPanel() {
346 ShelfID id = AddPanelNoWait();
347 test_api_->RunMessageLoopUntilAnimationsDone();
348 return id;
351 ShelfID AddPlatformAppNoWait() {
352 ShelfItem item;
353 item.type = TYPE_PLATFORM_APP;
354 item.status = STATUS_RUNNING;
356 ShelfID id = model_->next_id();
357 model_->Add(item);
358 CreateAndSetShelfItemDelegateForID(id);
359 return id;
362 ShelfID AddPanelNoWait() {
363 ShelfItem item;
364 item.type = TYPE_APP_PANEL;
365 item.status = STATUS_RUNNING;
367 ShelfID id = model_->next_id();
368 model_->Add(item);
369 CreateAndSetShelfItemDelegateForID(id);
370 return id;
373 ShelfID AddPlatformApp() {
374 ShelfID id = AddPlatformAppNoWait();
375 test_api_->RunMessageLoopUntilAnimationsDone();
376 return id;
379 void RemoveByID(ShelfID id) {
380 model_->RemoveItemAt(model_->ItemIndexByID(id));
381 test_api_->RunMessageLoopUntilAnimationsDone();
384 ShelfButton* GetButtonByID(ShelfID id) {
385 int index = model_->ItemIndexByID(id);
386 return test_api_->GetButton(index);
389 ShelfItem GetItemByID(ShelfID id) {
390 ShelfItems::const_iterator items = model_->ItemByID(id);
391 return *items;
394 void CheckModelIDs(
395 const std::vector<std::pair<ShelfID, views::View*> >& id_map) {
396 size_t map_index = 0;
397 for (size_t model_index = 0;
398 model_index < model_->items().size();
399 ++model_index) {
400 ShelfItem item = model_->items()[model_index];
401 ShelfID id = item.id;
402 EXPECT_EQ(id_map[map_index].first, id);
403 EXPECT_EQ(id_map[map_index].second, GetButtonByID(id));
404 ++map_index;
406 ASSERT_EQ(map_index, id_map.size());
409 void VerifyShelfItemBoundsAreValid() {
410 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
411 if (test_api_->GetButton(i)) {
412 gfx::Rect shelf_view_bounds = shelf_view_->GetLocalBounds();
413 gfx::Rect item_bounds = test_api_->GetBoundsByIndex(i);
414 EXPECT_GE(item_bounds.x(), 0);
415 EXPECT_GE(item_bounds.y(), 0);
416 EXPECT_LE(item_bounds.right(), shelf_view_bounds.width());
417 EXPECT_LE(item_bounds.bottom(), shelf_view_bounds.height());
422 ShelfButton* SimulateButtonPressed(ShelfButtonHost::Pointer pointer,
423 int button_index) {
424 ShelfButtonHost* button_host = shelf_view_;
425 ShelfButton* button = test_api_->GetButton(button_index);
426 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED, gfx::Point(),
427 button->GetBoundsInScreen().origin(),
428 ui::EventTimeForNow(), 0, 0);
429 button_host->PointerPressedOnButton(button, pointer, click_event);
430 return button;
433 // Simulates a single mouse click.
434 void SimulateClick(int button_index) {
435 ShelfButtonHost* button_host = shelf_view_;
436 ShelfButton* button =
437 SimulateButtonPressed(ShelfButtonHost::MOUSE, button_index);
438 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, gfx::Point(),
439 button->GetBoundsInScreen().origin(),
440 ui::EventTimeForNow(), 0, 0);
441 test_api_->ButtonPressed(button, release_event);
442 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false);
445 // Simulates the second click of a double click.
446 void SimulateDoubleClick(int button_index) {
447 ShelfButtonHost* button_host = shelf_view_;
448 ShelfButton* button =
449 SimulateButtonPressed(ShelfButtonHost::MOUSE, button_index);
450 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, gfx::Point(),
451 button->GetBoundsInScreen().origin(),
452 ui::EventTimeForNow(), ui::EF_IS_DOUBLE_CLICK,
454 test_api_->ButtonPressed(button, release_event);
455 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false);
458 views::View* SimulateDrag(ShelfButtonHost::Pointer pointer,
459 int button_index,
460 int destination_index) {
461 ShelfButtonHost* button_host = shelf_view_;
462 views::View* button = SimulateButtonPressed(pointer, button_index);
464 // Drag.
465 views::View* destination = test_api_->GetButton(destination_index);
466 ui::MouseEvent drag_event(
467 ui::ET_MOUSE_DRAGGED, gfx::Point(destination->x() - button->x(),
468 destination->y() - button->y()),
469 destination->GetBoundsInScreen().origin(), ui::EventTimeForNow(), 0, 0);
470 button_host->PointerDraggedOnButton(button, pointer, drag_event);
471 return button;
474 void SetupForDragTest(
475 std::vector<std::pair<ShelfID, views::View*> >* id_map) {
476 // Initialize |id_map| with the automatically-created shelf buttons.
477 for (size_t i = 0; i < model_->items().size(); ++i) {
478 ShelfButton* button = test_api_->GetButton(i);
479 id_map->push_back(std::make_pair(model_->items()[i].id, button));
481 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
483 // Add 5 app shelf buttons for testing.
484 for (int i = 0; i < 5; ++i) {
485 ShelfID id = AddAppShortcut();
486 // App Icon is located at index 0, and browser shortcut is located at
487 // index 1. So we should start to add app shortcut at index 2.
488 id_map->insert(id_map->begin() + (i + browser_index_ + 1),
489 std::make_pair(id, GetButtonByID(id)));
491 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
494 views::View* GetTooltipAnchorView() {
495 return shelf_view_->tooltip_manager()->anchor_;
498 void AddButtonsUntilOverflow() {
499 int items_added = 0;
500 while (!test_api_->IsOverflowButtonVisible()) {
501 AddAppShortcut();
502 ++items_added;
503 ASSERT_LT(items_added, 10000);
507 void ShowTooltip() {
508 shelf_view_->tooltip_manager()->ShowInternal();
511 void TestDraggingAnItemFromOverflowToShelf(bool cancel) {
512 test_api_->ShowOverflowBubble();
513 ASSERT_TRUE(test_api_->overflow_bubble() &&
514 test_api_->overflow_bubble()->IsShowing());
516 ash::test::ShelfViewTestAPI test_api_for_overflow(
517 test_api_->overflow_bubble()->shelf_view());
519 int total_item_count = model_->item_count();
521 int last_visible_item_id_in_shelf =
522 GetItemId(test_api_->GetLastVisibleIndex());
523 int second_last_visible_item_id_in_shelf =
524 GetItemId(test_api_->GetLastVisibleIndex() - 1);
525 int first_visible_item_id_in_overflow =
526 GetItemId(test_api_for_overflow.GetFirstVisibleIndex());
527 int second_last_visible_item_id_in_overflow =
528 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1);
530 int drag_item_index =
531 test_api_for_overflow.GetLastVisibleIndex();
532 ShelfID drag_item_id = GetItemId(drag_item_index);
533 ShelfButton* drag_button = test_api_for_overflow.GetButton(drag_item_index);
534 gfx::Point center_point_of_drag_item =
535 drag_button->GetBoundsInScreen().CenterPoint();
537 ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow(),
538 center_point_of_drag_item);
539 // Rip an item off to OverflowBubble.
540 generator.PressLeftButton();
541 gfx::Point rip_off_point(center_point_of_drag_item.x(), 0);
542 generator.MoveMouseTo(rip_off_point);
543 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
544 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
545 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
547 // Move a dragged item into Shelf at |drop_index|.
548 int drop_index = 1;
549 gfx::Point drop_point =
550 test_api_->GetButton(drop_index)->GetBoundsInScreen().CenterPoint();
551 int item_width = test_api_for_overflow.GetButtonSize();
552 // To insert at |drop_index|, more smaller x-axis value of |drop_point|
553 // should be used.
554 gfx::Point modified_drop_point(drop_point.x() - item_width / 4,
555 drop_point.y());
556 generator.MoveMouseTo(modified_drop_point);
557 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
558 test_api_->RunMessageLoopUntilAnimationsDone();
559 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
560 ASSERT_TRUE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
562 if (cancel)
563 drag_button->OnMouseCaptureLost();
564 else
565 generator.ReleaseLeftButton();
567 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
568 test_api_->RunMessageLoopUntilAnimationsDone();
569 ASSERT_FALSE(test_api_for_overflow.IsRippedOffFromShelf());
570 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
572 // Compare pre-stored items' id with newly positioned items' after dragging
573 // is canceled or finished.
574 if (cancel) {
575 EXPECT_EQ(last_visible_item_id_in_shelf,
576 GetItemId(test_api_->GetLastVisibleIndex()));
577 EXPECT_EQ(second_last_visible_item_id_in_shelf,
578 GetItemId(test_api_->GetLastVisibleIndex() - 1));
579 EXPECT_EQ(first_visible_item_id_in_overflow,
580 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
581 EXPECT_EQ(second_last_visible_item_id_in_overflow,
582 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1));
583 } else {
584 EXPECT_EQ(drag_item_id, GetItemId(drop_index));
585 EXPECT_EQ(total_item_count, model_->item_count());
586 EXPECT_EQ(last_visible_item_id_in_shelf,
587 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
588 EXPECT_EQ(second_last_visible_item_id_in_shelf,
589 GetItemId(test_api_->GetLastVisibleIndex()));
590 EXPECT_EQ(first_visible_item_id_in_overflow,
591 GetItemId(test_api_for_overflow.GetFirstVisibleIndex() + 1));
592 EXPECT_EQ(second_last_visible_item_id_in_overflow,
593 GetItemId(test_api_for_overflow.GetLastVisibleIndex()));
597 // Returns the item's ShelfID at |index|.
598 ShelfID GetItemId(int index) {
599 DCHECK_GE(index, 0);
600 return model_->items()[index].id;
603 void ReplaceShelfDelegateForRipOffTest() {
604 // Replace ShelfDelegate.
605 test::ShellTestApi test_api(Shell::GetInstance());
606 test_api.SetShelfDelegate(NULL);
607 ShelfDelegate* delegate = new TestShelfDelegateForShelfView(model_);
608 test_api.SetShelfDelegate(delegate);
609 test::ShelfTestAPI(Shelf::ForPrimaryDisplay()).SetShelfDelegate(delegate);
610 test_api_->SetShelfDelegate(delegate);
613 ShelfModel* model_;
614 ShelfView* shelf_view_;
615 int browser_index_;
616 ShelfItemDelegateManager* item_manager_;
618 scoped_ptr<ShelfViewTestAPI> test_api_;
620 private:
621 DISALLOW_COPY_AND_ASSIGN(ShelfViewTest);
624 class ScopedTextDirectionChange {
625 public:
626 explicit ScopedTextDirectionChange(bool is_rtl) : is_rtl_(is_rtl) {
627 original_locale_ = l10n_util::GetApplicationLocale(std::string());
628 if (is_rtl_)
629 base::i18n::SetICUDefaultLocale("he");
630 CheckTextDirectionIsCorrect();
633 ~ScopedTextDirectionChange() {
634 if (is_rtl_)
635 base::i18n::SetICUDefaultLocale(original_locale_);
638 private:
639 void CheckTextDirectionIsCorrect() {
640 ASSERT_EQ(is_rtl_, base::i18n::IsRTL());
643 bool is_rtl_;
644 std::string original_locale_;
647 class ShelfViewTextDirectionTest
648 : public ShelfViewTest,
649 public testing::WithParamInterface<bool> {
650 public:
651 ShelfViewTextDirectionTest() : text_direction_change_(GetParam()) {}
652 virtual ~ShelfViewTextDirectionTest() {}
654 void SetUp() override { ShelfViewTest::SetUp(); }
656 void TearDown() override { ShelfViewTest::TearDown(); }
658 private:
659 ScopedTextDirectionChange text_direction_change_;
661 DISALLOW_COPY_AND_ASSIGN(ShelfViewTextDirectionTest);
664 // Checks that the ideal item icon bounds match the view's bounds in the screen
665 // in both LTR and RTL.
666 TEST_P(ShelfViewTextDirectionTest, IdealBoundsOfItemIcon) {
667 ShelfID id = AddPlatformApp();
668 ShelfButton* button = GetButtonByID(id);
669 gfx::Rect item_bounds = button->GetBoundsInScreen();
670 gfx::Point icon_offset = button->GetIconBounds().origin();
671 item_bounds.Offset(icon_offset.OffsetFromOrigin());
672 gfx::Rect ideal_bounds = shelf_view_->GetIdealBoundsOfItemIcon(id);
673 gfx::Point screen_origin;
674 views::View::ConvertPointToScreen(shelf_view_, &screen_origin);
675 ideal_bounds.Offset(screen_origin.x(), screen_origin.y());
676 EXPECT_EQ(item_bounds.x(), ideal_bounds.x());
677 EXPECT_EQ(item_bounds.y(), ideal_bounds.y());
680 // Check that items in the overflow area are returning the overflow button as
681 // ideal bounds.
682 TEST_F(ShelfViewTest, OverflowButtonBounds) {
683 ShelfID first_id = AddPlatformApp();
684 ShelfID overflow_id = AddPlatformApp();
685 int items_added = 0;
686 while (!test_api_->IsOverflowButtonVisible()) {
687 // Added button is visible after animation while in this loop.
688 EXPECT_TRUE(GetButtonByID(overflow_id)->visible());
689 overflow_id = AddPlatformApp();
690 ++items_added;
691 ASSERT_LT(items_added, 10000);
693 ShelfID last_id = AddPlatformApp();
695 gfx::Rect first_bounds = shelf_view_->GetIdealBoundsOfItemIcon(first_id);
696 gfx::Rect overflow_bounds =
697 shelf_view_->GetIdealBoundsOfItemIcon(overflow_id);
698 gfx::Rect last_bounds = shelf_view_->GetIdealBoundsOfItemIcon(last_id);
700 // Check that all items have the same size and that the overflow items are
701 // identical whereas the first one does not match either of them.
702 EXPECT_EQ(first_bounds.size().ToString(), last_bounds.size().ToString());
703 EXPECT_NE(first_bounds.ToString(), last_bounds.ToString());
704 EXPECT_EQ(overflow_bounds.ToString(), last_bounds.ToString());
707 // Checks that shelf view contents are considered in the correct drag group.
708 TEST_F(ShelfViewTest, EnforceDragType) {
709 EXPECT_TRUE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_PLATFORM_APP));
710 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_SHORTCUT));
711 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP,
712 TYPE_BROWSER_SHORTCUT));
713 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_WINDOWED_APP));
714 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_LIST));
715 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_PANEL));
717 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_SHORTCUT));
718 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
719 TYPE_BROWSER_SHORTCUT));
720 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
721 TYPE_WINDOWED_APP));
722 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_LIST));
723 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_PANEL));
725 EXPECT_TRUE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
726 TYPE_BROWSER_SHORTCUT));
727 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
728 TYPE_WINDOWED_APP));
729 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_LIST));
730 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_PANEL));
732 EXPECT_TRUE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_WINDOWED_APP));
733 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_LIST));
734 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_PANEL));
736 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_LIST));
737 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_PANEL));
739 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_PANEL, TYPE_APP_PANEL));
742 // Adds platform app button until overflow and verifies that the last added
743 // platform app button is hidden.
744 TEST_F(ShelfViewTest, AddBrowserUntilOverflow) {
745 // All buttons should be visible.
746 ASSERT_EQ(test_api_->GetButtonCount(),
747 test_api_->GetLastVisibleIndex() + 1);
749 // Add platform app button until overflow.
750 int items_added = 0;
751 ShelfID last_added = AddPlatformApp();
752 while (!test_api_->IsOverflowButtonVisible()) {
753 // Added button is visible after animation while in this loop.
754 EXPECT_TRUE(GetButtonByID(last_added)->visible());
756 last_added = AddPlatformApp();
757 ++items_added;
758 ASSERT_LT(items_added, 10000);
761 // The last added button should be invisible.
762 EXPECT_FALSE(GetButtonByID(last_added)->visible());
765 // Adds one platform app button then adds app shortcut until overflow. Verifies
766 // that the browser button gets hidden on overflow and last added app shortcut
767 // is still visible.
768 TEST_F(ShelfViewTest, AddAppShortcutWithBrowserButtonUntilOverflow) {
769 // All buttons should be visible.
770 ASSERT_EQ(test_api_->GetButtonCount(),
771 test_api_->GetLastVisibleIndex() + 1);
773 ShelfID browser_button_id = AddPlatformApp();
775 // Add app shortcut until overflow.
776 int items_added = 0;
777 ShelfID last_added = AddAppShortcut();
778 while (!test_api_->IsOverflowButtonVisible()) {
779 // Added button is visible after animation while in this loop.
780 EXPECT_TRUE(GetButtonByID(last_added)->visible());
782 last_added = AddAppShortcut();
783 ++items_added;
784 ASSERT_LT(items_added, 10000);
787 // And the platform app button is invisible.
788 EXPECT_FALSE(GetButtonByID(browser_button_id)->visible());
791 TEST_F(ShelfViewTest, AddPanelHidesPlatformAppButton) {
792 // All buttons should be visible.
793 ASSERT_EQ(test_api_->GetButtonCount(),
794 test_api_->GetLastVisibleIndex() + 1);
796 // Add platform app button until overflow, remember last visible platform app
797 // button.
798 int items_added = 0;
799 ShelfID first_added = AddPlatformApp();
800 EXPECT_TRUE(GetButtonByID(first_added)->visible());
801 while (true) {
802 ShelfID added = AddPlatformApp();
803 if (test_api_->IsOverflowButtonVisible()) {
804 EXPECT_FALSE(GetButtonByID(added)->visible());
805 RemoveByID(added);
806 break;
808 ++items_added;
809 ASSERT_LT(items_added, 10000);
812 ShelfID panel = AddPanel();
813 EXPECT_TRUE(test_api_->IsOverflowButtonVisible());
815 RemoveByID(panel);
816 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
819 // When there are more panels then platform app buttons we should hide panels
820 // rather than platform apps.
821 TEST_F(ShelfViewTest, PlatformAppHidesExcessPanels) {
822 // All buttons should be visible.
823 ASSERT_EQ(test_api_->GetButtonCount(),
824 test_api_->GetLastVisibleIndex() + 1);
826 // Add platform app button.
827 ShelfID platform_app = AddPlatformApp();
828 ShelfID first_panel = AddPanel();
830 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
831 EXPECT_TRUE(GetButtonByID(first_panel)->visible());
833 // Add panels until there is an overflow.
834 ShelfID last_panel = first_panel;
835 int items_added = 0;
836 while (!test_api_->IsOverflowButtonVisible()) {
837 last_panel = AddPanel();
838 ++items_added;
839 ASSERT_LT(items_added, 10000);
842 // The first panel should now be hidden by the new platform apps needing
843 // space.
844 EXPECT_FALSE(GetButtonByID(first_panel)->visible());
845 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
846 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
848 // Adding platform apps should eventually begin to hide platform apps. We will
849 // add platform apps until either the last panel or platform app is hidden.
850 items_added = 0;
851 while (GetButtonByID(platform_app)->visible() &&
852 GetButtonByID(last_panel)->visible()) {
853 platform_app = AddPlatformApp();
854 ++items_added;
855 ASSERT_LT(items_added, 10000);
857 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
858 EXPECT_FALSE(GetButtonByID(platform_app)->visible());
861 // Making sure that no buttons on the shelf will ever overlap after adding many
862 // of them.
863 TEST_F(ShelfViewTest, AssertNoButtonsOverlap) {
864 std::vector<ShelfID> button_ids;
865 // Add app icons until the overflow button is visible.
866 while (!test_api_->IsOverflowButtonVisible()) {
867 ShelfID id = AddPlatformApp();
868 button_ids.push_back(id);
870 ASSERT_LT(button_ids.size(), 10000U);
871 ASSERT_GT(button_ids.size(), 2U);
873 // Remove 2 icons to make more room for panel icons, the overflow button
874 // should go away.
875 for (int i = 0; i < 2; ++i) {
876 ShelfID id = button_ids.back();
877 RemoveByID(id);
878 button_ids.pop_back();
880 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
881 EXPECT_TRUE(GetButtonByID(button_ids.back())->visible());
883 // Add 20 panel icons, and expect to have overflow.
884 for (int i = 0; i < 20; ++i) {
885 ShelfID id = AddPanel();
886 button_ids.push_back(id);
888 ASSERT_LT(button_ids.size(), 10000U);
889 EXPECT_TRUE(test_api_->IsOverflowButtonVisible());
891 // Test that any two successive visible icons never overlap in all shelf
892 // alignment types.
893 const ShelfAlignment kAlignments[] = {
894 SHELF_ALIGNMENT_LEFT,
895 SHELF_ALIGNMENT_RIGHT,
896 SHELF_ALIGNMENT_TOP,
897 SHELF_ALIGNMENT_BOTTOM
900 for (ShelfAlignment alignment : kAlignments) {
901 EXPECT_TRUE(shelf_view_->shelf_layout_manager()->SetAlignment(alignment));
902 // For every 2 successive visible icons, expect that their bounds don't
903 // intersect.
904 for (int i = 1; i < test_api_->GetButtonCount() - 1; ++i) {
905 if (!(test_api_->GetButton(i)->visible() &&
906 test_api_->GetButton(i + 1)->visible())) {
907 continue;
910 const gfx::Rect& bounds1 = test_api_->GetBoundsByIndex(i);
911 const gfx::Rect& bounds2 = test_api_->GetBoundsByIndex(i + 1);
912 EXPECT_FALSE(bounds1.Intersects(bounds2));
917 // Adds button until overflow then removes first added one. Verifies that
918 // the last added one changes from invisible to visible and overflow
919 // chevron is gone.
920 TEST_F(ShelfViewTest, RemoveButtonRevealsOverflowed) {
921 // All buttons should be visible.
922 ASSERT_EQ(test_api_->GetButtonCount(),
923 test_api_->GetLastVisibleIndex() + 1);
925 // Add platform app buttons until overflow.
926 int items_added = 0;
927 ShelfID first_added = AddPlatformApp();
928 ShelfID last_added = first_added;
929 while (!test_api_->IsOverflowButtonVisible()) {
930 last_added = AddPlatformApp();
931 ++items_added;
932 ASSERT_LT(items_added, 10000);
935 // Expect add more than 1 button. First added is visible and last is not.
936 EXPECT_NE(first_added, last_added);
937 EXPECT_TRUE(GetButtonByID(first_added)->visible());
938 EXPECT_FALSE(GetButtonByID(last_added)->visible());
940 // Remove first added.
941 RemoveByID(first_added);
943 // Last added button becomes visible and overflow chevron is gone.
944 EXPECT_TRUE(GetButtonByID(last_added)->visible());
945 EXPECT_EQ(1.0f, GetButtonByID(last_added)->layer()->opacity());
946 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
949 // Verifies that remove last overflowed button should hide overflow chevron.
950 TEST_F(ShelfViewTest, RemoveLastOverflowed) {
951 // All buttons should be visible.
952 ASSERT_EQ(test_api_->GetButtonCount(),
953 test_api_->GetLastVisibleIndex() + 1);
955 // Add platform app button until overflow.
956 int items_added = 0;
957 ShelfID last_added = AddPlatformApp();
958 while (!test_api_->IsOverflowButtonVisible()) {
959 last_added = AddPlatformApp();
960 ++items_added;
961 ASSERT_LT(items_added, 10000);
964 RemoveByID(last_added);
965 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
968 // Adds platform app button without waiting for animation to finish and verifies
969 // that all added buttons are visible.
970 TEST_F(ShelfViewTest, AddButtonQuickly) {
971 // All buttons should be visible.
972 ASSERT_EQ(test_api_->GetButtonCount(),
973 test_api_->GetLastVisibleIndex() + 1);
975 // Add a few platform buttons quickly without wait for animation.
976 int added_count = 0;
977 while (!test_api_->IsOverflowButtonVisible()) {
978 AddPlatformAppNoWait();
979 ++added_count;
980 ASSERT_LT(added_count, 10000);
983 // ShelfView should be big enough to hold at least 3 new buttons.
984 ASSERT_GE(added_count, 3);
986 // Wait for the last animation to finish.
987 test_api_->RunMessageLoopUntilAnimationsDone();
989 // Verifies non-overflow buttons are visible.
990 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
991 ShelfButton* button = test_api_->GetButton(i);
992 if (button) {
993 EXPECT_TRUE(button->visible()) << "button index=" << i;
994 EXPECT_EQ(1.0f, button->layer()->opacity()) << "button index=" << i;
999 // Check that model changes are handled correctly while a shelf icon is being
1000 // dragged.
1001 TEST_F(ShelfViewTest, ModelChangesWhileDragging) {
1002 ShelfButtonHost* button_host = shelf_view_;
1004 std::vector<std::pair<ShelfID, views::View*> > id_map;
1005 SetupForDragTest(&id_map);
1007 // Dragging browser shortcut at index 1.
1008 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
1009 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1010 std::rotate(id_map.begin() + 1,
1011 id_map.begin() + 2,
1012 id_map.begin() + 4);
1013 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1014 button_host->PointerReleasedOnButton(
1015 dragged_button, ShelfButtonHost::MOUSE, false);
1016 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
1018 // Dragging changes model order.
1019 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1020 std::rotate(id_map.begin() + 1,
1021 id_map.begin() + 2,
1022 id_map.begin() + 4);
1023 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1025 // Cancelling the drag operation restores previous order.
1026 button_host->PointerReleasedOnButton(
1027 dragged_button, ShelfButtonHost::MOUSE, true);
1028 std::rotate(id_map.begin() + 1,
1029 id_map.begin() + 3,
1030 id_map.begin() + 4);
1031 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1033 // Deleting an item keeps the remaining intact.
1034 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1035 model_->RemoveItemAt(1);
1036 id_map.erase(id_map.begin() + 1);
1037 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1038 button_host->PointerReleasedOnButton(
1039 dragged_button, ShelfButtonHost::MOUSE, false);
1041 // Adding a shelf item cancels the drag and respects the order.
1042 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1043 ShelfID new_id = AddAppShortcut();
1044 id_map.insert(id_map.begin() + 6,
1045 std::make_pair(new_id, GetButtonByID(new_id)));
1046 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1047 button_host->PointerReleasedOnButton(
1048 dragged_button, ShelfButtonHost::MOUSE, false);
1050 // Adding a shelf item at the end (i.e. a panel) canels drag and respects
1051 // the order.
1052 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1053 new_id = AddPanel();
1054 id_map.insert(id_map.begin() + 7,
1055 std::make_pair(new_id, GetButtonByID(new_id)));
1056 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1057 button_host->PointerReleasedOnButton(
1058 dragged_button, ShelfButtonHost::MOUSE, false);
1061 // Check that 2nd drag from the other pointer would be ignored.
1062 TEST_F(ShelfViewTest, SimultaneousDrag) {
1063 ShelfButtonHost* button_host = shelf_view_;
1065 std::vector<std::pair<ShelfID, views::View*> > id_map;
1066 SetupForDragTest(&id_map);
1068 // Start a mouse drag.
1069 views::View* dragged_button_mouse =
1070 SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1071 std::rotate(id_map.begin() + 1,
1072 id_map.begin() + 2,
1073 id_map.begin() + 4);
1074 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1075 // Attempt a touch drag before the mouse drag finishes.
1076 views::View* dragged_button_touch =
1077 SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
1079 // Nothing changes since 2nd drag is ignored.
1080 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1082 // Finish the mouse drag.
1083 button_host->PointerReleasedOnButton(
1084 dragged_button_mouse, ShelfButtonHost::MOUSE, false);
1085 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1087 // Now start a touch drag.
1088 dragged_button_touch = SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
1089 std::rotate(id_map.begin() + 3,
1090 id_map.begin() + 4,
1091 id_map.begin() + 5);
1092 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1094 // And attempt a mouse drag before the touch drag finishes.
1095 dragged_button_mouse = SimulateDrag(ShelfButtonHost::MOUSE, 1, 2);
1097 // Nothing changes since 2nd drag is ignored.
1098 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1100 button_host->PointerReleasedOnButton(
1101 dragged_button_touch, ShelfButtonHost::TOUCH, false);
1102 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1105 // Check that clicking first on one item and then dragging another works as
1106 // expected.
1107 TEST_F(ShelfViewTest, ClickOneDragAnother) {
1108 ShelfButtonHost* button_host = shelf_view_;
1110 std::vector<std::pair<ShelfID, views::View*> > id_map;
1111 SetupForDragTest(&id_map);
1113 // A click on item 1 is simulated.
1114 SimulateClick(1);
1116 // Dragging browser index at 0 should change the model order correctly.
1117 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
1118 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1119 std::rotate(id_map.begin() + 1,
1120 id_map.begin() + 2,
1121 id_map.begin() + 4);
1122 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1123 button_host->PointerReleasedOnButton(
1124 dragged_button, ShelfButtonHost::MOUSE, false);
1125 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
1128 // Tests that double-clicking an item does not activate it twice.
1129 TEST_F(ShelfViewTest, ClickingTwiceActivatesOnce) {
1130 // Watch for selection of the browser shortcut.
1131 ShelfID browser_shelf_id = model_->items()[browser_index_].id;
1132 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1133 item_manager_->SetShelfItemDelegate(
1134 browser_shelf_id,
1135 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1137 // A single click selects the item.
1138 SimulateClick(browser_index_);
1139 EXPECT_TRUE(selection_tracker->WasSelected());
1141 // A double-click does not select the item.
1142 selection_tracker->Reset();
1143 SimulateDoubleClick(browser_index_);
1144 EXPECT_FALSE(selection_tracker->WasSelected());
1147 // Check that clicking an item and jittering the mouse a bit still selects the
1148 // item.
1149 TEST_F(ShelfViewTest, ClickAndMoveSlightly) {
1150 std::vector<std::pair<ShelfID, views::View*> > id_map;
1151 SetupForDragTest(&id_map);
1153 ShelfID shelf_id = (id_map.begin() + 1)->first;
1154 views::View* button = (id_map.begin() + 1)->second;
1156 // Replace the ShelfItemDelegate for |shelf_id| with one which tracks whether
1157 // the shelf item gets selected.
1158 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1159 item_manager_->SetShelfItemDelegate(
1160 shelf_id,
1161 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1163 gfx::Vector2d press_offset(5, 30);
1164 gfx::Point press_location = gfx::Point() + press_offset;
1165 gfx::Point press_location_in_screen =
1166 button->GetBoundsInScreen().origin() + press_offset;
1168 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED, press_location,
1169 press_location_in_screen, ui::EventTimeForNow(),
1170 ui::EF_LEFT_MOUSE_BUTTON, 0);
1171 button->OnMousePressed(click_event);
1173 ui::MouseEvent drag_event1(
1174 ui::ET_MOUSE_DRAGGED, press_location + gfx::Vector2d(0, 1),
1175 press_location_in_screen + gfx::Vector2d(0, 1), ui::EventTimeForNow(),
1176 ui::EF_LEFT_MOUSE_BUTTON, 0);
1177 button->OnMouseDragged(drag_event1);
1179 ui::MouseEvent drag_event2(
1180 ui::ET_MOUSE_DRAGGED, press_location + gfx::Vector2d(-1, 0),
1181 press_location_in_screen + gfx::Vector2d(-1, 0), ui::EventTimeForNow(),
1182 ui::EF_LEFT_MOUSE_BUTTON, 0);
1183 button->OnMouseDragged(drag_event2);
1185 ui::MouseEvent release_event(
1186 ui::ET_MOUSE_RELEASED, press_location + gfx::Vector2d(-1, 0),
1187 press_location_in_screen + gfx::Vector2d(-1, 0), ui::EventTimeForNow(),
1188 ui::EF_LEFT_MOUSE_BUTTON, 0);
1189 button->OnMouseReleased(release_event);
1191 EXPECT_TRUE(selection_tracker->WasSelected());
1194 // Confirm that item status changes are reflected in the buttons.
1195 TEST_F(ShelfViewTest, ShelfItemStatus) {
1196 // All buttons should be visible.
1197 ASSERT_EQ(test_api_->GetButtonCount(),
1198 test_api_->GetLastVisibleIndex() + 1);
1200 // Add platform app button.
1201 ShelfID last_added = AddPlatformApp();
1202 ShelfItem item = GetItemByID(last_added);
1203 int index = model_->ItemIndexByID(last_added);
1204 ShelfButton* button = GetButtonByID(last_added);
1205 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1206 item.status = STATUS_ACTIVE;
1207 model_->Set(index, item);
1208 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1209 item.status = STATUS_ATTENTION;
1210 model_->Set(index, item);
1211 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1214 // Confirm that item status changes are reflected in the buttons
1215 // for platform apps.
1216 TEST_F(ShelfViewTest, ShelfItemStatusPlatformApp) {
1217 // All buttons should be visible.
1218 ASSERT_EQ(test_api_->GetButtonCount(),
1219 test_api_->GetLastVisibleIndex() + 1);
1221 // Add platform app button.
1222 ShelfID last_added = AddPlatformApp();
1223 ShelfItem item = GetItemByID(last_added);
1224 int index = model_->ItemIndexByID(last_added);
1225 ShelfButton* button = GetButtonByID(last_added);
1226 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1227 item.status = STATUS_ACTIVE;
1228 model_->Set(index, item);
1229 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1230 item.status = STATUS_ATTENTION;
1231 model_->Set(index, item);
1232 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1235 // Confirm that shelf item bounds are correctly updated on shelf changes.
1236 TEST_F(ShelfViewTest, ShelfItemBoundsCheck) {
1237 VerifyShelfItemBoundsAreValid();
1238 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1239 SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
1240 test_api_->RunMessageLoopUntilAnimationsDone();
1241 VerifyShelfItemBoundsAreValid();
1242 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1243 SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
1244 test_api_->RunMessageLoopUntilAnimationsDone();
1245 VerifyShelfItemBoundsAreValid();
1248 TEST_F(ShelfViewTest, ShelfTooltipTest) {
1249 ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
1250 test_api_->GetButtonCount());
1252 // Prepare some items to the shelf.
1253 ShelfID app_button_id = AddAppShortcut();
1254 ShelfID platform_button_id = AddPlatformApp();
1256 ShelfButton* app_button = GetButtonByID(app_button_id);
1257 ShelfButton* platform_button = GetButtonByID(platform_button_id);
1259 ShelfButtonHost* button_host = shelf_view_;
1260 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1262 button_host->MouseEnteredButton(app_button);
1263 // There's a delay to show the tooltip, so it's not visible yet.
1264 EXPECT_FALSE(tooltip_manager->IsVisible());
1265 EXPECT_EQ(app_button, GetTooltipAnchorView());
1267 ShowTooltip();
1268 EXPECT_TRUE(tooltip_manager->IsVisible());
1270 // Once it's visible, it keeps visibility and is pointing to the same
1271 // item.
1272 button_host->MouseExitedButton(app_button);
1273 EXPECT_TRUE(tooltip_manager->IsVisible());
1274 EXPECT_EQ(app_button, GetTooltipAnchorView());
1276 // When entered to another item, it switches to the new item. There is no
1277 // delay for the visibility.
1278 button_host->MouseEnteredButton(platform_button);
1279 EXPECT_TRUE(tooltip_manager->IsVisible());
1280 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1282 button_host->MouseExitedButton(platform_button);
1283 tooltip_manager->Close();
1285 // Next time: enter app_button -> move immediately to tab_button.
1286 button_host->MouseEnteredButton(app_button);
1287 button_host->MouseExitedButton(app_button);
1288 button_host->MouseEnteredButton(platform_button);
1289 EXPECT_FALSE(tooltip_manager->IsVisible());
1290 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1293 // Verify a fix for crash caused by a tooltip update for a deletedshelf
1294 // button, see crbug.com/288838.
1295 TEST_F(ShelfViewTest, RemovingItemClosesTooltip) {
1296 ShelfButtonHost* button_host = shelf_view_;
1297 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1299 // Add an item to the shelf.
1300 ShelfID app_button_id = AddAppShortcut();
1301 ShelfButton* app_button = GetButtonByID(app_button_id);
1303 // Spawn a tooltip on that item.
1304 button_host->MouseEnteredButton(app_button);
1305 ShowTooltip();
1306 EXPECT_TRUE(tooltip_manager->IsVisible());
1308 // Remove the app shortcut while the tooltip is open. The tooltip should be
1309 // closed.
1310 RemoveByID(app_button_id);
1311 EXPECT_FALSE(tooltip_manager->IsVisible());
1313 // Change the shelf layout. This should not crash.
1314 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1315 Shell::GetPrimaryRootWindow());
1318 // Changing the shelf alignment closes any open tooltip.
1319 TEST_F(ShelfViewTest, ShelfAlignmentClosesTooltip) {
1320 ShelfButtonHost* button_host = shelf_view_;
1321 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1323 // Add an item to the shelf.
1324 ShelfID app_button_id = AddAppShortcut();
1325 ShelfButton* app_button = GetButtonByID(app_button_id);
1327 // Spawn a tooltip on the item.
1328 button_host->MouseEnteredButton(app_button);
1329 ShowTooltip();
1330 EXPECT_TRUE(tooltip_manager->IsVisible());
1332 // Changing shelf alignment hides the tooltip.
1333 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1334 Shell::GetPrimaryRootWindow());
1335 EXPECT_FALSE(tooltip_manager->IsVisible());
1338 TEST_F(ShelfViewTest, ShouldHideTooltipTest) {
1339 ShelfID app_button_id = AddAppShortcut();
1340 ShelfID platform_button_id = AddPlatformApp();
1342 // The tooltip shouldn't hide if the mouse is on normal buttons.
1343 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1344 ShelfButton* button = test_api_->GetButton(i);
1345 if (!button)
1346 continue;
1348 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1349 button->GetMirroredBounds().CenterPoint()))
1350 << "ShelfView tries to hide on button " << i;
1353 // The tooltip should not hide on the app-list button.
1354 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1355 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1356 app_list_button->GetMirroredBounds().CenterPoint()));
1358 // The tooltip shouldn't hide if the mouse is in the gap between two buttons.
1359 gfx::Rect app_button_rect = GetButtonByID(app_button_id)->GetMirroredBounds();
1360 gfx::Rect platform_button_rect =
1361 GetButtonByID(platform_button_id)->GetMirroredBounds();
1362 ASSERT_FALSE(app_button_rect.Intersects(platform_button_rect));
1363 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1364 gfx::UnionRects(app_button_rect, platform_button_rect).CenterPoint()));
1366 // The tooltip should hide if it's outside of all buttons.
1367 gfx::Rect all_area;
1368 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1369 ShelfButton* button = test_api_->GetButton(i);
1370 if (!button)
1371 continue;
1373 all_area.Union(button->GetMirroredBounds());
1375 all_area.Union(shelf_view_->GetAppListButtonView()->GetMirroredBounds());
1376 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(all_area.origin()));
1377 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1378 gfx::Point(all_area.right() - 1, all_area.bottom() - 1)));
1379 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1380 gfx::Point(all_area.right(), all_area.y())));
1381 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1382 gfx::Point(all_area.x() - 1, all_area.y())));
1383 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1384 gfx::Point(all_area.x(), all_area.y() - 1)));
1385 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1386 gfx::Point(all_area.x(), all_area.bottom())));
1389 TEST_F(ShelfViewTest, ShouldHideTooltipWithAppListWindowTest) {
1390 Shell::GetInstance()->ShowAppList(NULL);
1391 ASSERT_TRUE(Shell::GetInstance()->GetAppListWindow());
1393 // The tooltip shouldn't hide if the mouse is on normal buttons.
1394 for (int i = 1; i < test_api_->GetButtonCount(); i++) {
1395 ShelfButton* button = test_api_->GetButton(i);
1396 if (!button)
1397 continue;
1399 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1400 button->GetMirroredBounds().CenterPoint()))
1401 << "ShelfView tries to hide on button " << i;
1404 // The tooltip should hide on the app-list button.
1405 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1406 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1407 app_list_button->GetMirroredBounds().CenterPoint()));
1410 // Test that by moving the mouse cursor off the button onto the bubble it closes
1411 // the bubble.
1412 TEST_F(ShelfViewTest, ShouldHideTooltipWhenHoveringOnTooltip) {
1413 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1414 tooltip_manager->CreateZeroDelayTimerForTest();
1415 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1417 // Move the mouse off any item and check that no tooltip is shown.
1418 generator.MoveMouseTo(gfx::Point(0, 0));
1419 EXPECT_FALSE(tooltip_manager->IsVisible());
1421 // Move the mouse over the button and check that it is visible.
1422 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1423 gfx::Rect bounds = app_list_button->GetBoundsInScreen();
1424 generator.MoveMouseTo(bounds.CenterPoint());
1425 // Wait for the timer to go off.
1426 RunAllPendingInMessageLoop();
1427 EXPECT_TRUE(tooltip_manager->IsVisible());
1429 // Move the mouse cursor slightly to the right of the item. The tooltip should
1430 // stay open.
1431 generator.MoveMouseBy(bounds.width() / 2 + 5, 0);
1432 // Make sure there is no delayed close.
1433 RunAllPendingInMessageLoop();
1434 EXPECT_TRUE(tooltip_manager->IsVisible());
1436 // Move back - it should still stay open.
1437 generator.MoveMouseBy(-(bounds.width() / 2 + 5), 0);
1438 // Make sure there is no delayed close.
1439 RunAllPendingInMessageLoop();
1440 EXPECT_TRUE(tooltip_manager->IsVisible());
1442 // Now move the mouse cursor slightly above the item - so that it is over the
1443 // tooltip bubble. Now it should disappear.
1444 generator.MoveMouseBy(0, -(bounds.height() / 2 + 5));
1445 // Wait until the delayed close kicked in.
1446 RunAllPendingInMessageLoop();
1447 EXPECT_FALSE(tooltip_manager->IsVisible());
1450 // Resizing shelf view while an add animation without fade-in is running,
1451 // which happens when overflow happens. App list button should end up in its
1452 // new ideal bounds.
1453 TEST_F(ShelfViewTest, ResizeDuringOverflowAddAnimation) {
1454 // All buttons should be visible.
1455 ASSERT_EQ(test_api_->GetButtonCount(),
1456 test_api_->GetLastVisibleIndex() + 1);
1458 // Add buttons until overflow. Let the non-overflow add animations finish but
1459 // leave the last running.
1460 int items_added = 0;
1461 AddPlatformAppNoWait();
1462 while (!test_api_->IsOverflowButtonVisible()) {
1463 test_api_->RunMessageLoopUntilAnimationsDone();
1464 AddPlatformAppNoWait();
1465 ++items_added;
1466 ASSERT_LT(items_added, 10000);
1469 // Resize shelf view with that animation running and stay overflown.
1470 gfx::Rect bounds = shelf_view_->bounds();
1471 bounds.set_width(bounds.width() - kShelfSize);
1472 shelf_view_->SetBoundsRect(bounds);
1473 ASSERT_TRUE(test_api_->IsOverflowButtonVisible());
1475 // Finish the animation.
1476 test_api_->RunMessageLoopUntilAnimationsDone();
1478 // App list button should ends up in its new ideal bounds.
1479 const int app_list_button_index = test_api_->GetButtonCount() - 1;
1480 const gfx::Rect& app_list_ideal_bounds =
1481 test_api_->GetIdealBoundsByIndex(app_list_button_index);
1482 const gfx::Rect& app_list_bounds =
1483 test_api_->GetBoundsByIndex(app_list_button_index);
1484 EXPECT_EQ(app_list_ideal_bounds, app_list_bounds);
1487 // Checks the overflow bubble size when an item is ripped off and re-inserted.
1488 TEST_F(ShelfViewTest, OverflowBubbleSize) {
1489 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1490 ReplaceShelfDelegateForRipOffTest();
1492 AddButtonsUntilOverflow();
1493 // Add one more button to prevent the overflow bubble to disappear upon
1494 // dragging an item out on windows (flakiness, see crbug.com/436131).
1495 AddAppShortcut();
1497 // Show overflow bubble.
1498 test_api_->ShowOverflowBubble();
1499 ASSERT_TRUE(test_api_->overflow_bubble() &&
1500 test_api_->overflow_bubble()->IsShowing());
1502 ShelfViewTestAPI test_for_overflow_view(
1503 test_api_->overflow_bubble()->shelf_view());
1505 int ripped_index = test_for_overflow_view.GetLastVisibleIndex();
1506 gfx::Size bubble_size = test_for_overflow_view.GetPreferredSize();
1507 int item_width = test_for_overflow_view.GetButtonSize() +
1508 test_for_overflow_view.GetButtonSpacing();
1510 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
1511 gfx::Point());
1512 ShelfButton* button = test_for_overflow_view.GetButton(ripped_index);
1513 // Rip off the last visible item.
1514 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1515 gfx::Point rip_off_point(start_point.x(), 0);
1516 generator.MoveMouseTo(start_point.x(), start_point.y());
1517 base::MessageLoop::current()->RunUntilIdle();
1518 generator.PressLeftButton();
1519 base::MessageLoop::current()->RunUntilIdle();
1520 generator.MoveMouseTo(rip_off_point.x(), rip_off_point.y());
1521 base::MessageLoop::current()->RunUntilIdle();
1522 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1524 // Check the overflow bubble size when an item is ripped off.
1525 EXPECT_EQ(bubble_size.width() - item_width,
1526 test_for_overflow_view.GetPreferredSize().width());
1527 ASSERT_TRUE(test_api_->overflow_bubble() &&
1528 test_api_->overflow_bubble()->IsShowing());
1530 // Re-insert an item into the overflow bubble.
1531 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1532 button = test_for_overflow_view.GetButton(first_index);
1534 // Check the bubble size after an item is re-inserted.
1535 generator.MoveMouseTo(button->GetBoundsInScreen().CenterPoint());
1536 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1537 EXPECT_EQ(bubble_size.width(),
1538 test_for_overflow_view.GetPreferredSize().width());
1540 generator.ReleaseLeftButton();
1541 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1542 EXPECT_EQ(bubble_size.width(),
1543 test_for_overflow_view.GetPreferredSize().width());
1546 // Check the drag insertion bounds of scrolled overflow bubble.
1547 TEST_F(ShelfViewTest, CheckDragInsertBoundsOfScrolledOverflowBubble) {
1548 UpdateDisplay("400x300");
1550 EXPECT_EQ(2, model_->item_count());
1552 AddButtonsUntilOverflow();
1554 // Show overflow bubble.
1555 test_api_->ShowOverflowBubble();
1556 ASSERT_TRUE(test_api_->overflow_bubble() &&
1557 test_api_->overflow_bubble()->IsShowing());
1559 int item_width = test_api_->GetButtonSize() +
1560 test_api_->GetButtonSpacing();
1561 OverflowBubbleView* bubble_view = test_api_->overflow_bubble()->bubble_view();
1562 test::OverflowBubbleViewTestAPI bubble_view_api(bubble_view);
1564 // Add more buttons until OverflowBubble is scrollable and it has 3 invisible
1565 // items.
1566 while (bubble_view_api.GetContentsSize().width() <
1567 (bubble_view->GetContentsBounds().width() + 3 * item_width))
1568 AddAppShortcut();
1570 ASSERT_TRUE(test_api_->overflow_bubble() &&
1571 test_api_->overflow_bubble()->IsShowing());
1573 ShelfViewTestAPI test_for_overflow_view(
1574 test_api_->overflow_bubble()->shelf_view());
1575 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1576 int last_index = test_for_overflow_view.GetLastVisibleIndex();
1578 ShelfButton* first_button = test_for_overflow_view.GetButton(first_index);
1579 ShelfButton* last_button = test_for_overflow_view.GetButton(last_index);
1580 gfx::Point first_point = first_button->GetBoundsInScreen().CenterPoint();
1581 gfx::Point last_point = last_button->GetBoundsInScreen().CenterPoint();
1582 gfx::Rect drag_reinsert_bounds =
1583 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1584 EXPECT_TRUE(drag_reinsert_bounds.Contains(first_point));
1585 EXPECT_FALSE(drag_reinsert_bounds.Contains(last_point));
1587 // Scrolls sufficiently to show last item.
1588 bubble_view_api.ScrollByXOffset(3 * item_width);
1589 drag_reinsert_bounds =
1590 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1591 first_point = first_button->GetBoundsInScreen().CenterPoint();
1592 last_point = last_button->GetBoundsInScreen().CenterPoint();
1593 EXPECT_FALSE(drag_reinsert_bounds.Contains(first_point));
1594 EXPECT_TRUE(drag_reinsert_bounds.Contains(last_point));
1597 // Check the drag insertion bounds of shelf view in multi monitor environment.
1598 TEST_F(ShelfViewTest, CheckDragInsertBoundsWithMultiMonitor) {
1599 // win8-aura doesn't support multiple display.
1600 if (!SupportsMultipleDisplays())
1601 return;
1603 UpdateDisplay("800x600,800x600");
1604 Shelf* secondary_shelf = Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
1605 ShelfView* shelf_view_for_secondary =
1606 ShelfTestAPI(secondary_shelf).shelf_view();
1608 // The bounds should be big enough for 4 buttons + overflow chevron.
1609 shelf_view_for_secondary->SetBounds(0, 0, 500, kShelfSize);
1611 ShelfViewTestAPI test_api_for_secondary(shelf_view_for_secondary);
1612 // Speeds up animation for test.
1613 test_api_for_secondary.SetAnimationDuration(1);
1615 AddButtonsUntilOverflow();
1617 // Test #1: Test drag insertion bounds of primary shelf.
1618 // Show overflow bubble.
1619 test_api_->ShowOverflowBubble();
1620 ASSERT_TRUE(test_api_->overflow_bubble() &&
1621 test_api_->overflow_bubble()->IsShowing());
1623 ShelfViewTestAPI test_api_for_overflow_view(
1624 test_api_->overflow_bubble()->shelf_view());
1626 ShelfButton* button = test_api_for_overflow_view.GetButton(
1627 test_api_for_overflow_view.GetLastVisibleIndex());
1629 // Checks that a point in shelf is contained in drag insert bounds.
1630 gfx::Point point_in_shelf_view = button->GetBoundsInScreen().CenterPoint();
1631 gfx::Rect drag_reinsert_bounds =
1632 test_api_for_overflow_view.GetBoundsForDragInsertInScreen();
1633 EXPECT_TRUE(drag_reinsert_bounds.Contains(point_in_shelf_view));
1634 // Checks that a point out of shelf is not contained in drag insert bounds.
1635 EXPECT_FALSE(drag_reinsert_bounds.Contains(
1636 gfx::Point(point_in_shelf_view.x(), 0)));
1638 // Test #2: Test drag insertion bounds of secondary shelf.
1639 // Show overflow bubble.
1640 test_api_for_secondary.ShowOverflowBubble();
1641 ASSERT_TRUE(test_api_for_secondary.overflow_bubble() &&
1642 test_api_for_secondary.overflow_bubble()->IsShowing());
1644 ShelfViewTestAPI test_api_for_overflow_view_of_secondary(
1645 test_api_for_secondary.overflow_bubble()->shelf_view());
1647 ShelfButton* button_in_secondary =
1648 test_api_for_overflow_view_of_secondary.GetButton(
1649 test_api_for_overflow_view_of_secondary.GetLastVisibleIndex());
1651 // Checks that a point in shelf is contained in drag insert bounds.
1652 gfx::Point point_in_secondary_shelf_view =
1653 button_in_secondary->GetBoundsInScreen().CenterPoint();
1654 gfx::Rect drag_reinsert_bounds_in_secondary =
1655 test_api_for_overflow_view_of_secondary.GetBoundsForDragInsertInScreen();
1656 EXPECT_TRUE(drag_reinsert_bounds_in_secondary.Contains(
1657 point_in_secondary_shelf_view));
1658 // Checks that a point out of shelf is not contained in drag insert bounds.
1659 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(
1660 gfx::Point(point_in_secondary_shelf_view.x(), 0)));
1661 // Checks that a point of overflow bubble in primary shelf should not be
1662 // contained by insert bounds of secondary shelf.
1663 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(point_in_shelf_view));
1666 // Checks the rip an item off from left aligned shelf in secondary monitor.
1667 TEST_F(ShelfViewTest, CheckRipOffFromLeftShelfAlignmentWithMultiMonitor) {
1668 // win8-aura doesn't support multiple display.
1669 if (!SupportsMultipleDisplays())
1670 return;
1672 UpdateDisplay("800x600,800x600");
1673 ASSERT_EQ(2U, Shell::GetAllRootWindows().size());
1675 aura::Window* second_root = Shell::GetAllRootWindows()[1];
1677 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT, second_root);
1678 ASSERT_EQ(SHELF_ALIGNMENT_LEFT,
1679 Shell::GetInstance()->GetShelfAlignment(second_root));
1681 // Initially, app list and browser shortcut are added.
1682 EXPECT_EQ(2, model_->item_count());
1683 int browser_index = model_->GetItemIndexForType(TYPE_BROWSER_SHORTCUT);
1684 EXPECT_GT(browser_index, 0);
1686 Shelf* secondary_shelf = Shelf::ForWindow(second_root);
1687 ShelfView* shelf_view_for_secondary =
1688 ShelfTestAPI(secondary_shelf).shelf_view();
1690 ShelfViewTestAPI test_api_for_secondary_shelf_view(shelf_view_for_secondary);
1691 ShelfButton* button =
1692 test_api_for_secondary_shelf_view.GetButton(browser_index);
1694 // Fetch the start point of dragging.
1695 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1696 ::wm::ConvertPointFromScreen(second_root, &start_point);
1698 ui::test::EventGenerator generator(second_root, start_point);
1700 // Rip off the browser item.
1701 generator.PressLeftButton();
1702 generator.MoveMouseTo(start_point.x() + 400, start_point.y());
1703 test_api_for_secondary_shelf_view.RunMessageLoopUntilAnimationsDone();
1704 EXPECT_TRUE(test_api_for_secondary_shelf_view.IsRippedOffFromShelf());
1707 // Checks various drag and drop operations from OverflowBubble to Shelf.
1708 TEST_F(ShelfViewTest, CheckDragAndDropFromOverflowBubbleToShelf) {
1709 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1710 ReplaceShelfDelegateForRipOffTest();
1712 AddButtonsUntilOverflow();
1713 // Add one more button to prevent the overflow bubble to disappear upon
1714 // dragging an item out on windows (flakiness, see crbug.com/425097).
1715 AddAppShortcut();
1717 TestDraggingAnItemFromOverflowToShelf(false);
1718 TestDraggingAnItemFromOverflowToShelf(true);
1721 // Tests that the AppListButton renders as active in response to touches.
1722 TEST_F(ShelfViewTest, AppListButtonTouchFeedback) {
1723 AppListButton* app_list_button =
1724 static_cast<AppListButton*>(shelf_view_->GetAppListButtonView());
1725 EXPECT_FALSE(app_list_button->draw_background_as_active());
1727 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1728 generator.set_current_location(app_list_button->
1729 GetBoundsInScreen().CenterPoint());
1730 generator.PressTouch();
1731 EXPECT_TRUE(app_list_button->draw_background_as_active());
1733 generator.ReleaseTouch();
1734 EXPECT_FALSE(app_list_button->draw_background_as_active());
1735 EXPECT_TRUE(Shell::GetInstance()->GetAppListTargetVisibility());
1738 // Tests that a touch that slides out of the bounds of the AppListButton leads
1739 // to the end of rendering an active state.
1740 TEST_F(ShelfViewTest, AppListButtonTouchFeedbackCancellation) {
1741 AppListButton* app_list_button =
1742 static_cast<AppListButton*>(shelf_view_->GetAppListButtonView());
1743 EXPECT_FALSE(app_list_button->draw_background_as_active());
1745 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1746 generator.set_current_location(app_list_button->
1747 GetBoundsInScreen().CenterPoint());
1748 generator.PressTouch();
1749 EXPECT_TRUE(app_list_button->draw_background_as_active());
1751 gfx::Point moved_point(app_list_button->GetBoundsInScreen().right() + 1,
1752 app_list_button->
1753 GetBoundsInScreen().CenterPoint().y());
1754 generator.MoveTouch(moved_point);
1755 EXPECT_FALSE(app_list_button->draw_background_as_active());
1757 generator.set_current_location(moved_point);
1758 generator.ReleaseTouch();
1759 EXPECT_FALSE(app_list_button->draw_background_as_active());
1760 EXPECT_FALSE(Shell::GetInstance()->GetAppListTargetVisibility());
1763 class ShelfViewVisibleBoundsTest : public ShelfViewTest,
1764 public testing::WithParamInterface<bool> {
1765 public:
1766 ShelfViewVisibleBoundsTest() : text_direction_change_(GetParam()) {}
1768 void CheckAllItemsAreInBounds() {
1769 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1770 gfx::Rect shelf_bounds = shelf_view_->GetBoundsInScreen();
1771 EXPECT_TRUE(shelf_bounds.Contains(visible_bounds));
1772 for (int i = 0; i < test_api_->GetButtonCount(); ++i)
1773 if (ShelfButton* button = test_api_->GetButton(i))
1774 EXPECT_TRUE(visible_bounds.Contains(button->GetBoundsInScreen()));
1775 CheckAppListButtonIsInBounds();
1778 void CheckAppListButtonIsInBounds() {
1779 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1780 gfx::Rect app_list_button_bounds = shelf_view_->GetAppListButtonView()->
1781 GetBoundsInScreen();
1782 EXPECT_TRUE(visible_bounds.Contains(app_list_button_bounds));
1785 private:
1786 ScopedTextDirectionChange text_direction_change_;
1788 DISALLOW_COPY_AND_ASSIGN(ShelfViewVisibleBoundsTest);
1791 TEST_P(ShelfViewVisibleBoundsTest, ItemsAreInBounds) {
1792 // Adding elements leaving some empty space.
1793 for (int i = 0; i < 3; i++) {
1794 AddAppShortcut();
1796 test_api_->RunMessageLoopUntilAnimationsDone();
1797 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
1798 CheckAllItemsAreInBounds();
1799 // Same for overflow case.
1800 while (!test_api_->IsOverflowButtonVisible()) {
1801 AddAppShortcut();
1803 test_api_->RunMessageLoopUntilAnimationsDone();
1804 CheckAllItemsAreInBounds();
1807 INSTANTIATE_TEST_CASE_P(LtrRtl, ShelfViewTextDirectionTest, testing::Bool());
1808 INSTANTIATE_TEST_CASE_P(VisibleBounds, ShelfViewVisibleBoundsTest,
1809 testing::Bool());
1811 } // namespace test
1812 } // namespace ash