Mac: Fix performance issues with remote CoreAnimation
[chromium-blink-merge.git] / ash / shelf / shelf_view_unittest.cc
blobbf7f21d9a7375298dd21c02e5420b8a9c0a0083b
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/command_line.h"
34 #include "base/compiler_specific.h"
35 #include "base/memory/scoped_ptr.h"
36 #include "base/strings/string_number_conversions.h"
37 #include "ui/aura/test/aura_test_base.h"
38 #include "ui/aura/window.h"
39 #include "ui/aura/window_event_dispatcher.h"
40 #include "ui/base/l10n/l10n_util.h"
41 #include "ui/base/ui_base_switches.h"
42 #include "ui/compositor/layer.h"
43 #include "ui/events/event.h"
44 #include "ui/events/event_constants.h"
45 #include "ui/events/test/event_generator.h"
46 #include "ui/views/view_model.h"
47 #include "ui/views/widget/widget.h"
48 #include "ui/views/widget/widget_delegate.h"
49 #include "ui/wm/core/coordinate_conversion.h"
51 namespace ash {
52 namespace test {
54 ////////////////////////////////////////////////////////////////////////////////
55 // ShelfIconObserver tests.
57 class TestShelfIconObserver : public ShelfIconObserver {
58 public:
59 explicit TestShelfIconObserver(Shelf* shelf)
60 : shelf_(shelf),
61 change_notified_(false) {
62 if (shelf_)
63 shelf_->AddIconObserver(this);
66 ~TestShelfIconObserver() override {
67 if (shelf_)
68 shelf_->RemoveIconObserver(this);
71 // ShelfIconObserver implementation.
72 void OnShelfIconPositionsChanged() override { change_notified_ = true; }
74 int change_notified() const { return change_notified_; }
75 void Reset() { change_notified_ = false; }
77 private:
78 Shelf* shelf_;
79 bool change_notified_;
81 DISALLOW_COPY_AND_ASSIGN(TestShelfIconObserver);
84 class ShelfViewIconObserverTest : public AshTestBase {
85 public:
86 ShelfViewIconObserverTest() {}
87 ~ShelfViewIconObserverTest() override {}
89 void SetUp() override {
90 AshTestBase::SetUp();
91 Shelf* shelf = Shelf::ForPrimaryDisplay();
92 observer_.reset(new TestShelfIconObserver(shelf));
94 shelf_view_test_.reset(
95 new ShelfViewTestAPI(ShelfTestAPI(shelf).shelf_view()));
96 shelf_view_test_->SetAnimationDuration(1);
99 void TearDown() override {
100 observer_.reset();
101 AshTestBase::TearDown();
104 TestShelfIconObserver* observer() { return observer_.get(); }
106 ShelfViewTestAPI* shelf_view_test() {
107 return shelf_view_test_.get();
110 Shelf* ShelfForSecondaryDisplay() {
111 return Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
114 private:
115 scoped_ptr<TestShelfIconObserver> observer_;
116 scoped_ptr<ShelfViewTestAPI> shelf_view_test_;
118 DISALLOW_COPY_AND_ASSIGN(ShelfViewIconObserverTest);
121 // TestShelfItemDelegate which tracks whether it gets selected.
122 class ShelfItemSelectionTracker : public TestShelfItemDelegate {
123 public:
124 ShelfItemSelectionTracker() : TestShelfItemDelegate(NULL), selected_(false) {
127 ~ShelfItemSelectionTracker() override {}
129 // Resets to the initial state.
130 void Reset() { selected_ = false; }
132 // Returns true if the delegate was selected.
133 bool WasSelected() {
134 return selected_;
137 // TestShelfItemDelegate:
138 bool ItemSelected(const ui::Event& event) override {
139 selected_ = true;
140 return false;
143 private:
144 bool selected_;
146 DISALLOW_COPY_AND_ASSIGN(ShelfItemSelectionTracker);
149 TEST_F(ShelfViewIconObserverTest, AddRemove) {
150 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
151 ASSERT_TRUE(shelf_delegate);
153 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
154 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
155 params.bounds = gfx::Rect(0, 0, 200, 200);
156 params.context = CurrentContext();
158 scoped_ptr<views::Widget> widget(new views::Widget());
159 widget->Init(params);
160 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
161 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
162 EXPECT_TRUE(observer()->change_notified());
163 observer()->Reset();
165 widget->Show();
166 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
167 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
168 EXPECT_TRUE(observer()->change_notified());
169 observer()->Reset();
172 // Sometimes fails on trybots on win7_aura. http://crbug.com/177135
173 #if defined(OS_WIN)
174 #define MAYBE_AddRemoveWithMultipleDisplays \
175 DISABLED_AddRemoveWithMultipleDisplays
176 #else
177 #define MAYBE_AddRemoveWithMultipleDisplays \
178 AddRemoveWithMultipleDisplays
179 #endif
180 // Make sure creating/deleting an window on one displays notifies a
181 // shelf on external display as well as one on primary.
182 TEST_F(ShelfViewIconObserverTest, MAYBE_AddRemoveWithMultipleDisplays) {
183 UpdateDisplay("400x400,400x400");
184 TestShelfIconObserver second_observer(ShelfForSecondaryDisplay());
186 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
187 ASSERT_TRUE(shelf_delegate);
189 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
190 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
191 params.bounds = gfx::Rect(0, 0, 200, 200);
192 params.context = CurrentContext();
194 scoped_ptr<views::Widget> widget(new views::Widget());
195 widget->Init(params);
196 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
197 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
198 EXPECT_TRUE(observer()->change_notified());
199 EXPECT_TRUE(second_observer.change_notified());
200 observer()->Reset();
201 second_observer.Reset();
203 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
204 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
205 EXPECT_TRUE(observer()->change_notified());
206 EXPECT_TRUE(second_observer.change_notified());
208 observer()->Reset();
209 second_observer.Reset();
212 TEST_F(ShelfViewIconObserverTest, BoundsChanged) {
213 ShelfWidget* widget = Shell::GetPrimaryRootWindowController()->shelf();
214 Shelf* shelf = Shelf::ForPrimaryDisplay();
215 gfx::Size shelf_size = widget->GetWindowBoundsInScreen().size();
216 shelf_size.set_width(shelf_size.width() / 2);
217 ASSERT_GT(shelf_size.width(), 0);
218 shelf->SetShelfViewBounds(gfx::Rect(shelf_size));
219 // No animation happens for ShelfView bounds change.
220 EXPECT_TRUE(observer()->change_notified());
221 observer()->Reset();
224 ////////////////////////////////////////////////////////////////////////////////
225 // ShelfView tests.
227 // Simple ShelfDelegate implmentation for ShelfViewTest.OverflowBubbleSize
228 // and CheckDragAndDropFromOverflowBubbleToShelf
229 class TestShelfDelegateForShelfView : public ShelfDelegate {
230 public:
231 explicit TestShelfDelegateForShelfView(ShelfModel* model)
232 : model_(model) {}
233 ~TestShelfDelegateForShelfView() override {}
235 // ShelfDelegate overrides:
236 void OnShelfCreated(Shelf* shelf) override {}
238 void OnShelfDestroyed(Shelf* shelf) override {}
240 ShelfID GetShelfIDForAppID(const std::string& app_id) override {
241 ShelfID id = 0;
242 EXPECT_TRUE(base::StringToInt(app_id, &id));
243 return id;
246 const std::string& GetAppIDForShelfID(ShelfID id) override {
247 // Use |app_id_| member variable because returning a reference to local
248 // variable is not allowed.
249 app_id_ = base::IntToString(id);
250 return app_id_;
253 void PinAppWithID(const std::string& app_id) override {}
255 bool IsAppPinned(const std::string& app_id) override {
256 // Returns true for ShelfViewTest.OverflowBubbleSize. To test ripping off in
257 // that test, an item is already pinned state.
258 return true;
261 bool CanPin() const override { return true; }
263 void UnpinAppWithID(const std::string& app_id) override {
264 ShelfID id = 0;
265 EXPECT_TRUE(base::StringToInt(app_id, &id));
266 ASSERT_GT(id, 0);
267 int index = model_->ItemIndexByID(id);
268 ASSERT_GE(index, 0);
270 model_->RemoveItemAt(index);
273 private:
274 ShelfModel* model_;
276 // Temp member variable for returning a value. See the comment in the
277 // GetAppIDForShelfID().
278 std::string app_id_;
280 DISALLOW_COPY_AND_ASSIGN(TestShelfDelegateForShelfView);
283 class ShelfViewTest : public AshTestBase {
284 public:
285 ShelfViewTest()
286 : model_(NULL),
287 shelf_view_(NULL),
288 browser_index_(1),
289 item_manager_(NULL) {}
290 ~ShelfViewTest() override {}
292 void SetUp() override {
293 base::CommandLine::ForCurrentProcess()->AppendSwitch(
294 switches::kEnableTouchFeedback);
295 AshTestBase::SetUp();
296 test::ShellTestApi test_api(Shell::GetInstance());
297 model_ = test_api.shelf_model();
298 Shelf* shelf = Shelf::ForPrimaryDisplay();
299 shelf_view_ = ShelfTestAPI(shelf).shelf_view();
301 // The bounds should be big enough for 4 buttons + overflow chevron.
302 shelf_view_->SetBounds(0, 0, 500, kShelfSize);
304 test_api_.reset(new ShelfViewTestAPI(shelf_view_));
305 test_api_->SetAnimationDuration(1); // Speeds up animation for test.
307 item_manager_ = Shell::GetInstance()->shelf_item_delegate_manager();
308 DCHECK(item_manager_);
310 // Add browser shortcut shelf item at index 0 for test.
311 AddBrowserShortcut();
314 void TearDown() override {
315 test_api_.reset();
316 AshTestBase::TearDown();
319 protected:
320 void CreateAndSetShelfItemDelegateForID(ShelfID id) {
321 scoped_ptr<ShelfItemDelegate> delegate(new TestShelfItemDelegate(NULL));
322 item_manager_->SetShelfItemDelegate(id, delegate.Pass());
325 ShelfID AddBrowserShortcut() {
326 ShelfItem browser_shortcut;
327 browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
329 ShelfID id = model_->next_id();
330 model_->AddAt(browser_index_, browser_shortcut);
331 CreateAndSetShelfItemDelegateForID(id);
332 test_api_->RunMessageLoopUntilAnimationsDone();
333 return id;
336 ShelfID AddAppShortcut() {
337 ShelfItem item;
338 item.type = TYPE_APP_SHORTCUT;
339 item.status = STATUS_CLOSED;
341 ShelfID id = model_->next_id();
342 model_->Add(item);
343 CreateAndSetShelfItemDelegateForID(id);
344 test_api_->RunMessageLoopUntilAnimationsDone();
345 return id;
348 ShelfID AddPanel() {
349 ShelfID id = AddPanelNoWait();
350 test_api_->RunMessageLoopUntilAnimationsDone();
351 return id;
354 ShelfID AddPlatformAppNoWait() {
355 ShelfItem item;
356 item.type = TYPE_PLATFORM_APP;
357 item.status = STATUS_RUNNING;
359 ShelfID id = model_->next_id();
360 model_->Add(item);
361 CreateAndSetShelfItemDelegateForID(id);
362 return id;
365 ShelfID AddPanelNoWait() {
366 ShelfItem item;
367 item.type = TYPE_APP_PANEL;
368 item.status = STATUS_RUNNING;
370 ShelfID id = model_->next_id();
371 model_->Add(item);
372 CreateAndSetShelfItemDelegateForID(id);
373 return id;
376 ShelfID AddPlatformApp() {
377 ShelfID id = AddPlatformAppNoWait();
378 test_api_->RunMessageLoopUntilAnimationsDone();
379 return id;
382 void RemoveByID(ShelfID id) {
383 model_->RemoveItemAt(model_->ItemIndexByID(id));
384 test_api_->RunMessageLoopUntilAnimationsDone();
387 ShelfButton* GetButtonByID(ShelfID id) {
388 int index = model_->ItemIndexByID(id);
389 return test_api_->GetButton(index);
392 ShelfItem GetItemByID(ShelfID id) {
393 ShelfItems::const_iterator items = model_->ItemByID(id);
394 return *items;
397 void CheckModelIDs(
398 const std::vector<std::pair<ShelfID, views::View*> >& id_map) {
399 size_t map_index = 0;
400 for (size_t model_index = 0;
401 model_index < model_->items().size();
402 ++model_index) {
403 ShelfItem item = model_->items()[model_index];
404 ShelfID id = item.id;
405 EXPECT_EQ(id_map[map_index].first, id);
406 EXPECT_EQ(id_map[map_index].second, GetButtonByID(id));
407 ++map_index;
409 ASSERT_EQ(map_index, id_map.size());
412 void VerifyShelfItemBoundsAreValid() {
413 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
414 if (test_api_->GetButton(i)) {
415 gfx::Rect shelf_view_bounds = shelf_view_->GetLocalBounds();
416 gfx::Rect item_bounds = test_api_->GetBoundsByIndex(i);
417 EXPECT_GE(item_bounds.x(), 0);
418 EXPECT_GE(item_bounds.y(), 0);
419 EXPECT_LE(item_bounds.right(), shelf_view_bounds.width());
420 EXPECT_LE(item_bounds.bottom(), shelf_view_bounds.height());
425 ShelfButton* SimulateButtonPressed(ShelfButtonHost::Pointer pointer,
426 int button_index) {
427 ShelfButtonHost* button_host = shelf_view_;
428 ShelfButton* button = test_api_->GetButton(button_index);
429 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED,
430 gfx::Point(),
431 button->GetBoundsInScreen().origin(), 0, 0);
432 button_host->PointerPressedOnButton(button, pointer, click_event);
433 return button;
436 // Simulates a single mouse click.
437 void SimulateClick(int button_index) {
438 ShelfButtonHost* button_host = shelf_view_;
439 ShelfButton* button =
440 SimulateButtonPressed(ShelfButtonHost::MOUSE, button_index);
441 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
442 gfx::Point(),
443 button->GetBoundsInScreen().origin(),
446 test_api_->ButtonPressed(button, release_event);
447 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false);
450 // Simulates the second click of a double click.
451 void SimulateDoubleClick(int button_index) {
452 ShelfButtonHost* button_host = shelf_view_;
453 ShelfButton* button =
454 SimulateButtonPressed(ShelfButtonHost::MOUSE, button_index);
455 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
456 gfx::Point(),
457 button->GetBoundsInScreen().origin(),
458 ui::EF_IS_DOUBLE_CLICK,
460 test_api_->ButtonPressed(button, release_event);
461 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false);
464 views::View* SimulateDrag(ShelfButtonHost::Pointer pointer,
465 int button_index,
466 int destination_index) {
467 ShelfButtonHost* button_host = shelf_view_;
468 views::View* button = SimulateButtonPressed(pointer, button_index);
470 // Drag.
471 views::View* destination = test_api_->GetButton(destination_index);
472 ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED,
473 gfx::Point(destination->x() - button->x(),
474 destination->y() - button->y()),
475 destination->GetBoundsInScreen().origin(), 0, 0);
476 button_host->PointerDraggedOnButton(button, pointer, drag_event);
477 return button;
480 void SetupForDragTest(
481 std::vector<std::pair<ShelfID, views::View*> >* id_map) {
482 // Initialize |id_map| with the automatically-created shelf buttons.
483 for (size_t i = 0; i < model_->items().size(); ++i) {
484 ShelfButton* button = test_api_->GetButton(i);
485 id_map->push_back(std::make_pair(model_->items()[i].id, button));
487 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
489 // Add 5 app shelf buttons for testing.
490 for (int i = 0; i < 5; ++i) {
491 ShelfID id = AddAppShortcut();
492 // App Icon is located at index 0, and browser shortcut is located at
493 // index 1. So we should start to add app shortcut at index 2.
494 id_map->insert(id_map->begin() + (i + browser_index_ + 1),
495 std::make_pair(id, GetButtonByID(id)));
497 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
500 views::View* GetTooltipAnchorView() {
501 return shelf_view_->tooltip_manager()->anchor_;
504 void AddButtonsUntilOverflow() {
505 int items_added = 0;
506 while (!test_api_->IsOverflowButtonVisible()) {
507 AddAppShortcut();
508 ++items_added;
509 ASSERT_LT(items_added, 10000);
513 void ShowTooltip() {
514 shelf_view_->tooltip_manager()->ShowInternal();
517 void TestDraggingAnItemFromOverflowToShelf(bool cancel) {
518 test_api_->ShowOverflowBubble();
519 ASSERT_TRUE(test_api_->overflow_bubble() &&
520 test_api_->overflow_bubble()->IsShowing());
522 ash::test::ShelfViewTestAPI test_api_for_overflow(
523 test_api_->overflow_bubble()->shelf_view());
525 int total_item_count = model_->item_count();
527 int last_visible_item_id_in_shelf =
528 GetItemId(test_api_->GetLastVisibleIndex());
529 int second_last_visible_item_id_in_shelf =
530 GetItemId(test_api_->GetLastVisibleIndex() - 1);
531 int first_visible_item_id_in_overflow =
532 GetItemId(test_api_for_overflow.GetFirstVisibleIndex());
533 int second_last_visible_item_id_in_overflow =
534 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1);
536 int drag_item_index =
537 test_api_for_overflow.GetLastVisibleIndex();
538 ShelfID drag_item_id = GetItemId(drag_item_index);
539 ShelfButton* drag_button = test_api_for_overflow.GetButton(drag_item_index);
540 gfx::Point center_point_of_drag_item =
541 drag_button->GetBoundsInScreen().CenterPoint();
543 ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow(),
544 center_point_of_drag_item);
545 // Rip an item off to OverflowBubble.
546 generator.PressLeftButton();
547 gfx::Point rip_off_point(center_point_of_drag_item.x(), 0);
548 generator.MoveMouseTo(rip_off_point);
549 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
550 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
551 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
553 // Move a dragged item into Shelf at |drop_index|.
554 int drop_index = 1;
555 gfx::Point drop_point =
556 test_api_->GetButton(drop_index)->GetBoundsInScreen().CenterPoint();
557 int item_width = test_api_for_overflow.GetButtonSize();
558 // To insert at |drop_index|, more smaller x-axis value of |drop_point|
559 // should be used.
560 gfx::Point modified_drop_point(drop_point.x() - item_width / 4,
561 drop_point.y());
562 generator.MoveMouseTo(modified_drop_point);
563 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
564 test_api_->RunMessageLoopUntilAnimationsDone();
565 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
566 ASSERT_TRUE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
568 if (cancel)
569 drag_button->OnMouseCaptureLost();
570 else
571 generator.ReleaseLeftButton();
573 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
574 test_api_->RunMessageLoopUntilAnimationsDone();
575 ASSERT_FALSE(test_api_for_overflow.IsRippedOffFromShelf());
576 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
578 // Compare pre-stored items' id with newly positioned items' after dragging
579 // is canceled or finished.
580 if (cancel) {
581 EXPECT_EQ(last_visible_item_id_in_shelf,
582 GetItemId(test_api_->GetLastVisibleIndex()));
583 EXPECT_EQ(second_last_visible_item_id_in_shelf,
584 GetItemId(test_api_->GetLastVisibleIndex() - 1));
585 EXPECT_EQ(first_visible_item_id_in_overflow,
586 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
587 EXPECT_EQ(second_last_visible_item_id_in_overflow,
588 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1));
589 } else {
590 EXPECT_EQ(drag_item_id, GetItemId(drop_index));
591 EXPECT_EQ(total_item_count, model_->item_count());
592 EXPECT_EQ(last_visible_item_id_in_shelf,
593 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
594 EXPECT_EQ(second_last_visible_item_id_in_shelf,
595 GetItemId(test_api_->GetLastVisibleIndex()));
596 EXPECT_EQ(first_visible_item_id_in_overflow,
597 GetItemId(test_api_for_overflow.GetFirstVisibleIndex() + 1));
598 EXPECT_EQ(second_last_visible_item_id_in_overflow,
599 GetItemId(test_api_for_overflow.GetLastVisibleIndex()));
603 // Returns the item's ShelfID at |index|.
604 ShelfID GetItemId(int index) {
605 DCHECK_GE(index, 0);
606 return model_->items()[index].id;
609 void ReplaceShelfDelegateForRipOffTest() {
610 // Replace ShelfDelegate.
611 test::ShellTestApi test_api(Shell::GetInstance());
612 test_api.SetShelfDelegate(NULL);
613 ShelfDelegate* delegate = new TestShelfDelegateForShelfView(model_);
614 test_api.SetShelfDelegate(delegate);
615 test::ShelfTestAPI(Shelf::ForPrimaryDisplay()).SetShelfDelegate(delegate);
616 test_api_->SetShelfDelegate(delegate);
619 ShelfModel* model_;
620 ShelfView* shelf_view_;
621 int browser_index_;
622 ShelfItemDelegateManager* item_manager_;
624 scoped_ptr<ShelfViewTestAPI> test_api_;
626 private:
627 DISALLOW_COPY_AND_ASSIGN(ShelfViewTest);
630 class ScopedTextDirectionChange {
631 public:
632 explicit ScopedTextDirectionChange(bool is_rtl) : is_rtl_(is_rtl) {
633 original_locale_ = l10n_util::GetApplicationLocale(std::string());
634 if (is_rtl_)
635 base::i18n::SetICUDefaultLocale("he");
636 CheckTextDirectionIsCorrect();
639 ~ScopedTextDirectionChange() {
640 if (is_rtl_)
641 base::i18n::SetICUDefaultLocale(original_locale_);
644 private:
645 void CheckTextDirectionIsCorrect() {
646 ASSERT_EQ(is_rtl_, base::i18n::IsRTL());
649 bool is_rtl_;
650 std::string original_locale_;
653 class ShelfViewTextDirectionTest
654 : public ShelfViewTest,
655 public testing::WithParamInterface<bool> {
656 public:
657 ShelfViewTextDirectionTest() : text_direction_change_(GetParam()) {}
658 virtual ~ShelfViewTextDirectionTest() {}
660 void SetUp() override { ShelfViewTest::SetUp(); }
662 void TearDown() override { ShelfViewTest::TearDown(); }
664 private:
665 ScopedTextDirectionChange text_direction_change_;
667 DISALLOW_COPY_AND_ASSIGN(ShelfViewTextDirectionTest);
670 // Checks that the ideal item icon bounds match the view's bounds in the screen
671 // in both LTR and RTL.
672 TEST_P(ShelfViewTextDirectionTest, IdealBoundsOfItemIcon) {
673 ShelfID id = AddPlatformApp();
674 ShelfButton* button = GetButtonByID(id);
675 gfx::Rect item_bounds = button->GetBoundsInScreen();
676 gfx::Point icon_offset = button->GetIconBounds().origin();
677 item_bounds.Offset(icon_offset.OffsetFromOrigin());
678 gfx::Rect ideal_bounds = shelf_view_->GetIdealBoundsOfItemIcon(id);
679 gfx::Point screen_origin;
680 views::View::ConvertPointToScreen(shelf_view_, &screen_origin);
681 ideal_bounds.Offset(screen_origin.x(), screen_origin.y());
682 EXPECT_EQ(item_bounds.x(), ideal_bounds.x());
683 EXPECT_EQ(item_bounds.y(), ideal_bounds.y());
686 // Check that items in the overflow area are returning the overflow button as
687 // ideal bounds.
688 TEST_F(ShelfViewTest, OverflowButtonBounds) {
689 ShelfID first_id = AddPlatformApp();
690 ShelfID overflow_id = AddPlatformApp();
691 int items_added = 0;
692 while (!test_api_->IsOverflowButtonVisible()) {
693 // Added button is visible after animation while in this loop.
694 EXPECT_TRUE(GetButtonByID(overflow_id)->visible());
695 overflow_id = AddPlatformApp();
696 ++items_added;
697 ASSERT_LT(items_added, 10000);
699 ShelfID last_id = AddPlatformApp();
701 gfx::Rect first_bounds = shelf_view_->GetIdealBoundsOfItemIcon(first_id);
702 gfx::Rect overflow_bounds =
703 shelf_view_->GetIdealBoundsOfItemIcon(overflow_id);
704 gfx::Rect last_bounds = shelf_view_->GetIdealBoundsOfItemIcon(last_id);
706 // Check that all items have the same size and that the overflow items are
707 // identical whereas the first one does not match either of them.
708 EXPECT_EQ(first_bounds.size().ToString(), last_bounds.size().ToString());
709 EXPECT_NE(first_bounds.ToString(), last_bounds.ToString());
710 EXPECT_EQ(overflow_bounds.ToString(), last_bounds.ToString());
713 // Checks that shelf view contents are considered in the correct drag group.
714 TEST_F(ShelfViewTest, EnforceDragType) {
715 EXPECT_TRUE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_PLATFORM_APP));
716 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_SHORTCUT));
717 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP,
718 TYPE_BROWSER_SHORTCUT));
719 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_WINDOWED_APP));
720 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_LIST));
721 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_PANEL));
723 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_SHORTCUT));
724 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
725 TYPE_BROWSER_SHORTCUT));
726 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
727 TYPE_WINDOWED_APP));
728 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_LIST));
729 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_PANEL));
731 EXPECT_TRUE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
732 TYPE_BROWSER_SHORTCUT));
733 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
734 TYPE_WINDOWED_APP));
735 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_LIST));
736 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_PANEL));
738 EXPECT_TRUE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_WINDOWED_APP));
739 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_LIST));
740 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_PANEL));
742 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_LIST));
743 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_PANEL));
745 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_PANEL, TYPE_APP_PANEL));
748 // Adds platform app button until overflow and verifies that the last added
749 // platform app button is hidden.
750 TEST_F(ShelfViewTest, AddBrowserUntilOverflow) {
751 // All buttons should be visible.
752 ASSERT_EQ(test_api_->GetButtonCount(),
753 test_api_->GetLastVisibleIndex() + 1);
755 // Add platform app button until overflow.
756 int items_added = 0;
757 ShelfID last_added = AddPlatformApp();
758 while (!test_api_->IsOverflowButtonVisible()) {
759 // Added button is visible after animation while in this loop.
760 EXPECT_TRUE(GetButtonByID(last_added)->visible());
762 last_added = AddPlatformApp();
763 ++items_added;
764 ASSERT_LT(items_added, 10000);
767 // The last added button should be invisible.
768 EXPECT_FALSE(GetButtonByID(last_added)->visible());
771 // Adds one platform app button then adds app shortcut until overflow. Verifies
772 // that the browser button gets hidden on overflow and last added app shortcut
773 // is still visible.
774 TEST_F(ShelfViewTest, AddAppShortcutWithBrowserButtonUntilOverflow) {
775 // All buttons should be visible.
776 ASSERT_EQ(test_api_->GetButtonCount(),
777 test_api_->GetLastVisibleIndex() + 1);
779 ShelfID browser_button_id = AddPlatformApp();
781 // Add app shortcut until overflow.
782 int items_added = 0;
783 ShelfID last_added = AddAppShortcut();
784 while (!test_api_->IsOverflowButtonVisible()) {
785 // Added button is visible after animation while in this loop.
786 EXPECT_TRUE(GetButtonByID(last_added)->visible());
788 last_added = AddAppShortcut();
789 ++items_added;
790 ASSERT_LT(items_added, 10000);
793 // And the platform app button is invisible.
794 EXPECT_FALSE(GetButtonByID(browser_button_id)->visible());
797 TEST_F(ShelfViewTest, AddPanelHidesPlatformAppButton) {
798 // All buttons should be visible.
799 ASSERT_EQ(test_api_->GetButtonCount(),
800 test_api_->GetLastVisibleIndex() + 1);
802 // Add platform app button until overflow, remember last visible platform app
803 // button.
804 int items_added = 0;
805 ShelfID first_added = AddPlatformApp();
806 EXPECT_TRUE(GetButtonByID(first_added)->visible());
807 while (true) {
808 ShelfID added = AddPlatformApp();
809 if (test_api_->IsOverflowButtonVisible()) {
810 EXPECT_FALSE(GetButtonByID(added)->visible());
811 RemoveByID(added);
812 break;
814 ++items_added;
815 ASSERT_LT(items_added, 10000);
818 ShelfID panel = AddPanel();
819 EXPECT_TRUE(test_api_->IsOverflowButtonVisible());
821 RemoveByID(panel);
822 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
825 // When there are more panels then platform app buttons we should hide panels
826 // rather than platform apps.
827 TEST_F(ShelfViewTest, PlatformAppHidesExcessPanels) {
828 // All buttons should be visible.
829 ASSERT_EQ(test_api_->GetButtonCount(),
830 test_api_->GetLastVisibleIndex() + 1);
832 // Add platform app button.
833 ShelfID platform_app = AddPlatformApp();
834 ShelfID first_panel = AddPanel();
836 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
837 EXPECT_TRUE(GetButtonByID(first_panel)->visible());
839 // Add panels until there is an overflow.
840 ShelfID last_panel = first_panel;
841 int items_added = 0;
842 while (!test_api_->IsOverflowButtonVisible()) {
843 last_panel = AddPanel();
844 ++items_added;
845 ASSERT_LT(items_added, 10000);
848 // The first panel should now be hidden by the new platform apps needing
849 // space.
850 EXPECT_FALSE(GetButtonByID(first_panel)->visible());
851 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
852 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
854 // Adding platform apps should eventually begin to hide platform apps. We will
855 // add platform apps until either the last panel or platform app is hidden.
856 items_added = 0;
857 while (GetButtonByID(platform_app)->visible() &&
858 GetButtonByID(last_panel)->visible()) {
859 platform_app = AddPlatformApp();
860 ++items_added;
861 ASSERT_LT(items_added, 10000);
863 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
864 EXPECT_FALSE(GetButtonByID(platform_app)->visible());
867 // Adds button until overflow then removes first added one. Verifies that
868 // the last added one changes from invisible to visible and overflow
869 // chevron is gone.
870 TEST_F(ShelfViewTest, RemoveButtonRevealsOverflowed) {
871 // All buttons should be visible.
872 ASSERT_EQ(test_api_->GetButtonCount(),
873 test_api_->GetLastVisibleIndex() + 1);
875 // Add platform app buttons until overflow.
876 int items_added = 0;
877 ShelfID first_added = AddPlatformApp();
878 ShelfID last_added = first_added;
879 while (!test_api_->IsOverflowButtonVisible()) {
880 last_added = AddPlatformApp();
881 ++items_added;
882 ASSERT_LT(items_added, 10000);
885 // Expect add more than 1 button. First added is visible and last is not.
886 EXPECT_NE(first_added, last_added);
887 EXPECT_TRUE(GetButtonByID(first_added)->visible());
888 EXPECT_FALSE(GetButtonByID(last_added)->visible());
890 // Remove first added.
891 RemoveByID(first_added);
893 // Last added button becomes visible and overflow chevron is gone.
894 EXPECT_TRUE(GetButtonByID(last_added)->visible());
895 EXPECT_EQ(1.0f, GetButtonByID(last_added)->layer()->opacity());
896 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
899 // Verifies that remove last overflowed button should hide overflow chevron.
900 TEST_F(ShelfViewTest, RemoveLastOverflowed) {
901 // All buttons should be visible.
902 ASSERT_EQ(test_api_->GetButtonCount(),
903 test_api_->GetLastVisibleIndex() + 1);
905 // Add platform app button until overflow.
906 int items_added = 0;
907 ShelfID last_added = AddPlatformApp();
908 while (!test_api_->IsOverflowButtonVisible()) {
909 last_added = AddPlatformApp();
910 ++items_added;
911 ASSERT_LT(items_added, 10000);
914 RemoveByID(last_added);
915 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
918 // Adds platform app button without waiting for animation to finish and verifies
919 // that all added buttons are visible.
920 TEST_F(ShelfViewTest, AddButtonQuickly) {
921 // All buttons should be visible.
922 ASSERT_EQ(test_api_->GetButtonCount(),
923 test_api_->GetLastVisibleIndex() + 1);
925 // Add a few platform buttons quickly without wait for animation.
926 int added_count = 0;
927 while (!test_api_->IsOverflowButtonVisible()) {
928 AddPlatformAppNoWait();
929 ++added_count;
930 ASSERT_LT(added_count, 10000);
933 // ShelfView should be big enough to hold at least 3 new buttons.
934 ASSERT_GE(added_count, 3);
936 // Wait for the last animation to finish.
937 test_api_->RunMessageLoopUntilAnimationsDone();
939 // Verifies non-overflow buttons are visible.
940 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
941 ShelfButton* button = test_api_->GetButton(i);
942 if (button) {
943 EXPECT_TRUE(button->visible()) << "button index=" << i;
944 EXPECT_EQ(1.0f, button->layer()->opacity()) << "button index=" << i;
949 // Check that model changes are handled correctly while a shelf icon is being
950 // dragged.
951 TEST_F(ShelfViewTest, ModelChangesWhileDragging) {
952 ShelfButtonHost* button_host = shelf_view_;
954 std::vector<std::pair<ShelfID, views::View*> > id_map;
955 SetupForDragTest(&id_map);
957 // Dragging browser shortcut at index 1.
958 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
959 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
960 std::rotate(id_map.begin() + 1,
961 id_map.begin() + 2,
962 id_map.begin() + 4);
963 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
964 button_host->PointerReleasedOnButton(
965 dragged_button, ShelfButtonHost::MOUSE, false);
966 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
968 // Dragging changes model order.
969 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
970 std::rotate(id_map.begin() + 1,
971 id_map.begin() + 2,
972 id_map.begin() + 4);
973 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
975 // Cancelling the drag operation restores previous order.
976 button_host->PointerReleasedOnButton(
977 dragged_button, ShelfButtonHost::MOUSE, true);
978 std::rotate(id_map.begin() + 1,
979 id_map.begin() + 3,
980 id_map.begin() + 4);
981 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
983 // Deleting an item keeps the remaining intact.
984 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
985 model_->RemoveItemAt(1);
986 id_map.erase(id_map.begin() + 1);
987 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
988 button_host->PointerReleasedOnButton(
989 dragged_button, ShelfButtonHost::MOUSE, false);
991 // Adding a shelf item cancels the drag and respects the order.
992 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
993 ShelfID new_id = AddAppShortcut();
994 id_map.insert(id_map.begin() + 6,
995 std::make_pair(new_id, GetButtonByID(new_id)));
996 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
997 button_host->PointerReleasedOnButton(
998 dragged_button, ShelfButtonHost::MOUSE, false);
1000 // Adding a shelf item at the end (i.e. a panel) canels drag and respects
1001 // the order.
1002 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1003 new_id = AddPanel();
1004 id_map.insert(id_map.begin() + 7,
1005 std::make_pair(new_id, GetButtonByID(new_id)));
1006 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1007 button_host->PointerReleasedOnButton(
1008 dragged_button, ShelfButtonHost::MOUSE, false);
1011 // Check that 2nd drag from the other pointer would be ignored.
1012 TEST_F(ShelfViewTest, SimultaneousDrag) {
1013 ShelfButtonHost* button_host = shelf_view_;
1015 std::vector<std::pair<ShelfID, views::View*> > id_map;
1016 SetupForDragTest(&id_map);
1018 // Start a mouse drag.
1019 views::View* dragged_button_mouse =
1020 SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1021 std::rotate(id_map.begin() + 1,
1022 id_map.begin() + 2,
1023 id_map.begin() + 4);
1024 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1025 // Attempt a touch drag before the mouse drag finishes.
1026 views::View* dragged_button_touch =
1027 SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
1029 // Nothing changes since 2nd drag is ignored.
1030 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1032 // Finish the mouse drag.
1033 button_host->PointerReleasedOnButton(
1034 dragged_button_mouse, ShelfButtonHost::MOUSE, false);
1035 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1037 // Now start a touch drag.
1038 dragged_button_touch = SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
1039 std::rotate(id_map.begin() + 3,
1040 id_map.begin() + 4,
1041 id_map.begin() + 5);
1042 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1044 // And attempt a mouse drag before the touch drag finishes.
1045 dragged_button_mouse = SimulateDrag(ShelfButtonHost::MOUSE, 1, 2);
1047 // Nothing changes since 2nd drag is ignored.
1048 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1050 button_host->PointerReleasedOnButton(
1051 dragged_button_touch, ShelfButtonHost::TOUCH, false);
1052 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1055 // Check that clicking first on one item and then dragging another works as
1056 // expected.
1057 TEST_F(ShelfViewTest, ClickOneDragAnother) {
1058 ShelfButtonHost* button_host = shelf_view_;
1060 std::vector<std::pair<ShelfID, views::View*> > id_map;
1061 SetupForDragTest(&id_map);
1063 // A click on item 1 is simulated.
1064 SimulateClick(1);
1066 // Dragging browser index at 0 should change the model order correctly.
1067 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
1068 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1069 std::rotate(id_map.begin() + 1,
1070 id_map.begin() + 2,
1071 id_map.begin() + 4);
1072 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1073 button_host->PointerReleasedOnButton(
1074 dragged_button, ShelfButtonHost::MOUSE, false);
1075 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
1078 // Tests that double-clicking an item does not activate it twice.
1079 TEST_F(ShelfViewTest, ClickingTwiceActivatesOnce) {
1080 // Watch for selection of the browser shortcut.
1081 ShelfID browser_shelf_id = model_->items()[browser_index_].id;
1082 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1083 item_manager_->SetShelfItemDelegate(
1084 browser_shelf_id,
1085 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1087 // A single click selects the item.
1088 SimulateClick(browser_index_);
1089 EXPECT_TRUE(selection_tracker->WasSelected());
1091 // A double-click does not select the item.
1092 selection_tracker->Reset();
1093 SimulateDoubleClick(browser_index_);
1094 EXPECT_FALSE(selection_tracker->WasSelected());
1097 // Check that clicking an item and jittering the mouse a bit still selects the
1098 // item.
1099 TEST_F(ShelfViewTest, ClickAndMoveSlightly) {
1100 std::vector<std::pair<ShelfID, views::View*> > id_map;
1101 SetupForDragTest(&id_map);
1103 ShelfID shelf_id = (id_map.begin() + 1)->first;
1104 views::View* button = (id_map.begin() + 1)->second;
1106 // Replace the ShelfItemDelegate for |shelf_id| with one which tracks whether
1107 // the shelf item gets selected.
1108 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1109 item_manager_->SetShelfItemDelegate(
1110 shelf_id,
1111 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1113 gfx::Vector2d press_offset(5, 30);
1114 gfx::Point press_location = gfx::Point() + press_offset;
1115 gfx::Point press_location_in_screen =
1116 button->GetBoundsInScreen().origin() + press_offset;
1118 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED,
1119 press_location,
1120 press_location_in_screen,
1121 ui::EF_LEFT_MOUSE_BUTTON, 0);
1122 button->OnMousePressed(click_event);
1124 ui::MouseEvent drag_event1(ui::ET_MOUSE_DRAGGED,
1125 press_location + gfx::Vector2d(0, 1),
1126 press_location_in_screen + gfx::Vector2d(0, 1),
1127 ui::EF_LEFT_MOUSE_BUTTON, 0);
1128 button->OnMouseDragged(drag_event1);
1130 ui::MouseEvent drag_event2(ui::ET_MOUSE_DRAGGED,
1131 press_location + gfx::Vector2d(-1, 0),
1132 press_location_in_screen + gfx::Vector2d(-1, 0),
1133 ui::EF_LEFT_MOUSE_BUTTON, 0);
1134 button->OnMouseDragged(drag_event2);
1136 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED,
1137 press_location + gfx::Vector2d(-1, 0),
1138 press_location_in_screen + gfx::Vector2d(-1, 0),
1139 ui::EF_LEFT_MOUSE_BUTTON, 0);
1140 button->OnMouseReleased(release_event);
1142 EXPECT_TRUE(selection_tracker->WasSelected());
1145 // Confirm that item status changes are reflected in the buttons.
1146 TEST_F(ShelfViewTest, ShelfItemStatus) {
1147 // All buttons should be visible.
1148 ASSERT_EQ(test_api_->GetButtonCount(),
1149 test_api_->GetLastVisibleIndex() + 1);
1151 // Add platform app button.
1152 ShelfID last_added = AddPlatformApp();
1153 ShelfItem item = GetItemByID(last_added);
1154 int index = model_->ItemIndexByID(last_added);
1155 ShelfButton* button = GetButtonByID(last_added);
1156 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1157 item.status = STATUS_ACTIVE;
1158 model_->Set(index, item);
1159 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1160 item.status = STATUS_ATTENTION;
1161 model_->Set(index, item);
1162 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1165 // Confirm that item status changes are reflected in the buttons
1166 // for platform apps.
1167 TEST_F(ShelfViewTest, ShelfItemStatusPlatformApp) {
1168 // All buttons should be visible.
1169 ASSERT_EQ(test_api_->GetButtonCount(),
1170 test_api_->GetLastVisibleIndex() + 1);
1172 // Add platform app button.
1173 ShelfID last_added = AddPlatformApp();
1174 ShelfItem item = GetItemByID(last_added);
1175 int index = model_->ItemIndexByID(last_added);
1176 ShelfButton* button = GetButtonByID(last_added);
1177 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1178 item.status = STATUS_ACTIVE;
1179 model_->Set(index, item);
1180 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1181 item.status = STATUS_ATTENTION;
1182 model_->Set(index, item);
1183 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1186 // Confirm that shelf item bounds are correctly updated on shelf changes.
1187 TEST_F(ShelfViewTest, ShelfItemBoundsCheck) {
1188 VerifyShelfItemBoundsAreValid();
1189 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1190 SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
1191 test_api_->RunMessageLoopUntilAnimationsDone();
1192 VerifyShelfItemBoundsAreValid();
1193 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1194 SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
1195 test_api_->RunMessageLoopUntilAnimationsDone();
1196 VerifyShelfItemBoundsAreValid();
1199 TEST_F(ShelfViewTest, ShelfTooltipTest) {
1200 ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
1201 test_api_->GetButtonCount());
1203 // Prepare some items to the shelf.
1204 ShelfID app_button_id = AddAppShortcut();
1205 ShelfID platform_button_id = AddPlatformApp();
1207 ShelfButton* app_button = GetButtonByID(app_button_id);
1208 ShelfButton* platform_button = GetButtonByID(platform_button_id);
1210 ShelfButtonHost* button_host = shelf_view_;
1211 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1213 button_host->MouseEnteredButton(app_button);
1214 // There's a delay to show the tooltip, so it's not visible yet.
1215 EXPECT_FALSE(tooltip_manager->IsVisible());
1216 EXPECT_EQ(app_button, GetTooltipAnchorView());
1218 ShowTooltip();
1219 EXPECT_TRUE(tooltip_manager->IsVisible());
1221 // Once it's visible, it keeps visibility and is pointing to the same
1222 // item.
1223 button_host->MouseExitedButton(app_button);
1224 EXPECT_TRUE(tooltip_manager->IsVisible());
1225 EXPECT_EQ(app_button, GetTooltipAnchorView());
1227 // When entered to another item, it switches to the new item. There is no
1228 // delay for the visibility.
1229 button_host->MouseEnteredButton(platform_button);
1230 EXPECT_TRUE(tooltip_manager->IsVisible());
1231 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1233 button_host->MouseExitedButton(platform_button);
1234 tooltip_manager->Close();
1236 // Next time: enter app_button -> move immediately to tab_button.
1237 button_host->MouseEnteredButton(app_button);
1238 button_host->MouseExitedButton(app_button);
1239 button_host->MouseEnteredButton(platform_button);
1240 EXPECT_FALSE(tooltip_manager->IsVisible());
1241 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1244 // Verify a fix for crash caused by a tooltip update for a deletedshelf
1245 // button, see crbug.com/288838.
1246 TEST_F(ShelfViewTest, RemovingItemClosesTooltip) {
1247 ShelfButtonHost* button_host = shelf_view_;
1248 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1250 // Add an item to the shelf.
1251 ShelfID app_button_id = AddAppShortcut();
1252 ShelfButton* app_button = GetButtonByID(app_button_id);
1254 // Spawn a tooltip on that item.
1255 button_host->MouseEnteredButton(app_button);
1256 ShowTooltip();
1257 EXPECT_TRUE(tooltip_manager->IsVisible());
1259 // Remove the app shortcut while the tooltip is open. The tooltip should be
1260 // closed.
1261 RemoveByID(app_button_id);
1262 EXPECT_FALSE(tooltip_manager->IsVisible());
1264 // Change the shelf layout. This should not crash.
1265 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1266 Shell::GetPrimaryRootWindow());
1269 // Changing the shelf alignment closes any open tooltip.
1270 TEST_F(ShelfViewTest, ShelfAlignmentClosesTooltip) {
1271 ShelfButtonHost* button_host = shelf_view_;
1272 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1274 // Add an item to the shelf.
1275 ShelfID app_button_id = AddAppShortcut();
1276 ShelfButton* app_button = GetButtonByID(app_button_id);
1278 // Spawn a tooltip on the item.
1279 button_host->MouseEnteredButton(app_button);
1280 ShowTooltip();
1281 EXPECT_TRUE(tooltip_manager->IsVisible());
1283 // Changing shelf alignment hides the tooltip.
1284 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1285 Shell::GetPrimaryRootWindow());
1286 EXPECT_FALSE(tooltip_manager->IsVisible());
1289 TEST_F(ShelfViewTest, ShouldHideTooltipTest) {
1290 ShelfID app_button_id = AddAppShortcut();
1291 ShelfID platform_button_id = AddPlatformApp();
1293 // The tooltip shouldn't hide if the mouse is on normal buttons.
1294 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1295 ShelfButton* button = test_api_->GetButton(i);
1296 if (!button)
1297 continue;
1299 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1300 button->GetMirroredBounds().CenterPoint()))
1301 << "ShelfView tries to hide on button " << i;
1304 // The tooltip should not hide on the app-list button.
1305 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1306 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1307 app_list_button->GetMirroredBounds().CenterPoint()));
1309 // The tooltip shouldn't hide if the mouse is in the gap between two buttons.
1310 gfx::Rect app_button_rect = GetButtonByID(app_button_id)->GetMirroredBounds();
1311 gfx::Rect platform_button_rect =
1312 GetButtonByID(platform_button_id)->GetMirroredBounds();
1313 ASSERT_FALSE(app_button_rect.Intersects(platform_button_rect));
1314 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1315 gfx::UnionRects(app_button_rect, platform_button_rect).CenterPoint()));
1317 // The tooltip should hide if it's outside of all buttons.
1318 gfx::Rect all_area;
1319 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1320 ShelfButton* button = test_api_->GetButton(i);
1321 if (!button)
1322 continue;
1324 all_area.Union(button->GetMirroredBounds());
1326 all_area.Union(shelf_view_->GetAppListButtonView()->GetMirroredBounds());
1327 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(all_area.origin()));
1328 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1329 gfx::Point(all_area.right() - 1, all_area.bottom() - 1)));
1330 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1331 gfx::Point(all_area.right(), all_area.y())));
1332 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1333 gfx::Point(all_area.x() - 1, all_area.y())));
1334 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1335 gfx::Point(all_area.x(), all_area.y() - 1)));
1336 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1337 gfx::Point(all_area.x(), all_area.bottom())));
1340 TEST_F(ShelfViewTest, ShouldHideTooltipWithAppListWindowTest) {
1341 Shell::GetInstance()->ShowAppList(NULL);
1342 ASSERT_TRUE(Shell::GetInstance()->GetAppListWindow());
1344 // The tooltip shouldn't hide if the mouse is on normal buttons.
1345 for (int i = 1; i < test_api_->GetButtonCount(); i++) {
1346 ShelfButton* button = test_api_->GetButton(i);
1347 if (!button)
1348 continue;
1350 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1351 button->GetMirroredBounds().CenterPoint()))
1352 << "ShelfView tries to hide on button " << i;
1355 // The tooltip should hide on the app-list button.
1356 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1357 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1358 app_list_button->GetMirroredBounds().CenterPoint()));
1361 // Test that by moving the mouse cursor off the button onto the bubble it closes
1362 // the bubble.
1363 TEST_F(ShelfViewTest, ShouldHideTooltipWhenHoveringOnTooltip) {
1364 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1365 tooltip_manager->CreateZeroDelayTimerForTest();
1366 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1368 // Move the mouse off any item and check that no tooltip is shown.
1369 generator.MoveMouseTo(gfx::Point(0, 0));
1370 EXPECT_FALSE(tooltip_manager->IsVisible());
1372 // Move the mouse over the button and check that it is visible.
1373 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1374 gfx::Rect bounds = app_list_button->GetBoundsInScreen();
1375 generator.MoveMouseTo(bounds.CenterPoint());
1376 // Wait for the timer to go off.
1377 RunAllPendingInMessageLoop();
1378 EXPECT_TRUE(tooltip_manager->IsVisible());
1380 // Move the mouse cursor slightly to the right of the item. The tooltip should
1381 // stay open.
1382 generator.MoveMouseBy(bounds.width() / 2 + 5, 0);
1383 // Make sure there is no delayed close.
1384 RunAllPendingInMessageLoop();
1385 EXPECT_TRUE(tooltip_manager->IsVisible());
1387 // Move back - it should still stay open.
1388 generator.MoveMouseBy(-(bounds.width() / 2 + 5), 0);
1389 // Make sure there is no delayed close.
1390 RunAllPendingInMessageLoop();
1391 EXPECT_TRUE(tooltip_manager->IsVisible());
1393 // Now move the mouse cursor slightly above the item - so that it is over the
1394 // tooltip bubble. Now it should disappear.
1395 generator.MoveMouseBy(0, -(bounds.height() / 2 + 5));
1396 // Wait until the delayed close kicked in.
1397 RunAllPendingInMessageLoop();
1398 EXPECT_FALSE(tooltip_manager->IsVisible());
1401 // Resizing shelf view while an add animation without fade-in is running,
1402 // which happens when overflow happens. App list button should end up in its
1403 // new ideal bounds.
1404 TEST_F(ShelfViewTest, ResizeDuringOverflowAddAnimation) {
1405 // All buttons should be visible.
1406 ASSERT_EQ(test_api_->GetButtonCount(),
1407 test_api_->GetLastVisibleIndex() + 1);
1409 // Add buttons until overflow. Let the non-overflow add animations finish but
1410 // leave the last running.
1411 int items_added = 0;
1412 AddPlatformAppNoWait();
1413 while (!test_api_->IsOverflowButtonVisible()) {
1414 test_api_->RunMessageLoopUntilAnimationsDone();
1415 AddPlatformAppNoWait();
1416 ++items_added;
1417 ASSERT_LT(items_added, 10000);
1420 // Resize shelf view with that animation running and stay overflown.
1421 gfx::Rect bounds = shelf_view_->bounds();
1422 bounds.set_width(bounds.width() - kShelfSize);
1423 shelf_view_->SetBoundsRect(bounds);
1424 ASSERT_TRUE(test_api_->IsOverflowButtonVisible());
1426 // Finish the animation.
1427 test_api_->RunMessageLoopUntilAnimationsDone();
1429 // App list button should ends up in its new ideal bounds.
1430 const int app_list_button_index = test_api_->GetButtonCount() - 1;
1431 const gfx::Rect& app_list_ideal_bounds =
1432 test_api_->GetIdealBoundsByIndex(app_list_button_index);
1433 const gfx::Rect& app_list_bounds =
1434 test_api_->GetBoundsByIndex(app_list_button_index);
1435 EXPECT_EQ(app_list_ideal_bounds, app_list_bounds);
1438 // Checks the overflow bubble size when an item is ripped off and re-inserted.
1439 TEST_F(ShelfViewTest, OverflowBubbleSize) {
1440 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1441 ReplaceShelfDelegateForRipOffTest();
1443 AddButtonsUntilOverflow();
1445 // Show overflow bubble.
1446 test_api_->ShowOverflowBubble();
1447 ASSERT_TRUE(test_api_->overflow_bubble() &&
1448 test_api_->overflow_bubble()->IsShowing());
1450 ShelfViewTestAPI test_for_overflow_view(
1451 test_api_->overflow_bubble()->shelf_view());
1453 int ripped_index = test_for_overflow_view.GetLastVisibleIndex();
1454 gfx::Size bubble_size = test_for_overflow_view.GetPreferredSize();
1455 int item_width = test_for_overflow_view.GetButtonSize() +
1456 test_for_overflow_view.GetButtonSpacing();
1458 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
1459 gfx::Point());
1460 ShelfButton* button = test_for_overflow_view.GetButton(ripped_index);
1461 // Rip off the last visible item.
1462 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1463 gfx::Point rip_off_point(start_point.x(), 0);
1464 generator.MoveMouseTo(start_point.x(), start_point.y());
1465 base::MessageLoop::current()->RunUntilIdle();
1466 generator.PressLeftButton();
1467 base::MessageLoop::current()->RunUntilIdle();
1468 generator.MoveMouseTo(rip_off_point.x(), rip_off_point.y());
1469 base::MessageLoop::current()->RunUntilIdle();
1470 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1472 // Check the overflow bubble size when an item is ripped off.
1473 EXPECT_EQ(bubble_size.width() - item_width,
1474 test_for_overflow_view.GetPreferredSize().width());
1475 ASSERT_TRUE(test_api_->overflow_bubble() &&
1476 test_api_->overflow_bubble()->IsShowing());
1478 // Re-insert an item into the overflow bubble.
1479 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1480 button = test_for_overflow_view.GetButton(first_index);
1482 // Check the bubble size after an item is re-inserted.
1483 generator.MoveMouseTo(button->GetBoundsInScreen().CenterPoint());
1484 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1485 EXPECT_EQ(bubble_size.width(),
1486 test_for_overflow_view.GetPreferredSize().width());
1488 generator.ReleaseLeftButton();
1489 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1490 EXPECT_EQ(bubble_size.width(),
1491 test_for_overflow_view.GetPreferredSize().width());
1494 // Check the drag insertion bounds of scrolled overflow bubble.
1495 TEST_F(ShelfViewTest, CheckDragInsertBoundsOfScrolledOverflowBubble) {
1496 UpdateDisplay("400x300");
1498 EXPECT_EQ(2, model_->item_count());
1500 AddButtonsUntilOverflow();
1502 // Show overflow bubble.
1503 test_api_->ShowOverflowBubble();
1504 ASSERT_TRUE(test_api_->overflow_bubble() &&
1505 test_api_->overflow_bubble()->IsShowing());
1507 int item_width = test_api_->GetButtonSize() +
1508 test_api_->GetButtonSpacing();
1509 OverflowBubbleView* bubble_view = test_api_->overflow_bubble()->bubble_view();
1510 test::OverflowBubbleViewTestAPI bubble_view_api(bubble_view);
1512 // Add more buttons until OverflowBubble is scrollable and it has 3 invisible
1513 // items.
1514 while (bubble_view_api.GetContentsSize().width() <
1515 (bubble_view->GetContentsBounds().width() + 3 * item_width))
1516 AddAppShortcut();
1518 ASSERT_TRUE(test_api_->overflow_bubble() &&
1519 test_api_->overflow_bubble()->IsShowing());
1521 ShelfViewTestAPI test_for_overflow_view(
1522 test_api_->overflow_bubble()->shelf_view());
1523 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1524 int last_index = test_for_overflow_view.GetLastVisibleIndex();
1526 ShelfButton* first_button = test_for_overflow_view.GetButton(first_index);
1527 ShelfButton* last_button = test_for_overflow_view.GetButton(last_index);
1528 gfx::Point first_point = first_button->GetBoundsInScreen().CenterPoint();
1529 gfx::Point last_point = last_button->GetBoundsInScreen().CenterPoint();
1530 gfx::Rect drag_reinsert_bounds =
1531 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1532 EXPECT_TRUE(drag_reinsert_bounds.Contains(first_point));
1533 EXPECT_FALSE(drag_reinsert_bounds.Contains(last_point));
1535 // Scrolls sufficiently to show last item.
1536 bubble_view_api.ScrollByXOffset(3 * item_width);
1537 drag_reinsert_bounds =
1538 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1539 first_point = first_button->GetBoundsInScreen().CenterPoint();
1540 last_point = last_button->GetBoundsInScreen().CenterPoint();
1541 EXPECT_FALSE(drag_reinsert_bounds.Contains(first_point));
1542 EXPECT_TRUE(drag_reinsert_bounds.Contains(last_point));
1545 // Check the drag insertion bounds of shelf view in multi monitor environment.
1546 TEST_F(ShelfViewTest, CheckDragInsertBoundsWithMultiMonitor) {
1547 // win8-aura doesn't support multiple display.
1548 if (!SupportsMultipleDisplays())
1549 return;
1551 UpdateDisplay("800x600,800x600");
1552 Shelf* secondary_shelf = Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
1553 ShelfView* shelf_view_for_secondary =
1554 ShelfTestAPI(secondary_shelf).shelf_view();
1556 // The bounds should be big enough for 4 buttons + overflow chevron.
1557 shelf_view_for_secondary->SetBounds(0, 0, 500, kShelfSize);
1559 ShelfViewTestAPI test_api_for_secondary(shelf_view_for_secondary);
1560 // Speeds up animation for test.
1561 test_api_for_secondary.SetAnimationDuration(1);
1563 AddButtonsUntilOverflow();
1565 // Test #1: Test drag insertion bounds of primary shelf.
1566 // Show overflow bubble.
1567 test_api_->ShowOverflowBubble();
1568 ASSERT_TRUE(test_api_->overflow_bubble() &&
1569 test_api_->overflow_bubble()->IsShowing());
1571 ShelfViewTestAPI test_api_for_overflow_view(
1572 test_api_->overflow_bubble()->shelf_view());
1574 ShelfButton* button = test_api_for_overflow_view.GetButton(
1575 test_api_for_overflow_view.GetLastVisibleIndex());
1577 // Checks that a point in shelf is contained in drag insert bounds.
1578 gfx::Point point_in_shelf_view = button->GetBoundsInScreen().CenterPoint();
1579 gfx::Rect drag_reinsert_bounds =
1580 test_api_for_overflow_view.GetBoundsForDragInsertInScreen();
1581 EXPECT_TRUE(drag_reinsert_bounds.Contains(point_in_shelf_view));
1582 // Checks that a point out of shelf is not contained in drag insert bounds.
1583 EXPECT_FALSE(drag_reinsert_bounds.Contains(
1584 gfx::Point(point_in_shelf_view.x(), 0)));
1586 // Test #2: Test drag insertion bounds of secondary shelf.
1587 // Show overflow bubble.
1588 test_api_for_secondary.ShowOverflowBubble();
1589 ASSERT_TRUE(test_api_for_secondary.overflow_bubble() &&
1590 test_api_for_secondary.overflow_bubble()->IsShowing());
1592 ShelfViewTestAPI test_api_for_overflow_view_of_secondary(
1593 test_api_for_secondary.overflow_bubble()->shelf_view());
1595 ShelfButton* button_in_secondary =
1596 test_api_for_overflow_view_of_secondary.GetButton(
1597 test_api_for_overflow_view_of_secondary.GetLastVisibleIndex());
1599 // Checks that a point in shelf is contained in drag insert bounds.
1600 gfx::Point point_in_secondary_shelf_view =
1601 button_in_secondary->GetBoundsInScreen().CenterPoint();
1602 gfx::Rect drag_reinsert_bounds_in_secondary =
1603 test_api_for_overflow_view_of_secondary.GetBoundsForDragInsertInScreen();
1604 EXPECT_TRUE(drag_reinsert_bounds_in_secondary.Contains(
1605 point_in_secondary_shelf_view));
1606 // Checks that a point out of shelf is not contained in drag insert bounds.
1607 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(
1608 gfx::Point(point_in_secondary_shelf_view.x(), 0)));
1609 // Checks that a point of overflow bubble in primary shelf should not be
1610 // contained by insert bounds of secondary shelf.
1611 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(point_in_shelf_view));
1614 // Checks the rip an item off from left aligned shelf in secondary monitor.
1615 TEST_F(ShelfViewTest, CheckRipOffFromLeftShelfAlignmentWithMultiMonitor) {
1616 // win8-aura doesn't support multiple display.
1617 if (!SupportsMultipleDisplays())
1618 return;
1620 UpdateDisplay("800x600,800x600");
1621 ASSERT_EQ(2U, Shell::GetAllRootWindows().size());
1623 aura::Window* second_root = Shell::GetAllRootWindows()[1];
1625 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT, second_root);
1626 ASSERT_EQ(SHELF_ALIGNMENT_LEFT,
1627 Shell::GetInstance()->GetShelfAlignment(second_root));
1629 // Initially, app list and browser shortcut are added.
1630 EXPECT_EQ(2, model_->item_count());
1631 int browser_index = model_->GetItemIndexForType(TYPE_BROWSER_SHORTCUT);
1632 EXPECT_GT(browser_index, 0);
1634 Shelf* secondary_shelf = Shelf::ForWindow(second_root);
1635 ShelfView* shelf_view_for_secondary =
1636 ShelfTestAPI(secondary_shelf).shelf_view();
1638 ShelfViewTestAPI test_api_for_secondary_shelf_view(shelf_view_for_secondary);
1639 ShelfButton* button =
1640 test_api_for_secondary_shelf_view.GetButton(browser_index);
1642 // Fetch the start point of dragging.
1643 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1644 ::wm::ConvertPointFromScreen(second_root, &start_point);
1646 ui::test::EventGenerator generator(second_root, start_point);
1648 // Rip off the browser item.
1649 generator.PressLeftButton();
1650 generator.MoveMouseTo(start_point.x() + 400, start_point.y());
1651 test_api_for_secondary_shelf_view.RunMessageLoopUntilAnimationsDone();
1652 EXPECT_TRUE(test_api_for_secondary_shelf_view.IsRippedOffFromShelf());
1655 // Checks various drag and drop operations from OverflowBubble to Shelf.
1656 TEST_F(ShelfViewTest, CheckDragAndDropFromOverflowBubbleToShelf) {
1657 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1658 ReplaceShelfDelegateForRipOffTest();
1660 AddButtonsUntilOverflow();
1662 TestDraggingAnItemFromOverflowToShelf(false);
1663 TestDraggingAnItemFromOverflowToShelf(true);
1666 // Tests that the AppListButton renders as active in response to touches.
1667 TEST_F(ShelfViewTest, DISABLED_AppListButtonTouchFeedback) {
1668 AppListButton* app_list_button =
1669 static_cast<AppListButton*>(shelf_view_->GetAppListButtonView());
1670 EXPECT_FALSE(app_list_button->draw_background_as_active());
1672 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1673 generator.set_current_location(app_list_button->
1674 GetBoundsInScreen().CenterPoint());
1675 generator.PressTouch();
1676 RunAllPendingInMessageLoop();
1677 EXPECT_TRUE(app_list_button->draw_background_as_active());
1679 generator.ReleaseTouch();
1680 RunAllPendingInMessageLoop();
1681 EXPECT_FALSE(app_list_button->draw_background_as_active());
1682 EXPECT_TRUE(Shell::GetInstance()->GetAppListTargetVisibility());
1685 // Tests that a touch that slides out of the bounds of the AppListButton leads
1686 // to the end of rendering an active state.
1687 TEST_F(ShelfViewTest, DISABLED_AppListButtonTouchFeedbackCancellation) {
1688 AppListButton* app_list_button =
1689 static_cast<AppListButton*>(shelf_view_->GetAppListButtonView());
1690 EXPECT_FALSE(app_list_button->draw_background_as_active());
1692 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1693 generator.set_current_location(app_list_button->
1694 GetBoundsInScreen().CenterPoint());
1695 generator.PressTouch();
1696 RunAllPendingInMessageLoop();
1697 EXPECT_TRUE(app_list_button->draw_background_as_active());
1699 gfx::Point moved_point(app_list_button->GetBoundsInScreen().right() + 1,
1700 app_list_button->
1701 GetBoundsInScreen().CenterPoint().y());
1702 generator.MoveTouch(moved_point);
1703 RunAllPendingInMessageLoop();
1704 EXPECT_FALSE(app_list_button->draw_background_as_active());
1706 generator.set_current_location(moved_point);
1707 generator.ReleaseTouch();
1708 RunAllPendingInMessageLoop();
1709 EXPECT_FALSE(app_list_button->draw_background_as_active());
1710 EXPECT_FALSE(Shell::GetInstance()->GetAppListTargetVisibility());
1713 class ShelfViewVisibleBoundsTest : public ShelfViewTest,
1714 public testing::WithParamInterface<bool> {
1715 public:
1716 ShelfViewVisibleBoundsTest() : text_direction_change_(GetParam()) {}
1718 void CheckAllItemsAreInBounds() {
1719 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1720 gfx::Rect shelf_bounds = shelf_view_->GetBoundsInScreen();
1721 EXPECT_TRUE(shelf_bounds.Contains(visible_bounds));
1722 for (int i = 0; i < test_api_->GetButtonCount(); ++i)
1723 if (ShelfButton* button = test_api_->GetButton(i))
1724 EXPECT_TRUE(visible_bounds.Contains(button->GetBoundsInScreen()));
1725 CheckAppListButtonIsInBounds();
1728 void CheckAppListButtonIsInBounds() {
1729 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1730 gfx::Rect app_list_button_bounds = shelf_view_->GetAppListButtonView()->
1731 GetBoundsInScreen();
1732 EXPECT_TRUE(visible_bounds.Contains(app_list_button_bounds));
1735 private:
1736 ScopedTextDirectionChange text_direction_change_;
1738 DISALLOW_COPY_AND_ASSIGN(ShelfViewVisibleBoundsTest);
1741 TEST_P(ShelfViewVisibleBoundsTest, ItemsAreInBounds) {
1742 // Adding elements leaving some empty space.
1743 for (int i = 0; i < 3; i++) {
1744 AddAppShortcut();
1746 test_api_->RunMessageLoopUntilAnimationsDone();
1747 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
1748 CheckAllItemsAreInBounds();
1749 // Same for overflow case.
1750 while (!test_api_->IsOverflowButtonVisible()) {
1751 AddAppShortcut();
1753 test_api_->RunMessageLoopUntilAnimationsDone();
1754 CheckAllItemsAreInBounds();
1757 INSTANTIATE_TEST_CASE_P(LtrRtl, ShelfViewTextDirectionTest, testing::Bool());
1758 INSTANTIATE_TEST_CASE_P(VisibleBounds, ShelfViewVisibleBoundsTest,
1759 testing::Bool());
1761 } // namespace test
1762 } // namespace ash