1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/launcher/launcher_view.h"
#include <algorithm>
#include <vector>
#include "ash/launcher/launcher.h"
#include "ash/launcher/launcher_button.h"
#include "ash/launcher/launcher_icon_observer.h"
#include "ash/launcher/launcher_model.h"
#include "ash/launcher/launcher_tooltip_manager.h"
#include "ash/root_window_controller.h"
#include "ash/shelf/shelf_layout_manager.h"
#include "ash/shelf/shelf_widget.h"
#include "ash/shell.h"
#include "ash/shell_window_ids.h"
#include "ash/test/ash_test_base.h"
#include "ash/test/launcher_view_test_api.h"
#include "ash/test/shell_test_api.h"
#include "ash/test/test_launcher_delegate.h"
#include "base/basictypes.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "grit/ash_resources.h"
#include "ui/aura/root_window.h"
#include "ui/aura/test/aura_test_base.h"
#include "ui/aura/window.h"
#include "ui/base/events/event.h"
#include "ui/base/events/event_constants.h"
#include "ui/compositor/layer.h"
#include "ui/views/widget/widget.h"
#include "ui/views/widget/widget_delegate.h"
namespace {
const int kExpectedAppIndex = 1;
}
namespace ash {
namespace test {
////////////////////////////////////////////////////////////////////////////////
// LauncherIconObserver tests.
class TestLauncherIconObserver : public LauncherIconObserver {
public:
explicit TestLauncherIconObserver(Launcher* launcher)
: launcher_(launcher),
change_notified_(false) {
if (launcher_)
launcher_->AddIconObserver(this);
}
virtual ~TestLauncherIconObserver() {
if (launcher_)
launcher_->RemoveIconObserver(this);
}
// LauncherIconObserver implementation.
virtual void OnLauncherIconPositionsChanged() OVERRIDE {
change_notified_ = true;
}
int change_notified() const { return change_notified_; }
void Reset() { change_notified_ = false; }
private:
Launcher* launcher_;
bool change_notified_;
DISALLOW_COPY_AND_ASSIGN(TestLauncherIconObserver);
};
class LauncherViewIconObserverTest : public ash::test::AshTestBase {
public:
LauncherViewIconObserverTest() {}
virtual ~LauncherViewIconObserverTest() {}
virtual void SetUp() OVERRIDE {
AshTestBase::SetUp();
Launcher* launcher = Launcher::ForPrimaryDisplay();
observer_.reset(new TestLauncherIconObserver(launcher));
launcher_view_test_.reset(new LauncherViewTestAPI(
launcher->GetLauncherViewForTest()));
launcher_view_test_->SetAnimationDuration(1);
}
virtual void TearDown() OVERRIDE {
observer_.reset();
AshTestBase::TearDown();
}
TestLauncherIconObserver* observer() { return observer_.get(); }
LauncherViewTestAPI* launcher_view_test() {
return launcher_view_test_.get();
}
Launcher* LauncherForSecondaryDisplay() {
return Launcher::ForWindow(Shell::GetAllRootWindows()[1]);
}
private:
scoped_ptr<TestLauncherIconObserver> observer_;
scoped_ptr<LauncherViewTestAPI> launcher_view_test_;
DISALLOW_COPY_AND_ASSIGN(LauncherViewIconObserverTest);
};
TEST_F(LauncherViewIconObserverTest, AddRemove) {
ash::test::TestLauncherDelegate* launcher_delegate =
ash::test::TestLauncherDelegate::instance();
ASSERT_TRUE(launcher_delegate);
views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(0, 0, 200, 200);
params.context = CurrentContext();
scoped_ptr<views::Widget> widget(new views::Widget());
widget->Init(params);
launcher_delegate->AddLauncherItem(widget->GetNativeWindow());
launcher_view_test()->RunMessageLoopUntilAnimationsDone();
EXPECT_TRUE(observer()->change_notified());
observer()->Reset();
widget->Show();
widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
launcher_view_test()->RunMessageLoopUntilAnimationsDone();
EXPECT_TRUE(observer()->change_notified());
observer()->Reset();
}
// Sometimes fails on trybots on win7_aura. http://crbug.com/177135
#if defined(OS_WIN)
#define MAYBE_AddRemoveWithMultipleDisplays \
DISABLED_AddRemoveWithMultipleDisplays
#else
#define MAYBE_AddRemoveWithMultipleDisplays \
AddRemoveWithMultipleDisplays
#endif
// Make sure creating/deleting an window on one displays notifies a
// launcher on external display as well as one on primary.
TEST_F(LauncherViewIconObserverTest, MAYBE_AddRemoveWithMultipleDisplays) {
UpdateDisplay("400x400,400x400");
TestLauncherIconObserver second_observer(LauncherForSecondaryDisplay());
ash::test::TestLauncherDelegate* launcher_delegate =
ash::test::TestLauncherDelegate::instance();
ASSERT_TRUE(launcher_delegate);
views::Widget::InitParams params(views::Widget::InitParams::TYPE_WINDOW);
params.ownership = views::Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
params.bounds = gfx::Rect(0, 0, 200, 200);
params.context = CurrentContext();
scoped_ptr<views::Widget> widget(new views::Widget());
widget->Init(params);
launcher_delegate->AddLauncherItem(widget->GetNativeWindow());
launcher_view_test()->RunMessageLoopUntilAnimationsDone();
EXPECT_TRUE(observer()->change_notified());
EXPECT_TRUE(second_observer.change_notified());
observer()->Reset();
second_observer.Reset();
widget->GetNativeWindow()->parent()->RemoveChild(widget->GetNativeWindow());
launcher_view_test()->RunMessageLoopUntilAnimationsDone();
EXPECT_TRUE(observer()->change_notified());
EXPECT_TRUE(second_observer.change_notified());
observer()->Reset();
second_observer.Reset();
}
TEST_F(LauncherViewIconObserverTest, BoundsChanged) {
ash::ShelfWidget* shelf = Shell::GetPrimaryRootWindowController()->shelf();
Launcher* launcher = Launcher::ForPrimaryDisplay();
gfx::Size shelf_size =
shelf->GetWindowBoundsInScreen().size();
shelf_size.set_width(shelf_size.width() / 2);
ASSERT_GT(shelf_size.width(), 0);
launcher->SetLauncherViewBounds(gfx::Rect(shelf_size));
// No animation happens for LauncherView bounds change.
EXPECT_TRUE(observer()->change_notified());
observer()->Reset();
}
////////////////////////////////////////////////////////////////////////////////
// LauncherView tests.
class LauncherViewTest : public AshTestBase {
public:
LauncherViewTest() : model_(NULL), launcher_view_(NULL) {}
virtual ~LauncherViewTest() {}
virtual void SetUp() OVERRIDE {
AshTestBase::SetUp();
test::ShellTestApi test_api(Shell::GetInstance());
model_ = test_api.launcher_model();
Launcher* launcher = Launcher::ForPrimaryDisplay();
launcher_view_ = launcher->GetLauncherViewForTest();
// The bounds should be big enough for 4 buttons + overflow chevron.
launcher_view_->SetBounds(0, 0, 500, 50);
test_api_.reset(new LauncherViewTestAPI(launcher_view_));
test_api_->SetAnimationDuration(1); // Speeds up animation for test.
}
virtual void TearDown() OVERRIDE {
test_api_.reset();
AshTestBase::TearDown();
}
protected:
LauncherID AddAppShortcut() {
LauncherItem item;
item.type = TYPE_APP_SHORTCUT;
item.status = STATUS_CLOSED;
LauncherID id = model_->next_id();
model_->Add(item);
test_api_->RunMessageLoopUntilAnimationsDone();
return id;
}
LauncherID AddTabbedBrowserNoWait() {
LauncherItem item;
item.type = TYPE_TABBED;
item.status = STATUS_RUNNING;
LauncherID id = model_->next_id();
model_->Add(item);
return id;
}
LauncherID AddTabbedBrowser() {
LauncherID id = AddTabbedBrowserNoWait();
test_api_->RunMessageLoopUntilAnimationsDone();
return id;
}
LauncherID AddPanel() {
LauncherID id = AddPanelNoWait();
test_api_->RunMessageLoopUntilAnimationsDone();
return id;
}
LauncherID AddPlatformAppNoWait() {
LauncherItem item;
item.type = TYPE_PLATFORM_APP;
item.status = STATUS_RUNNING;
LauncherID id = model_->next_id();
model_->Add(item);
return id;
}
LauncherID AddPanelNoWait() {
LauncherItem item;
item.type = TYPE_APP_PANEL;
item.status = STATUS_RUNNING;
LauncherID id = model_->next_id();
model_->Add(item);
return id;
}
LauncherID AddPlatformApp() {
LauncherID id = AddPlatformAppNoWait();
test_api_->RunMessageLoopUntilAnimationsDone();
return id;
}
void RemoveByID(LauncherID id) {
model_->RemoveItemAt(model_->ItemIndexByID(id));
test_api_->RunMessageLoopUntilAnimationsDone();
}
internal::LauncherButton* GetButtonByID(LauncherID id) {
int index = model_->ItemIndexByID(id);
return test_api_->GetButton(index);
}
LauncherItem GetItemByID(LauncherID id) {
LauncherItems::const_iterator items = model_->ItemByID(id);
return *items;
}
void CheckModelIDs(
const std::vector<std::pair<LauncherID, views::View*> >& id_map) {
size_t map_index = 0;
for (size_t model_index = 0;
model_index < model_->items().size();
++model_index) {
ash::LauncherItem item = model_->items()[model_index];
ash::LauncherID id = item.id;
EXPECT_EQ(id_map[map_index].first, id);
EXPECT_EQ(id_map[map_index].second, GetButtonByID(id));
++map_index;
}
ASSERT_EQ(map_index, id_map.size());
}
void VerifyLauncherItemBoundsAreValid() {
for (int i=0;i <= test_api_->GetLastVisibleIndex(); ++i) {
if (test_api_->GetButton(i)) {
gfx::Rect launcher_view_bounds = launcher_view_->GetLocalBounds();
gfx::Rect item_bounds = test_api_->GetBoundsByIndex(i);
EXPECT_TRUE(item_bounds.x() >= 0);
EXPECT_TRUE(item_bounds.y() >= 0);
EXPECT_TRUE(item_bounds.right() <= launcher_view_bounds.width());
EXPECT_TRUE(item_bounds.bottom() <= launcher_view_bounds.height());
}
}
}
views::View* SimulateDrag(internal::LauncherButtonHost::Pointer pointer,
int button_index,
int destination_index) {
// Add kExpectedAppIndex to each button index to allow default icons.
internal::LauncherButtonHost* button_host = launcher_view_;
// Mouse down.
views::View* button =
test_api_->GetButton(kExpectedAppIndex + button_index);
ui::MouseEvent click_event(ui::ET_MOUSE_PRESSED,
button->bounds().origin(),
button->bounds().origin(), 0);
button_host->PointerPressedOnButton(button, pointer, click_event);
// Drag.
views::View* destination =
test_api_->GetButton(kExpectedAppIndex + destination_index);
ui::MouseEvent drag_event(ui::ET_MOUSE_DRAGGED,
destination->bounds().origin(),
destination->bounds().origin(), 0);
button_host->PointerDraggedOnButton(button, pointer, drag_event);
return button;
}
void SetupForDragTest(
std::vector<std::pair<LauncherID, views::View*> >* id_map) {
// Initialize |id_map| with the automatically-created launcher buttons.
for (size_t i = 0; i < model_->items().size(); ++i) {
internal::LauncherButton* button = test_api_->GetButton(i);
id_map->push_back(std::make_pair(model_->items()[i].id, button));
}
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
// Add 5 app launcher buttons for testing.
for (int i = 0; i < 5; ++i) {
LauncherID id = AddAppShortcut();
id_map->insert(id_map->begin() + (kExpectedAppIndex + i),
std::make_pair(id, GetButtonByID(id)));
}
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(*id_map));
}
views::View* GetTooltipAnchorView() {
return launcher_view_->tooltip_manager()->anchor_;
}
void ShowTooltip() {
launcher_view_->tooltip_manager()->ShowInternal();
}
LauncherModel* model_;
internal::LauncherView* launcher_view_;
scoped_ptr<LauncherViewTestAPI> test_api_;
private:
DISALLOW_COPY_AND_ASSIGN(LauncherViewTest);
};
// Checks that the icon positions do not shift with a state change.
TEST_F(LauncherViewTest, NoStateChangeIconMovement) {
LauncherID last_added = AddAppShortcut();
internal::LauncherButton* button = GetButtonByID(last_added);
EXPECT_EQ(button->state(), ash::internal::LauncherButton::STATE_NORMAL);
gfx::Rect old_bounds = button->GetIconBounds();
button->AddState(ash::internal::LauncherButton::STATE_HOVERED);
gfx::Rect hovered_bounds = button->GetIconBounds();
EXPECT_EQ(old_bounds.ToString(), hovered_bounds.ToString());
}
// Adds browser button until overflow and verifies that the last added browser
// button is hidden.
TEST_F(LauncherViewTest, AddBrowserUntilOverflow) {
// All buttons should be visible.
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
// Add tabbed browser until overflow.
int items_added = 0;
LauncherID last_added = AddTabbedBrowser();
while (!test_api_->IsOverflowButtonVisible()) {
// Added button is visible after animation while in this loop.
EXPECT_TRUE(GetButtonByID(last_added)->visible());
last_added = AddTabbedBrowser();
++items_added;
ASSERT_LT(items_added, 10000);
}
// The last added button should be invisible.
EXPECT_FALSE(GetButtonByID(last_added)->visible());
}
// Adds one browser button then adds app shortcut until overflow. Verifies that
// the browser button gets hidden on overflow and last added app shortcut is
// still visible.
TEST_F(LauncherViewTest, AddAppShortcutWithBrowserButtonUntilOverflow) {
// All buttons should be visible.
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
LauncherID browser_button_id = AddTabbedBrowser();
// Add app shortcut until overflow.
int items_added = 0;
LauncherID last_added = AddAppShortcut();
while (!test_api_->IsOverflowButtonVisible()) {
// Added button is visible after animation while in this loop.
EXPECT_TRUE(GetButtonByID(last_added)->visible());
last_added = AddAppShortcut();
++items_added;
ASSERT_LT(items_added, 10000);
}
// The last added app short button should be visible.
EXPECT_TRUE(GetButtonByID(last_added)->visible());
// And the browser button is invisible.
EXPECT_FALSE(GetButtonByID(browser_button_id)->visible());
}
TEST_F(LauncherViewTest, AddPanelHidesTabbedBrowser) {
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
// Add tabbed browser until overflow, remember last visible tabbed browser.
int items_added = 0;
LauncherID first_added = AddTabbedBrowser();
EXPECT_TRUE(GetButtonByID(first_added)->visible());
LauncherID last_visible = first_added;
while (true) {
LauncherID added = AddTabbedBrowser();
if (test_api_->IsOverflowButtonVisible()) {
EXPECT_FALSE(GetButtonByID(added)->visible());
break;
}
last_visible = added;
++items_added;
ASSERT_LT(items_added, 10000);
}
LauncherID panel = AddPanel();
EXPECT_TRUE(GetButtonByID(panel)->visible());
EXPECT_FALSE(GetButtonByID(last_visible)->visible());
RemoveByID(panel);
EXPECT_TRUE(GetButtonByID(last_visible)->visible());
}
// When there are more panels then browsers we should hide panels rather
// than browsers.
TEST_F(LauncherViewTest, BrowserHidesExcessPanels) {
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
// Add tabbed browser.
LauncherID browser = AddTabbedBrowser();
LauncherID first_panel = AddPanel();
EXPECT_TRUE(GetButtonByID(browser)->visible());
EXPECT_TRUE(GetButtonByID(first_panel)->visible());
// Add panels until there is an overflow.
LauncherID last_panel = first_panel;
int items_added = 0;
while (!test_api_->IsOverflowButtonVisible()) {
last_panel = AddPanel();
++items_added;
ASSERT_LT(items_added, 10000);
}
// The first panel should now be hidden by the new browsers needing space.
EXPECT_FALSE(GetButtonByID(first_panel)->visible());
EXPECT_TRUE(GetButtonByID(last_panel)->visible());
EXPECT_TRUE(GetButtonByID(browser)->visible());
// Adding browsers should eventually begin to hide browsers. We will add
// browsers until either the last panel or browser is hidden.
items_added = 0;
while (GetButtonByID(browser)->visible() &&
GetButtonByID(last_panel)->visible()) {
browser = AddTabbedBrowser();
++items_added;
ASSERT_LT(items_added, 10000);
}
EXPECT_TRUE(GetButtonByID(last_panel)->visible());
EXPECT_FALSE(GetButtonByID(browser)->visible());
}
// Adds button until overflow then removes first added one. Verifies that
// the last added one changes from invisible to visible and overflow
// chevron is gone.
TEST_F(LauncherViewTest, RemoveButtonRevealsOverflowed) {
// All buttons should be visible.
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
// Add tabbed browser until overflow.
int items_added = 0;
LauncherID first_added = AddTabbedBrowser();
LauncherID last_added = first_added;
while (!test_api_->IsOverflowButtonVisible()) {
last_added = AddTabbedBrowser();
++items_added;
ASSERT_LT(items_added, 10000);
}
// Expect add more than 1 button. First added is visible and last is not.
EXPECT_NE(first_added, last_added);
EXPECT_TRUE(GetButtonByID(first_added)->visible());
EXPECT_FALSE(GetButtonByID(last_added)->visible());
// Remove first added.
RemoveByID(first_added);
// Last added button becomes visible and overflow chevron is gone.
EXPECT_TRUE(GetButtonByID(last_added)->visible());
EXPECT_EQ(1.0f, GetButtonByID(last_added)->layer()->opacity());
EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
}
// Verifies that remove last overflowed button should hide overflow chevron.
TEST_F(LauncherViewTest, RemoveLastOverflowed) {
// All buttons should be visible.
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
// Add tabbed browser until overflow.
int items_added = 0;
LauncherID last_added = AddTabbedBrowser();
while (!test_api_->IsOverflowButtonVisible()) {
last_added = AddTabbedBrowser();
++items_added;
ASSERT_LT(items_added, 10000);
}
RemoveByID(last_added);
EXPECT_FALSE(test_api_->IsOverflowButtonVisible());
}
// Adds browser button without waiting for animation to finish and verifies
// that all added buttons are visible.
TEST_F(LauncherViewTest, AddButtonQuickly) {
// All buttons should be visible.
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
// Add a few tabbed browser quickly without wait for animation.
int added_count = 0;
while (!test_api_->IsOverflowButtonVisible()) {
AddTabbedBrowserNoWait();
++added_count;
ASSERT_LT(added_count, 10000);
}
// LauncherView should be big enough to hold at least 3 new buttons.
ASSERT_GE(added_count, 3);
// Wait for the last animation to finish.
test_api_->RunMessageLoopUntilAnimationsDone();
// Verifies non-overflow buttons are visible.
for (int i = 0; i <= test_api_->GetLastVisibleIndex(); ++i) {
internal::LauncherButton* button = test_api_->GetButton(i);
if (button) {
EXPECT_TRUE(button->visible()) << "button index=" << i;
EXPECT_EQ(1.0f, button->layer()->opacity()) << "button index=" << i;
}
}
}
// Check that model changes are handled correctly while a launcher icon is being
// dragged.
TEST_F(LauncherViewTest, ModelChangesWhileDragging) {
internal::LauncherButtonHost* button_host = launcher_view_;
std::vector<std::pair<LauncherID, views::View*> > id_map;
SetupForDragTest(&id_map);
// Dragging changes model order.
views::View* dragged_button = SimulateDrag(
internal::LauncherButtonHost::MOUSE, 0, 2);
std::rotate(id_map.begin() + kExpectedAppIndex,
id_map.begin() + kExpectedAppIndex + 1,
id_map.begin() + kExpectedAppIndex + 3);
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
// Cancelling the drag operation restores previous order.
button_host->PointerReleasedOnButton(dragged_button,
internal::LauncherButtonHost::MOUSE,
true);
std::rotate(id_map.begin() + kExpectedAppIndex,
id_map.begin() + kExpectedAppIndex + 2,
id_map.begin() + kExpectedAppIndex + 3);
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
// Deleting an item keeps the remaining intact.
dragged_button = SimulateDrag(internal::LauncherButtonHost::MOUSE, 0, 2);
model_->RemoveItemAt(kExpectedAppIndex + 1);
id_map.erase(id_map.begin() + kExpectedAppIndex + 1);
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
button_host->PointerReleasedOnButton(dragged_button,
internal::LauncherButtonHost::MOUSE,
false);
// Adding a launcher item cancels the drag and respects the order.
dragged_button = SimulateDrag(internal::LauncherButtonHost::MOUSE, 0, 2);
LauncherID new_id = AddAppShortcut();
id_map.insert(id_map.begin() + kExpectedAppIndex + 4,
std::make_pair(new_id, GetButtonByID(new_id)));
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
button_host->PointerReleasedOnButton(dragged_button,
internal::LauncherButtonHost::MOUSE,
false);
// Adding a launcher item at the end (i.e. a panel) canels drag and respects
// the order.
dragged_button = SimulateDrag(internal::LauncherButtonHost::MOUSE, 0, 2);
new_id = AddPanel();
id_map.insert(id_map.begin() + kExpectedAppIndex + 6,
std::make_pair(new_id, GetButtonByID(new_id)));
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
button_host->PointerReleasedOnButton(dragged_button,
internal::LauncherButtonHost::MOUSE,
false);
}
// Check that 2nd drag from the other pointer would be ignored.
TEST_F(LauncherViewTest, SimultaneousDrag) {
internal::LauncherButtonHost* button_host = launcher_view_;
std::vector<std::pair<LauncherID, views::View*> > id_map;
SetupForDragTest(&id_map);
// Start a mouse drag.
views::View* dragged_button_mouse = SimulateDrag(
internal::LauncherButtonHost::MOUSE, 0, 2);
std::rotate(id_map.begin() + kExpectedAppIndex,
id_map.begin() + kExpectedAppIndex + 1,
id_map.begin() + kExpectedAppIndex + 3);
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
// Attempt a touch drag before the mouse drag finishes.
views::View* dragged_button_touch = SimulateDrag(
internal::LauncherButtonHost::TOUCH, 3, 1);
// Nothing changes since 2nd drag is ignored.
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
// Finish the mouse drag.
button_host->PointerReleasedOnButton(dragged_button_mouse,
internal::LauncherButtonHost::MOUSE,
false);
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
// Now start a touch drag.
dragged_button_touch = SimulateDrag(
internal::LauncherButtonHost::TOUCH, 3, 1);
std::rotate(id_map.begin() + kExpectedAppIndex + 2,
id_map.begin() + kExpectedAppIndex + 3,
id_map.begin() + kExpectedAppIndex + 4);
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
// And attempt a mouse drag before the touch drag finishes.
dragged_button_mouse = SimulateDrag(
internal::LauncherButtonHost::MOUSE, 0, 1);
// Nothing changes since 2nd drag is ignored.
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
button_host->PointerReleasedOnButton(dragged_button_touch,
internal::LauncherButtonHost::TOUCH,
false);
ASSERT_NO_FATAL_FAILURE(CheckModelIDs(id_map));
}
// Confirm that item status changes are reflected in the buttons.
TEST_F(LauncherViewTest, LauncherItemStatus) {
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
// Add tabbed browser.
LauncherID last_added = AddTabbedBrowser();
LauncherItem item = GetItemByID(last_added);
int index = model_->ItemIndexByID(last_added);
internal::LauncherButton* button = GetButtonByID(last_added);
ASSERT_EQ(internal::LauncherButton::STATE_RUNNING, button->state());
item.status = ash::STATUS_ACTIVE;
model_->Set(index, item);
ASSERT_EQ(internal::LauncherButton::STATE_ACTIVE, button->state());
item.status = ash::STATUS_ATTENTION;
model_->Set(index, item);
ASSERT_EQ(internal::LauncherButton::STATE_ATTENTION, button->state());
}
// Confirm that item status changes are reflected in the buttons
// for platform apps.
TEST_F(LauncherViewTest, LauncherItemStatusPlatformApp) {
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
// Add tabbed browser.
LauncherID last_added = AddPlatformApp();
LauncherItem item = GetItemByID(last_added);
int index = model_->ItemIndexByID(last_added);
internal::LauncherButton* button = GetButtonByID(last_added);
ASSERT_EQ(internal::LauncherButton::STATE_RUNNING, button->state());
item.status = ash::STATUS_ACTIVE;
model_->Set(index, item);
ASSERT_EQ(internal::LauncherButton::STATE_ACTIVE, button->state());
item.status = ash::STATUS_ATTENTION;
model_->Set(index, item);
ASSERT_EQ(internal::LauncherButton::STATE_ATTENTION, button->state());
}
// Confirm that launcher item bounds are correctly updated on shelf changes.
TEST_F(LauncherViewTest, LauncherItemBoundsCheck) {
internal::ShelfLayoutManager* shelf_layout_manager =
Shell::GetPrimaryRootWindowController()->shelf()->shelf_layout_manager();
VerifyLauncherItemBoundsAreValid();
shelf_layout_manager->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_ALWAYS);
test_api_->RunMessageLoopUntilAnimationsDone();
VerifyLauncherItemBoundsAreValid();
shelf_layout_manager->SetAutoHideBehavior(SHELF_AUTO_HIDE_BEHAVIOR_NEVER);
test_api_->RunMessageLoopUntilAnimationsDone();
VerifyLauncherItemBoundsAreValid();
}
TEST_F(LauncherViewTest, LauncherTooltipTest) {
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
// Prepare some items to the launcher.
LauncherID app_button_id = AddAppShortcut();
LauncherID tab_button_id = AddTabbedBrowser();
internal::LauncherButton* app_button = GetButtonByID(app_button_id);
internal::LauncherButton* tab_button = GetButtonByID(tab_button_id);
internal::LauncherButtonHost* button_host = launcher_view_;
internal::LauncherTooltipManager* tooltip_manager =
launcher_view_->tooltip_manager();
button_host->MouseEnteredButton(app_button);
// There's a delay to show the tooltip, so it's not visible yet.
EXPECT_FALSE(tooltip_manager->IsVisible());
EXPECT_EQ(app_button, GetTooltipAnchorView());
ShowTooltip();
EXPECT_TRUE(tooltip_manager->IsVisible());
// Once it's visible, it keeps visibility and is pointing to the same
// item.
button_host->MouseExitedButton(app_button);
EXPECT_TRUE(tooltip_manager->IsVisible());
EXPECT_EQ(app_button, GetTooltipAnchorView());
// When entered to another item, it switches to the new item. There is no
// delay for the visibility.
button_host->MouseEnteredButton(tab_button);
EXPECT_TRUE(tooltip_manager->IsVisible());
EXPECT_EQ(tab_button, GetTooltipAnchorView());
button_host->MouseExitedButton(tab_button);
tooltip_manager->Close();
// Next time: enter app_button -> move immediately to tab_button.
button_host->MouseEnteredButton(app_button);
button_host->MouseExitedButton(app_button);
button_host->MouseEnteredButton(tab_button);
EXPECT_FALSE(tooltip_manager->IsVisible());
EXPECT_EQ(tab_button, GetTooltipAnchorView());
}
TEST_F(LauncherViewTest, ShouldHideTooltipTest) {
LauncherID app_button_id = AddAppShortcut();
LauncherID tab_button_id = AddTabbedBrowser();
// The tooltip shouldn't hide if the mouse is on normal buttons.
for (int i = 0; i < test_api_->GetButtonCount(); i++) {
internal::LauncherButton* button = test_api_->GetButton(i);
if (!button)
continue;
EXPECT_FALSE(launcher_view_->ShouldHideTooltip(
button->GetMirroredBounds().CenterPoint()))
<< "LauncherView tries to hide on button " << i;
}
// The tooltip should not hide on the app-list button.
views::View* app_list_button = launcher_view_->GetAppListButtonView();
EXPECT_FALSE(launcher_view_->ShouldHideTooltip(
app_list_button->GetMirroredBounds().CenterPoint()));
// The tooltip shouldn't hide if the mouse is in the gap between two buttons.
gfx::Rect app_button_rect = GetButtonByID(app_button_id)->GetMirroredBounds();
gfx::Rect tab_button_rect = GetButtonByID(tab_button_id)->GetMirroredBounds();
ASSERT_FALSE(app_button_rect.Intersects(tab_button_rect));
EXPECT_FALSE(launcher_view_->ShouldHideTooltip(
gfx::UnionRects(app_button_rect, tab_button_rect).CenterPoint()));
// The tooltip should hide if it's outside of all buttons.
gfx::Rect all_area;
for (int i = 0; i < test_api_->GetButtonCount(); i++) {
internal::LauncherButton* button = test_api_->GetButton(i);
if (!button)
continue;
all_area.Union(button->GetMirroredBounds());
}
all_area.Union(launcher_view_->GetAppListButtonView()->GetMirroredBounds());
EXPECT_FALSE(launcher_view_->ShouldHideTooltip(all_area.origin()));
EXPECT_FALSE(launcher_view_->ShouldHideTooltip(
gfx::Point(all_area.right() - 1, all_area.bottom() - 1)));
EXPECT_TRUE(launcher_view_->ShouldHideTooltip(
gfx::Point(all_area.right(), all_area.y())));
EXPECT_TRUE(launcher_view_->ShouldHideTooltip(
gfx::Point(all_area.x() - 1, all_area.y())));
EXPECT_TRUE(launcher_view_->ShouldHideTooltip(
gfx::Point(all_area.x(), all_area.y() - 1)));
EXPECT_TRUE(launcher_view_->ShouldHideTooltip(
gfx::Point(all_area.x(), all_area.bottom())));
}
TEST_F(LauncherViewTest, ShouldHideTooltipWithAppListWindowTest) {
Shell::GetInstance()->ToggleAppList(NULL);
ASSERT_TRUE(Shell::GetInstance()->GetAppListWindow());
// The tooltip shouldn't hide if the mouse is on normal buttons.
for (int i = 1; i < test_api_->GetButtonCount(); i++) {
internal::LauncherButton* button = test_api_->GetButton(i);
if (!button)
continue;
EXPECT_FALSE(launcher_view_->ShouldHideTooltip(
button->GetMirroredBounds().CenterPoint()))
<< "LauncherView tries to hide on button " << i;
}
// The tooltip should hide on the app-list button.
views::View* app_list_button = launcher_view_->GetAppListButtonView();
EXPECT_TRUE(launcher_view_->ShouldHideTooltip(
app_list_button->GetMirroredBounds().CenterPoint()));
}
// Resizing launcher view while an add animation without fade-in is running,
// which happens when overflow happens. App list button should end up in its
// new ideal bounds.
TEST_F(LauncherViewTest, ResizeDuringOverflowAddAnimation) {
// All buttons should be visible.
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
// Add buttons until overflow. Let the non-overflow add animations finish but
// leave the last running.
int items_added = 0;
AddTabbedBrowserNoWait();
while (!test_api_->IsOverflowButtonVisible()) {
test_api_->RunMessageLoopUntilAnimationsDone();
AddTabbedBrowserNoWait();
++items_added;
ASSERT_LT(items_added, 10000);
}
// Resize launcher view with that animation running and stay overflown.
gfx::Rect bounds = launcher_view_->bounds();
bounds.set_width(bounds.width() - kLauncherPreferredSize);
launcher_view_->SetBoundsRect(bounds);
ASSERT_TRUE(test_api_->IsOverflowButtonVisible());
// Finish the animation.
test_api_->RunMessageLoopUntilAnimationsDone();
// App list button should ends up in its new ideal bounds.
const int app_list_button_index = test_api_->GetButtonCount() - 1;
const gfx::Rect& app_list_ideal_bounds =
test_api_->GetIdealBoundsByIndex(app_list_button_index);
const gfx::Rect& app_list_bounds =
test_api_->GetBoundsByIndex(app_list_button_index);
EXPECT_EQ(app_list_bounds, app_list_ideal_bounds);
}
// Check that the first item in the list follows Fitt's law by including the
// first pixel and being therefore bigger then the others.
TEST_F(LauncherViewTest, CheckFittsLaw) {
// All buttons should be visible.
ASSERT_EQ(test_api_->GetLastVisibleIndex() + 1,
test_api_->GetButtonCount());
gfx::Rect ideal_bounds_0 = test_api_->GetIdealBoundsByIndex(0);
gfx::Rect ideal_bounds_1 = test_api_->GetIdealBoundsByIndex(1);
EXPECT_GT(ideal_bounds_0.width(), ideal_bounds_1.width());
}
} // namespace test
} // namespace ash
|