blob: e47d7054bcb6bda1b95f9837d29208a6d9c9e48b [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
Tao Bao1fe1afe2018-05-01 15:56:05 -070017#include <stddef.h>
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070018
19#include <string>
Tao Bao1fe1afe2018-05-01 15:56:05 -070020#include <vector>
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070021
22#include <gtest/gtest.h>
23
Tao Bao1fe1afe2018-05-01 15:56:05 -070024#include "screen_ui.h"
25
26static const std::vector<std::string> HEADERS{ "header" };
27static const std::vector<std::string> ITEMS{ "item1", "item2", "item3", "item4", "1234567890" };
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070028
29TEST(ScreenUITest, StartPhoneMenuSmoke) {
Tao Bao1fe1afe2018-05-01 15:56:05 -070030 Menu menu(false, 10, 20, HEADERS, ITEMS, 0);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070031 ASSERT_FALSE(menu.scrollable());
Tao Bao1fe1afe2018-05-01 15:56:05 -070032 ASSERT_EQ(HEADERS[0], menu.text_headers()[0]);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070033 ASSERT_EQ(5u, menu.ItemsCount());
34
35 std::string message;
36 ASSERT_FALSE(menu.ItemsOverflow(&message));
37 for (size_t i = 0; i < menu.ItemsCount(); i++) {
38 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
39 }
40
41 ASSERT_EQ(0, menu.selection());
42}
43
44TEST(ScreenUITest, StartWearMenuSmoke) {
Tao Bao1fe1afe2018-05-01 15:56:05 -070045 Menu menu(true, 10, 8, HEADERS, ITEMS, 1);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070046 ASSERT_TRUE(menu.scrollable());
Tao Bao1fe1afe2018-05-01 15:56:05 -070047 ASSERT_EQ(HEADERS[0], menu.text_headers()[0]);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070048 ASSERT_EQ(5u, menu.ItemsCount());
49
50 std::string message;
51 ASSERT_FALSE(menu.ItemsOverflow(&message));
52 for (size_t i = 0; i < menu.ItemsCount() - 1; i++) {
53 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
54 }
55 // Test of the last item is truncated
56 ASSERT_EQ("12345678", menu.TextItem(4));
57 ASSERT_EQ(1, menu.selection());
58}
59
60TEST(ScreenUITest, StartPhoneMenuItemsOverflow) {
Tao Bao1fe1afe2018-05-01 15:56:05 -070061 Menu menu(false, 1, 20, HEADERS, ITEMS, 0);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070062 ASSERT_FALSE(menu.scrollable());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070063 ASSERT_EQ(1u, menu.ItemsCount());
64
65 std::string message;
66 ASSERT_FALSE(menu.ItemsOverflow(&message));
67 for (size_t i = 0; i < menu.ItemsCount(); i++) {
68 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
69 }
70
71 ASSERT_EQ(0u, menu.MenuStart());
72 ASSERT_EQ(1u, menu.MenuEnd());
73}
74
75TEST(ScreenUITest, StartWearMenuItemsOverflow) {
Tao Bao1fe1afe2018-05-01 15:56:05 -070076 Menu menu(true, 1, 20, HEADERS, ITEMS, 0);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070077 ASSERT_TRUE(menu.scrollable());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070078 ASSERT_EQ(5u, menu.ItemsCount());
79
80 std::string message;
81 ASSERT_TRUE(menu.ItemsOverflow(&message));
82 ASSERT_EQ("Current item: 1/5", message);
83
84 for (size_t i = 0; i < menu.ItemsCount(); i++) {
85 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
86 }
87
88 ASSERT_EQ(0u, menu.MenuStart());
89 ASSERT_EQ(1u, menu.MenuEnd());
90}
91
92TEST(ScreenUITest, PhoneMenuSelectSmoke) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070093 int sel = 0;
Tao Bao1fe1afe2018-05-01 15:56:05 -070094 Menu menu(false, 10, 20, HEADERS, ITEMS, sel);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070095 // Mimic down button 10 times (2 * items size)
96 for (int i = 0; i < 10; i++) {
97 sel = menu.Select(++sel);
98 ASSERT_EQ(sel, menu.selection());
99
100 // Wraps the selection for unscrollable menu when it reaches the boundary.
101 int expected = (i + 1) % 5;
102 ASSERT_EQ(expected, menu.selection());
103
104 ASSERT_EQ(0u, menu.MenuStart());
105 ASSERT_EQ(5u, menu.MenuEnd());
106 }
107
108 // Mimic up button 10 times
109 for (int i = 0; i < 10; i++) {
110 sel = menu.Select(--sel);
111 ASSERT_EQ(sel, menu.selection());
112
113 int expected = (9 - i) % 5;
114 ASSERT_EQ(expected, menu.selection());
115
116 ASSERT_EQ(0u, menu.MenuStart());
117 ASSERT_EQ(5u, menu.MenuEnd());
118 }
119}
120
121TEST(ScreenUITest, WearMenuSelectSmoke) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700122 int sel = 0;
Tao Bao1fe1afe2018-05-01 15:56:05 -0700123 Menu menu(true, 10, 20, HEADERS, ITEMS, sel);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700124 // Mimic pressing down button 10 times (2 * items size)
125 for (int i = 0; i < 10; i++) {
126 sel = menu.Select(++sel);
127 ASSERT_EQ(sel, menu.selection());
128
129 // Stops the selection at the boundary if the menu is scrollable.
130 int expected = std::min(i + 1, 4);
131 ASSERT_EQ(expected, menu.selection());
132
133 ASSERT_EQ(0u, menu.MenuStart());
134 ASSERT_EQ(5u, menu.MenuEnd());
135 }
136
137 // Mimic pressing up button 10 times
138 for (int i = 0; i < 10; i++) {
139 sel = menu.Select(--sel);
140 ASSERT_EQ(sel, menu.selection());
141
142 int expected = std::max(3 - i, 0);
143 ASSERT_EQ(expected, menu.selection());
144
145 ASSERT_EQ(0u, menu.MenuStart());
146 ASSERT_EQ(5u, menu.MenuEnd());
147 }
148}
149
150TEST(ScreenUITest, WearMenuSelectItemsOverflow) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700151 int sel = 1;
Tao Bao1fe1afe2018-05-01 15:56:05 -0700152 Menu menu(true, 3, 20, HEADERS, ITEMS, sel);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700153 ASSERT_EQ(5u, menu.ItemsCount());
154
155 // Scroll the menu to the end, and check the start & end of menu.
156 for (int i = 0; i < 3; i++) {
157 sel = menu.Select(++sel);
158 ASSERT_EQ(i + 2, sel);
159 ASSERT_EQ(static_cast<size_t>(i), menu.MenuStart());
160 ASSERT_EQ(static_cast<size_t>(i + 3), menu.MenuEnd());
161 }
162
163 // Press down button one more time won't change the MenuStart() and MenuEnd().
164 sel = menu.Select(++sel);
165 ASSERT_EQ(4, sel);
166 ASSERT_EQ(2u, menu.MenuStart());
167 ASSERT_EQ(5u, menu.MenuEnd());
168
169 // Scroll the menu to the top.
170 // The expected menu sel, start & ends are:
171 // sel 3, start 2, end 5
172 // sel 2, start 2, end 5
173 // sel 1, start 1, end 4
174 // sel 0, start 0, end 3
175 for (int i = 0; i < 4; i++) {
176 sel = menu.Select(--sel);
177 ASSERT_EQ(3 - i, sel);
178 ASSERT_EQ(static_cast<size_t>(std::min(3 - i, 2)), menu.MenuStart());
179 ASSERT_EQ(static_cast<size_t>(std::min(6 - i, 5)), menu.MenuEnd());
180 }
181
182 // Press up button one more time won't change the MenuStart() and MenuEnd().
183 sel = menu.Select(--sel);
184 ASSERT_EQ(0, sel);
185 ASSERT_EQ(0u, menu.MenuStart());
186 ASSERT_EQ(3u, menu.MenuEnd());
187}