blob: 269222faabd91b854d8a691afb55a81fad674270 [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>
Tao Bao152e0eb2018-05-13 00:34:45 -070018#include <stdio.h>
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070019
Tao Bao6cd81682018-05-03 21:53:11 -070020#include <functional>
21#include <map>
22#include <memory>
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070023#include <string>
Tao Bao1fe1afe2018-05-01 15:56:05 -070024#include <vector>
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070025
Tao Bao6cd81682018-05-03 21:53:11 -070026#include <android-base/logging.h>
Tao Bao152e0eb2018-05-13 00:34:45 -070027#include <android-base/stringprintf.h>
28#include <android-base/test_utils.h>
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070029#include <gtest/gtest.h>
30
Tao Bao6cd81682018-05-03 21:53:11 -070031#include "common/test_constants.h"
32#include "device.h"
33#include "otautil/paths.h"
34#include "private/resources.h"
Tao Bao1fe1afe2018-05-01 15:56:05 -070035#include "screen_ui.h"
36
37static const std::vector<std::string> HEADERS{ "header" };
38static const std::vector<std::string> ITEMS{ "item1", "item2", "item3", "item4", "1234567890" };
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070039
40TEST(ScreenUITest, StartPhoneMenuSmoke) {
Tao Bao1fe1afe2018-05-01 15:56:05 -070041 Menu menu(false, 10, 20, HEADERS, ITEMS, 0);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070042 ASSERT_FALSE(menu.scrollable());
Tao Bao1fe1afe2018-05-01 15:56:05 -070043 ASSERT_EQ(HEADERS[0], menu.text_headers()[0]);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070044 ASSERT_EQ(5u, menu.ItemsCount());
45
46 std::string message;
47 ASSERT_FALSE(menu.ItemsOverflow(&message));
48 for (size_t i = 0; i < menu.ItemsCount(); i++) {
49 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
50 }
51
52 ASSERT_EQ(0, menu.selection());
53}
54
55TEST(ScreenUITest, StartWearMenuSmoke) {
Tao Bao1fe1afe2018-05-01 15:56:05 -070056 Menu menu(true, 10, 8, HEADERS, ITEMS, 1);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070057 ASSERT_TRUE(menu.scrollable());
Tao Bao1fe1afe2018-05-01 15:56:05 -070058 ASSERT_EQ(HEADERS[0], menu.text_headers()[0]);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070059 ASSERT_EQ(5u, menu.ItemsCount());
60
61 std::string message;
62 ASSERT_FALSE(menu.ItemsOverflow(&message));
63 for (size_t i = 0; i < menu.ItemsCount() - 1; i++) {
64 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
65 }
66 // Test of the last item is truncated
67 ASSERT_EQ("12345678", menu.TextItem(4));
68 ASSERT_EQ(1, menu.selection());
69}
70
71TEST(ScreenUITest, StartPhoneMenuItemsOverflow) {
Tao Bao1fe1afe2018-05-01 15:56:05 -070072 Menu menu(false, 1, 20, HEADERS, ITEMS, 0);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070073 ASSERT_FALSE(menu.scrollable());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070074 ASSERT_EQ(1u, menu.ItemsCount());
75
76 std::string message;
77 ASSERT_FALSE(menu.ItemsOverflow(&message));
78 for (size_t i = 0; i < menu.ItemsCount(); i++) {
79 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
80 }
81
82 ASSERT_EQ(0u, menu.MenuStart());
83 ASSERT_EQ(1u, menu.MenuEnd());
84}
85
86TEST(ScreenUITest, StartWearMenuItemsOverflow) {
Tao Bao1fe1afe2018-05-01 15:56:05 -070087 Menu menu(true, 1, 20, HEADERS, ITEMS, 0);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070088 ASSERT_TRUE(menu.scrollable());
Tianjie Xu5fe5eb62018-03-20 16:07:39 -070089 ASSERT_EQ(5u, menu.ItemsCount());
90
91 std::string message;
92 ASSERT_TRUE(menu.ItemsOverflow(&message));
93 ASSERT_EQ("Current item: 1/5", message);
94
95 for (size_t i = 0; i < menu.ItemsCount(); i++) {
96 ASSERT_EQ(ITEMS[i], menu.TextItem(i));
97 }
98
99 ASSERT_EQ(0u, menu.MenuStart());
100 ASSERT_EQ(1u, menu.MenuEnd());
101}
102
103TEST(ScreenUITest, PhoneMenuSelectSmoke) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700104 int sel = 0;
Tao Bao1fe1afe2018-05-01 15:56:05 -0700105 Menu menu(false, 10, 20, HEADERS, ITEMS, sel);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700106 // Mimic down button 10 times (2 * items size)
107 for (int i = 0; i < 10; i++) {
108 sel = menu.Select(++sel);
109 ASSERT_EQ(sel, menu.selection());
110
111 // Wraps the selection for unscrollable menu when it reaches the boundary.
112 int expected = (i + 1) % 5;
113 ASSERT_EQ(expected, menu.selection());
114
115 ASSERT_EQ(0u, menu.MenuStart());
116 ASSERT_EQ(5u, menu.MenuEnd());
117 }
118
119 // Mimic up button 10 times
120 for (int i = 0; i < 10; i++) {
121 sel = menu.Select(--sel);
122 ASSERT_EQ(sel, menu.selection());
123
124 int expected = (9 - i) % 5;
125 ASSERT_EQ(expected, menu.selection());
126
127 ASSERT_EQ(0u, menu.MenuStart());
128 ASSERT_EQ(5u, menu.MenuEnd());
129 }
130}
131
132TEST(ScreenUITest, WearMenuSelectSmoke) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700133 int sel = 0;
Tao Bao1fe1afe2018-05-01 15:56:05 -0700134 Menu menu(true, 10, 20, HEADERS, ITEMS, sel);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700135 // Mimic pressing down button 10 times (2 * items size)
136 for (int i = 0; i < 10; i++) {
137 sel = menu.Select(++sel);
138 ASSERT_EQ(sel, menu.selection());
139
140 // Stops the selection at the boundary if the menu is scrollable.
141 int expected = std::min(i + 1, 4);
142 ASSERT_EQ(expected, menu.selection());
143
144 ASSERT_EQ(0u, menu.MenuStart());
145 ASSERT_EQ(5u, menu.MenuEnd());
146 }
147
148 // Mimic pressing up button 10 times
149 for (int i = 0; i < 10; i++) {
150 sel = menu.Select(--sel);
151 ASSERT_EQ(sel, menu.selection());
152
153 int expected = std::max(3 - i, 0);
154 ASSERT_EQ(expected, menu.selection());
155
156 ASSERT_EQ(0u, menu.MenuStart());
157 ASSERT_EQ(5u, menu.MenuEnd());
158 }
159}
160
161TEST(ScreenUITest, WearMenuSelectItemsOverflow) {
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700162 int sel = 1;
Tao Bao1fe1afe2018-05-01 15:56:05 -0700163 Menu menu(true, 3, 20, HEADERS, ITEMS, sel);
Tianjie Xu5fe5eb62018-03-20 16:07:39 -0700164 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}
Tao Bao6cd81682018-05-03 21:53:11 -0700199
200static constexpr int kMagicAction = 101;
201
202enum class KeyCode : int {
203 TIMEOUT = -1,
204 NO_OP = 0,
205 UP = 1,
206 DOWN = 2,
207 ENTER = 3,
208 MAGIC = 1001,
209 LAST,
210};
211
212static const std::map<KeyCode, int> kKeyMapping{
213 // clang-format off
214 { KeyCode::NO_OP, Device::kNoAction },
215 { KeyCode::UP, Device::kHighlightUp },
216 { KeyCode::DOWN, Device::kHighlightDown },
217 { KeyCode::ENTER, Device::kInvokeItem },
218 { KeyCode::MAGIC, kMagicAction },
219 // clang-format on
220};
221
222class TestableScreenRecoveryUI : public ScreenRecoveryUI {
223 public:
224 int WaitKey() override;
225
226 void SetKeyBuffer(const std::vector<KeyCode>& buffer);
227
228 int KeyHandler(int key, bool visible) const;
229
Tao Bao152e0eb2018-05-13 00:34:45 -0700230 // The following functions expose the protected members for test purpose.
231 void RunLoadAnimation() {
232 LoadAnimation();
233 }
234
235 size_t GetLoopFrames() const {
236 return loop_frames;
237 }
238
239 size_t GetIntroFrames() const {
240 return intro_frames;
241 }
242
Tao Bao6cd81682018-05-03 21:53:11 -0700243 bool GetRtlLocale() const {
244 return rtl_locale_;
245 }
246
247 private:
248 std::vector<KeyCode> key_buffer_;
249 size_t key_buffer_index_;
250};
251
252void TestableScreenRecoveryUI::SetKeyBuffer(const std::vector<KeyCode>& buffer) {
253 key_buffer_ = buffer;
254 key_buffer_index_ = 0;
255}
256
257int TestableScreenRecoveryUI::KeyHandler(int key, bool) const {
258 KeyCode key_code = static_cast<KeyCode>(key);
259 if (kKeyMapping.find(key_code) != kKeyMapping.end()) {
260 return kKeyMapping.at(key_code);
261 }
262 return Device::kNoAction;
263}
264
265int TestableScreenRecoveryUI::WaitKey() {
266 CHECK_LT(key_buffer_index_, key_buffer_.size());
267 return static_cast<int>(key_buffer_[key_buffer_index_++]);
268}
269
270class ScreenRecoveryUITest : public ::testing::Test {
271 protected:
272 const std::string kTestLocale = "en-US";
273 const std::string kTestRtlLocale = "ar";
Tao Bao347a6592018-05-08 15:58:29 -0700274 const std::string kTestRtlLocaleWithSuffix = "ar-EG";
Tao Bao6cd81682018-05-03 21:53:11 -0700275
276 void SetUp() override {
277 ui_ = std::make_unique<TestableScreenRecoveryUI>();
278
Tao Bao152e0eb2018-05-13 00:34:45 -0700279 testdata_dir_ = from_testdata_base("");
280 Paths::Get().set_resource_dir(testdata_dir_);
281 res_set_resource_dir(testdata_dir_);
Tao Bao6cd81682018-05-03 21:53:11 -0700282
283 ASSERT_TRUE(ui_->Init(kTestLocale));
284 }
285
286 std::unique_ptr<TestableScreenRecoveryUI> ui_;
Tao Bao152e0eb2018-05-13 00:34:45 -0700287 std::string testdata_dir_;
Tao Bao6cd81682018-05-03 21:53:11 -0700288};
289
290TEST_F(ScreenRecoveryUITest, Init) {
291 ASSERT_EQ(kTestLocale, ui_->GetLocale());
292 ASSERT_FALSE(ui_->GetRtlLocale());
293 ASSERT_FALSE(ui_->IsTextVisible());
294 ASSERT_FALSE(ui_->WasTextEverVisible());
295}
296
297TEST_F(ScreenRecoveryUITest, ShowText) {
298 ASSERT_FALSE(ui_->IsTextVisible());
299 ui_->ShowText(true);
300 ASSERT_TRUE(ui_->IsTextVisible());
301 ASSERT_TRUE(ui_->WasTextEverVisible());
302
303 ui_->ShowText(false);
304 ASSERT_FALSE(ui_->IsTextVisible());
305 ASSERT_TRUE(ui_->WasTextEverVisible());
306}
307
308TEST_F(ScreenRecoveryUITest, RtlLocale) {
309 ASSERT_TRUE(ui_->Init(kTestRtlLocale));
310 ASSERT_TRUE(ui_->GetRtlLocale());
311
312 ASSERT_TRUE(ui_->Init(kTestRtlLocaleWithSuffix));
313 ASSERT_TRUE(ui_->GetRtlLocale());
314}
315
316TEST_F(ScreenRecoveryUITest, ShowMenu) {
317 ui_->SetKeyBuffer({
318 KeyCode::UP,
319 KeyCode::DOWN,
320 KeyCode::UP,
321 KeyCode::DOWN,
322 KeyCode::ENTER,
323 });
324 ASSERT_EQ(3u, ui_->ShowMenu(HEADERS, ITEMS, 3, true,
325 std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(),
326 std::placeholders::_1, std::placeholders::_2)));
327
328 ui_->SetKeyBuffer({
329 KeyCode::UP,
330 KeyCode::UP,
331 KeyCode::NO_OP,
332 KeyCode::NO_OP,
333 KeyCode::UP,
334 KeyCode::ENTER,
335 });
336 ASSERT_EQ(2u, ui_->ShowMenu(HEADERS, ITEMS, 0, true,
337 std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(),
338 std::placeholders::_1, std::placeholders::_2)));
339}
340
341TEST_F(ScreenRecoveryUITest, ShowMenu_NotMenuOnly) {
342 ui_->SetKeyBuffer({
343 KeyCode::MAGIC,
344 });
345 ASSERT_EQ(static_cast<size_t>(kMagicAction),
346 ui_->ShowMenu(HEADERS, ITEMS, 3, false,
347 std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(),
348 std::placeholders::_1, std::placeholders::_2)));
349}
350
351TEST_F(ScreenRecoveryUITest, ShowMenu_TimedOut) {
352 ui_->SetKeyBuffer({
353 KeyCode::TIMEOUT,
354 });
355 ASSERT_EQ(static_cast<size_t>(-1), ui_->ShowMenu(HEADERS, ITEMS, 3, true, nullptr));
356}
357
358TEST_F(ScreenRecoveryUITest, ShowMenu_TimedOut_TextWasEverVisible) {
359 ui_->ShowText(true);
360 ui_->ShowText(false);
361 ASSERT_TRUE(ui_->WasTextEverVisible());
362
363 ui_->SetKeyBuffer({
364 KeyCode::TIMEOUT,
365 KeyCode::DOWN,
366 KeyCode::ENTER,
367 });
368 ASSERT_EQ(4u, ui_->ShowMenu(HEADERS, ITEMS, 3, true,
369 std::bind(&TestableScreenRecoveryUI::KeyHandler, ui_.get(),
370 std::placeholders::_1, std::placeholders::_2)));
371}
Tao Bao152e0eb2018-05-13 00:34:45 -0700372
373TEST_F(ScreenRecoveryUITest, LoadAnimation) {
374 // Make a few copies of loop00000.png from testdata.
375 std::string image_data;
376 ASSERT_TRUE(android::base::ReadFileToString(testdata_dir_ + "/loop00000.png", &image_data));
377
378 std::vector<std::string> tempfiles;
379 TemporaryDir resource_dir;
380 for (const auto& name : { "00002", "00100", "00050" }) {
381 tempfiles.push_back(android::base::StringPrintf("%s/loop%s.png", resource_dir.path, name));
382 ASSERT_TRUE(android::base::WriteStringToFile(image_data, tempfiles.back()));
383 }
384 for (const auto& name : { "00", "01" }) {
385 tempfiles.push_back(android::base::StringPrintf("%s/intro%s.png", resource_dir.path, name));
386 ASSERT_TRUE(android::base::WriteStringToFile(image_data, tempfiles.back()));
387 }
388 Paths::Get().set_resource_dir(resource_dir.path);
389
390 ui_->RunLoadAnimation();
391
392 ASSERT_EQ(2u, ui_->GetIntroFrames());
393 ASSERT_EQ(3u, ui_->GetLoopFrames());
394
395 for (const auto& name : tempfiles) {
396 ASSERT_EQ(0, unlink(name.c_str()));
397 }
398}
399
400TEST_F(ScreenRecoveryUITest, LoadAnimation_MissingAnimation) {
401 TemporaryDir resource_dir;
402 Paths::Get().set_resource_dir(resource_dir.path);
403 ASSERT_EXIT(ui_->RunLoadAnimation(), ::testing::KilledBySignal(SIGABRT), "");
404}