Tianjie Xu | 5fe5eb6 | 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 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 17 | #include <stddef.h> |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 18 | |
| 19 | #include <string> |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 20 | #include <vector> |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 21 | |
| 22 | #include <gtest/gtest.h> |
| 23 | |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 24 | #include "screen_ui.h" |
| 25 | |
| 26 | static const std::vector<std::string> HEADERS{ "header" }; |
| 27 | static const std::vector<std::string> ITEMS{ "item1", "item2", "item3", "item4", "1234567890" }; |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 28 | |
| 29 | TEST(ScreenUITest, StartPhoneMenuSmoke) { |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 30 | Menu menu(false, 10, 20, HEADERS, ITEMS, 0); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 31 | ASSERT_FALSE(menu.scrollable()); |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 32 | ASSERT_EQ(HEADERS[0], menu.text_headers()[0]); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 33 | 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 | |
| 44 | TEST(ScreenUITest, StartWearMenuSmoke) { |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 45 | Menu menu(true, 10, 8, HEADERS, ITEMS, 1); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 46 | ASSERT_TRUE(menu.scrollable()); |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 47 | ASSERT_EQ(HEADERS[0], menu.text_headers()[0]); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 48 | 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 | |
| 60 | TEST(ScreenUITest, StartPhoneMenuItemsOverflow) { |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 61 | Menu menu(false, 1, 20, HEADERS, ITEMS, 0); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 62 | ASSERT_FALSE(menu.scrollable()); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 63 | 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 | |
| 75 | TEST(ScreenUITest, StartWearMenuItemsOverflow) { |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 76 | Menu menu(true, 1, 20, HEADERS, ITEMS, 0); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 77 | ASSERT_TRUE(menu.scrollable()); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 78 | 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 | |
| 92 | TEST(ScreenUITest, PhoneMenuSelectSmoke) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 93 | int sel = 0; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 94 | Menu menu(false, 10, 20, HEADERS, ITEMS, sel); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 95 | // 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 | |
| 121 | TEST(ScreenUITest, WearMenuSelectSmoke) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 122 | int sel = 0; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 123 | Menu menu(true, 10, 20, HEADERS, ITEMS, sel); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 124 | // 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 | |
| 150 | TEST(ScreenUITest, WearMenuSelectItemsOverflow) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 151 | int sel = 1; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 152 | Menu menu(true, 3, 20, HEADERS, ITEMS, sel); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 153 | 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 | } |