1 /*
2 * Copyright (C) 2023 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 <inttypes.h>
18 #include <uapi/err.h>
19 #include <optional>
20 #include "device_parameters.h"
21
22 #define TLOG_TAG "confui-test"
23 #include <trusty_unittest.h>
24
25 #define MIN_EXPECTED_DRAW_COUNT 1000u
26 #define MIN_EXPECTED_DRAW_PERCENTAGE 1u
27
28 static const char* language_ids[] = {"en"};
29
30 static const char* messages[] = {
31 /* Simple message */
32 "Android Test Message",
33 /* Two line message as required by Android design guidelines */
34 "WM Line 1\nWM Line 2",
35 /* 100 'W' characters as required by Android design guidelines */
36 "WWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWWW",
37 /* 8 groups of 12 'W' characters as required by Android design
38 guidelines */
39 "WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW WWWWWWWWWWWW"};
40
TEST(confui,display_count)41 TEST(confui, display_count) {
42 int display_count = devices::getDisplayCount();
43
44 EXPECT_GT(display_count, 0);
45 }
46
47 typedef struct {
48 uint32_t count;
49 } draw_stats_t;
50
51 typedef struct {
52 const char* lang_id;
53 const char* message;
54 bool magnified;
55 bool inverse;
56 } confuip_t;
57
TEST_F_SETUP(confuip)58 TEST_F_SETUP(confuip) {
59 const void* const* param_arr = (const void* const*)GetParam();
60 const bool* magnified = (const bool*)param_arr[0];
61 const bool* inverse = (const bool*)param_arr[1];
62 const char** lang_id = (const char**)param_arr[2];
63 const char** message = (const char**)param_arr[3];
64
65 _state->magnified = *magnified;
66 _state->inverse = *inverse;
67 _state->lang_id = *lang_id;
68 _state->message = *message;
69 }
70
TEST_F_TEARDOWN(confuip)71 TEST_F_TEARDOWN(confuip) {}
72
TEST_P(confuip,display_params)73 TEST_P(confuip, display_params) {
74 int display_count = devices::getDisplayCount();
75
76 for (int i = 0; i < display_count; i++) {
77 std::optional<teeui::context<teeui::ConUIParameters>> params;
78
79 params = devices::getDisplayContext(i, _state->magnified);
80 EXPECT_EQ(params.has_value(), true, "getDisplayContext %d", i);
81
82 if (params) {
83 const uint32_t screen_width =
84 params->getParam<teeui::RightEdgeOfScreen>()->count() + 1;
85 const uint32_t screen_height =
86 params->getParam<teeui::BottomOfScreen>()->count() + 1;
87 std::optional<std::unique_ptr<teeui::layouts::ILayout>> optlayout;
88
89 optlayout = devices::getDisplayLayout(i, _state->inverse, *params);
90 EXPECT_EQ(optlayout.has_value(), true, "getDisplayLayout %d", i);
91
92 /* Configure the layout */
93 if (optlayout) {
94 std::unique_ptr<teeui::layouts::ILayout> layout =
95 std::move(*optlayout);
96 draw_stats_t stats = {};
97
98 /* Configure the layout */
99 layout->setLanguage(_state->lang_id);
100 layout->setConfirmationMessage(_state->message);
101 layout->showInstructions(true /* enable */);
102
103 auto drawPixel = teeui::makePixelDrawer(
104 [&](uint32_t x, uint32_t y,
105 teeui::Color color) -> teeui::Error {
106 TLOGD("px %u %u: %08x", x, y, color);
107
108 /* Check bounds */
109 if (x >= screen_width || y >= screen_height) {
110 return teeui::Error::OutOfBoundsDrawing;
111 }
112
113 /* Count calls, noting there is no check if some
114 * pixels are written multiple times.
115 */
116 stats.count++;
117
118 return teeui::Error::OK;
119 });
120
121 /* Render the layout */
122 teeui::Error rc = layout->drawElements(drawPixel);
123 EXPECT_EQ(rc.code(), teeui::Error::OK, "drawElements %d", i);
124
125 EXPECT_GT(stats.count, MIN_EXPECTED_DRAW_COUNT,
126 "pixel count %d", i);
127
128 const uint32_t area = screen_width * screen_height;
129 const uint32_t coverage =
130 (float)stats.count / (float)area * 1000.0f;
131
132 EXPECT_GT(coverage, MIN_EXPECTED_DRAW_PERCENTAGE * 10u,
133 "pixel coverage %" PRIu32 ".%" PRIu32 "%%",
134 coverage / 10, coverage % 10);
135
136 trusty_unittest_printf("[ DATA ] %" PRIu32 " x %" PRIu32
137 ", %" PRIu32
138 " plot calls = approx %" PRIu32
139 ".%" PRIu32 "%% coverage\n",
140 screen_width, screen_height, stats.count,
141 coverage / 10, coverage % 10);
142 }
143 }
144 }
145 }
146
confuip_to_string(const void * param,char * buf,size_t buf_size)147 void confuip_to_string(const void* param, char* buf, size_t buf_size) {
148 const void* const* param_arr = (const void* const*)GetParam();
149 const bool* magnified = (const bool*)param_arr[0];
150 const bool* inverse = (const bool*)param_arr[1];
151 const char** lang_id = (const char**)param_arr[2];
152 const char** message = (const char**)param_arr[3];
153 uint8_t msg_index = message - messages;
154
155 snprintf(buf, buf_size, "%s/msg%" PRIu8 "%s%s", *lang_id, msg_index,
156 *magnified ? "/mag" : "", *inverse ? "/inv" : "");
157 }
158
159 INSTANTIATE_TEST_SUITE_P(confui,
160 confuip,
161 testing_Combine(testing_Bool(), /* magnified */
162 testing_Bool(), /* inverse */
163 testing_ValuesIn(language_ids),
164 testing_ValuesIn(messages)),
165 confuip_to_string);
166
167 PORT_TEST(confui, "com.android.trusty.confirmationui.test");
168