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 #define private public
17 #include "want_receiver_proxy.h"
18 #undef private
19 
20 #include <gtest/gtest.h>
21 #include "want_receiver_stub_mock.h"
22 
23 using namespace testing::ext;
24 using namespace testing;
25 using namespace OHOS::AppExecFwk;
26 
27 namespace OHOS {
28 namespace AAFwk {
29 class WantReceiverProxyTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35     std::shared_ptr<WantReceiverProxy> proxy_{ nullptr };
36     sptr<WantReceiverStubMock> mock_{ nullptr };
37 };
38 
SetUpTestCase(void)39 void WantReceiverProxyTest::SetUpTestCase(void)
40 {}
TearDownTestCase(void)41 void WantReceiverProxyTest::TearDownTestCase(void)
42 {}
TearDown()43 void WantReceiverProxyTest::TearDown()
44 {}
SetUp()45 void WantReceiverProxyTest::SetUp()
46 {
47     mock_ = new WantReceiverStubMock();
48     proxy_ = std::make_shared<WantReceiverProxy>(mock_);
49 }
50 
51 /*
52  * Feature: WantReceiverProxy
53  * Function: Send
54  * SubFunction: NA
55  * FunctionPoints: WantReceiverProxy Send
56  * EnvConditions: NA
57  * CaseDescription: Verify that the return value of Send is normal
58  */
59 HWTEST_F(WantReceiverProxyTest, WantReceiverProxyTest_001, TestSize.Level1)
60 {
61     EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
62         .Times(1)
63         .WillOnce(Invoke(mock_.GetRefPtr(), &WantReceiverStubMock::InvokeSendRequest));
64     int32_t resultCode = 0;
65     proxy_->Send(resultCode);
66 
67     EXPECT_EQ(IWantReceiver::WANT_RECEIVER_SEND, mock_->code_);
68 }
69 
70 /*
71  * Feature: WantReceiverProxy
72  * Function: Send
73  * SubFunction: NA
74  * FunctionPoints: WantReceiverProxy Send
75  * EnvConditions: NA
76  * CaseDescription: Verify that the return value of PerformReceive is normal
77  */
78 HWTEST_F(WantReceiverProxyTest, WantReceiverProxyTest_002, TestSize.Level1)
79 {
80     EXPECT_CALL(*mock_, SendRequest(_, _, _, _))
81         .Times(1)
82         .WillOnce(Invoke(mock_.GetRefPtr(), &WantReceiverStubMock::InvokeSendRequest));
83     const Want want;
84     const WantParams wantParams;
85     std::string data = "hello world";
86     proxy_->PerformReceive(want, 0, data, wantParams, true, true, 0);
87 
88     EXPECT_EQ(IWantReceiver::WANT_RECEIVER_PERFORM_RECEIVE, mock_->code_);
89 }
90 }  // namespace AAFwk
91 }  // namespace OHOS
92