Tianjie Xu | ba0460c | 2018-03-20 16:07:39 -0700 | [diff] [blame^] | 1 | /* |
| 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 | |
| 23 | constexpr const char* HEADER[] = { "header", nullptr }; |
| 24 | constexpr const char* ITEMS[] = { "items1", "items2", "items3", "items4", "1234567890", nullptr }; |
| 25 | |
| 26 | TEST(ScreenUITest, StartPhoneMenuSmoke) { |
| 27 | Menu menu(false, 10, 20); |
| 28 | ASSERT_FALSE(menu.scrollable()); |
| 29 | |
| 30 | menu.Start(HEADER, ITEMS, 0); |
| 31 | ASSERT_EQ(HEADER[0], menu.text_headers()[0]); |
| 32 | ASSERT_EQ(5u, menu.ItemsCount()); |
| 33 | |
| 34 | std::string message; |
| 35 | ASSERT_FALSE(menu.ItemsOverflow(&message)); |
| 36 | for (size_t i = 0; i < menu.ItemsCount(); i++) { |
| 37 | ASSERT_EQ(ITEMS[i], menu.TextItem(i)); |
| 38 | } |
| 39 | |
| 40 | ASSERT_EQ(0, menu.selection()); |
| 41 | } |
| 42 | |
| 43 | TEST(ScreenUITest, StartWearMenuSmoke) { |
| 44 | Menu menu(true, 10, 8); |
| 45 | ASSERT_TRUE(menu.scrollable()); |
| 46 | |
| 47 | menu.Start(HEADER, ITEMS, 1); |
| 48 | ASSERT_EQ(HEADER[0], menu.text_headers()[0]); |
| 49 | ASSERT_EQ(5u, menu.ItemsCount()); |
| 50 | |
| 51 | std::string message; |
| 52 | ASSERT_FALSE(menu.ItemsOverflow(&message)); |
| 53 | for (size_t i = 0; i < menu.ItemsCount() - 1; i++) { |
| 54 | ASSERT_EQ(ITEMS[i], menu.TextItem(i)); |
| 55 | } |
| 56 | // Test of the last item is truncated |
| 57 | ASSERT_EQ("12345678", menu.TextItem(4)); |
| 58 | ASSERT_EQ(1, menu.selection()); |
| 59 | } |
| 60 | |
| 61 | TEST(ScreenUITest, StartPhoneMenuItemsOverflow) { |
| 62 | Menu menu(false, 1, 20); |
| 63 | ASSERT_FALSE(menu.scrollable()); |
| 64 | |
| 65 | menu.Start(HEADER, ITEMS, 0); |
| 66 | ASSERT_EQ(1u, menu.ItemsCount()); |
| 67 | |
| 68 | std::string message; |
| 69 | ASSERT_FALSE(menu.ItemsOverflow(&message)); |
| 70 | for (size_t i = 0; i < menu.ItemsCount(); i++) { |
| 71 | ASSERT_EQ(ITEMS[i], menu.TextItem(i)); |
| 72 | } |
| 73 | |
| 74 | ASSERT_EQ(0u, menu.MenuStart()); |
| 75 | ASSERT_EQ(1u, menu.MenuEnd()); |
| 76 | } |
| 77 | |
| 78 | TEST(ScreenUITest, StartWearMenuItemsOverflow) { |
| 79 | Menu menu(true, 1, 20); |
| 80 | ASSERT_TRUE(menu.scrollable()); |
| 81 | |
| 82 | menu.Start(HEADER, ITEMS, 0); |
| 83 | ASSERT_EQ(5u, menu.ItemsCount()); |
| 84 | |
| 85 | std::string message; |
| 86 | ASSERT_TRUE(menu.ItemsOverflow(&message)); |
| 87 | ASSERT_EQ("Current item: 1/5", message); |
| 88 | |
| 89 | for (size_t i = 0; i < menu.ItemsCount(); i++) { |
| 90 | ASSERT_EQ(ITEMS[i], menu.TextItem(i)); |
| 91 | } |
| 92 | |
| 93 | ASSERT_EQ(0u, menu.MenuStart()); |
| 94 | ASSERT_EQ(1u, menu.MenuEnd()); |
| 95 | } |
| 96 | |
| 97 | TEST(ScreenUITest, PhoneMenuSelectSmoke) { |
| 98 | Menu menu(false, 10, 20); |
| 99 | |
| 100 | int sel = 0; |
| 101 | menu.Start(HEADER, ITEMS, sel); |
| 102 | // Mimic down button 10 times (2 * items size) |
| 103 | for (int i = 0; i < 10; i++) { |
| 104 | sel = menu.Select(++sel); |
| 105 | ASSERT_EQ(sel, menu.selection()); |
| 106 | |
| 107 | // Wraps the selection for unscrollable menu when it reaches the boundary. |
| 108 | int expected = (i + 1) % 5; |
| 109 | ASSERT_EQ(expected, menu.selection()); |
| 110 | |
| 111 | ASSERT_EQ(0u, menu.MenuStart()); |
| 112 | ASSERT_EQ(5u, menu.MenuEnd()); |
| 113 | } |
| 114 | |
| 115 | // Mimic up button 10 times |
| 116 | for (int i = 0; i < 10; i++) { |
| 117 | sel = menu.Select(--sel); |
| 118 | ASSERT_EQ(sel, menu.selection()); |
| 119 | |
| 120 | int expected = (9 - i) % 5; |
| 121 | ASSERT_EQ(expected, menu.selection()); |
| 122 | |
| 123 | ASSERT_EQ(0u, menu.MenuStart()); |
| 124 | ASSERT_EQ(5u, menu.MenuEnd()); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | TEST(ScreenUITest, WearMenuSelectSmoke) { |
| 129 | Menu menu(true, 10, 20); |
| 130 | |
| 131 | int sel = 0; |
| 132 | menu.Start(HEADER, ITEMS, sel); |
| 133 | // Mimic pressing down button 10 times (2 * items size) |
| 134 | for (int i = 0; i < 10; i++) { |
| 135 | sel = menu.Select(++sel); |
| 136 | ASSERT_EQ(sel, menu.selection()); |
| 137 | |
| 138 | // Stops the selection at the boundary if the menu is scrollable. |
| 139 | int expected = std::min(i + 1, 4); |
| 140 | ASSERT_EQ(expected, menu.selection()); |
| 141 | |
| 142 | ASSERT_EQ(0u, menu.MenuStart()); |
| 143 | ASSERT_EQ(5u, menu.MenuEnd()); |
| 144 | } |
| 145 | |
| 146 | // Mimic pressing up button 10 times |
| 147 | for (int i = 0; i < 10; i++) { |
| 148 | sel = menu.Select(--sel); |
| 149 | ASSERT_EQ(sel, menu.selection()); |
| 150 | |
| 151 | int expected = std::max(3 - i, 0); |
| 152 | ASSERT_EQ(expected, menu.selection()); |
| 153 | |
| 154 | ASSERT_EQ(0u, menu.MenuStart()); |
| 155 | ASSERT_EQ(5u, menu.MenuEnd()); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | TEST(ScreenUITest, WearMenuSelectItemsOverflow) { |
| 160 | Menu menu(true, 3, 20); |
| 161 | |
| 162 | int sel = 1; |
| 163 | menu.Start(HEADER, ITEMS, sel); |
| 164 | ASSERT_EQ(5u, menu.ItemsCount()); |
| 165 | |
| 166 | // Scroll the menu to the end, and check the start & end of menu. |
| 167 | for (int i = 0; i < 3; i++) { |
| 168 | sel = menu.Select(++sel); |
| 169 | ASSERT_EQ(i + 2, sel); |
| 170 | ASSERT_EQ(static_cast<size_t>(i), menu.MenuStart()); |
| 171 | ASSERT_EQ(static_cast<size_t>(i + 3), menu.MenuEnd()); |
| 172 | } |
| 173 | |
| 174 | // Press down button one more time won't change the MenuStart() and MenuEnd(). |
| 175 | sel = menu.Select(++sel); |
| 176 | ASSERT_EQ(4, sel); |
| 177 | ASSERT_EQ(2u, menu.MenuStart()); |
| 178 | ASSERT_EQ(5u, menu.MenuEnd()); |
| 179 | |
| 180 | // Scroll the menu to the top. |
| 181 | // The expected menu sel, start & ends are: |
| 182 | // sel 3, start 2, end 5 |
| 183 | // sel 2, start 2, end 5 |
| 184 | // sel 1, start 1, end 4 |
| 185 | // sel 0, start 0, end 3 |
| 186 | for (int i = 0; i < 4; i++) { |
| 187 | sel = menu.Select(--sel); |
| 188 | ASSERT_EQ(3 - i, sel); |
| 189 | ASSERT_EQ(static_cast<size_t>(std::min(3 - i, 2)), menu.MenuStart()); |
| 190 | ASSERT_EQ(static_cast<size_t>(std::min(6 - i, 5)), menu.MenuEnd()); |
| 191 | } |
| 192 | |
| 193 | // Press up button one more time won't change the MenuStart() and MenuEnd(). |
| 194 | sel = menu.Select(--sel); |
| 195 | ASSERT_EQ(0, sel); |
| 196 | ASSERT_EQ(0u, menu.MenuStart()); |
| 197 | ASSERT_EQ(3u, menu.MenuEnd()); |
| 198 | } |