• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 
16 #include <gtest/gtest.h>
17 #include <memory>
18 
19 #define private public
20 #define protected public
21 #include "notification_action_button.h"
22 #undef private
23 #undef protected
24 
25 #include "want_agent_helper.h"
26 
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace Notification {
30 class NotificationActionButtontTest : public testing::Test {
31 public:
SetUpTestCase()32     static void SetUpTestCase() {};
TearDownTestCase()33     static void TearDownTestCase() {};
SetUp()34     void SetUp() {};
TearDown()35     void TearDown() {};
36 };
37 
38 /**
39  * @tc.name: AddMimeTypeOnlyUserInput_0100
40  * @tc.desc: AddMimeTypeOnlyUserInput
41  * @tc.type: FUNC
42  * @tc.require: issueI65R21
43  */
HWTEST_F(NotificationActionButtontTest,AddActionButton_0100,Level1)44 HWTEST_F(NotificationActionButtontTest, AddActionButton_0100, Level1)
45 {
46     std::shared_ptr<NotificationActionButton> actionButton = nullptr;
47     std::shared_ptr<NotificationActionButton> notificationActionButton =
48         NotificationActionButton::Create(actionButton);
49     AbilityRuntime::WantAgent::WantAgentInfo paramsInfo;
50     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent =
51         AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(paramsInfo);
52     std::shared_ptr<NotificationActionButton> actionButton1 =
53         NotificationActionButton::Create(nullptr, "title", wantAgent);
54     std::shared_ptr<NotificationActionButton> notificationActionButton1 =
55         NotificationActionButton::Create(actionButton1);
56     notificationActionButton1->AddMimeTypeOnlyUserInput(nullptr);
57     EXPECT_EQ(notificationActionButton, nullptr);
58 }
59 
60 /**
61  * @tc.name: Marshalling_0100
62  * @tc.desc: Marshalling
63  * @tc.type: FUNC
64  * @tc.require: issueI65R21
65  */
HWTEST_F(NotificationActionButtontTest,Marshalling_0100,Level1)66 HWTEST_F(NotificationActionButtontTest, Marshalling_0100, Level1)
67 {
68     AbilityRuntime::WantAgent::WantAgentInfo paramsInfo;
69     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent =
70         AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(paramsInfo);
71     std::shared_ptr<NotificationActionButton> actionButton1 =
72         NotificationActionButton::Create(nullptr, "title", wantAgent);
73     std::shared_ptr<NotificationActionButton> notificationActionButton1 =
74         NotificationActionButton::Create(actionButton1);
75 
76     Parcel parcel;
77     bool result = notificationActionButton1->Marshalling(parcel);
78     EXPECT_EQ(result, true);
79 }
80 
81 /**
82  * @tc.name: Unmarshalling_0100
83  * @tc.desc: Unmarshalling
84  * @tc.type: FUNC
85  * @tc.require: issueI65R21
86  */
HWTEST_F(NotificationActionButtontTest,Unmarshalling_0100,Level1)87 HWTEST_F(NotificationActionButtontTest, Unmarshalling_0100, Level1)
88 {
89     AbilityRuntime::WantAgent::WantAgentInfo paramsInfo;
90     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent =
91         AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(paramsInfo);
92     std::shared_ptr<NotificationActionButton> actionButton1 =
93         NotificationActionButton::Create(nullptr, "title", wantAgent);
94     std::shared_ptr<NotificationActionButton> notificationActionButton1 =
95         NotificationActionButton::Create(actionButton1);
96 
97     Parcel parcel;
98     bool result = notificationActionButton1->ReadFromParcel(parcel);
99     notificationActionButton1->Unmarshalling(parcel);
100     EXPECT_EQ(result, false);
101 }
102 
103 /**
104  * @tc.name: Unmarshalling_0200
105  * @tc.desc: Unmarshalling
106  * @tc.type: FUNC
107  * @tc.require: issueI65R21
108  */
HWTEST_F(NotificationActionButtontTest,Unmarshalling_0200,Level1)109 HWTEST_F(NotificationActionButtontTest, Unmarshalling_0200, Level1)
110 {
111     std::shared_ptr<Media::PixelMap> icon = nullptr;
112     std::string title = "thios is title";
113     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent = nullptr;
114     std::shared_ptr<AAFwk::WantParams> extras;
115     NotificationConstant::SemanticActionButton semanticActionButton =
116         NotificationConstant::SemanticActionButton(2);
117     bool autoCreatedReplies = true;
118     auto userInput = std::make_shared<NotificationUserInput>();
119     std::vector<std::string> options = {"test1", "test2"};
120     userInput->SetOptions(options);
121     userInput->SetPermitMimeTypes("test", true);
122     std::vector<std::shared_ptr<NotificationUserInput>> mimeTypeOnlyInputs = {userInput};
123     bool isContextual = false;
124 
125     auto button = std::make_shared<NotificationActionButton>(icon, title, wantAgent, extras,
126         semanticActionButton, autoCreatedReplies, mimeTypeOnlyInputs, userInput, isContextual);
127     Parcel parcel;
128     EXPECT_EQ(button->Marshalling(parcel), true);
129     EXPECT_NE(button->Unmarshalling(parcel), nullptr);
130 }
131 
132 /**
133  * @tc.name: Create_00001
134  * @tc.desc: Test Create parameters.
135  * @tc.type: FUNC
136  * @tc.require: issue
137  */
138 HWTEST_F(NotificationActionButtontTest, Create_00001, Function | SmallTest | Level1)
139 {
140     std::shared_ptr<Media::PixelMap> icon = nullptr;
141     std::string title = "thios is title";
142     AbilityRuntime::WantAgent::WantAgentInfo paramsInfo;
143     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent = nullptr;
144     std::shared_ptr<AAFwk::WantParams> extras;
145     NotificationConstant::SemanticActionButton semanticActionButton =
146         NotificationConstant::SemanticActionButton(2);
147     bool autoCreatedReplies = true;
148     std::vector<std::shared_ptr<NotificationUserInput>> mimeTypeOnlyInputs;
149     std::shared_ptr<NotificationUserInput> userInput;
150     bool isContextual = true;
151     std::shared_ptr<NotificationActionButton> notificationActionButton = std::make_shared<NotificationActionButton>();
152     ASSERT_NE(nullptr, notificationActionButton);
153     std::shared_ptr<NotificationActionButton> result = notificationActionButton->Create
154     (icon, title, wantAgent, extras, semanticActionButton,
155     autoCreatedReplies, mimeTypeOnlyInputs, userInput, isContextual);
156 }
157 
158 /**
159  * @tc.name: Create_00002
160  * @tc.desc: Test Create parameters.
161  * @tc.type: FUNC
162  * @tc.require: issue
163  */
164 HWTEST_F(NotificationActionButtontTest, Create_00002, Function | SmallTest | Level1)
165 {
166     std::shared_ptr<Media::PixelMap> icon = std::make_shared<Media::PixelMap>();
167     std::string title = "thios is title";
168     AbilityRuntime::WantAgent::WantAgentInfo paramsInfo;
169     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent =
170         AbilityRuntime::WantAgent::WantAgentHelper::GetWantAgent(paramsInfo);
171     std::shared_ptr<AAFwk::WantParams> extras;
172     NotificationConstant::SemanticActionButton semanticActionButton =
173         NotificationConstant::SemanticActionButton(2);
174     bool autoCreatedReplies = true;
175     std::vector<std::shared_ptr<NotificationUserInput>> mimeTypeOnlyInputs;
176     std::shared_ptr<NotificationUserInput> userInput;
177     bool isContextual = true;
178     auto rrc = std::make_shared<NotificationActionButton>();
179     ASSERT_NE(nullptr, rrc);
180     rrc->Create(icon, title, wantAgent, extras, semanticActionButton,
181     autoCreatedReplies, mimeTypeOnlyInputs, userInput, isContextual);
182 }
183 
184 /**
185  * @tc.name: Create_00003
186  * @tc.desc: Test Create parameters.
187  * @tc.type: FUNC
188  * @tc.require: issue
189  */
190 HWTEST_F(NotificationActionButtontTest, Create_00003, Function | SmallTest | Level1)
191 {
192     std::shared_ptr<Media::PixelMap> icon = std::make_shared<Media::PixelMap>();
193     std::string title = "thios is title";
194     AbilityRuntime::WantAgent::WantAgentInfo paramsInfo;
195     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent =
196         std::make_shared<AbilityRuntime::WantAgent::WantAgent>();
197     std::shared_ptr<AAFwk::WantParams> extras;
198     NotificationConstant::SemanticActionButton semanticActionButton =
199         NotificationConstant::SemanticActionButton(2);
200     bool autoCreatedReplies = true;
201     std::vector<std::shared_ptr<NotificationUserInput>> mimeTypeOnlyInputs;
202     ASSERT_EQ(mimeTypeOnlyInputs.size(), 0);
203     std::shared_ptr<NotificationUserInput> userInput = std::make_shared<NotificationUserInput>();
204     userInput->SetPermitFreeFormInput(false);
205     userInput->SetPermitMimeTypes("test", true);
206     bool isContextual = true;
207 
208     std::shared_ptr<NotificationActionButton> notificationActionButton = std::make_shared<NotificationActionButton>();
209 
210     std::shared_ptr<NotificationActionButton> result = notificationActionButton->Create
211     (icon, title, wantAgent, extras, semanticActionButton,
212     autoCreatedReplies, mimeTypeOnlyInputs, userInput, isContextual);
213     ASSERT_NE(result, nullptr);
214     ASSERT_EQ(result->GetMimeTypeOnlyUserInputs().size(), 1);
215 }
216 
217 /**
218  * @tc.name: Dump_00002
219  * @tc.desc: Test Dump parameters.
220  * @tc.type: FUNC
221  * @tc.require: issue
222  */
223 HWTEST_F(NotificationActionButtontTest, Dump_00002, Function | SmallTest | Level1)
224 {
225     std::shared_ptr<Media::PixelMap> icon = std::make_shared<Media::PixelMap>();
226     std::string title = "thios is title";
227     AbilityRuntime::WantAgent::WantAgentInfo paramsInfo;
228     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent =
229         std::make_shared<AbilityRuntime::WantAgent::WantAgent>();
230     std::shared_ptr<AAFwk::WantParams> extras;
231     NotificationConstant::SemanticActionButton semanticActionButton =
232         NotificationConstant::SemanticActionButton(2);
233     bool autoCreatedReplies = true;
234     std::vector<std::shared_ptr<NotificationUserInput>> mimeTypeOnlyInputs;
235     ASSERT_EQ(mimeTypeOnlyInputs.size(), 0);
236     std::shared_ptr<NotificationUserInput> userInput = std::make_shared<NotificationUserInput>();
237     userInput->SetPermitFreeFormInput(false);
238     userInput->SetPermitMimeTypes("test", true);
239     bool isContextual = true;
240 
241     std::shared_ptr<NotificationActionButton> notificationActionButton = std::make_shared<NotificationActionButton>();
242 
243     std::shared_ptr<NotificationActionButton> result = notificationActionButton->Create
244     (icon, title, wantAgent, extras, semanticActionButton,
245     autoCreatedReplies, mimeTypeOnlyInputs, userInput, isContextual);
246     ASSERT_NE(result, nullptr);
247     ASSERT_EQ(result->GetMimeTypeOnlyUserInputs().size(), 1);
248     std::string temp = result->Dump();
249 
250     auto it = temp.find("mimeTypeOnlyUserInputs");
251     ASSERT_NE(it, std::string::npos);
252 }
253 
254 
255 /**
256  * @tc.name: FromJson_00002
257  * @tc.desc: Test FromJson parameters.
258  * @tc.type: FUNC
259  * @tc.require: issue
260  */
261 HWTEST_F(NotificationActionButtontTest, FromJson_00002, Function | SmallTest | Level1)
262 {
263     auto rrc = std::make_shared<NotificationActionButton>();
264     nlohmann::json jsonObject = nlohmann::json{"processName", "soundEnabled", "name", "arrivedTime1"};
265     rrc->FromJson(jsonObject);
266     EXPECT_EQ(jsonObject.is_object(), false);
267     EXPECT_EQ(rrc->FromJson(jsonObject), nullptr);
268 }
269 
270 /**
271  * @tc.name: FromJson_00003
272  * @tc.desc: Test FromJson parameters.
273  * @tc.type: FUNC
274  * @tc.require: issue
275  */
276 HWTEST_F(NotificationActionButtontTest, FromJson_00003, Function | SmallTest | Level1)
277 {
278     auto rrc = std::make_shared<NotificationActionButton>();
279     nlohmann::json jsonObject = nlohmann::json{
280         {"processName", "process6"}, {"APL", 1},
281         {"version", 2}, {"tokenId", 685266937},
282         {"tokenAttr", 0},
283         {"dcaps", {"AT_CAP", "ST_CAP"}}};
284     rrc->FromJson(jsonObject);
285     EXPECT_EQ(jsonObject.is_object(), true);
286 }
287 
288 /**
289  * @tc.name: FromJson_00004
290  * @tc.desc: Test FromJson parameters.
291  * @tc.type: FUNC
292  * @tc.require: issue
293  */
294 HWTEST_F(NotificationActionButtontTest, FromJson_00004, Function | SmallTest | Level1)
295 {
296     std::shared_ptr<Media::PixelMap> icon = std::make_shared<Media::PixelMap>();
297     std::string title = "test";
298     std::shared_ptr<AbilityRuntime::WantAgent::WantAgent> wantAgent =
299         std::make_shared<AbilityRuntime::WantAgent::WantAgent>();
300     std::shared_ptr<AAFwk::WantParams> extras = std::make_shared<AAFwk::WantParams>();
301     NotificationConstant::SemanticActionButton semanticActionButton =
302         NotificationConstant::SemanticActionButton(2);
303     bool autoCreatedReplies = true;
304     std::vector<std::shared_ptr<NotificationUserInput>> mimeTypeOnlyInputs;
305     ASSERT_EQ(mimeTypeOnlyInputs.size(), 0);
306     std::shared_ptr<NotificationUserInput> userInput = std::make_shared<NotificationUserInput>();
307     userInput->SetPermitFreeFormInput(false);
308     userInput->SetPermitMimeTypes("test", true);
309     bool isContextual = true;
310 
311     std::shared_ptr<NotificationActionButton> notificationActionButton = std::make_shared<NotificationActionButton>();
312     std::shared_ptr<NotificationActionButton> rrc = notificationActionButton->Create
313     (icon, title, wantAgent, extras, semanticActionButton,
314     autoCreatedReplies, mimeTypeOnlyInputs, userInput, isContextual);
315 
316     nlohmann::json jsonObject;
317     auto res = rrc->ToJson(jsonObject);
318     ASSERT_EQ(res, true);
319 
320     std::string jsonString = jsonObject.dump();
321     auto it = jsonString.find("icon");
322     ASSERT_NE(it, std::string::npos);
323     it = jsonString.find("extras");
324     ASSERT_NE(it, std::string::npos);
325     it = jsonString.find("wantAgent");
326     ASSERT_NE(it, std::string::npos);
327 
328     auto temp = rrc->FromJson(jsonObject);
329     ASSERT_NE(temp, nullptr);
330     ASSERT_EQ(temp->GetTitle(), "test");
331 }
332 
333 /**
334  * @tc.name: Dump_00001
335  * @tc.desc: Test Dump parameters.
336  * @tc.type: FUNC
337  * @tc.require: issue
338  */
339 HWTEST_F(NotificationActionButtontTest, Dump_00001, Function | SmallTest | Level1)
340 {
341     auto button = std::make_shared<NotificationActionButton>();
342     auto userInput = std::make_shared<NotificationUserInput>();
343     userInput->SetPermitFreeFormInput(false);
344     userInput->SetPermitMimeTypes("test", true);
345     button->AddMimeTypeOnlyUserInput(userInput);
346     button->AddNotificationUserInput(userInput);
347 
348     EXPECT_EQ(button->GetMimeTypeOnlyUserInputs().size(), 1);
349     EXPECT_NE(button->GetUserInput(), nullptr);
350 
351     std::string dumpStr = "";
352     EXPECT_NE(button->Dump(), dumpStr);
353 }
354 
355 }
356 }
357