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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include "data_sync_manager.h"
20 #include "dfs_error.h"
21 #include "dk_record.h"
22 #include "gallery_data_syncer.h"
23 #include "sdk_helper.h"
24
25 /* template link */
26 namespace OHOS::FileManagement::CloudSync {
27 template<typename T, typename RET, typename... ARGS>
AsyncCallback(RET (T::* f)(ARGS...))28 std::function<RET(ARGS...)> DataSyncer::AsyncCallback(RET(T::*f)(ARGS...))
29 {
30 return taskRunner_->AsyncCallback<DataSyncer>(f, this);
31 }
32 }
33
34 namespace OHOS::FileManagement::CloudSync::Test {
35 using namespace testing;
36 using namespace testing::ext;
37 using namespace std;
38
39 class SdkHelperTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp();
44 void TearDown();
45 shared_ptr<DataSyncManager> dataSyncManager_;
46 shared_ptr<DataSyncer> dataSyncer_;
47 shared_ptr<SdkHelper> sdkHelper_;
48 shared_ptr<FileDataHandler> handler_;
49 };
SetUpTestCase(void)50 void SdkHelperTest::SetUpTestCase(void)
51 {
52 GTEST_LOG_(INFO) << "SetUpTestCase";
53 }
54
TearDownTestCase(void)55 void SdkHelperTest::TearDownTestCase(void)
56 {
57 GTEST_LOG_(INFO) << "TearDownTestCase";
58 }
59
60 #undef LOGI
LOGI(string content)61 void LOGI(string content)
62 {
63 GTEST_LOG_(INFO) << content;
64 }
65
SetUp(void)66 void SdkHelperTest::SetUp(void)
67 {
68 GTEST_LOG_(INFO) << "SetUp";
69 dataSyncManager_ = make_shared<DataSyncManager>();
70 int32_t userId = 100;
71 string bundleName = "com.ohos.photos";
72 dataSyncer_ = dataSyncManager_->GetDataSyncer(bundleName, userId);
73 shared_ptr<GalleryDataSyncer> galleryDataSyncer_ = static_pointer_cast<GalleryDataSyncer>(dataSyncer_);
74 /* init handler */
75 galleryDataSyncer_->GetHandler();
76 handler_ = galleryDataSyncer_->fileHandler_;
77 sdkHelper_ = std::make_shared<SdkHelper>();
78 }
79
TearDown(void)80 void SdkHelperTest::TearDown(void)
81 {
82 GTEST_LOG_(INFO) << "TearDown";
83 }
84
85 /**
86 * @tc.name: FetchRecordsErrorTest
87 * @tc.desc: Verify the FetchRecords function
88 * @tc.type: FUNC
89 * @tc.require: I6JPKG
90 */
91 HWTEST_F(SdkHelperTest, FetchRecordsErrorTest, TestSize.Level1)
92 {
93 shared_ptr<TaskContext> context = make_shared<TaskContext>(handler_);
94 auto callback = dataSyncer_->AsyncCallback(&DataSyncer::OnFetchRecords);
95 ASSERT_NE(callback, nullptr);
96 FetchCondition cond;
97 handler_->GetFetchCondition(cond);
98 int32_t ret = sdkHelper_->FetchRecords(context, cond, "nextCursor", callback);
99 EXPECT_EQ(E_CLOUD_SDK, ret);
100 }
101
102 /**
103 * @tc.name: FetchDatabaseChangesErrorTest
104 * @tc.desc: Verify the FetchDatabaseChanges function
105 * @tc.type: FUNC
106 * @tc.require: I6JPKG
107 */
108 HWTEST_F(SdkHelperTest, FetchDatabaseChangesErrorTest, TestSize.Level1)
109 {
110 shared_ptr<TaskContext> context = make_shared<TaskContext>(handler_);
111 auto callback = dataSyncer_->AsyncCallback(&DataSyncer::OnFetchDatabaseChanges);
112 ASSERT_NE(callback, nullptr);
113 FetchCondition cond;
114 handler_->GetFetchCondition(cond);
115 int32_t ret = sdkHelper_->FetchDatabaseChanges(context, cond, "nextCursor", callback);
116 EXPECT_EQ(E_CLOUD_SDK, ret);
117 }
118
119 /**
120 * @tc.name: CreateRecordsErrorTest
121 * @tc.desc: Verify the CreateRecords function
122 * @tc.type: FUNC
123 * @tc.require: I6JPKG
124 */
125 HWTEST_F(SdkHelperTest, CreateRecordsErrorTest, TestSize.Level1)
126 {
127 shared_ptr<TaskContext> context = make_shared<TaskContext>(handler_);
128 auto handler = context->GetHandler();
129 ASSERT_NE(handler, nullptr);
130 vector<DriveKit::DKRecord> records;
131 int32_t ret = handler->GetCreatedRecords(records);
132 EXPECT_EQ(E_OK, ret);
133 auto callback = dataSyncer_->AsyncCallback(&DataSyncer::OnCreateRecords);
134 ASSERT_NE(callback, nullptr);
135 ret = sdkHelper_->CreateRecords(context, records, callback);
136 EXPECT_EQ(E_CLOUD_SDK, ret);
137 }
138
139 /**
140 * @tc.name: DeleteRecordsErrorTest
141 * @tc.desc: Verify the DeleteRecords function
142 * @tc.type: FUNC
143 * @tc.require: I6JPKG
144 */
145 HWTEST_F(SdkHelperTest, DeleteRecordsErrorTest, TestSize.Level1)
146 {
147 shared_ptr<TaskContext> context = make_shared<TaskContext>(handler_);
148 auto handler = context->GetHandler();
149 ASSERT_NE(handler, nullptr);
150 vector<DriveKit::DKRecord> records;
151 int32_t ret = handler->GetDeletedRecords(records);
152 EXPECT_EQ(E_STOP, ret);
153 auto callback = dataSyncer_->AsyncCallback(&DataSyncer::OnDeleteRecords);
154 ASSERT_NE(callback, nullptr);
155 ret = sdkHelper_->DeleteRecords(context, records, callback);
156 EXPECT_EQ(E_CLOUD_SDK, ret);
157 }
158
159 /**
160 * @tc.name: ModifyRecordsErrorTest
161 * @tc.desc: Verify the ModifyRecords function
162 * @tc.type: FUNC
163 * @tc.require: I6JPKG
164 */
165 HWTEST_F(SdkHelperTest, ModifyRecordsErrorTest, TestSize.Level1)
166 {
167 shared_ptr<TaskContext> context = make_shared<TaskContext>(handler_);
168 auto handler = context->GetHandler();
169 ASSERT_NE(handler, nullptr);
170 vector<DriveKit::DKRecord> records;
171 int32_t ret = handler->GetMetaModifiedRecords(records);
172 EXPECT_EQ(E_STOP, ret);
173 auto callback = dataSyncer_->AsyncCallback(&DataSyncer::OnModifyMdirtyRecords);
174 ASSERT_NE(callback, nullptr);
175 ret = sdkHelper_->ModifyRecords(context, records, callback);
176 EXPECT_EQ(E_CLOUD_SDK, ret);
177 }
178
179 /**
180 * @tc.name: GetAssetReadSessionErrorTest
181 * @tc.desc: Verify the GetAssetReadSession function
182 * @tc.type: FUNC
183 * @tc.require: I6JPKG
184 */
185 HWTEST_F(SdkHelperTest, GetAssetReadSessionErrorTest, TestSize.Level1)
186 {
187 DriveKit::DKRecordType recordType = "SdkHelperTest";
188 DriveKit::DKRecordId recordId = "GetAssetReadSessionTest";
189 DriveKit::DKFieldKey assetKey = "test";
190 DriveKit::DKAssetPath assetPath = "/data/file";
191 auto ret = sdkHelper_->GetAssetReadSession(recordType, recordId, assetKey, assetPath);
192 EXPECT_EQ(ret, nullptr);
193 }
194 } // namespace OHOS::FileManagement::CloudSync::Test