1 /*
2 * Copyright (C) 2021-2022 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 "mediathumbnail_test.h"
17
18 #include <unistd.h>
19
20 #include "datashare_helper.h"
21 #include "get_self_permissions.h"
22 #include "iservice_registry.h"
23 #include "medialibrary_db_const.h"
24 #include "media_log.h"
25 #include "media_thumbnail_helper.h"
26 #include "scanner_utils.h"
27 #include "result_set_utils.h"
28
29 using namespace std;
30 using namespace OHOS::NativeRdb;
31 using namespace testing::ext;
32
33 namespace OHOS {
34 namespace Media {
35 constexpr int STORAGE_MANAGER_MANAGER_ID = 5003;
36 const int32_t PARAM1 = 1;
37 std::shared_ptr<MediaThumbnailHelper> g_thumbnailHelper = nullptr;
38 std::shared_ptr<DataShare::DataShareHelper> g_mediaDataShareHelper = nullptr;
39
CreateDataHelper(int32_t systemAbilityId)40 std::shared_ptr<DataShare::DataShareHelper> CreateDataHelper(int32_t systemAbilityId)
41 {
42 MEDIA_INFO_LOG("DataMedialibraryRdbHelper::CreateDataHelper ");
43 auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
44 if (saManager == nullptr) {
45 MEDIA_INFO_LOG("DataMedialibraryRdbHelper::CreateDataHelper Get system ability mgr failed.");
46 return nullptr;
47 }
48 auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
49 while (remoteObj == nullptr) {
50 MEDIA_INFO_LOG("DataMedialibraryRdbHelper::CreateDataHelper GetSystemAbility Service Failed.");
51 return nullptr;
52 }
53 return DataShare::DataShareHelper::Creator(remoteObj, MEDIALIBRARY_DATA_URI);
54 }
55
SetUpTestCase(void)56 void MediaThumbnailTest::SetUpTestCase(void)
57 {
58 MEDIA_INFO_LOG("SetUpTestCase invoked");
59 vector<string> perms;
60 perms.push_back("ohos.permission.READ_MEDIA");
61 perms.push_back("ohos.permission.WRITE_MEDIA");
62 perms.push_back("ohos.permission.FILE_ACCESS_MANAGER");
63 perms.push_back("ohos.permission.GET_BUNDLE_INFO_PRIVILEGED");
64 uint64_t tokenId = 0;
65 PermissionUtilsUnitTest::SetAccessTokenPermission("MediaThumbnailUnitTest", perms, tokenId);
66 ASSERT_TRUE(tokenId != 0);
67
68 g_thumbnailHelper = std::make_shared<MediaThumbnailHelper>();
69 g_mediaDataShareHelper = CreateDataHelper(STORAGE_MANAGER_MANAGER_ID);
70 if (g_mediaDataShareHelper == nullptr) {
71 MEDIA_ERR_LOG("SetUpTestCase::DataShareHelper == nullptr");
72 return;
73 }
74 }
75
TearDownTestCase(void)76 void MediaThumbnailTest::TearDownTestCase(void) { }
77
SetUp(void)78 void MediaThumbnailTest::SetUp(void) { }
79
TearDown(void)80 void MediaThumbnailTest::TearDown(void) { }
81
GetFileAsset(unique_ptr<FileAsset> & fileAsset,string displayName)82 bool GetFileAsset(unique_ptr<FileAsset> &fileAsset, string displayName)
83 {
84 if (g_mediaDataShareHelper == nullptr) {
85 MEDIA_ERR_LOG("GetFileAsset::DataShareHelper == nullptr");
86 return false;
87 }
88 vector<string> columns;
89 DataShare::DataSharePredicates predicates;
90 string selections = MEDIA_DATA_DB_NAME + " = ? ";
91 vector<string> selectionArgs = { displayName };
92 predicates.SetWhereClause(selections);
93 predicates.SetWhereArgs(selectionArgs);
94 Uri queryFileUri(MEDIALIBRARY_DATA_URI);
95 auto resultSet = g_mediaDataShareHelper->Query(queryFileUri, predicates, columns);
96 if (resultSet == nullptr) {
97 MEDIA_ERR_LOG("GetFileAsset::resultSet == nullptr");
98 return false;
99 }
100
101 // Create FetchResult object using the contents of resultSet
102 unique_ptr<FetchResult<FileAsset>> fetchFileResult = make_unique<FetchResult<FileAsset>>(move(resultSet));
103 if (fetchFileResult->GetCount() <= 0) {
104 MEDIA_ERR_LOG("GetFileAsset::GetCount <= 0");
105 return false;
106 }
107
108 fileAsset = fetchFileResult->GetFirstObject();
109 if (fileAsset == nullptr) {
110 MEDIA_ERR_LOG("GetFileAsset::fileAsset = nullptr.");
111 return false;
112 }
113 return true;
114 }
115
ParseThumbnailResult(int32_t file_id,std::string & thumbnailKey,std::string & lcdKey)116 static bool ParseThumbnailResult(int32_t file_id, std::string &thumbnailKey, std::string &lcdKey)
117 {
118 if (g_mediaDataShareHelper == nullptr) {
119 MEDIA_ERR_LOG("GetFileAsset::DataShareHelper == nullptr");
120 return false;
121 }
122 vector<string> columns;
123 DataShare::DataSharePredicates predicates;
124 string selections = MEDIA_DATA_DB_ID + " = ? ";
125 vector<string> selectionArgs = { to_string(file_id) };
126 predicates.SetWhereClause(selections);
127 predicates.SetWhereArgs(selectionArgs);
128 Uri queryFileUri(MEDIALIBRARY_DATA_URI);
129 auto resultSet = g_mediaDataShareHelper->Query(queryFileUri, predicates, columns);
130 if (resultSet == nullptr) {
131 MEDIA_ERR_LOG("GetFileAsset::resultSet == nullptr");
132 return false;
133 }
134 int rowCount = 0;
135 resultSet->GetRowCount(rowCount);
136 if (rowCount <= 0) {
137 return false;
138 }
139 if (resultSet->GoToFirstRow() != E_OK) {
140 return false;
141 }
142 thumbnailKey = get<string>(ResultSetUtils::GetValFromColumn(MEDIA_DATA_DB_THUMBNAIL, resultSet, TYPE_STRING));
143 lcdKey = get<string>(ResultSetUtils::GetValFromColumn(MEDIA_DATA_DB_LCD, resultSet, TYPE_STRING));
144 return true;
145 }
146
QueryThumbnail(const string & uri,Size size)147 shared_ptr<DataShare::DataShareResultSet> QueryThumbnail(const string &uri, Size size)
148 {
149 Uri queryUri(uri + "?" +
150 Media::MEDIA_OPERN_KEYWORD + "=" + Media::MEDIA_DATA_DB_THUMBNAIL + "&" +
151 Media::MEDIA_DATA_DB_WIDTH + "=" + to_string(size.width) + "&" +
152 Media::MEDIA_DATA_DB_HEIGHT + "=" + to_string(size.height));
153 if (g_mediaDataShareHelper == nullptr) {
154 MEDIA_ERR_LOG("CreateImageThumbnailTest_001:: DataShareHelper == nullptr");
155 return nullptr;
156 }
157 DataShare::DataSharePredicates predicates;
158 std::vector<std::string> columns;
159 MEDIA_DEBUG_LOG("CreateImageThumbnailTest_001:: Start gen thumbnail key");
160 return g_mediaDataShareHelper->Query(queryUri, predicates, columns);
161 }
162
163 HWTEST_F(MediaThumbnailTest, ResizeImage_Test_001, TestSize.Level0)
164 {
165 MEDIA_INFO_LOG("CreateImageThumbnailTest_001:: Start");
166 string displayName = "CreateImageThumbnailTest_001.jpg";
167 unique_ptr<FileAsset> fileAsset = nullptr;
168 if (!GetFileAsset(fileAsset, displayName)) {
169 return;
170 }
171
172 Size size = {
173 .width = 256, .height = 256
174 };
175 shared_ptr<DataShare::DataShareResultSet> resultSet = QueryThumbnail(fileAsset->GetUri(), size);
176 if (resultSet == nullptr) {
177 EXPECT_EQ(true, false);
178 return;
179 }
180 resultSet->GoToFirstRow();
181 vector<uint8_t> TEST_DATA;
182 unique_ptr<PixelMap> pixelMap;
183 MediaThumbnailHelper::ResizeImage(TEST_DATA, size, pixelMap);
184 TEST_DATA.push_back(1);
185 MediaThumbnailHelper::ResizeImage(TEST_DATA, size, pixelMap);
186 EXPECT_EQ(pixelMap, nullptr);
187
188 vector<uint8_t> image;
189 resultSet->GetBlob(PARAM1, image);
190 resultSet->Close();
191 Size TEST_SIZE = {
192 .width = PIXEL_MAP_MAX_RAM_SIZE, .height = PIXEL_MAP_MAX_RAM_SIZE
193 };
194 MediaThumbnailHelper::ResizeImage(image, TEST_SIZE, pixelMap);
195 EXPECT_EQ(pixelMap, nullptr);
196 }
197
198 HWTEST_F(MediaThumbnailTest, CreateImageThumbnailTest_001, TestSize.Level0)
199 {
200 MEDIA_INFO_LOG("CreateImageThumbnailTest_001:: Start");
201 string displayName = "CreateImageThumbnailTest_001.jpg";
202 unique_ptr<FileAsset> fileAsset = nullptr;
203 if (!GetFileAsset(fileAsset, displayName)) {
204 return;
205 }
206
207 Size size = {
208 .width = 256, .height = 256
209 };
210 shared_ptr<DataShare::DataShareResultSet> resultSet = QueryThumbnail(fileAsset->GetUri(), size);
211 if (resultSet == nullptr) {
212 EXPECT_EQ(true, false);
213 return;
214 }
215 resultSet->GoToFirstRow();
216 vector<uint8_t> image;
217 unique_ptr<PixelMap> pixelMap;
218 MediaThumbnailHelper::ResizeImage(image, size, pixelMap);
219 resultSet->GetBlob(PARAM1, image);
220 resultSet->Close();
221 MediaThumbnailHelper::ResizeImage(image, size, pixelMap);
222 EXPECT_NE(pixelMap, nullptr);
223 if (pixelMap) {
224 EXPECT_EQ(pixelMap->GetWidth(), size.width);
225 EXPECT_EQ(pixelMap->GetHeight(), size.height);
226 }
227
228 string thumbnailKey, lcdKey;
229 if (!ParseThumbnailResult(fileAsset->GetId(), thumbnailKey, lcdKey)) {
230 MEDIA_ERR_LOG("CreateImageThumbnailTest_001:: ParseThumbnailResult failed");
231 EXPECT_EQ(true, false);
232 return;
233 }
234 EXPECT_NE(thumbnailKey.empty(), true);
235 EXPECT_EQ(lcdKey.empty(), true);
236 }
237
238 HWTEST_F(MediaThumbnailTest, CreateImageLcdTest_001, TestSize.Level0)
239 {
240 MEDIA_INFO_LOG("CreateImageLcdTest_001:: Start");
241 string displayName = "CreateImageLcdTest_001.jpg";
242 unique_ptr<FileAsset> fileAsset = nullptr;
243 if (!GetFileAsset(fileAsset, displayName)) {
244 return;
245 }
246
247 Size size1 = {
248 .width = 256, .height = 256
249 };
250 shared_ptr<DataShare::DataShareResultSet> resultSet1 = QueryThumbnail(fileAsset->GetUri(), size1);
251 if (resultSet1 == nullptr) {
252 EXPECT_EQ(true, false);
253 return;
254 }
255 Size size2 = {
256 .width = 472, .height = 226
257 };
258 shared_ptr<DataShare::DataShareResultSet> resultSet = QueryThumbnail(fileAsset->GetUri(), size2);
259 if (resultSet == nullptr) {
260 EXPECT_EQ(true, false);
261 return;
262 }
263 resultSet->GoToFirstRow();
264 vector<uint8_t> image;
265 resultSet->GetBlob(PARAM1, image);
266 resultSet->Close();
267 unique_ptr<PixelMap> pixelMap;
268 MediaThumbnailHelper::ResizeImage(image, size2, pixelMap);
269 EXPECT_NE(pixelMap, nullptr);
270 if (pixelMap) {
271 EXPECT_EQ(pixelMap->GetWidth(), size2.width);
272 EXPECT_EQ(pixelMap->GetHeight(), size2.height);
273 }
274
275 string thumbnailKey, lcdKey;
276 if (!ParseThumbnailResult(fileAsset->GetId(), thumbnailKey, lcdKey)) {
277 MEDIA_ERR_LOG("CreateImageLcdTest_001:: ParseThumbnailResult failed");
278 EXPECT_EQ(true, false);
279 return;
280 }
281 EXPECT_NE(thumbnailKey.empty(), true);
282 EXPECT_NE(lcdKey.empty(), true);
283 }
284
285 HWTEST_F(MediaThumbnailTest, CreateAudioThumbnailTest_001, TestSize.Level0)
286 {
287 MEDIA_INFO_LOG("CreateAudioThumbnailTest_001:: Start");
288 string displayName = "CreateAudioThumbnailTest_001.mp3";
289 unique_ptr<FileAsset> fileAsset = nullptr;
290 if (!GetFileAsset(fileAsset, displayName)) {
291 return;
292 }
293
294 Size size = {
295 .width = 256, .height = 256
296 };
297 shared_ptr<DataShare::DataShareResultSet> resultSet = QueryThumbnail(fileAsset->GetUri(), size);
298 if (resultSet == nullptr) {
299 EXPECT_EQ(true, false);
300 return;
301 }
302 resultSet->GoToFirstRow();
303 vector<uint8_t> image;
304 resultSet->GetBlob(PARAM1, image);
305 resultSet->Close();
306 unique_ptr<PixelMap> pixelMap;
307 MediaThumbnailHelper::ResizeImage(image, size, pixelMap);
308 EXPECT_NE(pixelMap, nullptr);
309 if (pixelMap) {
310 EXPECT_EQ(pixelMap->GetWidth(), size.width);
311 EXPECT_EQ(pixelMap->GetHeight(), size.height);
312 }
313
314 string thumbnailKey, lcdKey;
315 if (!ParseThumbnailResult(fileAsset->GetId(), thumbnailKey, lcdKey)) {
316 MEDIA_ERR_LOG("CreateAudioThumbnailTest_001:: ParseThumbnailResult failed");
317 EXPECT_EQ(true, false);
318 return;
319 }
320 EXPECT_NE(thumbnailKey.empty(), true);
321 EXPECT_EQ(lcdKey.empty(), true);
322 }
323
324 HWTEST_F(MediaThumbnailTest, CreateVideoThumbnailTest_001, TestSize.Level0)
325 {
326 MEDIA_INFO_LOG("CreateVideoThumbnailTest_001:: Start");
327 string displayName = "CreateVideoThumbnailTest_001.mp4";
328 unique_ptr<FileAsset> fileAsset = nullptr;
329 if (!GetFileAsset(fileAsset, displayName)) {
330 return;
331 }
332
333 Size size = {
334 .width = 256, .height = 256
335 };
336 shared_ptr<DataShare::DataShareResultSet> resultSet = QueryThumbnail(fileAsset->GetUri(), size);
337 if (resultSet == nullptr) {
338 EXPECT_EQ(true, false);
339 return;
340 }
341 resultSet->GoToFirstRow();
342 vector<uint8_t> image;
343 resultSet->GetBlob(PARAM1, image);
344 resultSet->Close();
345 unique_ptr<PixelMap> pixelMap;
346 MediaThumbnailHelper::ResizeImage(image, size, pixelMap);
347 EXPECT_NE(pixelMap, nullptr);
348 if (pixelMap) {
349 EXPECT_EQ(pixelMap->GetWidth(), size.width);
350 EXPECT_EQ(pixelMap->GetHeight(), size.height);
351 }
352
353 string thumbnailKey, lcdKey;
354 if (!ParseThumbnailResult(fileAsset->GetId(), thumbnailKey, lcdKey)) {
355 MEDIA_ERR_LOG("CreateVideoThumbnailTest_001:: ParseThumbnailResult failed");
356 EXPECT_EQ(true, false);
357 return;
358 }
359 EXPECT_NE(thumbnailKey.empty(), true);
360 EXPECT_EQ(lcdKey.empty(), true);
361 }
362 } // namespace Media
363 } // namespace OHOS
364