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"
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/user_action_tester.h"
38 #include "base/time/time.h"
39 #include "ui/aura/test/aura_test_base.h"
40 #include "ui/aura/window.h"
41 #include "ui/aura/window_event_dispatcher.h"
42 #include "ui/base/l10n/l10n_util.h"
43 #include "ui/compositor/layer.h"
44 #include "ui/events/event.h"
45 #include "ui/events/event_constants.h"
46 #include "ui/events/event_utils.h"
47 #include "ui/events/test/event_generator.h"
48 #include "ui/gfx/geometry/point.h"
49 #include "ui/views/view_model.h"
50 #include "ui/views/widget/widget.h"
51 #include "ui/views/widget/widget_delegate.h"
52 #include "ui/wm/core/coordinate_conversion.h"
57 ////////////////////////////////////////////////////////////////////////////////
58 // ShelfIconObserver tests.
60 class TestShelfIconObserver
: public ShelfIconObserver
{
62 explicit TestShelfIconObserver(Shelf
* shelf
)
64 change_notified_(false) {
66 shelf_
->AddIconObserver(this);
69 ~TestShelfIconObserver() override
{
71 shelf_
->RemoveIconObserver(this);
74 // ShelfIconObserver implementation.
75 void OnShelfIconPositionsChanged() override
{ change_notified_
= true; }
77 int change_notified() const { return change_notified_
; }
78 void Reset() { change_notified_
= false; }
82 bool change_notified_
;
84 DISALLOW_COPY_AND_ASSIGN(TestShelfIconObserver
);
87 class ShelfViewIconObserverTest
: public AshTestBase
{
89 ShelfViewIconObserverTest() {}
90 ~ShelfViewIconObserverTest() override
{}
92 void SetUp() override
{
94 Shelf
* shelf
= Shelf::ForPrimaryDisplay();
95 observer_
.reset(new TestShelfIconObserver(shelf
));
97 shelf_view_test_
.reset(
98 new ShelfViewTestAPI(ShelfTestAPI(shelf
).shelf_view()));
99 shelf_view_test_
->SetAnimationDuration(1);
102 void TearDown() override
{
104 AshTestBase::TearDown();
107 TestShelfIconObserver
* observer() { return observer_
.get(); }
109 ShelfViewTestAPI
* shelf_view_test() {
110 return shelf_view_test_
.get();
113 Shelf
* ShelfForSecondaryDisplay() {
114 return Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
118 scoped_ptr
<TestShelfIconObserver
> observer_
;
119 scoped_ptr
<ShelfViewTestAPI
> shelf_view_test_
;
121 DISALLOW_COPY_AND_ASSIGN(ShelfViewIconObserverTest
);
124 // TestShelfItemDelegate which tracks whether it gets selected.
125 class ShelfItemSelectionTracker
: public TestShelfItemDelegate
{
127 ShelfItemSelectionTracker()
128 : TestShelfItemDelegate(NULL
),
130 item_selected_action_(kNoAction
) {}
132 ~ShelfItemSelectionTracker() override
{}
134 // Resets to the initial state.
135 void Reset() { selected_
= false; }
137 void set_item_selected_action(
138 ShelfItemDelegate::PerformedAction item_selected_action
) {
139 item_selected_action_
= item_selected_action
;
142 // Returns true if the delegate was selected.
147 // TestShelfItemDelegate:
148 ShelfItemDelegate::PerformedAction
ItemSelected(
149 const ui::Event
& event
) override
{
151 return item_selected_action_
;
157 // The action returned from ItemSelected(const ui::Event&).
158 ShelfItemDelegate::PerformedAction item_selected_action_
;
160 DISALLOW_COPY_AND_ASSIGN(ShelfItemSelectionTracker
);
163 TEST_F(ShelfViewIconObserverTest
, AddRemove
) {
164 TestShelfDelegate
* shelf_delegate
= TestShelfDelegate::instance();
165 ASSERT_TRUE(shelf_delegate
);
167 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_WINDOW
);
168 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
169 params
.bounds
= gfx::Rect(0, 0, 200, 200);
170 params
.context
= CurrentContext();
172 scoped_ptr
<views::Widget
> widget(new views::Widget());
173 widget
->Init(params
);
174 shelf_delegate
->AddShelfItem(widget
->GetNativeWindow());
175 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
176 EXPECT_TRUE(observer()->change_notified());
180 widget
->GetNativeWindow()->parent()->RemoveChild(widget
->GetNativeWindow());
181 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
182 EXPECT_TRUE(observer()->change_notified());
186 // Sometimes fails on trybots on win7_aura. http://crbug.com/177135
188 #define MAYBE_AddRemoveWithMultipleDisplays \
189 DISABLED_AddRemoveWithMultipleDisplays
191 #define MAYBE_AddRemoveWithMultipleDisplays \
192 AddRemoveWithMultipleDisplays
194 // Make sure creating/deleting an window on one displays notifies a
195 // shelf on external display as well as one on primary.
196 TEST_F(ShelfViewIconObserverTest
, MAYBE_AddRemoveWithMultipleDisplays
) {
197 UpdateDisplay("400x400,400x400");
198 TestShelfIconObserver
second_observer(ShelfForSecondaryDisplay());
200 TestShelfDelegate
* shelf_delegate
= TestShelfDelegate::instance();
201 ASSERT_TRUE(shelf_delegate
);
203 views::Widget::InitParams
params(views::Widget::InitParams::TYPE_WINDOW
);
204 params
.ownership
= views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET
;
205 params
.bounds
= gfx::Rect(0, 0, 200, 200);
206 params
.context
= CurrentContext();
208 scoped_ptr
<views::Widget
> widget(new views::Widget());
209 widget
->Init(params
);
210 shelf_delegate
->AddShelfItem(widget
->GetNativeWindow());
211 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
212 EXPECT_TRUE(observer()->change_notified());
213 EXPECT_TRUE(second_observer
.change_notified());
215 second_observer
.Reset();
217 widget
->GetNativeWindow()->parent()->RemoveChild(widget
->GetNativeWindow());
218 shelf_view_test()->RunMessageLoopUntilAnimationsDone();
219 EXPECT_TRUE(observer()->change_notified());
220 EXPECT_TRUE(second_observer
.change_notified());
223 second_observer
.Reset();
226 TEST_F(ShelfViewIconObserverTest
, BoundsChanged
) {
227 ShelfWidget
* widget
= Shell::GetPrimaryRootWindowController()->shelf();
228 Shelf
* shelf
= Shelf::ForPrimaryDisplay();
229 gfx::Size shelf_size
= widget
->GetWindowBoundsInScreen().size();
230 shelf_size
.set_width(shelf_size
.width() / 2);
231 ASSERT_GT(shelf_size
.width(), 0);
232 shelf
->SetShelfViewBounds(gfx::Rect(shelf_size
));
233 // No animation happens for ShelfView bounds change.
234 EXPECT_TRUE(observer()->change_notified());
238 ////////////////////////////////////////////////////////////////////////////////
241 // Simple ShelfDelegate implmentation for ShelfViewTest.OverflowBubbleSize
242 // and CheckDragAndDropFromOverflowBubbleToShelf
243 class TestShelfDelegateForShelfView
: public ShelfDelegate
{
245 explicit TestShelfDelegateForShelfView(ShelfModel
* model
)
247 ~TestShelfDelegateForShelfView() override
{}
249 // ShelfDelegate overrides:
250 void OnShelfCreated(Shelf
* shelf
) override
{}
252 void OnShelfDestroyed(Shelf
* shelf
) override
{}
254 ShelfID
GetShelfIDForAppID(const std::string
& app_id
) override
{
256 EXPECT_TRUE(base::StringToInt(app_id
, &id
));
260 const std::string
& GetAppIDForShelfID(ShelfID id
) override
{
261 // Use |app_id_| member variable because returning a reference to local
262 // variable is not allowed.
263 app_id_
= base::IntToString(id
);
267 void PinAppWithID(const std::string
& app_id
) override
{}
269 bool IsAppPinned(const std::string
& app_id
) override
{
270 // Returns true for ShelfViewTest.OverflowBubbleSize. To test ripping off in
271 // that test, an item is already pinned state.
275 bool CanPin() const override
{ return true; }
277 void UnpinAppWithID(const std::string
& app_id
) override
{
279 EXPECT_TRUE(base::StringToInt(app_id
, &id
));
281 int index
= model_
->ItemIndexByID(id
);
284 model_
->RemoveItemAt(index
);
290 // Temp member variable for returning a value. See the comment in the
291 // GetAppIDForShelfID().
294 DISALLOW_COPY_AND_ASSIGN(TestShelfDelegateForShelfView
);
297 class ShelfViewTest
: public AshTestBase
{
303 item_manager_(NULL
) {}
304 ~ShelfViewTest() override
{}
306 void SetUp() override
{
307 AshTestBase::SetUp();
308 test::ShellTestApi
test_api(Shell::GetInstance());
309 model_
= test_api
.shelf_model();
310 Shelf
* shelf
= Shelf::ForPrimaryDisplay();
311 shelf_view_
= ShelfTestAPI(shelf
).shelf_view();
313 // The bounds should be big enough for 4 buttons + overflow chevron.
314 shelf_view_
->SetBounds(0, 0, 500, kShelfSize
);
316 test_api_
.reset(new ShelfViewTestAPI(shelf_view_
));
317 test_api_
->SetAnimationDuration(1); // Speeds up animation for test.
319 item_manager_
= Shell::GetInstance()->shelf_item_delegate_manager();
320 DCHECK(item_manager_
);
322 // Add browser shortcut shelf item at index 0 for test.
323 AddBrowserShortcut();
326 void TearDown() override
{
328 AshTestBase::TearDown();
332 void CreateAndSetShelfItemDelegateForID(ShelfID id
) {
333 scoped_ptr
<ShelfItemDelegate
> delegate(new TestShelfItemDelegate(NULL
));
334 item_manager_
->SetShelfItemDelegate(id
, delegate
.Pass());
337 ShelfID
AddBrowserShortcut() {
338 ShelfItem browser_shortcut
;
339 browser_shortcut
.type
= TYPE_BROWSER_SHORTCUT
;
341 ShelfID id
= model_
->next_id();
342 model_
->AddAt(browser_index_
, browser_shortcut
);
343 CreateAndSetShelfItemDelegateForID(id
);
344 test_api_
->RunMessageLoopUntilAnimationsDone();
348 ShelfID
AddAppShortcut() {
350 item
.type
= TYPE_APP_SHORTCUT
;
351 item
.status
= STATUS_CLOSED
;
353 ShelfID id
= model_
->next_id();
355 CreateAndSetShelfItemDelegateForID(id
);
356 test_api_
->RunMessageLoopUntilAnimationsDone();
361 ShelfID id
= AddPanelNoWait();
362 test_api_
->RunMessageLoopUntilAnimationsDone();
366 ShelfID
AddPlatformAppNoWait() {
368 item
.type
= TYPE_PLATFORM_APP
;
369 item
.status
= STATUS_RUNNING
;
371 ShelfID id
= model_
->next_id();
373 CreateAndSetShelfItemDelegateForID(id
);
377 ShelfID
AddPanelNoWait() {
379 item
.type
= TYPE_APP_PANEL
;
380 item
.status
= STATUS_RUNNING
;
382 ShelfID id
= model_
->next_id();
384 CreateAndSetShelfItemDelegateForID(id
);
388 ShelfID
AddPlatformApp() {
389 ShelfID id
= AddPlatformAppNoWait();
390 test_api_
->RunMessageLoopUntilAnimationsDone();
394 void RemoveByID(ShelfID id
) {
395 model_
->RemoveItemAt(model_
->ItemIndexByID(id
));
396 test_api_
->RunMessageLoopUntilAnimationsDone();
399 ShelfButton
* GetButtonByID(ShelfID id
) {
400 int index
= model_
->ItemIndexByID(id
);
401 return test_api_
->GetButton(index
);
404 ShelfItem
GetItemByID(ShelfID id
) {
405 ShelfItems::const_iterator items
= model_
->ItemByID(id
);
410 const std::vector
<std::pair
<ShelfID
, views::View
*> >& id_map
) {
411 size_t map_index
= 0;
412 for (size_t model_index
= 0;
413 model_index
< model_
->items().size();
415 ShelfItem item
= model_
->items()[model_index
];
416 ShelfID id
= item
.id
;
417 EXPECT_EQ(id_map
[map_index
].first
, id
);
418 EXPECT_EQ(id_map
[map_index
].second
, GetButtonByID(id
));
421 ASSERT_EQ(map_index
, id_map
.size());
424 void VerifyShelfItemBoundsAreValid() {
425 for (int i
= 0; i
<= test_api_
->GetLastVisibleIndex(); ++i
) {
426 if (test_api_
->GetButton(i
)) {
427 gfx::Rect shelf_view_bounds
= shelf_view_
->GetLocalBounds();
428 gfx::Rect item_bounds
= test_api_
->GetBoundsByIndex(i
);
429 EXPECT_GE(item_bounds
.x(), 0);
430 EXPECT_GE(item_bounds
.y(), 0);
431 EXPECT_LE(item_bounds
.right(), shelf_view_bounds
.width());
432 EXPECT_LE(item_bounds
.bottom(), shelf_view_bounds
.height());
437 ShelfButton
* SimulateButtonPressed(ShelfButtonHost::Pointer pointer
,
439 ShelfButtonHost
* button_host
= shelf_view_
;
440 ShelfButton
* button
= test_api_
->GetButton(button_index
);
441 ui::MouseEvent
click_event(ui::ET_MOUSE_PRESSED
, gfx::Point(),
442 button
->GetBoundsInScreen().origin(),
443 ui::EventTimeForNow(), 0, 0);
444 button_host
->PointerPressedOnButton(button
, pointer
, click_event
);
448 // Simulates a single mouse click.
449 void SimulateClick(int button_index
) {
450 ShelfButtonHost
* button_host
= shelf_view_
;
451 ShelfButton
* button
=
452 SimulateButtonPressed(ShelfButtonHost::MOUSE
, button_index
);
453 ui::MouseEvent
release_event(ui::ET_MOUSE_RELEASED
, gfx::Point(),
454 button
->GetBoundsInScreen().origin(),
455 ui::EventTimeForNow(), 0, 0);
456 test_api_
->ButtonPressed(button
, release_event
);
457 button_host
->PointerReleasedOnButton(button
, ShelfButtonHost::MOUSE
, false);
460 // Simulates the second click of a double click.
461 void SimulateDoubleClick(int button_index
) {
462 ShelfButtonHost
* button_host
= shelf_view_
;
463 ShelfButton
* button
=
464 SimulateButtonPressed(ShelfButtonHost::MOUSE
, button_index
);
465 ui::MouseEvent
release_event(ui::ET_MOUSE_RELEASED
, gfx::Point(),
466 button
->GetBoundsInScreen().origin(),
467 ui::EventTimeForNow(), ui::EF_IS_DOUBLE_CLICK
,
469 test_api_
->ButtonPressed(button
, release_event
);
470 button_host
->PointerReleasedOnButton(button
, ShelfButtonHost::MOUSE
, false);
473 views::View
* SimulateDrag(ShelfButtonHost::Pointer pointer
,
475 int destination_index
) {
476 ShelfButtonHost
* button_host
= shelf_view_
;
477 views::View
* button
= SimulateButtonPressed(pointer
, button_index
);
480 views::View
* destination
= test_api_
->GetButton(destination_index
);
481 ui::MouseEvent
drag_event(
482 ui::ET_MOUSE_DRAGGED
, gfx::Point(destination
->x() - button
->x(),
483 destination
->y() - button
->y()),
484 destination
->GetBoundsInScreen().origin(), ui::EventTimeForNow(), 0, 0);
485 button_host
->PointerDraggedOnButton(button
, pointer
, drag_event
);
489 void SetupForDragTest(
490 std::vector
<std::pair
<ShelfID
, views::View
*> >* id_map
) {
491 // Initialize |id_map| with the automatically-created shelf buttons.
492 for (size_t i
= 0; i
< model_
->items().size(); ++i
) {
493 ShelfButton
* button
= test_api_
->GetButton(i
);
494 id_map
->push_back(std::make_pair(model_
->items()[i
].id
, button
));
496 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map
));
498 // Add 5 app shelf buttons for testing.
499 for (int i
= 0; i
< 5; ++i
) {
500 ShelfID id
= AddAppShortcut();
501 // App Icon is located at index 0, and browser shortcut is located at
502 // index 1. So we should start to add app shortcut at index 2.
503 id_map
->insert(id_map
->begin() + (i
+ browser_index_
+ 1),
504 std::make_pair(id
, GetButtonByID(id
)));
506 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map
));
509 views::View
* GetTooltipAnchorView() {
510 return shelf_view_
->tooltip_manager()->anchor_
;
513 void AddButtonsUntilOverflow() {
515 while (!test_api_
->IsOverflowButtonVisible()) {
518 ASSERT_LT(items_added
, 10000);
523 shelf_view_
->tooltip_manager()->ShowInternal();
526 void TestDraggingAnItemFromOverflowToShelf(bool cancel
) {
527 test_api_
->ShowOverflowBubble();
528 ASSERT_TRUE(test_api_
->overflow_bubble() &&
529 test_api_
->overflow_bubble()->IsShowing());
531 ash::test::ShelfViewTestAPI
test_api_for_overflow(
532 test_api_
->overflow_bubble()->shelf_view());
534 int total_item_count
= model_
->item_count();
536 int last_visible_item_id_in_shelf
=
537 GetItemId(test_api_
->GetLastVisibleIndex());
538 int second_last_visible_item_id_in_shelf
=
539 GetItemId(test_api_
->GetLastVisibleIndex() - 1);
540 int first_visible_item_id_in_overflow
=
541 GetItemId(test_api_for_overflow
.GetFirstVisibleIndex());
542 int second_last_visible_item_id_in_overflow
=
543 GetItemId(test_api_for_overflow
.GetLastVisibleIndex() - 1);
545 int drag_item_index
=
546 test_api_for_overflow
.GetLastVisibleIndex();
547 ShelfID drag_item_id
= GetItemId(drag_item_index
);
548 ShelfButton
* drag_button
= test_api_for_overflow
.GetButton(drag_item_index
);
549 gfx::Point center_point_of_drag_item
=
550 drag_button
->GetBoundsInScreen().CenterPoint();
552 ui::test::EventGenerator
generator(ash::Shell::GetPrimaryRootWindow(),
553 center_point_of_drag_item
);
554 // Rip an item off to OverflowBubble.
555 generator
.PressLeftButton();
556 gfx::Point
rip_off_point(center_point_of_drag_item
.x(), 0);
557 generator
.MoveMouseTo(rip_off_point
);
558 test_api_for_overflow
.RunMessageLoopUntilAnimationsDone();
559 ASSERT_TRUE(test_api_for_overflow
.IsRippedOffFromShelf());
560 ASSERT_FALSE(test_api_for_overflow
.DraggedItemFromOverflowToShelf());
562 // Move a dragged item into Shelf at |drop_index|.
564 gfx::Point drop_point
=
565 test_api_
->GetButton(drop_index
)->GetBoundsInScreen().CenterPoint();
566 int item_width
= test_api_for_overflow
.GetButtonSize();
567 // To insert at |drop_index|, more smaller x-axis value of |drop_point|
569 gfx::Point
modified_drop_point(drop_point
.x() - item_width
/ 4,
571 generator
.MoveMouseTo(modified_drop_point
);
572 test_api_for_overflow
.RunMessageLoopUntilAnimationsDone();
573 test_api_
->RunMessageLoopUntilAnimationsDone();
574 ASSERT_TRUE(test_api_for_overflow
.IsRippedOffFromShelf());
575 ASSERT_TRUE(test_api_for_overflow
.DraggedItemFromOverflowToShelf());
578 drag_button
->OnMouseCaptureLost();
580 generator
.ReleaseLeftButton();
582 test_api_for_overflow
.RunMessageLoopUntilAnimationsDone();
583 test_api_
->RunMessageLoopUntilAnimationsDone();
584 ASSERT_FALSE(test_api_for_overflow
.IsRippedOffFromShelf());
585 ASSERT_FALSE(test_api_for_overflow
.DraggedItemFromOverflowToShelf());
587 // Compare pre-stored items' id with newly positioned items' after dragging
588 // is canceled or finished.
590 EXPECT_EQ(last_visible_item_id_in_shelf
,
591 GetItemId(test_api_
->GetLastVisibleIndex()));
592 EXPECT_EQ(second_last_visible_item_id_in_shelf
,
593 GetItemId(test_api_
->GetLastVisibleIndex() - 1));
594 EXPECT_EQ(first_visible_item_id_in_overflow
,
595 GetItemId(test_api_for_overflow
.GetFirstVisibleIndex()));
596 EXPECT_EQ(second_last_visible_item_id_in_overflow
,
597 GetItemId(test_api_for_overflow
.GetLastVisibleIndex() - 1));
599 EXPECT_EQ(drag_item_id
, GetItemId(drop_index
));
600 EXPECT_EQ(total_item_count
, model_
->item_count());
601 EXPECT_EQ(last_visible_item_id_in_shelf
,
602 GetItemId(test_api_for_overflow
.GetFirstVisibleIndex()));
603 EXPECT_EQ(second_last_visible_item_id_in_shelf
,
604 GetItemId(test_api_
->GetLastVisibleIndex()));
605 EXPECT_EQ(first_visible_item_id_in_overflow
,
606 GetItemId(test_api_for_overflow
.GetFirstVisibleIndex() + 1));
607 EXPECT_EQ(second_last_visible_item_id_in_overflow
,
608 GetItemId(test_api_for_overflow
.GetLastVisibleIndex()));
612 // Returns the item's ShelfID at |index|.
613 ShelfID
GetItemId(int index
) {
615 return model_
->items()[index
].id
;
618 void ReplaceShelfDelegateForRipOffTest() {
619 // Replace ShelfDelegate.
620 test::ShellTestApi
test_api(Shell::GetInstance());
621 test_api
.SetShelfDelegate(NULL
);
622 ShelfDelegate
* delegate
= new TestShelfDelegateForShelfView(model_
);
623 test_api
.SetShelfDelegate(delegate
);
624 test::ShelfTestAPI(Shelf::ForPrimaryDisplay()).SetShelfDelegate(delegate
);
625 test_api_
->SetShelfDelegate(delegate
);
629 ShelfView
* shelf_view_
;
631 ShelfItemDelegateManager
* item_manager_
;
633 scoped_ptr
<ShelfViewTestAPI
> test_api_
;
636 DISALLOW_COPY_AND_ASSIGN(ShelfViewTest
);
639 class ScopedTextDirectionChange
{
641 explicit ScopedTextDirectionChange(bool is_rtl
) : is_rtl_(is_rtl
) {
642 original_locale_
= l10n_util::GetApplicationLocale(std::string());
644 base::i18n::SetICUDefaultLocale("he");
645 CheckTextDirectionIsCorrect();
648 ~ScopedTextDirectionChange() {
650 base::i18n::SetICUDefaultLocale(original_locale_
);
654 void CheckTextDirectionIsCorrect() {
655 ASSERT_EQ(is_rtl_
, base::i18n::IsRTL());
659 std::string original_locale_
;
662 class ShelfViewTextDirectionTest
663 : public ShelfViewTest
,
664 public testing::WithParamInterface
<bool> {
666 ShelfViewTextDirectionTest() : text_direction_change_(GetParam()) {}
667 virtual ~ShelfViewTextDirectionTest() {}
669 void SetUp() override
{ ShelfViewTest::SetUp(); }
671 void TearDown() override
{ ShelfViewTest::TearDown(); }
674 ScopedTextDirectionChange text_direction_change_
;
676 DISALLOW_COPY_AND_ASSIGN(ShelfViewTextDirectionTest
);
679 // Checks that the ideal item icon bounds match the view's bounds in the screen
680 // in both LTR and RTL.
681 TEST_P(ShelfViewTextDirectionTest
, IdealBoundsOfItemIcon
) {
682 ShelfID id
= AddPlatformApp();
683 ShelfButton
* button
= GetButtonByID(id
);
684 gfx::Rect item_bounds
= button
->GetBoundsInScreen();
685 gfx::Point icon_offset
= button
->GetIconBounds().origin();
686 item_bounds
.Offset(icon_offset
.OffsetFromOrigin());
687 gfx::Rect ideal_bounds
= shelf_view_
->GetIdealBoundsOfItemIcon(id
);
688 gfx::Point screen_origin
;
689 views::View::ConvertPointToScreen(shelf_view_
, &screen_origin
);
690 ideal_bounds
.Offset(screen_origin
.x(), screen_origin
.y());
691 EXPECT_EQ(item_bounds
.x(), ideal_bounds
.x());
692 EXPECT_EQ(item_bounds
.y(), ideal_bounds
.y());
695 // Check that items in the overflow area are returning the overflow button as
697 TEST_F(ShelfViewTest
, OverflowButtonBounds
) {
698 ShelfID first_id
= AddPlatformApp();
699 ShelfID overflow_id
= AddPlatformApp();
701 while (!test_api_
->IsOverflowButtonVisible()) {
702 // Added button is visible after animation while in this loop.
703 EXPECT_TRUE(GetButtonByID(overflow_id
)->visible());
704 overflow_id
= AddPlatformApp();
706 ASSERT_LT(items_added
, 10000);
708 ShelfID last_id
= AddPlatformApp();
710 gfx::Rect first_bounds
= shelf_view_
->GetIdealBoundsOfItemIcon(first_id
);
711 gfx::Rect overflow_bounds
=
712 shelf_view_
->GetIdealBoundsOfItemIcon(overflow_id
);
713 gfx::Rect last_bounds
= shelf_view_
->GetIdealBoundsOfItemIcon(last_id
);
715 // Check that all items have the same size and that the overflow items are
716 // identical whereas the first one does not match either of them.
717 EXPECT_EQ(first_bounds
.size().ToString(), last_bounds
.size().ToString());
718 EXPECT_NE(first_bounds
.ToString(), last_bounds
.ToString());
719 EXPECT_EQ(overflow_bounds
.ToString(), last_bounds
.ToString());
722 // Checks that shelf view contents are considered in the correct drag group.
723 TEST_F(ShelfViewTest
, EnforceDragType
) {
724 EXPECT_TRUE(test_api_
->SameDragType(TYPE_PLATFORM_APP
, TYPE_PLATFORM_APP
));
725 EXPECT_FALSE(test_api_
->SameDragType(TYPE_PLATFORM_APP
, TYPE_APP_SHORTCUT
));
726 EXPECT_FALSE(test_api_
->SameDragType(TYPE_PLATFORM_APP
,
727 TYPE_BROWSER_SHORTCUT
));
728 EXPECT_FALSE(test_api_
->SameDragType(TYPE_PLATFORM_APP
, TYPE_WINDOWED_APP
));
729 EXPECT_FALSE(test_api_
->SameDragType(TYPE_PLATFORM_APP
, TYPE_APP_LIST
));
730 EXPECT_FALSE(test_api_
->SameDragType(TYPE_PLATFORM_APP
, TYPE_APP_PANEL
));
732 EXPECT_TRUE(test_api_
->SameDragType(TYPE_APP_SHORTCUT
, TYPE_APP_SHORTCUT
));
733 EXPECT_TRUE(test_api_
->SameDragType(TYPE_APP_SHORTCUT
,
734 TYPE_BROWSER_SHORTCUT
));
735 EXPECT_FALSE(test_api_
->SameDragType(TYPE_APP_SHORTCUT
,
737 EXPECT_FALSE(test_api_
->SameDragType(TYPE_APP_SHORTCUT
, TYPE_APP_LIST
));
738 EXPECT_FALSE(test_api_
->SameDragType(TYPE_APP_SHORTCUT
, TYPE_APP_PANEL
));
740 EXPECT_TRUE(test_api_
->SameDragType(TYPE_BROWSER_SHORTCUT
,
741 TYPE_BROWSER_SHORTCUT
));
742 EXPECT_FALSE(test_api_
->SameDragType(TYPE_BROWSER_SHORTCUT
,
744 EXPECT_FALSE(test_api_
->SameDragType(TYPE_BROWSER_SHORTCUT
, TYPE_APP_LIST
));
745 EXPECT_FALSE(test_api_
->SameDragType(TYPE_BROWSER_SHORTCUT
, TYPE_APP_PANEL
));
747 EXPECT_TRUE(test_api_
->SameDragType(TYPE_WINDOWED_APP
, TYPE_WINDOWED_APP
));
748 EXPECT_FALSE(test_api_
->SameDragType(TYPE_WINDOWED_APP
, TYPE_APP_LIST
));
749 EXPECT_FALSE(test_api_
->SameDragType(TYPE_WINDOWED_APP
, TYPE_APP_PANEL
));
751 EXPECT_TRUE(test_api_
->SameDragType(TYPE_APP_LIST
, TYPE_APP_LIST
));
752 EXPECT_FALSE(test_api_
->SameDragType(TYPE_APP_LIST
, TYPE_APP_PANEL
));
754 EXPECT_TRUE(test_api_
->SameDragType(TYPE_APP_PANEL
, TYPE_APP_PANEL
));
757 // Adds platform app button until overflow and verifies that the last added
758 // platform app button is hidden.
759 TEST_F(ShelfViewTest
, AddBrowserUntilOverflow
) {
760 // All buttons should be visible.
761 ASSERT_EQ(test_api_
->GetButtonCount(),
762 test_api_
->GetLastVisibleIndex() + 1);
764 // Add platform app button until overflow.
766 ShelfID last_added
= AddPlatformApp();
767 while (!test_api_
->IsOverflowButtonVisible()) {
768 // Added button is visible after animation while in this loop.
769 EXPECT_TRUE(GetButtonByID(last_added
)->visible());
771 last_added
= AddPlatformApp();
773 ASSERT_LT(items_added
, 10000);
776 // The last added button should be invisible.
777 EXPECT_FALSE(GetButtonByID(last_added
)->visible());
780 // Adds one platform app button then adds app shortcut until overflow. Verifies
781 // that the browser button gets hidden on overflow and last added app shortcut
783 TEST_F(ShelfViewTest
, AddAppShortcutWithBrowserButtonUntilOverflow
) {
784 // All buttons should be visible.
785 ASSERT_EQ(test_api_
->GetButtonCount(),
786 test_api_
->GetLastVisibleIndex() + 1);
788 ShelfID browser_button_id
= AddPlatformApp();
790 // Add app shortcut until overflow.
792 ShelfID last_added
= AddAppShortcut();
793 while (!test_api_
->IsOverflowButtonVisible()) {
794 // Added button is visible after animation while in this loop.
795 EXPECT_TRUE(GetButtonByID(last_added
)->visible());
797 last_added
= AddAppShortcut();
799 ASSERT_LT(items_added
, 10000);
802 // And the platform app button is invisible.
803 EXPECT_FALSE(GetButtonByID(browser_button_id
)->visible());
806 TEST_F(ShelfViewTest
, AddPanelHidesPlatformAppButton
) {
807 // All buttons should be visible.
808 ASSERT_EQ(test_api_
->GetButtonCount(),
809 test_api_
->GetLastVisibleIndex() + 1);
811 // Add platform app button until overflow, remember last visible platform app
814 ShelfID first_added
= AddPlatformApp();
815 EXPECT_TRUE(GetButtonByID(first_added
)->visible());
817 ShelfID added
= AddPlatformApp();
818 if (test_api_
->IsOverflowButtonVisible()) {
819 EXPECT_FALSE(GetButtonByID(added
)->visible());
824 ASSERT_LT(items_added
, 10000);
827 ShelfID panel
= AddPanel();
828 EXPECT_TRUE(test_api_
->IsOverflowButtonVisible());
831 EXPECT_FALSE(test_api_
->IsOverflowButtonVisible());
834 // When there are more panels then platform app buttons we should hide panels
835 // rather than platform apps.
836 TEST_F(ShelfViewTest
, PlatformAppHidesExcessPanels
) {
837 // All buttons should be visible.
838 ASSERT_EQ(test_api_
->GetButtonCount(),
839 test_api_
->GetLastVisibleIndex() + 1);
841 // Add platform app button.
842 ShelfID platform_app
= AddPlatformApp();
843 ShelfID first_panel
= AddPanel();
845 EXPECT_TRUE(GetButtonByID(platform_app
)->visible());
846 EXPECT_TRUE(GetButtonByID(first_panel
)->visible());
848 // Add panels until there is an overflow.
849 ShelfID last_panel
= first_panel
;
851 while (!test_api_
->IsOverflowButtonVisible()) {
852 last_panel
= AddPanel();
854 ASSERT_LT(items_added
, 10000);
857 // The first panel should now be hidden by the new platform apps needing
859 EXPECT_FALSE(GetButtonByID(first_panel
)->visible());
860 EXPECT_TRUE(GetButtonByID(last_panel
)->visible());
861 EXPECT_TRUE(GetButtonByID(platform_app
)->visible());
863 // Adding platform apps should eventually begin to hide platform apps. We will
864 // add platform apps until either the last panel or platform app is hidden.
866 while (GetButtonByID(platform_app
)->visible() &&
867 GetButtonByID(last_panel
)->visible()) {
868 platform_app
= AddPlatformApp();
870 ASSERT_LT(items_added
, 10000);
872 EXPECT_TRUE(GetButtonByID(last_panel
)->visible());
873 EXPECT_FALSE(GetButtonByID(platform_app
)->visible());
876 // Making sure that no buttons on the shelf will ever overlap after adding many
878 TEST_F(ShelfViewTest
, AssertNoButtonsOverlap
) {
879 std::vector
<ShelfID
> button_ids
;
880 // Add app icons until the overflow button is visible.
881 while (!test_api_
->IsOverflowButtonVisible()) {
882 ShelfID id
= AddPlatformApp();
883 button_ids
.push_back(id
);
885 ASSERT_LT(button_ids
.size(), 10000U);
886 ASSERT_GT(button_ids
.size(), 2U);
888 // Remove 2 icons to make more room for panel icons, the overflow button
890 for (int i
= 0; i
< 2; ++i
) {
891 ShelfID id
= button_ids
.back();
893 button_ids
.pop_back();
895 EXPECT_FALSE(test_api_
->IsOverflowButtonVisible());
896 EXPECT_TRUE(GetButtonByID(button_ids
.back())->visible());
898 // Add 20 panel icons, and expect to have overflow.
899 for (int i
= 0; i
< 20; ++i
) {
900 ShelfID id
= AddPanel();
901 button_ids
.push_back(id
);
903 ASSERT_LT(button_ids
.size(), 10000U);
904 EXPECT_TRUE(test_api_
->IsOverflowButtonVisible());
906 // Test that any two successive visible icons never overlap in all shelf
908 const ShelfAlignment kAlignments
[] = {
909 SHELF_ALIGNMENT_LEFT
,
910 SHELF_ALIGNMENT_RIGHT
,
912 SHELF_ALIGNMENT_BOTTOM
915 for (ShelfAlignment alignment
: kAlignments
) {
916 EXPECT_TRUE(shelf_view_
->shelf_layout_manager()->SetAlignment(alignment
));
917 // For every 2 successive visible icons, expect that their bounds don't
919 for (int i
= 1; i
< test_api_
->GetButtonCount() - 1; ++i
) {
920 if (!(test_api_
->GetButton(i
)->visible() &&
921 test_api_
->GetButton(i
+ 1)->visible())) {
925 const gfx::Rect
& bounds1
= test_api_
->GetBoundsByIndex(i
);
926 const gfx::Rect
& bounds2
= test_api_
->GetBoundsByIndex(i
+ 1);
927 EXPECT_FALSE(bounds1
.Intersects(bounds2
));
932 // Adds button until overflow then removes first added one. Verifies that
933 // the last added one changes from invisible to visible and overflow
935 TEST_F(ShelfViewTest
, RemoveButtonRevealsOverflowed
) {
936 // All buttons should be visible.
937 ASSERT_EQ(test_api_
->GetButtonCount(),
938 test_api_
->GetLastVisibleIndex() + 1);
940 // Add platform app buttons until overflow.
942 ShelfID first_added
= AddPlatformApp();
943 ShelfID last_added
= first_added
;
944 while (!test_api_
->IsOverflowButtonVisible()) {
945 last_added
= AddPlatformApp();
947 ASSERT_LT(items_added
, 10000);
950 // Expect add more than 1 button. First added is visible and last is not.
951 EXPECT_NE(first_added
, last_added
);
952 EXPECT_TRUE(GetButtonByID(first_added
)->visible());
953 EXPECT_FALSE(GetButtonByID(last_added
)->visible());
955 // Remove first added.
956 RemoveByID(first_added
);
958 // Last added button becomes visible and overflow chevron is gone.
959 EXPECT_TRUE(GetButtonByID(last_added
)->visible());
960 EXPECT_EQ(1.0f
, GetButtonByID(last_added
)->layer()->opacity());
961 EXPECT_FALSE(test_api_
->IsOverflowButtonVisible());
964 // Verifies that remove last overflowed button should hide overflow chevron.
965 TEST_F(ShelfViewTest
, RemoveLastOverflowed
) {
966 // All buttons should be visible.
967 ASSERT_EQ(test_api_
->GetButtonCount(),
968 test_api_
->GetLastVisibleIndex() + 1);
970 // Add platform app button until overflow.
972 ShelfID last_added
= AddPlatformApp();
973 while (!test_api_
->IsOverflowButtonVisible()) {
974 last_added
= AddPlatformApp();
976 ASSERT_LT(items_added
, 10000);
979 RemoveByID(last_added
);
980 EXPECT_FALSE(test_api_
->IsOverflowButtonVisible());
983 // Adds platform app button without waiting for animation to finish and verifies
984 // that all added buttons are visible.
985 TEST_F(ShelfViewTest
, AddButtonQuickly
) {
986 // All buttons should be visible.
987 ASSERT_EQ(test_api_
->GetButtonCount(),
988 test_api_
->GetLastVisibleIndex() + 1);
990 // Add a few platform buttons quickly without wait for animation.
992 while (!test_api_
->IsOverflowButtonVisible()) {
993 AddPlatformAppNoWait();
995 ASSERT_LT(added_count
, 10000);
998 // ShelfView should be big enough to hold at least 3 new buttons.
999 ASSERT_GE(added_count
, 3);
1001 // Wait for the last animation to finish.
1002 test_api_
->RunMessageLoopUntilAnimationsDone();
1004 // Verifies non-overflow buttons are visible.
1005 for (int i
= 0; i
<= test_api_
->GetLastVisibleIndex(); ++i
) {
1006 ShelfButton
* button
= test_api_
->GetButton(i
);
1008 EXPECT_TRUE(button
->visible()) << "button index=" << i
;
1009 EXPECT_EQ(1.0f
, button
->layer()->opacity()) << "button index=" << i
;
1014 // Check that model changes are handled correctly while a shelf icon is being
1016 TEST_F(ShelfViewTest
, ModelChangesWhileDragging
) {
1017 ShelfButtonHost
* button_host
= shelf_view_
;
1019 std::vector
<std::pair
<ShelfID
, views::View
*> > id_map
;
1020 SetupForDragTest(&id_map
);
1022 // Dragging browser shortcut at index 1.
1023 EXPECT_TRUE(model_
->items()[1].type
== TYPE_BROWSER_SHORTCUT
);
1024 views::View
* dragged_button
= SimulateDrag(ShelfButtonHost::MOUSE
, 1, 3);
1025 std::rotate(id_map
.begin() + 1,
1027 id_map
.begin() + 4);
1028 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1029 button_host
->PointerReleasedOnButton(
1030 dragged_button
, ShelfButtonHost::MOUSE
, false);
1031 EXPECT_TRUE(model_
->items()[3].type
== TYPE_BROWSER_SHORTCUT
);
1033 // Dragging changes model order.
1034 dragged_button
= SimulateDrag(ShelfButtonHost::MOUSE
, 1, 3);
1035 std::rotate(id_map
.begin() + 1,
1037 id_map
.begin() + 4);
1038 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1040 // Cancelling the drag operation restores previous order.
1041 button_host
->PointerReleasedOnButton(
1042 dragged_button
, ShelfButtonHost::MOUSE
, true);
1043 std::rotate(id_map
.begin() + 1,
1045 id_map
.begin() + 4);
1046 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1048 // Deleting an item keeps the remaining intact.
1049 dragged_button
= SimulateDrag(ShelfButtonHost::MOUSE
, 1, 3);
1050 model_
->RemoveItemAt(1);
1051 id_map
.erase(id_map
.begin() + 1);
1052 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1053 button_host
->PointerReleasedOnButton(
1054 dragged_button
, ShelfButtonHost::MOUSE
, false);
1056 // Adding a shelf item cancels the drag and respects the order.
1057 dragged_button
= SimulateDrag(ShelfButtonHost::MOUSE
, 1, 3);
1058 ShelfID new_id
= AddAppShortcut();
1059 id_map
.insert(id_map
.begin() + 6,
1060 std::make_pair(new_id
, GetButtonByID(new_id
)));
1061 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1062 button_host
->PointerReleasedOnButton(
1063 dragged_button
, ShelfButtonHost::MOUSE
, false);
1065 // Adding a shelf item at the end (i.e. a panel) canels drag and respects
1067 dragged_button
= SimulateDrag(ShelfButtonHost::MOUSE
, 1, 3);
1068 new_id
= AddPanel();
1069 id_map
.insert(id_map
.begin() + 7,
1070 std::make_pair(new_id
, GetButtonByID(new_id
)));
1071 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1072 button_host
->PointerReleasedOnButton(
1073 dragged_button
, ShelfButtonHost::MOUSE
, false);
1076 // Check that 2nd drag from the other pointer would be ignored.
1077 TEST_F(ShelfViewTest
, SimultaneousDrag
) {
1078 ShelfButtonHost
* button_host
= shelf_view_
;
1080 std::vector
<std::pair
<ShelfID
, views::View
*> > id_map
;
1081 SetupForDragTest(&id_map
);
1083 // Start a mouse drag.
1084 views::View
* dragged_button_mouse
=
1085 SimulateDrag(ShelfButtonHost::MOUSE
, 1, 3);
1086 std::rotate(id_map
.begin() + 1,
1088 id_map
.begin() + 4);
1089 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1090 // Attempt a touch drag before the mouse drag finishes.
1091 views::View
* dragged_button_touch
=
1092 SimulateDrag(ShelfButtonHost::TOUCH
, 4, 2);
1094 // Nothing changes since 2nd drag is ignored.
1095 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1097 // Finish the mouse drag.
1098 button_host
->PointerReleasedOnButton(
1099 dragged_button_mouse
, ShelfButtonHost::MOUSE
, false);
1100 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1102 // Now start a touch drag.
1103 dragged_button_touch
= SimulateDrag(ShelfButtonHost::TOUCH
, 4, 2);
1104 std::rotate(id_map
.begin() + 3,
1106 id_map
.begin() + 5);
1107 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1109 // And attempt a mouse drag before the touch drag finishes.
1110 dragged_button_mouse
= SimulateDrag(ShelfButtonHost::MOUSE
, 1, 2);
1112 // Nothing changes since 2nd drag is ignored.
1113 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1115 button_host
->PointerReleasedOnButton(
1116 dragged_button_touch
, ShelfButtonHost::TOUCH
, false);
1117 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1120 // Check that clicking first on one item and then dragging another works as
1122 TEST_F(ShelfViewTest
, ClickOneDragAnother
) {
1123 ShelfButtonHost
* button_host
= shelf_view_
;
1125 std::vector
<std::pair
<ShelfID
, views::View
*> > id_map
;
1126 SetupForDragTest(&id_map
);
1128 // A click on item 1 is simulated.
1131 // Dragging browser index at 0 should change the model order correctly.
1132 EXPECT_TRUE(model_
->items()[1].type
== TYPE_BROWSER_SHORTCUT
);
1133 views::View
* dragged_button
= SimulateDrag(ShelfButtonHost::MOUSE
, 1, 3);
1134 std::rotate(id_map
.begin() + 1,
1136 id_map
.begin() + 4);
1137 ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map
));
1138 button_host
->PointerReleasedOnButton(
1139 dragged_button
, ShelfButtonHost::MOUSE
, false);
1140 EXPECT_TRUE(model_
->items()[3].type
== TYPE_BROWSER_SHORTCUT
);
1143 // Tests that double-clicking an item does not activate it twice.
1144 TEST_F(ShelfViewTest
, ClickingTwiceActivatesOnce
) {
1145 // Watch for selection of the browser shortcut.
1146 ShelfID browser_shelf_id
= model_
->items()[browser_index_
].id
;
1147 ShelfItemSelectionTracker
* selection_tracker
= new ShelfItemSelectionTracker
;
1148 item_manager_
->SetShelfItemDelegate(
1150 scoped_ptr
<ShelfItemDelegate
>(selection_tracker
).Pass());
1152 // A single click selects the item.
1153 SimulateClick(browser_index_
);
1154 EXPECT_TRUE(selection_tracker
->WasSelected());
1156 // A double-click does not select the item.
1157 selection_tracker
->Reset();
1158 SimulateDoubleClick(browser_index_
);
1159 EXPECT_FALSE(selection_tracker
->WasSelected());
1162 // Check that clicking an item and jittering the mouse a bit still selects the
1164 TEST_F(ShelfViewTest
, ClickAndMoveSlightly
) {
1165 std::vector
<std::pair
<ShelfID
, views::View
*> > id_map
;
1166 SetupForDragTest(&id_map
);
1168 ShelfID shelf_id
= (id_map
.begin() + 1)->first
;
1169 views::View
* button
= (id_map
.begin() + 1)->second
;
1171 // Replace the ShelfItemDelegate for |shelf_id| with one which tracks whether
1172 // the shelf item gets selected.
1173 ShelfItemSelectionTracker
* selection_tracker
= new ShelfItemSelectionTracker
;
1174 item_manager_
->SetShelfItemDelegate(
1176 scoped_ptr
<ShelfItemDelegate
>(selection_tracker
).Pass());
1178 gfx::Vector2d
press_offset(5, 30);
1179 gfx::Point press_location
= gfx::Point() + press_offset
;
1180 gfx::Point press_location_in_screen
=
1181 button
->GetBoundsInScreen().origin() + press_offset
;
1183 ui::MouseEvent
click_event(ui::ET_MOUSE_PRESSED
, press_location
,
1184 press_location_in_screen
, ui::EventTimeForNow(),
1185 ui::EF_LEFT_MOUSE_BUTTON
, 0);
1186 button
->OnMousePressed(click_event
);
1188 ui::MouseEvent
drag_event1(
1189 ui::ET_MOUSE_DRAGGED
, press_location
+ gfx::Vector2d(0, 1),
1190 press_location_in_screen
+ gfx::Vector2d(0, 1), ui::EventTimeForNow(),
1191 ui::EF_LEFT_MOUSE_BUTTON
, 0);
1192 button
->OnMouseDragged(drag_event1
);
1194 ui::MouseEvent
drag_event2(
1195 ui::ET_MOUSE_DRAGGED
, press_location
+ gfx::Vector2d(-1, 0),
1196 press_location_in_screen
+ gfx::Vector2d(-1, 0), ui::EventTimeForNow(),
1197 ui::EF_LEFT_MOUSE_BUTTON
, 0);
1198 button
->OnMouseDragged(drag_event2
);
1200 ui::MouseEvent
release_event(
1201 ui::ET_MOUSE_RELEASED
, press_location
+ gfx::Vector2d(-1, 0),
1202 press_location_in_screen
+ gfx::Vector2d(-1, 0), ui::EventTimeForNow(),
1203 ui::EF_LEFT_MOUSE_BUTTON
, 0);
1204 button
->OnMouseReleased(release_event
);
1206 EXPECT_TRUE(selection_tracker
->WasSelected());
1209 // Confirm that item status changes are reflected in the buttons.
1210 TEST_F(ShelfViewTest
, ShelfItemStatus
) {
1211 // All buttons should be visible.
1212 ASSERT_EQ(test_api_
->GetButtonCount(),
1213 test_api_
->GetLastVisibleIndex() + 1);
1215 // Add platform app button.
1216 ShelfID last_added
= AddPlatformApp();
1217 ShelfItem item
= GetItemByID(last_added
);
1218 int index
= model_
->ItemIndexByID(last_added
);
1219 ShelfButton
* button
= GetButtonByID(last_added
);
1220 ASSERT_EQ(ShelfButton::STATE_RUNNING
, button
->state());
1221 item
.status
= STATUS_ACTIVE
;
1222 model_
->Set(index
, item
);
1223 ASSERT_EQ(ShelfButton::STATE_ACTIVE
, button
->state());
1224 item
.status
= STATUS_ATTENTION
;
1225 model_
->Set(index
, item
);
1226 ASSERT_EQ(ShelfButton::STATE_ATTENTION
, button
->state());
1229 // Confirm that item status changes are reflected in the buttons
1230 // for platform apps.
1231 TEST_F(ShelfViewTest
, ShelfItemStatusPlatformApp
) {
1232 // All buttons should be visible.
1233 ASSERT_EQ(test_api_
->GetButtonCount(),
1234 test_api_
->GetLastVisibleIndex() + 1);
1236 // Add platform app button.
1237 ShelfID last_added
= AddPlatformApp();
1238 ShelfItem item
= GetItemByID(last_added
);
1239 int index
= model_
->ItemIndexByID(last_added
);
1240 ShelfButton
* button
= GetButtonByID(last_added
);
1241 ASSERT_EQ(ShelfButton::STATE_RUNNING
, button
->state());
1242 item
.status
= STATUS_ACTIVE
;
1243 model_
->Set(index
, item
);
1244 ASSERT_EQ(ShelfButton::STATE_ACTIVE
, button
->state());
1245 item
.status
= STATUS_ATTENTION
;
1246 model_
->Set(index
, item
);
1247 ASSERT_EQ(ShelfButton::STATE_ATTENTION
, button
->state());
1250 // Confirm that shelf item bounds are correctly updated on shelf changes.
1251 TEST_F(ShelfViewTest
, ShelfItemBoundsCheck
) {
1252 VerifyShelfItemBoundsAreValid();
1253 shelf_view_
->shelf_layout_manager()->SetAutoHideBehavior(
1254 SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS
);
1255 test_api_
->RunMessageLoopUntilAnimationsDone();
1256 VerifyShelfItemBoundsAreValid();
1257 shelf_view_
->shelf_layout_manager()->SetAutoHideBehavior(
1258 SHELF_AUTO_HIDE_BEHAVIOR_NEVER
);
1259 test_api_
->RunMessageLoopUntilAnimationsDone();
1260 VerifyShelfItemBoundsAreValid();
1263 TEST_F(ShelfViewTest
, ShelfTooltipTest
) {
1264 ASSERT_EQ(test_api_
->GetLastVisibleIndex() + 1,
1265 test_api_
->GetButtonCount());
1267 // Prepare some items to the shelf.
1268 ShelfID app_button_id
= AddAppShortcut();
1269 ShelfID platform_button_id
= AddPlatformApp();
1271 ShelfButton
* app_button
= GetButtonByID(app_button_id
);
1272 ShelfButton
* platform_button
= GetButtonByID(platform_button_id
);
1274 ShelfButtonHost
* button_host
= shelf_view_
;
1275 ShelfTooltipManager
* tooltip_manager
= shelf_view_
->tooltip_manager();
1277 button_host
->MouseEnteredButton(app_button
);
1278 // There's a delay to show the tooltip, so it's not visible yet.
1279 EXPECT_FALSE(tooltip_manager
->IsVisible());
1280 EXPECT_EQ(app_button
, GetTooltipAnchorView());
1283 EXPECT_TRUE(tooltip_manager
->IsVisible());
1285 // Once it's visible, it keeps visibility and is pointing to the same
1287 button_host
->MouseExitedButton(app_button
);
1288 EXPECT_TRUE(tooltip_manager
->IsVisible());
1289 EXPECT_EQ(app_button
, GetTooltipAnchorView());
1291 // When entered to another item, it switches to the new item. There is no
1292 // delay for the visibility.
1293 button_host
->MouseEnteredButton(platform_button
);
1294 EXPECT_TRUE(tooltip_manager
->IsVisible());
1295 EXPECT_EQ(platform_button
, GetTooltipAnchorView());
1297 button_host
->MouseExitedButton(platform_button
);
1298 tooltip_manager
->Close();
1300 // Next time: enter app_button -> move immediately to tab_button.
1301 button_host
->MouseEnteredButton(app_button
);
1302 button_host
->MouseExitedButton(app_button
);
1303 button_host
->MouseEnteredButton(platform_button
);
1304 EXPECT_FALSE(tooltip_manager
->IsVisible());
1305 EXPECT_EQ(platform_button
, GetTooltipAnchorView());
1308 // Verify a fix for crash caused by a tooltip update for a deletedshelf
1309 // button, see crbug.com/288838.
1310 TEST_F(ShelfViewTest
, RemovingItemClosesTooltip
) {
1311 ShelfButtonHost
* button_host
= shelf_view_
;
1312 ShelfTooltipManager
* tooltip_manager
= shelf_view_
->tooltip_manager();
1314 // Add an item to the shelf.
1315 ShelfID app_button_id
= AddAppShortcut();
1316 ShelfButton
* app_button
= GetButtonByID(app_button_id
);
1318 // Spawn a tooltip on that item.
1319 button_host
->MouseEnteredButton(app_button
);
1321 EXPECT_TRUE(tooltip_manager
->IsVisible());
1323 // Remove the app shortcut while the tooltip is open. The tooltip should be
1325 RemoveByID(app_button_id
);
1326 EXPECT_FALSE(tooltip_manager
->IsVisible());
1328 // Change the shelf layout. This should not crash.
1329 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT
,
1330 Shell::GetPrimaryRootWindow());
1333 // Changing the shelf alignment closes any open tooltip.
1334 TEST_F(ShelfViewTest
, ShelfAlignmentClosesTooltip
) {
1335 ShelfButtonHost
* button_host
= shelf_view_
;
1336 ShelfTooltipManager
* tooltip_manager
= shelf_view_
->tooltip_manager();
1338 // Add an item to the shelf.
1339 ShelfID app_button_id
= AddAppShortcut();
1340 ShelfButton
* app_button
= GetButtonByID(app_button_id
);
1342 // Spawn a tooltip on the item.
1343 button_host
->MouseEnteredButton(app_button
);
1345 EXPECT_TRUE(tooltip_manager
->IsVisible());
1347 // Changing shelf alignment hides the tooltip.
1348 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT
,
1349 Shell::GetPrimaryRootWindow());
1350 EXPECT_FALSE(tooltip_manager
->IsVisible());
1353 TEST_F(ShelfViewTest
, ShouldHideTooltipTest
) {
1354 ShelfID app_button_id
= AddAppShortcut();
1355 ShelfID platform_button_id
= AddPlatformApp();
1357 // The tooltip shouldn't hide if the mouse is on normal buttons.
1358 for (int i
= 0; i
< test_api_
->GetButtonCount(); i
++) {
1359 ShelfButton
* button
= test_api_
->GetButton(i
);
1363 EXPECT_FALSE(shelf_view_
->ShouldHideTooltip(
1364 button
->GetMirroredBounds().CenterPoint()))
1365 << "ShelfView tries to hide on button " << i
;
1368 // The tooltip should not hide on the app-list button.
1369 views::View
* app_list_button
= shelf_view_
->GetAppListButtonView();
1370 EXPECT_FALSE(shelf_view_
->ShouldHideTooltip(
1371 app_list_button
->GetMirroredBounds().CenterPoint()));
1373 // The tooltip shouldn't hide if the mouse is in the gap between two buttons.
1374 gfx::Rect app_button_rect
= GetButtonByID(app_button_id
)->GetMirroredBounds();
1375 gfx::Rect platform_button_rect
=
1376 GetButtonByID(platform_button_id
)->GetMirroredBounds();
1377 ASSERT_FALSE(app_button_rect
.Intersects(platform_button_rect
));
1378 EXPECT_FALSE(shelf_view_
->ShouldHideTooltip(
1379 gfx::UnionRects(app_button_rect
, platform_button_rect
).CenterPoint()));
1381 // The tooltip should hide if it's outside of all buttons.
1383 for (int i
= 0; i
< test_api_
->GetButtonCount(); i
++) {
1384 ShelfButton
* button
= test_api_
->GetButton(i
);
1388 all_area
.Union(button
->GetMirroredBounds());
1390 all_area
.Union(shelf_view_
->GetAppListButtonView()->GetMirroredBounds());
1391 EXPECT_FALSE(shelf_view_
->ShouldHideTooltip(all_area
.origin()));
1392 EXPECT_FALSE(shelf_view_
->ShouldHideTooltip(
1393 gfx::Point(all_area
.right() - 1, all_area
.bottom() - 1)));
1394 EXPECT_TRUE(shelf_view_
->ShouldHideTooltip(
1395 gfx::Point(all_area
.right(), all_area
.y())));
1396 EXPECT_TRUE(shelf_view_
->ShouldHideTooltip(
1397 gfx::Point(all_area
.x() - 1, all_area
.y())));
1398 EXPECT_TRUE(shelf_view_
->ShouldHideTooltip(
1399 gfx::Point(all_area
.x(), all_area
.y() - 1)));
1400 EXPECT_TRUE(shelf_view_
->ShouldHideTooltip(
1401 gfx::Point(all_area
.x(), all_area
.bottom())));
1404 TEST_F(ShelfViewTest
, ShouldHideTooltipWithAppListWindowTest
) {
1405 Shell::GetInstance()->ShowAppList(NULL
);
1406 ASSERT_TRUE(Shell::GetInstance()->GetAppListWindow());
1408 // The tooltip shouldn't hide if the mouse is on normal buttons.
1409 for (int i
= 1; i
< test_api_
->GetButtonCount(); i
++) {
1410 ShelfButton
* button
= test_api_
->GetButton(i
);
1414 EXPECT_FALSE(shelf_view_
->ShouldHideTooltip(
1415 button
->GetMirroredBounds().CenterPoint()))
1416 << "ShelfView tries to hide on button " << i
;
1419 // The tooltip should hide on the app-list button.
1420 views::View
* app_list_button
= shelf_view_
->GetAppListButtonView();
1421 EXPECT_TRUE(shelf_view_
->ShouldHideTooltip(
1422 app_list_button
->GetMirroredBounds().CenterPoint()));
1425 // Test that by moving the mouse cursor off the button onto the bubble it closes
1427 TEST_F(ShelfViewTest
, ShouldHideTooltipWhenHoveringOnTooltip
) {
1428 ShelfTooltipManager
* tooltip_manager
= shelf_view_
->tooltip_manager();
1429 tooltip_manager
->CreateZeroDelayTimerForTest();
1430 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
1432 // Move the mouse off any item and check that no tooltip is shown.
1433 generator
.MoveMouseTo(gfx::Point(0, 0));
1434 EXPECT_FALSE(tooltip_manager
->IsVisible());
1436 // Move the mouse over the button and check that it is visible.
1437 views::View
* app_list_button
= shelf_view_
->GetAppListButtonView();
1438 gfx::Rect bounds
= app_list_button
->GetBoundsInScreen();
1439 generator
.MoveMouseTo(bounds
.CenterPoint());
1440 // Wait for the timer to go off.
1441 RunAllPendingInMessageLoop();
1442 EXPECT_TRUE(tooltip_manager
->IsVisible());
1444 // Move the mouse cursor slightly to the right of the item. The tooltip should
1446 generator
.MoveMouseBy(bounds
.width() / 2 + 5, 0);
1447 // Make sure there is no delayed close.
1448 RunAllPendingInMessageLoop();
1449 EXPECT_TRUE(tooltip_manager
->IsVisible());
1451 // Move back - it should still stay open.
1452 generator
.MoveMouseBy(-(bounds
.width() / 2 + 5), 0);
1453 // Make sure there is no delayed close.
1454 RunAllPendingInMessageLoop();
1455 EXPECT_TRUE(tooltip_manager
->IsVisible());
1457 // Now move the mouse cursor slightly above the item - so that it is over the
1458 // tooltip bubble. Now it should disappear.
1459 generator
.MoveMouseBy(0, -(bounds
.height() / 2 + 5));
1460 // Wait until the delayed close kicked in.
1461 RunAllPendingInMessageLoop();
1462 EXPECT_FALSE(tooltip_manager
->IsVisible());
1465 // Resizing shelf view while an add animation without fade-in is running,
1466 // which happens when overflow happens. App list button should end up in its
1467 // new ideal bounds.
1468 TEST_F(ShelfViewTest
, ResizeDuringOverflowAddAnimation
) {
1469 // All buttons should be visible.
1470 ASSERT_EQ(test_api_
->GetButtonCount(),
1471 test_api_
->GetLastVisibleIndex() + 1);
1473 // Add buttons until overflow. Let the non-overflow add animations finish but
1474 // leave the last running.
1475 int items_added
= 0;
1476 AddPlatformAppNoWait();
1477 while (!test_api_
->IsOverflowButtonVisible()) {
1478 test_api_
->RunMessageLoopUntilAnimationsDone();
1479 AddPlatformAppNoWait();
1481 ASSERT_LT(items_added
, 10000);
1484 // Resize shelf view with that animation running and stay overflown.
1485 gfx::Rect bounds
= shelf_view_
->bounds();
1486 bounds
.set_width(bounds
.width() - kShelfSize
);
1487 shelf_view_
->SetBoundsRect(bounds
);
1488 ASSERT_TRUE(test_api_
->IsOverflowButtonVisible());
1490 // Finish the animation.
1491 test_api_
->RunMessageLoopUntilAnimationsDone();
1493 // App list button should ends up in its new ideal bounds.
1494 const int app_list_button_index
= test_api_
->GetButtonCount() - 1;
1495 const gfx::Rect
& app_list_ideal_bounds
=
1496 test_api_
->GetIdealBoundsByIndex(app_list_button_index
);
1497 const gfx::Rect
& app_list_bounds
=
1498 test_api_
->GetBoundsByIndex(app_list_button_index
);
1499 EXPECT_EQ(app_list_ideal_bounds
, app_list_bounds
);
1502 // Checks the overflow bubble size when an item is ripped off and re-inserted.
1503 TEST_F(ShelfViewTest
, OverflowBubbleSize
) {
1504 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1505 ReplaceShelfDelegateForRipOffTest();
1507 AddButtonsUntilOverflow();
1508 // Add one more button to prevent the overflow bubble to disappear upon
1509 // dragging an item out on windows (flakiness, see crbug.com/436131).
1512 // Show overflow bubble.
1513 test_api_
->ShowOverflowBubble();
1514 ASSERT_TRUE(test_api_
->overflow_bubble() &&
1515 test_api_
->overflow_bubble()->IsShowing());
1517 ShelfViewTestAPI
test_for_overflow_view(
1518 test_api_
->overflow_bubble()->shelf_view());
1520 int ripped_index
= test_for_overflow_view
.GetLastVisibleIndex();
1521 gfx::Size bubble_size
= test_for_overflow_view
.GetPreferredSize();
1522 int item_width
= test_for_overflow_view
.GetButtonSize() +
1523 test_for_overflow_view
.GetButtonSpacing();
1525 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow(),
1527 ShelfButton
* button
= test_for_overflow_view
.GetButton(ripped_index
);
1528 // Rip off the last visible item.
1529 gfx::Point start_point
= button
->GetBoundsInScreen().CenterPoint();
1530 gfx::Point
rip_off_point(start_point
.x(), 0);
1531 generator
.MoveMouseTo(start_point
.x(), start_point
.y());
1532 base::MessageLoop::current()->RunUntilIdle();
1533 generator
.PressLeftButton();
1534 base::MessageLoop::current()->RunUntilIdle();
1535 generator
.MoveMouseTo(rip_off_point
.x(), rip_off_point
.y());
1536 base::MessageLoop::current()->RunUntilIdle();
1537 test_for_overflow_view
.RunMessageLoopUntilAnimationsDone();
1539 // Check the overflow bubble size when an item is ripped off.
1540 EXPECT_EQ(bubble_size
.width() - item_width
,
1541 test_for_overflow_view
.GetPreferredSize().width());
1542 ASSERT_TRUE(test_api_
->overflow_bubble() &&
1543 test_api_
->overflow_bubble()->IsShowing());
1545 // Re-insert an item into the overflow bubble.
1546 int first_index
= test_for_overflow_view
.GetFirstVisibleIndex();
1547 button
= test_for_overflow_view
.GetButton(first_index
);
1549 // Check the bubble size after an item is re-inserted.
1550 generator
.MoveMouseTo(button
->GetBoundsInScreen().CenterPoint());
1551 test_for_overflow_view
.RunMessageLoopUntilAnimationsDone();
1552 EXPECT_EQ(bubble_size
.width(),
1553 test_for_overflow_view
.GetPreferredSize().width());
1555 generator
.ReleaseLeftButton();
1556 test_for_overflow_view
.RunMessageLoopUntilAnimationsDone();
1557 EXPECT_EQ(bubble_size
.width(),
1558 test_for_overflow_view
.GetPreferredSize().width());
1561 // Check the drag insertion bounds of scrolled overflow bubble.
1562 TEST_F(ShelfViewTest
, CheckDragInsertBoundsOfScrolledOverflowBubble
) {
1563 UpdateDisplay("400x300");
1565 EXPECT_EQ(2, model_
->item_count());
1567 AddButtonsUntilOverflow();
1569 // Show overflow bubble.
1570 test_api_
->ShowOverflowBubble();
1571 ASSERT_TRUE(test_api_
->overflow_bubble() &&
1572 test_api_
->overflow_bubble()->IsShowing());
1574 int item_width
= test_api_
->GetButtonSize() +
1575 test_api_
->GetButtonSpacing();
1576 OverflowBubbleView
* bubble_view
= test_api_
->overflow_bubble()->bubble_view();
1577 test::OverflowBubbleViewTestAPI
bubble_view_api(bubble_view
);
1579 // Add more buttons until OverflowBubble is scrollable and it has 3 invisible
1581 while (bubble_view_api
.GetContentsSize().width() <
1582 (bubble_view
->GetContentsBounds().width() + 3 * item_width
))
1585 ASSERT_TRUE(test_api_
->overflow_bubble() &&
1586 test_api_
->overflow_bubble()->IsShowing());
1588 ShelfViewTestAPI
test_for_overflow_view(
1589 test_api_
->overflow_bubble()->shelf_view());
1590 int first_index
= test_for_overflow_view
.GetFirstVisibleIndex();
1591 int last_index
= test_for_overflow_view
.GetLastVisibleIndex();
1593 ShelfButton
* first_button
= test_for_overflow_view
.GetButton(first_index
);
1594 ShelfButton
* last_button
= test_for_overflow_view
.GetButton(last_index
);
1595 gfx::Point first_point
= first_button
->GetBoundsInScreen().CenterPoint();
1596 gfx::Point last_point
= last_button
->GetBoundsInScreen().CenterPoint();
1597 gfx::Rect drag_reinsert_bounds
=
1598 test_for_overflow_view
.GetBoundsForDragInsertInScreen();
1599 EXPECT_TRUE(drag_reinsert_bounds
.Contains(first_point
));
1600 EXPECT_FALSE(drag_reinsert_bounds
.Contains(last_point
));
1602 // Scrolls sufficiently to show last item.
1603 bubble_view_api
.ScrollByXOffset(3 * item_width
);
1604 drag_reinsert_bounds
=
1605 test_for_overflow_view
.GetBoundsForDragInsertInScreen();
1606 first_point
= first_button
->GetBoundsInScreen().CenterPoint();
1607 last_point
= last_button
->GetBoundsInScreen().CenterPoint();
1608 EXPECT_FALSE(drag_reinsert_bounds
.Contains(first_point
));
1609 EXPECT_TRUE(drag_reinsert_bounds
.Contains(last_point
));
1612 // Check the drag insertion bounds of shelf view in multi monitor environment.
1613 TEST_F(ShelfViewTest
, CheckDragInsertBoundsWithMultiMonitor
) {
1614 // win8-aura doesn't support multiple display.
1615 if (!SupportsMultipleDisplays())
1618 UpdateDisplay("800x600,800x600");
1619 Shelf
* secondary_shelf
= Shelf::ForWindow(Shell::GetAllRootWindows()[1]);
1620 ShelfView
* shelf_view_for_secondary
=
1621 ShelfTestAPI(secondary_shelf
).shelf_view();
1623 // The bounds should be big enough for 4 buttons + overflow chevron.
1624 shelf_view_for_secondary
->SetBounds(0, 0, 500, kShelfSize
);
1626 ShelfViewTestAPI
test_api_for_secondary(shelf_view_for_secondary
);
1627 // Speeds up animation for test.
1628 test_api_for_secondary
.SetAnimationDuration(1);
1630 AddButtonsUntilOverflow();
1632 // Test #1: Test drag insertion bounds of primary shelf.
1633 // Show overflow bubble.
1634 test_api_
->ShowOverflowBubble();
1635 ASSERT_TRUE(test_api_
->overflow_bubble() &&
1636 test_api_
->overflow_bubble()->IsShowing());
1638 ShelfViewTestAPI
test_api_for_overflow_view(
1639 test_api_
->overflow_bubble()->shelf_view());
1641 ShelfButton
* button
= test_api_for_overflow_view
.GetButton(
1642 test_api_for_overflow_view
.GetLastVisibleIndex());
1644 // Checks that a point in shelf is contained in drag insert bounds.
1645 gfx::Point point_in_shelf_view
= button
->GetBoundsInScreen().CenterPoint();
1646 gfx::Rect drag_reinsert_bounds
=
1647 test_api_for_overflow_view
.GetBoundsForDragInsertInScreen();
1648 EXPECT_TRUE(drag_reinsert_bounds
.Contains(point_in_shelf_view
));
1649 // Checks that a point out of shelf is not contained in drag insert bounds.
1650 EXPECT_FALSE(drag_reinsert_bounds
.Contains(
1651 gfx::Point(point_in_shelf_view
.x(), 0)));
1653 // Test #2: Test drag insertion bounds of secondary shelf.
1654 // Show overflow bubble.
1655 test_api_for_secondary
.ShowOverflowBubble();
1656 ASSERT_TRUE(test_api_for_secondary
.overflow_bubble() &&
1657 test_api_for_secondary
.overflow_bubble()->IsShowing());
1659 ShelfViewTestAPI
test_api_for_overflow_view_of_secondary(
1660 test_api_for_secondary
.overflow_bubble()->shelf_view());
1662 ShelfButton
* button_in_secondary
=
1663 test_api_for_overflow_view_of_secondary
.GetButton(
1664 test_api_for_overflow_view_of_secondary
.GetLastVisibleIndex());
1666 // Checks that a point in shelf is contained in drag insert bounds.
1667 gfx::Point point_in_secondary_shelf_view
=
1668 button_in_secondary
->GetBoundsInScreen().CenterPoint();
1669 gfx::Rect drag_reinsert_bounds_in_secondary
=
1670 test_api_for_overflow_view_of_secondary
.GetBoundsForDragInsertInScreen();
1671 EXPECT_TRUE(drag_reinsert_bounds_in_secondary
.Contains(
1672 point_in_secondary_shelf_view
));
1673 // Checks that a point out of shelf is not contained in drag insert bounds.
1674 EXPECT_FALSE(drag_reinsert_bounds_in_secondary
.Contains(
1675 gfx::Point(point_in_secondary_shelf_view
.x(), 0)));
1676 // Checks that a point of overflow bubble in primary shelf should not be
1677 // contained by insert bounds of secondary shelf.
1678 EXPECT_FALSE(drag_reinsert_bounds_in_secondary
.Contains(point_in_shelf_view
));
1681 // Checks the rip an item off from left aligned shelf in secondary monitor.
1682 TEST_F(ShelfViewTest
, CheckRipOffFromLeftShelfAlignmentWithMultiMonitor
) {
1683 // win8-aura doesn't support multiple display.
1684 if (!SupportsMultipleDisplays())
1687 UpdateDisplay("800x600,800x600");
1688 ASSERT_EQ(2U, Shell::GetAllRootWindows().size());
1690 aura::Window
* second_root
= Shell::GetAllRootWindows()[1];
1692 Shell::GetInstance()->SetShelfAlignment(SHELF_ALIGNMENT_LEFT
, second_root
);
1693 ASSERT_EQ(SHELF_ALIGNMENT_LEFT
,
1694 Shell::GetInstance()->GetShelfAlignment(second_root
));
1696 // Initially, app list and browser shortcut are added.
1697 EXPECT_EQ(2, model_
->item_count());
1698 int browser_index
= model_
->GetItemIndexForType(TYPE_BROWSER_SHORTCUT
);
1699 EXPECT_GT(browser_index
, 0);
1701 Shelf
* secondary_shelf
= Shelf::ForWindow(second_root
);
1702 ShelfView
* shelf_view_for_secondary
=
1703 ShelfTestAPI(secondary_shelf
).shelf_view();
1705 ShelfViewTestAPI
test_api_for_secondary_shelf_view(shelf_view_for_secondary
);
1706 ShelfButton
* button
=
1707 test_api_for_secondary_shelf_view
.GetButton(browser_index
);
1709 // Fetch the start point of dragging.
1710 gfx::Point start_point
= button
->GetBoundsInScreen().CenterPoint();
1711 ::wm::ConvertPointFromScreen(second_root
, &start_point
);
1713 ui::test::EventGenerator
generator(second_root
, start_point
);
1715 // Rip off the browser item.
1716 generator
.PressLeftButton();
1717 generator
.MoveMouseTo(start_point
.x() + 400, start_point
.y());
1718 test_api_for_secondary_shelf_view
.RunMessageLoopUntilAnimationsDone();
1719 EXPECT_TRUE(test_api_for_secondary_shelf_view
.IsRippedOffFromShelf());
1722 // Checks various drag and drop operations from OverflowBubble to Shelf.
1723 TEST_F(ShelfViewTest
, CheckDragAndDropFromOverflowBubbleToShelf
) {
1724 // Replace current ShelfDelegate with TestShelfDelegateForShelfView.
1725 ReplaceShelfDelegateForRipOffTest();
1727 AddButtonsUntilOverflow();
1728 // Add one more button to prevent the overflow bubble to disappear upon
1729 // dragging an item out on windows (flakiness, see crbug.com/425097).
1732 TestDraggingAnItemFromOverflowToShelf(false);
1733 TestDraggingAnItemFromOverflowToShelf(true);
1736 // Tests that the AppListButton renders as active in response to touches.
1737 TEST_F(ShelfViewTest
, AppListButtonTouchFeedback
) {
1738 AppListButton
* app_list_button
=
1739 static_cast<AppListButton
*>(shelf_view_
->GetAppListButtonView());
1740 EXPECT_FALSE(app_list_button
->draw_background_as_active());
1742 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
1743 generator
.set_current_location(app_list_button
->
1744 GetBoundsInScreen().CenterPoint());
1745 generator
.PressTouch();
1746 EXPECT_TRUE(app_list_button
->draw_background_as_active());
1748 generator
.ReleaseTouch();
1749 EXPECT_FALSE(app_list_button
->draw_background_as_active());
1750 EXPECT_TRUE(Shell::GetInstance()->GetAppListTargetVisibility());
1753 // Tests that a touch that slides out of the bounds of the AppListButton leads
1754 // to the end of rendering an active state.
1755 TEST_F(ShelfViewTest
, AppListButtonTouchFeedbackCancellation
) {
1756 AppListButton
* app_list_button
=
1757 static_cast<AppListButton
*>(shelf_view_
->GetAppListButtonView());
1758 EXPECT_FALSE(app_list_button
->draw_background_as_active());
1760 ui::test::EventGenerator
generator(Shell::GetPrimaryRootWindow());
1761 generator
.set_current_location(app_list_button
->
1762 GetBoundsInScreen().CenterPoint());
1763 generator
.PressTouch();
1764 EXPECT_TRUE(app_list_button
->draw_background_as_active());
1766 gfx::Point
moved_point(app_list_button
->GetBoundsInScreen().right() + 1,
1768 GetBoundsInScreen().CenterPoint().y());
1769 generator
.MoveTouch(moved_point
);
1770 EXPECT_FALSE(app_list_button
->draw_background_as_active());
1772 generator
.set_current_location(moved_point
);
1773 generator
.ReleaseTouch();
1774 EXPECT_FALSE(app_list_button
->draw_background_as_active());
1775 EXPECT_FALSE(Shell::GetInstance()->GetAppListTargetVisibility());
1778 // Verifies that Launcher_ButtonPressed_* UMA user actions are recorded when an
1779 // item is selected.
1780 TEST_F(ShelfViewTest
,
1781 Launcher_ButtonPressedUserActionsRecordedWhenItemSelected
) {
1782 base::UserActionTester user_action_tester
;
1784 ShelfID browser_shelf_id
= model_
->items()[browser_index_
].id
;
1785 ShelfItemSelectionTracker
* selection_tracker
= new ShelfItemSelectionTracker
;
1786 item_manager_
->SetShelfItemDelegate(
1788 scoped_ptr
<ShelfItemDelegate
>(selection_tracker
).Pass());
1790 SimulateClick(browser_index_
);
1792 user_action_tester
.GetActionCount("Launcher_ButtonPressed_Mouse"));
1795 // Verifies that Launcher_*Task UMA user actions are recorded when an item is
1797 TEST_F(ShelfViewTest
, Launcher_TaskUserActionsRecordedWhenItemSelected
) {
1798 base::UserActionTester user_action_tester
;
1800 ShelfID browser_shelf_id
= model_
->items()[browser_index_
].id
;
1801 ShelfItemSelectionTracker
* selection_tracker
= new ShelfItemSelectionTracker
;
1802 selection_tracker
->set_item_selected_action(
1803 ShelfItemDelegate::kNewWindowCreated
);
1804 item_manager_
->SetShelfItemDelegate(
1806 scoped_ptr
<ShelfItemDelegate
>(selection_tracker
).Pass());
1808 SimulateClick(browser_index_
);
1809 EXPECT_EQ(1, user_action_tester
.GetActionCount("Launcher_LaunchTask"));
1812 // Verifies that a Launcher_ButtonPressed_Mouse UMA user action is recorded when
1813 // an icon is activated by a mouse event.
1814 TEST_F(ShelfViewTest
,
1815 Launcher_ButtonPressed_MouseIsRecordedWhenIconActivatedByMouse
) {
1816 base::UserActionTester user_action_tester
;
1817 ui::MouseEvent
mouse_event(ui::ET_MOUSE_PRESSED
, gfx::Point(), gfx::Point(),
1818 base::TimeDelta(), 0, 0);
1819 test_api_
->RecordIconActivatedSource(mouse_event
);
1821 user_action_tester
.GetActionCount("Launcher_ButtonPressed_Mouse"));
1824 // Verifies that a Launcher_ButtonPressed_Touch UMA user action is recorded when
1825 // an icon is activated by a touch event.
1826 TEST_F(ShelfViewTest
,
1827 Launcher_ButtonPressed_MouseIsRecordedWhenIconActivatedByTouch
) {
1828 base::UserActionTester user_action_tester
;
1829 ui::TouchEvent
touch_event(ui::ET_GESTURE_TAP
, gfx::Point(), 0,
1831 test_api_
->RecordIconActivatedSource(touch_event
);
1833 user_action_tester
.GetActionCount("Launcher_ButtonPressed_Touch"));
1836 // Verifies that a Launcher_LaunchTask UMA user action is recorded when
1837 // selecting an icon causes a new window to be created.
1838 TEST_F(ShelfViewTest
, Launcher_LaunchTaskIsRecordedWhenNewWindowIsCreated
) {
1839 base::UserActionTester user_action_tester
;
1840 test_api_
->RecordIconActivatedAction(ShelfItemDelegate::kNewWindowCreated
);
1841 EXPECT_EQ(1, user_action_tester
.GetActionCount("Launcher_LaunchTask"));
1844 // Verifies that a Launcher_SwitchTask UMA user action is recorded when
1845 // selecting an icon causes an existing window to be activated.
1846 TEST_F(ShelfViewTest
,
1847 Launcher_SwitchTaskIsRecordedWhenExistingWindowIsActivated
) {
1848 base::UserActionTester user_action_tester
;
1849 test_api_
->RecordIconActivatedAction(
1850 ShelfItemDelegate::kExistingWindowActivated
);
1851 EXPECT_EQ(1, user_action_tester
.GetActionCount("Launcher_SwitchTask"));
1854 class ShelfViewVisibleBoundsTest
: public ShelfViewTest
,
1855 public testing::WithParamInterface
<bool> {
1857 ShelfViewVisibleBoundsTest() : text_direction_change_(GetParam()) {}
1859 void CheckAllItemsAreInBounds() {
1860 gfx::Rect visible_bounds
= shelf_view_
->GetVisibleItemsBoundsInScreen();
1861 gfx::Rect shelf_bounds
= shelf_view_
->GetBoundsInScreen();
1862 EXPECT_TRUE(shelf_bounds
.Contains(visible_bounds
));
1863 for (int i
= 0; i
< test_api_
->GetButtonCount(); ++i
)
1864 if (ShelfButton
* button
= test_api_
->GetButton(i
))
1865 EXPECT_TRUE(visible_bounds
.Contains(button
->GetBoundsInScreen()));
1866 CheckAppListButtonIsInBounds();
1869 void CheckAppListButtonIsInBounds() {
1870 gfx::Rect visible_bounds
= shelf_view_
->GetVisibleItemsBoundsInScreen();
1871 gfx::Rect app_list_button_bounds
= shelf_view_
->GetAppListButtonView()->
1872 GetBoundsInScreen();
1873 EXPECT_TRUE(visible_bounds
.Contains(app_list_button_bounds
));
1877 ScopedTextDirectionChange text_direction_change_
;
1879 DISALLOW_COPY_AND_ASSIGN(ShelfViewVisibleBoundsTest
);
1882 TEST_P(ShelfViewVisibleBoundsTest
, ItemsAreInBounds
) {
1883 // Adding elements leaving some empty space.
1884 for (int i
= 0; i
< 3; i
++) {
1887 test_api_
->RunMessageLoopUntilAnimationsDone();
1888 EXPECT_FALSE(test_api_
->IsOverflowButtonVisible());
1889 CheckAllItemsAreInBounds();
1890 // Same for overflow case.
1891 while (!test_api_
->IsOverflowButtonVisible()) {
1894 test_api_
->RunMessageLoopUntilAnimationsDone();
1895 CheckAllItemsAreInBounds();
1898 INSTANTIATE_TEST_CASE_P(LtrRtl
, ShelfViewTextDirectionTest
, testing::Bool());
1899 INSTANTIATE_TEST_CASE_P(VisibleBounds
, ShelfViewVisibleBoundsTest
,