OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "ash/common/shelf/shelf_navigator.h" | |
6 | |
7 #include "ash/common/shelf/shelf_item_types.h" | |
8 #include "ash/common/shelf/shelf_model.h" | |
9 #include "base/macros.h" | |
10 #include "testing/gtest/include/gtest/gtest.h" | |
11 | |
12 namespace ash { | |
13 | |
14 namespace { | |
15 | |
16 class ShelfNavigatorTest : public testing::Test { | |
17 public: | |
18 ShelfNavigatorTest() {} | |
19 | |
20 protected: | |
21 void SetUp() override { | |
22 model_.reset(new ShelfModel); | |
23 | |
24 // Add APP_LIST for test. | |
25 ShelfItem app_list; | |
26 app_list.type = TYPE_APP_LIST; | |
27 model_->Add(app_list); | |
28 | |
29 // Initially, applist launcher item is only created. | |
30 int total_num = model_->item_count(); | |
31 EXPECT_EQ(1, total_num); | |
32 EXPECT_TRUE(model_->items()[0].type == TYPE_APP_LIST); | |
33 | |
34 // Add BROWSER_SHORTCUT for test. | |
35 ShelfItem browser_shortcut; | |
36 browser_shortcut.type = TYPE_BROWSER_SHORTCUT; | |
37 model_->Add(browser_shortcut); | |
38 } | |
39 | |
40 void SetupMockShelfModel(ShelfItemType* types, | |
41 int types_length, | |
42 int focused_index) { | |
43 for (int i = 0; i < types_length; ++i) { | |
44 ShelfItem new_item; | |
45 new_item.type = types[i]; | |
46 new_item.status = | |
47 (types[i] == TYPE_PLATFORM_APP) ? STATUS_RUNNING : STATUS_CLOSED; | |
48 model_->Add(new_item); | |
49 } | |
50 | |
51 // Set the focused item. | |
52 if (focused_index >= 0) { | |
53 ShelfItem focused_item = model_->items()[focused_index]; | |
54 if (focused_item.type == TYPE_PLATFORM_APP) { | |
55 focused_item.status = STATUS_ACTIVE; | |
56 model_->Set(focused_index, focused_item); | |
57 } | |
58 } | |
59 } | |
60 | |
61 const ShelfModel& model() { return *model_.get(); } | |
62 | |
63 private: | |
64 std::unique_ptr<ShelfModel> model_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(ShelfNavigatorTest); | |
67 }; | |
68 | |
69 } // namespace | |
70 | |
71 TEST_F(ShelfNavigatorTest, BasicCycle) { | |
72 // An app shortcut and three platform apps. | |
73 ShelfItemType types[] = { | |
74 TYPE_APP_SHORTCUT, TYPE_PLATFORM_APP, TYPE_PLATFORM_APP, | |
75 TYPE_PLATFORM_APP, | |
76 }; | |
77 // ShelfModel automatically adds BROWSER_SHORTCUT item at the | |
78 // beginning, so '3' refers the first TYPE_PLATFORM_APP item. | |
79 SetupMockShelfModel(types, arraysize(types), 3); | |
80 | |
81 EXPECT_EQ(4, GetNextActivatedItemIndex(model(), CYCLE_FORWARD)); | |
82 | |
83 // Fourth one. It will skip the APP_SHORTCUT at the beginning of the list and | |
84 // the APP_LIST item which is automatically added at the end of items. | |
85 EXPECT_EQ(5, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD)); | |
86 } | |
87 | |
88 TEST_F(ShelfNavigatorTest, WrapToBeginning) { | |
89 ShelfItemType types[] = { | |
90 TYPE_APP_SHORTCUT, TYPE_PLATFORM_APP, TYPE_PLATFORM_APP, | |
91 TYPE_PLATFORM_APP, | |
92 }; | |
93 SetupMockShelfModel(types, arraysize(types), 5); | |
94 | |
95 // Second one. It skips the APP_LIST item at the end of the list, | |
96 // wraps to the beginning, and skips BROWSER_SHORTCUT and APP_SHORTCUT | |
97 // at the beginning of the list. | |
98 EXPECT_EQ(3, GetNextActivatedItemIndex(model(), CYCLE_FORWARD)); | |
99 } | |
100 | |
101 TEST_F(ShelfNavigatorTest, Empty) { | |
102 SetupMockShelfModel(NULL, 0, -1); | |
103 EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_FORWARD)); | |
104 EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD)); | |
105 } | |
106 | |
107 TEST_F(ShelfNavigatorTest, SingleEntry) { | |
108 ShelfItemType type = TYPE_PLATFORM_APP; | |
109 SetupMockShelfModel(&type, 1, 2); | |
110 | |
111 // If there's only one item there and it is already active, there's no item | |
112 // to be activated next. | |
113 EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_FORWARD)); | |
114 EXPECT_EQ(-1, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD)); | |
115 } | |
116 | |
117 TEST_F(ShelfNavigatorTest, NoActive) { | |
118 ShelfItemType types[] = { | |
119 TYPE_PLATFORM_APP, TYPE_PLATFORM_APP, | |
120 }; | |
121 // Special case: no items are 'STATUS_ACTIVE'. | |
122 SetupMockShelfModel(types, arraysize(types), -1); | |
123 | |
124 // If there are no active status, pick the first running item as a fallback. | |
125 EXPECT_EQ(2, GetNextActivatedItemIndex(model(), CYCLE_FORWARD)); | |
126 EXPECT_EQ(2, GetNextActivatedItemIndex(model(), CYCLE_BACKWARD)); | |
127 } | |
128 | |
129 } // namespace ash | |
OLD | NEW |