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: FromJson_00002
186  * @tc.desc: Test FromJson parameters.
187  * @tc.type: FUNC
188  * @tc.require: issue
189  */
190 HWTEST_F(NotificationActionButtontTest, FromJson_00002, Function | SmallTest | Level1)
191 {
192     auto rrc = std::make_shared<NotificationActionButton>();
193     nlohmann::json jsonObject = nlohmann::json{"processName", "soundEnabled", "name", "arrivedTime1"};
194     rrc->FromJson(jsonObject);
195     EXPECT_EQ(jsonObject.is_object(), false);
196     EXPECT_EQ(rrc->FromJson(jsonObject), nullptr);
197 }
198 
199 /**
200  * @tc.name: FromJson_00003
201  * @tc.desc: Test FromJson parameters.
202  * @tc.type: FUNC
203  * @tc.require: issue
204  */
205 HWTEST_F(NotificationActionButtontTest, FromJson_00003, Function | SmallTest | Level1)
206 {
207     auto rrc = std::make_shared<NotificationActionButton>();
208     nlohmann::json jsonObject = nlohmann::json{
209         {"processName", "process6"}, {"APL", 1},
210         {"version", 2}, {"tokenId", 685266937},
211         {"tokenAttr", 0},
212         {"dcaps", {"AT_CAP", "ST_CAP"}}};
213     rrc->FromJson(jsonObject);
214     EXPECT_EQ(jsonObject.is_object(), true);
215 }
216 
217 /**
218  * @tc.name: Dump_00001
219  * @tc.desc: Test Dump parameters.
220  * @tc.type: FUNC
221  * @tc.require: issue
222  */
223 HWTEST_F(NotificationActionButtontTest, Dump_00001, Function | SmallTest | Level1)
224 {
225     auto button = std::make_shared<NotificationActionButton>();
226     auto userInput = std::make_shared<NotificationUserInput>();
227     userInput->SetPermitFreeFormInput(false);
228     userInput->SetPermitMimeTypes("test", true);
229     button->AddMimeTypeOnlyUserInput(userInput);
230     button->AddNotificationUserInput(userInput);
231 
232     EXPECT_EQ(button->GetMimeTypeOnlyUserInputs().size(), 1);
233     EXPECT_NE(button->GetUserInput(), nullptr);
234 
235     std::string dumpStr = "";
236     EXPECT_NE(button->Dump(), dumpStr);
237 }
238 
239 }
240 }
241