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> |
Tao Bao | 152e0eb | 2018-05-13 00:34:45 -0700 | [diff] [blame] | 18 | #include <stdio.h> |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 19 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 20 | #include <functional> |
| 21 | #include <map> |
| 22 | #include <memory> |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 23 | #include <string> |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 24 | #include <vector> |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 25 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 26 | #include <android-base/logging.h> |
Tao Bao | 152e0eb | 2018-05-13 00:34:45 -0700 | [diff] [blame] | 27 | #include <android-base/stringprintf.h> |
| 28 | #include <android-base/test_utils.h> |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 29 | #include <gtest/gtest.h> |
| 30 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 31 | #include "common/test_constants.h" |
| 32 | #include "device.h" |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 33 | #include "minui/minui.h" |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 34 | #include "otautil/paths.h" |
| 35 | #include "private/resources.h" |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 36 | #include "screen_ui.h" |
| 37 | |
| 38 | static const std::vector<std::string> HEADERS{ "header" }; |
| 39 | static const std::vector<std::string> ITEMS{ "item1", "item2", "item3", "item4", "1234567890" }; |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 40 | |
| 41 | TEST(ScreenUITest, StartPhoneMenuSmoke) { |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 42 | Menu menu(false, 10, 20, HEADERS, ITEMS, 0); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 43 | ASSERT_FALSE(menu.scrollable()); |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 44 | ASSERT_EQ(HEADERS[0], menu.text_headers()[0]); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 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(); i++) { |
| 50 | ASSERT_EQ(ITEMS[i], menu.TextItem(i)); |
| 51 | } |
| 52 | |
| 53 | ASSERT_EQ(0, menu.selection()); |
| 54 | } |
| 55 | |
| 56 | TEST(ScreenUITest, StartWearMenuSmoke) { |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 57 | Menu menu(true, 10, 8, HEADERS, ITEMS, 1); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 58 | ASSERT_TRUE(menu.scrollable()); |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 59 | ASSERT_EQ(HEADERS[0], menu.text_headers()[0]); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 60 | ASSERT_EQ(5u, menu.ItemsCount()); |
| 61 | |
| 62 | std::string message; |
| 63 | ASSERT_FALSE(menu.ItemsOverflow(&message)); |
| 64 | for (size_t i = 0; i < menu.ItemsCount() - 1; i++) { |
| 65 | ASSERT_EQ(ITEMS[i], menu.TextItem(i)); |
| 66 | } |
| 67 | // Test of the last item is truncated |
| 68 | ASSERT_EQ("12345678", menu.TextItem(4)); |
| 69 | ASSERT_EQ(1, menu.selection()); |
| 70 | } |
| 71 | |
| 72 | TEST(ScreenUITest, StartPhoneMenuItemsOverflow) { |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 73 | Menu menu(false, 1, 20, HEADERS, ITEMS, 0); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 74 | ASSERT_FALSE(menu.scrollable()); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 75 | ASSERT_EQ(1u, menu.ItemsCount()); |
| 76 | |
| 77 | std::string message; |
| 78 | ASSERT_FALSE(menu.ItemsOverflow(&message)); |
| 79 | for (size_t i = 0; i < menu.ItemsCount(); i++) { |
| 80 | ASSERT_EQ(ITEMS[i], menu.TextItem(i)); |
| 81 | } |
| 82 | |
| 83 | ASSERT_EQ(0u, menu.MenuStart()); |
| 84 | ASSERT_EQ(1u, menu.MenuEnd()); |
| 85 | } |
| 86 | |
| 87 | TEST(ScreenUITest, StartWearMenuItemsOverflow) { |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 88 | Menu menu(true, 1, 20, HEADERS, ITEMS, 0); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 89 | ASSERT_TRUE(menu.scrollable()); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 90 | ASSERT_EQ(5u, menu.ItemsCount()); |
| 91 | |
| 92 | std::string message; |
| 93 | ASSERT_TRUE(menu.ItemsOverflow(&message)); |
| 94 | ASSERT_EQ("Current item: 1/5", message); |
| 95 | |
| 96 | for (size_t i = 0; i < menu.ItemsCount(); i++) { |
| 97 | ASSERT_EQ(ITEMS[i], menu.TextItem(i)); |
| 98 | } |
| 99 | |
| 100 | ASSERT_EQ(0u, menu.MenuStart()); |
| 101 | ASSERT_EQ(1u, menu.MenuEnd()); |
| 102 | } |
| 103 | |
| 104 | TEST(ScreenUITest, PhoneMenuSelectSmoke) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 105 | int sel = 0; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 106 | Menu menu(false, 10, 20, HEADERS, ITEMS, sel); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 107 | // Mimic down button 10 times (2 * items size) |
| 108 | for (int i = 0; i < 10; i++) { |
| 109 | sel = menu.Select(++sel); |
| 110 | ASSERT_EQ(sel, menu.selection()); |
| 111 | |
| 112 | // Wraps the selection for unscrollable menu when it reaches the boundary. |
| 113 | int expected = (i + 1) % 5; |
| 114 | ASSERT_EQ(expected, menu.selection()); |
| 115 | |
| 116 | ASSERT_EQ(0u, menu.MenuStart()); |
| 117 | ASSERT_EQ(5u, menu.MenuEnd()); |
| 118 | } |
| 119 | |
| 120 | // Mimic up button 10 times |
| 121 | for (int i = 0; i < 10; i++) { |
| 122 | sel = menu.Select(--sel); |
| 123 | ASSERT_EQ(sel, menu.selection()); |
| 124 | |
| 125 | int expected = (9 - i) % 5; |
| 126 | ASSERT_EQ(expected, menu.selection()); |
| 127 | |
| 128 | ASSERT_EQ(0u, menu.MenuStart()); |
| 129 | ASSERT_EQ(5u, menu.MenuEnd()); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | TEST(ScreenUITest, WearMenuSelectSmoke) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 134 | int sel = 0; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 135 | Menu menu(true, 10, 20, HEADERS, ITEMS, sel); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 136 | // Mimic pressing down button 10 times (2 * items size) |
| 137 | for (int i = 0; i < 10; i++) { |
| 138 | sel = menu.Select(++sel); |
| 139 | ASSERT_EQ(sel, menu.selection()); |
| 140 | |
| 141 | // Stops the selection at the boundary if the menu is scrollable. |
| 142 | int expected = std::min(i + 1, 4); |
| 143 | ASSERT_EQ(expected, menu.selection()); |
| 144 | |
| 145 | ASSERT_EQ(0u, menu.MenuStart()); |
| 146 | ASSERT_EQ(5u, menu.MenuEnd()); |
| 147 | } |
| 148 | |
| 149 | // Mimic pressing up button 10 times |
| 150 | for (int i = 0; i < 10; i++) { |
| 151 | sel = menu.Select(--sel); |
| 152 | ASSERT_EQ(sel, menu.selection()); |
| 153 | |
| 154 | int expected = std::max(3 - i, 0); |
| 155 | ASSERT_EQ(expected, menu.selection()); |
| 156 | |
| 157 | ASSERT_EQ(0u, menu.MenuStart()); |
| 158 | ASSERT_EQ(5u, menu.MenuEnd()); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | TEST(ScreenUITest, WearMenuSelectItemsOverflow) { |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 163 | int sel = 1; |
Tao Bao | 1fe1afe | 2018-05-01 15:56:05 -0700 | [diff] [blame] | 164 | Menu menu(true, 3, 20, HEADERS, ITEMS, sel); |
Tianjie Xu | 5fe5eb6 | 2018-03-20 16:07:39 -0700 | [diff] [blame] | 165 | ASSERT_EQ(5u, menu.ItemsCount()); |
| 166 | |
| 167 | // Scroll the menu to the end, and check the start & end of menu. |
| 168 | for (int i = 0; i < 3; i++) { |
| 169 | sel = menu.Select(++sel); |
| 170 | ASSERT_EQ(i + 2, sel); |
| 171 | ASSERT_EQ(static_cast<size_t>(i), menu.MenuStart()); |
| 172 | ASSERT_EQ(static_cast<size_t>(i + 3), menu.MenuEnd()); |
| 173 | } |
| 174 | |
| 175 | // Press down button one more time won't change the MenuStart() and MenuEnd(). |
| 176 | sel = menu.Select(++sel); |
| 177 | ASSERT_EQ(4, sel); |
| 178 | ASSERT_EQ(2u, menu.MenuStart()); |
| 179 | ASSERT_EQ(5u, menu.MenuEnd()); |
| 180 | |
| 181 | // Scroll the menu to the top. |
| 182 | // The expected menu sel, start & ends are: |
| 183 | // sel 3, start 2, end 5 |
| 184 | // sel 2, start 2, end 5 |
| 185 | // sel 1, start 1, end 4 |
| 186 | // sel 0, start 0, end 3 |
| 187 | for (int i = 0; i < 4; i++) { |
| 188 | sel = menu.Select(--sel); |
| 189 | ASSERT_EQ(3 - i, sel); |
| 190 | ASSERT_EQ(static_cast<size_t>(std::min(3 - i, 2)), menu.MenuStart()); |
| 191 | ASSERT_EQ(static_cast<size_t>(std::min(6 - i, 5)), menu.MenuEnd()); |
| 192 | } |
| 193 | |
| 194 | // Press up button one more time won't change the MenuStart() and MenuEnd(). |
| 195 | sel = menu.Select(--sel); |
| 196 | ASSERT_EQ(0, sel); |
| 197 | ASSERT_EQ(0u, menu.MenuStart()); |
| 198 | ASSERT_EQ(3u, menu.MenuEnd()); |
| 199 | } |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 200 | |
| 201 | static constexpr int kMagicAction = 101; |
| 202 | |
| 203 | enum class KeyCode : int { |
| 204 | TIMEOUT = -1, |
| 205 | NO_OP = 0, |
| 206 | UP = 1, |
| 207 | DOWN = 2, |
| 208 | ENTER = 3, |
| 209 | MAGIC = 1001, |
| 210 | LAST, |
| 211 | }; |
| 212 | |
| 213 | static const std::map<KeyCode, int> kKeyMapping{ |
| 214 | // clang-format off |
| 215 | { KeyCode::NO_OP, Device::kNoAction }, |
| 216 | { KeyCode::UP, Device::kHighlightUp }, |
| 217 | { KeyCode::DOWN, Device::kHighlightDown }, |
| 218 | { KeyCode::ENTER, Device::kInvokeItem }, |
| 219 | { KeyCode::MAGIC, kMagicAction }, |
| 220 | // clang-format on |
| 221 | }; |
| 222 | |
| 223 | class TestableScreenRecoveryUI : public ScreenRecoveryUI { |
| 224 | public: |
| 225 | int WaitKey() override; |
| 226 | |
| 227 | void SetKeyBuffer(const std::vector<KeyCode>& buffer); |
| 228 | |
| 229 | int KeyHandler(int key, bool visible) const; |
| 230 | |
Tao Bao | 152e0eb | 2018-05-13 00:34:45 -0700 | [diff] [blame] | 231 | // The following functions expose the protected members for test purpose. |
| 232 | void RunLoadAnimation() { |
| 233 | LoadAnimation(); |
| 234 | } |
| 235 | |
| 236 | size_t GetLoopFrames() const { |
| 237 | return loop_frames; |
| 238 | } |
| 239 | |
| 240 | size_t GetIntroFrames() const { |
| 241 | return intro_frames; |
| 242 | } |
| 243 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 244 | bool GetRtlLocale() const { |
| 245 | return rtl_locale_; |
| 246 | } |
| 247 | |
| 248 | private: |
| 249 | std::vector<KeyCode> key_buffer_; |
| 250 | size_t key_buffer_index_; |
| 251 | }; |
| 252 | |
| 253 | void TestableScreenRecoveryUI::SetKeyBuffer(const std::vector<KeyCode>& buffer) { |
| 254 | key_buffer_ = buffer; |
| 255 | key_buffer_index_ = 0; |
| 256 | } |
| 257 | |
| 258 | int TestableScreenRecoveryUI::KeyHandler(int key, bool) const { |
| 259 | KeyCode key_code = static_cast<KeyCode>(key); |
| 260 | if (kKeyMapping.find(key_code) != kKeyMapping.end()) { |
| 261 | return kKeyMapping.at(key_code); |
| 262 | } |
| 263 | return Device::kNoAction; |
| 264 | } |
| 265 | |
| 266 | int TestableScreenRecoveryUI::WaitKey() { |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 267 | if (IsKeyInterrupted()) { |
| 268 | return static_cast<int>(RecoveryUI::KeyError::INTERRUPTED); |
| 269 | } |
| 270 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 271 | CHECK_LT(key_buffer_index_, key_buffer_.size()); |
| 272 | return static_cast<int>(key_buffer_[key_buffer_index_++]); |
| 273 | } |
| 274 | |
| 275 | class ScreenRecoveryUITest : public ::testing::Test { |
| 276 | protected: |
| 277 | const std::string kTestLocale = "en-US"; |
| 278 | const std::string kTestRtlLocale = "ar"; |
Tao Bao | 347a659 | 2018-05-08 15:58:29 -0700 | [diff] [blame] | 279 | const std::string kTestRtlLocaleWithSuffix = "ar-EG"; |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 280 | |
| 281 | void SetUp() override { |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 282 | has_graphics_ = gr_init() == 0; |
| 283 | gr_exit(); |
| 284 | |
| 285 | if (has_graphics_) { |
| 286 | ui_ = std::make_unique<TestableScreenRecoveryUI>(); |
| 287 | } |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 288 | |
Tao Bao | 152e0eb | 2018-05-13 00:34:45 -0700 | [diff] [blame] | 289 | testdata_dir_ = from_testdata_base(""); |
| 290 | Paths::Get().set_resource_dir(testdata_dir_); |
| 291 | res_set_resource_dir(testdata_dir_); |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 292 | } |
| 293 | |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 294 | bool has_graphics_; |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 295 | std::unique_ptr<TestableScreenRecoveryUI> ui_; |
Tao Bao | 152e0eb | 2018-05-13 00:34:45 -0700 | [diff] [blame] | 296 | std::string testdata_dir_; |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 297 | }; |
| 298 | |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 299 | #define RETURN_IF_NO_GRAPHICS \ |
| 300 | do { \ |
| 301 | if (!has_graphics_) { \ |
| 302 | GTEST_LOG_(INFO) << "Test skipped due to no available graphics device"; \ |
| 303 | return; \ |
| 304 | } \ |
| 305 | } while (false) |
| 306 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 307 | TEST_F(ScreenRecoveryUITest, Init) { |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 308 | RETURN_IF_NO_GRAPHICS; |
| 309 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 310 | ASSERT_TRUE(ui_->Init(kTestLocale)); |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 311 | ASSERT_EQ(kTestLocale, ui_->GetLocale()); |
| 312 | ASSERT_FALSE(ui_->GetRtlLocale()); |
| 313 | ASSERT_FALSE(ui_->IsTextVisible()); |
| 314 | ASSERT_FALSE(ui_->WasTextEverVisible()); |
| 315 | } |
| 316 | |
Tao Bao | 94371fd | 2018-06-06 07:38:54 -0700 | [diff] [blame] | 317 | TEST_F(ScreenRecoveryUITest, dtor_NotCallingInit) { |
| 318 | ui_.reset(); |
| 319 | ASSERT_FALSE(ui_); |
| 320 | } |
| 321 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 322 | TEST_F(ScreenRecoveryUITest, ShowText) { |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 323 | RETURN_IF_NO_GRAPHICS; |
| 324 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 325 | ASSERT_TRUE(ui_->Init(kTestLocale)); |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 326 | ASSERT_FALSE(ui_->IsTextVisible()); |
| 327 | ui_->ShowText(true); |
| 328 | ASSERT_TRUE(ui_->IsTextVisible()); |
| 329 | ASSERT_TRUE(ui_->WasTextEverVisible()); |
| 330 | |
| 331 | ui_->ShowText(false); |
| 332 | ASSERT_FALSE(ui_->IsTextVisible()); |
| 333 | ASSERT_TRUE(ui_->WasTextEverVisible()); |
| 334 | } |
| 335 | |
| 336 | TEST_F(ScreenRecoveryUITest, RtlLocale) { |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 337 | RETURN_IF_NO_GRAPHICS; |
| 338 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 339 | ASSERT_TRUE(ui_->Init(kTestRtlLocale)); |
| 340 | ASSERT_TRUE(ui_->GetRtlLocale()); |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 341 | } |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 342 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 343 | TEST_F(ScreenRecoveryUITest, RtlLocaleWithSuffix) { |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 344 | RETURN_IF_NO_GRAPHICS; |
| 345 | |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 346 | ASSERT_TRUE(ui_->Init(kTestRtlLocaleWithSuffix)); |
| 347 | ASSERT_TRUE(ui_->GetRtlLocale()); |
| 348 | } |
| 349 | |
| 350 | TEST_F(ScreenRecoveryUITest, ShowMenu) { |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 351 | RETURN_IF_NO_GRAPHICS; |
| 352 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 353 | ASSERT_TRUE(ui_->Init(kTestLocale)); |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 354 | ui_->SetKeyBuffer({ |
| 355 | KeyCode::UP, |
| 356 | KeyCode::DOWN, |
| 357 | KeyCode::UP, |
| 358 | KeyCode::DOWN, |
| 359 | KeyCode::ENTER, |
| 360 | }); |
| 361 | ASSERT_EQ(3u, ui_->ShowMenu(HEADERS, ITEMS, 3, true, |
| 362 | std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(), |
| 363 | std::placeholders::_1, std::placeholders::_2))); |
| 364 | |
| 365 | ui_->SetKeyBuffer({ |
| 366 | KeyCode::UP, |
| 367 | KeyCode::UP, |
| 368 | KeyCode::NO_OP, |
| 369 | KeyCode::NO_OP, |
| 370 | KeyCode::UP, |
| 371 | KeyCode::ENTER, |
| 372 | }); |
| 373 | ASSERT_EQ(2u, ui_->ShowMenu(HEADERS, ITEMS, 0, true, |
| 374 | std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(), |
| 375 | std::placeholders::_1, std::placeholders::_2))); |
| 376 | } |
| 377 | |
| 378 | TEST_F(ScreenRecoveryUITest, ShowMenu_NotMenuOnly) { |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 379 | RETURN_IF_NO_GRAPHICS; |
| 380 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 381 | ASSERT_TRUE(ui_->Init(kTestLocale)); |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 382 | ui_->SetKeyBuffer({ |
| 383 | KeyCode::MAGIC, |
| 384 | }); |
| 385 | ASSERT_EQ(static_cast<size_t>(kMagicAction), |
| 386 | ui_->ShowMenu(HEADERS, ITEMS, 3, false, |
| 387 | std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(), |
| 388 | std::placeholders::_1, std::placeholders::_2))); |
| 389 | } |
| 390 | |
| 391 | TEST_F(ScreenRecoveryUITest, ShowMenu_TimedOut) { |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 392 | RETURN_IF_NO_GRAPHICS; |
| 393 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 394 | ASSERT_TRUE(ui_->Init(kTestLocale)); |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 395 | ui_->SetKeyBuffer({ |
| 396 | KeyCode::TIMEOUT, |
| 397 | }); |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 398 | ASSERT_EQ(static_cast<size_t>(RecoveryUI::KeyError::TIMED_OUT), |
| 399 | ui_->ShowMenu(HEADERS, ITEMS, 3, true, nullptr)); |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | TEST_F(ScreenRecoveryUITest, ShowMenu_TimedOut_TextWasEverVisible) { |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 403 | RETURN_IF_NO_GRAPHICS; |
| 404 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 405 | ASSERT_TRUE(ui_->Init(kTestLocale)); |
Tao Bao | 6cd8168 | 2018-05-03 21:53:11 -0700 | [diff] [blame] | 406 | ui_->ShowText(true); |
| 407 | ui_->ShowText(false); |
| 408 | ASSERT_TRUE(ui_->WasTextEverVisible()); |
| 409 | |
| 410 | ui_->SetKeyBuffer({ |
| 411 | KeyCode::TIMEOUT, |
| 412 | KeyCode::DOWN, |
| 413 | KeyCode::ENTER, |
| 414 | }); |
| 415 | ASSERT_EQ(4u, ui_->ShowMenu(HEADERS, ITEMS, 3, true, |
| 416 | std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(), |
| 417 | std::placeholders::_1, std::placeholders::_2))); |
| 418 | } |
Tao Bao | 152e0eb | 2018-05-13 00:34:45 -0700 | [diff] [blame] | 419 | |
Jerry Zhang | b76af93 | 2018-05-22 12:08:35 -0700 | [diff] [blame] | 420 | TEST_F(ScreenRecoveryUITest, ShowMenuWithInterrupt) { |
| 421 | RETURN_IF_NO_GRAPHICS; |
| 422 | |
| 423 | ASSERT_TRUE(ui_->Init(kTestLocale)); |
| 424 | ui_->SetKeyBuffer({ |
| 425 | KeyCode::UP, |
| 426 | KeyCode::DOWN, |
| 427 | KeyCode::UP, |
| 428 | KeyCode::DOWN, |
| 429 | KeyCode::ENTER, |
| 430 | }); |
| 431 | |
| 432 | ui_->InterruptKey(); |
| 433 | ASSERT_EQ(static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED), |
| 434 | ui_->ShowMenu(HEADERS, ITEMS, 3, true, |
| 435 | std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(), |
| 436 | std::placeholders::_1, std::placeholders::_2))); |
| 437 | |
| 438 | ui_->SetKeyBuffer({ |
| 439 | KeyCode::UP, |
| 440 | KeyCode::UP, |
| 441 | KeyCode::NO_OP, |
| 442 | KeyCode::NO_OP, |
| 443 | KeyCode::UP, |
| 444 | KeyCode::ENTER, |
| 445 | }); |
| 446 | ASSERT_EQ(static_cast<size_t>(RecoveryUI::KeyError::INTERRUPTED), |
| 447 | ui_->ShowMenu(HEADERS, ITEMS, 0, true, |
| 448 | std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(), |
| 449 | std::placeholders::_1, std::placeholders::_2))); |
| 450 | } |
| 451 | |
Tao Bao | 152e0eb | 2018-05-13 00:34:45 -0700 | [diff] [blame] | 452 | TEST_F(ScreenRecoveryUITest, LoadAnimation) { |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 453 | RETURN_IF_NO_GRAPHICS; |
| 454 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 455 | ASSERT_TRUE(ui_->Init(kTestLocale)); |
Tao Bao | 152e0eb | 2018-05-13 00:34:45 -0700 | [diff] [blame] | 456 | // Make a few copies of loop00000.png from testdata. |
| 457 | std::string image_data; |
| 458 | ASSERT_TRUE(android::base::ReadFileToString(testdata_dir_ + "/loop00000.png", &image_data)); |
| 459 | |
| 460 | std::vector<std::string> tempfiles; |
| 461 | TemporaryDir resource_dir; |
| 462 | for (const auto& name : { "00002", "00100", "00050" }) { |
| 463 | tempfiles.push_back(android::base::StringPrintf("%s/loop%s.png", resource_dir.path, name)); |
| 464 | ASSERT_TRUE(android::base::WriteStringToFile(image_data, tempfiles.back())); |
| 465 | } |
| 466 | for (const auto& name : { "00", "01" }) { |
| 467 | tempfiles.push_back(android::base::StringPrintf("%s/intro%s.png", resource_dir.path, name)); |
| 468 | ASSERT_TRUE(android::base::WriteStringToFile(image_data, tempfiles.back())); |
| 469 | } |
| 470 | Paths::Get().set_resource_dir(resource_dir.path); |
| 471 | |
| 472 | ui_->RunLoadAnimation(); |
| 473 | |
| 474 | ASSERT_EQ(2u, ui_->GetIntroFrames()); |
| 475 | ASSERT_EQ(3u, ui_->GetLoopFrames()); |
| 476 | |
| 477 | for (const auto& name : tempfiles) { |
| 478 | ASSERT_EQ(0, unlink(name.c_str())); |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | TEST_F(ScreenRecoveryUITest, LoadAnimation_MissingAnimation) { |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 483 | RETURN_IF_NO_GRAPHICS; |
| 484 | |
Tao Bao | 26ea959 | 2018-05-09 16:32:02 -0700 | [diff] [blame] | 485 | ASSERT_TRUE(ui_->Init(kTestLocale)); |
Tao Bao | b1c5b62 | 2018-07-12 11:49:05 -0700 | [diff] [blame] | 486 | // We need a dir that doesn't contain any animation. However, using TemporaryDir will give |
| 487 | // leftovers since this is a death test where TemporaryDir::~TemporaryDir() won't be called. |
| 488 | Paths::Get().set_resource_dir("/proc/self"); |
Tao Bao | 42be0d4 | 2018-06-05 14:03:34 -0700 | [diff] [blame] | 489 | |
| 490 | ::testing::FLAGS_gtest_death_test_style = "threadsafe"; |
Tao Bao | 152e0eb | 2018-05-13 00:34:45 -0700 | [diff] [blame] | 491 | ASSERT_EXIT(ui_->RunLoadAnimation(), ::testing::KilledBySignal(SIGABRT), ""); |
| 492 | } |
Tao Bao | 51f16ec | 2018-06-04 11:40:20 -0700 | [diff] [blame] | 493 | |
| 494 | #undef RETURN_IF_NO_GRAPHICS |