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 #define private public
19 #define protected public
20 #include "sender_info.h"
21 #undef private
22 #undef protected
23 #include "want_receiver_stub.h"
24
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS::AppExecFwk;
28 using OHOS::AppExecFwk::ElementName;
29
30 namespace OHOS {
31 namespace AAFwk {
32 #define SLEEP(milli) std::this_thread::sleep_for(std::chrono::seconds(milli))
33 class SenderInfoTest : public testing::Test {
34 public:
35 static void SetUpTestCase();
36 static void TearDownTestCase();
37 void SetUp();
38 void TearDown();
39 static constexpr int TEST_WAIT_TIME = 100000;
40 class CancelReceiver : public AAFwk::WantReceiverStub {
41 public:
42 int performReceiveCount = 0;
43 void Send(const int32_t resultCode) override;
44 void PerformReceive(const AAFwk::Want& want, int resultCode, const std::string& data,
45 const AAFwk::WantParams& extras, bool serialized, bool sticky, int sendingUser) override;
46 };
47 public:
48 };
49
Send(const int32_t resultCode)50 void SenderInfoTest::CancelReceiver::Send(const int32_t resultCode)
51 {}
PerformReceive(const AAFwk::Want & want,int resultCode,const std::string & data,const AAFwk::WantParams & extras,bool serialized,bool sticky,int sendingUser)52 void SenderInfoTest::CancelReceiver::PerformReceive(const AAFwk::Want& want, int resultCode, const std::string& data,
53 const AAFwk::WantParams& extras, bool serialized, bool sticky, int sendingUser)
54 {}
55
SetUpTestCase()56 void SenderInfoTest::SetUpTestCase()
57 {}
58
TearDownTestCase()59 void SenderInfoTest::TearDownTestCase()
60 {}
61
SetUp()62 void SenderInfoTest::SetUp()
63 {}
64
TearDown()65 void SenderInfoTest::TearDown()
66 {}
67
68 /*
69 * @tc.number : SenderInfoTest_0100
70 * @tc.name : Marshalling/UnMarshalling
71 * @tc.desc : 1.Marshalling/UnMarshalling
72 */
73 HWTEST_F(SenderInfoTest, SenderInfoTest_0100, TestSize.Level1)
74 {
75 SenderInfo info;
76 info.code = 10;
77 Want want;
78 ElementName element("device", "com.ix.hiMusic", "MusicSAbility");
79 want.SetElement(element);
80 info.want = want;
81 info.resolvedType = "nihao";
82 sptr<CancelReceiver> cance = new CancelReceiver();
83 cance->performReceiveCount = 10;
84 info.finishedReceiver = cance;
85 info.requiredPermission = "xiaoming";
86 MessageParcel parcel;
87 info.Marshalling(parcel);
88 auto unInfo = SenderInfo::Unmarshalling(parcel);
89 EXPECT_NE(unInfo, nullptr);
90 if (!unInfo) {
91 return;
92 }
93 EXPECT_EQ(unInfo->code, 10);
94 EXPECT_EQ(unInfo->want.GetElement().GetBundleName(), "com.ix.hiMusic");
95 EXPECT_EQ(unInfo->want.GetElement().GetAbilityName(), "MusicSAbility");
96 EXPECT_EQ(unInfo->resolvedType, "nihao");
97 auto callBackPtr = iface_cast<CancelReceiver>(unInfo->finishedReceiver->AsObject());
98 EXPECT_EQ(callBackPtr->performReceiveCount, 10);
99 EXPECT_EQ(unInfo->requiredPermission, "xiaoming");
100 delete unInfo;
101 }
102 } // namespace AAFwk
103 } // namespace OHOS
104