1 /*
2 * Copyright (c) 2023 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 <memory>
18 #define protect public
19 #include "cloud_sync_service_proxy.h"
20 #include "dfs_error.h"
21 #include "i_cloud_download_callback_mock.h"
22 #include "i_cloud_sync_service_mock.h"
23 #include "service_callback_mock.h"
24
25 #include "peer_holder.h"
26 #include "if_system_ability_manager.h"
27 #include "system_ability_manager_client_mock.h"
28 #include "if_system_ability_manager_mock.h"
29 #include "cloud_sync_service_proxy_lite.cpp"
30
31 #undef protect
32 namespace OHOS {
33 namespace FileManagement::CloudSync {
34 namespace Test {
35 using namespace testing::ext;
36 using namespace testing;
37 using namespace std;
38
39 static const int64_t FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID = 5204;
40
41 class CloudSyncServiceProxyLiteTest : public testing::Test {
42 public:
43 static void SetUpTestCase(void);
44 static void TearDownTestCase(void);
45 void SetUp();
46 void TearDown();
47 shared_ptr<CloudSyncServiceProxy> proxy_ = nullptr;
48 sptr<CloudSyncServiceMock> mock_ = nullptr;
49 sptr<CloudSyncCallbackMock> remote_ = nullptr;
50 sptr<CloudDownloadCallbackMock> download_ = nullptr;
51 static inline shared_ptr<SystemAbilityManagerClientMock> smc_ = nullptr;
52 static inline shared_ptr<ISystemAbilityManagerMock> samgr = make_shared<ISystemAbilityManagerMock>();
53 };
54
SetUpTestCase(void)55 void CloudSyncServiceProxyLiteTest::SetUpTestCase(void)
56 {
57 smc_ = make_shared<SystemAbilityManagerClientMock>();
58 SystemAbilityManagerClientMock::smc = smc_;
59 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = sptr<ISystemAbilityManager>(samgr.get());
60 std::cout << "SetUpTestCase" << std::endl;
61 }
62
TearDownTestCase(void)63 void CloudSyncServiceProxyLiteTest::TearDownTestCase(void)
64 {
65 SystemAbilityManagerClientMock::smc = nullptr;
66 smc_ = nullptr;
67 samgr = nullptr;
68 SystemAbilityManagerClient::GetInstance().systemAbilityManager_ = nullptr;
69 std::cout << "TearDownTestCase" << std::endl;
70 }
71
SetUp(void)72 void CloudSyncServiceProxyLiteTest::SetUp(void)
73 {
74 mock_ = sptr(new CloudSyncServiceMock());
75 proxy_ = make_shared<CloudSyncServiceProxy>(mock_);
76 remote_ = sptr(new CloudSyncCallbackMock());
77 download_ = sptr(new CloudDownloadCallbackMock());
78 std::cout << "SetUp" << std::endl;
79 }
80
TearDown(void)81 void CloudSyncServiceProxyLiteTest::TearDown(void)
82 {
83 proxy_ = nullptr;
84 mock_ = nullptr;
85 remote_ = nullptr;
86 std::cout << "TearDown" << std::endl;
87 }
88
89 /**
90 * @tc.name: TriggerSyncInnerInner001
91 * @tc.desc: Verify the TriggerSyncInner function.
92 * @tc.type: FUNC
93 * @tc.require: I6H5MH
94 */
95 HWTEST_F(CloudSyncServiceProxyLiteTest, TriggerSyncInner001, TestSize.Level1)
96 {
97 GTEST_LOG_(INFO) << "TriggerSyncInner Start";
98 string bundleName = "com.ohos.photos";
99 int32_t userId = 100;
100 EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(-1));
101
102 int result = proxy_->TriggerSyncInner(bundleName, userId);
103 EXPECT_EQ(result, E_BROKEN_IPC);
104 GTEST_LOG_(INFO) << "TriggerSyncInner End";
105 }
106
107 /**
108 * @tc.name: TriggerSyncInner002
109 * @tc.desc: Verify the TriggerSyncInner function.
110 * @tc.type: FUNC
111 * @tc.require: I6H5MH
112 */
113 HWTEST_F(CloudSyncServiceProxyLiteTest, TriggerSyncInner002, TestSize.Level1)
114 {
115 GTEST_LOG_(INFO) << "TriggerSyncInner Start";
116 string bundleName = "com.ohos.photos";
117 int32_t userId = 100;
118 EXPECT_CALL(*mock_, SendRequest(_, _, _, _)).Times(1).WillOnce(Return(E_OK));
119
120 int result = proxy_->TriggerSyncInner(bundleName, userId);
121 EXPECT_EQ(result, E_OK);
122 GTEST_LOG_(INFO) << "TriggerSyncInner End";
123 }
124
125 /**
126 * @tc.name: OnLoadSystemAbilityTest001
127 * @tc.desc: Verify the TriggerSyncInner function.
128 * @tc.type: FUNC
129 * @tc.require: I6H5MH
130 */
131 HWTEST_F(CloudSyncServiceProxyLiteTest, OnLoadSystemAbilityTest001, TestSize.Level1)
132 {
133 GTEST_LOG_(INFO) << "OnLoadSystemAbilityTest001 Start";
134
135 sptr<IRemoteObject> remoteObject = sptr(new CloudSyncServiceMock());
136 int32_t systemAbilityId = FILEMANAGEMENT_CLOUD_SYNC_SERVICE_SA_ID;
137 sptr<CloudSyncServiceProxy::ServiceProxyLoadCallback> callback =
138 new CloudSyncServiceProxy::ServiceProxyLoadCallback();
139 callback->OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject);
140 callback->OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject);
141 EXPECT_TRUE(callback->isLoadSuccess_.load());
142
143 GTEST_LOG_(INFO) << "OnLoadSystemAbilityTest001 End";
144 }
145
146 /**
147 * @tc.name: OnLoadSystemAbilityTest001
148 * @tc.desc: Verify the TriggerSyncInner function.
149 * @tc.type: FUNC
150 * @tc.require: I6H5MH
151 */
152 HWTEST_F(CloudSyncServiceProxyLiteTest, GetInstanceTest001, TestSize.Level1)
153 {
154 GTEST_LOG_(INFO) << "GetInstanceTest001 Start";
155 try {
156 auto CloudSyncServiceProxy = CloudSyncServiceProxy::GetInstance();
157 EXPECT_NE(CloudSyncServiceProxy, nullptr);
158 } catch (...) {
159 EXPECT_TRUE(false);
160 GTEST_LOG_(INFO) << "GetInstanceTest001 ERROR";
161 }
162 GTEST_LOG_(INFO) << "GetInstanceTest001 End";
163 }
164
165 /**
166 * @tc.name: GetInstanceTest002
167 * @tc.desc: Verify the TriggerSyncInner function.
168 * @tc.type: FUNC
169 * @tc.require: I6H5MH
170 */
171 HWTEST_F(CloudSyncServiceProxyLiteTest, GetInstanceTest002, TestSize.Level1)
172 {
173 GTEST_LOG_(INFO) << "GetInstanceTest002 Start";
174 EXPECT_CALL(*smc_, GetSystemAbilityManager()).WillOnce(Return(nullptr));
175 shared_ptr<CloudSyncServiceProxy> proxy_ = make_shared<CloudSyncServiceProxy>(nullptr);
176 proxy_->serviceProxy_ = nullptr;
177 auto ret = proxy_->GetInstance();
178 EXPECT_TRUE(ret == nullptr);
179 GTEST_LOG_(INFO) << "GetInstanceTest002 End";
180 }
181
182 /**
183 * @tc.name: GetInstanceTest003
184 * @tc.desc: Verify the TriggerSyncInner function.
185 * @tc.type: FUNC
186 * @tc.require: I6H5MH
187 */
188 HWTEST_F(CloudSyncServiceProxyLiteTest, GetInstanceTest003, TestSize.Level1)
189 {
190 GTEST_LOG_(INFO) << "GetInstanceTest003 Start";
191 try {
192 auto sysAbilityManager = sptr<ISystemAbilityManagerMock>(new ISystemAbilityManagerMock());
193 EXPECT_CALL(*smc_, GetSystemAbilityManager()).WillOnce(Return(sysAbilityManager));
194 EXPECT_CALL(*sysAbilityManager, LoadSystemAbility(An<int32_t>(), An<const sptr<ISystemAbilityLoadCallback>&>()))
195 .WillOnce(Return(E_PERMISSION));
196 shared_ptr<CloudSyncServiceProxy> proxy_ = make_shared<CloudSyncServiceProxy>(nullptr);
197 proxy_->serviceProxy_ = nullptr;
198 auto ret = proxy_->GetInstance();
199 EXPECT_TRUE(ret == nullptr);
200 } catch (...) {
201 GTEST_LOG_(ERROR) << "GetInstanceTest003 occurs an exception.";
202 }
203 GTEST_LOG_(INFO) << "GetInstanceTest003 End";
204 }
205
206 /**
207 * @tc.name: GetInstanceTest004
208 * @tc.desc: Verify the TriggerSyncInner function.
209 * @tc.type: FUNC
210 * @tc.require: I6H5MH
211 */
212 HWTEST_F(CloudSyncServiceProxyLiteTest, GetInstanceTest004, TestSize.Level1)
213 {
214 GTEST_LOG_(INFO) << "GetInstanceTest004 Start";
215 try {
216 auto sysAbilityManager = sptr<ISystemAbilityManagerMock>(new ISystemAbilityManagerMock());
217 EXPECT_CALL(*smc_, GetSystemAbilityManager()).WillOnce(Return(sysAbilityManager));
218 EXPECT_CALL(*sysAbilityManager, LoadSystemAbility(An<int32_t>(), An<const sptr<ISystemAbilityLoadCallback>&>()))
219 .WillOnce(Return(ERR_OK));
220 shared_ptr<CloudSyncServiceProxy> proxy_ = make_shared<CloudSyncServiceProxy>(nullptr);
221 proxy_->serviceProxy_ = nullptr;
222 auto ret = proxy_->GetInstance();
223 EXPECT_TRUE(ret == nullptr);
224 } catch (...) {
225 GTEST_LOG_(ERROR) << "GetInstanceTest004 occurs an exception.";
226 }
227 GTEST_LOG_(INFO) << "GetInstanceTest004 End";
228 }
229
230 } // namespace Test
231 } // namespace FileManagement::CloudSync {
232 } // namespace OHOS