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 #include "parcel.h"
18 #include "sender_info.h"
19 #include "wants_info.h"
20 #define private public
21 #define protected public
22 #include "want_sender_info.h"
23 #undef private
24 #undef protected
25 #include "want_receiver_stub.h"
26 
27 using namespace testing;
28 using namespace testing::ext;
29 using namespace OHOS::AppExecFwk;
30 using OHOS::AppExecFwk::ElementName;
31 
32 namespace OHOS {
33 namespace AAFwk {
34 #define SLEEP(milli) std::this_thread::sleep_for(std::chrono::seconds(milli))
35 namespace {}  // namespace
36 class WantSenderInfoTest : public testing::Test {
37 public:
38     static void SetUpTestCase();
39     static void TearDownTestCase();
40     void SetUp();
41     void TearDown();
42     static constexpr int TEST_WAIT_TIME = 100000;
43     class CancelReceiver : public AAFwk::WantReceiverStub {
44     public:
45         int performReceiveCount = 0;
46         void Send(const int32_t resultCode) override;
47         void PerformReceive(const AAFwk::Want& want, int resultCode, const std::string& data,
48             const AAFwk::WantParams& extras, bool serialized, bool sticky, int sendingUser) override;
49     };
50 
51 public:
52 };
53 
Send(const int32_t resultCode)54 void WantSenderInfoTest::CancelReceiver::Send(const int32_t resultCode)
55 {}
PerformReceive(const AAFwk::Want & want,int resultCode,const std::string & data,const AAFwk::WantParams & extras,bool serialized,bool sticky,int sendingUser)56 void WantSenderInfoTest::CancelReceiver::PerformReceive(const AAFwk::Want& want, int resultCode,
57     const std::string& data, const AAFwk::WantParams& extras, bool serialized, bool sticky, int sendingUser)
58 {}
59 
SetUpTestCase()60 void WantSenderInfoTest::SetUpTestCase()
61 {}
62 
TearDownTestCase()63 void WantSenderInfoTest::TearDownTestCase()
64 {}
65 
SetUp()66 void WantSenderInfoTest::SetUp()
67 {}
68 
TearDown()69 void WantSenderInfoTest::TearDown()
70 {}
71 
72 /*
73  * @tc.number    : WantSenderInfoTest_0100
74  * @tc.name      : Marshalling/UnMarshalling
75  * @tc.desc      : 1.Marshalling/UnMarshalling
76  */
77 HWTEST_F(WantSenderInfoTest, WantSenderInfoTest_0100, TestSize.Level1)
78 {
79     WantSenderInfo info;
80     info.type = 10;
81     info.bundleName = "bundleName";
82     info.resultWho = "abilityA";
83     info.requestCode = 100;
84     Want want;
85     ElementName element("device", "com.ix.hiMusic", "MusicSAbility");
86     want.SetElement(element);
87     WantsInfo wantInfo;
88     wantInfo.want = want;
89     wantInfo.resolvedTypes = "nihao";
90     info.allWants.emplace_back(wantInfo);
91     info.flags = 3;
92     info.userId = 99;
93     Parcel parcel;
94     info.Marshalling(parcel);
95     auto unInfo = WantSenderInfo::Unmarshalling(parcel);
96     EXPECT_NE(unInfo, nullptr);
97     if (!unInfo) {
98         return;
99     }
100     EXPECT_EQ(unInfo->type, 10);
101     EXPECT_EQ(unInfo->bundleName, "bundleName");
102     EXPECT_EQ(unInfo->resultWho, "abilityA");
103     EXPECT_EQ(static_cast<int>(unInfo->allWants.size()), 1);
104     EXPECT_EQ(unInfo->allWants.at(0).want.GetElement().GetBundleName(), "com.ix.hiMusic");
105     EXPECT_EQ(unInfo->allWants.at(0).want.GetElement().GetAbilityName(), "MusicSAbility");
106     EXPECT_EQ(unInfo->allWants.at(0).resolvedTypes, "nihao");
107     EXPECT_EQ(unInfo->requestCode, 100);
108     EXPECT_EQ(static_cast<int>(unInfo->flags), 3);
109     EXPECT_EQ(unInfo->userId, 99);
110     delete unInfo;
111 }
112 
113 /*
114  * @tc.number    : WantSenderInfoTest_0200
115  * @tc.name      : Marshalling/UnMarshalling
116  * @tc.desc      : 1.Marshalling/UnMarshalling
117  */
118 HWTEST_F(WantSenderInfoTest, WantSenderInfoTest_0200, TestSize.Level1)
119 {
120     WantSenderInfo info;
121 
122     for (int i = 0; i < 999; i++) {
123         Want want;
124         WantsInfo wantInfo;
125         wantInfo.want = want;
126         info.allWants.emplace_back(wantInfo);
127     }
128     EXPECT_EQ(info.allWants.size(), 999);
129     Parcel parcel;
130     info.Marshalling(parcel);
131     // succeed as the iteration time is under the limit
132     auto unInfo = WantSenderInfo::Unmarshalling(parcel);
133     EXPECT_NE(unInfo, nullptr);
134 }
135 
136 /*
137  * @tc.number    : WantSenderInfoTest_0300
138  * @tc.name      : Marshalling/UnMarshalling
139  * @tc.desc      : 1.Marshalling/UnMarshalling
140  */
141 HWTEST_F(WantSenderInfoTest, WantSenderInfoTest_0300, TestSize.Level1)
142 {
143     WantSenderInfo info;
144 
145     for (int i = 0; i < 1000; i++) {
146         Want want;
147         WantsInfo wantInfo;
148         wantInfo.want = want;
149         info.allWants.emplace_back(wantInfo);
150     }
151     EXPECT_EQ(info.allWants.size(), 1000);
152     Parcel parcel;
153     info.Marshalling(parcel);
154     // succeed as the iteration time equals the limit.
155     auto unInfo = WantSenderInfo::Unmarshalling(parcel);
156     EXPECT_NE(unInfo, nullptr);
157 }
158 
159 /*
160  * @tc.number    : WantSenderInfoTest_0400
161  * @tc.name      : Marshalling/UnMarshalling
162  * @tc.desc      : 1.Marshalling/UnMarshalling
163  */
164 HWTEST_F(WantSenderInfoTest, WantSenderInfoTest_0400, TestSize.Level1)
165 {
166     WantSenderInfo info;
167 
168     for (int i = 0; i < 1001; i++) {
169         Want want;
170         WantsInfo wantInfo;
171         wantInfo.want = want;
172         info.allWants.emplace_back(wantInfo);
173     }
174     EXPECT_EQ(info.allWants.size(), 1001);
175     Parcel parcel;
176     info.Marshalling(parcel);
177     // fail as the iteration time exceeds the limit.
178     auto unInfo = WantSenderInfo::Unmarshalling(parcel);
179     EXPECT_EQ(unInfo, nullptr);
180 }
181 }  // namespace AAFwk
182 }  // namespace OHOS
183