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