1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "rs_graphic_test.h"
17 #include "rs_graphic_test_director.h"
18 #include "rs_graphic_test_utils.h"
19 #include "rs_parameter_parse.h"
20 #include "rs_trace.h"
21 #include "transaction/rs_interfaces.h"
22 #include "ui/rs_root_node.h"
23 #include "ui/rs_surface_node.h"
24
25 #include <chrono>
26 #include <filesystem>
27 #include <iostream>
28 #include <thread>
29
30 using namespace std;
31 namespace OHOS {
32 namespace Rosen {
33 namespace {
34 constexpr uint32_t SURFACE_COLOR = 0xffffffff;
35 constexpr int64_t LOAD_TREE_WAIT_TIME = 3;
36 constexpr int PLAYBACK_PREPARE_WAIT_TIME = 5000;
37
ShouldRunCurrentTest()38 bool ShouldRunCurrentTest()
39 {
40 const ::testing::TestInfo* const testInfo =
41 ::testing::UnitTest::GetInstance()->current_test_info();
42 const auto& extInfo = ::OHOS::Rosen::TestDefManager::Instance().GetTestInfo(
43 testInfo->test_case_name(), testInfo->name());
44 const auto& params = RSParameterParse::Instance();
45 if (!extInfo) {
46 LOGE("RSGraphicTest no testinfo %{public}s-%{public}s",
47 testInfo->test_case_name(), testInfo->name());
48 return false;
49 }
50 if (!params.filterTestTypes.empty() && params.filterTestTypes.count(extInfo->testType) == 0) {
51 return false;
52 }
53
54 if (params.runTestMode != RSGraphicTestMode::ALL && extInfo->testMode != params.runTestMode) {
55 return false;
56 }
57
58 return true;
59 }
60 }
61
62 uint32_t RSGraphicTest::imageWriteId_ = 0;
63
SetUpTestCase()64 void RSGraphicTest::SetUpTestCase()
65 {
66 imageWriteId_ = 0;
67 }
68
TearDownTestCase()69 void RSGraphicTest::TearDownTestCase()
70 {
71 return;
72 }
73
GetScreenCapacity(const string testCase)74 UIPoint RSGraphicTest::GetScreenCapacity(const string testCase)
75 {
76 int testCnt = ::OHOS::Rosen::TestDefManager::Instance().GetTestCaseCnt(testCase);
77 int cl = 1;
78 int num = 1;
79 while (num < testCnt) {
80 cl++;
81 num = cl * cl;
82 }
83 int rl = ceil(static_cast<double>(testCnt) / cl);
84 return {cl, rl};
85 }
86
GetPos(int id,int cl)87 UIPoint RSGraphicTest::GetPos(int id, int cl)
88 {
89 if (cl > 0) {
90 int x = id % cl;
91 int y = id / cl;
92 return {x, y};
93 }
94
95 return {0, 0};
96 }
97
SetUp()98 void RSGraphicTest::SetUp()
99 {
100 shouldRunTest_ = ShouldRunCurrentTest();
101 if (!shouldRunTest_) {
102 GTEST_SKIP();
103 return;
104 }
105
106 RSSurfaceNodeConfig config;
107 string surfaceNodeName = "TestSurface";
108 config.SurfaceNodeName = surfaceNodeName.append(to_string(imageWriteId_));
109 auto testSurface = RSSurfaceNode::Create(config, false);
110
111 testSurface->SetBounds({0, 0, GetScreenSize()[0], GetScreenSize()[1]});
112 testSurface->SetFrame({0, 0, GetScreenSize()[0], GetScreenSize()[1]});
113 testSurface->SetBackgroundColor(SURFACE_COLOR);
114 GetRootNode()->SetTestSurface(testSurface);
115
116 BeforeEach();
117
118 const ::testing::TestInfo* const testInfo =
119 ::testing::UnitTest::GetInstance()->current_test_info();
120 const auto& extInfo = ::OHOS::Rosen::TestDefManager::Instance().GetTestInfo(
121 testInfo->test_case_name(), testInfo->name());
122 if (extInfo == nullptr) {
123 return;
124 }
125 auto size = GetScreenSize();
126 cout << "SetUp:size:" << size.x_ << "*" << size.y_ << endl;
127 if (!extInfo->isMultiple) {
128 cout << "SetUp:isMultiple is false" << endl;
129 SetScreenSurfaceBounds({0, 0, size.x_, size.y_});
130 return;
131 }
132
133 cout << "SetUp:isMultiple is true" << endl;
134 auto capacity = GetScreenCapacity(string(testInfo->test_case_name()));
135 cout << "SetUp:capacity:" << capacity.x_ << "*" << capacity.y_ << endl;
136 if (imageWriteId_ == 0) {
137 SetScreenSurfaceBounds({0, 0, capacity.x_*size.x_, capacity.y_*size.y_});
138 cout << "ScreenSurfaceBounds:[" << capacity.x_*size.x_ << "*" << capacity.y_*size.y_ << "]" << endl;
139 }
140 auto pos = GetPos(extInfo->testId, capacity.x_);
141 cout << "pos:id:" << imageWriteId_ << "[" << pos.x_ << "." << pos.y_ << "]" << endl;
142
143 testSurface->SetBounds({pos.x_*size.x_, pos.y_*size.y_, size.x_, size.y_});
144 testSurface->SetFrame({pos.x_*size.x_, pos.y_*size.y_, size.x_, size.y_});
145 }
146
WaitOtherTest()147 bool RSGraphicTest::WaitOtherTest()
148 {
149 if (IsSingleTest()) {
150 return false;
151 }
152 const ::testing::TestInfo* const testInfo =
153 ::testing::UnitTest::GetInstance()->current_test_info();
154 const auto& extInfo = ::OHOS::Rosen::TestDefManager::Instance().GetTestInfo(
155 testInfo->test_case_name(), testInfo->name());
156 int testCaseCnt = ::OHOS::Rosen::TestDefManager::Instance().GetTestCaseCnt(
157 string(testInfo->test_case_name()));
158 if (extInfo == nullptr) {
159 return true;
160 }
161 if (!extInfo->isMultiple) {
162 return false;
163 }
164 if (++imageWriteId_ < testCaseCnt) {
165 return true;
166 }
167 return false;
168 }
169
TestCaseCapture(bool isScreenshot)170 void RSGraphicTest::TestCaseCapture(bool isScreenshot)
171 {
172 const ::testing::TestInfo* const testInfo =
173 ::testing::UnitTest::GetInstance()->current_test_info();
174 const auto& extInfo = ::OHOS::Rosen::TestDefManager::Instance().GetTestInfo(
175 testInfo->test_case_name(), testInfo->name());
176
177 auto pixelMap = RSGraphicTestDirector::Instance().TakeScreenCaptureAndWait(
178 RSParameterParse::Instance().surfaceCaptureWaitTime, isScreenshot);
179 if (pixelMap) {
180 std::string filename = GetImageSavePath(extInfo->filePath);
181 filename += testInfo->test_case_name();
182 if (!extInfo->isMultiple) {
183 filename += std::string("_") + testInfo->name();
184 }
185 filename += std::string(".png");
186 if (std::filesystem::exists(filename)) {
187 LOGW("RSGraphicTest file exists %{public}s", filename.c_str());
188 }
189 if (!WriteToPngWithPixelMap(filename, *pixelMap)) {
190 LOGE("RSGraphicTest::TearDown write image failed %{public}s-%{public}s",
191 testInfo->test_case_name(), testInfo->name());
192 }
193 std::cout << "png write to " << filename << std::endl;
194 }
195 }
196
TearDown()197 void RSGraphicTest::TearDown()
198 {
199 if (!shouldRunTest_ || RSParameterParse::Instance().skipCapture_) {
200 GetRootNode()->ResetTestSurface();
201 return;
202 }
203
204 const ::testing::TestInfo* const testInfo =
205 ::testing::UnitTest::GetInstance()->current_test_info();
206 const auto& extInfo = ::OHOS::Rosen::TestDefManager::Instance().GetTestInfo(
207 testInfo->test_case_name(), testInfo->name());
208
209 if (WaitOtherTest()) {
210 return;
211 }
212
213 StartUIAnimation();
214 RSGraphicTestDirector::Instance().FlushMessage();
215 WaitTimeout(RSParameterParse::Instance().testCaseWaitTime);
216
217 bool isManualTest = false;
218 if (extInfo) {
219 isManualTest = (extInfo->testMode == RSGraphicTestMode::MANUAL);
220 } else {
221 LOGE("RSGraphicTest no testinfo %{public}s-%{public}s", testInfo->test_case_name(), testInfo->name());
222 return;
223 }
224
225 bool isDynamicTest = extInfo->testMode == RSGraphicTestMode::DYNAMIC;
226 if (isManualTest) {
227 WaitTimeout(RSParameterParse::Instance().manualTestWaitTime);
228 } else {
229 TestCaseCapture(isDynamicTest);
230 }
231
232 AfterEach();
233 WaitTimeout(RSParameterParse::Instance().testCaseWaitTime);
234
235 if (isDynamicTest) {
236 PlaybackStop();
237 }
238
239 GetRootNode()->ResetTestSurface();
240 RSGraphicTestDirector::Instance().SendProfilerCommand("rssubtree_clear");
241 RSGraphicTestDirector::Instance().FlushMessage();
242 WaitTimeout(RSParameterParse::Instance().testCaseWaitTime);
243 }
244
RegisterNode(std::shared_ptr<RSNode> node)245 void RSGraphicTest::RegisterNode(std::shared_ptr<RSNode> node)
246 {
247 nodes_.push_back(node);
248 GetRootNode()->RegisterNode(node);
249 }
250
AddFileRenderNodeTreeToNode(std::shared_ptr<RSNode> node,const std::string & filePath)251 void RSGraphicTest::AddFileRenderNodeTreeToNode(std::shared_ptr<RSNode> node, const std::string& filePath)
252 {
253 if (!shouldRunTest_ || RSParameterParse::Instance().skipCapture_) {
254 return;
255 }
256 //need flush client node to rs firstly
257 RSGraphicTestDirector::Instance().FlushMessage();
258 WaitTimeout(RSParameterParse::Instance().testCaseWaitTime);
259
260 std::cout << "load subbtree to node file path is " << filePath << std::endl;
261 std::string command = "rssubtree_load " + std::to_string(node->GetId()) + " " + filePath;
262 RSGraphicTestDirector::Instance().SendProfilerCommand(command);
263 RSGraphicTestDirector::Instance().FlushMessage();
264 WaitTimeout(LOAD_TREE_WAIT_TIME * RSParameterParse::Instance().testCaseWaitTime);
265 }
266
PlaybackRecover(const std::string & filePath,float pauseTimeStamp)267 void RSGraphicTest::PlaybackRecover(const std::string& filePath, float pauseTimeStamp)
268 {
269 if (!shouldRunTest_ || RSParameterParse::Instance().skipCapture_) {
270 return;
271 }
272 // playback prepare
273 int64_t pid = 0;
274 std::string command =
275 "rsrecord_replay_prepare " + std::to_string(pid) + " " + std::to_string(pauseTimeStamp) + " " + filePath;
276 std::cout << "Playback Prepare: " << command << std::endl;
277 RSGraphicTestDirector::Instance().SendProfilerCommand(command);
278 WaitTimeout(PLAYBACK_PREPARE_WAIT_TIME);
279
280 // playback start
281 command = "rsrecord_replay";
282 std::cout << "Playback Start: " << command << std::endl;
283 RSGraphicTestDirector::Instance().SendProfilerCommand(command);
284 WaitTimeout(static_cast<int>(pauseTimeStamp * UNIT_SEC_TO_MS) + RSParameterParse::Instance().testCaseWaitTime);
285 }
286
PlaybackStop()287 void RSGraphicTest::PlaybackStop()
288 {
289 // playback stop
290 std::string command = "rsrecord_replay_stop";
291 std::cout << "Playback Stop: " << command << std::endl;
292 RSGraphicTestDirector::Instance().SendProfilerCommand(command);
293 }
294
GetRootNode() const295 std::shared_ptr<RSGraphicRootNode> RSGraphicTest::GetRootNode() const
296 {
297 return RSGraphicTestDirector::Instance().GetRootNode();
298 }
299
GetScreenSize() const300 Vector2f RSGraphicTest::GetScreenSize() const
301 {
302 return RSGraphicTestDirector::Instance().GetScreenSize();
303 }
304
SetSurfaceBounds(const Vector4f & bounds)305 void RSGraphicTest::SetSurfaceBounds(const Vector4f& bounds)
306 {
307 RSGraphicTestDirector::Instance().SetSurfaceBounds(bounds);
308 }
309
SetScreenSurfaceBounds(const Vector4f & bounds)310 void RSGraphicTest::SetScreenSurfaceBounds(const Vector4f& bounds)
311 {
312 RSGraphicTestDirector::Instance().SetScreenSurfaceBounds(bounds);
313 }
314
SetSurfaceColor(const RSColor & color)315 void RSGraphicTest::SetSurfaceColor(const RSColor& color)
316 {
317 RSGraphicTestDirector::Instance().SetSurfaceColor(color);
318 }
319
SetScreenSize(float width,float height)320 void RSGraphicTest::SetScreenSize(float width, float height)
321 {
322 RSGraphicTestDirector::Instance().SetScreenSize(width, height);
323 }
324
IsSingleTest()325 bool RSGraphicTest::IsSingleTest()
326 {
327 return RSGraphicTestDirector::Instance().IsSingleTest();
328 }
329
GetImageSavePath(const std::string path)330 std::string RSGraphicTest::GetImageSavePath(const std::string path)
331 {
332 std::string imagePath = "/data/local/";
333 size_t posCnt = path.rfind("/") + 1;
334 std::string subPath = path.substr(0, posCnt);
335 imagePath.append(subPath);
336
337 namespace fs = std::filesystem;
338 if (!fs::exists(imagePath)) {
339 if (!fs::create_directories(imagePath)) {
340 LOGE("RSGraphicTestDirector create dir failed");
341 }
342 } else {
343 if (!fs::is_directory(imagePath)) {
344 LOGE("RSGraphicTestDirector path is not dir");
345 }
346 }
347
348 return imagePath;
349 }
350
StartUIAnimation()351 void RSGraphicTest::StartUIAnimation()
352 {
353 RSGraphicTestDirector::Instance().StartRunUIAnimation();
354 }
355 } // namespace Rosen
356 } // namespace OHOS