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 "ability_context.h"
18 #include "completed_callback.h"
19 #include "context_container.h"
20 #include "element_name.h"
21 #include "event_handler.h"
22 #include "base_types.h"
23 #include "pending_want.h"
24 #include "want.h"
25 #include "want_agent.h"
26 #include "want_agent_constant.h"
27 #include "want_agent_helper.h"
28 #define private public
29 #define protected public
30 #include "want_agent_info.h"
31 #undef private
32 #undef protected
33 #include "want_params.h"
34 #include "want_receiver_stub.h"
35 #include "want_sender_stub.h"
36 #include "bool_wrapper.h"
37 
38 using namespace testing::ext;
39 using namespace OHOS::AAFwk;
40 using namespace OHOS;
41 using OHOS::AppExecFwk::ElementName;
42 using namespace OHOS::AppExecFwk;
43 using vector_str = std::vector<std::string>;
44 
45 namespace OHOS::AbilityRuntime::WantAgent {
46 class WantAgentInfoTest : public testing::Test {
47 public:
WantAgentInfoTest()48     WantAgentInfoTest()
49     {}
~WantAgentInfoTest()50     ~WantAgentInfoTest()
51     {}
52     static void SetUpTestCase(void);
53     static void TearDownTestCase(void);
54     void SetUp();
55     void TearDown();
56 };
57 
SetUpTestCase(void)58 void WantAgentInfoTest::SetUpTestCase(void)
59 {}
60 
TearDownTestCase(void)61 void WantAgentInfoTest::TearDownTestCase(void)
62 {}
63 
SetUp(void)64 void WantAgentInfoTest::SetUp(void)
65 {}
66 
TearDown(void)67 void WantAgentInfoTest::TearDown(void)
68 {}
69 
70 /*
71  * @tc.number    : WantAgentInfo_0100
72  * @tc.name      : WantAgentInfo Constructors
73  * @tc.desc      : 1.Constructors
74  */
75 HWTEST_F(WantAgentInfoTest, WantAgentInfo_0100, Function | MediumTest | Level1)
76 {
77     int requestCode = 10;
78     WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
79     WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
80     std::vector<std::shared_ptr<Want>> wants;
81     std::shared_ptr<Want> want = std::make_shared<Want>();
82     ElementName element("device", "bundleName", "abilityName");
83     want->SetElement(element);
84     wants.emplace_back(want);
85     std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
86     bool value = true;
87     std::string key = "key";
88     extraInfo->SetParam(key, Boolean::Box(value));
89     std::shared_ptr<WantAgentInfo> wantAgentInfo =
90         std::make_shared<WantAgentInfo>(requestCode, operationType, flag, wants, extraInfo);
91     EXPECT_EQ(wantAgentInfo->GetRequestCode(), requestCode);
92     EXPECT_EQ(wantAgentInfo->GetOperationType(), operationType);
93     EXPECT_EQ(wantAgentInfo->GetFlags().at(0), flag);
94     EXPECT_NE(wantAgentInfo->GetWants().at(0), want);
95     EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetBundleName(), want->GetElement().GetBundleName());
96     EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetAbilityName(), want->GetElement().GetAbilityName());
97     EXPECT_NE(wantAgentInfo->GetExtraInfo(), extraInfo);
98     EXPECT_EQ(Boolean::Unbox(IBoolean::Query(wantAgentInfo->GetExtraInfo()->GetParam(key))),
99         Boolean::Unbox(IBoolean::Query(extraInfo->GetParam(key))));
100 }
101 
102 /*
103  * @tc.number    : WantAgentInfo_0200
104  * @tc.name      : WantAgentInfo Constructors
105  * @tc.desc      : 1.Constructors
106  */
107 HWTEST_F(WantAgentInfoTest, WantAgentInfo_0200, Function | MediumTest | Level1)
108 {
109     int requestCode = 10;
110     WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
111     std::vector<WantAgentConstant::Flags> flags;
112     WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
113     flags.emplace_back(flag);
114     std::vector<std::shared_ptr<Want>> wants;
115     std::shared_ptr<Want> want = std::make_shared<Want>();
116     ElementName element("device", "bundleName", "abilityName");
117     want->SetElement(element);
118     wants.emplace_back(want);
119     std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
120     bool value = true;
121     std::string key = "key";
122     extraInfo->SetParam(key, Boolean::Box(value));
123     std::shared_ptr<WantAgentInfo> wantAgentInfo =
124         std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
125     EXPECT_EQ(wantAgentInfo->GetRequestCode(), requestCode);
126     EXPECT_EQ(wantAgentInfo->GetOperationType(), operationType);
127     EXPECT_EQ(wantAgentInfo->GetFlags().at(0), flag);
128     EXPECT_NE(wantAgentInfo->GetWants().at(0), want);
129     EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetBundleName(), want->GetElement().GetBundleName());
130     EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetAbilityName(), want->GetElement().GetAbilityName());
131     EXPECT_NE(wantAgentInfo->GetExtraInfo(), extraInfo);
132     EXPECT_EQ(Boolean::Unbox(IBoolean::Query(wantAgentInfo->GetExtraInfo()->GetParam(key))),
133         Boolean::Unbox(IBoolean::Query(extraInfo->GetParam(key))));
134 }
135 
136 /*
137  * @tc.number    : WantAgentInfo_0300
138  * @tc.name      : WantAgentInfo Constructors
139  * @tc.desc      : 1.Copy Constructors
140  */
141 HWTEST_F(WantAgentInfoTest, WantAgentInfo_0300, Function | MediumTest | Level1)
142 {
143     int requestCode = 10;
144     WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
145     std::vector<WantAgentConstant::Flags> flags;
146     WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
147     flags.emplace_back(flag);
148     std::vector<std::shared_ptr<Want>> wants;
149     std::shared_ptr<Want> want = std::make_shared<Want>();
150     ElementName element("device", "bundleName", "abilityName");
151     want->SetElement(element);
152     wants.emplace_back(want);
153     std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
154     bool value = true;
155     std::string key = "key";
156     extraInfo->SetParam(key, Boolean::Box(value));
157     std::shared_ptr<WantAgentInfo> wantAgentInfo =
158         std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
159     EXPECT_EQ(wantAgentInfo->GetRequestCode(), requestCode);
160     EXPECT_EQ(wantAgentInfo->GetOperationType(), operationType);
161     EXPECT_EQ(wantAgentInfo->GetFlags().at(0), flag);
162     EXPECT_NE(wantAgentInfo->GetWants().at(0), want);
163     EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetBundleName(), want->GetElement().GetBundleName());
164     EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetAbilityName(), want->GetElement().GetAbilityName());
165     EXPECT_NE(wantAgentInfo->GetExtraInfo(), extraInfo);
166     EXPECT_EQ(Boolean::Unbox(IBoolean::Query(wantAgentInfo->GetExtraInfo()->GetParam(key))),
167         Boolean::Unbox(IBoolean::Query(extraInfo->GetParam(key))));
168 
169     WantAgentInfo wantAgentInfo1(wantAgentInfo);
170     EXPECT_EQ(wantAgentInfo->GetRequestCode(), wantAgentInfo1.GetRequestCode());
171     EXPECT_EQ(wantAgentInfo->GetOperationType(), wantAgentInfo1.GetOperationType());
172     EXPECT_EQ(wantAgentInfo->GetFlags().at(0), wantAgentInfo1.GetFlags().at(0));
173     EXPECT_NE(wantAgentInfo->GetWants().at(0), wantAgentInfo1.GetWants().at(0));
174     EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetBundleName(),
175         wantAgentInfo1.GetWants().at(0)->GetElement().GetBundleName());
176     EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetAbilityName(),
177         wantAgentInfo1.GetWants().at(0)->GetElement().GetAbilityName());
178     EXPECT_NE(wantAgentInfo->GetExtraInfo(), wantAgentInfo1.GetExtraInfo());
179     EXPECT_EQ(Boolean::Unbox(IBoolean::Query(wantAgentInfo->GetExtraInfo()->GetParam(key))),
180         Boolean::Unbox(IBoolean::Query(wantAgentInfo1.GetExtraInfo()->GetParam(key))));
181 }
182 
183 /*
184  * @tc.number    : WantAgentInfo_0400
185  * @tc.name      : WantAgentInfo GetRequestCode
186  * @tc.desc      : 1.GetRequestCode
187  */
188 HWTEST_F(WantAgentInfoTest, WantAgentInfo_0400, Function | MediumTest | Level1)
189 {
190     int requestCode = 10;
191     WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
192     std::vector<WantAgentConstant::Flags> flags;
193     WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
194     flags.emplace_back(flag);
195     std::vector<std::shared_ptr<Want>> wants;
196     std::shared_ptr<Want> want = std::make_shared<Want>();
197     ElementName element("device", "bundleName", "abilityName");
198     want->SetElement(element);
199     wants.emplace_back(want);
200     std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
201     bool value = true;
202     std::string key = "key";
203     extraInfo->SetParam(key, Boolean::Box(value));
204     std::shared_ptr<WantAgentInfo> wantAgentInfo =
205         std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
206     EXPECT_EQ(wantAgentInfo->GetRequestCode(), requestCode);
207 }
208 
209 /*
210  * @tc.number    : WantAgentInfo_0500
211  * @tc.name      : WantAgentInfo GetOperationType
212  * @tc.desc      : 1.GetOperationType
213  */
214 HWTEST_F(WantAgentInfoTest, WantAgentInfo_0500, Function | MediumTest | Level1)
215 {
216     int requestCode = 10;
217     WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
218     std::vector<WantAgentConstant::Flags> flags;
219     WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
220     flags.emplace_back(flag);
221     std::vector<std::shared_ptr<Want>> wants;
222     std::shared_ptr<Want> want = std::make_shared<Want>();
223     ElementName element("device", "bundleName", "abilityName");
224     want->SetElement(element);
225     wants.emplace_back(want);
226     std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
227     bool value = true;
228     std::string key = "key";
229     extraInfo->SetParam(key, Boolean::Box(value));
230     std::shared_ptr<WantAgentInfo> wantAgentInfo =
231         std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
232     EXPECT_EQ(wantAgentInfo->GetOperationType(), operationType);
233 }
234 
235 /*
236  * @tc.number    : WantAgentInfo_0600
237  * @tc.name      : WantAgentInfo GetFlags
238  * @tc.desc      : 1.GetFlags
239  */
240 HWTEST_F(WantAgentInfoTest, WantAgentInfo_0600, Function | MediumTest | Level1)
241 {
242     int requestCode = 10;
243     WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
244     std::vector<WantAgentConstant::Flags> flags;
245     WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
246     flags.emplace_back(flag);
247     std::vector<std::shared_ptr<Want>> wants;
248     std::shared_ptr<Want> want = std::make_shared<Want>();
249     ElementName element("device", "bundleName", "abilityName");
250     want->SetElement(element);
251     wants.emplace_back(want);
252     std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
253     bool value = true;
254     std::string key = "key";
255     extraInfo->SetParam(key, Boolean::Box(value));
256     std::shared_ptr<WantAgentInfo> wantAgentInfo =
257         std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
258     EXPECT_EQ(wantAgentInfo->GetFlags().at(0), flag);
259 }
260 
261 /*
262  * @tc.number    : WantAgentInfo_0700
263  * @tc.name      : WantAgentInfo GetWants
264  * @tc.desc      : 1.GetWants
265  */
266 HWTEST_F(WantAgentInfoTest, WantAgentInfo_0700, Function | MediumTest | Level1)
267 {
268     int requestCode = 10;
269     WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
270     std::vector<WantAgentConstant::Flags> flags;
271     WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
272     flags.emplace_back(flag);
273     std::vector<std::shared_ptr<Want>> wants;
274     std::shared_ptr<Want> want = std::make_shared<Want>();
275     ElementName element("device", "bundleName", "abilityName");
276     want->SetElement(element);
277     wants.emplace_back(want);
278     std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
279     bool value = true;
280     std::string key = "key";
281     extraInfo->SetParam(key, Boolean::Box(value));
282     std::shared_ptr<WantAgentInfo> wantAgentInfo =
283         std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
284     EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetBundleName(), want->GetElement().GetBundleName());
285     EXPECT_EQ(wantAgentInfo->GetWants().at(0)->GetElement().GetAbilityName(), want->GetElement().GetAbilityName());
286 }
287 
288 /*
289  * @tc.number    : WantAgentInfo_0800
290  * @tc.name      : WantAgentInfo GetWants
291  * @tc.desc      : 1.GetWants
292  */
293 HWTEST_F(WantAgentInfoTest, WantAgentInfo_0800, Function | MediumTest | Level1)
294 {
295     int requestCode = 10;
296     WantAgentConstant::OperationType operationType = WantAgentConstant::OperationType::START_ABILITY;
297     std::vector<WantAgentConstant::Flags> flags;
298     WantAgentConstant::Flags flag = WantAgentConstant::Flags::CONSTANT_FLAG;
299     flags.emplace_back(flag);
300     std::vector<std::shared_ptr<Want>> wants;
301     std::shared_ptr<Want> want = std::make_shared<Want>();
302     ElementName element("device", "bundleName", "abilityName");
303     want->SetElement(element);
304     wants.emplace_back(want);
305     std::shared_ptr<WantParams> extraInfo = std::make_shared<WantParams>();
306     bool value = true;
307     std::string key = "key";
308     extraInfo->SetParam(key, Boolean::Box(value));
309     std::shared_ptr<WantAgentInfo> wantAgentInfo =
310         std::make_shared<WantAgentInfo>(requestCode, operationType, flags, wants, extraInfo);
311     EXPECT_NE(wantAgentInfo->GetExtraInfo(), extraInfo);
312     EXPECT_EQ(Boolean::Unbox(IBoolean::Query(wantAgentInfo->GetExtraInfo()->GetParam(key))),
313         Boolean::Unbox(IBoolean::Query(extraInfo->GetParam(key))));
314 }
315 }  // namespace OHOS::AbilityRuntime::WantAgent
316