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.
4
5 #include "ash/launcher/launcher.h"
6 #include "ash/shelf/shelf_button.h"
7 #include "ash/shelf/shelf_item_delegate_manager.h"
8 #include "ash/shelf/shelf_model.h"
9 #include "ash/shelf/shelf_view.h"
10 #include "ash/shelf/shelf_widget.h"
11 #include "ash/shell.h"
12 #include "ash/test/ash_test_base.h"
13 #include "ash/test/launcher_test_api.h"
14 #include "ash/test/shelf_view_test_api.h"
15 #include "ash/test/test_shelf_item_delegate.h"
16 #include "ash/wm/window_util.h"
17 #include "ui/aura/root_window.h"
18 #include "ui/gfx/display.h"
19 #include "ui/gfx/screen.h"
20 #include "ui/views/corewm/corewm_switches.h"
21 #include "ui/views/view.h"
22 #include "ui/views/widget/widget.h"
23
24 #if defined(OS_WIN)
25 #include "base/win/windows_version.h"
26 #endif
27
28 typedef ash::test::AshTestBase LauncherTest;
29 using ash::internal::ShelfView;
30 using ash::internal::ShelfButton;
31
32 namespace ash {
33
34 class LauncherTest : public ash::test::AshTestBase {
35 public:
LauncherTest()36 LauncherTest() : launcher_(NULL),
37 shelf_view_(NULL),
38 shelf_model_(NULL),
39 item_delegate_manager_(NULL) {
40 }
41
~LauncherTest()42 virtual ~LauncherTest() {}
43
SetUp()44 virtual void SetUp() {
45 test::AshTestBase::SetUp();
46
47 launcher_ = Launcher::ForPrimaryDisplay();
48 ASSERT_TRUE(launcher_);
49
50 ash::test::LauncherTestAPI test(launcher_);
51 shelf_view_ = test.shelf_view();
52 shelf_model_ = shelf_view_->model();
53 item_delegate_manager_ =
54 Shell::GetInstance()->shelf_item_delegate_manager();
55
56 test_.reset(new ash::test::ShelfViewTestAPI(shelf_view_));
57 }
58
TearDown()59 virtual void TearDown() OVERRIDE {
60 test::AshTestBase::TearDown();
61 }
62
launcher()63 Launcher* launcher() {
64 return launcher_;
65 }
66
shelf_view()67 ShelfView* shelf_view() {
68 return shelf_view_;
69 }
70
shelf_model()71 ShelfModel* shelf_model() {
72 return shelf_model_;
73 }
74
item_manager()75 ShelfItemDelegateManager* item_manager() {
76 return item_delegate_manager_;
77 }
78
test_api()79 ash::test::ShelfViewTestAPI* test_api() {
80 return test_.get();
81 }
82
83 private:
84 Launcher* launcher_;
85 ShelfView* shelf_view_;
86 ShelfModel* shelf_model_;
87 ShelfItemDelegateManager* item_delegate_manager_;
88 scoped_ptr<test::ShelfViewTestAPI> test_;
89
90 DISALLOW_COPY_AND_ASSIGN(LauncherTest);
91 };
92
93 // Confirms that LauncherItem reflects the appropriated state.
TEST_F(LauncherTest,StatusReflection)94 TEST_F(LauncherTest, StatusReflection) {
95 // Initially we have the app list.
96 int button_count = test_api()->GetButtonCount();
97
98 // Add running platform app.
99 LauncherItem item;
100 item.type = TYPE_PLATFORM_APP;
101 item.status = STATUS_RUNNING;
102 int index = shelf_model()->Add(item);
103 ASSERT_EQ(++button_count, test_api()->GetButtonCount());
104 ShelfButton* button = test_api()->GetButton(index);
105 EXPECT_EQ(ShelfButton::STATE_RUNNING, button->state());
106
107 // Remove it.
108 shelf_model()->RemoveItemAt(index);
109 ASSERT_EQ(--button_count, test_api()->GetButtonCount());
110 }
111
112 // Confirm that using the menu will clear the hover attribute. To avoid another
113 // browser test we check this here.
TEST_F(LauncherTest,checkHoverAfterMenu)114 TEST_F(LauncherTest, checkHoverAfterMenu) {
115 // Initially we have the app list.
116 int button_count = test_api()->GetButtonCount();
117
118 // Add running platform app.
119 LauncherItem item;
120 item.type = TYPE_PLATFORM_APP;
121 item.status = STATUS_RUNNING;
122 int index = shelf_model()->Add(item);
123
124 scoped_ptr<ShelfItemDelegate> delegate(
125 new test::TestShelfItemDelegate(NULL));
126 item_manager()->SetShelfItemDelegate(shelf_model()->items()[index].id,
127 delegate.Pass());
128
129 ASSERT_EQ(++button_count, test_api()->GetButtonCount());
130 ShelfButton* button = test_api()->GetButton(index);
131 button->AddState(ShelfButton::STATE_HOVERED);
132 button->ShowContextMenu(gfx::Point(), ui::MENU_SOURCE_MOUSE);
133 EXPECT_FALSE(button->state() & ShelfButton::STATE_HOVERED);
134
135 // Remove it.
136 shelf_model()->RemoveItemAt(index);
137 }
138
TEST_F(LauncherTest,ShowOverflowBubble)139 TEST_F(LauncherTest, ShowOverflowBubble) {
140 LauncherID first_item_id = shelf_model()->next_id();
141
142 // Add platform app button until overflow.
143 int items_added = 0;
144 while (!test_api()->IsOverflowButtonVisible()) {
145 LauncherItem item;
146 item.type = TYPE_PLATFORM_APP;
147 item.status = STATUS_RUNNING;
148 shelf_model()->Add(item);
149
150 ++items_added;
151 ASSERT_LT(items_added, 10000);
152 }
153
154 // Shows overflow bubble.
155 test_api()->ShowOverflowBubble();
156 EXPECT_TRUE(launcher()->IsShowingOverflowBubble());
157
158 // Removes the first item in main shelf view.
159 shelf_model()->RemoveItemAt(shelf_model()->ItemIndexByID(first_item_id));
160
161 // Waits for all transitions to finish and there should be no crash.
162 test_api()->RunMessageLoopUntilAnimationsDone();
163 EXPECT_FALSE(launcher()->IsShowingOverflowBubble());
164 }
165
166 } // namespace ash
167