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 "iremote_proxy.h"
18 #include "want_sender_stub_impl_mock.h"
19
20 using namespace testing::ext;
21 using namespace testing;
22
23 namespace OHOS {
24 namespace AAFwk {
25 class WantSenderStubTest : public testing::Test {
26 public:
27 static void SetUpTestCase(void);
28 static void TearDownTestCase(void);
29 void SetUp();
30 void TearDown();
31 void WriteInterfaceToken(MessageParcel& data);
32 sptr<WantSenderStubImplMock> stub_{ nullptr };
33 };
34
SetUpTestCase(void)35 void WantSenderStubTest::SetUpTestCase(void)
36 {}
TearDownTestCase(void)37 void WantSenderStubTest::TearDownTestCase(void)
38 {}
TearDown()39 void WantSenderStubTest::TearDown()
40 {}
41
SetUp()42 void WantSenderStubTest::SetUp()
43 {
44 stub_ = new WantSenderStubImplMock();
45 }
46
WriteInterfaceToken(MessageParcel & data)47 void WantSenderStubTest::WriteInterfaceToken(MessageParcel& data)
48 {
49 data.WriteInterfaceToken(WantSenderStubImplMock::GetDescriptor());
50 }
51
52 /*
53 * Feature: WantSenderStub
54 * Function: OnRemoteRequest
55 * SubFunction: NA
56 * FunctionPoints: WantSenderStub OnRemoteRequest
57 * EnvConditions: The code which not exist
58 * CaseDescription: Verify that on remote request is abnormal
59 */
60 HWTEST_F(WantSenderStubTest, WantSenderStubTest_OnRemoteRequest_001, TestSize.Level1)
61 {
62 MessageParcel data;
63 MessageParcel reply;
64 MessageOption option;
65
66 WriteInterfaceToken(data);
67 int res = stub_->OnRemoteRequest(10000, data, reply, option);
68
69 EXPECT_EQ(res, IPC_STUB_UNKNOW_TRANS_ERR);
70 }
71
72 /*
73 * Feature: WantSenderStub
74 * Function: OnRemoteRequest
75 * SubFunction: NA
76 * FunctionPoints: WantSenderStub OnRemoteRequest
77 * EnvConditions: Description abnormal
78 * CaseDescription: Verify that on remote request is abnormal
79 */
80 HWTEST_F(WantSenderStubTest, WantSenderStubTest_OnRemoteRequest_002, TestSize.Level1)
81 {
82 MessageParcel data;
83 MessageParcel reply;
84 MessageOption option;
85
86 int res = stub_->OnRemoteRequest(IWantSender::WANT_SENDER_SEND, data, reply, option);
87
88 EXPECT_EQ(res, ERR_INVALID_STATE);
89 }
90
91 /*
92 * Feature: WantSenderStub
93 * Function: OnRemoteRequest
94 * SubFunction: NA
95 * FunctionPoints: WantSenderStub OnRemoteRequest
96 * EnvConditions: Code is WANT_SENDER_SEND
97 * CaseDescription: Verify that on remote request is normal
98 */
99 HWTEST_F(WantSenderStubTest, WantSenderStubTest_OnRemoteRequest_003, TestSize.Level1)
100 {
101 MessageParcel data;
102 MessageParcel reply;
103 MessageOption option;
104
105 SenderInfo info;
106 WriteInterfaceToken(data);
107 data.WriteParcelable(&info);
108
109 int res = stub_->OnRemoteRequest(IWantSender::WANT_SENDER_SEND, data, reply, option);
110
111 EXPECT_EQ(res, NO_ERROR);
112 }
113
114 /*
115 * Feature: WantSenderStub
116 * Function: SendInner
117 * SubFunction: NA
118 * FunctionPoints: WantSenderStub SendInner
119 * EnvConditions: Invalid parameter
120 * CaseDescription: Verify the function SendInner request is abnormal.
121 */
122 HWTEST_F(WantSenderStubTest, WantSenderStubTest_SendInner_001, TestSize.Level1)
123 {
124 MessageParcel data;
125 MessageParcel reply;
126
127 int res = stub_->SendInner(data, reply);
128
129 EXPECT_EQ(res, ERR_INVALID_VALUE);
130 }
131
132 /*
133 * Feature: WantSenderStub
134 * Function: OnRemoteRequest
135 * SubFunction: NA
136 * FunctionPoints: WantSenderStub SendInner
137 * EnvConditions: Valid parameter
138 * CaseDescription: Verify the function SendInner request is normal.
139 */
140 HWTEST_F(WantSenderStubTest, WantSenderStubTest_SendInner_002, TestSize.Level1)
141 {
142 MessageParcel data;
143 MessageParcel reply;
144 MessageOption option;
145
146 SenderInfo info;
147 data.WriteParcelable(&info);
148
149 int res = stub_->SendInner(data, reply);
150
151 EXPECT_EQ(res, NO_ERROR);
152 }
153 } // namespace AAFwk
154 } // namespace OHOS
155