1 // Copyright 2013 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_navigator.h"
7 #include "ash/ash_switches.h"
8 #include "ash/shelf/shelf.h"
9 #include "ash/shelf/shelf_item_types.h"
10 #include "ash/shelf/shelf_model.h"
11 #include "base/command_line.h"
12 #include "base/macros.h"
13 #include "testing/gtest/include/gtest/gtest.h"
19 class ShelfNavigatorTest : public testing::Test {
21 ShelfNavigatorTest() {}
24 virtual void SetUp() OVERRIDE {
25 model_.reset(new ShelfModel);
27 // Add APP_LIST for test.
28 LauncherItem app_list;
29 app_list.type = TYPE_APP_LIST;
30 model_->Add(app_list);
32 // Initially, applist launcher item is only created.
33 int total_num = model_->item_count();
34 EXPECT_EQ(1, total_num);
35 EXPECT_TRUE(model_->items()[0].type == TYPE_APP_LIST);
37 // Add BROWSER_SHORTCUT for test.
38 LauncherItem browser_shortcut;
39 browser_shortcut.type = TYPE_BROWSER_SHORTCUT;
40 model_->Add(browser_shortcut);
43 void SetupMockShelfModel(ShelfItemType* types,
46 for (int i = 0; i < types_length; ++i) {
47 LauncherItem new_item;
48 new_item.type = types[i];
50 (types[i] == TYPE_PLATFORM_APP) ? STATUS_RUNNING : STATUS_CLOSED;
51 model_->Add(new_item);
54 // Set the focused item.
55 if (focused_index >= 0) {
56 LauncherItem focused_item =model_->items()[focused_index];
57 if (focused_item.type == TYPE_PLATFORM_APP) {
58 focused_item.status = STATUS_ACTIVE;
59 model_->Set(focused_index, focused_item);
64 const ShelfModel& model() { return *model_.get(); }
67 scoped_ptr<ShelfModel> model_;
69 DISALLOW_COPY_AND_ASSIGN(ShelfNavigatorTest);
72 class ShelfNavigatorLegacyShelfLayoutTest : public ShelfNavigatorTest {
74 ShelfNavigatorLegacyShelfLayoutTest() : ShelfNavigatorTest() {}
77 virtual void SetUp() OVERRIDE {
78 CommandLine::ForCurrentProcess()->AppendSwitch(
79 switches::kAshDisableAlternateShelfLayout);
80 ShelfNavigatorTest::SetUp();
84 DISALLOW_COPY_AND_ASSIGN(ShelfNavigatorLegacyShelfLayoutTest);
89 TEST_F(ShelfNavigatorTest, BasicCycle) {
90 // An app shortcut and three platform apps.
91 ShelfItemType types[] = {
92 TYPE_APP_SHORTCUT, TYPE_PLATFORM_APP, TYPE_PLATFORM_APP, TYPE_PLATFORM_APP,
94 // ShelfModel automatically adds BROWSER_SHORTCUT item at the
95 // beginning, so '3' refers the first TYPE_PLATFORM_APP item.
96 SetupMockShelfModel(types, arraysize(types), 3);
98 EXPECT_EQ(4, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
100 // Fourth one. It will skip the APP_SHORTCUT at the beginning of the list and
101 // the APP_LIST item which is automatically added at the end of items.
102 EXPECT_EQ(5, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD));
105 TEST_F(ShelfNavigatorLegacyShelfLayoutTest, BasicCycle) {
106 // An app shortcut and three platform apps.
107 ShelfItemType types[] = {
108 TYPE_APP_SHORTCUT, TYPE_PLATFORM_APP, TYPE_PLATFORM_APP, TYPE_PLATFORM_APP,
110 // ShelfModel automatically adds BROWSER_SHORTCUT item at the
111 // beginning, so '2' refers the first TYPE_PLATFORM_APP item.
112 SetupMockShelfModel(types, arraysize(types), 2);
114 EXPECT_EQ(3, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
116 // Fourth one. It will skip the APP_SHORTCUT at the beginning of the list and
117 // the APP_LIST item which is automatically added at the end of items.
118 EXPECT_EQ(4, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD));
121 TEST_F(ShelfNavigatorTest, WrapToBeginning) {
122 ShelfItemType types[] = {
123 TYPE_APP_SHORTCUT, TYPE_PLATFORM_APP, TYPE_PLATFORM_APP, TYPE_PLATFORM_APP,
125 SetupMockShelfModel(types, arraysize(types), 5);
127 // Second one. It skips the APP_LIST item at the end of the list,
128 // wraps to the beginning, and skips BROWSER_SHORTCUT and APP_SHORTCUT
129 // at the beginning of the list.
130 EXPECT_EQ(3, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
133 TEST_F(ShelfNavigatorLegacyShelfLayoutTest, WrapToBeginning) {
134 ShelfItemType types[] = {
135 TYPE_APP_SHORTCUT, TYPE_PLATFORM_APP, TYPE_PLATFORM_APP, TYPE_PLATFORM_APP,
137 SetupMockShelfModel(types, arraysize(types), 4);
139 // Second one. It skips the APP_LIST item at the end of the list,
140 // wraps to the beginning, and skips BROWSER_SHORTCUT and APP_SHORTCUT
141 // at the beginning of the list.
142 EXPECT_EQ(2, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
145 TEST_F(ShelfNavigatorTest, Empty) {
146 SetupMockShelfModel(NULL, 0, -1);
147 EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
148 EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD));
151 TEST_F(ShelfNavigatorTest, SingleEntry) {
152 ShelfItemType type = TYPE_PLATFORM_APP;
153 SetupMockShelfModel(&type, 1, 2);
155 // If there's only one item there and it is already active, there's no item
156 // to be activated next.
157 EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
158 EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD));
161 TEST_F(ShelfNavigatorLegacyShelfLayoutTest, SingleEntry) {
162 ShelfItemType type = TYPE_PLATFORM_APP;
163 SetupMockShelfModel(&type, 1, 1);
165 // If there's only one item there and it is already active, there's no item
166 // to be activated next.
167 EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
168 EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD));
171 TEST_F(ShelfNavigatorTest, NoActive) {
172 ShelfItemType types[] = {
173 TYPE_PLATFORM_APP, TYPE_PLATFORM_APP,
175 // Special case: no items are 'STATUS_ACTIVE'.
176 SetupMockShelfModel(types, arraysize(types), -1);
178 // If there are no active status, pick the first running item as a fallback.
179 EXPECT_EQ(2, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
180 EXPECT_EQ(2, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD));
183 TEST_F(ShelfNavigatorLegacyShelfLayoutTest, NoActive) {
184 ShelfItemType types[] = {
185 TYPE_PLATFORM_APP, TYPE_PLATFORM_APP,
187 // Special case: no items are 'STATUS_ACTIVE'.
188 SetupMockShelfModel(types, arraysize(types), -1);
190 // If there are no active status, pick the first running item as a fallback.
191 EXPECT_EQ(1, GetNextActivatedItemIndex(model(), CYCLE_FORWARD));
192 EXPECT_EQ(1, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD));