• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
18 #include "ohos/aafwk/content/operation.h"
19 #include "ohos/aafwk/content/operation_builder.h"
20 
21 using namespace testing::ext;
22 using namespace OHOS::AAFwk;
23 using OHOS::Parcel;
24 using Uri = OHOS::Uri;
25 class OperationBaseTest : public testing::Test {
26 public:
OperationBaseTest()27     OperationBaseTest()
28     {}
~OperationBaseTest()29     ~OperationBaseTest()
30     {}
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35 
36     std::shared_ptr<OperationBuilder> operationbuilder_ = nullptr;
37 };
38 
SetUpTestCase(void)39 void OperationBaseTest::SetUpTestCase(void)
40 {}
41 
TearDownTestCase(void)42 void OperationBaseTest::TearDownTestCase(void)
43 {}
44 
SetUp(void)45 void OperationBaseTest::SetUp(void)
46 {
47     operationbuilder_ = std::make_shared<OperationBuilder>();
48 }
49 
TearDown(void)50 void OperationBaseTest::TearDown(void)
51 {}
52 
53 /**
54  * @tc.number: AaFwk_Operation_GetAbilityName_0100
55  * @tc.name: WithAbilityName/GetAbilityName.
56  * @tc.desc: Verify the function when the input string contains special characters.
57  */
58 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetAbilityName_0100, Function | MediumTest | Level1)
59 {
60     std::string value = "enter";
61     GTEST_LOG_(INFO) << "AaFwk_Operation_GetAbilityName_0100 start";
62 
63     operationbuilder_->WithAbilityName(value);
64     std::shared_ptr<Operation> operation = operationbuilder_->build();
65     EXPECT_STREQ(value.c_str(), operation->GetAbilityName().c_str());
66 
67     GTEST_LOG_(INFO) << "AaFwk_Operation_GetAbilityName_0100 end";
68 }
69 
70 /**
71  * @tc.number: AaFwk_Operation_GetAbilityName_0200
72  * @tc.name: WithAbilityName/GetAbilityName.
73  * @tc.desc: Verify the function when the input string is empty.
74  */
75 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetAbilityName_0200, Function | MediumTest | Level3)
76 {
77     std::string value = "";
78     operationbuilder_->WithAbilityName(value);
79     std::shared_ptr<Operation> operation = operationbuilder_->build();
80 
81     EXPECT_STREQ(value.c_str(), operation->GetAbilityName().c_str());
82 }
83 
84 /**
85  * @tc.number:  AaFwk_Operation_GetBundleName_0100
86  * @tc.name: WithBundleName/GetBundleName
87  * @tc.desc: Verify the function when the input string contains special characters.
88  */
89 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetBundleName_0100, Function | MediumTest | Level1)
90 {
91     std::string value = "value";
92     operationbuilder_->WithBundleName(value);
93     std::shared_ptr<Operation> operation = operationbuilder_->build();
94     EXPECT_STREQ(value.c_str(), operation->GetBundleName().c_str());
95 }
96 
97 /**
98  * @tc.number: AaFwk_Operation_GetBundleName_0200
99  * @tc.name: WithBundleName/GetBundleName
100  * @tc.desc: Verify the function when the input string is empty.
101  */
102 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetBundleName_0200, Function | MediumTest | Level3)
103 {
104     std::string value = "";
105     operationbuilder_->WithBundleName(value);
106     std::shared_ptr<Operation> operation = operationbuilder_->build();
107     EXPECT_STREQ(value.c_str(), operation->GetBundleName().c_str());
108 }
109 
110 /**
111  * @tc.number: AaFwk_Operation_GetDeviceId_0100
112  * @tc.name: WithDeviceId/GetDeviceId
113  * @tc.desc: Verify the function when the input string contains special characters.
114  */
115 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetDeviceId_0100, Function | MediumTest | Level1)
116 {
117     std::string value = "value";
118     operationbuilder_->WithDeviceId(value);
119     std::shared_ptr<Operation> operation = operationbuilder_->build();
120     EXPECT_STREQ(value.c_str(), operation->GetDeviceId().c_str());
121 }
122 
123 /**
124  * @tc.number: AaFwk_Operation_GetDeviceId_0200
125  * @tc.name: WithDeviceId/GetDeviceId
126  * @tc.desc: Verify the function when the input string is empty.
127  */
128 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetDeviceId_0200, Function | MediumTest | Level3)
129 {
130     std::string value = "";
131     operationbuilder_->WithDeviceId(value);
132     std::shared_ptr<Operation> operation = operationbuilder_->build();
133     EXPECT_STREQ(value.c_str(), operation->GetDeviceId().c_str());
134 }
135 
136 /**
137  * @tc.number: AaFwk_Operation_GetAction_0100
138  * @tc.name: WithAction/GetAction
139  * @tc.desc: Verify the function when the input string contains special characters.
140  */
141 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetAction_0100, Function | MediumTest | Level1)
142 {
143     std::string value = "value";
144     operationbuilder_->WithAction(value);
145     std::shared_ptr<Operation> operation = operationbuilder_->build();
146     EXPECT_STREQ(value.c_str(), operation->GetAction().c_str());
147 }
148 
149 /**
150  * @tc.number: AaFwk_Operation_GetAction_0200
151  * @tc.name: WithAction/GetAction
152  * @tc.desc: Verify the function when the input string is empty.
153  */
154 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetAction_0200, Function | MediumTest | Level3)
155 {
156     std::string value = "";
157     operationbuilder_->WithAction(value);
158     std::shared_ptr<Operation> operation = operationbuilder_->build();
159     EXPECT_STREQ(value.c_str(), operation->GetAction().c_str());
160 }
161 
162 /**
163  * @tc.number: AaFwk_Operation_GetEntities_0100
164  * @tc.name: WithEntities/GetEntities
165  * @tc.desc: Verify the function when the input string contains special characters.
166  */
167 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetEntities_0100, Function | MediumTest | Level1)
168 {
169     std::vector<std::string> value;
170     value.push_back("string1");
171     operationbuilder_->WithEntities(value);
172     std::shared_ptr<Operation> operation = operationbuilder_->build();
173 
174     std::vector<std::string> revValue = operation->GetEntities();
175 
176     if (value.size() > 0 && revValue.size() > 0) {
177         EXPECT_STREQ(value.at(0).c_str(), operation->GetEntities().at(0).c_str());
178     } else {
179         EXPECT_EQ(true, revValue.size() > 0);
180     }
181 }
182 
183 /**
184  * @tc.number: AaFwk_Operation_GetEntities_0200
185  * @tc.name: WithEntities/GetEntities
186  * @tc.desc: Verify the function when the input string is empty.
187  */
188 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetEntities_0200, Function | MediumTest | Level3)
189 {
190     std::vector<std::string> value;
191     operationbuilder_->WithEntities(value);
192     std::shared_ptr<Operation> operation = operationbuilder_->build();
193     EXPECT_EQ(true, operation->GetEntities().size() == 0);
194 }
195 
196 /**
197  * @tc.number: AaFwk_Operation_GetFlags_0100
198  * @tc.name: WithFlags/GetFlags
199  * @tc.desc: Verify the function when the input string contains special characters.
200  */
201 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetFlags_0100, Function | MediumTest | Level1)
202 {
203     unsigned int value = 1;
204     operationbuilder_->WithFlags(value);
205     std::shared_ptr<Operation> operation = operationbuilder_->build();
206     EXPECT_EQ(value, operation->GetFlags());
207 }
208 
209 /**
210  * @tc.number: AaFwk_Operation_GetFlags_0200
211  * @tc.name: WithFlags/GetFlags
212  * @tc.desc: Verify the function when the input string is empty.
213  */
214 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetFlags_0200, Function | MediumTest | Level3)
215 {
216     unsigned int value = 0;
217     operationbuilder_->WithFlags(value);
218     std::shared_ptr<Operation> operation = operationbuilder_->build();
219     EXPECT_EQ(value, operation->GetFlags());
220 }
221 
222 /**
223  * @tc.number: AaFwk_Operation_GetUri_0100
224  * @tc.name: WithUri/GetUri
225  * @tc.desc: Verify the function when the input string contains special characters.
226  */
227 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetUri_0100, Function | MediumTest | Level1)
228 {
229     std::string value = "scheme://authority/path1/path2/path3?id = 1&name = mingming&old#fragment";
230     OHOS::Uri uri(value);
231     operationbuilder_->WithUri(uri);
232     std::shared_ptr<Operation> operation = operationbuilder_->build();
233 
234     EXPECT_EQ(uri, operation->GetUri());
235 }
236 
237 /**
238  * @tc.number: AaFwk_Operation_GetUri_0200
239  * @tc.name: WithUri/GetUri
240  * @tc.desc: Verify the function when the input string is empty.
241  */
242 HWTEST_F(OperationBaseTest, AaFwk_Operation_GetUri_0200, Function | MediumTest | Level3)
243 {
244     std::string value = "";
245     OHOS::Uri uri(value);
246     operationbuilder_->WithUri(uri);
247     std::shared_ptr<Operation> operation = operationbuilder_->build();
248     EXPECT_EQ(uri, operation->GetUri());
249 }
250 
251 /**
252  * @tc.number: AaFwk_Operation_build_0100
253  * @tc.name: build
254  * @tc.desc: Verify that the parameters are correct.
255  */
256 HWTEST_F(OperationBaseTest, AaFwk_Operation_build_0100, Function | MediumTest | Level1)
257 {
258     std::string value = "value";
259     OHOS::Uri uri(value);
260     std::vector<std::string> columns;
261     columns.push_back("string1");
262     operationbuilder_->WithUri(uri);
263     operationbuilder_->WithAction(value);
264     operationbuilder_->WithEntities(columns);
265     operationbuilder_->WithDeviceId(value);
266     operationbuilder_->WithBundleName(value);
267     operationbuilder_->WithAbilityName(value);
268 
269     std::shared_ptr<Operation> operation = operationbuilder_->build();
270 
271     EXPECT_EQ(uri, operation->GetUri());
272     EXPECT_STREQ(value.c_str(), operation->GetAction().c_str());
273 
274     std::vector<std::string> revValue = operation->GetEntities();
275 
276     if (columns.size() > 0 && revValue.size() > 0) {
277         EXPECT_STREQ(columns.at(0).c_str(), operation->GetEntities().at(0).c_str());
278     } else {
279         EXPECT_EQ(true, revValue.size() > 0);
280     }
281     EXPECT_STREQ(value.c_str(), operation->GetDeviceId().c_str());
282     EXPECT_STREQ(value.c_str(), operation->GetBundleName().c_str());
283     EXPECT_STREQ(value.c_str(), operation->GetAbilityName().c_str());
284 }
285 
286 /**
287  * @tc.number: AaFwk_Operation_Marshalling_0100
288  * @tc.name: Marshalling/Unmarshalling
289  * @tc.desc: Validation serialization.
290  */
291 HWTEST_F(OperationBaseTest, AaFwk_Operation_Marshalling_0100, Function | MediumTest | Level1)
292 {
293     std::string value = "value";
294     OHOS::Uri uri(value);
295     std::vector<std::string> columns;
296     columns.push_back("string1");
297     operationbuilder_->WithUri(uri);
298     operationbuilder_->WithAction(value);
299     operationbuilder_->WithEntities(columns);
300     operationbuilder_->WithDeviceId(value);
301     operationbuilder_->WithBundleName(value);
302     operationbuilder_->WithAbilityName(value);
303 
304     std::shared_ptr<Operation> operation = operationbuilder_->build();
305     Parcel in;
306     operation->Marshalling(in);
307 
308     Operation *pOperation = operation->Unmarshalling(in);
309     if (pOperation != nullptr) {
310         EXPECT_EQ(true, *pOperation == *(operation.get()));
311     } else {
312         EXPECT_EQ(true, pOperation != nullptr);
313     }
314 }
315 
316 /**
317  * @tc.number: AaFwk_Operation_Operator_0100
318  * @tc.name: Operator
319  * @tc.desc: Verify string overload.
320  */
321 HWTEST_F(OperationBaseTest, AaFwk_Operation_Operator_0100, Function | MediumTest | Level1)
322 {
323     Operation operation_;
324     std::string value = "value";
325     OHOS::Uri uri(value);
326     std::vector<std::string> columns;
327     columns.push_back("string1");
328     operationbuilder_->WithUri(uri);
329     operationbuilder_->WithAction(value);
330     operationbuilder_->WithEntities(columns);
331     operationbuilder_->WithDeviceId(value);
332     operationbuilder_->WithBundleName(value);
333     operationbuilder_->WithAbilityName(value);
334 
335     std::shared_ptr<Operation> operation = operationbuilder_->build();
336     operation_ = *(operation.get());
337 
338     EXPECT_EQ(true, operation_ == *(operation.get()));
339 }
340