1 /*
2 * Copyright (C) 2022 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 #define private public
16 #define protected public
17 #include "wallpaper_service.h"
18 #undef private
19 #undef protected
20
21 #include <gtest/gtest.h>
22
23 #include <ctime>
24
25 #include "accesstoken_kit.h"
26 #include "directory_ex.h"
27 #include "file_deal.h"
28 #include "hilog_wrapper.h"
29 #include "image_packer.h"
30 #include "nativetoken_kit.h"
31 #include "pixel_map.h"
32 #include "scene_board_judgement.h"
33 #include "token_setproc.h"
34 #include "wallpaper_common_event_subscriber.h"
35 #include "wallpaper_manager.h"
36 #include "wallpaper_service.h"
37
38 namespace OHOS {
39 namespace WallpaperMgrService {
40 constexpr int32_t SYSTYEM = 0;
41 constexpr int32_t LOCKSCREEN = 1;
42 constexpr int32_t INVALID_WALLPAPER_TYPE = 2;
43 constexpr int32_t HUNDRED = 100;
44 constexpr int32_t DEFAULT_WALLPAPER_ID = -1;
45 constexpr int32_t FOO_MAX_LEN = 60000000;
46 constexpr int32_t TEST_USERID = 99;
47 constexpr int32_t TEST_USERID1 = 98;
48 constexpr int32_t INVALID_USERID = -1;
49 uint64_t selfTokenID_ = 0;
50 constexpr const char *URI = "/data/test/theme/wallpaper/wallpaper_test.JPG";
51 constexpr const char *URI_ZIP = "/data/test/theme/wallpaper/test.zip";
52 constexpr const char *URI_30FPS_3S_MP4 = "/data/test/theme/wallpaper/30fps_3s.mp4";
53 constexpr const char *URI_15FPS_7S_MP4 = "/data/test/theme/wallpaper/15fps_7s.mp4";
54 constexpr const char *URI_30FPS_3S_MOV = "/data/test/theme/wallpaper/30fps_3s.mov";
55 constexpr const char *WALLPAPER_DEFAULT_PATH = "/data/service/el1/public/wallpaper";
56 constexpr const char *SYSTEM_DIR = "/system";
57 constexpr const char *LOCKSCREEN_DIR = "/lockscreen";
58 constexpr const char *LOCKSCREEN_FILE = "/lockscreen/wallpaper_lock";
59 constexpr const char *WALLPAPER_DEFAULT = "wallpaperdefault.jpeg";
60 constexpr const char *HOME_WALLPAPER = "home_wallpaper_0.jpg";
61
62 std::shared_ptr<WallpaperCommonEventSubscriber> subscriber = nullptr;
63
64 using namespace testing::ext;
65 using namespace testing;
66 using namespace OHOS::Media;
67 using namespace OHOS::HiviewDFX;
68 using namespace OHOS::MiscServices;
69 using namespace OHOS::Security::AccessToken;
70
71 static HapPolicyParams policyParams = { .apl = APL_SYSTEM_CORE,
72 .domain = "test.domain",
73 .permList = { { .permissionName = "ohos.permission.GET_WALLPAPER",
74 .bundleName = "ohos.wallpaper_test.demo",
75 .grantMode = 1,
76 .availableLevel = APL_NORMAL,
77 .label = "label",
78 .labelId = 1,
79 .description = "test",
80 .descriptionId = 1 },
81 { .permissionName = "ohos.permission.SET_WALLPAPER",
82 .bundleName = "ohos.wallpaper_test.demo",
83 .grantMode = 1,
84 .availableLevel = APL_NORMAL,
85 .label = "label",
86 .labelId = 1,
87 .description = "test",
88 .descriptionId = 1 },
89 { .permissionName = "ohos.permission.MANAGE_LOCAL_ACCOUNTS",
90 .bundleName = "ohos.wallpaper_test.demo",
91 .grantMode = 1,
92 .availableLevel = APL_NORMAL,
93 .label = "label",
94 .labelId = 1,
95 .description = "test",
96 .descriptionId = 1 } },
97 .permStateList = { { .permissionName = "ohos.permission.GET_WALLPAPER",
98 .isGeneral = true,
99 .resDeviceID = { "local" },
100 .grantStatus = { PermissionState::PERMISSION_GRANTED },
101 .grantFlags = { 1 } },
102 { .permissionName = "ohos.permission.SET_WALLPAPER",
103 .isGeneral = true,
104 .resDeviceID = { "local" },
105 .grantStatus = { PermissionState::PERMISSION_GRANTED },
106 .grantFlags = { 1 } },
107 { .permissionName = "ohos.permission.MANAGE_LOCAL_ACCOUNTS",
108 .isGeneral = true,
109 .resDeviceID = { "local" },
110 .grantStatus = { PermissionState::PERMISSION_GRANTED },
111 .grantFlags = { 1 } } } };
112
113 HapInfoParams infoParams = { .userID = 1,
114 .bundleName = "wallpaper_service",
115 .instIndex = 0,
116 .appIDDesc = "test",
117 .apiVersion = 9,
118 .isSystemApp = true };
119
GrantNativePermission()120 void GrantNativePermission()
121 {
122 selfTokenID_ = GetSelfTokenID();
123 AccessTokenIDEx tokenIdEx = { 0 };
124 tokenIdEx = AccessTokenKit::AllocHapToken(infoParams, policyParams);
125 int32_t ret = SetSelfTokenID(tokenIdEx.tokenIDEx);
126 if (ret == 0) {
127 HILOG_INFO("SetSelfTokenID success!");
128 } else {
129 HILOG_ERROR("SetSelfTokenID fail!");
130 }
131 }
132
133 class WallpaperTest : public testing::Test {
134 public:
135 static void SetUpTestCase(void);
136 static void TearDownTestCase(void);
137 void SetUp();
138 void TearDown();
139 static void CreateTempImage();
140 static std::shared_ptr<PixelMap> CreateTempPixelMap();
141 static bool SubscribeCommonEvent(shared_ptr<WallpaperService> wallpaperService);
142 static void TriggerEvent(int32_t userId, const std::string &commonEventSupport);
143 static std::string GetUserFilePath(int32_t userId, const char *filePath);
144 static bool TestCallBack(int32_t num);
145 };
146 const std::string VALID_SCHEMA_STRICT_DEFINE = "{\"SCHEMA_VERSION\":\"1.0\","
147 "\"SCHEMA_MODE\":\"STRICT\","
148 "\"SCHEMA_SKIPSIZE\":0,"
149 "\"SCHEMA_DEFINE\":{"
150 "\"age\":\"INTEGER, NOT NULL\""
151 "},"
152 "\"SCHEMA_INDEXES\":[\"$.age\"]}";
153
SetUpTestCase(void)154 void WallpaperTest::SetUpTestCase(void)
155 {
156 HILOG_INFO("SetUpTestCase");
157 GrantNativePermission();
158 CreateTempImage();
159 HILOG_INFO("SetUpTestCase end");
160 }
161
TearDownTestCase(void)162 void WallpaperTest::TearDownTestCase(void)
163 {
164 HILOG_INFO("TearDownTestCase");
165 ApiInfo apiInfo{ false, false };
166 WallpaperManager::GetInstance().ResetWallpaper(SYSTYEM, apiInfo);
167 WallpaperManager::GetInstance().ResetWallpaper(LOCKSCREEN, apiInfo);
168 auto ret = SetSelfTokenID(selfTokenID_);
169 HILOG_INFO("SetSelfTokenID ret = %{public}d", ret);
170 }
171
SetUp(void)172 void WallpaperTest::SetUp(void)
173 {
174 }
175
TearDown(void)176 void WallpaperTest::TearDown(void)
177 {
178 }
179
180 class WallpaperEventListenerTestImpl : public WallpaperEventListener {
181 public:
182 std::vector<uint64_t> color_;
183 int32_t wallpaperType_;
184 WallpaperEventListenerTestImpl();
~WallpaperEventListenerTestImpl()185 ~WallpaperEventListenerTestImpl()
186 {
187 }
188
189 WallpaperEventListenerTestImpl(const WallpaperEventListenerTestImpl &) = delete;
190 WallpaperEventListenerTestImpl &operator=(const WallpaperEventListenerTestImpl &) = delete;
191 WallpaperEventListenerTestImpl(WallpaperEventListenerTestImpl &&) = delete;
192 WallpaperEventListenerTestImpl &operator=(WallpaperEventListenerTestImpl &&) = delete;
193
194 // callback function will be called when the db data is changed.
195 void OnColorsChange(const std::vector<uint64_t> &color, int32_t wallpaperType) override;
196 void OnWallpaperChange(
197 WallpaperType wallpaperType, WallpaperResourceType resourceType, const std::string &uri) override;
198 // reset the callCount_ to zero.
199 void ResetToZero();
200
201 unsigned long GetCallCount() const;
202
203 private:
204 unsigned long callCount_;
205 };
206
OnColorsChange(const std::vector<uint64_t> &color, int32_t wallpaperType)207 void WallpaperEventListenerTestImpl::OnColorsChange(const std::vector<uint64_t> &color, int32_t wallpaperType)
208 {
209 callCount_++;
210 for (auto const &each : color) {
211 color_.push_back(each);
212 }
213 wallpaperType_ = wallpaperType;
214 }
215
OnWallpaperChange( WallpaperType wallpaperType, WallpaperResourceType resourceType, const std::string &uri)216 void WallpaperEventListenerTestImpl::OnWallpaperChange(
217 WallpaperType wallpaperType, WallpaperResourceType resourceType, const std::string &uri)
218 {
219 HILOG_INFO("wallpaperType: %{public}d, resourceType: %{public}d, uri: %{public}s",
220 static_cast<int32_t>(wallpaperType), static_cast<int32_t>(resourceType), uri.c_str());
221 }
222
WallpaperEventListenerTestImpl()223 WallpaperEventListenerTestImpl::WallpaperEventListenerTestImpl()
224 {
225 callCount_ = 0;
226 wallpaperType_ = -1;
227 }
228
ResetToZero()229 void WallpaperEventListenerTestImpl::ResetToZero()
230 {
231 callCount_ = 0;
232 }
233
GetCallCount() const234 unsigned long WallpaperEventListenerTestImpl::GetCallCount() const
235 {
236 return callCount_;
237 }
238
CreateTempImage()239 void WallpaperTest::CreateTempImage()
240 {
241 std::shared_ptr<PixelMap> pixelMap = CreateTempPixelMap();
242 ImagePacker imagePacker;
243 PackOption option;
244 option.format = "image/jpeg";
245 option.quality = HUNDRED;
246 option.numberHint = 1;
247 std::set<std::string> formats;
248 imagePacker.GetSupportedFormats(formats);
249 imagePacker.StartPacking(URI, option);
250 HILOG_INFO("AddImage start");
251 imagePacker.AddImage(*pixelMap);
252 int64_t packedSize = 0;
253 HILOG_INFO("FinalizePacking start");
254 imagePacker.FinalizePacking(packedSize);
255 if (packedSize == 0) {
256 HILOG_INFO("FinalizePacking error");
257 }
258 }
259
CreateTempPixelMap()260 std::shared_ptr<PixelMap> WallpaperTest::CreateTempPixelMap()
261 {
262 uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
263 InitializationOptions opts = { { 5, 7 }, OHOS::Media::PixelFormat::ARGB_8888 };
264 std::unique_ptr<PixelMap> uniquePixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
265 std::shared_ptr<PixelMap> pixelMap = std::move(uniquePixelMap);
266 return pixelMap;
267 }
268
SubscribeCommonEvent(shared_ptr<WallpaperService> wallpaperService)269 bool WallpaperTest::SubscribeCommonEvent(shared_ptr<WallpaperService> wallpaperService)
270 {
271 subscriber = std::make_shared<WallpaperCommonEventSubscriber>(*wallpaperService);
272 if (subscriber == nullptr) {
273 HILOG_INFO("wallpaperCommonEvent is nullptr");
274 return false;
275 }
276 if (!EventFwk::CommonEventManager::SubscribeCommonEvent(subscriber)) {
277 HILOG_INFO("SubscribeCommonEvent failed");
278 return false;
279 }
280 return true;
281 }
282
TriggerEvent(int32_t userId, const std::string &commonEventSupport)283 void WallpaperTest::TriggerEvent(int32_t userId, const std::string &commonEventSupport)
284 {
285 EventFwk::Want want;
286 want.SetAction(commonEventSupport);
287 int32_t code = userId;
288 std::string data(commonEventSupport);
289 EventFwk::CommonEventData eventData(want, code, data);
290 subscriber->OnReceiveEvent(eventData);
291 }
292
GetUserFilePath(int32_t userId, const char *filePath)293 std::string WallpaperTest::GetUserFilePath(int32_t userId, const char *filePath)
294 {
295 return WALLPAPER_DEFAULT_PATH + std::string("/") + std::to_string(userId) + filePath;
296 }
297
TestCallBack(int32_t num)298 bool WallpaperTest::TestCallBack(int32_t num)
299 {
300 if (num > 0) {
301 return true;
302 }
303 return false;
304 }
305 /********************* ResetWallpaper *********************/
306 /**
307 * @tc.name: Reset001
308 * @tc.desc: Reset wallpaper with wallpaperType[0].
309 * @tc.type: FUNC
310 * @tc.require: issueI5UHRG
311 */
HWTEST_F(WallpaperTest, Reset001, TestSize.Level1)312 HWTEST_F(WallpaperTest, Reset001, TestSize.Level1)
313 {
314 HILOG_INFO("Reset001 begin.");
315 ApiInfo apiInfo{ false, false };
316 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(SYSTYEM, apiInfo);
317 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to reset.";
318 }
319
320 /**
321 * @tc.name: Reset002
322 * @tc.desc: Reset wallpaper with wallpaperType[1].
323 * @tc.type: FUNC
324 * @tc.require: issueI5UHRG
325 */
HWTEST_F(WallpaperTest, Reset002, TestSize.Level1)326 HWTEST_F(WallpaperTest, Reset002, TestSize.Level1)
327 {
328 HILOG_INFO("Reset002 begin.");
329 ApiInfo apiInfo{ false, false };
330 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(LOCKSCREEN, apiInfo);
331 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to reset.";
332 }
333
334 /**
335 * @tc.name: Reset003
336 * @tc.desc: Reset wallpaper with wallpaperType[2] throw parameters error.
337 * @tc.type: FUNC
338 * @tc.require: issueI5UHRG
339 */
HWTEST_F(WallpaperTest, Reset003, TestSize.Level1)340 HWTEST_F(WallpaperTest, Reset003, TestSize.Level1)
341 {
342 HILOG_INFO("Reset003 begin.");
343 ApiInfo apiInfo{ false, false };
344 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(INVALID_WALLPAPER_TYPE, apiInfo);
345 EXPECT_EQ(wallpaperErrorCode, E_PARAMETERS_INVALID) << "Failed to throw error.";
346 }
347
348 /**
349 * @tc.name: Reset004
350 * @tc.desc: Reset wallpaper with wallpaperType[0] after resetting wallpaper[0].
351 * @tc.type: FUNC
352 * @tc.require: issueI5UHRG
353 */
HWTEST_F(WallpaperTest, Reset004, TestSize.Level1)354 HWTEST_F(WallpaperTest, Reset004, TestSize.Level1)
355 {
356 HILOG_INFO("Reset004 begin.");
357 ApiInfo apiInfo{ false, false };
358 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(SYSTYEM, apiInfo);
359 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to reset.";
360
361 /* duplicate reset */
362 wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(SYSTYEM, apiInfo);
363 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to reset.";
364 }
365
366 /**
367 * @tc.name: Reset005
368 * @tc.desc: Reset wallpaper with wallpaperType[1] after resetting wallpaper[1] and check whether Id is same one.
369 * @tc.type: FUNC
370 * @tc.require: issueI5UHRG
371 */
HWTEST_F(WallpaperTest, Reset005, TestSize.Level1)372 HWTEST_F(WallpaperTest, Reset005, TestSize.Level1)
373 {
374 HILOG_INFO("Reset005 begin.");
375 ApiInfo apiInfo{ false, false };
376 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(LOCKSCREEN, apiInfo);
377 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to reset.";
378 int32_t firstId = WallpaperManager::GetInstance().GetWallpaperId(LOCKSCREEN);
379
380 /* duplicate reset */
381 wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(LOCKSCREEN, apiInfo);
382 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to reset.";
383 int32_t secondId = WallpaperManager::GetInstance().GetWallpaperId(LOCKSCREEN);
384 EXPECT_EQ(firstId, secondId) << "Id should be same one.";
385 }
386
387 /**
388 * @tc.name: Reset006
389 * @tc.desc: Reset wallpaper throw permission error
390 * @tc.type: FUNC
391 * @tc.require: issueI5UHRG
392 */
HWTEST_F(WallpaperTest, Reset006, TestSize.Level1)393 HWTEST_F(WallpaperTest, Reset006, TestSize.Level1)
394 {
395 HILOG_INFO("Reset006 begin.");
396 ApiInfo apiInfo{ true, true };
397 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(LOCKSCREEN, apiInfo);
398 EXPECT_EQ(wallpaperErrorCode, E_OK);
399 wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(SYSTYEM, apiInfo);
400 EXPECT_EQ(wallpaperErrorCode, E_OK);
401 }
402 /********************* ResetWallpaper *********************/
403
404 /********************* IsChangePermitted *********************/
405
406 /**
407 * @tc.name: IsChangePermitted001
408 * @tc.desc: check permission.
409 * @tc.type: FUNC
410 * @tc.require:
411 * @tc.author:
412 */
HWTEST_F(WallpaperTest, IsChangePermitted001, TestSize.Level1)413 HWTEST_F(WallpaperTest, IsChangePermitted001, TestSize.Level1)
414 {
415 EXPECT_EQ(WallpaperManager::GetInstance().IsChangePermitted(), true);
416 }
417
418 /********************* IsChangePermitted *********************/
419
420 /********************* IsOperationAllowed *********************/
421
422 /**
423 * @tc.name: IsOperationAllowed001
424 * @tc.desc: check permission.
425 * @tc.type: FUNC
426 * @tc.require:
427 * @tc.author:
428 */
HWTEST_F(WallpaperTest, IsOperationAllowed001, TestSize.Level1)429 HWTEST_F(WallpaperTest, IsOperationAllowed001, TestSize.Level1)
430 {
431 EXPECT_EQ(WallpaperManager::GetInstance().IsOperationAllowed(), true);
432 }
433
434 /********************* IsOperationAllowed *********************/
435
436 /********************* On & Off *********************/
437
438 /**
439 * @tc.name: On001
440 * @tc.desc: set wallpaper and get callback.
441 * @tc.type: FUNC
442 * @tc.require:
443 * @tc.author:
444 */
HWTEST_F(WallpaperTest, On001, TestSize.Level1)445 HWTEST_F(WallpaperTest, On001, TestSize.Level1)
446 {
447 HILOG_INFO("On001 begin");
448 auto listener = std::make_shared<WallpaperEventListenerTestImpl>();
449 auto status = WallpaperManager::GetInstance().On("colorChange", listener);
450 EXPECT_EQ(status, E_OK) << "subscribe wallpaper color change failed.";
451 auto offSubStatus = WallpaperManager::GetInstance().Off("colorChange", listener);
452 EXPECT_EQ(offSubStatus, E_OK) << "unsubscribe wallpaper color change failed.";
453 }
454
455 /**
456 * @tc.name: On002
457 * @tc.desc: set wallpaper and get callback.
458 * @tc.type: FUNC
459 * @tc.require:
460 * @tc.author:
461 */
HWTEST_F(WallpaperTest, On002, TestSize.Level1)462 HWTEST_F(WallpaperTest, On002, TestSize.Level1)
463 {
464 HILOG_INFO("On002 begin");
465 auto listener = std::make_shared<WallpaperEventListenerTestImpl>();
466 auto status = WallpaperManager::GetInstance().On("wallpaperChange", listener);
467 EXPECT_EQ(status, E_OK);
468 auto offSubStatus = WallpaperManager::GetInstance().Off("wallpaperChange", listener);
469 EXPECT_EQ(offSubStatus, E_OK);
470 }
471
472 /********************* On & Off *********************/
473
474 /********************* GetColors *********************/
475 /**
476 * @tc.name: GetColors001
477 * @tc.desc: GetColors with wallpaperType[0].
478 * @tc.type: FUNC
479 * @tc.require:
480 * @tc.author:
481 */
HWTEST_F(WallpaperTest, GetColors001, TestSize.Level0)482 HWTEST_F(WallpaperTest, GetColors001, TestSize.Level0)
483 {
484 HILOG_INFO("GetColors001 begin");
485 std::vector<uint64_t> colors;
486 ApiInfo apiInfo{ false, false };
487 ErrorCode errorCode = WallpaperManager::GetInstance().GetColors(SYSTYEM, apiInfo, colors);
488 EXPECT_EQ(errorCode, E_OK) << "Failed to GetColors.";
489 EXPECT_FALSE(colors.empty());
490 }
491
492 /**
493 * @tc.name: GetColors002
494 * @tc.desc: GetColors with wallpaperType[1].
495 * @tc.type: FUNC
496 * @tc.require:
497 * @tc.author:
498 */
HWTEST_F(WallpaperTest, GetColors002, TestSize.Level0)499 HWTEST_F(WallpaperTest, GetColors002, TestSize.Level0)
500 {
501 HILOG_INFO("GetColors002 begin");
502 std::vector<uint64_t> colors;
503 ApiInfo apiInfo{ false, false };
504 ErrorCode errorCode = WallpaperManager::GetInstance().GetColors(LOCKSCREEN, apiInfo, colors);
505 EXPECT_EQ(errorCode, E_OK) << "Failed to GetColors.";
506 EXPECT_FALSE(colors.empty());
507 }
508
509 /**
510 * @tc.name: GetColors003
511 * @tc.desc: GetColors throw permission error.
512 * @tc.type: FUNC
513 * @tc.require:
514 * @tc.author:
515 */
HWTEST_F(WallpaperTest, GetColors003, TestSize.Level0)516 HWTEST_F(WallpaperTest, GetColors003, TestSize.Level0)
517 {
518 HILOG_INFO("GetColors003 begin");
519 std::vector<uint64_t> colors;
520 ApiInfo apiInfo{ true, true };
521 ErrorCode errorCode = WallpaperManager::GetInstance().GetColors(LOCKSCREEN, apiInfo, colors);
522 EXPECT_EQ(errorCode, E_OK);
523 errorCode = WallpaperManager::GetInstance().GetColors(SYSTYEM, apiInfo, colors);
524 EXPECT_EQ(errorCode, E_OK);
525 }
526 /********************* GetColors *********************/
527
528 /********************* GetId *********************/
529 /**
530 * @tc.name: GetId001
531 * @tc.desc: GetId with wallpaperType[0].
532 * @tc.type: FUNC
533 * @tc.require: issueI65VF1
534 * @tc.author: lvbai
535 */
HWTEST_F(WallpaperTest, GetId001, TestSize.Level0)536 HWTEST_F(WallpaperTest, GetId001, TestSize.Level0)
537 {
538 HILOG_INFO("GetId001 begin");
539 ApiInfo apiInfo{ false, false };
540 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(SYSTYEM, apiInfo);
541 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to ResetWallpaper";
542 int32_t id = WallpaperManager::GetInstance().GetWallpaperId(SYSTYEM);
543 EXPECT_EQ(id, DEFAULT_WALLPAPER_ID) << "Failed to GetId";
544 }
545
546 /**
547 * @tc.name: GetId002
548 * @tc.desc: GetId with wallpaperType[1].
549 * @tc.type: FUNC
550 * @tc.require: issueI65VF1
551 * @tc.author: lvbai
552 */
HWTEST_F(WallpaperTest, GetId002, TestSize.Level0)553 HWTEST_F(WallpaperTest, GetId002, TestSize.Level0)
554 {
555 HILOG_INFO("GetId002 begin");
556 ApiInfo apiInfo{ false, false };
557 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(LOCKSCREEN, apiInfo);
558 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to ResetWallpaper";
559 int32_t id = WallpaperManager::GetInstance().GetWallpaperId(LOCKSCREEN);
560 EXPECT_EQ(id, DEFAULT_WALLPAPER_ID) << "Failed to GetId";
561 }
562
563 /**
564 * @tc.name: GetId003
565 * @tc.desc: GetId with wallpaperType[0] after setWallpaper.
566 * @tc.type: FUNC
567 * @tc.require: issueI65VF1
568 * @tc.author: lvbai
569 */
HWTEST_F(WallpaperTest, GetId003, TestSize.Level0)570 HWTEST_F(WallpaperTest, GetId003, TestSize.Level0)
571 {
572 HILOG_INFO("GetId003 begin");
573 ApiInfo apiInfo{ false, false };
574 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().SetWallpaper(URI, SYSTYEM, apiInfo);
575 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to SetWallpaper";
576 int32_t id = WallpaperManager::GetInstance().GetWallpaperId(SYSTYEM);
577 EXPECT_GT(id, DEFAULT_WALLPAPER_ID) << "Failed to GetId";
578 }
579
580 /**
581 * @tc.name: GetId004
582 * @tc.desc: GetId with wallpaperType[1] after setWallpaper.
583 * @tc.type: FUNC
584 * @tc.require: issueI65VF1
585 * @tc.author: lvbai
586 */
HWTEST_F(WallpaperTest, GetId004, TestSize.Level0)587 HWTEST_F(WallpaperTest, GetId004, TestSize.Level0)
588 {
589 HILOG_INFO("GetId004 begin");
590 ApiInfo apiInfo{ false, false };
591 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().SetWallpaper(URI, LOCKSCREEN, apiInfo);
592 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to SetWallpaper";
593 int32_t id = WallpaperManager::GetInstance().GetWallpaperId(LOCKSCREEN);
594 EXPECT_GT(id, DEFAULT_WALLPAPER_ID) << "Failed to GetId";
595 }
596 /********************* GetId *********************/
597
598 /********************* GetFile *********************/
599 /**
600 * @tc.name: GetFile001
601 * @tc.desc: GetFile with wallpaperType[0].
602 * @tc.type: FUNC
603 * @tc.require: issueI5UHRG
604 */
HWTEST_F(WallpaperTest, GetFile001, TestSize.Level0)605 HWTEST_F(WallpaperTest, GetFile001, TestSize.Level0)
606 {
607 HILOG_INFO("GetFile001 begin");
608 int32_t wallpaperFd = 0;
609 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().GetFile(SYSTYEM, wallpaperFd);
610 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to get File.";
611 }
612
613 /**
614 * @tc.name: GetFile002
615 * @tc.desc: GetFile with wallpaperType[1].
616 * @tc.type: FUNC
617 * @tc.require: issueI5UHRG
618 */
HWTEST_F(WallpaperTest, GetFile002, TestSize.Level0)619 HWTEST_F(WallpaperTest, GetFile002, TestSize.Level0)
620 {
621 HILOG_INFO("GetFile002 begin");
622 int32_t wallpaperFd = 0;
623 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().GetFile(LOCKSCREEN, wallpaperFd);
624 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to get File.";
625 }
626
627 /**
628 * @tc.name: GetFile003
629 * @tc.desc: GetFile with wallpaperType[2] throw parameters error.
630 * @tc.type: FUNC
631 * @tc.require: issueI5UHRG
632 */
HWTEST_F(WallpaperTest, GetFile003, TestSize.Level0)633 HWTEST_F(WallpaperTest, GetFile003, TestSize.Level0)
634 {
635 HILOG_INFO("GetFile003 begin");
636 int32_t wallpaperFd = 0;
637 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().GetFile(INVALID_WALLPAPER_TYPE, wallpaperFd);
638 EXPECT_EQ(wallpaperErrorCode, E_PARAMETERS_INVALID) << "Failed to throw parameters error";
639 }
640
641 /**
642 * @tc.name: GetFile004
643 * @tc.desc: GetFile with wallpaperType[0].
644 * @tc.type: FUNC
645 * @tc.require:
646 * @tc.author:
647 */
HWTEST_F(WallpaperTest, GetFile004, TestSize.Level0)648 HWTEST_F(WallpaperTest, GetFile004, TestSize.Level0)
649 {
650 HILOG_INFO("GetFile001 begin");
651 int32_t wallpaperFd = 0;
652 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().GetFile(SYSTYEM, wallpaperFd);
653 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to get File.";
654 }
655 /********************* GetFile *********************/
656
657 /********************* GetWallpaperMinHeight *********************/
658 /**
659 * @tc.name: getMinHeight001
660 * @tc.desc: GetWallpaperMinHeight .
661 * @tc.type: FUNC
662 * @tc.require:
663 */
HWTEST_F(WallpaperTest, getMinHeight001, TestSize.Level0)664 HWTEST_F(WallpaperTest, getMinHeight001, TestSize.Level0)
665 {
666 HILOG_INFO("getMinHeight001 begin");
667 int32_t height = 0;
668 ApiInfo apiInfo{ false, false };
669 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().GetWallpaperMinHeight(apiInfo, height);
670 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to get WallpaperMinHeight.";
671 EXPECT_GT(height, 0);
672 }
673
674 /**
675 * @tc.name: getMinHeight002
676 * @tc.desc: GetWallpaperMinHeight throw permission error.
677 * @tc.type: FUNC
678 * @tc.require:
679 */
HWTEST_F(WallpaperTest, getMinHeight002, TestSize.Level0)680 HWTEST_F(WallpaperTest, getMinHeight002, TestSize.Level0)
681 {
682 HILOG_INFO("getMinHeight002 begin");
683 int32_t height = 0;
684 ApiInfo apiInfo{ true, true };
685 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().GetWallpaperMinHeight(apiInfo, height);
686 EXPECT_EQ(wallpaperErrorCode, E_OK);
687 }
688 /********************* GetWallpaperMinHeight *********************/
689
690 /********************* GetWallpaperMinWidth *********************/
691 /**
692 * @tc.name: getMinWidth001
693 * @tc.desc: GetWallpaperMinWidth .
694 * @tc.type: FUNC
695 * @tc.require:
696 */
HWTEST_F(WallpaperTest, getMinWidth001, TestSize.Level0)697 HWTEST_F(WallpaperTest, getMinWidth001, TestSize.Level0)
698 {
699 HILOG_INFO("getMinWidth001 begin");
700 int32_t width = 0;
701 ApiInfo apiInfo{ false, false };
702 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().GetWallpaperMinWidth(apiInfo, width);
703 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to get WallpaperMinWidth.";
704 EXPECT_GT(width, 0);
705 }
706
707 /**
708 * @tc.name: getMinWidth002
709 * @tc.desc: GetWallpaperMinWidth throw permission error.
710 * @tc.type: FUNC
711 * @tc.require:
712 */
HWTEST_F(WallpaperTest, getMinWidth002, TestSize.Level0)713 HWTEST_F(WallpaperTest, getMinWidth002, TestSize.Level0)
714 {
715 HILOG_INFO("getMinWidth002 begin");
716 int32_t width = 0;
717 ApiInfo apiInfo{ true, true };
718 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().GetWallpaperMinWidth(apiInfo, width);
719 EXPECT_EQ(wallpaperErrorCode, E_OK);
720 }
721 /********************* GetWallpaperMinWidth *********************/
722
723 /********************* GetPixelMap *********************/
724 /**
725 * @tc.name: GetPixelMap001
726 * @tc.desc: GetPixelMap with wallpaperType[0].
727 * @tc.type: FUNC
728 * @tc.require: issueI5UHRG
729 */
HWTEST_F(WallpaperTest, GetPixelMap001, TestSize.Level0)730 HWTEST_F(WallpaperTest, GetPixelMap001, TestSize.Level0)
731 {
732 HILOG_INFO("GetPixelMap001 begin");
733 std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
734 ApiInfo apiInfo{ false, false };
735 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().GetPixelMap(SYSTYEM, apiInfo, pixelMap);
736 EXPECT_EQ(wallpaperErrorCode, E_OK);
737 }
738
739 /**
740 * @tc.name: GetPixelMap002
741 * @tc.desc: GetPixelMap with wallpaperType[1].
742 * @tc.type: FUNC
743 * @tc.require: issueI5UHRG
744 */
HWTEST_F(WallpaperTest, GetPixelMap002, TestSize.Level0)745 HWTEST_F(WallpaperTest, GetPixelMap002, TestSize.Level0)
746 {
747 HILOG_INFO("GetPixelMap002 begin");
748 std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
749 ApiInfo apiInfo{ false, false };
750 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().GetPixelMap(LOCKSCREEN, apiInfo, pixelMap);
751 EXPECT_EQ(wallpaperErrorCode, E_OK);
752 }
753
754 /**
755 * @tc.name: GetPixelMap003
756 * @tc.desc: GetPixelMap throw permission error.
757 * @tc.type: FUNC
758 * @tc.require:
759 */
HWTEST_F(WallpaperTest, GetPixelMap003, TestSize.Level0)760 HWTEST_F(WallpaperTest, GetPixelMap003, TestSize.Level0)
761 {
762 HILOG_INFO("GetPixelMap003 begin");
763 std::shared_ptr<OHOS::Media::PixelMap> pixelMap;
764 ApiInfo apiInfo{ true, true };
765 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().GetPixelMap(LOCKSCREEN, apiInfo, pixelMap);
766 EXPECT_EQ(wallpaperErrorCode, E_OK);
767 wallpaperErrorCode = WallpaperManager::GetInstance().GetPixelMap(SYSTYEM, apiInfo, pixelMap);
768 EXPECT_EQ(wallpaperErrorCode, E_OK);
769 }
770 /********************* GetPixelMap *********************/
771
772 /********************* SetWallpaperByMap *********************/
773 /**
774 * @tc.name: SetWallpaperByMap001
775 * @tc.desc: SetWallpaperByMap with wallpaperType[0].
776 * @tc.type: FUNC
777 * @tc.require: issueI5UHRG
778 */
HWTEST_F(WallpaperTest, SetWallpaperByMap001, TestSize.Level0)779 HWTEST_F(WallpaperTest, SetWallpaperByMap001, TestSize.Level0)
780 {
781 HILOG_INFO("SetWallpaperByMap001 begin");
782 std::shared_ptr<PixelMap> pixelMap = WallpaperTest::CreateTempPixelMap();
783 ApiInfo apiInfo{ false, false };
784 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().SetWallpaper(pixelMap, SYSTYEM, apiInfo);
785 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to set SYSTYEM PixelMap.";
786 }
787
788 /**
789 * @tc.name: SetWallpaperByMap002
790 * @tc.desc: SetWallpaperByMap with wallpaperType[1].
791 * @tc.type: FUNC
792 * @tc.require: issueI5UHRG
793 */
HWTEST_F(WallpaperTest, SetWallpaperByMap002, TestSize.Level0)794 HWTEST_F(WallpaperTest, SetWallpaperByMap002, TestSize.Level0)
795 {
796 HILOG_INFO("SetWallpaperByMap002 begin");
797 std::shared_ptr<PixelMap> pixelMap = WallpaperTest::CreateTempPixelMap();
798 ApiInfo apiInfo{ false, false };
799 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().SetWallpaper(pixelMap, LOCKSCREEN, apiInfo);
800 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to set LOCKSCREEN PixelMap.";
801 }
802
803 /**
804 * @tc.name: SetWallpaperByMap003
805 * @tc.desc: SetWallpaperByMap with wallpaperType[2] throw parameters error.
806 * @tc.type: FUNC
807 * @tc.require: issueI5UHRG
808 */
HWTEST_F(WallpaperTest, SetWallpaperByMap003, TestSize.Level0)809 HWTEST_F(WallpaperTest, SetWallpaperByMap003, TestSize.Level0)
810 {
811 HILOG_INFO("SetWallpaperByMap003 begin");
812 std::shared_ptr<PixelMap> pixelMap = WallpaperTest::CreateTempPixelMap();
813 ApiInfo apiInfo{ false, false };
814 ErrorCode wallpaperErrorCode =
815 WallpaperManager::GetInstance().SetWallpaper(pixelMap, INVALID_WALLPAPER_TYPE, apiInfo);
816 EXPECT_EQ(wallpaperErrorCode, E_PARAMETERS_INVALID) << "Failed to throw parameters error";
817 }
818 /********************* SetWallpaperByMap *********************/
819
820 /********************* SetWallpaperByUri *********************/
821 /**
822 * @tc.name: SetWallpaperByUri001
823 * @tc.desc: SetWallpaperByUri with wallpaperType[0] .
824 * @tc.type: FUNC
825 * @tc.require: issueI5UHRG
826 */
HWTEST_F(WallpaperTest, SetWallpaperByUri001, TestSize.Level0)827 HWTEST_F(WallpaperTest, SetWallpaperByUri001, TestSize.Level0)
828 {
829 HILOG_INFO("SetWallpaperByUri001 begin");
830 ApiInfo apiInfo{ false, false };
831 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().SetWallpaper(URI, SYSTYEM, apiInfo);
832 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to set SYSTYEM.";
833 }
834
835 /**
836 * @tc.name: SetWallpaperByUri002
837 * @tc.desc: SetWallpaperByUri with wallpaperType[1].
838 * @tc.type: FUNC
839 * @tc.require: issueI5UHRG
840 */
HWTEST_F(WallpaperTest, SetWallpaperByUri002, TestSize.Level0)841 HWTEST_F(WallpaperTest, SetWallpaperByUri002, TestSize.Level0)
842 {
843 HILOG_INFO("SetWallpaperByUri002 begin");
844 ApiInfo apiInfo{ false, false };
845 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().SetWallpaper(URI, LOCKSCREEN, apiInfo);
846 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to set LOCKSCREEN.";
847 }
848
849 /**
850 * @tc.name: SetWallpaperByUri003
851 * @tc.desc: SetWallpaperByUri with wallpaperType[2] throw parameters error.
852 * @tc.type: FUNC
853 * @tc.require: issueI5UHRG
854 */
HWTEST_F(WallpaperTest, SetWallpaperByUri003, TestSize.Level0)855 HWTEST_F(WallpaperTest, SetWallpaperByUri003, TestSize.Level0)
856 {
857 HILOG_INFO("SetWallpaperByUri003 begin");
858 ApiInfo apiInfo{ false, false };
859 ErrorCode wallpaperErrorCode =
860 WallpaperManager::GetInstance().SetWallpaper(URI, INVALID_WALLPAPER_TYPE, apiInfo);
861 EXPECT_EQ(wallpaperErrorCode, E_PARAMETERS_INVALID) << "Failed to throw error";
862 }
863
864 /**
865 * @tc.name: SetWallpaperByUri004
866 * @tc.desc: SetWallpaperByUri with error uri.
867 * @tc.type: FUNC
868 * @tc.require: issueI5UHRG
869 */
HWTEST_F(WallpaperTest, SetWallpaperByUri004, TestSize.Level0)870 HWTEST_F(WallpaperTest, SetWallpaperByUri004, TestSize.Level0)
871 {
872 HILOG_INFO("SetWallpaperByUri004 begin");
873 ApiInfo apiInfo{ false, false };
874 ErrorCode wallpaperErrorCode =
875 WallpaperManager::GetInstance().SetWallpaper("/data/test/theme/wallpaper/errorURI", LOCKSCREEN, apiInfo);
876 EXPECT_NE(wallpaperErrorCode, E_OK) << "Failed to throw error";
877 }
878
879 /**
880 * @tc.name: SetWallpaperByUri005
881 * @tc.desc: SetWallpaperByUri with unsafe uri.
882 * @tc.type: FUNC
883 * @tc.require: issueI647HI
884 */
HWTEST_F(WallpaperTest, SetWallpaperByUri005, TestSize.Level0)885 HWTEST_F(WallpaperTest, SetWallpaperByUri005, TestSize.Level0)
886 {
887 HILOG_INFO("SetWallpaperByUri005 begin");
888 ApiInfo apiInfo{ false, false };
889 ErrorCode wallpaperErrorCode =
890 WallpaperManager::GetInstance().SetWallpaper("../data/test/theme/wallpaper/errorURI", LOCKSCREEN, apiInfo);
891 EXPECT_EQ(wallpaperErrorCode, E_FILE_ERROR) << "Failed to return error";
892 }
893
894 /**
895 * @tc.name: SetWallpaperByUri006
896 * @tc.desc: SetWallpaperByUri throw permission error.
897 * @tc.type: FUNC
898 * @tc.require:
899 */
HWTEST_F(WallpaperTest, SetWallpaperByUri006, TestSize.Level0)900 HWTEST_F(WallpaperTest, SetWallpaperByUri006, TestSize.Level0)
901 {
902 HILOG_INFO("SetWallpaperByUri006 begin");
903 ApiInfo apiInfo{ true, true };
904 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().SetWallpaper(URI, LOCKSCREEN, apiInfo);
905 EXPECT_EQ(wallpaperErrorCode, E_OK);
906 wallpaperErrorCode = WallpaperManager::GetInstance().SetWallpaper(URI, SYSTYEM, apiInfo);
907 EXPECT_EQ(wallpaperErrorCode, E_OK);
908 }
909
910 /**
911 * @tc.name: SetWallpaperByUri007
912 * @tc.desc: SetWallpaperByUri throw permission error.
913 * @tc.type: FUNC
914 * @tc.require:
915 */
HWTEST_F(WallpaperTest, SetWallpaperByUri007, TestSize.Level0)916 HWTEST_F(WallpaperTest, SetWallpaperByUri007, TestSize.Level0)
917 {
918 HILOG_INFO("SetWallpaperByUri007 begin");
919 ApiInfo apiInfo{ true, true };
920 WallpaperManager::GetInstance().ResetWallpaper(LOCKSCREEN, apiInfo);
921 std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
922 int32_t userId = wallpaperService->QueryActiveUserId();
923 HILOG_INFO("guochao userId:%{public}d", userId);
924 bool ret = FileDeal::IsFileExist(WallpaperTest::GetUserFilePath(userId, LOCKSCREEN_FILE));
925 EXPECT_EQ(ret, false) << "Failed to reset.";
926 WallpaperManager::GetInstance().SetWallpaper(URI, LOCKSCREEN, apiInfo);
927 ret = FileDeal::IsFileExist(WallpaperTest::GetUserFilePath(userId, LOCKSCREEN_FILE));
928 EXPECT_EQ(ret, true);
929 WallpaperManager::GetInstance().ResetWallpaper(LOCKSCREEN, apiInfo);
930 }
931
932 /********************* SetWallpaperByUri *********************/
933
934 /********************* FILE_DEAL *********************/
935 /**
936 * @tc.name: FILE_DEAL001
937 * @tc.desc: File operation-related interfaces
938 * @tc.type: FUNC
939 * @tc.require:
940 * @tc.author:
941 */
HWTEST_F(WallpaperTest, FILE_DEAL001, TestSize.Level0)942 HWTEST_F(WallpaperTest, FILE_DEAL001, TestSize.Level0)
943 {
944 HILOG_INFO("FILE_DEAL001 begin");
945 FileDeal fileOperation;
946 bool isExist = fileOperation.Mkdir("/data/test/theme/wallpaper/");
947 EXPECT_EQ(isExist, true);
948 isExist = fileOperation.Mkdir("/data/test/theme/errorURI/");
949 EXPECT_EQ(isExist, true);
950 isExist = fileOperation.IsFileExist(URI);
951 EXPECT_EQ(isExist, true);
952 isExist = fileOperation.IsFileExist("/data/test/theme/wallpaper/errorURI");
953 EXPECT_EQ(isExist, false);
954 }
955 /********************* FILE_DEAL *********************/
956
957 /**
958 * @tc.name: SetWallpaper001
959 * @tc.desc: SetWallpaper with error length
960 * @tc.type: FUNC
961 * @tc.require: issueI6AW6M
962 * @tc.author: weishaoxiong
963 */
HWTEST_F(WallpaperTest, SetWallpaper001, TestSize.Level0)964 HWTEST_F(WallpaperTest, SetWallpaper001, TestSize.Level0)
965 {
966 HILOG_INFO("SetWallpaper001 begin");
967 std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
968 ErrorCode wallpaperErrorCode = wallpaperService->SetWallpaper(0, 0, -1);
969 EXPECT_EQ(wallpaperErrorCode, E_PARAMETERS_INVALID) << "Failed to throw error";
970 wallpaperErrorCode = wallpaperService->SetWallpaper(0, 0, FOO_MAX_LEN);
971
972 EXPECT_EQ(wallpaperErrorCode, E_PARAMETERS_INVALID) << "Failed to throw error";
973 }
974
975 /********************* USER_DEAL *********************/
976 /**
977 * @tc.name: AddUsersDeal001
978 * @tc.desc: Create a user directory after the user is added
979 * @tc.type: FUNC
980 * @tc.require: issueI6DWHR
981 */
HWTEST_F(WallpaperTest, AddUsersDeal001, TestSize.Level0)982 HWTEST_F(WallpaperTest, AddUsersDeal001, TestSize.Level0)
983 {
984 std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
985 bool ret = WallpaperTest::SubscribeCommonEvent(wallpaperService);
986 ASSERT_EQ(ret, true);
987 std::string commonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED;
988 WallpaperTest::TriggerEvent(TEST_USERID, commonEvent);
989 ret = FileDeal::IsDirExist(WallpaperTest::GetUserFilePath(TEST_USERID, SYSTEM_DIR));
990 EXPECT_EQ(ret, true);
991 ret = FileDeal::IsDirExist(WallpaperTest::GetUserFilePath(TEST_USERID, LOCKSCREEN_DIR));
992 EXPECT_EQ(ret, true);
993 std::string userDir = WALLPAPER_DEFAULT_PATH + std::string("/") + std::to_string(TEST_USERID);
994 if (!OHOS::ForceRemoveDirectory(userDir)) {
995 HILOG_ERROR("Force remove user directory path failed, errno %{public}d.", errno);
996 }
997 }
998
999 /**
1000 * @tc.name: RemovedUserDeal001
1001 * @tc.desc: delete a user directory after the user is removed
1002 * @tc.type: FUNC
1003 * @tc.require: issueI6DWHR
1004 */
HWTEST_F(WallpaperTest, RemovedUserDeal001, TestSize.Level0)1005 HWTEST_F(WallpaperTest, RemovedUserDeal001, TestSize.Level0)
1006 {
1007 std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
1008 ASSERT_EQ(WallpaperTest::SubscribeCommonEvent(wallpaperService), true);
1009 std::string commonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED;
1010 WallpaperTest::TriggerEvent(TEST_USERID, commonEvent);
1011 std::string userDir = WALLPAPER_DEFAULT_PATH + std::string("/") + std::to_string(TEST_USERID);
1012 ASSERT_EQ(FileDeal::IsDirExist(userDir), true);
1013
1014 commonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED;
1015 WallpaperTest::TriggerEvent(TEST_USERID, commonEvent);
1016 EXPECT_EQ(FileDeal::IsDirExist(userDir), false);
1017 if (!OHOS::ForceRemoveDirectory(userDir)) {
1018 HILOG_ERROR("Force remove user directory path failed, errno %{public}d.", errno);
1019 }
1020 }
1021
1022 /**
1023 * @tc.name: SwitchedUserIdDeal001
1024 * @tc.desc: The wallpaper has changed after switched user
1025 * @tc.type: FUNC
1026 * @tc.require: issueI6DWHR
1027 */
HWTEST_F(WallpaperTest, SwitchedUserIdDeal001, TestSize.Level0)1028 HWTEST_F(WallpaperTest, SwitchedUserIdDeal001, TestSize.Level0)
1029 {
1030 std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
1031 wallpaperService->InitServiceHandler();
1032 ASSERT_EQ(WallpaperTest::SubscribeCommonEvent(wallpaperService), true);
1033 ApiInfo apiInfo{ false, false };
1034 std::vector<int32_t> ids;
1035 AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
1036 int32_t beforeUserId = ids[0];
1037 std::string addCommonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED;
1038 WallpaperTest::TriggerEvent(TEST_USERID, addCommonEvent);
1039 std::string userDir = WALLPAPER_DEFAULT_PATH + std::string("/") + std::to_string(TEST_USERID);
1040 ASSERT_EQ(FileDeal::IsDirExist(userDir), true);
1041 ErrorCode wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(LOCKSCREEN, apiInfo);
1042 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to reset lockscreen wallpaper";
1043
1044 std::vector<uint64_t> oldLockscreenColor;
1045 wallpaperErrorCode = WallpaperManager::GetInstance().GetColors(LOCKSCREEN, apiInfo, oldLockscreenColor);
1046 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to GetColors";
1047 wallpaperErrorCode = WallpaperManager::GetInstance().ResetWallpaper(SYSTYEM, apiInfo);
1048 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to reset system wallpaper";
1049 std::vector<uint64_t> oldSystemColor;
1050 wallpaperErrorCode = WallpaperManager::GetInstance().GetColors(SYSTYEM, apiInfo, oldSystemColor);
1051 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to GetColors";
1052
1053 std::string switchCommonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED;
1054 WallpaperTest::TriggerEvent(TEST_USERID, switchCommonEvent);
1055 wallpaperErrorCode = WallpaperManager::GetInstance().SetWallpaper(URI, LOCKSCREEN, apiInfo);
1056 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to set lockscreen wallpaper";
1057 std::vector<uint64_t> newLockscreenColor;
1058 wallpaperErrorCode = WallpaperManager::GetInstance().GetColors(LOCKSCREEN, apiInfo, newLockscreenColor);
1059 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to GetColors";
1060 wallpaperErrorCode = WallpaperManager::GetInstance().SetWallpaper(URI, SYSTYEM, apiInfo);
1061 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to set system wallpaper";
1062 std::vector<uint64_t> newSystemColor;
1063 wallpaperErrorCode = WallpaperManager::GetInstance().GetColors(SYSTYEM, apiInfo, newSystemColor);
1064 EXPECT_EQ(wallpaperErrorCode, E_OK) << "Failed to GetColors";
1065 EXPECT_NE(oldLockscreenColor, newLockscreenColor);
1066 EXPECT_NE(oldSystemColor, newSystemColor);
1067
1068 WallpaperTest::TriggerEvent(beforeUserId, switchCommonEvent);
1069 std::string removeCommonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED;
1070 WallpaperTest::TriggerEvent(TEST_USERID, removeCommonEvent);
1071 EXPECT_EQ(FileDeal::IsDirExist(userDir), false);
1072 if (!OHOS::ForceRemoveDirectory(userDir)) {
1073 HILOG_ERROR("Force remove user directory path failed, errno %{public}d.", errno);
1074 }
1075 }
1076
1077 /**
1078 * @tc.name: InvalidUserIdDeal001
1079 * @tc.desc: Invalid user id deal
1080 * @tc.type: FUNC
1081 * @tc.require: issueI6DWHR
1082 */
HWTEST_F(WallpaperTest, InvalidUserIdDeal001, TestSize.Level0)1083 HWTEST_F(WallpaperTest, InvalidUserIdDeal001, TestSize.Level0)
1084 {
1085 std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
1086 wallpaperService->InitServiceHandler();
1087 ASSERT_EQ(WallpaperTest::SubscribeCommonEvent(wallpaperService), true);
1088 std::string commonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_USER_ADDED;
1089 WallpaperTest::TriggerEvent(INVALID_USERID, commonEvent);
1090 std::string userDir = WALLPAPER_DEFAULT_PATH + std::string("/") + std::to_string(INVALID_USERID);
1091 EXPECT_EQ(FileDeal::IsDirExist(userDir), false);
1092 FileDeal::Mkdir(userDir);
1093 commonEvent = EventFwk::CommonEventSupport::COMMON_EVENT_USER_REMOVED;
1094 WallpaperTest::TriggerEvent(INVALID_USERID, commonEvent);
1095 EXPECT_EQ(FileDeal::IsDirExist(userDir), true);
1096 if (!OHOS::ForceRemoveDirectory(userDir)) {
1097 HILOG_ERROR("Force remove user directory path failed, errno %{public}d.", errno);
1098 }
1099 }
1100 /********************* USER_DEAL *********************/
1101
1102 /********************* SetVideo *********************/
1103 /**
1104 * @tc.name: SetVideo001
1105 * @tc.desc: SetVideo input error fileType
1106 * @tc.type: FUNC
1107 * @tc.require: issueI6R07J
1108 */
HWTEST_F(WallpaperTest, SetVideo001, TestSize.Level0)1109 HWTEST_F(WallpaperTest, SetVideo001, TestSize.Level0)
1110 {
1111 HILOG_INFO("SetVideo001 begin");
1112 ErrorCode ret = WallpaperManager::GetInstance().SetVideo(URI_30FPS_3S_MOV, SYSTYEM);
1113 EXPECT_EQ(ret, E_PARAMETERS_INVALID);
1114 }
1115
1116 /**
1117 * @tc.name: SetVideo002
1118 * @tc.desc: SetVideo input error uri
1119 * @tc.type: FUNC
1120 * @tc.require: issueI6R07J
1121 */
HWTEST_F(WallpaperTest, SetVideo002, TestSize.Level0)1122 HWTEST_F(WallpaperTest, SetVideo002, TestSize.Level0)
1123 {
1124 HILOG_INFO("SetVideo002 begin");
1125 std::string errUri = "errorPath/zm_30fps_4s.mp4";
1126 ErrorCode ret = WallpaperManager::GetInstance().SetVideo(errUri, SYSTYEM);
1127 EXPECT_EQ(ret, E_FILE_ERROR);
1128 }
1129
1130 /**
1131 * @tc.name: SetVideo003
1132 * @tc.desc: SetVideo input correct parameter
1133 * @tc.type: FUNC
1134 * @tc.require: issueI6R07J
1135 */
HWTEST_F(WallpaperTest, SetVideo003, TestSize.Level0)1136 HWTEST_F(WallpaperTest, SetVideo003, TestSize.Level0)
1137 {
1138 HILOG_INFO("SetVideo003 begin");
1139 ErrorCode ret = WallpaperManager::GetInstance().SetVideo(URI_30FPS_3S_MP4, SYSTYEM);
1140 EXPECT_EQ(ret, E_OK);
1141 }
1142
1143 /**
1144 * @tc.name: SetVideo004
1145 * @tc.desc: SetVideo input error duration
1146 * @tc.type: FUNC
1147 * @tc.require: issueI6R07J
1148 */
HWTEST_F(WallpaperTest, SetVideo004, TestSize.Level0)1149 HWTEST_F(WallpaperTest, SetVideo004, TestSize.Level0)
1150 {
1151 HILOG_INFO("SetVideo004 begin");
1152 ErrorCode ret = WallpaperManager::GetInstance().SetVideo(URI_15FPS_7S_MP4, SYSTYEM);
1153 EXPECT_EQ(ret, E_PARAMETERS_INVALID);
1154 }
1155
1156 /**
1157 * @tc.name: SetVideo005
1158 * @tc.desc: SetVideo input error fileType
1159 * @tc.type: FUNC
1160 * @tc.require: issueI6R07J
1161 */
HWTEST_F(WallpaperTest, SetVideo005, TestSize.Level0)1162 HWTEST_F(WallpaperTest, SetVideo005, TestSize.Level0)
1163 {
1164 HILOG_INFO("SetVideo005 begin");
1165 ErrorCode ret = WallpaperManager::GetInstance().SetVideo(URI_30FPS_3S_MOV, LOCKSCREEN);
1166 EXPECT_EQ(ret, E_PARAMETERS_INVALID);
1167 }
1168
1169 /**
1170 * @tc.name: SetVideo006
1171 * @tc.desc: SetVideo input error uri
1172 * @tc.type: FUNC
1173 * @tc.require: issueI6R07J
1174 */
HWTEST_F(WallpaperTest, SetVideo006, TestSize.Level0)1175 HWTEST_F(WallpaperTest, SetVideo006, TestSize.Level0)
1176 {
1177 HILOG_INFO("SetVideo006 begin");
1178 std::string errUri = "errorPath/zm_30fps_4s.mp4";
1179 ErrorCode ret = WallpaperManager::GetInstance().SetVideo(errUri, LOCKSCREEN);
1180 EXPECT_EQ(ret, E_FILE_ERROR);
1181 }
1182
1183 /**
1184 * @tc.name: SetVideo007
1185 * @tc.desc: SetVideo input correct parameter
1186 * @tc.type: FUNC
1187 * @tc.require: issueI6R07J
1188 */
HWTEST_F(WallpaperTest, SetVideo007, TestSize.Level0)1189 HWTEST_F(WallpaperTest, SetVideo007, TestSize.Level0)
1190 {
1191 HILOG_INFO("SetVideo007 begin");
1192 ErrorCode ret = WallpaperManager::GetInstance().SetVideo(URI_30FPS_3S_MP4, LOCKSCREEN);
1193 EXPECT_EQ(ret, E_OK);
1194 }
1195
1196 /**
1197 * @tc.name: SetVideo008
1198 * @tc.desc: SetVideo input error duration
1199 * @tc.type: FUNC
1200 * @tc.require: issueI6R07J
1201 */
HWTEST_F(WallpaperTest, SetVideo008, TestSize.Level0)1202 HWTEST_F(WallpaperTest, SetVideo008, TestSize.Level0)
1203 {
1204 HILOG_INFO("SetVideo008 begin");
1205 ErrorCode ret = WallpaperManager::GetInstance().SetVideo(URI_15FPS_7S_MP4, LOCKSCREEN);
1206 EXPECT_EQ(ret, E_PARAMETERS_INVALID);
1207 }
1208 /********************* SetVideo *********************/
1209
1210 /**
1211 * @tc.name: SetCustomWallpaper001
1212 * @tc.desc: Set a custom wallpaper in the Sceneborad scene
1213 * @tc.type: FUNC
1214 * @tc.require: issueI7AAMU
1215 */
HWTEST_F(WallpaperTest, SetCustomWallpaper001, TestSize.Level0)1216 HWTEST_F(WallpaperTest, SetCustomWallpaper001, TestSize.Level0)
1217 {
1218 HILOG_INFO("SetCustomWallpaper001 begin");
1219 ErrorCode testErrorCode = E_OK;
1220 if (!Rosen::SceneBoardJudgement::IsSceneBoardEnabled()) {
1221 testErrorCode = E_NO_PERMISSION;
1222 }
1223 ErrorCode ret = WallpaperManager::GetInstance().SetCustomWallpaper(URI_ZIP, SYSTYEM);
1224 EXPECT_EQ(ret, testErrorCode);
1225 ret = WallpaperManager::GetInstance().SetCustomWallpaper(URI_ZIP, LOCKSCREEN);
1226 EXPECT_EQ(ret, testErrorCode);
1227 }
1228
1229 /********************* SendEvent *********************/
1230
1231 /**
1232 * @tc.name: SendEvent001
1233 * @tc.desc: SetVideo input error fileType
1234 * @tc.type: FUNC
1235 * @tc.require: issueI6R07J
1236 */
HWTEST_F(WallpaperTest, SendEvent001, TestSize.Level0)1237 HWTEST_F(WallpaperTest, SendEvent001, TestSize.Level0)
1238 {
1239 HILOG_INFO("SendEvent001 begin");
1240 std::string errEventType = "SHOW_ERROREVENTTYPE";
1241 ErrorCode ret = WallpaperManager::GetInstance().SendEvent("SHOW_ERROREVENTTYPE");
1242 EXPECT_EQ(ret, E_PARAMETERS_INVALID);
1243 }
1244
1245 /**
1246 * @tc.name: SendEvent002
1247 * @tc.desc: SetVideo input error fileType
1248 * @tc.type: FUNC
1249 * @tc.require: issueI6R07J
1250 */
HWTEST_F(WallpaperTest, SendEvent002, TestSize.Level0)1251 HWTEST_F(WallpaperTest, SendEvent002, TestSize.Level0)
1252 {
1253 HILOG_INFO("SendEvent002 begin");
1254 ErrorCode ret = WallpaperManager::GetInstance().SendEvent("SHOW_SYSTEMSCREEN");
1255 EXPECT_EQ(ret, E_OK);
1256 }
1257
1258 /**
1259 * @tc.name: SendEvent003
1260 * @tc.desc: SetVideo input error fileType
1261 * @tc.type: FUNC
1262 * @tc.require: issueI6R07J
1263 */
HWTEST_F(WallpaperTest, SendEvent003, TestSize.Level0)1264 HWTEST_F(WallpaperTest, SendEvent003, TestSize.Level0)
1265 {
1266 HILOG_INFO("SendEvent003 begin");
1267 ErrorCode ret = WallpaperManager::GetInstance().SendEvent("SHOW_LOCKSCREEN");
1268 EXPECT_EQ(ret, E_OK);
1269 }
1270
1271 /**
1272 * @tc.name: WallpaperTest_001
1273 * @tc.desc: Test Onstop and OnStart.
1274 * @tc.type: FUNC
1275 * @tc.require: issueI7OUB6
1276 */
HWTEST_F(WallpaperTest, WallpaperTest_001, TestSize.Level0)1277 HWTEST_F(WallpaperTest, WallpaperTest_001, TestSize.Level0)
1278 {
1279 HILOG_INFO("Test Onstop");
1280 std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
1281 wallpaperService->state_ = WallpaperService::ServiceRunningState::STATE_RUNNING;
1282 wallpaperService->OnStop();
1283 EXPECT_EQ(wallpaperService->state_, WallpaperService::ServiceRunningState::STATE_NOT_START);
1284 }
1285
1286 /**
1287 * @tc.name: GetPictureFileName_001
1288 * @tc.desc: Load userIds that are not found in the Map.
1289 * @tc.type: FUNC
1290 * @tc.require: issueI90IUI
1291 */
HWTEST_F(WallpaperTest, GetPictureFileName_001, TestSize.Level0)1292 HWTEST_F(WallpaperTest, GetPictureFileName_001, TestSize.Level0)
1293 {
1294 HILOG_INFO("GetPictureFileName_001 begin");
1295 std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
1296 std::string fileName;
1297 wallpaperService->GetPictureFileName(TEST_USERID1, WALLPAPER_SYSTEM, fileName);
1298 auto wallpaperDefault = fileName.find(WALLPAPER_DEFAULT);
1299 auto homeWallpaper = fileName.find(HOME_WALLPAPER);
1300 EXPECT_EQ((wallpaperDefault != string::npos) || (homeWallpaper != string::npos), true);
1301 wallpaperService->SetWallpaperBackupData(TEST_USERID1, PICTURE, URI, WALLPAPER_SYSTEM);
1302 wallpaperService->GetPictureFileName(TEST_USERID1, WALLPAPER_SYSTEM, fileName);
1303 auto pos = fileName.find(to_string(TEST_USERID1));
1304 EXPECT_NE(pos, string::npos);
1305 wallpaperService->OnRemovedUser(TEST_USERID1);
1306 }
1307
1308 /**
1309 * @tc.name: RegisterWallpaperCallback_001
1310 * @tc.desc: Test RegisterWallpaperCallback
1311 * @tc.type: FUNC
1312 * @tc.require: issueIA87VK
1313 */
HWTEST_F(WallpaperTest, RegisterWallpaperCallback_001, TestSize.Level0)1314 HWTEST_F(WallpaperTest, RegisterWallpaperCallback_001, TestSize.Level0)
1315 {
1316 HILOG_INFO("RegisterWallpaperCallback_001 begin");
1317 JScallback callback = &WallpaperTest::TestCallBack;
1318 bool res = WallpaperManager::GetInstance().RegisterWallpaperCallback(callback);
1319 EXPECT_EQ(res, true);
1320 }
1321
1322 /**
1323 * @tc.name: RegisterWallpaperListener_001
1324 * @tc.desc: Test RegisterWallpaperListener
1325 * @tc.type: FUNC
1326 * @tc.require: issueIA87VK
1327 */
HWTEST_F(WallpaperTest, RegisterWallpaperListener_001, TestSize.Level0)1328 HWTEST_F(WallpaperTest, RegisterWallpaperListener_001, TestSize.Level0)
1329 {
1330 HILOG_INFO("RegisterWallpaperListener_001 begin");
1331 bool res = WallpaperManager::GetInstance().RegisterWallpaperListener();
1332 EXPECT_EQ(res, true);
1333 }
1334 } // namespace WallpaperMgrService
1335 } // namespace OHOS