• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #include "asset_send_callback_proxy.h"
16 
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19 
20 #include "iremote_stub.h"
21 
22 #include "asset_obj.h"
23 #include "asset_send_callback_mock.h"
24 #include "dfs_error.h"
25 #include "message_parcel_mock.h"
26 
27 namespace OHOS::Storage::DistributedFile::Test {
28 using namespace OHOS::FileManagement;
29 using namespace testing;
30 using namespace testing::ext;
31 using namespace std;
32 
33 class AssetSendCallbackProxyTest : public testing::Test {
34 public:
35     static void SetUpTestCase(void);
36     static void TearDownTestCase(void);
37     void SetUp();
38     void TearDown();
39 public:
40     static inline shared_ptr<AssetSendCallbackProxy> proxy_ = nullptr;
41     static inline sptr<IAssetSendCallbackMock> mock_ = nullptr;
42     static inline shared_ptr<MessageParcelMock> messageParcelMock_ = nullptr;
43 };
44 
SetUpTestCase(void)45 void AssetSendCallbackProxyTest::SetUpTestCase(void)
46 {
47     GTEST_LOG_(INFO) << "SetUpTestCase";
48     mock_ = sptr(new IAssetSendCallbackMock());
49     proxy_ = make_shared<AssetSendCallbackProxy>(mock_);
50     messageParcelMock_ = make_shared<MessageParcelMock>();
51     MessageParcelMock::messageParcel = messageParcelMock_;
52 }
53 
TearDownTestCase(void)54 void AssetSendCallbackProxyTest::TearDownTestCase(void)
55 {
56     GTEST_LOG_(INFO) << "TearDownTestCase";
57     mock_ = nullptr;
58     proxy_ = nullptr;
59     MessageParcelMock::messageParcel = nullptr;
60     messageParcelMock_ = nullptr;
61 }
62 
SetUp(void)63 void AssetSendCallbackProxyTest::SetUp(void)
64 {
65     GTEST_LOG_(INFO) << "SetUp";
66 }
67 
TearDown(void)68 void AssetSendCallbackProxyTest::TearDown(void)
69 {
70     GTEST_LOG_(INFO) << "TearDown";
71 }
72 
73 /**
74  * @tc.name: AssetSendCallbackProxyTest_OnSendResult_0100
75  * @tc.desc: The execution of the OnSendResult failed.
76  * @tc.type: FUNC
77  * @tc.require: I7TDJK
78  */
79 HWTEST_F(AssetSendCallbackProxyTest, AssetSendCallbackProxyTest_OnSendResult_0100, TestSize.Level1)
80 {
81     GTEST_LOG_(INFO) << "AssetSendCallbackProxyTest_OnSendResult_0100 Start";
82     sptr<AssetObj> assetObj (new (std::nothrow) AssetObj());
83     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(false));
84     ASSERT_NE(proxy_, nullptr);
85     auto ret = proxy_->OnSendResult(assetObj, 0);
86     EXPECT_EQ(ret, E_BROKEN_IPC);
87 
88     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
89     EXPECT_CALL(*messageParcelMock_, WriteParcelable(_)).WillOnce(Return(false));
90     ret = proxy_->OnSendResult(assetObj, 0);
91     EXPECT_EQ(ret, E_INVAL_ARG);
92 
93     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
94     EXPECT_CALL(*messageParcelMock_, WriteParcelable(_)).WillOnce(Return(true));
95     EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(false));
96     ret = proxy_->OnSendResult(assetObj, 0);
97     EXPECT_EQ(ret, E_INVAL_ARG);
98 
99     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
100     EXPECT_CALL(*messageParcelMock_, WriteParcelable(_)).WillOnce(Return(true));
101     EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true));
102     auto testProxy = make_shared<AssetSendCallbackProxy>(nullptr);
103     ret = testProxy->OnSendResult(assetObj, 0);
104     EXPECT_EQ(ret, E_BROKEN_IPC);
105 
106     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
107     EXPECT_CALL(*messageParcelMock_, WriteParcelable(_)).WillOnce(Return(true));
108     EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true));
109     EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).WillOnce(Return(E_INVAL_ARG));
110     ret = proxy_->OnSendResult(assetObj, 0);
111     EXPECT_EQ(ret, E_BROKEN_IPC);
112     GTEST_LOG_(INFO) << "AssetSendCallbackProxyTest_OnSendResult_0100 End";
113 }
114 
115 /**
116  * @tc.name: AssetSendCallbackProxyTest_OnSendResult_0200
117  * @tc.desc: The execution of the OnSendResult success.
118  * @tc.type: FUNC
119  * @tc.require: I7TDJK
120  */
121 HWTEST_F(AssetSendCallbackProxyTest, AssetSendCallbackProxyTest_OnSendResult_0200, TestSize.Level1)
122 {
123     GTEST_LOG_(INFO) << "AssetSendCallbackProxyTest_OnSendResult_0200 Start";
124     sptr<AssetObj> assetObj (new (std::nothrow) AssetObj());
125     EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
126     EXPECT_CALL(*messageParcelMock_, WriteParcelable(_)).WillOnce(Return(true));
127     EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true));
128     EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).WillOnce(Return(E_OK));
129     EXPECT_CALL(*messageParcelMock_, ReadInt32()).WillOnce(Return(E_OK));
130     auto ret = proxy_->OnSendResult(assetObj, 0);
131     EXPECT_EQ(ret, E_OK);
132     GTEST_LOG_(INFO) << "AssetSendCallbackProxyTest_OnSendResult_0200 End";
133 }
134 }