1 /*
2  * Copyright (c) 2023 iSoftStone Information Technology (Group) 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 "gmock/gmock.h"
17 #include "gtest/gtest.h"
18 #define private public
19 #define protected public
20 #include "core/common/rosen/rosen_asset_manager.h"
21 #include "core/common/rosen/rosen_convert_helper.h"
22 #include "core/common/rosen/rosen_enums.h"
23 #include "core/common/rosen/rosen_window.h"
24 #undef private
25 #undef protected
26 #include "mock_rosen.h"
27 #include "test/mock/core/common/mock_container.h"
28 
29 #include "adapter/ohos/entrance/file_asset_provider_impl.h"
30 #include "core/pipeline/base/render_node.h"
31 
32 using namespace testing;
33 using namespace testing::ext;
34 
35 namespace OHOS::Ace {
36 namespace {
37 const std::string RET_TEST = "error";
38 const std::string HAPPATTH = "/system/app/com.ohos.photos/Photos.hap";
39 const std::string ASSETBASEPATHS = "/resources/base/profile/";
40 uint64_t nanoTimestamp_ = 0;
41 uint32_t frameCount_ = 0;
42 AceVsyncCallback callback_;
43 } // namespace
44 
45 class RosenTest : public testing::Test {
46 public:
SetUpTestCase()47     static void SetUpTestCase() {}
TearDownTestCase()48     static void TearDownTestCase() {}
SetUp()49     void SetUp() {}
TearDown()50     void TearDown() {}
51 };
52 
53 /**
54  * @tc.name: CastToRosenTest001
55  * @tc.desc: Test cast to rosen.
56  * @tc.type: FUNC
57  */
58 HWTEST_F(RosenTest, CastToRosenTest001, TestSize.Level1)
59 {
60     /**
61      * @tc.steps: step1. Build a object RSWindow.
62      */
63     Platform::RSWindow object;
64 
65     /**
66      * @tc.steps: step2. callback Create.
67      * @tc.expected: step2. Return expected results..
68      */
69     auto result1 = PlatformWindow::Create(nullptr);
70     EXPECT_EQ(result1, nullptr);
71 
72     /**
73      * @tc.steps: step3. callback SetRootRenderNode.
74      * @tc.expected: step3. SetRootRenderNode success.
75      */
76     object.SetRootRenderNode(nullptr);
77 }
78 
79 /**
80  * @tc.name: CastToRosenTest002
81  * @tc.desc: Test cast to rosen.
82  * @tc.type: FUNC
83  */
84 HWTEST_F(RosenTest, CastToRosenTest002, TestSize.Level1)
85 {
86     /**
87      * @tc.steps: step1. Build a object RSWindow.
88      */
89     Platform::RSWindow object;
90 
91     /**
92      * @tc.steps: step2. callback RegisterVsyncCallback.
93      * @tc.expected: step2. Return expected results.
94      */
__anon2d11be5a0202(uint64_t deltaNanoTimestamp, uint32_t deltaFrameCount) 95     auto callback = [](uint64_t deltaNanoTimestamp, uint32_t deltaFrameCount) {
96         nanoTimestamp_ += deltaNanoTimestamp;
97         frameCount_ += deltaFrameCount;
98         if (callback_) {
99             callback_(nanoTimestamp_, frameCount_);
100         }
101     };
102     object.RegisterVsyncCallback(callback);
103     EXPECT_EQ(nanoTimestamp_, 0);
104 }
105 
106 /**
107  * @tc.name: CastToRosenTest004
108  * @tc.desc: Test cast to rosen.
109  * @tc.type: FUNC
110  */
111 HWTEST_F(RosenTest, CastToRosenTest004, TestSize.Level1)
112 {
113     /**
114      * @tc.steps: step1. Build a object RSWindow.
115      */
116     Platform::RSWindow object;
117 
118     /**
119      * @tc.steps: step2. callback RequestFrame.vsyncThread_ is null.
120      * @tc.expected: step2. Return expected results.
121      */
__anon2d11be5a0302(uint64_t deltaNanoTimestamp, uint32_t deltaFrameCount) 122     auto callback = [](uint64_t deltaNanoTimestamp, uint32_t deltaFrameCount) {
123         nanoTimestamp_ += deltaNanoTimestamp;
124         frameCount_ += deltaFrameCount;
125         if (callback_) {
126             callback_(nanoTimestamp_, frameCount_);
127         }
128     };
129     object.RegisterVsyncCallback(callback);
130     object.vsyncRequests_.Push(true);
131     object.RequestFrame();
132     EXPECT_EQ(object.vsyncRequests_.queue_.size(), 2);
133 
134     /**
135      * @tc.steps: step3. callback Destroy.
136      * @tc.expected: step3. Return expected results.
137      */
138     object.Destroy();
139     EXPECT_EQ(object.vsyncRequests_.queue_.size(), 0);
140 
141     /**
142      * @tc.steps: step4. callback RequestFrame.vsyncThread_ is not null.
143      * @tc.expected: step4. Return expected results.
144      */
145     object.vsyncThread_ = std::make_unique<std::thread>();
146     object.RequestFrame();
147     EXPECT_EQ(object.vsyncRequests_.queue_.size(), 1);
148 }
149 
150 /**
151  * @tc.name: CastToRosenTest005
152  * @tc.desc: Test cast to rosen.
153  * @tc.type: FUNC
154  */
155 HWTEST_F(RosenTest, CastToRosenTest005, TestSize.Level1)
156 {
157     /**
158      * @tc.steps: step1. callback GetRosenBlurStyleValue.The blurStyleOption parameter is the default value.
159      * @tc.expected: step1. Return expected results.
160      */
161     auto result = GetRosenBlurStyleValue(BlurStyleOption());
162     EXPECT_EQ(result, MATERIAL_BLUR_STYLE::NO_MATERIAL);
163 
164     /**
165      * @tc.steps: step2. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
166      * @tc.expected: step2. Return expected results.
167      */
168     BlurStyleOption blurStyleOption;
169     blurStyleOption.blurStyle = BlurStyle::THIN;
170     blurStyleOption.colorMode = ThemeColorMode::DARK;
171     auto retDark = GetRosenBlurStyleValue(blurStyleOption);
172     EXPECT_EQ(retDark, MATERIAL_BLUR_STYLE::STYLE_CARD_THIN_DARK);
173 
174     /**
175      * @tc.steps: step3. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
176      * @tc.expected: step3. Return expected results.
177      */
178     blurStyleOption.blurStyle = BlurStyle::REGULAR;
179     blurStyleOption.colorMode = ThemeColorMode::DARK;
180     auto retDark1 = GetRosenBlurStyleValue(blurStyleOption);
181     EXPECT_EQ(retDark1, MATERIAL_BLUR_STYLE::STYLE_CARD_DARK);
182 
183     /**
184      * @tc.steps: step4. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
185      * @tc.expected: step4. Return expected results.
186      */
187     blurStyleOption.blurStyle = BlurStyle::THICK;
188     blurStyleOption.colorMode = ThemeColorMode::DARK;
189     auto retDark2 = GetRosenBlurStyleValue(blurStyleOption);
190     EXPECT_EQ(retDark2, MATERIAL_BLUR_STYLE::STYLE_CARD_THICK_DARK);
191 
192     /**
193      * @tc.steps: step5. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
194      * @tc.expected: step5. Return expected results.
195      */
196     blurStyleOption.blurStyle = BlurStyle::BACKGROUND_THIN;
197     blurStyleOption.colorMode = ThemeColorMode::DARK;
198     auto retDark3 = GetRosenBlurStyleValue(blurStyleOption);
199     EXPECT_EQ(retDark3, MATERIAL_BLUR_STYLE::STYLE_BACKGROUND_SMALL_DARK);
200 
201     /**
202      * @tc.steps: step6. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
203      * @tc.expected: step6. Return expected results.
204      */
205     blurStyleOption.blurStyle = BlurStyle::BACKGROUND_REGULAR;
206     blurStyleOption.colorMode = ThemeColorMode::DARK;
207     auto retDark4 = GetRosenBlurStyleValue(blurStyleOption);
208     EXPECT_EQ(retDark4, MATERIAL_BLUR_STYLE::STYLE_BACKGROUND_MEDIUM_DARK);
209 
210     /**
211      * @tc.steps: step7. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
212      * @tc.expected: step7. Return expected results.
213      */
214     blurStyleOption.blurStyle = BlurStyle::BACKGROUND_THICK;
215     blurStyleOption.colorMode = ThemeColorMode::DARK;
216     auto retDark5 = GetRosenBlurStyleValue(blurStyleOption);
217     EXPECT_EQ(retDark5, MATERIAL_BLUR_STYLE::STYLE_BACKGROUND_LARGE_DARK);
218 
219     /**
220      * @tc.steps: step8. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
221      * @tc.expected: step8. Return expected results.
222      */
223     blurStyleOption.blurStyle = BlurStyle::BACKGROUND_ULTRA_THICK;
224     blurStyleOption.colorMode = ThemeColorMode::DARK;
225     auto retDark6 = GetRosenBlurStyleValue(blurStyleOption);
226     EXPECT_EQ(retDark6, MATERIAL_BLUR_STYLE::STYLE_BACKGROUND_XLARGE_DARK);
227 }
228 
229 /**
230  * @tc.name: CastToRosenTest006
231  * @tc.desc: Test cast to rosen.
232  * @tc.type: FUNC
233  */
234 HWTEST_F(RosenTest, CastToRosenTest006, TestSize.Level1)
235 {
236     /**
237      * @tc.steps: step1. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
238      * @tc.expected: step1. Return expected results.
239      */
240     BlurStyleOption blurStyleOption;
241     blurStyleOption.blurStyle = BlurStyle::NO_MATERIAL;
242     blurStyleOption.colorMode = ThemeColorMode::LIGHT;
243     auto retLight = GetRosenBlurStyleValue(blurStyleOption);
244     EXPECT_EQ(retLight, MATERIAL_BLUR_STYLE::NO_MATERIAL);
245 
246     /**
247      * @tc.steps: step2. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
248      * @tc.expected: step2. Return expected results.
249      */
250     blurStyleOption.blurStyle = BlurStyle::THIN;
251     blurStyleOption.colorMode = ThemeColorMode::LIGHT;
252     auto retLight1 = GetRosenBlurStyleValue(blurStyleOption);
253     EXPECT_EQ(retLight1, MATERIAL_BLUR_STYLE::STYLE_CARD_THIN_LIGHT);
254 
255     /**
256      * @tc.steps: step3. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
257      * @tc.expected: step3. Return expected results.
258      */
259     blurStyleOption.blurStyle = BlurStyle::REGULAR;
260     blurStyleOption.colorMode = ThemeColorMode::LIGHT;
261     auto retLight2 = GetRosenBlurStyleValue(blurStyleOption);
262     EXPECT_EQ(retLight2, MATERIAL_BLUR_STYLE::STYLE_CARD_LIGHT);
263 
264     /**
265      * @tc.steps: step4. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
266      * @tc.expected: step4. Return expected results.
267      */
268     blurStyleOption.blurStyle = BlurStyle::THICK;
269     blurStyleOption.colorMode = ThemeColorMode::LIGHT;
270     auto retLight3 = GetRosenBlurStyleValue(blurStyleOption);
271     EXPECT_EQ(retLight3, MATERIAL_BLUR_STYLE::STYLE_CARD_THICK_LIGHT);
272 
273     /**
274      * @tc.steps: step5. callback GetRosenBlurStyleValue.The blurStyleOption parameter is new value.
275      * @tc.expected: step5. Return expected results.
276      */
277     blurStyleOption.blurStyle = BlurStyle::BACKGROUND_ULTRA_THICK;
278     blurStyleOption.colorMode = ThemeColorMode::LIGHT;
279     auto retLight4 = GetRosenBlurStyleValue(blurStyleOption);
280     EXPECT_EQ(retLight4, MATERIAL_BLUR_STYLE::STYLE_BACKGROUND_XLARGE_LIGHT);
281 }
282 
283 /**
284  * @tc.name: CastToRosenTest007
285  * @tc.desc: Test cast to rosen.
286  * @tc.type: FUNC
287  */
288 HWTEST_F(RosenTest, CastToRosenTest007, TestSize.Level1)
289 {
290     /**
291      * @tc.steps: step1. Build a object.
292      * @tc.steps: step2. callback GetAssetPath input provider is null.
293      * @tc.expected: step2. Return expected results.
294      */
295     RSAssetManager object;
296     auto assetProvider = AceType::MakeRefPtr<Ace::FileAssetProviderImpl>();
297     std::string hapPath = "/system/app/com.ohos.photos/Photos.hap";
298     std::vector<std::string> assetBasePaths;
299     assetBasePaths.push_back("resources/base/profile/");
300     assetProvider->Initialize(hapPath, assetBasePaths);
301     object.PushBack(std::move(assetProvider));
302     std::string assetName = RET_TEST;
303     std::string result = object.GetAssetPath(assetName, true);
304     EXPECT_EQ(result, "");
305 }
306 
307 /**
308  * @tc.name: CastToRosenTest008
309  * @tc.desc: Test cast to rosen.
310  * @tc.type: FUNC
311  */
312 HWTEST_F(RosenTest, CastToRosenTest008, TestSize.Level1)
313 {
314     /**
315      * @tc.steps: step1. Build a object.
316      * @tc.steps: step2. callback GetAssetPath input provider is null.
317      * @tc.expected: step2. Return expected results..
318      */
319     RSAssetManager object;
320     std::string assetName = RET_TEST;
321     std::string ret = object.GetAssetPath(assetName, true);
322     EXPECT_EQ(ret, "");
323 
324     /**
325      * @tc.steps: step3. callback GetAssetPath input provider is not null.
326      * @tc.expected: step3. Return expected results.
327      */
328     std::string hapPath = "test";
329     auto assetProvider = AceType::MakeRefPtr<MockAssetProvider>();
330     EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true));
331     EXPECT_CALL(*assetProvider, GetAssetPath(hapPath, true)).Times(1).WillOnce(Return("test"));
332     object.PushBack(std::move(assetProvider));
333     std::string result = object.GetAssetPath(hapPath, true);
334     EXPECT_EQ(result, "test");
335 }
336 
337 /**
338  * @tc.name: CastToRosenTest009
339  * @tc.desc: Test cast to rosen.
340  * @tc.type: FUNC
341  */
342 HWTEST_F(RosenTest, CastToRosenTest009, TestSize.Level1)
343 {
344     /**
345      * @tc.steps: step1. Build a object.
346      * @tc.steps: step2. callback GetAssetList input provider is not null.
347      * @tc.expected: step2. Return expected results.
348      */
349     RSAssetManager object;
350     std::string path = RET_TEST;
351     std::vector<std::string> assetList;
352     auto assetProvider = AceType::MakeRefPtr<MockAssetProvider>();
353     EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true));
354     EXPECT_CALL(*assetProvider, GetAssetList(path, assetList)).WillOnce(Return());
355     object.PushBack(std::move(assetProvider));
356     object.GetAssetList(path, assetList);
357     EXPECT_TRUE(assetList.empty());
358 }
359 
360 /**
361  * @tc.name: CastToRosenTest0010
362  * @tc.desc: Test cast to rosen.
363  * @tc.type: FUNC
364  */
365 HWTEST_F(RosenTest, CastToRosenTest0010, TestSize.Level1)
366 {
367     /**
368      * @tc.steps: step1. Build a object.
369      * @tc.steps: step2. callback GetFileInfo input assetName is null.
370      * @tc.expected: step2. Return results is false.
371      */
372     RSAssetManager object;
373     std::string hapPath = "test";
374     MediaFileInfo fileInfo = { .fileName = hapPath, .offset = 1, .length = 1, .lastModTime = 1, .lastModDate = 1 };
375     auto assetProvider = AceType::MakeRefPtr<Ace::FileAssetProviderImpl>();
376     std::vector<std::string> assetBasePaths;
377     assetBasePaths.push_back("resources/base/profile/");
378     assetProvider->Initialize(hapPath, assetBasePaths);
379     assetProvider->GetFileInfo(hapPath, fileInfo);
380     object.PushBack(std::move(assetProvider));
381     bool result = object.GetFileInfo(hapPath, fileInfo);
382     EXPECT_FALSE(result);
383 }
384 
385 /**
386  * @tc.name: CastToRosenTest0011
387  * @tc.desc: Test cast to rosen.
388  * @tc.type: FUNC
389  */
390 HWTEST_F(RosenTest, CastToRosenTest0011, TestSize.Level1)
391 {
392     /**
393      * @tc.steps: step1. Build a object.
394      * @tc.steps: step2. callback GetAsset input assetName is null.
395      * @tc.expected: step2. The expected result is null.
396      */
397     RSAssetManager object;
398     auto result = object.GetAsset("");
399     EXPECT_EQ(result, nullptr);
400 
401     /**
402      * @tc.steps: step3. callback GetAsset input assetName is not null.
403      * @tc.expected: step3. The expected result is null.
404      */
405     std::string assetName = RET_TEST;
406     auto testAsset = object.GetAsset(assetName);
407     EXPECT_EQ(testAsset, nullptr);
408 }
409 
410 /**
411  * @tc.name: CastToRosenTest0012
412  * @tc.desc: Test cast to rosen.
413  * @tc.type: FUNC
414  */
415 HWTEST_F(RosenTest, CastToRosenTest0012, TestSize.Level1)
416 {
417     /**
418      * @tc.steps: step1. Build a object.
419      * @tc.steps: step2. callback GetAsset input provider is not null.
420      * @tc.expected: step2. The expected result is null.
421      */
422     RSAssetManager object;
423     std::string hapPath = "core/common/test/unittest/rosen/BUILD.gn";
424     auto assetProvider = AceType::MakeRefPtr<MockAssetProvider>();
425     EXPECT_CALL(*assetProvider, IsValid()).Times(1).WillOnce(Return(true));
426     EXPECT_CALL(*assetProvider, GetAsset(hapPath)).Times(1).WillOnce(Return(nullptr));
427     assetProvider->GetAsset(hapPath);
428     object.PushBack(std::move(assetProvider));
429     auto ret = object.GetAsset(hapPath);
430     EXPECT_EQ(ret, nullptr);
431 }
432 } // namespace OHOS::Ace
433