• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2025 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 #include <ctime>
16 
17 #include "hilog_wrapper.h"
18 #include "wallpaper_cjson_mock.h"
19 #include "wallpaper_service.h"
20 
21 namespace OHOS {
22 namespace WallpaperMgrService {
23 
24 constexpr const char *WALLPAPER_MOCK_CJSON_NAME = "wallpaper_mock_cjson_name";
25 constexpr const char *WALLPAPER = "wallpaper";
26 
27 using namespace testing::ext;
28 using namespace testing;
29 using namespace OHOS::MiscServices;
30 using namespace OHOS::Security::AccessToken;
31 
32 class WallpaperTestMock : public testing::Test {
33 public:
34     static void SetUpTestCase(void);
35     static void TearDownTestCase(void);
36     void SetUp();
37     void TearDown();
38     static inline std::shared_ptr<WallpaperMgrCjsonMock> wallpaperMock = nullptr;
39 };
40 const std::string VALID_SCHEMA_STRICT_DEFINE = "{\"SCHEMA_VERSION\":\"1.0\","
41                                                "\"SCHEMA_MODE\":\"STRICT\","
42                                                "\"SCHEMA_SKIPSIZE\":0,"
43                                                "\"SCHEMA_DEFINE\":{"
44                                                "\"age\":\"INTEGER, NOT NULL\""
45                                                "},"
46                                                "\"SCHEMA_INDEXES\":[\"$.age\"]}";
47 
SetUpTestCase(void)48 void WallpaperTestMock::SetUpTestCase(void)
49 {
50     HILOG_INFO("SetUpTestCase");
51     HILOG_INFO("SetUpTestCase end");
52 }
53 
TearDownTestCase(void)54 void WallpaperTestMock::TearDownTestCase(void)
55 {
56     HILOG_INFO("TearDownTestCase");
57     HILOG_INFO("TearDownTestCase end");
58 }
59 
SetUp()60 void WallpaperTestMock::SetUp()
61 {
62     wallpaperMock = std::make_shared<WallpaperMgrCjsonMock>();
63     OHOS::WallpaperMgrCjson::wallpaperMgrCjson = wallpaperMock;
64 }
65 
TearDown()66 void WallpaperTestMock::TearDown()
67 {
68     wallpaperMock = nullptr;
69 }
70 
71 /*********************   Wallpaper_SaveWallpaperState_mock    *********************/
72 /**
73 * @tc.name: SaveWallpaperState
74 * @tc.desc: save wallpaper state err test, cJSON_AddNumberToObject return nullptr.
75 * @tc.type: FUNC
76 * @tc.require:
77 */
78 HWTEST_F(WallpaperTestMock, SaveWallpaperState001, TestSize.Level0)
79 {
80     HILOG_INFO("SaveWallpaperState001 begin");
81     std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
82     wallpaperService->LoadWallpaperState();
83     int32_t userId = wallpaperService->QueryActiveUserId();
84     cJSON *image = cJSON_CreateObject();
85     cJSON_AddStringToObject(image, "wallpaper", WALLPAPER_MOCK_CJSON_NAME);
86     EXPECT_CALL(*wallpaperMock, cJSON_AddNumberToObject(_, _, _)).WillOnce(Return(nullptr));
87     auto ret =
88         wallpaperService->SaveWallpaperState(userId, WallpaperType::WALLPAPER_SYSTEM, WallpaperResourceType::DEFAULT);
89     if (image != nullptr) {
90         cJSON_Delete(image);
91     }
92     EXPECT_EQ(ret, false) << "Failed to SaveWallpaperState";
93 }
94 
95 /**
96 * @tc.name: SaveWallpaperState
97 * @tc.desc: save wallpaper state err test, cJSON_Print return nullptr.
98 * @tc.type: FUNC
99 * @tc.require:
100 */
101 HWTEST_F(WallpaperTestMock, SaveWallpaperState002, TestSize.Level0)
102 {
103     HILOG_INFO("SaveWallpaperState002 begin");
104     std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
105     wallpaperService->LoadWallpaperState();
106     int32_t userId = wallpaperService->QueryActiveUserId();
107     cJSON *image = cJSON_CreateObject();
108     cJSON_AddStringToObject(image, WALLPAPER, WALLPAPER_MOCK_CJSON_NAME);
109     EXPECT_CALL(*wallpaperMock, cJSON_AddNumberToObject(_, _, _)).WillRepeatedly(Return(image));
110     EXPECT_CALL(*wallpaperMock, cJSON_Print(_)).WillOnce(Return(nullptr));
111     auto ret =
112         wallpaperService->SaveWallpaperState(userId, WallpaperType::WALLPAPER_SYSTEM, WallpaperResourceType::DEFAULT);
113     if (image != nullptr) {
114         cJSON_Delete(image);
115     }
116     EXPECT_EQ(ret, false) << "Failed to SaveWallpaperState";
117 }
118 
119 /**
120 * @tc.name: SaveWallpaperState
121 * @tc.desc: save wallpaper state err test, cJSON_Print return empty.
122 * @tc.type: FUNC
123 * @tc.require:
124 */
125 HWTEST_F(WallpaperTestMock, SaveWallpaperState003, TestSize.Level0)
126 {
127     HILOG_INFO("SaveWallpaperState003 begin");
128     std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
129     wallpaperService->LoadWallpaperState();
130     int32_t userId = wallpaperService->QueryActiveUserId();
131     cJSON *image = cJSON_CreateObject();
132     cJSON_AddStringToObject(image, WALLPAPER, WALLPAPER_MOCK_CJSON_NAME);
133     EXPECT_CALL(*wallpaperMock, cJSON_AddNumberToObject(_, _, _)).WillRepeatedly(Return(image));
134     EXPECT_CALL(*wallpaperMock, cJSON_Print(_)).WillOnce(Return(const_cast<char *>("")));
135     auto ret =
136         wallpaperService->SaveWallpaperState(userId, WallpaperType::WALLPAPER_SYSTEM, WallpaperResourceType::DEFAULT);
137     if (image != nullptr) {
138         cJSON_Delete(image);
139     }
140     EXPECT_EQ(ret, false) << "Failed to SaveWallpaperState";
141 }
142 
143 /**
144 * @tc.name: SaveWallpaperState
145 * @tc.desc: save wallpaper state err test, cJSON_Parse return nullptr.
146 * @tc.type: FUNC
147 * @tc.require:
148 */
149 HWTEST_F(WallpaperTestMock, SaveWallpaperState004, TestSize.Level0)
150 {
151     HILOG_INFO("SaveWallpaperState004 begin");
152     std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
153     wallpaperService->LoadWallpaperState();
154     EXPECT_CALL(*wallpaperMock, cJSON_Parse(_)).WillRepeatedly(Return(nullptr));
155     int32_t userId = wallpaperService->QueryActiveUserId();
156     auto ret =
157         wallpaperService->SaveWallpaperState(userId, WallpaperType::WALLPAPER_SYSTEM, WallpaperResourceType::DEFAULT);
158     EXPECT_EQ(ret, false) << "Failed to SaveWallpaperState";
159 }
160 
161 /**
162 * @tc.name: GetWallpaperPathInJson
163 * @tc.desc: get wallpaper path err test, cJSON_Parse return nullptr.
164 * @tc.type: FUNC
165 * @tc.require:
166 */
167 HWTEST_F(WallpaperTestMock, GetWallpaperPathInJson001, TestSize.Level0)
168 {
169     HILOG_INFO("GetWallpaperPathInJson001 begin");
170     std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
171     EXPECT_CALL(*wallpaperMock, cJSON_Parse(_)).WillRepeatedly(Return(nullptr));
172     std::string manifestName = "home/manifest.json";
173     std::string filePath = "";
174     auto ret = wallpaperService->GetWallpaperPathInJson(manifestName, filePath);
175     EXPECT_TRUE(ret.empty()) << "Failed to GetWallpaperPathInJson";
176 }
177 
178 /**
179 * @tc.name: GetWallpaperPathInJson
180 * @tc.desc: get wallpaper path err test, cJSON_GetObjectItemCaseSensitive return nullptr.
181 * @tc.type: FUNC
182 * @tc.require:
183 */
184 HWTEST_F(WallpaperTestMock, GetWallpaperPathInJson002, TestSize.Level0)
185 {
186     HILOG_INFO("GetWallpaperPathInJson002 begin");
187     std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
188     std::string resPath = wallpaperService->GetDefaultResDir();
189     if (resPath.empty() && !FileDeal::IsDirExist(resPath)) {
190         HILOG_ERROR("wallpaperDefaultDir get failed!");
191     }
192     std::string manifestName = "home/manifest.json";
193     std::string manifestFile = resPath + manifestName;
194     std::ifstream file(manifestFile);
195     if (!file.is_open()) {
196         HILOG_ERROR("open fail:%{public}s", manifestFile.c_str());
197         file.close();
198     }
199     std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
200     file.close();
201     cJSON *image = cJSON_Parse(content.c_str());
202     if (image == nullptr) {
203         HILOG_ERROR("Failed to parse json.");
204     }
205     EXPECT_CALL(*wallpaperMock, cJSON_Parse(_)).WillRepeatedly(Return(image));
206     EXPECT_CALL(*wallpaperMock, cJSON_GetObjectItemCaseSensitive(image, _)).WillRepeatedly(Return(nullptr));
207     std::string filePath = "";
208     auto ret = wallpaperService->GetWallpaperPathInJson(manifestName, filePath);
209     if (image != nullptr) {
210         cJSON_Delete(image);
211     }
212     EXPECT_TRUE(ret.empty()) << "Failed to GetWallpaperPathInJson";
213 }
214 
215 /**
216 * @tc.name: GetWallpaperPathInJson
217 * @tc.desc: get wallpaper path err test, cJSON is not object.
218 * @tc.type: FUNC
219 * @tc.require:
220 */
221 HWTEST_F(WallpaperTestMock, GetWallpaperPathInJson003, TestSize.Level0)
222 {
223     HILOG_INFO("GetWallpaperPathInJson003 begin");
224     std::shared_ptr<WallpaperService> wallpaperService = std::make_shared<WallpaperService>();
225     std::string resPath = wallpaperService->GetDefaultResDir();
226     if (resPath.empty() && !FileDeal::IsDirExist(resPath)) {
227         HILOG_ERROR("wallpaperDefaultDir get failed!");
228     }
229     std::string manifestName = "home/manifest.json";
230     std::string manifestFile = resPath + manifestName;
231     std::ifstream file(manifestFile);
232     if (!file.is_open()) {
233         HILOG_ERROR("open fail:%{public}s", manifestFile.c_str());
234         file.close();
235     }
236     std::string content((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
237     file.close();
238     cJSON *image = cJSON_Parse(content.c_str());
239     if (image == nullptr) {
240         HILOG_ERROR("Failed to parse json.");
241     }
242     EXPECT_CALL(*wallpaperMock, cJSON_Parse(_)).WillRepeatedly(Return(image));
243     EXPECT_CALL(*wallpaperMock, cJSON_GetObjectItemCaseSensitive(image, _)).WillRepeatedly(Return(image));
244     std::string filePath = "";
245     auto ret = wallpaperService->GetWallpaperPathInJson(manifestName, filePath);
246     if (image != nullptr) {
247         cJSON_Delete(image);
248     }
249     EXPECT_TRUE(ret.empty()) << "Failed to GetWallpaperPathInJson";
250 }
251 } // namespace WallpaperMgrService
252 } // namespace OHOS