1 /*
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 #include <memory>
18 
19 #define private public
20 #define protected public
21 #include "notification_live_view_content.h"
22 #undef private
23 #undef protected
24 
25 using namespace testing::ext;
26 namespace OHOS {
27 namespace Notification {
28 class NotificationLiveViewContentTest : public testing::Test {
29 public:
SetUpTestCase()30     static void SetUpTestCase() {}
TearDownTestCase()31     static void TearDownTestCase() {}
SetUp()32     void SetUp() {}
TearDown()33     void TearDown() {}
34 };
35 
36 /**
37  * @tc.name: SetLiveViewStatus_00001
38  * @tc.desc: Test SetLiveViewStatus parameter.
39  * @tc.type: FUNC
40  * @tc.require: issue
41  */
42 HWTEST_F(NotificationLiveViewContentTest, SetLiveViewStatus_00001, Function | SmallTest | Level1)
43 {
44     auto rrc = std::make_shared<NotificationLiveViewContent>();
45     rrc->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_CREATE);
46     EXPECT_EQ(rrc->GetLiveViewStatus(), NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_CREATE);
47     rrc->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT);
48     EXPECT_EQ(rrc->GetLiveViewStatus(), NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_BUTT);
49 }
50 
51 /**
52  * @tc.name: SetVersion_00001
53  * @tc.desc: Test SetVersion parameters.
54  * @tc.type: FUNC
55  * @tc.require: issue
56  */
57 HWTEST_F(NotificationLiveViewContentTest, SetVersion_00001, Function | SmallTest | Level1)
58 {
59     auto rrc = std::make_shared<NotificationLiveViewContent>();
60     rrc->SetVersion(NotificationLiveViewContent::MAX_VERSION);
61     EXPECT_EQ(rrc->GetVersion(), NotificationLiveViewContent::MAX_VERSION);
62 }
63 
64 /**
65  * @tc.name: SetExtraInfo_00001
66  * @tc.desc: Test SetExtraInfo parameters.
67  * @tc.type: FUNC
68  * @tc.require: issue
69  */
70 HWTEST_F(NotificationLiveViewContentTest, SetExtraInfo_00001, Function | SmallTest | Level1)
71 {
72     auto extraInfo = std::make_shared<AAFwk::WantParams>();
73     auto rrc = std::make_shared<NotificationLiveViewContent>();
74     rrc->SetExtraInfo(extraInfo);
75     EXPECT_EQ(rrc->GetExtraInfo(), extraInfo);
76 }
77 
78 /**
79  * @tc.name: SetPicture_00001
80  * @tc.desc: Test SetPicture parameters.
81  * @tc.type: FUNC
82  * @tc.require: issue
83  */
84 HWTEST_F(NotificationLiveViewContentTest, SetPicture_00001, Function | SmallTest | Level1)
85 {
86     auto rrc = std::make_shared<NotificationLiveViewContent>();
87     PictureMap pictureMap{};
88     rrc->SetPicture(pictureMap);
89     EXPECT_EQ(rrc->GetPicture(), pictureMap);
90 }
91 
92 /**
93  * @tc.name: SetIsOnlyLocalUpdate_00001
94  * @tc.desc: Test SetIsOnlyLocalUpdate parameters.
95  * @tc.type: FUNC
96  * @tc.require: issue
97  */
98 HWTEST_F(NotificationLiveViewContentTest, SetIsOnlyLocalUpdate_00001, Function | SmallTest | Level1)
99 {
100     bool isOnlyLocalUpdate = true;
101     auto rrc = std::make_shared<NotificationLiveViewContent>();
102     rrc->SetIsOnlyLocalUpdate(isOnlyLocalUpdate);
103     EXPECT_EQ(rrc->GetIsOnlyLocalUpdate(), isOnlyLocalUpdate);
104 }
105 
106 /**
107  * @tc.name: Dump_00001
108  * @tc.desc: Test Dump parameters.
109  * @tc.type: FUNC
110  * @tc.require: issue
111  */
112 HWTEST_F(NotificationLiveViewContentTest, Dump_00001, Function | SmallTest | Level1)
113 {
114     auto rrc = std::make_shared<NotificationLiveViewContent>();
115     std::string ret = "NotificationLiveViewContent{ title = , text = , "
116     "additionalText = , lockScreenPicture = null, status = 0, version = -1, extraInfo = null, "
117     "isOnlyLocalUpdate_ = false, pictureMap = {}}";
118 
119     EXPECT_EQ(rrc->Dump(), ret);
120 }
121 
122 /**
123  * @tc.name: Dump_00002
124  * @tc.desc: Test Dump parameters when pictureMap isn't empty.
125  * @tc.type: FUNC
126  * @tc.require: issue
127  */
128 HWTEST_F(NotificationLiveViewContentTest, Dump_00002, Function | SmallTest | Level1)
129 {
130     PictureMap pictureMap;
131     std::vector<std::shared_ptr<Media::PixelMap>> pixelVec;
132     pixelVec.push_back(std::make_shared<Media::PixelMap>());
133     auto picture = std::make_pair(std::string{"test"}, pixelVec);
134     pictureMap.insert(picture);
135 
136     auto rrc = std::make_shared<NotificationLiveViewContent>();
137     rrc->SetPicture(pictureMap);
138     rrc->SetTitle("title");
139     rrc->SetText("text");
140     rrc->SetAdditionalText("addText");
141 
142     std::string ret = "NotificationLiveViewContent{ title = title, text = text, "
143         "additionalText = addText, lockScreenPicture = null, status = 0, version = -1, extraInfo = null, "
144         "isOnlyLocalUpdate_ = false, pictureMap = { { key = test, value = not empty } }}";
145 
146     EXPECT_EQ(rrc->Dump(), ret);
147 }
148 
149 /**
150  * @tc.name: JsonConvert_00001
151  * @tc.desc: Test JsonConvert parameters.
152  * @tc.type: FUNC
153  * @tc.require: issue
154  */
155 HWTEST_F(NotificationLiveViewContentTest, JsonConvert_00001, Function | SmallTest | Level1)
156 {
157     auto rrc = std::make_shared<NotificationLiveViewContent>();
158 
159     PictureMap pictureMap;
160     std::vector<std::shared_ptr<Media::PixelMap>> pixelVec;
161     pixelVec.push_back(std::make_shared<Media::PixelMap>());
162     auto picture = std::make_pair(std::string{"test"}, pixelVec);
163     pictureMap.insert(picture);
164     rrc->SetPicture(pictureMap);
165 
166     auto extraInfo = std::make_shared<AAFwk::WantParams>();
167     rrc->SetExtraInfo(extraInfo);
168 
169     rrc->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_INCREMENTAL_UPDATE);
170     rrc->SetVersion(NotificationLiveViewContent::MAX_VERSION);
171     rrc->SetTitle("title");
172     rrc->SetText("text");
173     rrc->SetAdditionalText("addText");
174 
175     nlohmann::json jsonObject;
176     EXPECT_EQ(rrc->ToJson(jsonObject), true);
177     const auto &jsonEnd = jsonObject.cend();
178     EXPECT_NE(jsonObject.find("pictureMap"), jsonEnd);
179     EXPECT_NE(jsonObject.find("extraInfo"), jsonEnd);
180 
181     auto ptr = NotificationLiveViewContent::FromJson(jsonObject);
182     EXPECT_EQ(jsonObject.is_object(), true);
183     EXPECT_NE(ptr, nullptr);
184     EXPECT_EQ(ptr->GetTitle(), std::string("title"));
185     EXPECT_EQ(ptr->GetText(), std::string("text"));
186     EXPECT_EQ(ptr->GetAdditionalText(), std::string("addText"));
187     EXPECT_EQ(ptr->GetLiveViewStatus(), NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_INCREMENTAL_UPDATE);
188     EXPECT_EQ(ptr->GetVersion(), NotificationLiveViewContent::MAX_VERSION);
189     EXPECT_NE(ptr->GetExtraInfo(), nullptr);
190     EXPECT_EQ(ptr->GetPicture().size(), 1);
191     delete ptr;
192 }
193 
194 /**
195  * @tc.name: FromJson_00001
196  * @tc.desc: Test FromJson parameters when jsonObject is invalid.
197  * @tc.type: FUNC
198  * @tc.require: issue
199  */
200 HWTEST_F(NotificationLiveViewContentTest, FromJson_00001, Function | SmallTest | Level1)
201 {
202     auto rrc = std::make_shared<NotificationLiveViewContent>();
203     nlohmann::json jsonObject = nlohmann::json{"processName", "process6", "expandedTitle", "arrivedTime1"};
204     auto ptr = rrc->FromJson(jsonObject);
205     EXPECT_EQ(jsonObject.is_object(), false);
206     EXPECT_EQ(ptr, nullptr);
207 }
208 
209 /**
210  * @tc.name: MarshallConvert_00001
211  * @tc.desc: Test MarshallConvert parameters.
212  * @tc.type: FUNC
213  * @tc.require: issue
214  */
215 HWTEST_F(NotificationLiveViewContentTest, MarshallConvert_00001, Function | SmallTest | Level1)
216 {
217     auto rrc = std::make_shared<NotificationLiveViewContent>();
218 
219     PictureMap pictureMap;
220     std::vector<std::shared_ptr<Media::PixelMap>> pixelVec;
221     pixelVec.push_back(std::make_shared<Media::PixelMap>());
222     auto picture = std::make_pair(std::string{"picture"}, pixelVec);
223     pictureMap.insert(picture);
224     auto image = std::make_pair(std::string{"image"}, pixelVec);
225     pictureMap.insert(image);
226     rrc->SetPicture(pictureMap);
227 
228     auto extraInfo = std::make_shared<AAFwk::WantParams>();
229     rrc->SetExtraInfo(extraInfo);
230 
231     rrc->SetLiveViewStatus(NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_END);
232     uint32_t version = NotificationLiveViewContent::MAX_VERSION - 1;
233     rrc->SetVersion(version);
234 
235     bool isOnlyLocalUpdate = true;
236     rrc->SetIsOnlyLocalUpdate(isOnlyLocalUpdate);
237 
238     rrc->SetTitle("title");
239     rrc->SetText("text");
240     rrc->SetAdditionalText("addText");
241 
242     Parcel parcel;
243     EXPECT_EQ(rrc->Marshalling(parcel), true);
244 
245     auto ptr = NotificationLiveViewContent::Unmarshalling(parcel);
246     EXPECT_NE(ptr, nullptr);
247     EXPECT_EQ(ptr->GetTitle(), std::string("title"));
248     EXPECT_EQ(ptr->GetText(), std::string("text"));
249     EXPECT_EQ(ptr->GetAdditionalText(), std::string("addText"));
250     EXPECT_EQ(ptr->GetLiveViewStatus(), NotificationLiveViewContent::LiveViewStatus::LIVE_VIEW_END);
251     EXPECT_EQ(ptr->GetVersion(), version);
252     EXPECT_NE(ptr->GetExtraInfo(), nullptr);
253     EXPECT_EQ(ptr->GetPicture().size(), 2);
254     EXPECT_EQ(ptr->GetIsOnlyLocalUpdate(), true);
255     delete ptr;
256 }
257 
258 /**
259  * @tc.name: Unmarshalling_00001
260  * @tc.desc: Test Unmarshalling parameters.
261  * @tc.type: FUNC
262  * @tc.require: issue
263  */
264 HWTEST_F(NotificationLiveViewContentTest, Unmarshalling_00001, Function | SmallTest | Level1)
265 {
266     Parcel parcel;
267     auto ptr = NotificationLiveViewContent::Unmarshalling(parcel);
268     EXPECT_EQ(ptr, nullptr);
269 }
270 
271 /**
272  * @tc.name: MarshallingPictureMap_00001
273  * @tc.desc: Test MarshallingPictureMap.
274  * @tc.type: FUNC
275  * @tc.require: issue
276  */
277 HWTEST_F(NotificationLiveViewContentTest, MarshallingPictureMap_00001, Function | SmallTest | Level1)
278 {
279     Parcel parcel;
280     auto liveViewContent = std::make_shared<NotificationLiveViewContent>();
281     bool isSuccess = liveViewContent->MarshallingPictureMap(parcel);
282     EXPECT_EQ(isSuccess, true);
283 }
284 
285 /**
286  * @tc.name: MarshallingPictureMap_00002
287  * @tc.desc: Test MarshallingPictureMap.
288  * @tc.type: FUNC
289  * @tc.require: issue
290  */
291 HWTEST_F(NotificationLiveViewContentTest, MarshallingPictureMap_00002, Function | SmallTest | Level1)
292 {
293     PictureMap pictureMap;
294     std::vector<std::shared_ptr<Media::PixelMap>> pixelVec;
295     pixelVec.push_back(std::make_shared<Media::PixelMap>());
296     auto picture = std::make_pair(std::string{"test"}, pixelVec);
297     pictureMap.insert(picture);
298 
299     auto liveViewContent = std::make_shared<NotificationLiveViewContent>();
300     liveViewContent->SetPicture(pictureMap);
301     bool isEmptyMarshallingMap = liveViewContent->GetPictureMarshallingMap().empty();
302     EXPECT_EQ(isEmptyMarshallingMap, true);
303 
304     Parcel parcel;
305     bool isSuccess = liveViewContent->MarshallingPictureMap(parcel);
306     EXPECT_EQ(isSuccess, true);
307 }
308 
309 /**
310  * @tc.name: MarshallingPictureMap_00003
311  * @tc.desc: Test MarshallingPictureMap.
312  * @tc.type: FUNC
313  * @tc.require: issue
314  */
315 HWTEST_F(NotificationLiveViewContentTest, MarshallingPictureMap_00003, Function | SmallTest | Level1)
316 {
317     PictureMap pictureMap;
318     std::vector<std::shared_ptr<Media::PixelMap>> pixelVec;
319     pixelVec.push_back(std::make_shared<Media::PixelMap>());
320     auto picture = std::make_pair(std::string{"test"}, pixelVec);
321     pictureMap.insert(picture);
322 
323     auto liveViewContent = std::make_shared<NotificationLiveViewContent>();
324     liveViewContent->SetPicture(pictureMap);
325     liveViewContent->FillPictureMarshallingMap();
326     bool isEmptyMarshallingMap = liveViewContent->GetPictureMarshallingMap().empty();
327     EXPECT_EQ(isEmptyMarshallingMap, false);
328 
329     Parcel parcel;
330     bool isSuccess = liveViewContent->MarshallingPictureMap(parcel);
331     EXPECT_EQ(isSuccess, true);
332 }
333 }
334 }
335