Enable Touch Feedback Experiment by default
[chromium-blink-merge.git] / ash / shelf / shelf_view_unittest.cc
blob902cf517558526702f41e2b25b8c0defbc3b02b5
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/test/event_generator.h"
44 #include "ui/views/view_model.h"
45 #include "ui/views/widget/widget.h"
46 #include "ui/views/widget/widget_delegate.h"
47 #include "ui/wm/core/coordinate_conversion.h"
49 namespace ash {
50 namespace test {
52 ////////////////////////////////////////////////////////////////////////////////
53 // ShelfIconObserver tests.
55 class TestShelfIconObserver : public ShelfIconObserver {
56 public:
57 explicit TestShelfIconObserver(Shelf* shelf)
58 : shelf_(shelf),
59 change_notified_(false) {
60 if (shelf_)
61 shelf_->AddIconObserver(this);
64 ~TestShelfIconObserver() override {
65 if (shelf_)
66 shelf_->RemoveIconObserver(this);
69 // ShelfIconObserver implementation.
70 void OnShelfIconPositionsChanged() override { change_notified_ = true; }
72 int change_notified() const { return change_notified_; }
73 void Reset() { change_notified_ = false; }
75 private:
76 Shelf* shelf_;
77 bool change_notified_;
79 DISALLOW_COPY_AND_ASSIGN(TestShelfIconObserver);
82 class ShelfViewIconObserverTest : public AshTestBase {
83 public:
84 ShelfViewIconObserverTest() {}
85 ~ShelfViewIconObserverTest() override {}
87 void SetUp() override {
88 AshTestBase::SetUp();
89 Shelf* shelf = Shelf::ForPrimaryDisplay();
90 observer_.reset(new TestShelfIconObserver(shelf));
92 shelf_view_test_.reset(
93 new ShelfViewTestAPI(ShelfTestAPI(shelf).shelf_view()));
94 shelf_view_test_->SetAnimationDuration(1);
97 void TearDown() override {
98 observer_.reset();
99 AshTestBase::TearDown();
102 TestShelfIconObserver* observer() { return observer_.get(); }
104 ShelfViewTestAPI* shelf_view_test() {
105 return shelf_view_test_.get();
108 Shelf* ShelfForSecondaryDisplay() {
109 return Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
112 private:
113 scoped_ptr<TestShelfIconObserver> observer_;
114 scoped_ptr<ShelfViewTestAPI> shelf_view_test_;
116 DISALLOW_COPY_AND_ASSIGN(ShelfViewIconObserverTest);
119 // TestShelfItemDelegate which tracks whether it gets selected.
120 class ShelfItemSelectionTracker : public TestShelfItemDelegate {
121 public:
122 ShelfItemSelectionTracker() : TestShelfItemDelegate(NULL), selected_(false) {
125 ~ShelfItemSelectionTracker() override {}
127 // Resets to the initial state.
128 void Reset() { selected_ = false; }
130 // Returns true if the delegate was selected.
131 bool WasSelected() {
132 return selected_;
135 // TestShelfItemDelegate:
136 bool ItemSelected(const ui::Event& event) override {
137 selected_ = true;
138 return false;
141 private:
142 bool selected_;
144 DISALLOW_COPY_AND_ASSIGN(ShelfItemSelectionTracker);
147 TEST_F(ShelfViewIconObserverTest, AddRemove) {
148 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
149 ASSERT_TRUE(shelf_delegate);
151 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
152 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
153 params.bounds = gfx::Rect(0, 0, 200, 200);
154 params.context = CurrentContext();
156 scoped_ptr<views::Widget> widget(new views::Widget());
157 widget->Init(params);
158 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
159 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
160 EXPECT_TRUE(observer()->change_notified());
161 observer()->Reset();
163 widget->Show();
164 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
165 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
166 EXPECT_TRUE(observer()->change_notified());
167 observer()->Reset();
170 // Sometimes fails on trybots on win7_aura. http://crbug.com/177135
171 #if defined(OS_WIN)
172 #define MAYBE_AddRemoveWithMultipleDisplays \
173 DISABLED_AddRemoveWithMultipleDisplays
174 #else
175 #define MAYBE_AddRemoveWithMultipleDisplays \
176 AddRemoveWithMultipleDisplays
177 #endif
178 // Make sure creating/deleting an window on one displays notifies a
179 // shelf on external display as well as one on primary.
180 TEST_F(ShelfViewIconObserverTest, MAYBE_AddRemoveWithMultipleDisplays) {
181 UpdateDisplay("400x400,400x400");
182 TestShelfIconObserver second_observer(ShelfForSecondaryDisplay());
184 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
185 ASSERT_TRUE(shelf_delegate);
187 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
188 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
189 params.bounds = gfx::Rect(0, 0, 200, 200);
190 params.context = CurrentContext();
192 scoped_ptr<views::Widget> widget(new views::Widget());
193 widget->Init(params);
194 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
195 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
196 EXPECT_TRUE(observer()->change_notified());
197 EXPECT_TRUE(second_observer.change_notified());
198 observer()->Reset();
199 second_observer.Reset();
201 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
202 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
203 EXPECT_TRUE(observer()->change_notified());
204 EXPECT_TRUE(second_observer.change_notified());
206 observer()->Reset();
207 second_observer.Reset();
210 TEST_F(ShelfViewIconObserverTest, BoundsChanged) {
211 ShelfWidget* widget = Shell::GetPrimaryRootWindowController()->shelf();
212 Shelf* shelf = Shelf::ForPrimaryDisplay();
213 gfx::Size shelf_size = widget->GetWindowBoundsInScreen().size();
214 shelf_size.set_width(shelf_size.width() / 2);
215 ASSERT_GT(shelf_size.width(), 0);
216 shelf->SetShelfViewBounds(gfx::Rect(shelf_size));
217 // No animation happens for ShelfView bounds change.
218 EXPECT_TRUE(observer()->change_notified());
219 observer()->Reset();
222 ////////////////////////////////////////////////////////////////////////////////
223 // ShelfView tests.
225 // Simple ShelfDelegate implmentation for ShelfViewTest.OverflowBubbleSize
226 // and CheckDragAndDropFromOverflowBubbleToShelf
227 class TestShelfDelegateForShelfView : public ShelfDelegate {
228 public:
229 explicit TestShelfDelegateForShelfView(ShelfModel* model)
230 : model_(model) {}
231 ~TestShelfDelegateForShelfView() override {}
233 // ShelfDelegate overrides:
234 void OnShelfCreated(Shelf* shelf) override {}
236 void OnShelfDestroyed(Shelf* shelf) override {}
238 ShelfID GetShelfIDForAppID(const std::string& app_id) override {
239 ShelfID id = 0;
240 EXPECT_TRUE(base::StringToInt(app_id, &id));
241 return id;
244 const std::string& GetAppIDForShelfID(ShelfID id) override {
245 // Use |app_id_| member variable because returning a reference to local
246 // variable is not allowed.
247 app_id_ = base::IntToString(id);
248 return app_id_;
251 void PinAppWithID(const std::string& app_id) override {}
253 bool IsAppPinned(const std::string& app_id) override {
254 // Returns true for ShelfViewTest.OverflowBubbleSize. To test ripping off in
255 // that test, an item is already pinned state.
256 return true;
259 bool CanPin() const override { return true; }
261 void UnpinAppWithID(const std::string& app_id) override {
262 ShelfID id = 0;
263 EXPECT_TRUE(base::StringToInt(app_id, &id));
264 ASSERT_GT(id, 0);
265 int index = model_->ItemIndexByID(id);
266 ASSERT_GE(index, 0);
268 model_->RemoveItemAt(index);
271 private:
272 ShelfModel* model_;
274 // Temp member variable for returning a value. See the comment in the
275 // GetAppIDForShelfID().
276 std::string app_id_;
278 DISALLOW_COPY_AND_ASSIGN(TestShelfDelegateForShelfView);
281 class ShelfViewTest : public AshTestBase {
282 public:
283 ShelfViewTest()
284 : model_(NULL),
285 shelf_view_(NULL),
286 browser_index_(1),
287 item_manager_(NULL) {}
288 ~ShelfViewTest() override {}
290 void SetUp() override {
291 AshTestBase::SetUp();
292 test::ShellTestApi test_api(Shell::GetInstance());
293 model_ = test_api.shelf_model();
294 Shelf* shelf = Shelf::ForPrimaryDisplay();
295 shelf_view_ = ShelfTestAPI(shelf).shelf_view();
297 // The bounds should be big enough for 4 buttons + overflow chevron.
298 shelf_view_->SetBounds(0, 0, 500, kShelfSize);
300 test_api_.reset(new ShelfViewTestAPI(shelf_view_));
301 test_api_->SetAnimationDuration(1); // Speeds up animation for test.
303 item_manager_ = Shell::GetInstance()->shelf_item_delegate_manager();
304 DCHECK(item_manager_);
306 // Add browser shortcut shelf item at index 0 for test.
307 AddBrowserShortcut();
310 void TearDown() override {
311 test_api_.reset();
312 AshTestBase::TearDown();
315 protected:
316 void CreateAndSetShelfItemDelegateForID(ShelfID id) {
317 scoped_ptr<ShelfItemDelegate> delegate(new TestShelfItemDelegate(NULL));
318 item_manager_->SetShelfItemDelegate(id, delegate.Pass());
321 ShelfID AddBrowserShortcut() {
322 ShelfItem browser_shortcut;
323 browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
325 ShelfID id = model_->next_id();
326 model_->AddAt(browser_index_, browser_shortcut);
327 CreateAndSetShelfItemDelegateForID(id);
328 test_api_->RunMessageLoopUntilAnimationsDone();
329 return id;
332 ShelfID AddAppShortcut() {
333 ShelfItem item;
334 item.type = TYPE_APP_SHORTCUT;
335 item.status = STATUS_CLOSED;
337 ShelfID id = model_->next_id();
338 model_->Add(item);
339 CreateAndSetShelfItemDelegateForID(id);
340 test_api_->RunMessageLoopUntilAnimationsDone();
341 return id;
344 ShelfID AddPanel() {
345 ShelfID id = AddPanelNoWait();
346 test_api_->RunMessageLoopUntilAnimationsDone();
347 return id;
350 ShelfID AddPlatformAppNoWait() {
351 ShelfItem item;
352 item.type = TYPE_PLATFORM_APP;
353 item.status = STATUS_RUNNING;
355 ShelfID id = model_->next_id();
356 model_->Add(item);
357 CreateAndSetShelfItemDelegateForID(id);
358 return id;
361 ShelfID AddPanelNoWait() {
362 ShelfItem item;
363 item.type = TYPE_APP_PANEL;
364 item.status = STATUS_RUNNING;
366 ShelfID id = model_->next_id();
367 model_->Add(item);
368 CreateAndSetShelfItemDelegateForID(id);
369 return id;
372 ShelfID AddPlatformApp() {
373 ShelfID id = AddPlatformAppNoWait();
374 test_api_->RunMessageLoopUntilAnimationsDone();
375 return id;
378 void RemoveByID(ShelfID id) {
379 model_->RemoveItemAt(model_->ItemIndexByID(id));
380 test_api_->RunMessageLoopUntilAnimationsDone();
383 ShelfButton* GetButtonByID(ShelfID id) {
384 int index = model_->ItemIndexByID(id);
385 return test_api_->GetButton(index);
388 ShelfItem GetItemByID(ShelfID id) {
389 ShelfItems::const_iterator items = model_->ItemByID(id);
390 return *items;
393 void CheckModelIDs(
394 const std::vector<std::pair<ShelfID, views::View*> >& id_map) {
395 size_t map_index = 0;
396 for (size_t model_index = 0;
397 model_index < model_->items().size();
398 ++model_index) {
399 ShelfItem item = model_->items()[model_index];
400 ShelfID id = item.id;
401 EXPECT_EQ(id_map[map_index].first, id);
402 EXPECT_EQ(id_map[map_index].second, GetButtonByID(id));
403 ++map_index;
405 ASSERT_EQ(map_index, id_map.size());
408 void VerifyShelfItemBoundsAreValid() {
409 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
410 if (test_api_->GetButton(i)) {
411 gfx::Rect shelf_view_bounds = shelf_view_->GetLocalBounds();
412 gfx::Rect item_bounds = test_api_->GetBoundsByIndex(i);
413 EXPECT_GE(item_bounds.x(), 0);
414 EXPECT_GE(item_bounds.y(), 0);
415 EXPECT_LE(item_bounds.right(), shelf_view_bounds.width());
416 EXPECT_LE(item_bounds.bottom(), shelf_view_bounds.height());
421 ShelfButton* SimulateButtonPressed(ShelfButtonHost::Pointer pointer,
422 int button_index) {
423 ShelfButtonHost* button_host = shelf_view_;
424 ShelfButton* button = test_api_->GetButton(button_index);
425 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED,
426 gfx::Point(),
427 button->GetBoundsInScreen().origin(), 0, 0);
428 button_host->PointerPressedOnButton(button, pointer, click_event);
429 return button;
432 // Simulates a single mouse click.
433 void SimulateClick(int button_index) {
434 ShelfButtonHost* button_host = shelf_view_;
435 ShelfButton* button =
436 SimulateButtonPressed(ShelfButtonHost::MOUSE, button_index);
437 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
438 gfx::Point(),
439 button->GetBoundsInScreen().origin(),
442 test_api_->ButtonPressed(button, release_event);
443 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false);
446 // Simulates the second click of a double click.
447 void SimulateDoubleClick(int button_index) {
448 ShelfButtonHost* button_host = shelf_view_;
449 ShelfButton* button =
450 SimulateButtonPressed(ShelfButtonHost::MOUSE, button_index);
451 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
452 gfx::Point(),
453 button->GetBoundsInScreen().origin(),
454 ui::EF_IS_DOUBLE_CLICK,
456 test_api_->ButtonPressed(button, release_event);
457 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false);
460 views::View* SimulateDrag(ShelfButtonHost::Pointer pointer,
461 int button_index,
462 int destination_index) {
463 ShelfButtonHost* button_host = shelf_view_;
464 views::View* button = SimulateButtonPressed(pointer, button_index);
466 // Drag.
467 views::View* destination = test_api_->GetButton(destination_index);
468 ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED,
469 gfx::Point(destination->x() - button->x(),
470 destination->y() - button->y()),
471 destination->GetBoundsInScreen().origin(), 0, 0);
472 button_host->PointerDraggedOnButton(button, pointer, drag_event);
473 return button;
476 void SetupForDragTest(
477 std::vector<std::pair<ShelfID, views::View*> >* id_map) {
478 // Initialize |id_map| with the automatically-created shelf buttons.
479 for (size_t i = 0; i < model_->items().size(); ++i) {
480 ShelfButton* button = test_api_->GetButton(i);
481 id_map->push_back(std::make_pair(model_->items()[i].id, button));
483 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
485 // Add 5 app shelf buttons for testing.
486 for (int i = 0; i < 5; ++i) {
487 ShelfID id = AddAppShortcut();
488 // App Icon is located at index 0, and browser shortcut is located at
489 // index 1. So we should start to add app shortcut at index 2.
490 id_map->insert(id_map->begin() + (i + browser_index_ + 1),
491 std::make_pair(id, GetButtonByID(id)));
493 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
496 views::View* GetTooltipAnchorView() {
497 return shelf_view_->tooltip_manager()->anchor_;
500 void AddButtonsUntilOverflow() {
501 int items_added = 0;
502 while (!test_api_->IsOverflowButtonVisible()) {
503 AddAppShortcut();
504 ++items_added;
505 ASSERT_LT(items_added, 10000);
509 void ShowTooltip() {
510 shelf_view_->tooltip_manager()->ShowInternal();
513 void TestDraggingAnItemFromOverflowToShelf(bool cancel) {
514 test_api_->ShowOverflowBubble();
515 ASSERT_TRUE(test_api_->overflow_bubble() &&
516 test_api_->overflow_bubble()->IsShowing());
518 ash::test::ShelfViewTestAPI test_api_for_overflow(
519 test_api_->overflow_bubble()->shelf_view());
521 int total_item_count = model_->item_count();
523 int last_visible_item_id_in_shelf =
524 GetItemId(test_api_->GetLastVisibleIndex());
525 int second_last_visible_item_id_in_shelf =
526 GetItemId(test_api_->GetLastVisibleIndex() - 1);
527 int first_visible_item_id_in_overflow =
528 GetItemId(test_api_for_overflow.GetFirstVisibleIndex());
529 int second_last_visible_item_id_in_overflow =
530 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1);
532 int drag_item_index =
533 test_api_for_overflow.GetLastVisibleIndex();
534 ShelfID drag_item_id = GetItemId(drag_item_index);
535 ShelfButton* drag_button = test_api_for_overflow.GetButton(drag_item_index);
536 gfx::Point center_point_of_drag_item =
537 drag_button->GetBoundsInScreen().CenterPoint();
539 ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow(),
540 center_point_of_drag_item);
541 // Rip an item off to OverflowBubble.
542 generator.PressLeftButton();
543 gfx::Point rip_off_point(center_point_of_drag_item.x(), 0);
544 generator.MoveMouseTo(rip_off_point);
545 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
546 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
547 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
549 // Move a dragged item into Shelf at |drop_index|.
550 int drop_index = 1;
551 gfx::Point drop_point =
552 test_api_->GetButton(drop_index)->GetBoundsInScreen().CenterPoint();
553 int item_width = test_api_for_overflow.GetButtonSize();
554 // To insert at |drop_index|, more smaller x-axis value of |drop_point|
555 // should be used.
556 gfx::Point modified_drop_point(drop_point.x() - item_width / 4,
557 drop_point.y());
558 generator.MoveMouseTo(modified_drop_point);
559 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
560 test_api_->RunMessageLoopUntilAnimationsDone();
561 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
562 ASSERT_TRUE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
564 if (cancel)
565 drag_button->OnMouseCaptureLost();
566 else
567 generator.ReleaseLeftButton();
569 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
570 test_api_->RunMessageLoopUntilAnimationsDone();
571 ASSERT_FALSE(test_api_for_overflow.IsRippedOffFromShelf());
572 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
574 // Compare pre-stored items' id with newly positioned items' after dragging
575 // is canceled or finished.
576 if (cancel) {
577 EXPECT_EQ(last_visible_item_id_in_shelf,
578 GetItemId(test_api_->GetLastVisibleIndex()));
579 EXPECT_EQ(second_last_visible_item_id_in_shelf,
580 GetItemId(test_api_->GetLastVisibleIndex() - 1));
581 EXPECT_EQ(first_visible_item_id_in_overflow,
582 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
583 EXPECT_EQ(second_last_visible_item_id_in_overflow,
584 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1));
585 } else {
586 EXPECT_EQ(drag_item_id, GetItemId(drop_index));
587 EXPECT_EQ(total_item_count, model_->item_count());
588 EXPECT_EQ(last_visible_item_id_in_shelf,
589 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
590 EXPECT_EQ(second_last_visible_item_id_in_shelf,
591 GetItemId(test_api_->GetLastVisibleIndex()));
592 EXPECT_EQ(first_visible_item_id_in_overflow,
593 GetItemId(test_api_for_overflow.GetFirstVisibleIndex() + 1));
594 EXPECT_EQ(second_last_visible_item_id_in_overflow,
595 GetItemId(test_api_for_overflow.GetLastVisibleIndex()));
599 // Returns the item's ShelfID at |index|.
600 ShelfID GetItemId(int index) {
601 DCHECK_GE(index, 0);
602 return model_->items()[index].id;
605 void ReplaceShelfDelegateForRipOffTest() {
606 // Replace ShelfDelegate.
607 test::ShellTestApi test_api(Shell::GetInstance());
608 test_api.SetShelfDelegate(NULL);
609 ShelfDelegate* delegate = new TestShelfDelegateForShelfView(model_);
610 test_api.SetShelfDelegate(delegate);
611 test::ShelfTestAPI(Shelf::ForPrimaryDisplay()).SetShelfDelegate(delegate);
612 test_api_->SetShelfDelegate(delegate);
615 ShelfModel* model_;
616 ShelfView* shelf_view_;
617 int browser_index_;
618 ShelfItemDelegateManager* item_manager_;
620 scoped_ptr<ShelfViewTestAPI> test_api_;
622 private:
623 DISALLOW_COPY_AND_ASSIGN(ShelfViewTest);
626 class ScopedTextDirectionChange {
627 public:
628 explicit ScopedTextDirectionChange(bool is_rtl) : is_rtl_(is_rtl) {
629 original_locale_ = l10n_util::GetApplicationLocale(std::string());
630 if (is_rtl_)
631 base::i18n::SetICUDefaultLocale("he");
632 CheckTextDirectionIsCorrect();
635 ~ScopedTextDirectionChange() {
636 if (is_rtl_)
637 base::i18n::SetICUDefaultLocale(original_locale_);
640 private:
641 void CheckTextDirectionIsCorrect() {
642 ASSERT_EQ(is_rtl_, base::i18n::IsRTL());
645 bool is_rtl_;
646 std::string original_locale_;
649 class ShelfViewTextDirectionTest
650 : public ShelfViewTest,
651 public testing::WithParamInterface<bool> {
652 public:
653 ShelfViewTextDirectionTest() : text_direction_change_(GetParam()) {}
654 virtual ~ShelfViewTextDirectionTest() {}
656 void SetUp() override { ShelfViewTest::SetUp(); }
658 void TearDown() override { ShelfViewTest::TearDown(); }
660 private:
661 ScopedTextDirectionChange text_direction_change_;
663 DISALLOW_COPY_AND_ASSIGN(ShelfViewTextDirectionTest);
666 // Checks that the ideal item icon bounds match the view's bounds in the screen
667 // in both LTR and RTL.
668 TEST_P(ShelfViewTextDirectionTest, IdealBoundsOfItemIcon) {
669 ShelfID id = AddPlatformApp();
670 ShelfButton* button = GetButtonByID(id);
671 gfx::Rect item_bounds = button->GetBoundsInScreen();
672 gfx::Point icon_offset = button->GetIconBounds().origin();
673 item_bounds.Offset(icon_offset.OffsetFromOrigin());
674 gfx::Rect ideal_bounds = shelf_view_->GetIdealBoundsOfItemIcon(id);
675 gfx::Point screen_origin;
676 views::View::ConvertPointToScreen(shelf_view_, &screen_origin);
677 ideal_bounds.Offset(screen_origin.x(), screen_origin.y());
678 EXPECT_EQ(item_bounds.x(), ideal_bounds.x());
679 EXPECT_EQ(item_bounds.y(), ideal_bounds.y());
682 // Check that items in the overflow area are returning the overflow button as
683 // ideal bounds.
684 TEST_F(ShelfViewTest, OverflowButtonBounds) {
685 ShelfID first_id = AddPlatformApp();
686 ShelfID overflow_id = AddPlatformApp();
687 int items_added = 0;
688 while (!test_api_->IsOverflowButtonVisible()) {
689 // Added button is visible after animation while in this loop.
690 EXPECT_TRUE(GetButtonByID(overflow_id)->visible());
691 overflow_id = AddPlatformApp();
692 ++items_added;
693 ASSERT_LT(items_added, 10000);
695 ShelfID last_id = AddPlatformApp();
697 gfx::Rect first_bounds = shelf_view_->GetIdealBoundsOfItemIcon(first_id);
698 gfx::Rect overflow_bounds =
699 shelf_view_->GetIdealBoundsOfItemIcon(overflow_id);
700 gfx::Rect last_bounds = shelf_view_->GetIdealBoundsOfItemIcon(last_id);
702 // Check that all items have the same size and that the overflow items are
703 // identical whereas the first one does not match either of them.
704 EXPECT_EQ(first_bounds.size().ToString(), last_bounds.size().ToString());
705 EXPECT_NE(first_bounds.ToString(), last_bounds.ToString());
706 EXPECT_EQ(overflow_bounds.ToString(), last_bounds.ToString());
709 // Checks that shelf view contents are considered in the correct drag group.
710 TEST_F(ShelfViewTest, EnforceDragType) {
711 EXPECT_TRUE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_PLATFORM_APP));
712 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_SHORTCUT));
713 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP,
714 TYPE_BROWSER_SHORTCUT));
715 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_WINDOWED_APP));
716 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_LIST));
717 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_PANEL));
719 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_SHORTCUT));
720 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
721 TYPE_BROWSER_SHORTCUT));
722 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
723 TYPE_WINDOWED_APP));
724 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_LIST));
725 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_PANEL));
727 EXPECT_TRUE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
728 TYPE_BROWSER_SHORTCUT));
729 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
730 TYPE_WINDOWED_APP));
731 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_LIST));
732 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_PANEL));
734 EXPECT_TRUE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_WINDOWED_APP));
735 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_LIST));
736 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_PANEL));
738 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_LIST));
739 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_PANEL));
741 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_PANEL, TYPE_APP_PANEL));
744 // Adds platform app button until overflow and verifies that the last added
745 // platform app button is hidden.
746 TEST_F(ShelfViewTest, AddBrowserUntilOverflow) {
747 // All buttons should be visible.
748 ASSERT_EQ(test_api_->GetButtonCount(),
749 test_api_->GetLastVisibleIndex() + 1);
751 // Add platform app button until overflow.
752 int items_added = 0;
753 ShelfID last_added = AddPlatformApp();
754 while (!test_api_->IsOverflowButtonVisible()) {
755 // Added button is visible after animation while in this loop.
756 EXPECT_TRUE(GetButtonByID(last_added)->visible());
758 last_added = AddPlatformApp();
759 ++items_added;
760 ASSERT_LT(items_added, 10000);
763 // The last added button should be invisible.
764 EXPECT_FALSE(GetButtonByID(last_added)->visible());
767 // Adds one platform app button then adds app shortcut until overflow. Verifies
768 // that the browser button gets hidden on overflow and last added app shortcut
769 // is still visible.
770 TEST_F(ShelfViewTest, AddAppShortcutWithBrowserButtonUntilOverflow) {
771 // All buttons should be visible.
772 ASSERT_EQ(test_api_->GetButtonCount(),
773 test_api_->GetLastVisibleIndex() + 1);
775 ShelfID browser_button_id = AddPlatformApp();
777 // Add app shortcut until overflow.
778 int items_added = 0;
779 ShelfID last_added = AddAppShortcut();
780 while (!test_api_->IsOverflowButtonVisible()) {
781 // Added button is visible after animation while in this loop.
782 EXPECT_TRUE(GetButtonByID(last_added)->visible());
784 last_added = AddAppShortcut();
785 ++items_added;
786 ASSERT_LT(items_added, 10000);
789 // And the platform app button is invisible.
790 EXPECT_FALSE(GetButtonByID(browser_button_id)->visible());
793 TEST_F(ShelfViewTest, AddPanelHidesPlatformAppButton) {
794 // All buttons should be visible.
795 ASSERT_EQ(test_api_->GetButtonCount(),
796 test_api_->GetLastVisibleIndex() + 1);
798 // Add platform app button until overflow, remember last visible platform app
799 // button.
800 int items_added = 0;
801 ShelfID first_added = AddPlatformApp();
802 EXPECT_TRUE(GetButtonByID(first_added)->visible());
803 while (true) {
804 ShelfID added = AddPlatformApp();
805 if (test_api_->IsOverflowButtonVisible()) {
806 EXPECT_FALSE(GetButtonByID(added)->visible());
807 RemoveByID(added);
808 break;
810 ++items_added;
811 ASSERT_LT(items_added, 10000);
814 ShelfID panel = AddPanel();
815 EXPECT_TRUE(test_api_->IsOverflowButtonVisible());
817 RemoveByID(panel);
818 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
821 // When there are more panels then platform app buttons we should hide panels
822 // rather than platform apps.
823 TEST_F(ShelfViewTest, PlatformAppHidesExcessPanels) {
824 // All buttons should be visible.
825 ASSERT_EQ(test_api_->GetButtonCount(),
826 test_api_->GetLastVisibleIndex() + 1);
828 // Add platform app button.
829 ShelfID platform_app = AddPlatformApp();
830 ShelfID first_panel = AddPanel();
832 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
833 EXPECT_TRUE(GetButtonByID(first_panel)->visible());
835 // Add panels until there is an overflow.
836 ShelfID last_panel = first_panel;
837 int items_added = 0;
838 while (!test_api_->IsOverflowButtonVisible()) {
839 last_panel = AddPanel();
840 ++items_added;
841 ASSERT_LT(items_added, 10000);
844 // The first panel should now be hidden by the new platform apps needing
845 // space.
846 EXPECT_FALSE(GetButtonByID(first_panel)->visible());
847 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
848 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
850 // Adding platform apps should eventually begin to hide platform apps. We will
851 // add platform apps until either the last panel or platform app is hidden.
852 items_added = 0;
853 while (GetButtonByID(platform_app)->visible() &&
854 GetButtonByID(last_panel)->visible()) {
855 platform_app = AddPlatformApp();
856 ++items_added;
857 ASSERT_LT(items_added, 10000);
859 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
860 EXPECT_FALSE(GetButtonByID(platform_app)->visible());
863 // Adds button until overflow then removes first added one. Verifies that
864 // the last added one changes from invisible to visible and overflow
865 // chevron is gone.
866 TEST_F(ShelfViewTest, RemoveButtonRevealsOverflowed) {
867 // All buttons should be visible.
868 ASSERT_EQ(test_api_->GetButtonCount(),
869 test_api_->GetLastVisibleIndex() + 1);
871 // Add platform app buttons until overflow.
872 int items_added = 0;
873 ShelfID first_added = AddPlatformApp();
874 ShelfID last_added = first_added;
875 while (!test_api_->IsOverflowButtonVisible()) {
876 last_added = AddPlatformApp();
877 ++items_added;
878 ASSERT_LT(items_added, 10000);
881 // Expect add more than 1 button. First added is visible and last is not.
882 EXPECT_NE(first_added, last_added);
883 EXPECT_TRUE(GetButtonByID(first_added)->visible());
884 EXPECT_FALSE(GetButtonByID(last_added)->visible());
886 // Remove first added.
887 RemoveByID(first_added);
889 // Last added button becomes visible and overflow chevron is gone.
890 EXPECT_TRUE(GetButtonByID(last_added)->visible());
891 EXPECT_EQ(1.0f, GetButtonByID(last_added)->layer()->opacity());
892 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
895 // Verifies that remove last overflowed button should hide overflow chevron.
896 TEST_F(ShelfViewTest, RemoveLastOverflowed) {
897 // All buttons should be visible.
898 ASSERT_EQ(test_api_->GetButtonCount(),
899 test_api_->GetLastVisibleIndex() + 1);
901 // Add platform app button until overflow.
902 int items_added = 0;
903 ShelfID last_added = AddPlatformApp();
904 while (!test_api_->IsOverflowButtonVisible()) {
905 last_added = AddPlatformApp();
906 ++items_added;
907 ASSERT_LT(items_added, 10000);
910 RemoveByID(last_added);
911 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
914 // Adds platform app button without waiting for animation to finish and verifies
915 // that all added buttons are visible.
916 TEST_F(ShelfViewTest, AddButtonQuickly) {
917 // All buttons should be visible.
918 ASSERT_EQ(test_api_->GetButtonCount(),
919 test_api_->GetLastVisibleIndex() + 1);
921 // Add a few platform buttons quickly without wait for animation.
922 int added_count = 0;
923 while (!test_api_->IsOverflowButtonVisible()) {
924 AddPlatformAppNoWait();
925 ++added_count;
926 ASSERT_LT(added_count, 10000);
929 // ShelfView should be big enough to hold at least 3 new buttons.
930 ASSERT_GE(added_count, 3);
932 // Wait for the last animation to finish.
933 test_api_->RunMessageLoopUntilAnimationsDone();
935 // Verifies non-overflow buttons are visible.
936 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
937 ShelfButton* button = test_api_->GetButton(i);
938 if (button) {
939 EXPECT_TRUE(button->visible()) << "button index=" << i;
940 EXPECT_EQ(1.0f, button->layer()->opacity()) << "button index=" << i;
945 // Check that model changes are handled correctly while a shelf icon is being
946 // dragged.
947 TEST_F(ShelfViewTest, ModelChangesWhileDragging) {
948 ShelfButtonHost* button_host = shelf_view_;
950 std::vector<std::pair<ShelfID, views::View*> > id_map;
951 SetupForDragTest(&id_map);
953 // Dragging browser shortcut at index 1.
954 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
955 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
956 std::rotate(id_map.begin() + 1,
957 id_map.begin() + 2,
958 id_map.begin() + 4);
959 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
960 button_host->PointerReleasedOnButton(
961 dragged_button, ShelfButtonHost::MOUSE, false);
962 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
964 // Dragging changes model order.
965 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
966 std::rotate(id_map.begin() + 1,
967 id_map.begin() + 2,
968 id_map.begin() + 4);
969 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
971 // Cancelling the drag operation restores previous order.
972 button_host->PointerReleasedOnButton(
973 dragged_button, ShelfButtonHost::MOUSE, true);
974 std::rotate(id_map.begin() + 1,
975 id_map.begin() + 3,
976 id_map.begin() + 4);
977 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
979 // Deleting an item keeps the remaining intact.
980 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
981 model_->RemoveItemAt(1);
982 id_map.erase(id_map.begin() + 1);
983 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
984 button_host->PointerReleasedOnButton(
985 dragged_button, ShelfButtonHost::MOUSE, false);
987 // Adding a shelf item cancels the drag and respects the order.
988 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
989 ShelfID new_id = AddAppShortcut();
990 id_map.insert(id_map.begin() + 6,
991 std::make_pair(new_id, GetButtonByID(new_id)));
992 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
993 button_host->PointerReleasedOnButton(
994 dragged_button, ShelfButtonHost::MOUSE, false);
996 // Adding a shelf item at the end (i.e. a panel) canels drag and respects
997 // the order.
998 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
999 new_id = AddPanel();
1000 id_map.insert(id_map.begin() + 7,
1001 std::make_pair(new_id, GetButtonByID(new_id)));
1002 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1003 button_host->PointerReleasedOnButton(
1004 dragged_button, ShelfButtonHost::MOUSE, false);
1007 // Check that 2nd drag from the other pointer would be ignored.
1008 TEST_F(ShelfViewTest, SimultaneousDrag) {
1009 ShelfButtonHost* button_host = shelf_view_;
1011 std::vector<std::pair<ShelfID, views::View*> > id_map;
1012 SetupForDragTest(&id_map);
1014 // Start a mouse drag.
1015 views::View* dragged_button_mouse =
1016 SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1017 std::rotate(id_map.begin() + 1,
1018 id_map.begin() + 2,
1019 id_map.begin() + 4);
1020 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1021 // Attempt a touch drag before the mouse drag finishes.
1022 views::View* dragged_button_touch =
1023 SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
1025 // Nothing changes since 2nd drag is ignored.
1026 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1028 // Finish the mouse drag.
1029 button_host->PointerReleasedOnButton(
1030 dragged_button_mouse, ShelfButtonHost::MOUSE, false);
1031 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1033 // Now start a touch drag.
1034 dragged_button_touch = SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
1035 std::rotate(id_map.begin() + 3,
1036 id_map.begin() + 4,
1037 id_map.begin() + 5);
1038 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1040 // And attempt a mouse drag before the touch drag finishes.
1041 dragged_button_mouse = SimulateDrag(ShelfButtonHost::MOUSE, 1, 2);
1043 // Nothing changes since 2nd drag is ignored.
1044 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1046 button_host->PointerReleasedOnButton(
1047 dragged_button_touch, ShelfButtonHost::TOUCH, false);
1048 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1051 // Check that clicking first on one item and then dragging another works as
1052 // expected.
1053 TEST_F(ShelfViewTest, ClickOneDragAnother) {
1054 ShelfButtonHost* button_host = shelf_view_;
1056 std::vector<std::pair<ShelfID, views::View*> > id_map;
1057 SetupForDragTest(&id_map);
1059 // A click on item 1 is simulated.
1060 SimulateClick(1);
1062 // Dragging browser index at 0 should change the model order correctly.
1063 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
1064 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1065 std::rotate(id_map.begin() + 1,
1066 id_map.begin() + 2,
1067 id_map.begin() + 4);
1068 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1069 button_host->PointerReleasedOnButton(
1070 dragged_button, ShelfButtonHost::MOUSE, false);
1071 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
1074 // Tests that double-clicking an item does not activate it twice.
1075 TEST_F(ShelfViewTest, ClickingTwiceActivatesOnce) {
1076 // Watch for selection of the browser shortcut.
1077 ShelfID browser_shelf_id = model_->items()[browser_index_].id;
1078 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1079 item_manager_->SetShelfItemDelegate(
1080 browser_shelf_id,
1081 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1083 // A single click selects the item.
1084 SimulateClick(browser_index_);
1085 EXPECT_TRUE(selection_tracker->WasSelected());
1087 // A double-click does not select the item.
1088 selection_tracker->Reset();
1089 SimulateDoubleClick(browser_index_);
1090 EXPECT_FALSE(selection_tracker->WasSelected());
1093 // Check that clicking an item and jittering the mouse a bit still selects the
1094 // item.
1095 TEST_F(ShelfViewTest, ClickAndMoveSlightly) {
1096 std::vector<std::pair<ShelfID, views::View*> > id_map;
1097 SetupForDragTest(&id_map);
1099 ShelfID shelf_id = (id_map.begin() + 1)->first;
1100 views::View* button = (id_map.begin() + 1)->second;
1102 // Replace the ShelfItemDelegate for |shelf_id| with one which tracks whether
1103 // the shelf item gets selected.
1104 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1105 item_manager_->SetShelfItemDelegate(
1106 shelf_id,
1107 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1109 gfx::Vector2d press_offset(5, 30);
1110 gfx::Point press_location = gfx::Point() + press_offset;
1111 gfx::Point press_location_in_screen =
1112 button->GetBoundsInScreen().origin() + press_offset;
1114 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED,
1115 press_location,
1116 press_location_in_screen,
1117 ui::EF_LEFT_MOUSE_BUTTON, 0);
1118 button->OnMousePressed(click_event);
1120 ui::MouseEvent drag_event1(ui::ET_MOUSE_DRAGGED,
1121 press_location + gfx::Vector2d(0, 1),
1122 press_location_in_screen + gfx::Vector2d(0, 1),
1123 ui::EF_LEFT_MOUSE_BUTTON, 0);
1124 button->OnMouseDragged(drag_event1);
1126 ui::MouseEvent drag_event2(ui::ET_MOUSE_DRAGGED,
1127 press_location + gfx::Vector2d(-1, 0),
1128 press_location_in_screen + gfx::Vector2d(-1, 0),
1129 ui::EF_LEFT_MOUSE_BUTTON, 0);
1130 button->OnMouseDragged(drag_event2);
1132 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
1133 press_location + gfx::Vector2d(-1, 0),
1134 press_location_in_screen + gfx::Vector2d(-1, 0),
1135 ui::EF_LEFT_MOUSE_BUTTON, 0);
1136 button->OnMouseReleased(release_event);
1138 EXPECT_TRUE(selection_tracker->WasSelected());
1141 // Confirm that item status changes are reflected in the buttons.
1142 TEST_F(ShelfViewTest, ShelfItemStatus) {
1143 // All buttons should be visible.
1144 ASSERT_EQ(test_api_->GetButtonCount(),
1145 test_api_->GetLastVisibleIndex() + 1);
1147 // Add platform app button.
1148 ShelfID last_added = AddPlatformApp();
1149 ShelfItem item = GetItemByID(last_added);
1150 int index = model_->ItemIndexByID(last_added);
1151 ShelfButton* button = GetButtonByID(last_added);
1152 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1153 item.status = STATUS_ACTIVE;
1154 model_->Set(index, item);
1155 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1156 item.status = STATUS_ATTENTION;
1157 model_->Set(index, item);
1158 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1161 // Confirm that item status changes are reflected in the buttons
1162 // for platform apps.
1163 TEST_F(ShelfViewTest, ShelfItemStatusPlatformApp) {
1164 // All buttons should be visible.
1165 ASSERT_EQ(test_api_->GetButtonCount(),
1166 test_api_->GetLastVisibleIndex() + 1);
1168 // Add platform app button.
1169 ShelfID last_added = AddPlatformApp();
1170 ShelfItem item = GetItemByID(last_added);
1171 int index = model_->ItemIndexByID(last_added);
1172 ShelfButton* button = GetButtonByID(last_added);
1173 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1174 item.status = STATUS_ACTIVE;
1175 model_->Set(index, item);
1176 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1177 item.status = STATUS_ATTENTION;
1178 model_->Set(index, item);
1179 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1182 // Confirm that shelf item bounds are correctly updated on shelf changes.
1183 TEST_F(ShelfViewTest, ShelfItemBoundsCheck) {
1184 VerifyShelfItemBoundsAreValid();
1185 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1186 SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
1187 test_api_->RunMessageLoopUntilAnimationsDone();
1188 VerifyShelfItemBoundsAreValid();
1189 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1190 SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
1191 test_api_->RunMessageLoopUntilAnimationsDone();
1192 VerifyShelfItemBoundsAreValid();
1195 TEST_F(ShelfViewTest, ShelfTooltipTest) {
1196 ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
1197 test_api_->GetButtonCount());
1199 // Prepare some items to the shelf.
1200 ShelfID app_button_id = AddAppShortcut();
1201 ShelfID platform_button_id = AddPlatformApp();
1203 ShelfButton* app_button = GetButtonByID(app_button_id);
1204 ShelfButton* platform_button = GetButtonByID(platform_button_id);
1206 ShelfButtonHost* button_host = shelf_view_;
1207 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1209 button_host->MouseEnteredButton(app_button);
1210 // There's a delay to show the tooltip, so it's not visible yet.
1211 EXPECT_FALSE(tooltip_manager->IsVisible());
1212 EXPECT_EQ(app_button, GetTooltipAnchorView());
1214 ShowTooltip();
1215 EXPECT_TRUE(tooltip_manager->IsVisible());
1217 // Once it's visible, it keeps visibility and is pointing to the same
1218 // item.
1219 button_host->MouseExitedButton(app_button);
1220 EXPECT_TRUE(tooltip_manager->IsVisible());
1221 EXPECT_EQ(app_button, GetTooltipAnchorView());
1223 // When entered to another item, it switches to the new item. There is no
1224 // delay for the visibility.
1225 button_host->MouseEnteredButton(platform_button);
1226 EXPECT_TRUE(tooltip_manager->IsVisible());
1227 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1229 button_host->MouseExitedButton(platform_button);
1230 tooltip_manager->Close();
1232 // Next time: enter app_button -> move immediately to tab_button.
1233 button_host->MouseEnteredButton(app_button);
1234 button_host->MouseExitedButton(app_button);
1235 button_host->MouseEnteredButton(platform_button);
1236 EXPECT_FALSE(tooltip_manager->IsVisible());
1237 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1240 // Verify a fix for crash caused by a tooltip update for a deletedshelf
1241 // button, see crbug.com/288838.
1242 TEST_F(ShelfViewTest, RemovingItemClosesTooltip) {
1243 ShelfButtonHost* button_host = shelf_view_;
1244 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1246 // Add an item to the shelf.
1247 ShelfID app_button_id = AddAppShortcut();
1248 ShelfButton* app_button = GetButtonByID(app_button_id);
1250 // Spawn a tooltip on that item.
1251 button_host->MouseEnteredButton(app_button);
1252 ShowTooltip();
1253 EXPECT_TRUE(tooltip_manager->IsVisible());
1255 // Remove the app shortcut while the tooltip is open. The tooltip should be
1256 // closed.
1257 RemoveByID(app_button_id);
1258 EXPECT_FALSE(tooltip_manager->IsVisible());
1260 // Change the shelf layout. This should not crash.
1261 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1262 Shell::GetPrimaryRootWindow());
1265 // Changing the shelf alignment closes any open tooltip.
1266 TEST_F(ShelfViewTest, ShelfAlignmentClosesTooltip) {
1267 ShelfButtonHost* button_host = shelf_view_;
1268 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1270 // Add an item to the shelf.
1271 ShelfID app_button_id = AddAppShortcut();
1272 ShelfButton* app_button = GetButtonByID(app_button_id);
1274 // Spawn a tooltip on the item.
1275 button_host->MouseEnteredButton(app_button);
1276 ShowTooltip();
1277 EXPECT_TRUE(tooltip_manager->IsVisible());
1279 // Changing shelf alignment hides the tooltip.
1280 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1281 Shell::GetPrimaryRootWindow());
1282 EXPECT_FALSE(tooltip_manager->IsVisible());
1285 TEST_F(ShelfViewTest, ShouldHideTooltipTest) {
1286 ShelfID app_button_id = AddAppShortcut();
1287 ShelfID platform_button_id = AddPlatformApp();
1289 // The tooltip shouldn't hide if the mouse is on normal buttons.
1290 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1291 ShelfButton* button = test_api_->GetButton(i);
1292 if (!button)
1293 continue;
1295 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1296 button->GetMirroredBounds().CenterPoint()))
1297 << "ShelfView tries to hide on button " << i;
1300 // The tooltip should not hide on the app-list button.
1301 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1302 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1303 app_list_button->GetMirroredBounds().CenterPoint()));
1305 // The tooltip shouldn't hide if the mouse is in the gap between two buttons.
1306 gfx::Rect app_button_rect = GetButtonByID(app_button_id)->GetMirroredBounds();
1307 gfx::Rect platform_button_rect =
1308 GetButtonByID(platform_button_id)->GetMirroredBounds();
1309 ASSERT_FALSE(app_button_rect.Intersects(platform_button_rect));
1310 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1311 gfx::UnionRects(app_button_rect, platform_button_rect).CenterPoint()));
1313 // The tooltip should hide if it's outside of all buttons.
1314 gfx::Rect all_area;
1315 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1316 ShelfButton* button = test_api_->GetButton(i);
1317 if (!button)
1318 continue;
1320 all_area.Union(button->GetMirroredBounds());
1322 all_area.Union(shelf_view_->GetAppListButtonView()->GetMirroredBounds());
1323 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(all_area.origin()));
1324 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1325 gfx::Point(all_area.right() - 1, all_area.bottom() - 1)));
1326 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1327 gfx::Point(all_area.right(), all_area.y())));
1328 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1329 gfx::Point(all_area.x() - 1, all_area.y())));
1330 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1331 gfx::Point(all_area.x(), all_area.y() - 1)));
1332 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1333 gfx::Point(all_area.x(), all_area.bottom())));
1336 TEST_F(ShelfViewTest, ShouldHideTooltipWithAppListWindowTest) {
1337 Shell::GetInstance()->ShowAppList(NULL);
1338 ASSERT_TRUE(Shell::GetInstance()->GetAppListWindow());
1340 // The tooltip shouldn't hide if the mouse is on normal buttons.
1341 for (int i = 1; i < test_api_->GetButtonCount(); i++) {
1342 ShelfButton* button = test_api_->GetButton(i);
1343 if (!button)
1344 continue;
1346 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1347 button->GetMirroredBounds().CenterPoint()))
1348 << "ShelfView tries to hide on button " << i;
1351 // The tooltip should hide on the app-list button.
1352 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1353 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1354 app_list_button->GetMirroredBounds().CenterPoint()));
1357 // Test that by moving the mouse cursor off the button onto the bubble it closes
1358 // the bubble.
1359 TEST_F(ShelfViewTest, ShouldHideTooltipWhenHoveringOnTooltip) {
1360 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1361 tooltip_manager->CreateZeroDelayTimerForTest();
1362 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1364 // Move the mouse off any item and check that no tooltip is shown.
1365 generator.MoveMouseTo(gfx::Point(0, 0));
1366 EXPECT_FALSE(tooltip_manager->IsVisible());
1368 // Move the mouse over the button and check that it is visible.
1369 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1370 gfx::Rect bounds = app_list_button->GetBoundsInScreen();
1371 generator.MoveMouseTo(bounds.CenterPoint());
1372 // Wait for the timer to go off.
1373 RunAllPendingInMessageLoop();
1374 EXPECT_TRUE(tooltip_manager->IsVisible());
1376 // Move the mouse cursor slightly to the right of the item. The tooltip should
1377 // stay open.
1378 generator.MoveMouseBy(bounds.width() / 2 + 5, 0);
1379 // Make sure there is no delayed close.
1380 RunAllPendingInMessageLoop();
1381 EXPECT_TRUE(tooltip_manager->IsVisible());
1383 // Move back - it should still stay open.
1384 generator.MoveMouseBy(-(bounds.width() / 2 + 5), 0);
1385 // Make sure there is no delayed close.
1386 RunAllPendingInMessageLoop();
1387 EXPECT_TRUE(tooltip_manager->IsVisible());
1389 // Now move the mouse cursor slightly above the item - so that it is over the
1390 // tooltip bubble. Now it should disappear.
1391 generator.MoveMouseBy(0, -(bounds.height() / 2 + 5));
1392 // Wait until the delayed close kicked in.
1393 RunAllPendingInMessageLoop();
1394 EXPECT_FALSE(tooltip_manager->IsVisible());
1397 // Resizing shelf view while an add animation without fade-in is running,
1398 // which happens when overflow happens. App list button should end up in its
1399 // new ideal bounds.
1400 TEST_F(ShelfViewTest, ResizeDuringOverflowAddAnimation) {
1401 // All buttons should be visible.
1402 ASSERT_EQ(test_api_->GetButtonCount(),
1403 test_api_->GetLastVisibleIndex() + 1);
1405 // Add buttons until overflow. Let the non-overflow add animations finish but
1406 // leave the last running.
1407 int items_added = 0;
1408 AddPlatformAppNoWait();
1409 while (!test_api_->IsOverflowButtonVisible()) {
1410 test_api_->RunMessageLoopUntilAnimationsDone();
1411 AddPlatformAppNoWait();
1412 ++items_added;
1413 ASSERT_LT(items_added, 10000);
1416 // Resize shelf view with that animation running and stay overflown.
1417 gfx::Rect bounds = shelf_view_->bounds();
1418 bounds.set_width(bounds.width() - kShelfSize);
1419 shelf_view_->SetBoundsRect(bounds);
1420 ASSERT_TRUE(test_api_->IsOverflowButtonVisible());
1422 // Finish the animation.
1423 test_api_->RunMessageLoopUntilAnimationsDone();
1425 // App list button should ends up in its new ideal bounds.
1426 const int app_list_button_index = test_api_->GetButtonCount() - 1;
1427 const gfx::Rect& app_list_ideal_bounds =
1428 test_api_->GetIdealBoundsByIndex(app_list_button_index);
1429 const gfx::Rect& app_list_bounds =
1430 test_api_->GetBoundsByIndex(app_list_button_index);
1431 EXPECT_EQ(app_list_ideal_bounds, app_list_bounds);
1434 // Checks the overflow bubble size when an item is ripped off and re-inserted.
1435 TEST_F(ShelfViewTest, OverflowBubbleSize) {
1436 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1437 ReplaceShelfDelegateForRipOffTest();
1439 AddButtonsUntilOverflow();
1441 // Show overflow bubble.
1442 test_api_->ShowOverflowBubble();
1443 ASSERT_TRUE(test_api_->overflow_bubble() &&
1444 test_api_->overflow_bubble()->IsShowing());
1446 ShelfViewTestAPI test_for_overflow_view(
1447 test_api_->overflow_bubble()->shelf_view());
1449 int ripped_index = test_for_overflow_view.GetLastVisibleIndex();
1450 gfx::Size bubble_size = test_for_overflow_view.GetPreferredSize();
1451 int item_width = test_for_overflow_view.GetButtonSize() +
1452 test_for_overflow_view.GetButtonSpacing();
1454 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
1455 gfx::Point());
1456 ShelfButton* button = test_for_overflow_view.GetButton(ripped_index);
1457 // Rip off the last visible item.
1458 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1459 gfx::Point rip_off_point(start_point.x(), 0);
1460 generator.MoveMouseTo(start_point.x(), start_point.y());
1461 base::MessageLoop::current()->RunUntilIdle();
1462 generator.PressLeftButton();
1463 base::MessageLoop::current()->RunUntilIdle();
1464 generator.MoveMouseTo(rip_off_point.x(), rip_off_point.y());
1465 base::MessageLoop::current()->RunUntilIdle();
1466 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1468 // Check the overflow bubble size when an item is ripped off.
1469 EXPECT_EQ(bubble_size.width() - item_width,
1470 test_for_overflow_view.GetPreferredSize().width());
1471 ASSERT_TRUE(test_api_->overflow_bubble() &&
1472 test_api_->overflow_bubble()->IsShowing());
1474 // Re-insert an item into the overflow bubble.
1475 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1476 button = test_for_overflow_view.GetButton(first_index);
1478 // Check the bubble size after an item is re-inserted.
1479 generator.MoveMouseTo(button->GetBoundsInScreen().CenterPoint());
1480 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1481 EXPECT_EQ(bubble_size.width(),
1482 test_for_overflow_view.GetPreferredSize().width());
1484 generator.ReleaseLeftButton();
1485 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1486 EXPECT_EQ(bubble_size.width(),
1487 test_for_overflow_view.GetPreferredSize().width());
1490 // Check the drag insertion bounds of scrolled overflow bubble.
1491 TEST_F(ShelfViewTest, CheckDragInsertBoundsOfScrolledOverflowBubble) {
1492 UpdateDisplay("400x300");
1494 EXPECT_EQ(2, model_->item_count());
1496 AddButtonsUntilOverflow();
1498 // Show overflow bubble.
1499 test_api_->ShowOverflowBubble();
1500 ASSERT_TRUE(test_api_->overflow_bubble() &&
1501 test_api_->overflow_bubble()->IsShowing());
1503 int item_width = test_api_->GetButtonSize() +
1504 test_api_->GetButtonSpacing();
1505 OverflowBubbleView* bubble_view = test_api_->overflow_bubble()->bubble_view();
1506 test::OverflowBubbleViewTestAPI bubble_view_api(bubble_view);
1508 // Add more buttons until OverflowBubble is scrollable and it has 3 invisible
1509 // items.
1510 while (bubble_view_api.GetContentsSize().width() <
1511 (bubble_view->GetContentsBounds().width() + 3 * item_width))
1512 AddAppShortcut();
1514 ASSERT_TRUE(test_api_->overflow_bubble() &&
1515 test_api_->overflow_bubble()->IsShowing());
1517 ShelfViewTestAPI test_for_overflow_view(
1518 test_api_->overflow_bubble()->shelf_view());
1519 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1520 int last_index = test_for_overflow_view.GetLastVisibleIndex();
1522 ShelfButton* first_button = test_for_overflow_view.GetButton(first_index);
1523 ShelfButton* last_button = test_for_overflow_view.GetButton(last_index);
1524 gfx::Point first_point = first_button->GetBoundsInScreen().CenterPoint();
1525 gfx::Point last_point = last_button->GetBoundsInScreen().CenterPoint();
1526 gfx::Rect drag_reinsert_bounds =
1527 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1528 EXPECT_TRUE(drag_reinsert_bounds.Contains(first_point));
1529 EXPECT_FALSE(drag_reinsert_bounds.Contains(last_point));
1531 // Scrolls sufficiently to show last item.
1532 bubble_view_api.ScrollByXOffset(3 * item_width);
1533 drag_reinsert_bounds =
1534 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1535 first_point = first_button->GetBoundsInScreen().CenterPoint();
1536 last_point = last_button->GetBoundsInScreen().CenterPoint();
1537 EXPECT_FALSE(drag_reinsert_bounds.Contains(first_point));
1538 EXPECT_TRUE(drag_reinsert_bounds.Contains(last_point));
1541 // Check the drag insertion bounds of shelf view in multi monitor environment.
1542 TEST_F(ShelfViewTest, CheckDragInsertBoundsWithMultiMonitor) {
1543 // win8-aura doesn't support multiple display.
1544 if (!SupportsMultipleDisplays())
1545 return;
1547 UpdateDisplay("800x600,800x600");
1548 Shelf* secondary_shelf = Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
1549 ShelfView* shelf_view_for_secondary =
1550 ShelfTestAPI(secondary_shelf).shelf_view();
1552 // The bounds should be big enough for 4 buttons + overflow chevron.
1553 shelf_view_for_secondary->SetBounds(0, 0, 500, kShelfSize);
1555 ShelfViewTestAPI test_api_for_secondary(shelf_view_for_secondary);
1556 // Speeds up animation for test.
1557 test_api_for_secondary.SetAnimationDuration(1);
1559 AddButtonsUntilOverflow();
1561 // Test #1: Test drag insertion bounds of primary shelf.
1562 // Show overflow bubble.
1563 test_api_->ShowOverflowBubble();
1564 ASSERT_TRUE(test_api_->overflow_bubble() &&
1565 test_api_->overflow_bubble()->IsShowing());
1567 ShelfViewTestAPI test_api_for_overflow_view(
1568 test_api_->overflow_bubble()->shelf_view());
1570 ShelfButton* button = test_api_for_overflow_view.GetButton(
1571 test_api_for_overflow_view.GetLastVisibleIndex());
1573 // Checks that a point in shelf is contained in drag insert bounds.
1574 gfx::Point point_in_shelf_view = button->GetBoundsInScreen().CenterPoint();
1575 gfx::Rect drag_reinsert_bounds =
1576 test_api_for_overflow_view.GetBoundsForDragInsertInScreen();
1577 EXPECT_TRUE(drag_reinsert_bounds.Contains(point_in_shelf_view));
1578 // Checks that a point out of shelf is not contained in drag insert bounds.
1579 EXPECT_FALSE(drag_reinsert_bounds.Contains(
1580 gfx::Point(point_in_shelf_view.x(), 0)));
1582 // Test #2: Test drag insertion bounds of secondary shelf.
1583 // Show overflow bubble.
1584 test_api_for_secondary.ShowOverflowBubble();
1585 ASSERT_TRUE(test_api_for_secondary.overflow_bubble() &&
1586 test_api_for_secondary.overflow_bubble()->IsShowing());
1588 ShelfViewTestAPI test_api_for_overflow_view_of_secondary(
1589 test_api_for_secondary.overflow_bubble()->shelf_view());
1591 ShelfButton* button_in_secondary =
1592 test_api_for_overflow_view_of_secondary.GetButton(
1593 test_api_for_overflow_view_of_secondary.GetLastVisibleIndex());
1595 // Checks that a point in shelf is contained in drag insert bounds.
1596 gfx::Point point_in_secondary_shelf_view =
1597 button_in_secondary->GetBoundsInScreen().CenterPoint();
1598 gfx::Rect drag_reinsert_bounds_in_secondary =
1599 test_api_for_overflow_view_of_secondary.GetBoundsForDragInsertInScreen();
1600 EXPECT_TRUE(drag_reinsert_bounds_in_secondary.Contains(
1601 point_in_secondary_shelf_view));
1602 // Checks that a point out of shelf is not contained in drag insert bounds.
1603 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(
1604 gfx::Point(point_in_secondary_shelf_view.x(), 0)));
1605 // Checks that a point of overflow bubble in primary shelf should not be
1606 // contained by insert bounds of secondary shelf.
1607 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(point_in_shelf_view));
1610 // Checks the rip an item off from left aligned shelf in secondary monitor.
1611 TEST_F(ShelfViewTest, CheckRipOffFromLeftShelfAlignmentWithMultiMonitor) {
1612 // win8-aura doesn't support multiple display.
1613 if (!SupportsMultipleDisplays())
1614 return;
1616 UpdateDisplay("800x600,800x600");
1617 ASSERT_EQ(2U, Shell::GetAllRootWindows().size());
1619 aura::Window* second_root = Shell::GetAllRootWindows()[1];
1621 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT, second_root);
1622 ASSERT_EQ(SHELF_ALIGNMENT_LEFT,
1623 Shell::GetInstance()->GetShelfAlignment(second_root));
1625 // Initially, app list and browser shortcut are added.
1626 EXPECT_EQ(2, model_->item_count());
1627 int browser_index = model_->GetItemIndexForType(TYPE_BROWSER_SHORTCUT);
1628 EXPECT_GT(browser_index, 0);
1630 Shelf* secondary_shelf = Shelf::ForWindow(second_root);
1631 ShelfView* shelf_view_for_secondary =
1632 ShelfTestAPI(secondary_shelf).shelf_view();
1634 ShelfViewTestAPI test_api_for_secondary_shelf_view(shelf_view_for_secondary);
1635 ShelfButton* button =
1636 test_api_for_secondary_shelf_view.GetButton(browser_index);
1638 // Fetch the start point of dragging.
1639 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1640 ::wm::ConvertPointFromScreen(second_root, &start_point);
1642 ui::test::EventGenerator generator(second_root, start_point);
1644 // Rip off the browser item.
1645 generator.PressLeftButton();
1646 generator.MoveMouseTo(start_point.x() + 400, start_point.y());
1647 test_api_for_secondary_shelf_view.RunMessageLoopUntilAnimationsDone();
1648 EXPECT_TRUE(test_api_for_secondary_shelf_view.IsRippedOffFromShelf());
1651 // Checks various drag and drop operations from OverflowBubble to Shelf.
1652 TEST_F(ShelfViewTest, CheckDragAndDropFromOverflowBubbleToShelf) {
1653 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1654 ReplaceShelfDelegateForRipOffTest();
1656 AddButtonsUntilOverflow();
1658 TestDraggingAnItemFromOverflowToShelf(false);
1659 TestDraggingAnItemFromOverflowToShelf(true);
1662 // Tests that the AppListButton renders as active in response to touches.
1663 TEST_F(ShelfViewTest, DISABLED_AppListButtonTouchFeedback) {
1664 AppListButton* app_list_button =
1665 static_cast<AppListButton*>(shelf_view_->GetAppListButtonView());
1666 EXPECT_FALSE(app_list_button->draw_background_as_active());
1668 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1669 generator.set_current_location(app_list_button->
1670 GetBoundsInScreen().CenterPoint());
1671 generator.PressTouch();
1672 RunAllPendingInMessageLoop();
1673 EXPECT_TRUE(app_list_button->draw_background_as_active());
1675 generator.ReleaseTouch();
1676 RunAllPendingInMessageLoop();
1677 EXPECT_FALSE(app_list_button->draw_background_as_active());
1678 EXPECT_TRUE(Shell::GetInstance()->GetAppListTargetVisibility());
1681 // Tests that a touch that slides out of the bounds of the AppListButton leads
1682 // to the end of rendering an active state.
1683 TEST_F(ShelfViewTest, DISABLED_AppListButtonTouchFeedbackCancellation) {
1684 AppListButton* app_list_button =
1685 static_cast<AppListButton*>(shelf_view_->GetAppListButtonView());
1686 EXPECT_FALSE(app_list_button->draw_background_as_active());
1688 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1689 generator.set_current_location(app_list_button->
1690 GetBoundsInScreen().CenterPoint());
1691 generator.PressTouch();
1692 RunAllPendingInMessageLoop();
1693 EXPECT_TRUE(app_list_button->draw_background_as_active());
1695 gfx::Point moved_point(app_list_button->GetBoundsInScreen().right() + 1,
1696 app_list_button->
1697 GetBoundsInScreen().CenterPoint().y());
1698 generator.MoveTouch(moved_point);
1699 RunAllPendingInMessageLoop();
1700 EXPECT_FALSE(app_list_button->draw_background_as_active());
1702 generator.set_current_location(moved_point);
1703 generator.ReleaseTouch();
1704 RunAllPendingInMessageLoop();
1705 EXPECT_FALSE(app_list_button->draw_background_as_active());
1706 EXPECT_FALSE(Shell::GetInstance()->GetAppListTargetVisibility());
1709 class ShelfViewVisibleBoundsTest : public ShelfViewTest,
1710 public testing::WithParamInterface<bool> {
1711 public:
1712 ShelfViewVisibleBoundsTest() : text_direction_change_(GetParam()) {}
1714 void CheckAllItemsAreInBounds() {
1715 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1716 gfx::Rect shelf_bounds = shelf_view_->GetBoundsInScreen();
1717 EXPECT_TRUE(shelf_bounds.Contains(visible_bounds));
1718 for (int i = 0; i < test_api_->GetButtonCount(); ++i)
1719 if (ShelfButton* button = test_api_->GetButton(i))
1720 EXPECT_TRUE(visible_bounds.Contains(button->GetBoundsInScreen()));
1721 CheckAppListButtonIsInBounds();
1724 void CheckAppListButtonIsInBounds() {
1725 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1726 gfx::Rect app_list_button_bounds = shelf_view_->GetAppListButtonView()->
1727 GetBoundsInScreen();
1728 EXPECT_TRUE(visible_bounds.Contains(app_list_button_bounds));
1731 private:
1732 ScopedTextDirectionChange text_direction_change_;
1734 DISALLOW_COPY_AND_ASSIGN(ShelfViewVisibleBoundsTest);
1737 TEST_P(ShelfViewVisibleBoundsTest, ItemsAreInBounds) {
1738 // Adding elements leaving some empty space.
1739 for (int i = 0; i < 3; i++) {
1740 AddAppShortcut();
1742 test_api_->RunMessageLoopUntilAnimationsDone();
1743 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
1744 CheckAllItemsAreInBounds();
1745 // Same for overflow case.
1746 while (!test_api_->IsOverflowButtonVisible()) {
1747 AddAppShortcut();
1749 test_api_->RunMessageLoopUntilAnimationsDone();
1750 CheckAllItemsAreInBounds();
1753 INSTANTIATE_TEST_CASE_P(LtrRtl, ShelfViewTextDirectionTest, testing::Bool());
1754 INSTANTIATE_TEST_CASE_P(VisibleBounds, ShelfViewVisibleBoundsTest,
1755 testing::Bool());
1757 } // namespace test
1758 } // namespace ash