Don't preload rarely seen large images
[chromium-blink-merge.git] / ash / shelf / shelf_view_unittest.cc
bloba74979adc60c7587c4f460140e11444b65644860
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 <utility>
9 #include <vector>
11 #include "ash/root_window_controller.h"
12 #include "ash/shelf/app_list_button.h"
13 #include "ash/shelf/overflow_bubble.h"
14 #include "ash/shelf/overflow_bubble_view.h"
15 #include "ash/shelf/shelf.h"
16 #include "ash/shelf/shelf_button.h"
17 #include "ash/shelf/shelf_constants.h"
18 #include "ash/shelf/shelf_icon_observer.h"
19 #include "ash/shelf/shelf_item_delegate_manager.h"
20 #include "ash/shelf/shelf_layout_manager.h"
21 #include "ash/shelf/shelf_model.h"
22 #include "ash/shelf/shelf_tooltip_manager.h"
23 #include "ash/shelf/shelf_widget.h"
24 #include "ash/shell.h"
25 #include "ash/shell_window_ids.h"
26 #include "ash/test/ash_test_base.h"
27 #include "ash/test/overflow_bubble_view_test_api.h"
28 #include "ash/test/shelf_test_api.h"
29 #include "ash/test/shelf_view_test_api.h"
30 #include "ash/test/shell_test_api.h"
31 #include "ash/test/test_shelf_delegate.h"
32 #include "ash/test/test_shelf_item_delegate.h"
33 #include "base/basictypes.h"
34 #include "base/compiler_specific.h"
35 #include "base/memory/scoped_ptr.h"
36 #include "base/strings/string_number_conversions.h"
37 #include "base/test/histogram_tester.h"
38 #include "base/test/user_action_tester.h"
39 #include "base/time/time.h"
40 #include "ui/aura/test/aura_test_base.h"
41 #include "ui/aura/window.h"
42 #include "ui/aura/window_event_dispatcher.h"
43 #include "ui/base/l10n/l10n_util.h"
44 #include "ui/compositor/layer.h"
45 #include "ui/events/event.h"
46 #include "ui/events/event_constants.h"
47 #include "ui/events/event_utils.h"
48 #include "ui/events/test/event_generator.h"
49 #include "ui/gfx/geometry/point.h"
50 #include "ui/views/view_model.h"
51 #include "ui/views/widget/widget.h"
52 #include "ui/views/widget/widget_delegate.h"
53 #include "ui/wm/core/coordinate_conversion.h"
55 namespace ash {
56 namespace test {
58 ////////////////////////////////////////////////////////////////////////////////
59 // ShelfIconObserver tests.
61 class TestShelfIconObserver : public ShelfIconObserver {
62 public:
63 explicit TestShelfIconObserver(Shelf* shelf)
64 : shelf_(shelf),
65 change_notified_(false) {
66 if (shelf_)
67 shelf_->AddIconObserver(this);
70 ~TestShelfIconObserver() override {
71 if (shelf_)
72 shelf_->RemoveIconObserver(this);
75 // ShelfIconObserver implementation.
76 void OnShelfIconPositionsChanged() override { change_notified_ = true; }
78 int change_notified() const { return change_notified_; }
79 void Reset() { change_notified_ = false; }
81 private:
82 Shelf* shelf_;
83 bool change_notified_;
85 DISALLOW_COPY_AND_ASSIGN(TestShelfIconObserver);
88 class ShelfViewIconObserverTest : public AshTestBase {
89 public:
90 ShelfViewIconObserverTest() {}
91 ~ShelfViewIconObserverTest() override {}
93 void SetUp() override {
94 AshTestBase::SetUp();
95 Shelf* shelf = Shelf::ForPrimaryDisplay();
96 observer_.reset(new TestShelfIconObserver(shelf));
98 shelf_view_test_.reset(
99 new ShelfViewTestAPI(ShelfTestAPI(shelf).shelf_view()));
100 shelf_view_test_->SetAnimationDuration(1);
103 void TearDown() override {
104 observer_.reset();
105 AshTestBase::TearDown();
108 TestShelfIconObserver* observer() { return observer_.get(); }
110 ShelfViewTestAPI* shelf_view_test() {
111 return shelf_view_test_.get();
114 Shelf* ShelfForSecondaryDisplay() {
115 return Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
118 private:
119 scoped_ptr<TestShelfIconObserver> observer_;
120 scoped_ptr<ShelfViewTestAPI> shelf_view_test_;
122 DISALLOW_COPY_AND_ASSIGN(ShelfViewIconObserverTest);
125 // TestShelfItemDelegate which tracks whether it gets selected.
126 class ShelfItemSelectionTracker : public TestShelfItemDelegate {
127 public:
128 ShelfItemSelectionTracker()
129 : TestShelfItemDelegate(NULL),
130 selected_(false),
131 item_selected_action_(kNoAction) {}
133 ~ShelfItemSelectionTracker() override {}
135 // Resets to the initial state.
136 void Reset() { selected_ = false; }
138 void set_item_selected_action(
139 ShelfItemDelegate::PerformedAction item_selected_action) {
140 item_selected_action_ = item_selected_action;
143 // Returns true if the delegate was selected.
144 bool WasSelected() {
145 return selected_;
148 // TestShelfItemDelegate:
149 ShelfItemDelegate::PerformedAction ItemSelected(
150 const ui::Event& event) override {
151 selected_ = true;
152 return item_selected_action_;
155 private:
156 bool selected_;
158 // The action returned from ItemSelected(const ui::Event&).
159 ShelfItemDelegate::PerformedAction item_selected_action_;
161 DISALLOW_COPY_AND_ASSIGN(ShelfItemSelectionTracker);
164 TEST_F(ShelfViewIconObserverTest, AddRemove) {
165 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
166 ASSERT_TRUE(shelf_delegate);
168 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
169 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
170 params.bounds = gfx::Rect(0, 0, 200, 200);
171 params.context = CurrentContext();
173 scoped_ptr<views::Widget> widget(new views::Widget());
174 widget->Init(params);
175 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
176 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
177 EXPECT_TRUE(observer()->change_notified());
178 observer()->Reset();
180 widget->Show();
181 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
182 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
183 EXPECT_TRUE(observer()->change_notified());
184 observer()->Reset();
187 // Sometimes fails on trybots on win7_aura. http://crbug.com/177135
188 #if defined(OS_WIN)
189 #define MAYBE_AddRemoveWithMultipleDisplays \
190 DISABLED_AddRemoveWithMultipleDisplays
191 #else
192 #define MAYBE_AddRemoveWithMultipleDisplays \
193 AddRemoveWithMultipleDisplays
194 #endif
195 // Make sure creating/deleting an window on one displays notifies a
196 // shelf on external display as well as one on primary.
197 TEST_F(ShelfViewIconObserverTest, MAYBE_AddRemoveWithMultipleDisplays) {
198 UpdateDisplay("400x400,400x400");
199 TestShelfIconObserver second_observer(ShelfForSecondaryDisplay());
201 TestShelfDelegate* shelf_delegate = TestShelfDelegate::instance();
202 ASSERT_TRUE(shelf_delegate);
204 views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
205 params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
206 params.bounds = gfx::Rect(0, 0, 200, 200);
207 params.context = CurrentContext();
209 scoped_ptr<views::Widget> widget(new views::Widget());
210 widget->Init(params);
211 shelf_delegate->AddShelfItem(widget->GetNativeWindow());
212 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
213 EXPECT_TRUE(observer()->change_notified());
214 EXPECT_TRUE(second_observer.change_notified());
215 observer()->Reset();
216 second_observer.Reset();
218 widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
219 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
220 EXPECT_TRUE(observer()->change_notified());
221 EXPECT_TRUE(second_observer.change_notified());
223 observer()->Reset();
224 second_observer.Reset();
227 TEST_F(ShelfViewIconObserverTest, BoundsChanged) {
228 ShelfWidget* widget = Shell::GetPrimaryRootWindowController()->shelf();
229 Shelf* shelf = Shelf::ForPrimaryDisplay();
230 gfx::Size shelf_size = widget->GetWindowBoundsInScreen().size();
231 shelf_size.set_width(shelf_size.width() / 2);
232 ASSERT_GT(shelf_size.width(), 0);
233 shelf->SetShelfViewBounds(gfx::Rect(shelf_size));
234 // No animation happens for ShelfView bounds change.
235 EXPECT_TRUE(observer()->change_notified());
236 observer()->Reset();
239 ////////////////////////////////////////////////////////////////////////////////
240 // ShelfView tests.
242 // A ShelfDelegate test double that will always convert between ShelfIDs and app
243 // ids. This does not support pinning and unpinning operations however CanPin()
244 // will return true and the return value of IsAppPinned(...) is configurable.
245 class TestShelfDelegateForShelfView : public ShelfDelegate {
246 public:
247 explicit TestShelfDelegateForShelfView(ShelfModel* model)
248 : model_(model) {}
249 ~TestShelfDelegateForShelfView() override {}
251 void set_is_app_pinned(bool is_pinned) { is_app_pinned_ = is_pinned; }
253 // ShelfDelegate overrides:
254 void OnShelfCreated(Shelf* shelf) override {}
256 void OnShelfDestroyed(Shelf* shelf) override {}
258 ShelfID GetShelfIDForAppID(const std::string& app_id) override {
259 ShelfID id = 0;
260 EXPECT_TRUE(base::StringToInt(app_id, &id));
261 return id;
264 bool HasShelfIDToAppIDMapping(ShelfID id) const override { return true; }
266 const std::string& GetAppIDForShelfID(ShelfID id) override {
267 // Use |app_id_| member variable because returning a reference to local
268 // variable is not allowed.
269 app_id_ = base::IntToString(id);
270 return app_id_;
273 void PinAppWithID(const std::string& app_id) override { NOTREACHED(); }
275 bool IsAppPinned(const std::string& app_id) override {
276 return is_app_pinned_;
279 bool CanPin() const override { return true; }
281 void UnpinAppWithID(const std::string& app_id) override { NOTREACHED(); }
283 private:
284 ShelfModel* model_;
286 // Tracks whether apps are pinned or not.
287 bool is_app_pinned_ = false;
289 // Temp member variable for returning a value. See the comment in the
290 // GetAppIDForShelfID().
291 std::string app_id_;
293 DISALLOW_COPY_AND_ASSIGN(TestShelfDelegateForShelfView);
296 class ShelfViewTest : public AshTestBase {
297 public:
298 static const char*
299 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName;
301 ShelfViewTest()
302 : model_(NULL),
303 shelf_view_(NULL),
304 browser_index_(1),
305 item_manager_(NULL) {}
306 ~ShelfViewTest() override {}
308 void SetUp() override {
309 AshTestBase::SetUp();
310 test::ShellTestApi test_api(Shell::GetInstance());
311 model_ = test_api.shelf_model();
312 Shelf* shelf = Shelf::ForPrimaryDisplay();
313 shelf_view_ = ShelfTestAPI(shelf).shelf_view();
315 // The bounds should be big enough for 4 buttons + overflow chevron.
316 shelf_view_->SetBounds(0, 0, 500, kShelfSize);
318 test_api_.reset(new ShelfViewTestAPI(shelf_view_));
319 test_api_->SetAnimationDuration(1); // Speeds up animation for test.
321 ReplaceShelfDelegate();
323 item_manager_ = Shell::GetInstance()->shelf_item_delegate_manager();
324 DCHECK(item_manager_);
326 // Add browser shortcut shelf item at index 0 for test.
327 AddBrowserShortcut();
330 void TearDown() override {
331 shelf_delegate_ = nullptr;
332 test_api_.reset();
333 AshTestBase::TearDown();
336 protected:
337 void CreateAndSetShelfItemDelegateForID(ShelfID id) {
338 scoped_ptr<ShelfItemDelegate> delegate(new TestShelfItemDelegate(NULL));
339 item_manager_->SetShelfItemDelegate(id, delegate.Pass());
342 ShelfID AddBrowserShortcut() {
343 ShelfItem browser_shortcut;
344 browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
346 ShelfID id = model_->next_id();
347 model_->AddAt(browser_index_, browser_shortcut);
348 CreateAndSetShelfItemDelegateForID(id);
349 test_api_->RunMessageLoopUntilAnimationsDone();
350 return id;
353 ShelfID AddAppShortcut() {
354 ShelfItem item;
355 item.type = TYPE_APP_SHORTCUT;
356 item.status = STATUS_CLOSED;
358 ShelfID id = model_->next_id();
359 model_->Add(item);
360 CreateAndSetShelfItemDelegateForID(id);
361 test_api_->RunMessageLoopUntilAnimationsDone();
362 return id;
365 ShelfID AddPanel() {
366 ShelfID id = AddPanelNoWait();
367 test_api_->RunMessageLoopUntilAnimationsDone();
368 return id;
371 ShelfID AddPlatformAppNoWait() {
372 ShelfItem item;
373 item.type = TYPE_PLATFORM_APP;
374 item.status = STATUS_RUNNING;
376 ShelfID id = model_->next_id();
377 model_->Add(item);
378 CreateAndSetShelfItemDelegateForID(id);
379 return id;
382 ShelfID AddPanelNoWait() {
383 ShelfItem item;
384 item.type = TYPE_APP_PANEL;
385 item.status = STATUS_RUNNING;
387 ShelfID id = model_->next_id();
388 model_->Add(item);
389 CreateAndSetShelfItemDelegateForID(id);
390 return id;
393 ShelfID AddPlatformApp() {
394 ShelfID id = AddPlatformAppNoWait();
395 test_api_->RunMessageLoopUntilAnimationsDone();
396 return id;
399 void RemoveByID(ShelfID id) {
400 model_->RemoveItemAt(model_->ItemIndexByID(id));
401 test_api_->RunMessageLoopUntilAnimationsDone();
404 ShelfButton* GetButtonByID(ShelfID id) {
405 int index = model_->ItemIndexByID(id);
406 return test_api_->GetButton(index);
409 ShelfItem GetItemByID(ShelfID id) {
410 ShelfItems::const_iterator items = model_->ItemByID(id);
411 return *items;
414 void CheckModelIDs(
415 const std::vector<std::pair<ShelfID, views::View*> >& id_map) {
416 size_t map_index = 0;
417 for (size_t model_index = 0;
418 model_index < model_->items().size();
419 ++model_index) {
420 ShelfItem item = model_->items()[model_index];
421 ShelfID id = item.id;
422 EXPECT_EQ(id_map[map_index].first, id);
423 EXPECT_EQ(id_map[map_index].second, GetButtonByID(id));
424 ++map_index;
426 ASSERT_EQ(map_index, id_map.size());
429 void VerifyShelfItemBoundsAreValid() {
430 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
431 if (test_api_->GetButton(i)) {
432 gfx::Rect shelf_view_bounds = shelf_view_->GetLocalBounds();
433 gfx::Rect item_bounds = test_api_->GetBoundsByIndex(i);
434 EXPECT_GE(item_bounds.x(), 0);
435 EXPECT_GE(item_bounds.y(), 0);
436 EXPECT_LE(item_bounds.right(), shelf_view_bounds.width());
437 EXPECT_LE(item_bounds.bottom(), shelf_view_bounds.height());
442 ShelfButton* SimulateButtonPressed(ShelfButtonHost::Pointer pointer,
443 int button_index) {
444 ShelfButtonHost* button_host = shelf_view_;
445 ShelfButton* button = test_api_->GetButton(button_index);
446 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED, gfx::Point(),
447 button->GetBoundsInScreen().origin(),
448 ui::EventTimeForNow(), 0, 0);
449 button_host->PointerPressedOnButton(button, pointer, click_event);
450 return button;
453 // Simulates a single mouse click.
454 void SimulateClick(int button_index) {
455 ShelfButtonHost* button_host = shelf_view_;
456 ShelfButton* button =
457 SimulateButtonPressed(ShelfButtonHost::MOUSE, button_index);
458 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, gfx::Point(),
459 button->GetBoundsInScreen().origin(),
460 ui::EventTimeForNow(), 0, 0);
461 test_api_->ButtonPressed(button, release_event);
462 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false);
465 // Simulates the second click of a double click.
466 void SimulateDoubleClick(int button_index) {
467 ShelfButtonHost* button_host = shelf_view_;
468 ShelfButton* button =
469 SimulateButtonPressed(ShelfButtonHost::MOUSE, button_index);
470 ui::MouseEvent release_event(ui::ET_MOUSE_RELEASED, gfx::Point(),
471 button->GetBoundsInScreen().origin(),
472 ui::EventTimeForNow(), ui::EF_IS_DOUBLE_CLICK,
474 test_api_->ButtonPressed(button, release_event);
475 button_host->PointerReleasedOnButton(button, ShelfButtonHost::MOUSE, false);
478 views::View* SimulateDrag(ShelfButtonHost::Pointer pointer,
479 int button_index,
480 int destination_index) {
481 ShelfButtonHost* button_host = shelf_view_;
482 views::View* button = SimulateButtonPressed(pointer, button_index);
484 // Drag.
485 views::View* destination = test_api_->GetButton(destination_index);
486 ui::MouseEvent drag_event(
487 ui::ET_MOUSE_DRAGGED, gfx::Point(destination->x() - button->x(),
488 destination->y() - button->y()),
489 destination->GetBoundsInScreen().origin(), ui::EventTimeForNow(), 0, 0);
490 button_host->PointerDraggedOnButton(button, pointer, drag_event);
491 return button;
494 void SetupForDragTest(
495 std::vector<std::pair<ShelfID, views::View*> >* id_map) {
496 // Initialize |id_map| with the automatically-created shelf buttons.
497 for (size_t i = 0; i < model_->items().size(); ++i) {
498 ShelfButton* button = test_api_->GetButton(i);
499 id_map->push_back(std::make_pair(model_->items()[i].id, button));
501 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
503 // Add 5 app shelf buttons for testing.
504 for (int i = 0; i < 5; ++i) {
505 ShelfID id = AddAppShortcut();
506 // App Icon is located at index 0, and browser shortcut is located at
507 // index 1. So we should start to add app shortcut at index 2.
508 id_map->insert(id_map->begin() + (i + browser_index_ + 1),
509 std::make_pair(id, GetButtonByID(id)));
511 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
514 views::View* GetTooltipAnchorView() {
515 return shelf_view_->tooltip_manager()->anchor_;
518 void AddButtonsUntilOverflow() {
519 int items_added = 0;
520 while (!test_api_->IsOverflowButtonVisible()) {
521 AddAppShortcut();
522 ++items_added;
523 ASSERT_LT(items_added, 10000);
527 void ShowTooltip() {
528 shelf_view_->tooltip_manager()->ShowInternal();
531 void TestDraggingAnItemFromOverflowToShelf(bool cancel) {
532 test_api_->ShowOverflowBubble();
533 ASSERT_TRUE(test_api_->overflow_bubble() &&
534 test_api_->overflow_bubble()->IsShowing());
536 ash::test::ShelfViewTestAPI test_api_for_overflow(
537 test_api_->overflow_bubble()->shelf_view());
539 int total_item_count = model_->item_count();
541 int last_visible_item_id_in_shelf =
542 GetItemId(test_api_->GetLastVisibleIndex());
543 int second_last_visible_item_id_in_shelf =
544 GetItemId(test_api_->GetLastVisibleIndex() - 1);
545 int first_visible_item_id_in_overflow =
546 GetItemId(test_api_for_overflow.GetFirstVisibleIndex());
547 int second_last_visible_item_id_in_overflow =
548 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1);
550 int drag_item_index =
551 test_api_for_overflow.GetLastVisibleIndex();
552 ShelfID drag_item_id = GetItemId(drag_item_index);
553 ShelfButton* drag_button = test_api_for_overflow.GetButton(drag_item_index);
554 gfx::Point center_point_of_drag_item =
555 drag_button->GetBoundsInScreen().CenterPoint();
557 ui::test::EventGenerator generator(ash::Shell::GetPrimaryRootWindow(),
558 center_point_of_drag_item);
559 // Rip an item off to OverflowBubble.
560 generator.PressLeftButton();
561 gfx::Point rip_off_point(center_point_of_drag_item.x(), 0);
562 generator.MoveMouseTo(rip_off_point);
563 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
564 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
565 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
567 // Move a dragged item into Shelf at |drop_index|.
568 int drop_index = 1;
569 gfx::Point drop_point =
570 test_api_->GetButton(drop_index)->GetBoundsInScreen().CenterPoint();
571 int item_width = test_api_for_overflow.GetButtonSize();
572 // To insert at |drop_index|, more smaller x-axis value of |drop_point|
573 // should be used.
574 gfx::Point modified_drop_point(drop_point.x() - item_width / 4,
575 drop_point.y());
576 generator.MoveMouseTo(modified_drop_point);
577 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
578 test_api_->RunMessageLoopUntilAnimationsDone();
579 ASSERT_TRUE(test_api_for_overflow.IsRippedOffFromShelf());
580 ASSERT_TRUE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
582 if (cancel)
583 drag_button->OnMouseCaptureLost();
584 else
585 generator.ReleaseLeftButton();
587 test_api_for_overflow.RunMessageLoopUntilAnimationsDone();
588 test_api_->RunMessageLoopUntilAnimationsDone();
589 ASSERT_FALSE(test_api_for_overflow.IsRippedOffFromShelf());
590 ASSERT_FALSE(test_api_for_overflow.DraggedItemFromOverflowToShelf());
592 // Compare pre-stored items' id with newly positioned items' after dragging
593 // is canceled or finished.
594 if (cancel) {
595 EXPECT_EQ(last_visible_item_id_in_shelf,
596 GetItemId(test_api_->GetLastVisibleIndex()));
597 EXPECT_EQ(second_last_visible_item_id_in_shelf,
598 GetItemId(test_api_->GetLastVisibleIndex() - 1));
599 EXPECT_EQ(first_visible_item_id_in_overflow,
600 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
601 EXPECT_EQ(second_last_visible_item_id_in_overflow,
602 GetItemId(test_api_for_overflow.GetLastVisibleIndex() - 1));
603 } else {
604 EXPECT_EQ(drag_item_id, GetItemId(drop_index));
605 EXPECT_EQ(total_item_count, model_->item_count());
606 EXPECT_EQ(last_visible_item_id_in_shelf,
607 GetItemId(test_api_for_overflow.GetFirstVisibleIndex()));
608 EXPECT_EQ(second_last_visible_item_id_in_shelf,
609 GetItemId(test_api_->GetLastVisibleIndex()));
610 EXPECT_EQ(first_visible_item_id_in_overflow,
611 GetItemId(test_api_for_overflow.GetFirstVisibleIndex() + 1));
612 EXPECT_EQ(second_last_visible_item_id_in_overflow,
613 GetItemId(test_api_for_overflow.GetLastVisibleIndex()));
617 // Returns the item's ShelfID at |index|.
618 ShelfID GetItemId(int index) {
619 DCHECK_GE(index, 0);
620 return model_->items()[index].id;
623 void ReplaceShelfDelegate() {
624 // Replace ShelfDelegate.
625 test::ShellTestApi shell_test_api(Shell::GetInstance());
626 shell_test_api.SetShelfDelegate(NULL);
627 shelf_delegate_ = new TestShelfDelegateForShelfView(model_);
628 shell_test_api.SetShelfDelegate(shelf_delegate_);
629 test::ShelfTestAPI(Shelf::ForPrimaryDisplay())
630 .SetShelfDelegate(shelf_delegate_);
631 test_api_->SetShelfDelegate(shelf_delegate_);
634 ShelfModel* model_;
635 ShelfView* shelf_view_;
636 int browser_index_;
637 ShelfItemDelegateManager* item_manager_;
639 // Owned by ash::Shell.
640 TestShelfDelegateForShelfView* shelf_delegate_;
642 scoped_ptr<ShelfViewTestAPI> test_api_;
644 private:
645 DISALLOW_COPY_AND_ASSIGN(ShelfViewTest);
648 const char*
649 ShelfViewTest::kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName =
650 ShelfButtonPressedMetricTracker::
651 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName;
653 class ScopedTextDirectionChange {
654 public:
655 explicit ScopedTextDirectionChange(bool is_rtl) : is_rtl_(is_rtl) {
656 original_locale_ = l10n_util::GetApplicationLocale(std::string());
657 if (is_rtl_)
658 base::i18n::SetICUDefaultLocale("he");
659 CheckTextDirectionIsCorrect();
662 ~ScopedTextDirectionChange() {
663 if (is_rtl_)
664 base::i18n::SetICUDefaultLocale(original_locale_);
667 private:
668 void CheckTextDirectionIsCorrect() {
669 ASSERT_EQ(is_rtl_, base::i18n::IsRTL());
672 bool is_rtl_;
673 std::string original_locale_;
676 class ShelfViewTextDirectionTest
677 : public ShelfViewTest,
678 public testing::WithParamInterface<bool> {
679 public:
680 ShelfViewTextDirectionTest() : text_direction_change_(GetParam()) {}
681 virtual ~ShelfViewTextDirectionTest() {}
683 void SetUp() override { ShelfViewTest::SetUp(); }
685 void TearDown() override { ShelfViewTest::TearDown(); }
687 private:
688 ScopedTextDirectionChange text_direction_change_;
690 DISALLOW_COPY_AND_ASSIGN(ShelfViewTextDirectionTest);
693 // Checks that the ideal item icon bounds match the view's bounds in the screen
694 // in both LTR and RTL.
695 TEST_P(ShelfViewTextDirectionTest, IdealBoundsOfItemIcon) {
696 ShelfID id = AddPlatformApp();
697 ShelfButton* button = GetButtonByID(id);
698 gfx::Rect item_bounds = button->GetBoundsInScreen();
699 gfx::Point icon_offset = button->GetIconBounds().origin();
700 item_bounds.Offset(icon_offset.OffsetFromOrigin());
701 gfx::Rect ideal_bounds = shelf_view_->GetIdealBoundsOfItemIcon(id);
702 gfx::Point screen_origin;
703 views::View::ConvertPointToScreen(shelf_view_, &screen_origin);
704 ideal_bounds.Offset(screen_origin.x(), screen_origin.y());
705 EXPECT_EQ(item_bounds.x(), ideal_bounds.x());
706 EXPECT_EQ(item_bounds.y(), ideal_bounds.y());
709 // Check that items in the overflow area are returning the overflow button as
710 // ideal bounds.
711 TEST_F(ShelfViewTest, OverflowButtonBounds) {
712 ShelfID first_id = AddPlatformApp();
713 ShelfID overflow_id = AddPlatformApp();
714 int items_added = 0;
715 while (!test_api_->IsOverflowButtonVisible()) {
716 // Added button is visible after animation while in this loop.
717 EXPECT_TRUE(GetButtonByID(overflow_id)->visible());
718 overflow_id = AddPlatformApp();
719 ++items_added;
720 ASSERT_LT(items_added, 10000);
722 ShelfID last_id = AddPlatformApp();
724 gfx::Rect first_bounds = shelf_view_->GetIdealBoundsOfItemIcon(first_id);
725 gfx::Rect overflow_bounds =
726 shelf_view_->GetIdealBoundsOfItemIcon(overflow_id);
727 gfx::Rect last_bounds = shelf_view_->GetIdealBoundsOfItemIcon(last_id);
729 // Check that all items have the same size and that the overflow items are
730 // identical whereas the first one does not match either of them.
731 EXPECT_EQ(first_bounds.size().ToString(), last_bounds.size().ToString());
732 EXPECT_NE(first_bounds.ToString(), last_bounds.ToString());
733 EXPECT_EQ(overflow_bounds.ToString(), last_bounds.ToString());
736 // Checks that shelf view contents are considered in the correct drag group.
737 TEST_F(ShelfViewTest, EnforceDragType) {
738 EXPECT_TRUE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_PLATFORM_APP));
739 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_SHORTCUT));
740 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP,
741 TYPE_BROWSER_SHORTCUT));
742 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_WINDOWED_APP));
743 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_LIST));
744 EXPECT_FALSE(test_api_->SameDragType(TYPE_PLATFORM_APP, TYPE_APP_PANEL));
746 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_SHORTCUT));
747 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
748 TYPE_BROWSER_SHORTCUT));
749 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT,
750 TYPE_WINDOWED_APP));
751 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_LIST));
752 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_SHORTCUT, TYPE_APP_PANEL));
754 EXPECT_TRUE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
755 TYPE_BROWSER_SHORTCUT));
756 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT,
757 TYPE_WINDOWED_APP));
758 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_LIST));
759 EXPECT_FALSE(test_api_->SameDragType(TYPE_BROWSER_SHORTCUT, TYPE_APP_PANEL));
761 EXPECT_TRUE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_WINDOWED_APP));
762 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_LIST));
763 EXPECT_FALSE(test_api_->SameDragType(TYPE_WINDOWED_APP, TYPE_APP_PANEL));
765 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_LIST));
766 EXPECT_FALSE(test_api_->SameDragType(TYPE_APP_LIST, TYPE_APP_PANEL));
768 EXPECT_TRUE(test_api_->SameDragType(TYPE_APP_PANEL, TYPE_APP_PANEL));
771 // Adds platform app button until overflow and verifies that the last added
772 // platform app button is hidden.
773 TEST_F(ShelfViewTest, AddBrowserUntilOverflow) {
774 // All buttons should be visible.
775 ASSERT_EQ(test_api_->GetButtonCount(),
776 test_api_->GetLastVisibleIndex() + 1);
778 // Add platform app button until overflow.
779 int items_added = 0;
780 ShelfID last_added = AddPlatformApp();
781 while (!test_api_->IsOverflowButtonVisible()) {
782 // Added button is visible after animation while in this loop.
783 EXPECT_TRUE(GetButtonByID(last_added)->visible());
785 last_added = AddPlatformApp();
786 ++items_added;
787 ASSERT_LT(items_added, 10000);
790 // The last added button should be invisible.
791 EXPECT_FALSE(GetButtonByID(last_added)->visible());
794 // Adds one platform app button then adds app shortcut until overflow. Verifies
795 // that the browser button gets hidden on overflow and last added app shortcut
796 // is still visible.
797 TEST_F(ShelfViewTest, AddAppShortcutWithBrowserButtonUntilOverflow) {
798 // All buttons should be visible.
799 ASSERT_EQ(test_api_->GetButtonCount(),
800 test_api_->GetLastVisibleIndex() + 1);
802 ShelfID browser_button_id = AddPlatformApp();
804 // Add app shortcut until overflow.
805 int items_added = 0;
806 ShelfID last_added = AddAppShortcut();
807 while (!test_api_->IsOverflowButtonVisible()) {
808 // Added button is visible after animation while in this loop.
809 EXPECT_TRUE(GetButtonByID(last_added)->visible());
811 last_added = AddAppShortcut();
812 ++items_added;
813 ASSERT_LT(items_added, 10000);
816 // And the platform app button is invisible.
817 EXPECT_FALSE(GetButtonByID(browser_button_id)->visible());
820 TEST_F(ShelfViewTest, AddPanelHidesPlatformAppButton) {
821 // All buttons should be visible.
822 ASSERT_EQ(test_api_->GetButtonCount(),
823 test_api_->GetLastVisibleIndex() + 1);
825 // Add platform app button until overflow, remember last visible platform app
826 // button.
827 int items_added = 0;
828 ShelfID first_added = AddPlatformApp();
829 EXPECT_TRUE(GetButtonByID(first_added)->visible());
830 while (true) {
831 ShelfID added = AddPlatformApp();
832 if (test_api_->IsOverflowButtonVisible()) {
833 EXPECT_FALSE(GetButtonByID(added)->visible());
834 RemoveByID(added);
835 break;
837 ++items_added;
838 ASSERT_LT(items_added, 10000);
841 ShelfID panel = AddPanel();
842 EXPECT_TRUE(test_api_->IsOverflowButtonVisible());
844 RemoveByID(panel);
845 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
848 // When there are more panels then platform app buttons we should hide panels
849 // rather than platform apps.
850 TEST_F(ShelfViewTest, PlatformAppHidesExcessPanels) {
851 // All buttons should be visible.
852 ASSERT_EQ(test_api_->GetButtonCount(),
853 test_api_->GetLastVisibleIndex() + 1);
855 // Add platform app button.
856 ShelfID platform_app = AddPlatformApp();
857 ShelfID first_panel = AddPanel();
859 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
860 EXPECT_TRUE(GetButtonByID(first_panel)->visible());
862 // Add panels until there is an overflow.
863 ShelfID last_panel = first_panel;
864 int items_added = 0;
865 while (!test_api_->IsOverflowButtonVisible()) {
866 last_panel = AddPanel();
867 ++items_added;
868 ASSERT_LT(items_added, 10000);
871 // The first panel should now be hidden by the new platform apps needing
872 // space.
873 EXPECT_FALSE(GetButtonByID(first_panel)->visible());
874 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
875 EXPECT_TRUE(GetButtonByID(platform_app)->visible());
877 // Adding platform apps should eventually begin to hide platform apps. We will
878 // add platform apps until either the last panel or platform app is hidden.
879 items_added = 0;
880 while (GetButtonByID(platform_app)->visible() &&
881 GetButtonByID(last_panel)->visible()) {
882 platform_app = AddPlatformApp();
883 ++items_added;
884 ASSERT_LT(items_added, 10000);
886 EXPECT_TRUE(GetButtonByID(last_panel)->visible());
887 EXPECT_FALSE(GetButtonByID(platform_app)->visible());
890 // Making sure that no buttons on the shelf will ever overlap after adding many
891 // of them.
892 TEST_F(ShelfViewTest, AssertNoButtonsOverlap) {
893 std::vector<ShelfID> button_ids;
894 // Add app icons until the overflow button is visible.
895 while (!test_api_->IsOverflowButtonVisible()) {
896 ShelfID id = AddPlatformApp();
897 button_ids.push_back(id);
899 ASSERT_LT(button_ids.size(), 10000U);
900 ASSERT_GT(button_ids.size(), 2U);
902 // Remove 2 icons to make more room for panel icons, the overflow button
903 // should go away.
904 for (int i = 0; i < 2; ++i) {
905 ShelfID id = button_ids.back();
906 RemoveByID(id);
907 button_ids.pop_back();
909 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
910 EXPECT_TRUE(GetButtonByID(button_ids.back())->visible());
912 // Add 20 panel icons, and expect to have overflow.
913 for (int i = 0; i < 20; ++i) {
914 ShelfID id = AddPanel();
915 button_ids.push_back(id);
917 ASSERT_LT(button_ids.size(), 10000U);
918 EXPECT_TRUE(test_api_->IsOverflowButtonVisible());
920 // Test that any two successive visible icons never overlap in all shelf
921 // alignment types.
922 const ShelfAlignment kAlignments[] = {
923 SHELF_ALIGNMENT_LEFT,
924 SHELF_ALIGNMENT_RIGHT,
925 SHELF_ALIGNMENT_TOP,
926 SHELF_ALIGNMENT_BOTTOM
929 for (ShelfAlignment alignment : kAlignments) {
930 EXPECT_TRUE(shelf_view_->shelf_layout_manager()->SetAlignment(alignment));
931 // For every 2 successive visible icons, expect that their bounds don't
932 // intersect.
933 for (int i = 1; i < test_api_->GetButtonCount() - 1; ++i) {
934 if (!(test_api_->GetButton(i)->visible() &&
935 test_api_->GetButton(i + 1)->visible())) {
936 continue;
939 const gfx::Rect& bounds1 = test_api_->GetBoundsByIndex(i);
940 const gfx::Rect& bounds2 = test_api_->GetBoundsByIndex(i + 1);
941 EXPECT_FALSE(bounds1.Intersects(bounds2));
946 // Adds button until overflow then removes first added one. Verifies that
947 // the last added one changes from invisible to visible and overflow
948 // chevron is gone.
949 TEST_F(ShelfViewTest, RemoveButtonRevealsOverflowed) {
950 // All buttons should be visible.
951 ASSERT_EQ(test_api_->GetButtonCount(),
952 test_api_->GetLastVisibleIndex() + 1);
954 // Add platform app buttons until overflow.
955 int items_added = 0;
956 ShelfID first_added = AddPlatformApp();
957 ShelfID last_added = first_added;
958 while (!test_api_->IsOverflowButtonVisible()) {
959 last_added = AddPlatformApp();
960 ++items_added;
961 ASSERT_LT(items_added, 10000);
964 // Expect add more than 1 button. First added is visible and last is not.
965 EXPECT_NE(first_added, last_added);
966 EXPECT_TRUE(GetButtonByID(first_added)->visible());
967 EXPECT_FALSE(GetButtonByID(last_added)->visible());
969 // Remove first added.
970 RemoveByID(first_added);
972 // Last added button becomes visible and overflow chevron is gone.
973 EXPECT_TRUE(GetButtonByID(last_added)->visible());
974 EXPECT_EQ(1.0f, GetButtonByID(last_added)->layer()->opacity());
975 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
978 // Verifies that remove last overflowed button should hide overflow chevron.
979 TEST_F(ShelfViewTest, RemoveLastOverflowed) {
980 // All buttons should be visible.
981 ASSERT_EQ(test_api_->GetButtonCount(),
982 test_api_->GetLastVisibleIndex() + 1);
984 // Add platform app button until overflow.
985 int items_added = 0;
986 ShelfID last_added = AddPlatformApp();
987 while (!test_api_->IsOverflowButtonVisible()) {
988 last_added = AddPlatformApp();
989 ++items_added;
990 ASSERT_LT(items_added, 10000);
993 RemoveByID(last_added);
994 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
997 // Adds platform app button without waiting for animation to finish and verifies
998 // that all added buttons are visible.
999 TEST_F(ShelfViewTest, AddButtonQuickly) {
1000 // All buttons should be visible.
1001 ASSERT_EQ(test_api_->GetButtonCount(),
1002 test_api_->GetLastVisibleIndex() + 1);
1004 // Add a few platform buttons quickly without wait for animation.
1005 int added_count = 0;
1006 while (!test_api_->IsOverflowButtonVisible()) {
1007 AddPlatformAppNoWait();
1008 ++added_count;
1009 ASSERT_LT(added_count, 10000);
1012 // ShelfView should be big enough to hold at least 3 new buttons.
1013 ASSERT_GE(added_count, 3);
1015 // Wait for the last animation to finish.
1016 test_api_->RunMessageLoopUntilAnimationsDone();
1018 // Verifies non-overflow buttons are visible.
1019 for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
1020 ShelfButton* button = test_api_->GetButton(i);
1021 if (button) {
1022 EXPECT_TRUE(button->visible()) << "button index=" << i;
1023 EXPECT_EQ(1.0f, button->layer()->opacity()) << "button index=" << i;
1028 // Check that model changes are handled correctly while a shelf icon is being
1029 // dragged.
1030 TEST_F(ShelfViewTest, ModelChangesWhileDragging) {
1031 ShelfButtonHost* button_host = shelf_view_;
1033 std::vector<std::pair<ShelfID, views::View*> > id_map;
1034 SetupForDragTest(&id_map);
1036 // Dragging browser shortcut at index 1.
1037 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
1038 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1039 std::rotate(id_map.begin() + 1,
1040 id_map.begin() + 2,
1041 id_map.begin() + 4);
1042 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1043 button_host->PointerReleasedOnButton(
1044 dragged_button, ShelfButtonHost::MOUSE, false);
1045 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
1047 // Dragging changes model order.
1048 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1049 std::rotate(id_map.begin() + 1,
1050 id_map.begin() + 2,
1051 id_map.begin() + 4);
1052 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1054 // Cancelling the drag operation restores previous order.
1055 button_host->PointerReleasedOnButton(
1056 dragged_button, ShelfButtonHost::MOUSE, true);
1057 std::rotate(id_map.begin() + 1,
1058 id_map.begin() + 3,
1059 id_map.begin() + 4);
1060 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1062 // Deleting an item keeps the remaining intact.
1063 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1064 model_->RemoveItemAt(1);
1065 id_map.erase(id_map.begin() + 1);
1066 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1067 button_host->PointerReleasedOnButton(
1068 dragged_button, ShelfButtonHost::MOUSE, false);
1070 // Adding a shelf item cancels the drag and respects the order.
1071 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1072 ShelfID new_id = AddAppShortcut();
1073 id_map.insert(id_map.begin() + 6,
1074 std::make_pair(new_id, GetButtonByID(new_id)));
1075 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1076 button_host->PointerReleasedOnButton(
1077 dragged_button, ShelfButtonHost::MOUSE, false);
1079 // Adding a shelf item at the end (i.e. a panel) canels drag and respects
1080 // the order.
1081 dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1082 new_id = AddPanel();
1083 id_map.insert(id_map.begin() + 7,
1084 std::make_pair(new_id, GetButtonByID(new_id)));
1085 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1086 button_host->PointerReleasedOnButton(
1087 dragged_button, ShelfButtonHost::MOUSE, false);
1090 // Check that 2nd drag from the other pointer would be ignored.
1091 TEST_F(ShelfViewTest, SimultaneousDrag) {
1092 ShelfButtonHost* button_host = shelf_view_;
1094 std::vector<std::pair<ShelfID, views::View*> > id_map;
1095 SetupForDragTest(&id_map);
1097 // Start a mouse drag.
1098 views::View* dragged_button_mouse =
1099 SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1100 std::rotate(id_map.begin() + 1,
1101 id_map.begin() + 2,
1102 id_map.begin() + 4);
1103 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1104 // Attempt a touch drag before the mouse drag finishes.
1105 views::View* dragged_button_touch =
1106 SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
1108 // Nothing changes since 2nd drag is ignored.
1109 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1111 // Finish the mouse drag.
1112 button_host->PointerReleasedOnButton(
1113 dragged_button_mouse, ShelfButtonHost::MOUSE, false);
1114 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1116 // Now start a touch drag.
1117 dragged_button_touch = SimulateDrag(ShelfButtonHost::TOUCH, 4, 2);
1118 std::rotate(id_map.begin() + 3,
1119 id_map.begin() + 4,
1120 id_map.begin() + 5);
1121 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1123 // And attempt a mouse drag before the touch drag finishes.
1124 dragged_button_mouse = SimulateDrag(ShelfButtonHost::MOUSE, 1, 2);
1126 // Nothing changes since 2nd drag is ignored.
1127 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1129 button_host->PointerReleasedOnButton(
1130 dragged_button_touch, ShelfButtonHost::TOUCH, false);
1131 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1134 // Check that clicking first on one item and then dragging another works as
1135 // expected.
1136 TEST_F(ShelfViewTest, ClickOneDragAnother) {
1137 ShelfButtonHost* button_host = shelf_view_;
1139 std::vector<std::pair<ShelfID, views::View*> > id_map;
1140 SetupForDragTest(&id_map);
1142 // A click on item 1 is simulated.
1143 SimulateClick(1);
1145 // Dragging browser index at 0 should change the model order correctly.
1146 EXPECT_TRUE(model_->items()[1].type == TYPE_BROWSER_SHORTCUT);
1147 views::View* dragged_button = SimulateDrag(ShelfButtonHost::MOUSE, 1, 3);
1148 std::rotate(id_map.begin() + 1,
1149 id_map.begin() + 2,
1150 id_map.begin() + 4);
1151 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
1152 button_host->PointerReleasedOnButton(
1153 dragged_button, ShelfButtonHost::MOUSE, false);
1154 EXPECT_TRUE(model_->items()[3].type == TYPE_BROWSER_SHORTCUT);
1157 // Tests that double-clicking an item does not activate it twice.
1158 TEST_F(ShelfViewTest, ClickingTwiceActivatesOnce) {
1159 // Watch for selection of the browser shortcut.
1160 ShelfID browser_shelf_id = model_->items()[browser_index_].id;
1161 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1162 item_manager_->SetShelfItemDelegate(
1163 browser_shelf_id,
1164 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1166 // A single click selects the item.
1167 SimulateClick(browser_index_);
1168 EXPECT_TRUE(selection_tracker->WasSelected());
1170 // A double-click does not select the item.
1171 selection_tracker->Reset();
1172 SimulateDoubleClick(browser_index_);
1173 EXPECT_FALSE(selection_tracker->WasSelected());
1176 // Check that clicking an item and jittering the mouse a bit still selects the
1177 // item.
1178 TEST_F(ShelfViewTest, ClickAndMoveSlightly) {
1179 std::vector<std::pair<ShelfID, views::View*> > id_map;
1180 SetupForDragTest(&id_map);
1182 ShelfID shelf_id = (id_map.begin() + 1)->first;
1183 views::View* button = (id_map.begin() + 1)->second;
1185 // Replace the ShelfItemDelegate for |shelf_id| with one which tracks whether
1186 // the shelf item gets selected.
1187 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1188 item_manager_->SetShelfItemDelegate(
1189 shelf_id,
1190 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1192 gfx::Vector2d press_offset(5, 30);
1193 gfx::Point press_location = gfx::Point() + press_offset;
1194 gfx::Point press_location_in_screen =
1195 button->GetBoundsInScreen().origin() + press_offset;
1197 ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED, press_location,
1198 press_location_in_screen, ui::EventTimeForNow(),
1199 ui::EF_LEFT_MOUSE_BUTTON, 0);
1200 button->OnMousePressed(click_event);
1202 ui::MouseEvent drag_event1(
1203 ui::ET_MOUSE_DRAGGED, press_location + gfx::Vector2d(0, 1),
1204 press_location_in_screen + gfx::Vector2d(0, 1), ui::EventTimeForNow(),
1205 ui::EF_LEFT_MOUSE_BUTTON, 0);
1206 button->OnMouseDragged(drag_event1);
1208 ui::MouseEvent drag_event2(
1209 ui::ET_MOUSE_DRAGGED, press_location + gfx::Vector2d(-1, 0),
1210 press_location_in_screen + gfx::Vector2d(-1, 0), ui::EventTimeForNow(),
1211 ui::EF_LEFT_MOUSE_BUTTON, 0);
1212 button->OnMouseDragged(drag_event2);
1214 ui::MouseEvent release_event(
1215 ui::ET_MOUSE_RELEASED, press_location + gfx::Vector2d(-1, 0),
1216 press_location_in_screen + gfx::Vector2d(-1, 0), ui::EventTimeForNow(),
1217 ui::EF_LEFT_MOUSE_BUTTON, 0);
1218 button->OnMouseReleased(release_event);
1220 EXPECT_TRUE(selection_tracker->WasSelected());
1223 // Confirm that item status changes are reflected in the buttons.
1224 TEST_F(ShelfViewTest, ShelfItemStatus) {
1225 // All buttons should be visible.
1226 ASSERT_EQ(test_api_->GetButtonCount(),
1227 test_api_->GetLastVisibleIndex() + 1);
1229 // Add platform app button.
1230 ShelfID last_added = AddPlatformApp();
1231 ShelfItem item = GetItemByID(last_added);
1232 int index = model_->ItemIndexByID(last_added);
1233 ShelfButton* button = GetButtonByID(last_added);
1234 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1235 item.status = STATUS_ACTIVE;
1236 model_->Set(index, item);
1237 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1238 item.status = STATUS_ATTENTION;
1239 model_->Set(index, item);
1240 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1243 // Confirm that item status changes are reflected in the buttons
1244 // for platform apps.
1245 TEST_F(ShelfViewTest, ShelfItemStatusPlatformApp) {
1246 // All buttons should be visible.
1247 ASSERT_EQ(test_api_->GetButtonCount(),
1248 test_api_->GetLastVisibleIndex() + 1);
1250 // Add platform app button.
1251 ShelfID last_added = AddPlatformApp();
1252 ShelfItem item = GetItemByID(last_added);
1253 int index = model_->ItemIndexByID(last_added);
1254 ShelfButton* button = GetButtonByID(last_added);
1255 ASSERT_EQ(ShelfButton::STATE_RUNNING, button->state());
1256 item.status = STATUS_ACTIVE;
1257 model_->Set(index, item);
1258 ASSERT_EQ(ShelfButton::STATE_ACTIVE, button->state());
1259 item.status = STATUS_ATTENTION;
1260 model_->Set(index, item);
1261 ASSERT_EQ(ShelfButton::STATE_ATTENTION, button->state());
1264 // Confirm that shelf item bounds are correctly updated on shelf changes.
1265 TEST_F(ShelfViewTest, ShelfItemBoundsCheck) {
1266 VerifyShelfItemBoundsAreValid();
1267 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1268 SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
1269 test_api_->RunMessageLoopUntilAnimationsDone();
1270 VerifyShelfItemBoundsAreValid();
1271 shelf_view_->shelf_layout_manager()->SetAutoHideBehavior(
1272 SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
1273 test_api_->RunMessageLoopUntilAnimationsDone();
1274 VerifyShelfItemBoundsAreValid();
1277 TEST_F(ShelfViewTest, ShelfTooltipTest) {
1278 ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
1279 test_api_->GetButtonCount());
1281 // Prepare some items to the shelf.
1282 ShelfID app_button_id = AddAppShortcut();
1283 ShelfID platform_button_id = AddPlatformApp();
1285 ShelfButton* app_button = GetButtonByID(app_button_id);
1286 ShelfButton* platform_button = GetButtonByID(platform_button_id);
1288 ShelfButtonHost* button_host = shelf_view_;
1289 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1291 button_host->MouseEnteredButton(app_button);
1292 // There's a delay to show the tooltip, so it's not visible yet.
1293 EXPECT_FALSE(tooltip_manager->IsVisible());
1294 EXPECT_EQ(app_button, GetTooltipAnchorView());
1296 ShowTooltip();
1297 EXPECT_TRUE(tooltip_manager->IsVisible());
1299 // Once it's visible, it keeps visibility and is pointing to the same
1300 // item.
1301 button_host->MouseExitedButton(app_button);
1302 EXPECT_TRUE(tooltip_manager->IsVisible());
1303 EXPECT_EQ(app_button, GetTooltipAnchorView());
1305 // When entered to another item, it switches to the new item. There is no
1306 // delay for the visibility.
1307 button_host->MouseEnteredButton(platform_button);
1308 EXPECT_TRUE(tooltip_manager->IsVisible());
1309 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1311 button_host->MouseExitedButton(platform_button);
1312 tooltip_manager->Close();
1314 // Next time: enter app_button -> move immediately to tab_button.
1315 button_host->MouseEnteredButton(app_button);
1316 button_host->MouseExitedButton(app_button);
1317 button_host->MouseEnteredButton(platform_button);
1318 EXPECT_FALSE(tooltip_manager->IsVisible());
1319 EXPECT_EQ(platform_button, GetTooltipAnchorView());
1322 // Verify a fix for crash caused by a tooltip update for a deletedshelf
1323 // button, see crbug.com/288838.
1324 TEST_F(ShelfViewTest, RemovingItemClosesTooltip) {
1325 ShelfButtonHost* button_host = shelf_view_;
1326 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1328 // Add an item to the shelf.
1329 ShelfID app_button_id = AddAppShortcut();
1330 ShelfButton* app_button = GetButtonByID(app_button_id);
1332 // Spawn a tooltip on that item.
1333 button_host->MouseEnteredButton(app_button);
1334 ShowTooltip();
1335 EXPECT_TRUE(tooltip_manager->IsVisible());
1337 // Remove the app shortcut while the tooltip is open. The tooltip should be
1338 // closed.
1339 RemoveByID(app_button_id);
1340 EXPECT_FALSE(tooltip_manager->IsVisible());
1342 // Change the shelf layout. This should not crash.
1343 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1344 Shell::GetPrimaryRootWindow());
1347 // Changing the shelf alignment closes any open tooltip.
1348 TEST_F(ShelfViewTest, ShelfAlignmentClosesTooltip) {
1349 ShelfButtonHost* button_host = shelf_view_;
1350 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1352 // Add an item to the shelf.
1353 ShelfID app_button_id = AddAppShortcut();
1354 ShelfButton* app_button = GetButtonByID(app_button_id);
1356 // Spawn a tooltip on the item.
1357 button_host->MouseEnteredButton(app_button);
1358 ShowTooltip();
1359 EXPECT_TRUE(tooltip_manager->IsVisible());
1361 // Changing shelf alignment hides the tooltip.
1362 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT,
1363 Shell::GetPrimaryRootWindow());
1364 EXPECT_FALSE(tooltip_manager->IsVisible());
1367 TEST_F(ShelfViewTest, ShouldHideTooltipTest) {
1368 ShelfID app_button_id = AddAppShortcut();
1369 ShelfID platform_button_id = AddPlatformApp();
1371 // The tooltip shouldn't hide if the mouse is on normal buttons.
1372 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1373 ShelfButton* button = test_api_->GetButton(i);
1374 if (!button)
1375 continue;
1377 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1378 button->GetMirroredBounds().CenterPoint()))
1379 << "ShelfView tries to hide on button " << i;
1382 // The tooltip should not hide on the app-list button.
1383 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1384 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1385 app_list_button->GetMirroredBounds().CenterPoint()));
1387 // The tooltip shouldn't hide if the mouse is in the gap between two buttons.
1388 gfx::Rect app_button_rect = GetButtonByID(app_button_id)->GetMirroredBounds();
1389 gfx::Rect platform_button_rect =
1390 GetButtonByID(platform_button_id)->GetMirroredBounds();
1391 ASSERT_FALSE(app_button_rect.Intersects(platform_button_rect));
1392 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1393 gfx::UnionRects(app_button_rect, platform_button_rect).CenterPoint()));
1395 // The tooltip should hide if it's outside of all buttons.
1396 gfx::Rect all_area;
1397 for (int i = 0; i < test_api_->GetButtonCount(); i++) {
1398 ShelfButton* button = test_api_->GetButton(i);
1399 if (!button)
1400 continue;
1402 all_area.Union(button->GetMirroredBounds());
1404 all_area.Union(shelf_view_->GetAppListButtonView()->GetMirroredBounds());
1405 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(all_area.origin()));
1406 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1407 gfx::Point(all_area.right() - 1, all_area.bottom() - 1)));
1408 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1409 gfx::Point(all_area.right(), all_area.y())));
1410 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1411 gfx::Point(all_area.x() - 1, all_area.y())));
1412 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1413 gfx::Point(all_area.x(), all_area.y() - 1)));
1414 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1415 gfx::Point(all_area.x(), all_area.bottom())));
1418 TEST_F(ShelfViewTest, ShouldHideTooltipWithAppListWindowTest) {
1419 Shell::GetInstance()->ShowAppList(NULL);
1420 ASSERT_TRUE(Shell::GetInstance()->GetAppListWindow());
1422 // The tooltip shouldn't hide if the mouse is on normal buttons.
1423 for (int i = 1; i < test_api_->GetButtonCount(); i++) {
1424 ShelfButton* button = test_api_->GetButton(i);
1425 if (!button)
1426 continue;
1428 EXPECT_FALSE(shelf_view_->ShouldHideTooltip(
1429 button->GetMirroredBounds().CenterPoint()))
1430 << "ShelfView tries to hide on button " << i;
1433 // The tooltip should hide on the app-list button.
1434 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1435 EXPECT_TRUE(shelf_view_->ShouldHideTooltip(
1436 app_list_button->GetMirroredBounds().CenterPoint()));
1439 // Test that by moving the mouse cursor off the button onto the bubble it closes
1440 // the bubble.
1441 TEST_F(ShelfViewTest, ShouldHideTooltipWhenHoveringOnTooltip) {
1442 ShelfTooltipManager* tooltip_manager = shelf_view_->tooltip_manager();
1443 tooltip_manager->CreateZeroDelayTimerForTest();
1444 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1446 // Move the mouse off any item and check that no tooltip is shown.
1447 generator.MoveMouseTo(gfx::Point(0, 0));
1448 EXPECT_FALSE(tooltip_manager->IsVisible());
1450 // Move the mouse over the button and check that it is visible.
1451 views::View* app_list_button = shelf_view_->GetAppListButtonView();
1452 gfx::Rect bounds = app_list_button->GetBoundsInScreen();
1453 generator.MoveMouseTo(bounds.CenterPoint());
1454 // Wait for the timer to go off.
1455 RunAllPendingInMessageLoop();
1456 EXPECT_TRUE(tooltip_manager->IsVisible());
1458 // Move the mouse cursor slightly to the right of the item. The tooltip should
1459 // stay open.
1460 generator.MoveMouseBy(bounds.width() / 2 + 5, 0);
1461 // Make sure there is no delayed close.
1462 RunAllPendingInMessageLoop();
1463 EXPECT_TRUE(tooltip_manager->IsVisible());
1465 // Move back - it should still stay open.
1466 generator.MoveMouseBy(-(bounds.width() / 2 + 5), 0);
1467 // Make sure there is no delayed close.
1468 RunAllPendingInMessageLoop();
1469 EXPECT_TRUE(tooltip_manager->IsVisible());
1471 // Now move the mouse cursor slightly above the item - so that it is over the
1472 // tooltip bubble. Now it should disappear.
1473 generator.MoveMouseBy(0, -(bounds.height() / 2 + 5));
1474 // Wait until the delayed close kicked in.
1475 RunAllPendingInMessageLoop();
1476 EXPECT_FALSE(tooltip_manager->IsVisible());
1479 // Resizing shelf view while an add animation without fade-in is running,
1480 // which happens when overflow happens. App list button should end up in its
1481 // new ideal bounds.
1482 TEST_F(ShelfViewTest, ResizeDuringOverflowAddAnimation) {
1483 // All buttons should be visible.
1484 ASSERT_EQ(test_api_->GetButtonCount(),
1485 test_api_->GetLastVisibleIndex() + 1);
1487 // Add buttons until overflow. Let the non-overflow add animations finish but
1488 // leave the last running.
1489 int items_added = 0;
1490 AddPlatformAppNoWait();
1491 while (!test_api_->IsOverflowButtonVisible()) {
1492 test_api_->RunMessageLoopUntilAnimationsDone();
1493 AddPlatformAppNoWait();
1494 ++items_added;
1495 ASSERT_LT(items_added, 10000);
1498 // Resize shelf view with that animation running and stay overflown.
1499 gfx::Rect bounds = shelf_view_->bounds();
1500 bounds.set_width(bounds.width() - kShelfSize);
1501 shelf_view_->SetBoundsRect(bounds);
1502 ASSERT_TRUE(test_api_->IsOverflowButtonVisible());
1504 // Finish the animation.
1505 test_api_->RunMessageLoopUntilAnimationsDone();
1507 // App list button should ends up in its new ideal bounds.
1508 const int app_list_button_index = test_api_->GetButtonCount() - 1;
1509 const gfx::Rect& app_list_ideal_bounds =
1510 test_api_->GetIdealBoundsByIndex(app_list_button_index);
1511 const gfx::Rect& app_list_bounds =
1512 test_api_->GetBoundsByIndex(app_list_button_index);
1513 EXPECT_EQ(app_list_ideal_bounds, app_list_bounds);
1516 // Checks the overflow bubble size when an item is ripped off and re-inserted.
1517 TEST_F(ShelfViewTest, OverflowBubbleSize) {
1518 shelf_delegate_->set_is_app_pinned(true);
1519 AddButtonsUntilOverflow();
1520 // Add one more button to prevent the overflow bubble to disappear upon
1521 // dragging an item out on windows (flakiness, see crbug.com/436131).
1522 AddAppShortcut();
1524 // Show overflow bubble.
1525 test_api_->ShowOverflowBubble();
1526 ASSERT_TRUE(test_api_->overflow_bubble() &&
1527 test_api_->overflow_bubble()->IsShowing());
1529 ShelfViewTestAPI test_for_overflow_view(
1530 test_api_->overflow_bubble()->shelf_view());
1532 int ripped_index = test_for_overflow_view.GetLastVisibleIndex();
1533 gfx::Size bubble_size = test_for_overflow_view.GetPreferredSize();
1534 int item_width = test_for_overflow_view.GetButtonSize() +
1535 test_for_overflow_view.GetButtonSpacing();
1537 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow(),
1538 gfx::Point());
1539 ShelfButton* button = test_for_overflow_view.GetButton(ripped_index);
1540 // Rip off the last visible item.
1541 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1542 gfx::Point rip_off_point(start_point.x(), 0);
1543 generator.MoveMouseTo(start_point.x(), start_point.y());
1544 base::MessageLoop::current()->RunUntilIdle();
1545 generator.PressLeftButton();
1546 base::MessageLoop::current()->RunUntilIdle();
1547 generator.MoveMouseTo(rip_off_point.x(), rip_off_point.y());
1548 base::MessageLoop::current()->RunUntilIdle();
1549 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1551 // Check the overflow bubble size when an item is ripped off.
1552 EXPECT_EQ(bubble_size.width() - item_width,
1553 test_for_overflow_view.GetPreferredSize().width());
1554 ASSERT_TRUE(test_api_->overflow_bubble() &&
1555 test_api_->overflow_bubble()->IsShowing());
1557 // Re-insert an item into the overflow bubble.
1558 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1559 button = test_for_overflow_view.GetButton(first_index);
1561 // Check the bubble size after an item is re-inserted.
1562 generator.MoveMouseTo(button->GetBoundsInScreen().CenterPoint());
1563 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1564 EXPECT_EQ(bubble_size.width(),
1565 test_for_overflow_view.GetPreferredSize().width());
1567 generator.ReleaseLeftButton();
1568 test_for_overflow_view.RunMessageLoopUntilAnimationsDone();
1569 EXPECT_EQ(bubble_size.width(),
1570 test_for_overflow_view.GetPreferredSize().width());
1573 // Check the drag insertion bounds of scrolled overflow bubble.
1574 TEST_F(ShelfViewTest, CheckDragInsertBoundsOfScrolledOverflowBubble) {
1575 UpdateDisplay("400x300");
1577 EXPECT_EQ(2, model_->item_count());
1579 AddButtonsUntilOverflow();
1581 // Show overflow bubble.
1582 test_api_->ShowOverflowBubble();
1583 ASSERT_TRUE(test_api_->overflow_bubble() &&
1584 test_api_->overflow_bubble()->IsShowing());
1586 int item_width = test_api_->GetButtonSize() +
1587 test_api_->GetButtonSpacing();
1588 OverflowBubbleView* bubble_view = test_api_->overflow_bubble()->bubble_view();
1589 test::OverflowBubbleViewTestAPI bubble_view_api(bubble_view);
1591 // Add more buttons until OverflowBubble is scrollable and it has 3 invisible
1592 // items.
1593 while (bubble_view_api.GetContentsSize().width() <
1594 (bubble_view->GetContentsBounds().width() + 3 * item_width))
1595 AddAppShortcut();
1597 ASSERT_TRUE(test_api_->overflow_bubble() &&
1598 test_api_->overflow_bubble()->IsShowing());
1600 ShelfViewTestAPI test_for_overflow_view(
1601 test_api_->overflow_bubble()->shelf_view());
1602 int first_index = test_for_overflow_view.GetFirstVisibleIndex();
1603 int last_index = test_for_overflow_view.GetLastVisibleIndex();
1605 ShelfButton* first_button = test_for_overflow_view.GetButton(first_index);
1606 ShelfButton* last_button = test_for_overflow_view.GetButton(last_index);
1607 gfx::Point first_point = first_button->GetBoundsInScreen().CenterPoint();
1608 gfx::Point last_point = last_button->GetBoundsInScreen().CenterPoint();
1609 gfx::Rect drag_reinsert_bounds =
1610 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1611 EXPECT_TRUE(drag_reinsert_bounds.Contains(first_point));
1612 EXPECT_FALSE(drag_reinsert_bounds.Contains(last_point));
1614 // Scrolls sufficiently to show last item.
1615 bubble_view_api.ScrollByXOffset(3 * item_width);
1616 drag_reinsert_bounds =
1617 test_for_overflow_view.GetBoundsForDragInsertInScreen();
1618 first_point = first_button->GetBoundsInScreen().CenterPoint();
1619 last_point = last_button->GetBoundsInScreen().CenterPoint();
1620 EXPECT_FALSE(drag_reinsert_bounds.Contains(first_point));
1621 EXPECT_TRUE(drag_reinsert_bounds.Contains(last_point));
1624 // Check the drag insertion bounds of shelf view in multi monitor environment.
1625 TEST_F(ShelfViewTest, CheckDragInsertBoundsWithMultiMonitor) {
1626 // win8-aura doesn't support multiple display.
1627 if (!SupportsMultipleDisplays())
1628 return;
1630 UpdateDisplay("800x600,800x600");
1631 Shelf* secondary_shelf = Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
1632 ShelfView* shelf_view_for_secondary =
1633 ShelfTestAPI(secondary_shelf).shelf_view();
1635 // The bounds should be big enough for 4 buttons + overflow chevron.
1636 shelf_view_for_secondary->SetBounds(0, 0, 500, kShelfSize);
1638 ShelfViewTestAPI test_api_for_secondary(shelf_view_for_secondary);
1639 // Speeds up animation for test.
1640 test_api_for_secondary.SetAnimationDuration(1);
1642 AddButtonsUntilOverflow();
1644 // Test #1: Test drag insertion bounds of primary shelf.
1645 // Show overflow bubble.
1646 test_api_->ShowOverflowBubble();
1647 ASSERT_TRUE(test_api_->overflow_bubble() &&
1648 test_api_->overflow_bubble()->IsShowing());
1650 ShelfViewTestAPI test_api_for_overflow_view(
1651 test_api_->overflow_bubble()->shelf_view());
1653 ShelfButton* button = test_api_for_overflow_view.GetButton(
1654 test_api_for_overflow_view.GetLastVisibleIndex());
1656 // Checks that a point in shelf is contained in drag insert bounds.
1657 gfx::Point point_in_shelf_view = button->GetBoundsInScreen().CenterPoint();
1658 gfx::Rect drag_reinsert_bounds =
1659 test_api_for_overflow_view.GetBoundsForDragInsertInScreen();
1660 EXPECT_TRUE(drag_reinsert_bounds.Contains(point_in_shelf_view));
1661 // Checks that a point out of shelf is not contained in drag insert bounds.
1662 EXPECT_FALSE(drag_reinsert_bounds.Contains(
1663 gfx::Point(point_in_shelf_view.x(), 0)));
1665 // Test #2: Test drag insertion bounds of secondary shelf.
1666 // Show overflow bubble.
1667 test_api_for_secondary.ShowOverflowBubble();
1668 ASSERT_TRUE(test_api_for_secondary.overflow_bubble() &&
1669 test_api_for_secondary.overflow_bubble()->IsShowing());
1671 ShelfViewTestAPI test_api_for_overflow_view_of_secondary(
1672 test_api_for_secondary.overflow_bubble()->shelf_view());
1674 ShelfButton* button_in_secondary =
1675 test_api_for_overflow_view_of_secondary.GetButton(
1676 test_api_for_overflow_view_of_secondary.GetLastVisibleIndex());
1678 // Checks that a point in shelf is contained in drag insert bounds.
1679 gfx::Point point_in_secondary_shelf_view =
1680 button_in_secondary->GetBoundsInScreen().CenterPoint();
1681 gfx::Rect drag_reinsert_bounds_in_secondary =
1682 test_api_for_overflow_view_of_secondary.GetBoundsForDragInsertInScreen();
1683 EXPECT_TRUE(drag_reinsert_bounds_in_secondary.Contains(
1684 point_in_secondary_shelf_view));
1685 // Checks that a point out of shelf is not contained in drag insert bounds.
1686 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(
1687 gfx::Point(point_in_secondary_shelf_view.x(), 0)));
1688 // Checks that a point of overflow bubble in primary shelf should not be
1689 // contained by insert bounds of secondary shelf.
1690 EXPECT_FALSE(drag_reinsert_bounds_in_secondary.Contains(point_in_shelf_view));
1693 // Checks the rip an item off from left aligned shelf in secondary monitor.
1694 TEST_F(ShelfViewTest, CheckRipOffFromLeftShelfAlignmentWithMultiMonitor) {
1695 // win8-aura doesn't support multiple display.
1696 if (!SupportsMultipleDisplays())
1697 return;
1699 UpdateDisplay("800x600,800x600");
1700 ASSERT_EQ(2U, Shell::GetAllRootWindows().size());
1702 aura::Window* second_root = Shell::GetAllRootWindows()[1];
1704 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT, second_root);
1705 ASSERT_EQ(SHELF_ALIGNMENT_LEFT,
1706 Shell::GetInstance()->GetShelfAlignment(second_root));
1708 // Initially, app list and browser shortcut are added.
1709 EXPECT_EQ(2, model_->item_count());
1710 int browser_index = model_->GetItemIndexForType(TYPE_BROWSER_SHORTCUT);
1711 EXPECT_GT(browser_index, 0);
1713 Shelf* secondary_shelf = Shelf::ForWindow(second_root);
1714 ShelfView* shelf_view_for_secondary =
1715 ShelfTestAPI(secondary_shelf).shelf_view();
1717 ShelfViewTestAPI test_api_for_secondary_shelf_view(shelf_view_for_secondary);
1718 ShelfButton* button =
1719 test_api_for_secondary_shelf_view.GetButton(browser_index);
1721 // Fetch the start point of dragging.
1722 gfx::Point start_point = button->GetBoundsInScreen().CenterPoint();
1723 ::wm::ConvertPointFromScreen(second_root, &start_point);
1725 ui::test::EventGenerator generator(second_root, start_point);
1727 // Rip off the browser item.
1728 generator.PressLeftButton();
1729 generator.MoveMouseTo(start_point.x() + 400, start_point.y());
1730 test_api_for_secondary_shelf_view.RunMessageLoopUntilAnimationsDone();
1731 EXPECT_TRUE(test_api_for_secondary_shelf_view.IsRippedOffFromShelf());
1734 // Checks various drag and drop operations from OverflowBubble to Shelf.
1735 TEST_F(ShelfViewTest, CheckDragAndDropFromOverflowBubbleToShelf) {
1736 AddButtonsUntilOverflow();
1737 // Add one more button to prevent the overflow bubble to disappear upon
1738 // dragging an item out on windows (flakiness, see crbug.com/425097).
1739 AddAppShortcut();
1741 TestDraggingAnItemFromOverflowToShelf(false);
1742 TestDraggingAnItemFromOverflowToShelf(true);
1745 // Tests that the AppListButton renders as active in response to touches.
1746 TEST_F(ShelfViewTest, AppListButtonTouchFeedback) {
1747 AppListButton* app_list_button =
1748 static_cast<AppListButton*>(shelf_view_->GetAppListButtonView());
1749 EXPECT_FALSE(app_list_button->draw_background_as_active());
1751 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1752 generator.set_current_location(app_list_button->
1753 GetBoundsInScreen().CenterPoint());
1754 generator.PressTouch();
1755 EXPECT_TRUE(app_list_button->draw_background_as_active());
1757 generator.ReleaseTouch();
1758 EXPECT_FALSE(app_list_button->draw_background_as_active());
1759 EXPECT_TRUE(Shell::GetInstance()->GetAppListTargetVisibility());
1762 // Tests that a touch that slides out of the bounds of the AppListButton leads
1763 // to the end of rendering an active state.
1764 TEST_F(ShelfViewTest, AppListButtonTouchFeedbackCancellation) {
1765 AppListButton* app_list_button =
1766 static_cast<AppListButton*>(shelf_view_->GetAppListButtonView());
1767 EXPECT_FALSE(app_list_button->draw_background_as_active());
1769 ui::test::EventGenerator generator(Shell::GetPrimaryRootWindow());
1770 generator.set_current_location(app_list_button->
1771 GetBoundsInScreen().CenterPoint());
1772 generator.PressTouch();
1773 EXPECT_TRUE(app_list_button->draw_background_as_active());
1775 gfx::Point moved_point(app_list_button->GetBoundsInScreen().right() + 1,
1776 app_list_button->
1777 GetBoundsInScreen().CenterPoint().y());
1778 generator.MoveTouch(moved_point);
1779 EXPECT_FALSE(app_list_button->draw_background_as_active());
1781 generator.set_current_location(moved_point);
1782 generator.ReleaseTouch();
1783 EXPECT_FALSE(app_list_button->draw_background_as_active());
1784 EXPECT_FALSE(Shell::GetInstance()->GetAppListTargetVisibility());
1787 // Verifies that Launcher_ButtonPressed_* UMA user actions are recorded when an
1788 // item is selected.
1789 TEST_F(ShelfViewTest,
1790 Launcher_ButtonPressedUserActionsRecordedWhenItemSelected) {
1791 base::UserActionTester user_action_tester;
1793 ShelfID browser_shelf_id = model_->items()[browser_index_].id;
1794 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1795 item_manager_->SetShelfItemDelegate(
1796 browser_shelf_id,
1797 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1799 SimulateClick(browser_index_);
1800 EXPECT_EQ(1,
1801 user_action_tester.GetActionCount("Launcher_ButtonPressed_Mouse"));
1804 // Verifies that Launcher_*Task UMA user actions are recorded when an item is
1805 // selected.
1806 TEST_F(ShelfViewTest, Launcher_TaskUserActionsRecordedWhenItemSelected) {
1807 base::UserActionTester user_action_tester;
1809 ShelfID browser_shelf_id = model_->items()[browser_index_].id;
1810 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1811 selection_tracker->set_item_selected_action(
1812 ShelfItemDelegate::kNewWindowCreated);
1813 item_manager_->SetShelfItemDelegate(
1814 browser_shelf_id,
1815 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1817 SimulateClick(browser_index_);
1818 EXPECT_EQ(1, user_action_tester.GetActionCount("Launcher_LaunchTask"));
1821 // Verifies that metrics are recorded when an item is minimized and subsequently
1822 // activated.
1823 TEST_F(ShelfViewTest,
1824 VerifyMetricsAreRecordedWhenAnItemIsMinimizedAndActivated) {
1825 base::HistogramTester histogram_tester;
1827 ShelfID browser_shelf_id = model_->items()[browser_index_].id;
1828 ShelfItemSelectionTracker* selection_tracker = new ShelfItemSelectionTracker;
1829 item_manager_->SetShelfItemDelegate(
1830 browser_shelf_id,
1831 scoped_ptr<ShelfItemDelegate>(selection_tracker).Pass());
1833 selection_tracker->set_item_selected_action(
1834 ShelfItemDelegate::kExistingWindowMinimized);
1835 SimulateClick(browser_index_);
1837 selection_tracker->set_item_selected_action(
1838 ShelfItemDelegate::kExistingWindowActivated);
1839 SimulateClick(browser_index_);
1841 histogram_tester.ExpectTotalCount(
1842 kTimeBetweenWindowMinimizedAndActivatedActionsHistogramName, 1);
1845 class ShelfViewVisibleBoundsTest : public ShelfViewTest,
1846 public testing::WithParamInterface<bool> {
1847 public:
1848 ShelfViewVisibleBoundsTest() : text_direction_change_(GetParam()) {}
1850 void CheckAllItemsAreInBounds() {
1851 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1852 gfx::Rect shelf_bounds = shelf_view_->GetBoundsInScreen();
1853 EXPECT_TRUE(shelf_bounds.Contains(visible_bounds));
1854 for (int i = 0; i < test_api_->GetButtonCount(); ++i)
1855 if (ShelfButton* button = test_api_->GetButton(i))
1856 EXPECT_TRUE(visible_bounds.Contains(button->GetBoundsInScreen()));
1857 CheckAppListButtonIsInBounds();
1860 void CheckAppListButtonIsInBounds() {
1861 gfx::Rect visible_bounds = shelf_view_->GetVisibleItemsBoundsInScreen();
1862 gfx::Rect app_list_button_bounds = shelf_view_->GetAppListButtonView()->
1863 GetBoundsInScreen();
1864 EXPECT_TRUE(visible_bounds.Contains(app_list_button_bounds));
1867 private:
1868 ScopedTextDirectionChange text_direction_change_;
1870 DISALLOW_COPY_AND_ASSIGN(ShelfViewVisibleBoundsTest);
1873 TEST_P(ShelfViewVisibleBoundsTest, ItemsAreInBounds) {
1874 // Adding elements leaving some empty space.
1875 for (int i = 0; i < 3; i++) {
1876 AddAppShortcut();
1878 test_api_->RunMessageLoopUntilAnimationsDone();
1879 EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
1880 CheckAllItemsAreInBounds();
1881 // Same for overflow case.
1882 while (!test_api_->IsOverflowButtonVisible()) {
1883 AddAppShortcut();
1885 test_api_->RunMessageLoopUntilAnimationsDone();
1886 CheckAllItemsAreInBounds();
1889 INSTANTIATE_TEST_CASE_P(LtrRtl, ShelfViewTextDirectionTest, testing::Bool());
1890 INSTANTIATE_TEST_CASE_P(VisibleBounds, ShelfViewVisibleBoundsTest,
1891 testing::Bool());
1893 } // namespace test
1894 } // namespace ash