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 ASSERT_TRUE(assetObj != nullptr) << "assetObj assert failed!";
84 EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(false));
85 ASSERT_NE(proxy_, nullptr);
86 auto ret = proxy_->OnSendResult(assetObj, 0);
87 EXPECT_EQ(ret, E_BROKEN_IPC);
88
89 EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
90 EXPECT_CALL(*messageParcelMock_, WriteParcelable(_)).WillOnce(Return(false));
91 ret = proxy_->OnSendResult(assetObj, 0);
92 EXPECT_EQ(ret, E_INVAL_ARG);
93
94 EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
95 EXPECT_CALL(*messageParcelMock_, WriteParcelable(_)).WillOnce(Return(true));
96 EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(false));
97 ret = proxy_->OnSendResult(assetObj, 0);
98 EXPECT_EQ(ret, E_INVAL_ARG);
99
100 EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
101 EXPECT_CALL(*messageParcelMock_, WriteParcelable(_)).WillOnce(Return(true));
102 EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true));
103 auto testProxy = make_shared<AssetSendCallbackProxy>(nullptr);
104 ASSERT_NE(testProxy, nullptr);
105 ret = testProxy->OnSendResult(assetObj, 0);
106 EXPECT_EQ(ret, E_BROKEN_IPC);
107
108 EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
109 EXPECT_CALL(*messageParcelMock_, WriteParcelable(_)).WillOnce(Return(true));
110 EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true));
111 EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).WillOnce(Return(E_INVAL_ARG));
112 ret = proxy_->OnSendResult(assetObj, 0);
113 EXPECT_EQ(ret, E_BROKEN_IPC);
114 GTEST_LOG_(INFO) << "AssetSendCallbackProxyTest_OnSendResult_0100 End";
115 }
116
117 /**
118 * @tc.name: AssetSendCallbackProxyTest_OnSendResult_0200
119 * @tc.desc: The execution of the OnSendResult success.
120 * @tc.type: FUNC
121 * @tc.require: I7TDJK
122 */
123 HWTEST_F(AssetSendCallbackProxyTest, AssetSendCallbackProxyTest_OnSendResult_0200, TestSize.Level1)
124 {
125 GTEST_LOG_(INFO) << "AssetSendCallbackProxyTest_OnSendResult_0200 Start";
126 sptr<AssetObj> assetObj (new (std::nothrow) AssetObj());
127 ASSERT_TRUE(assetObj != nullptr) << "assetObj assert failed!";
128 EXPECT_CALL(*messageParcelMock_, WriteInterfaceToken(_)).WillOnce(Return(true));
129 EXPECT_CALL(*messageParcelMock_, WriteParcelable(_)).WillOnce(Return(true));
130 EXPECT_CALL(*messageParcelMock_, WriteInt32(_)).WillOnce(Return(true));
131 EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).WillOnce(Return(E_OK));
132 EXPECT_CALL(*messageParcelMock_, ReadInt32()).WillOnce(Return(E_OK));
133 ASSERT_NE(proxy_, nullptr);
134 auto ret = proxy_->OnSendResult(assetObj, 0);
135 EXPECT_EQ(ret, E_OK);
136 GTEST_LOG_(INFO) << "AssetSendCallbackProxyTest_OnSendResult_0200 End";
137 }
138 }