• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022-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 "media_space_statistics_test.h"
17 #include "datashare_helper.h"
18 #include "fetch_result.h"
19 #include "file_asset.h"
20 #include "get_self_permissions.h"
21 #include "hilog/log.h"
22 #include "iservice_registry.h"
23 #include "medialibrary_db_const.h"
24 #include "medialibrary_errno.h"
25 #include "media_file_utils.h"
26 #include "media_library_manager.h"
27 #include "media_log.h"
28 #include "media_volume.h"
29 #include "scanner_utils.h"
30 #include "system_ability_definition.h"
31 
32 using namespace std;
33 using namespace OHOS;
34 using namespace testing::ext;
35 using namespace OHOS::NativeRdb;
36 using namespace OHOS::AppExecFwk;
37 
38 /**
39  * @FileName MediaSpaceStatisticsTest
40  * @Desc Media space statistics function test
41  *
42  */
43 namespace OHOS {
44 namespace Media {
45 std::shared_ptr<DataShare::DataShareHelper> sDataShareHelper_ = nullptr;
46 void CreateFile(std::string baseURI, std::string targetPath, std::string newName, MediaType mediaType,
47     const unsigned char fileContent[], const int len);
48 std::unique_ptr<FileAsset> GetFile(int mediaTypeId);
49 void ClearFile();
50 void ClearAllFile();
51 void CreateDataHelper(int32_t systemAbilityId);
52 
53 constexpr int STORAGE_MANAGER_MANAGER_ID = 5003;
54 int g_albumMediaType = MEDIA_TYPE_ALBUM;
55 const int SCAN_WAIT_TIME = 10;
56 int64_t g_oneImageSize = 0;
57 int64_t g_oneVideoSize = 0;
58 int64_t g_oneAudioSize = 0;
59 int64_t g_oneFileSize = 0;
60 
61 static const unsigned char FILE_CONTENT_TXT[] = {
62     0x49, 0x44, 0x33, 0x03, 0x20, 0x20, 0x20, 0x0c, 0x24
63 };
64 static const unsigned char FILE_CONTENT_JPG[] = {
65     0x49, 0x44, 0x33, 0x03, 0x20, 0x20, 0x20, 0x0c, 0x24, 0x5d, 0x54, 0x45, 0x4e, 0x43, 0x20, 0x20, 0x20, 0x0b,
66     0x20, 0x20, 0x20, 0x50
67 };
68 static const unsigned char FILE_CONTENT_MP3[] = {
69     0x49, 0x44, 0x33, 0x03, 0x20, 0x20, 0x20, 0x0c, 0x24, 0x5d, 0x54, 0x45, 0x4e, 0x43, 0x20, 0x20, 0x20, 0x0b, 0x20,
70     0x20, 0x20, 0x50, 0x72, 0x6f, 0x20, 0x54, 0x6f, 0x6f, 0x6c, 0x73, 0x20, 0x54, 0x58, 0x58, 0x58, 0x20, 0x20, 0x20,
71     0x27, 0x20, 0x20, 0x20, 0x6f, 0x72, 0x69, 0x67, 0x69, 0x6e, 0x61, 0x74, 0x6f, 0x72, 0x5f, 0x72, 0x65, 0x66, 0x65,
72     0x72, 0x65, 0x6e, 0x63, 0x65, 0x20, 0x21, 0x46, 0x6c, 0x4c, 0x55, 0x6b, 0x6e, 0x45, 0x6d, 0x52, 0x62, 0x61, 0x61,
73     0x61, 0x47, 0x6b, 0x20, 0x54, 0x59, 0x45, 0x52, 0x20, 0x20, 0x20, 0x06, 0x20, 0x20, 0x20, 0x32, 0x30, 0x31, 0x35,
74     0x20, 0x54, 0x44, 0x41, 0x54, 0x20, 0x20, 0x20, 0x06, 0x20, 0x20, 0x20, 0x32, 0x33, 0x31, 0x31, 0x20, 0x54, 0x58,
75     0x58, 0x58, 0x20, 0x20, 0x20, 0x17, 0x20, 0x20, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x5f, 0x72, 0x65, 0x66, 0x65, 0x72,
76     0x65, 0x6e, 0x63, 0x65, 0x20, 0x31, 0x36, 0x36, 0x31, 0x31, 0x39, 0x20, 0x54, 0x43, 0x4f, 0x4d, 0x20, 0x20, 0x20,
77     0x09, 0x20, 0x20, 0x01, 0xff, 0xfe, 0x4b, 0x6d, 0xd5, 0x8b, 0x20, 0x20, 0x54, 0x50, 0x45, 0x31, 0x20, 0x20, 0x20,
78     0x0f, 0x20, 0x20, 0x01, 0xff, 0xfe, 0x43, 0x51, 0x70, 0x65, 0x6e, 0x63, 0x4b, 0x6d, 0xd5, 0x8b, 0x20, 0x20, 0x54,
79     0x41, 0x4c, 0x42, 0x20, 0x20, 0x20, 0x07, 0x20, 0x20, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x61, 0x20, 0x54, 0x49, 0x54,
80     0x32, 0x20, 0x20, 0x20, 0x06, 0x20, 0x20, 0x20, 0x74, 0x65, 0x73, 0x74, 0x20, 0x54, 0x50, 0x45, 0x32, 0x20, 0x20,
81     0x20, 0x0c, 0x20, 0x20, 0x20, 0x6d, 0x65, 0x64, 0x69, 0x61, 0x5f, 0x74, 0x65, 0x73, 0x74, 0x20, 0x54, 0x58, 0x58,
82     0x58, 0x20, 0x20, 0x20, 0x0e, 0x20, 0x20, 0x20, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x20, 0x6d, 0x65, 0x64, 0x69,
83     0x61, 0x20, 0x54, 0x43, 0x4f, 0x4e, 0x20, 0x20, 0x20, 0x09, 0x20, 0x20, 0x20, 0x4c, 0x79, 0x72, 0x69, 0x63, 0x61,
84     0x6c, 0x20, 0x54, 0x53, 0x53, 0x45, 0x20, 0x20, 0x20, 0x0f, 0x20, 0x20, 0x20, 0x4c, 0x61
85 };
86 static const unsigned char FILE_CONTENT_MP4[] = {
87     0x20, 0x20, 0x20, 0x20, 0x66, 0x74, 0x79, 0x70, 0x69, 0x73, 0x6f, 0x6d, 0x20, 0x20, 0x02, 0x20, 0x69, 0x73, 0x6f,
88     0x6d, 0x69, 0x73, 0x6f, 0x32, 0x61, 0x76, 0x63, 0x31, 0x6d, 0x70, 0x34, 0x31, 0x20, 0x20, 0x20, 0x08, 0x66, 0x72,
89     0x65, 0x65, 0x20, 0x49, 0xdd, 0x01, 0x6d, 0x64, 0x61, 0x74, 0x20, 0x20, 0x02, 0xa0, 0x06, 0x05, 0xff, 0xff, 0x9c,
90     0xdc, 0x45, 0xe9, 0xbd, 0xe6, 0xd9, 0x48, 0xb7, 0x96, 0x2c, 0xd8, 0x20, 0xd9, 0x23, 0xee, 0xef, 0x78, 0x32, 0x36,
91     0x34, 0x20, 0x2d, 0x20, 0x63, 0x6f, 0x72, 0x65, 0x20, 0x31, 0x35, 0x39, 0x20, 0x2d, 0x20, 0x48, 0x2e, 0x32, 0x36,
92     0x34, 0x2f, 0x4d, 0x50, 0x45, 0x47, 0x2d, 0x34, 0x20, 0x41, 0x56, 0x43, 0x20, 0x63, 0x6f, 0x64, 0x65, 0x63, 0x20,
93     0x2d, 0x20, 0x43, 0x6f, 0x70, 0x79, 0x6c, 0x65, 0x66, 0x74, 0x20, 0x32, 0x30, 0x30, 0x33, 0x2d, 0x32, 0x30, 0x31,
94     0x39, 0x20, 0x2d, 0x20, 0x68, 0x74, 0x74, 0x70, 0x3a, 0x2f, 0x2f, 0x77, 0x77, 0x77, 0x2e, 0x76, 0x69, 0x64, 0x65,
95     0x6f, 0x6c, 0x61, 0x6e, 0x2e, 0x6f, 0x72, 0x67, 0x2f, 0x78, 0x32, 0x36, 0x34, 0x2e, 0x68, 0x74, 0x6d, 0x6c, 0x20,
96     0x2d, 0x20, 0x6f, 0x70, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x3a, 0x20, 0x63, 0x61, 0x62, 0x61, 0x63, 0x3d, 0x31, 0x20,
97     0x72, 0x65, 0x66, 0x3d, 0x33, 0x20, 0x64, 0x65, 0x62, 0x6c, 0x6f, 0x63, 0x6b, 0x3d, 0x31, 0x3a, 0x30, 0x3a, 0x30,
98     0x20, 0x61, 0x6e, 0x61, 0x6c, 0x79, 0x73, 0x65, 0x3d, 0x30, 0x78, 0x33, 0x3a, 0x30, 0x78, 0x31, 0x31, 0x33, 0x20,
99     0x6d, 0x65, 0x3d, 0x68, 0x65, 0x78, 0x20, 0x73, 0x75, 0x62, 0x6d, 0x65, 0x3d, 0x37, 0x20, 0x70, 0x73, 0x79, 0x3d,
100     0x31, 0x20, 0x70, 0x73, 0x79, 0x5f, 0x72, 0x64, 0x3d, 0x31, 0x2e, 0x30, 0x30, 0x3a, 0x30, 0x2e, 0x30, 0x30, 0x20,
101     0x6d, 0x69, 0x78, 0x65, 0x64, 0x5f, 0x72, 0x65, 0x66, 0x3d, 0x31, 0x20, 0x6d, 0x65, 0x5f, 0x72, 0x61, 0x6e, 0x67,
102     0x65, 0x3d, 0x31, 0x36, 0x20, 0x63, 0x68, 0x72, 0x6f, 0x6d, 0x61, 0x5f, 0x6d, 0x65, 0x3d, 0x31, 0x20, 0x74, 0x72,
103     0x65, 0x6c, 0x6c, 0x69, 0x73, 0x3d, 0x31, 0x20, 0x38, 0x78, 0x38, 0x64, 0x63, 0x74, 0x3d, 0x31, 0x20, 0x63, 0x71,
104     0x6d, 0x3d, 0x30, 0x20, 0x64, 0x65, 0x61, 0x64, 0x7a, 0x6f, 0x6e, 0x65, 0x3d, 0x32, 0x31, 0x2c, 0x31, 0x31, 0x20,
105     0x66, 0x61, 0x73, 0x74, 0x5f, 0x70, 0x73, 0x6b, 0x69, 0x70, 0x3d, 0x31, 0x20, 0x63, 0x68, 0x72, 0x6f, 0x6d, 0x61,
106     0x5f, 0x71, 0x70, 0x5f, 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x3d, 0x2d, 0x32, 0x20, 0x74, 0x68, 0x72, 0x65, 0x61,
107     0x64, 0x73, 0x3d, 0x36, 0x20, 0x6c, 0x6f, 0x6f, 0x6b, 0x61, 0x68, 0x65, 0x61, 0x64, 0x5f, 0x74, 0x68, 0x72, 0x65,
108     0x61, 0x64, 0x73, 0x3d, 0x31, 0x20, 0x73, 0x6c, 0x69, 0x63, 0x65, 0x64, 0x5f, 0x74, 0x68, 0x72, 0x65, 0x61, 0x64,
109     0x73, 0x3d, 0x30, 0x20, 0x6e, 0x72, 0x3d, 0x30, 0x20, 0x64, 0x65, 0x63, 0x69, 0x6d, 0x61, 0x74, 0x65, 0x3d, 0x31,
110     0x20, 0x69, 0x6e, 0x74, 0x65, 0x72, 0x6c, 0x61, 0x63, 0x65, 0x64, 0x3d, 0x30, 0x20, 0x62, 0x6c, 0x75, 0x72, 0x61,
111     0x79, 0x5f, 0x63, 0x6f, 0x6d, 0x70, 0x61, 0x74, 0x3d, 0x30, 0x20, 0x63, 0x6f, 0x6e, 0x73, 0x74, 0x72, 0x61, 0x69,
112     0x6e, 0x65, 0x64, 0x5f, 0x69, 0x6e, 0x74, 0x72, 0x61, 0x3d, 0x30, 0x20, 0x62, 0x66, 0x72, 0x61, 0x6d, 0x65, 0x73,
113     0x3d, 0x33, 0x20, 0x62, 0x5f, 0x70, 0x79, 0x72, 0x61, 0x6d, 0x69, 0x64, 0x3d, 0x32, 0x20, 0x62, 0x5f, 0x61, 0x64,
114     0x61, 0x70, 0x74, 0x3d, 0x31, 0x20, 0x62, 0x5f, 0x62, 0x69, 0x61, 0x73, 0x3d, 0x30, 0x20, 0x64, 0x69, 0x72, 0x65,
115     0x63, 0x74, 0x3d, 0x31, 0x20, 0x77, 0x65, 0x69, 0x67, 0x68, 0x74, 0x62, 0x3d, 0x31, 0x20, 0x6f, 0x70, 0x65, 0x6e,
116     0x5f, 0x67, 0x6f, 0x70, 0x3d, 0x30, 0x20, 0x77, 0x65, 0x69, 0x67, 0x68, 0x74, 0x70, 0x3d, 0x32, 0x20, 0x6b, 0x65,
117     0x79, 0x69, 0x6e, 0x74, 0x3d, 0x32, 0x35, 0x30, 0x20, 0x6b, 0x65, 0x79, 0x69, 0x6e, 0x74, 0x5f, 0x6d, 0x69, 0x6e,
118     0x3d, 0x32, 0x35, 0x20, 0x73, 0x63, 0x65, 0x6e, 0x65, 0x63, 0x75
119 };
120 
121 MediaLibraryManager* mediaLibraryManager = MediaLibraryManager::GetMediaLibraryManager();
122 
SetUpTestCase(void)123 void MediaSpaceStatisticsTest::SetUpTestCase(void)
124 {
125     // test QueryTotalSize when sDataShareHelper_ is nullptr
126     MediaVolume mediaVolume;
127     mediaLibraryManager->QueryTotalSize(mediaVolume);
128     mediaLibraryManager->CloseAsset("", 0);
129 
130     vector<string> perms;
131     perms.push_back("ohos.permission.READ_MEDIA");
132     perms.push_back("ohos.permission.WRITE_MEDIA");
133     perms.push_back("ohos.permission.READ_IMAGEVIDEO");
134     perms.push_back("ohos.permission.WRITE_IMAGEVIDEO");
135     perms.push_back("ohos.permission.MEDIA_LOCATION");
136     perms.push_back("ohos.permission.FILE_ACCESS_MANAGER");
137     perms.push_back("ohos.permission.GET_BUNDLE_INFO_PRIVILEGED");
138     uint64_t tokenId = 0;
139     PermissionUtilsUnitTest::SetAccessTokenPermission("MediaSpaceStatisticsUnitTest", perms, tokenId);
140     ASSERT_TRUE(tokenId != 0);
141 
142     MEDIA_INFO_LOG("MediaSpaceStatisticsTest::SetUpTestCase:: invoked");
143     CreateDataHelper(STORAGE_MANAGER_MANAGER_ID);
144     if (sDataShareHelper_ == nullptr) {
145         EXPECT_NE(sDataShareHelper_, nullptr);
146         return;
147     }
148 
149     // make sure board is empty
150     ClearAllFile();
151 
152     // create base file
153     CreateFile(MEDIALIBRARY_IMAGE_URI, "Pictures/", "MediaSpaceStatisticsTest.jpg", MEDIA_TYPE_IMAGE,
154         FILE_CONTENT_JPG, sizeof(FILE_CONTENT_JPG));
155     CreateFile(MEDIALIBRARY_VIDEO_URI, "Videos/", "MediaSpaceStatisticsTest.mp4", MEDIA_TYPE_VIDEO,
156         FILE_CONTENT_MP4, sizeof(FILE_CONTENT_MP4));
157     CreateFile(MEDIALIBRARY_AUDIO_URI, "Audios/", "MediaSpaceStatisticsTest.mp3", MEDIA_TYPE_AUDIO,
158         FILE_CONTENT_MP3, sizeof(FILE_CONTENT_MP3));
159     CreateFile(MEDIALIBRARY_FILE_URI, "Docs/Documents/", "MediaSpaceStatisticsTest.txt", MEDIA_TYPE_FILE,
160         FILE_CONTENT_TXT, sizeof(FILE_CONTENT_TXT));
161 
162     Uri scanUri(URI_SCANNER);
163     DataShareValuesBucket valuesBucket;
164     valuesBucket.Put(MEDIA_DATA_DB_FILE_PATH, ROOT_MEDIA_DIR);
165     auto ret = sDataShareHelper_->Insert(scanUri, valuesBucket);
166     EXPECT_EQ(ret, 0);
167     sleep(SCAN_WAIT_TIME);
168 
169     // get base size
170     g_oneImageSize = GetFile(MEDIA_TYPE_IMAGE)->GetSize();
171     g_oneVideoSize = GetFile(MEDIA_TYPE_VIDEO)->GetSize();
172     g_oneAudioSize = GetFile(MEDIA_TYPE_AUDIO)->GetSize();
173     MEDIA_INFO_LOG("MediaSpaceStatisticsTest::SetUpTestCase:: g_oneImageSize = %{public}lld",
174         (long long)g_oneImageSize);
175     MEDIA_INFO_LOG("MediaSpaceStatisticsTest::SetUpTestCase:: g_oneVideoSize = %{public}lld",
176         (long long)g_oneVideoSize);
177     MEDIA_INFO_LOG("MediaSpaceStatisticsTest::SetUpTestCase:: g_oneAudioSize = %{public}lld",
178         (long long)g_oneAudioSize);
179     MEDIA_INFO_LOG("MediaSpaceStatisticsTest::SetUpTestCase:: g_oneFileSize = %{public}lld",
180         (long long)g_oneFileSize);
181     MEDIA_INFO_LOG("MediaSpaceStatisticsTest::SetUpTestCase:: Finish");
182 }
183 
TearDownTestCase(void)184 void MediaSpaceStatisticsTest::TearDownTestCase(void)
185 {
186     MEDIA_ERR_LOG("TearDownTestCase start");
187     if (sDataShareHelper_ != nullptr) {
188         sDataShareHelper_->Release();
189     }
190     ClearAllFile();
191     MEDIA_INFO_LOG("TearDownTestCase end");
192 }
193 // SetUp:Execute before each test case
SetUp(void)194 void MediaSpaceStatisticsTest::SetUp(void) {}
195 
TearDown(void)196 void MediaSpaceStatisticsTest::TearDown(void) {}
197 
CreateDataHelper(int32_t systemAbilityId)198 void CreateDataHelper(int32_t systemAbilityId)
199 {
200     auto saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
201     if (saManager == nullptr) {
202         MEDIA_ERR_LOG("Get system ability mgr failed.");
203         return;
204     }
205     auto remoteObj = saManager->GetSystemAbility(systemAbilityId);
206     if (remoteObj == nullptr) {
207         MEDIA_ERR_LOG("GetSystemAbility Service Failed.");
208         return;
209     }
210     mediaLibraryManager->InitMediaLibraryManager(remoteObj);
211     MEDIA_INFO_LOG("InitMediaLibraryManager success~!");
212 
213     if (sDataShareHelper_ == nullptr) {
214         const sptr<IRemoteObject> &token = remoteObj;
215         sDataShareHelper_ = DataShare::DataShareHelper::Creator(token, MEDIALIBRARY_DATA_URI);
216     }
217 
218     mediaLibraryManager->InitMediaLibraryManager(remoteObj);
219 }
220 
GetFile(int mediaTypeId)221 std::unique_ptr<FileAsset> GetFile(int mediaTypeId)
222 {
223     if (sDataShareHelper_ == nullptr) {
224         return nullptr;
225     }
226     vector<string> columns;
227     DataSharePredicates predicates;
228     string prefix = MEDIA_DATA_DB_MEDIA_TYPE + " = " + std::to_string(mediaTypeId);
229     predicates.SetWhereClause(prefix);
230     Uri queryFileUri(MEDIALIBRARY_DATA_URI);
231     shared_ptr<DataShareResultSet> resultSet = nullptr;
232     resultSet = sDataShareHelper_->Query(queryFileUri, predicates, columns);
233     EXPECT_NE((resultSet == nullptr), true);
234 
235     unique_ptr<FetchResult<FileAsset>> fetchFileResult = make_unique<FetchResult<FileAsset>>(move(resultSet));
236     EXPECT_NE((fetchFileResult->GetCount() <= 0), true);
237 
238     unique_ptr<FileAsset> fileAsset = fetchFileResult->GetLastObject();
239     EXPECT_NE((fileAsset == nullptr), true);
240     return fileAsset;
241 }
242 
ClearAllFile()243 void ClearAllFile()
244 {
245     system("rm -rf /storage/media/100/local/files/.thumbs/*");
246     system("rm -rf /storage/cloud/100/files/Audio/*");
247     system("rm -rf /storage/cloud/100/files/Audios/*");
248     system("rm -rf /storage/cloud/100/files/Camera/*");
249     system("rm -rf /storage/cloud/100/files/Docs/Documents/*");
250     system("rm -rf /storage/cloud/100/files/Photo/*");
251     system("rm -rf /storage/cloud/100/files/Pictures/*");
252     system("rm -rf /storage/cloud/100/files/Docs/Download/*");
253     system("rm -rf /storage/cloud/100/files/Docs/.*");
254     system("rm -rf /storage/cloud/100/files/Videos/*");
255     system("rm -rf /storage/cloud/100/files/.*");
256     system("rm -rf /data/app/el2/100/database/com.ohos.medialibrary.medialibrarydata/*");
257     system("kill -9 `pidof com.ohos.medialibrary.medialibrarydata`");
258     system("scanner");
259 }
260 
DeleteFile(std::string fileUri)261 void DeleteFile(std::string fileUri)
262 {
263     if (sDataShareHelper_ == nullptr) {
264         return;
265     }
266     Uri deleteAssetUri(MEDIALIBRARY_DATA_URI + "/" + MEDIA_FILEOPRN);
267     DataShare::DataSharePredicates predicates;
268     predicates.EqualTo(MEDIA_DATA_DB_ID, MediaFileUtils::GetIdFromUri(fileUri));
269     int retVal = sDataShareHelper_->Delete(deleteAssetUri, predicates);
270     MEDIA_INFO_LOG("MediaSpaceStatistics_test DeleteFile::uri :%{private}s", deleteAssetUri.ToString().c_str());
271     EXPECT_NE(retVal, E_ERR);
272 }
273 
ClearFile()274 void ClearFile()
275 {
276     if (sDataShareHelper_ == nullptr) {
277         return;
278     }
279     vector<string> columns;
280     DataSharePredicates predicates;
281     string prefix = MEDIA_DATA_DB_MEDIA_TYPE + " <> " + to_string(g_albumMediaType);
282     predicates.SetWhereClause(prefix);
283     Uri queryFileUri(MEDIALIBRARY_DATA_URI);
284     shared_ptr<DataShareResultSet> resultSet = nullptr;
285     resultSet = sDataShareHelper_->Query(queryFileUri, predicates, columns);
286     EXPECT_NE((resultSet == nullptr), true);
287 
288     unique_ptr<FetchResult<FileAsset>> fetchFileResult = make_unique<FetchResult<FileAsset>>(move(resultSet));
289     EXPECT_NE((fetchFileResult->GetCount() < 0), true);
290     unique_ptr<FileAsset> fileAsset = fetchFileResult->GetFirstObject();
291     while (fileAsset != nullptr) {
292         DeleteFile(fileAsset->GetUri());
293         fileAsset = fetchFileResult->GetNextObject();
294     }
295 }
296 
CreateFile(std::string baseURI,std::string targetPath,std::string newName,MediaType mediaType,const unsigned char fileContent[],const int len)297 void CreateFile(std::string baseURI, std::string targetPath, std::string newName, MediaType mediaType,
298     const unsigned char fileContent[], const int len)
299 {
300     MEDIA_INFO_LOG("CreateFile:: start Create file: %s", newName.c_str());
301     if (sDataShareHelper_ == nullptr) {
302         return;
303     }
304 
305     string abilityUri = Media::MEDIALIBRARY_DATA_URI;
306     if (MediaFileUtils::StartsWith(targetPath, "Pictures/") ||
307         MediaFileUtils::StartsWith(targetPath, "Videos/")) {
308         abilityUri += Media::MEDIA_PHOTOOPRN + "/" + Media::MEDIA_FILEOPRN_CREATEASSET;
309     } else if (MediaFileUtils::StartsWith(targetPath, "Audios/")) {
310         abilityUri += Media::MEDIA_AUDIOOPRN + "/" + Media::MEDIA_FILEOPRN_CREATEASSET;
311     } else {
312         abilityUri += Media::MEDIA_FILEOPRN + "/" + Media::MEDIA_FILEOPRN_CREATEASSET;
313     }
314     Uri createAssetUri(abilityUri);
315     DataShareValuesBucket valuesBucket;
316     valuesBucket.Put(MEDIA_DATA_DB_MEDIA_TYPE, mediaType);
317     valuesBucket.Put(MEDIA_DATA_DB_NAME, newName);
318     valuesBucket.Put(MEDIA_DATA_DB_RELATIVE_PATH, targetPath);
319 
320     int32_t index = sDataShareHelper_->Insert(createAssetUri, valuesBucket);
321     int64_t virtualIndex = MediaFileUtils::GetVirtualIdByType(index, mediaType);
322     string destUri = baseURI + "/" + std::to_string(virtualIndex);
323 
324     Uri openFileUriDest(destUri);
325     int32_t destFd = sDataShareHelper_->OpenFile(openFileUriDest, MEDIA_FILEMODE_READWRITE);
326     EXPECT_NE(destFd <= 0, true);
327 
328     int32_t resWrite = write(destFd, fileContent, len);
329     if (resWrite == -1) {
330         EXPECT_EQ(false, true);
331     }
332 
333     mediaLibraryManager->CloseAsset(destUri, destFd);
334     MEDIA_INFO_LOG("CreateFile:: end Create file: %s", newName.c_str());
335 }
336 
CopyFile(std::string srcUri,std::string baseURI,std::string targetPath,std::string newName,MediaType mediaType,int sleepSecond)337 void CopyFile(std::string srcUri, std::string baseURI, std::string targetPath, std::string newName,
338     MediaType mediaType, int sleepSecond)
339 {
340     MEDIA_INFO_LOG("CopyFile:: start Copy sleepSecond[%d] file: %s", sleepSecond, newName.c_str());
341     if (sDataShareHelper_ == nullptr) {
342         return;
343     }
344     Uri openFileUri(srcUri);
345     int32_t srcFd = sDataShareHelper_->OpenFile(openFileUri, MEDIA_FILEMODE_READWRITE);
346     EXPECT_NE(srcFd <= 0, true);
347 
348     string abilityUri = Media::MEDIALIBRARY_DATA_URI;
349     if (MediaFileUtils::StartsWith(targetPath, "Pictures/") ||
350         MediaFileUtils::StartsWith(targetPath, "Videos/")) {
351         abilityUri += Media::MEDIA_PHOTOOPRN + "/" + Media::MEDIA_FILEOPRN_CREATEASSET;
352     } else if (MediaFileUtils::StartsWith(targetPath, "Audios/")) {
353         abilityUri += Media::MEDIA_AUDIOOPRN + "/" + Media::MEDIA_FILEOPRN_CREATEASSET;
354     } else {
355         abilityUri += Media::MEDIA_FILEOPRN + "/" + Media::MEDIA_FILEOPRN_CREATEASSET;
356     }
357     Uri createAssetUri(abilityUri);
358     DataShareValuesBucket valuesBucket;
359     valuesBucket.Put(MEDIA_DATA_DB_MEDIA_TYPE, mediaType);
360     valuesBucket.Put(MEDIA_DATA_DB_NAME, newName);
361     valuesBucket.Put(MEDIA_DATA_DB_RELATIVE_PATH, targetPath);
362     int32_t index = sDataShareHelper_->Insert(createAssetUri, valuesBucket);
363     int64_t virtualIndex = MediaFileUtils::GetVirtualIdByType(index, mediaType);
364     string destUri = baseURI + "/" + std::to_string(virtualIndex);
365     Uri openFileUriDest(destUri);
366     int32_t destFd = sDataShareHelper_->OpenFile(openFileUriDest, MEDIA_FILEMODE_READWRITE);
367     EXPECT_NE(destFd <= 0, true);
368 
369     int64_t srcLen = lseek(srcFd, 0, SEEK_END);
370     lseek(srcFd, 0, SEEK_SET);
371     char buf[srcLen];
372     int32_t readRet = read(srcFd, buf, srcLen);
373     int32_t resWrite = write(destFd, buf, readRet);
374     if (resWrite == -1) {
375         EXPECT_EQ(false, true);
376     }
377 
378     mediaLibraryManager->CloseAsset(srcUri, srcFd);
379     mediaLibraryManager->CloseAsset(destUri, destFd);
380     sleep(sleepSecond);
381     MEDIA_INFO_LOG("CopyFile:: end Copy file: %s", newName.c_str());
382 }
383 
CheckQuerySize(std::string testNo,int mediaTypeId,int targetFileNumber)384 void CheckQuerySize(std::string testNo, int mediaTypeId, int targetFileNumber)
385 {
386     MediaVolume mediaVolume;
387     mediaLibraryManager->QueryTotalSize(mediaVolume);
388     int64_t querySize = 0;
389     int64_t targetSize = 0;
390     if (mediaTypeId == MEDIA_TYPE_IMAGE) {
391         querySize = mediaVolume.GetImagesSize();
392         targetSize = targetFileNumber * g_oneImageSize;
393     } else if (mediaTypeId == MEDIA_TYPE_VIDEO) {
394         querySize = mediaVolume.GetVideosSize();
395         targetSize = targetFileNumber * g_oneVideoSize;
396     } else if (mediaTypeId == MEDIA_TYPE_AUDIO) {
397         querySize = mediaVolume.GetAudiosSize();
398         targetSize = targetFileNumber * g_oneAudioSize;
399     } else if (mediaTypeId == MEDIA_TYPE_FILE) {
400         querySize = mediaVolume.GetFilesSize();
401         targetSize = targetFileNumber * g_oneFileSize;
402     }
403     MEDIA_INFO_LOG("%s QueryTotalSize querySize = %{public}lld", testNo.c_str(), (long long)querySize);
404     MEDIA_INFO_LOG("%s QueryTotalSize targetSize = %{public}lld", testNo.c_str(), (long long)targetSize);
405     if (mediaTypeId == MEDIA_TYPE_IMAGE) {
406         EXPECT_TRUE(querySize >= targetSize);
407     } else {
408         EXPECT_EQ(querySize, targetSize);
409     }
410 }
411 
CreateTestFile(MediaType MediaType,string fileName,string relativePath)412 int32_t CreateTestFile(MediaType MediaType, string fileName, string relativePath)
413 {
414     string abilityUri = Media::ML_FILE_URI_PREFIX;
415     Uri createAssetUri(abilityUri + "/" + Media::MEDIA_FILEOPRN + "/" + Media::MEDIA_FILEOPRN_CREATEASSET);
416     DataShareValuesBucket valuesBucket;
417     valuesBucket.Put(MEDIA_DATA_DB_MEDIA_TYPE, MediaType);
418     valuesBucket.Put(MEDIA_DATA_DB_NAME, fileName);
419     valuesBucket.Put(MEDIA_DATA_DB_RELATIVE_PATH, relativePath);
420     return sDataShareHelper_->Insert(createAssetUri, valuesBucket);
421 }
422 
ReturnUri(string type)423 string ReturnUri(string type)
424 {
425     return (ML_FILE_URI_PREFIX + '/' + "file" + "/" + type);
426 }
427 
428 /**
429  * @tc.number    : MediaSpaceStatistics_test_001
430  * @tc.name      : get Media image size
431  * @tc.desc      : 1.push 01.jpg into the device and make sure there is only one image
432  *                 2.call the method to get media size
433  *                 3.compare the size
434  */
435 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_001, TestSize.Level0)
436 {
437     MEDIA_INFO_LOG("MediaSpaceStatistics_test_001::Start");
438     if (sDataShareHelper_ == nullptr) {
439         return;
440     }
441     CheckQuerySize("MediaSpaceStatistics_test_001", MEDIA_TYPE_FILE, 0);
442     CheckQuerySize("MediaSpaceStatistics_test_001", MEDIA_TYPE_IMAGE, 1);
443     CheckQuerySize("MediaSpaceStatistics_test_001", MEDIA_TYPE_VIDEO, 1);
444     CheckQuerySize("MediaSpaceStatistics_test_001", MEDIA_TYPE_AUDIO, 1);
445     MEDIA_INFO_LOG("MediaSpaceStatistics_test_001::End");
446 }
447 
448 /**
449  * @tc.number    : MediaSpaceStatistics_test_002
450  * @tc.name      : get Media video size
451  * @tc.desc      : 1.push 01.mp4 into the device and make sure there is only one video
452  *                 2.call the method to get media size
453  *                 3.compare the size
454  */
455 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_002, TestSize.Level0)
456 {
457     MEDIA_INFO_LOG("MediaSpaceStatistics_test_002::Start");
458     if (sDataShareHelper_ == nullptr) {
459         return;
460     }
461     CheckQuerySize("MediaSpaceStatistics_test_002", MEDIA_TYPE_VIDEO, 1);
462     MEDIA_INFO_LOG("MediaSpaceStatistics_test_002::End");
463 }
464 
465 /**
466  * @tc.number    : MediaSpaceStatistics_test_003
467  * @tc.name      : get Media audio size
468  * @tc.desc      : 1.push 01.mp3 into the device and make sure there is only one audio
469  *                 2.call the method to get media size
470  *                 3.compare the size
471  */
472 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_003, TestSize.Level0)
473 {
474     MEDIA_INFO_LOG("MediaSpaceStatistics_test_003::Start");
475     if (sDataShareHelper_ == nullptr) {
476         return;
477     }
478     CheckQuerySize("MediaSpaceStatistics_test_003", MEDIA_TYPE_AUDIO, 1);
479     MEDIA_INFO_LOG("MediaSpaceStatistics_test_003::End");
480 }
481 
482 /**
483  * @tc.number    : MediaSpaceStatistics_test_004
484  * @tc.name      : get Media file size
485  * @tc.desc      : 1.push 01.txt into the device and make sure there is only one file
486  *                 2.call the method to get media size
487  *                 3.compare the size
488  */
489 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_004, TestSize.Level0)
490 {
491     MEDIA_INFO_LOG("MediaSpaceStatistics_test_004::Start");
492     if (sDataShareHelper_ == nullptr) {
493         return;
494     }
495     CheckQuerySize("MediaSpaceStatistics_test_004", MEDIA_TYPE_FILE, 0);
496     MEDIA_INFO_LOG("MediaSpaceStatistics_test_004::End");
497 }
498 
499 /**
500  * @tc.number    : MediaSpaceStatistics_test_005
501  * @tc.name      : test CloseAsset
502  * @tc.desc      : pass invalid fd
503  */
504 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_005, TestSize.Level0)
505 {
506     MEDIA_INFO_LOG("MediaSpaceStatistics_test_005::Start");
507     if (sDataShareHelper_ == nullptr) {
508         return;
509     }
510     const string TEST_URI = "";
511     const int32_t TEST_FD = 10000;
512     mediaLibraryManager->CloseAsset(TEST_URI, TEST_FD);
513     MEDIA_INFO_LOG("MediaSpaceStatistics_test_005::End");
514 }
515 
516 /**
517  * @tc.number    : MediaSpaceStatistics_test_006
518  * @tc.name      : get Media(image,video,audio,file) size
519  * @tc.desc      : 1.delete all media
520  *                 2.query media size
521  *                 3.make sure size is 0
522  */
523 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_006, TestSize.Level0)
524 {
525     MEDIA_INFO_LOG("MediaSpaceStatistics_test_006::Start");
526     ClearFile();
527     MediaVolume mediaVolume;
528     mediaLibraryManager->QueryTotalSize(mediaVolume);
529     EXPECT_NE(mediaVolume.GetImagesSize(), 0);
530     MEDIA_INFO_LOG("MediaSpaceStatistics_test_006::End");
531 }
532 
533 /**
534  * @tc.number    : MediaSpaceStatistics_test_007
535  * @tc.name      : test for get uri or path by default relative path
536  * @tc.desc      : 1.creat a file in Picture
537  *                 2.get uri from path
538  *                 3.get path from uri
539  */
540 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_007, TestSize.Level0)
541 {
542     MEDIA_INFO_LOG("MediaSpaceStatistics_test_007::Start");
543     int32_t index = CreateTestFile(MEDIA_TYPE_IMAGE, "MediaSpaceStatistics_test_007.jpg", "Pictures/");
544     EXPECT_EQ((index > 0), true);
545     int64_t virtualId = MediaFileUtils::GetVirtualIdByType(index, MediaType::MEDIA_TYPE_FILE);
546     const Uri realUri(ReturnUri(to_string(virtualId)));
547     const string realPath = "/storage/cloud/100/files/Pictures/MediaSpaceStatistics_test_007.jpg";
548     Uri fileUri(ML_FILE_URI_PREFIX);
549     string filePath;
550     string userId;
551     int32_t ret = mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
552     EXPECT_EQ((ret < 0), true);
553     MEDIA_INFO_LOG("GetUriFromFilePath::End");
554 
555     ret = mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
556     EXPECT_EQ((ret < 0), true);
557     MEDIA_INFO_LOG("GetFilePathFromUri::End");
558 
559     MEDIA_INFO_LOG("MediaSpaceStatistics_test_007::End");
560 }
561 
562 /**
563  * @tc.number    : MediaSpaceStatistics_test_008
564  * @tc.name      : test for get uri or path by true relative path
565  * @tc.desc      : 1.creat a file in Picture
566  *                 2.get uri from path
567  *                 3.get path from uri
568  */
569 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_008, TestSize.Level0)
570 {
571     MEDIA_INFO_LOG("MediaSpaceStatistics_test_008::Start");
572     int32_t index = CreateTestFile(MEDIA_TYPE_FILE, "MediaSpaceStatistics_test_008.txt", "Docs/Documents/");
573     EXPECT_EQ((index > 0), true);
574     int64_t virtualId = MediaFileUtils::GetVirtualIdByType(index, MediaType::MEDIA_TYPE_FILE);
575     const Uri realUri(ReturnUri(to_string(virtualId)));
576     const string realPath = "/storage/cloud/100/files/Docs/Documents/MediaSpaceStatistics_test_008.txt";
577     Uri fileUri(ML_FILE_URI_PREFIX);
578     string filePath;
579     string userId;
580     mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
581     EXPECT_EQ(fileUri.ToString(), realUri.ToString());
582     printf("MediaSpaceStatistics_test_008::fileUri is : %s\n", fileUri.ToString().c_str());
583     MEDIA_INFO_LOG("GetUriFromFilePath::End");
584     mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
585     EXPECT_EQ(filePath, realPath);
586     printf("MediaSpaceStatistics_test_008::filePath is : %s\n", filePath.c_str());
587     MEDIA_INFO_LOG("GetFilePathFromUri::End");
588 
589     MEDIA_INFO_LOG("MediaSpaceStatistics_test_008::End");
590 }
591 
592 /**
593  * @tc.number    : MediaSpaceStatistics_test_009
594  * @tc.name      : test for get uri or path by true relative path
595  * @tc.desc      : 1.creat a file in Picture
596  *                 2.get uri from path
597  *                 3.get path from uri
598  */
599 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_009, TestSize.Level0)
600 {
601     MEDIA_INFO_LOG("MediaSpaceStatistics_test_009::Start");
602 
603     int32_t index = CreateTestFile(MEDIA_TYPE_FILE, "MediaSpaceStatistics_test_009.txt", "Docs/Download/");
604     EXPECT_EQ((index > 0), true);
605     int64_t virtualId = MediaFileUtils::GetVirtualIdByType(index, MediaType::MEDIA_TYPE_FILE);
606     const Uri realUri(ReturnUri(to_string(virtualId)));
607     const string realPath = "/storage/cloud/100/files/Docs/Download/MediaSpaceStatistics_test_009.txt";
608     Uri fileUri(ML_FILE_URI_PREFIX);
609     string filePath;
610     string userId;
611     mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
612     EXPECT_EQ(fileUri.ToString(), realUri.ToString());
613     printf("MediaSpaceStatistics_test_009::fileUri is : %s\n", fileUri.ToString().c_str());
614     MEDIA_INFO_LOG("GetUriFromFilePath::End");
615 
616     mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
617     EXPECT_EQ(filePath, realPath);
618     printf("MediaSpaceStatistics_test_009::filePath is : %s\n", filePath.c_str());
619     MEDIA_INFO_LOG("GetFilePathFromUri::End");
620 
621     MEDIA_INFO_LOG("MediaSpaceStatistics_test_009::End");
622 }
623 
624 /**
625  * @tc.number    : MediaSpaceStatistics_test_010
626  * @tc.name      : test for get uri or path there is not file in database
627  * @tc.desc      : 1.get uri from path
628  *                 2.get path from uri
629  */
630 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_010, TestSize.Level0)
631 {
632     MEDIA_INFO_LOG("MediaSpaceStatistics_test_010::Start");
633     const Uri realUri(ReturnUri("1"));
634     const string realPath = "/storage/cloud/100/files/Docs/Documents/MediaSpaceStatistics_test_010.txt";
635     Uri fileUri(ML_FILE_URI_PREFIX);
636     string filePath;
637     string userId;
638     int32_t ret = mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
639     EXPECT_EQ((ret < 0), true);
640     MEDIA_INFO_LOG("GetUriFromFilePath::End");
641 
642     ret = mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
643     EXPECT_EQ((ret < 0), true);
644     MEDIA_INFO_LOG("GetFilePathFromUri::End");
645 
646     MEDIA_INFO_LOG("MediaSpaceStatistics_test_010::End");
647 }
648 
649 /**
650  * @tc.number    : MediaSpaceStatistics_test_011
651  * @tc.name      : test for get uri or path by true relative path
652  * @tc.desc      : 1.creat a file in Picture
653  *                 2.get uri from path
654  *                 3.get path from uri
655  */
656 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_011, TestSize.Level0)
657 {
658     MEDIA_INFO_LOG("MediaSpaceStatistics_test_011::Start");
659     int32_t index = CreateTestFile(MEDIA_TYPE_FILE, "MediaSpaceStatistics_test_011.txt", "Docs/Documents/test1/");
660     EXPECT_EQ((index > 0), true);
661     int64_t virtualId = MediaFileUtils::GetVirtualIdByType(index, MediaType::MEDIA_TYPE_FILE);
662     const Uri realUri(ReturnUri(to_string(virtualId)));
663     const string realPath = "/storage/cloud/100/files/Docs/Documents/test1/MediaSpaceStatistics_test_011.txt";
664     Uri fileUri(ML_FILE_URI_PREFIX);
665     string filePath;
666     string userId;
667     mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
668     EXPECT_EQ(fileUri.ToString(), realUri.ToString());
669     printf("MediaSpaceStatistics_test_011::fileUri is : %s\n", fileUri.ToString().c_str());
670     MEDIA_INFO_LOG("GetUriFromFilePath::End");
671 
672     mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
673     EXPECT_EQ(filePath, realPath);
674     printf("MediaSpaceStatistics_test_011::filePath is : %s\n", filePath.c_str());
675     MEDIA_INFO_LOG("GetFilePathFromUri::End");
676 
677     MEDIA_INFO_LOG("MediaSpaceStatistics_test_011::End");
678 }
679 
680 /**
681  * @tc.number    : MediaSpaceStatistics_test_012
682  * @tc.name      : test for get uri or path by true relative path
683  * @tc.desc      : 1.creat a file in Picture
684  *                 2.get uri from path
685  *                 3.get path from uri
686  */
687 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_012, TestSize.Level0)
688 {
689     MEDIA_INFO_LOG("MediaSpaceStatistics_test_012::Start");
690     int32_t index = CreateTestFile(MEDIA_TYPE_FILE, "test.txt", "Docs/Documents/weixin/");
691     EXPECT_EQ((index > 0), true);
692     int64_t virtualId = MediaFileUtils::GetVirtualIdByType(index, MediaType::MEDIA_TYPE_FILE);
693     const Uri realUri(ReturnUri(to_string(virtualId)));
694     const string realPath = "/storage/cloud/100/files/Docs/Documents/weixin/test.txt";
695     Uri fileUri(ML_FILE_URI_PREFIX);
696     string filePath;
697     string userId;
698     mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
699     EXPECT_EQ(fileUri.ToString(), realUri.ToString());
700     printf("MediaSpaceStatistics_test_012::fileUri is : %s\n", fileUri.ToString().c_str());
701     MEDIA_INFO_LOG("GetUriFromFilePath::End");
702 
703     mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
704     EXPECT_EQ(filePath, realPath);
705     printf("MediaSpaceStatistics_test_012::filePath is : %s\n", filePath.c_str());
706     MEDIA_INFO_LOG("GetFilePathFromUri::End");
707 
708     MEDIA_INFO_LOG("MediaSpaceStatistics_test_012::End");
709 }
710 
711 /**
712  * @tc.number    : MediaSpaceStatistics_test_013
713  * @tc.name      : test for get uri or path by true relative path
714  * @tc.desc      : 1.creat a file in Picture
715  *                 2.get uri from path
716  *                 3.get path from uri
717  */
718 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_013, TestSize.Level0)
719 {
720     MEDIA_INFO_LOG("MediaSpaceStatistics_test_013::Start");
721     int32_t index = CreateTestFile(MEDIA_TYPE_AUDIO, "MediaSpaceStatistics_test_013.mp3", "Docs/Download/weixin/");
722     EXPECT_EQ((index > 0), true);
723     int64_t virtualId = MediaFileUtils::GetVirtualIdByType(index, MediaType::MEDIA_TYPE_FILE);
724     const Uri realUri(ReturnUri(to_string(virtualId)));
725     const string realPath = "/storage/cloud/100/files/Docs/Download/weixin/MediaSpaceStatistics_test_013.mp3";
726     Uri fileUri(ML_FILE_URI_PREFIX);
727     string filePath;
728     string userId;
729     mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
730     EXPECT_EQ(fileUri.ToString(), realUri.ToString());
731     printf("MediaSpaceStatistics_test_013::fileUri is : %s\n", fileUri.ToString().c_str());
732     MEDIA_INFO_LOG("GetUriFromFilePath::End");
733 
734     mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
735     EXPECT_EQ(filePath, realPath);
736     printf("MediaSpaceStatistics_test_013::filePath is : %s\n", filePath.c_str());
737     MEDIA_INFO_LOG("GetFilePathFromUri::End");
738 
739     MEDIA_INFO_LOG("MediaSpaceStatistics_test_013::End");
740 }
741 
742 /**
743  * @tc.number    : MediaSpaceStatistics_test_014
744  * @tc.name      : test for get uri or path by true relative path
745  * @tc.desc      : 1.creat a file in Picture
746  *                 2.get uri from path
747  *                 3.get path from uri
748  */
749 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_014, TestSize.Level0)
750 {
751     MEDIA_INFO_LOG("MediaSpaceStatistics_test_014::Start");
752     int32_t index = CreateTestFile(MEDIA_TYPE_IMAGE, "MediaSpaceStatistics_test_014.jpg", "Camera/weixin/");
753     EXPECT_EQ((index > 0), true);
754     int64_t virtualId = MediaFileUtils::GetVirtualIdByType(index, MediaType::MEDIA_TYPE_FILE);
755     const Uri realUri(ReturnUri(to_string(virtualId)));
756     const string realPath = "/storage/cloud/100/files/Camera/weixin/MediaSpaceStatistics_test_014.jpg";
757     Uri fileUri(ML_FILE_URI_PREFIX);
758     string filePath;
759     string userId;
760     int32_t ret = mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
761     EXPECT_EQ((ret < 0), true);
762     MEDIA_INFO_LOG("GetUriFromFilePath::End");
763 
764     ret = mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
765     EXPECT_EQ((ret < 0), true);
766     MEDIA_INFO_LOG("GetFilePathFromUri::End");
767 
768     MEDIA_INFO_LOG("MediaSpaceStatistics_test_014::End");
769 }
770 
771 /**
772  * @tc.number    : MediaSpaceStatistics_test_015
773  * @tc.name      : test for get uri or path by true relative path
774  * @tc.desc      : 1.creat a file in Picture
775  *                 2.get uri from path
776  *                 3.get path from uri
777  */
778 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_015, TestSize.Level0)
779 {
780     MEDIA_INFO_LOG("MediaSpaceStatistics_test_015::Start");
781     int32_t index = CreateTestFile(MEDIA_TYPE_IMAGE, "MediaSpaceStatistics_test_015.jpg", "Docs/Download/weixin/");
782     EXPECT_EQ((index > 0), true);
783     int64_t virtualId = MediaFileUtils::GetVirtualIdByType(index, MediaType::MEDIA_TYPE_FILE);
784     const Uri realUri(ReturnUri(to_string(virtualId)));
785     const string realPath = "/storage/cloud/100/files/Docs/Download/weixin/MediaSpaceStatistics_test_015.jpg";
786     Uri fileUri(ML_FILE_URI_PREFIX);
787     string filePath;
788     string userId;
789     mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
790     EXPECT_EQ(fileUri.ToString(), realUri.ToString());
791     printf("MediaSpaceStatistics_test_015::fileUri is : %s\n", fileUri.ToString().c_str());
792     MEDIA_INFO_LOG("GetUriFromFilePath::End");
793 
794     mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
795     EXPECT_EQ(filePath, realPath);
796     printf("MediaSpaceStatistics_test_015::filePath is : %s\n", filePath.c_str());
797     MEDIA_INFO_LOG("GetFilePathFromUri::End");
798 
799     MEDIA_INFO_LOG("MediaSpaceStatistics_test_015::End");
800 }
801 
802 /**
803  * @tc.number    : MediaSpaceStatistics_test_016
804  * @tc.name      : test for get uri or path by true relative path
805  * @tc.desc      : 1.creat a file in Picture
806  *                 2.get uri from path
807  *                 3.get path from uri
808  */
809 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_016, TestSize.Level0)
810 {
811     MEDIA_INFO_LOG("MediaSpaceStatistics_test_016::Start");
812     int32_t index = CreateTestFile(MEDIA_TYPE_FILE, "MediaSpaceStatistics_test_016.xxx", "Docs/Download/weixin/");
813     EXPECT_EQ((index > 0), true);
814     int64_t virtualId = MediaFileUtils::GetVirtualIdByType(index, MediaType::MEDIA_TYPE_FILE);
815     const Uri realUri(ReturnUri(to_string(virtualId)));
816     const string realPath = "/storage/cloud/100/files/Docs/Download/weixin/MediaSpaceStatistics_test_016.xxx";
817     Uri fileUri(ML_FILE_URI_PREFIX);
818     string filePath;
819     string userId;
820     mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
821     EXPECT_EQ(fileUri.ToString(), realUri.ToString());
822     printf("MediaSpaceStatistics_test_016::fileUri is : %s\n", fileUri.ToString().c_str());
823     MEDIA_INFO_LOG("GetUriFromFilePath::End");
824 
825     mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
826     EXPECT_EQ(filePath, realPath);
827     printf("MediaSpaceStatistics_test_016::filePath is : %s\n", filePath.c_str());
828     MEDIA_INFO_LOG("GetFilePathFromUri::End");
829 
830     MEDIA_INFO_LOG("MediaSpaceStatistics_test_016::End");
831 }
832 
833 /**
834  * @tc.number    : MediaSpaceStatistics_test_017
835  * @tc.name      : test for get uri or path by default relative path
836  * @tc.desc      : 1.creat a file in Picture
837  *                 2.get uri from path
838  *                 3.get path from uri
839  */
840 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_017, TestSize.Level0)
841 {
842     MEDIA_INFO_LOG("MediaSpaceStatistics_test_017::Start");
843     int32_t index = CreateTestFile(MEDIA_TYPE_VIDEO, "MediaSpaceStatistics_test_017.mp4", "Videos/");
844     EXPECT_EQ((index > 0), true);
845     int64_t virtualId = MediaFileUtils::GetVirtualIdByType(index, MediaType::MEDIA_TYPE_FILE);
846     const Uri realUri(ReturnUri(to_string(virtualId)));
847     const string realPath = "/storage/cloud/100/files/Videos/MediaSpaceStatistics_test_017.mp4";
848     Uri fileUri(ML_FILE_URI_PREFIX);
849     string filePath;
850     string userId;
851     int32_t ret = mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
852     EXPECT_EQ((ret < 0), true);
853     MEDIA_INFO_LOG("GetUriFromFilePath::End");
854 
855     ret = mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
856     EXPECT_EQ((ret < 0), true);
857     MEDIA_INFO_LOG("GetFilePathFromUri::End");
858 
859     MEDIA_INFO_LOG("MediaSpaceStatistics_test_017::End");
860 }
861 
862 /**
863  * @tc.number    : MediaSpaceStatistics_test_018
864  * @tc.name      : test for get uri or path by default relative path
865  * @tc.desc      : 1.creat a file in Picture
866  *                 2.get uri from path
867  *                 3.get path from uri
868  */
869 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_018, TestSize.Level0)
870 {
871     MEDIA_INFO_LOG("MediaSpaceStatistics_test_018::Start");
872     int32_t index = CreateTestFile(MEDIA_TYPE_AUDIO, "MediaSpaceStatistics_test_018.mp3", "Audios/");
873     EXPECT_EQ((index > 0), true);
874     int64_t virtualId = MediaFileUtils::GetVirtualIdByType(index, MediaType::MEDIA_TYPE_FILE);
875     const Uri realUri(ReturnUri(to_string(virtualId)));
876     const string realPath = "/storage/cloud/100/files/Audios/MediaSpaceStatistics_test_018.mp3";
877     Uri fileUri(ML_FILE_URI_PREFIX);
878     string filePath;
879     string userId;
880     int32_t ret = mediaLibraryManager->GetUriFromFilePath(realPath, fileUri, userId);
881     EXPECT_EQ((ret < 0), true);
882     MEDIA_INFO_LOG("GetUriFromFilePath::End");
883 
884     ret = mediaLibraryManager->GetFilePathFromUri(realUri, filePath, "100");
885     EXPECT_EQ((ret < 0), true);
886     MEDIA_INFO_LOG("GetFilePathFromUri::End");
887 
888     MEDIA_INFO_LOG("MediaSpaceStatistics_test_018::End");
889 }
890 
891 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_019, TestSize.Level0)
892 {
893     MEDIA_INFO_LOG("MediaSpaceStatistics_test_019::Start");
894     string columnName;
895     string value;
896     vector<string> columns;
897     auto ret = mediaLibraryManager->GetResultSetFromDb(columnName, value, columns);
898     EXPECT_EQ((ret != nullptr), true);
899     MEDIA_INFO_LOG("MediaSpaceStatistics_test_019::End");
900 }
901 
902 /**
903  * @tc.number    : MediaSpaceStatistics_test_020
904  * @tc.name      : Get thumbnail by medialibraryManager
905  * @tc.desc      : Get thumbnail by medialibraryManager
906  */
907 HWTEST_F(MediaSpaceStatisticsTest, MediaSpaceStatistics_test_020, TestSize.Level0)
908 {
909     MEDIA_INFO_LOG("MediaSpaceStatistics_test_020::Start");
910     Uri uri("file://media/Photo/10");
911     auto ret = mediaLibraryManager->GetThumbnail(uri);
912     EXPECT_EQ((ret != nullptr), false);
913     MEDIA_INFO_LOG("MediaSpaceStatistics_test_020::End");
914 }
915 } // namespace Media
916 } // namespace OHOS
917