blob: 9c123e8637668496f339532b4b9b8cf08a60d1ab [file] [log] [blame]
Tianjie Xu5fe5eb62018-03-20 16:07:39 -07001/*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "screen_ui.h"
18
19#include <string>
20
21#include <gtest/gtest.h>
22
23constexpr const char* HEADER[] = { "header", nullptr };
24constexpr const char* ITEMS[] = { "items1", "items2", "items3", "items4", "1234567890", nullptr };
25
26TEST(ScreenUITest, StartPhoneMenuSmoke) {
Tao Baoe02a5b22018-05-02 15:46:11 -070027 Menu menu(false, 10, 20, HEADER, ITEMS, 0);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070028 ASSERT_FALSE(menu.scrollable());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070029 ASSERT_EQ(HEADER[0], menu.text_headers()[0]);
30 ASSERT_EQ(5u, menu.ItemsCount());
31
32 std::string message;
33 ASSERT_FALSE(menu.ItemsOverflow(&message));
34 for (size_t i = 0; i < menu.ItemsCount(); i++) {
35 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
36 }
37
38 ASSERT_EQ(0, menu.selection());
39}
40
41TEST(ScreenUITest, StartWearMenuSmoke) {
Tao Baoe02a5b22018-05-02 15:46:11 -070042 Menu menu(true, 10, 8, HEADER, ITEMS, 1);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070043 ASSERT_TRUE(menu.scrollable());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070044 ASSERT_EQ(HEADER[0], menu.text_headers()[0]);
45 ASSERT_EQ(5u, menu.ItemsCount());
46
47 std::string message;
48 ASSERT_FALSE(menu.ItemsOverflow(&message));
49 for (size_t i = 0; i < menu.ItemsCount() - 1; i++) {
50 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
51 }
52 // Test of the last item is truncated
53 ASSERT_EQ("12345678", menu.TextItem(4));
54 ASSERT_EQ(1, menu.selection());
55}
56
57TEST(ScreenUITest, StartPhoneMenuItemsOverflow) {
Tao Baoe02a5b22018-05-02 15:46:11 -070058 Menu menu(false, 1, 20, HEADER, ITEMS, 0);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070059 ASSERT_FALSE(menu.scrollable());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070060 ASSERT_EQ(1u, menu.ItemsCount());
61
62 std::string message;
63 ASSERT_FALSE(menu.ItemsOverflow(&message));
64 for (size_t i = 0; i < menu.ItemsCount(); i++) {
65 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
66 }
67
68 ASSERT_EQ(0u, menu.MenuStart());
69 ASSERT_EQ(1u, menu.MenuEnd());
70}
71
72TEST(ScreenUITest, StartWearMenuItemsOverflow) {
Tao Baoe02a5b22018-05-02 15:46:11 -070073 Menu menu(true, 1, 20, HEADER, ITEMS, 0);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070074 ASSERT_TRUE(menu.scrollable());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070075 ASSERT_EQ(5u, menu.ItemsCount());
76
77 std::string message;
78 ASSERT_TRUE(menu.ItemsOverflow(&message));
79 ASSERT_EQ("Current item: 1/5", message);
80
81 for (size_t i = 0; i < menu.ItemsCount(); i++) {
82 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
83 }
84
85 ASSERT_EQ(0u, menu.MenuStart());
86 ASSERT_EQ(1u, menu.MenuEnd());
87}
88
89TEST(ScreenUITest, PhoneMenuSelectSmoke) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070090 int sel = 0;
Tao Baoe02a5b22018-05-02 15:46:11 -070091 Menu menu(false, 10, 20, HEADER, ITEMS, sel);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070092 // Mimic down button 10 times (2 * items size)
93 for (int i = 0; i < 10; i++) {
94 sel = menu.Select(++sel);
95 ASSERT_EQ(sel, menu.selection());
96
97 // Wraps the selection for unscrollable menu when it reaches the boundary.
98 int expected = (i + 1) % 5;
99 ASSERT_EQ(expected, menu.selection());
100
101 ASSERT_EQ(0u, menu.MenuStart());
102 ASSERT_EQ(5u, menu.MenuEnd());
103 }
104
105 // Mimic up button 10 times
106 for (int i = 0; i < 10; i++) {
107 sel = menu.Select(--sel);
108 ASSERT_EQ(sel, menu.selection());
109
110 int expected = (9 - i) % 5;
111 ASSERT_EQ(expected, menu.selection());
112
113 ASSERT_EQ(0u, menu.MenuStart());
114 ASSERT_EQ(5u, menu.MenuEnd());
115 }
116}
117
118TEST(ScreenUITest, WearMenuSelectSmoke) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700119 int sel = 0;
Tao Baoe02a5b22018-05-02 15:46:11 -0700120 Menu menu(true, 10, 20, HEADER, ITEMS, sel);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700121 // Mimic pressing down button 10 times (2 * items size)
122 for (int i = 0; i < 10; i++) {
123 sel = menu.Select(++sel);
124 ASSERT_EQ(sel, menu.selection());
125
126 // Stops the selection at the boundary if the menu is scrollable.
127 int expected = std::min(i + 1, 4);
128 ASSERT_EQ(expected, menu.selection());
129
130 ASSERT_EQ(0u, menu.MenuStart());
131 ASSERT_EQ(5u, menu.MenuEnd());
132 }
133
134 // Mimic pressing up button 10 times
135 for (int i = 0; i < 10; i++) {
136 sel = menu.Select(--sel);
137 ASSERT_EQ(sel, menu.selection());
138
139 int expected = std::max(3 - i, 0);
140 ASSERT_EQ(expected, menu.selection());
141
142 ASSERT_EQ(0u, menu.MenuStart());
143 ASSERT_EQ(5u, menu.MenuEnd());
144 }
145}
146
147TEST(ScreenUITest, WearMenuSelectItemsOverflow) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700148 int sel = 1;
Tao Baoe02a5b22018-05-02 15:46:11 -0700149 Menu menu(true, 3, 20, HEADER, ITEMS, sel);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700150 ASSERT_EQ(5u, menu.ItemsCount());
151
152 // Scroll the menu to the end, and check the start & end of menu.
153 for (int i = 0; i < 3; i++) {
154 sel = menu.Select(++sel);
155 ASSERT_EQ(i + 2, sel);
156 ASSERT_EQ(static_cast<size_t>(i), menu.MenuStart());
157 ASSERT_EQ(static_cast<size_t>(i + 3), menu.MenuEnd());
158 }
159
160 // Press down button one more time won't change the MenuStart() and MenuEnd().
161 sel = menu.Select(++sel);
162 ASSERT_EQ(4, sel);
163 ASSERT_EQ(2u, menu.MenuStart());
164 ASSERT_EQ(5u, menu.MenuEnd());
165
166 // Scroll the menu to the top.
167 // The expected menu sel, start & ends are:
168 // sel 3, start 2, end 5
169 // sel 2, start 2, end 5
170 // sel 1, start 1, end 4
171 // sel 0, start 0, end 3
172 for (int i = 0; i < 4; i++) {
173 sel = menu.Select(--sel);
174 ASSERT_EQ(3 - i, sel);
175 ASSERT_EQ(static_cast<size_t>(std::min(3 - i, 2)), menu.MenuStart());
176 ASSERT_EQ(static_cast<size_t>(std::min(6 - i, 5)), menu.MenuEnd());
177 }
178
179 // Press up button one more time won't change the MenuStart() and MenuEnd().
180 sel = menu.Select(--sel);
181 ASSERT_EQ(0, sel);
182 ASSERT_EQ(0u, menu.MenuStart());
183 ASSERT_EQ(3u, menu.MenuEnd());
184}