1 /* 2 * Copyright (C) 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 #ifndef MEDIALIBRARY_UNITTEST_UTILS_H 17 #define MEDIALIBRARY_UNITTEST_UTILS_H 18 19 #include <condition_variable> 20 21 #include "imedia_scanner_callback.h" 22 #include "file_asset.h" 23 24 namespace OHOS { 25 namespace Media { 26 static const inline std::string TEST_CAMERA = "Camera"; 27 static const inline std::string TEST_VIDEOS = "Videos"; 28 static const inline std::string TEST_PICTURES = "Pictures"; 29 static const inline std::string TEST_AUDIOS = "Audios"; 30 static const inline std::string TEST_DOCUMENTS = "Documents"; 31 static const inline std::string TEST_DOWNLOAD = "Download"; 32 static const inline std::vector<std::string> TEST_ROOT_DIRS = { TEST_CAMERA, TEST_VIDEOS, TEST_PICTURES, TEST_AUDIOS, 33 TEST_DOCUMENTS, TEST_DOWNLOAD }; 34 35 class TestScannerCallback : public IMediaScannerCallback { 36 public: 37 TestScannerCallback(); 38 ~TestScannerCallback() = default; 39 40 int32_t status_; 41 std::condition_variable condVar_; 42 int32_t OnScanFinished(const int32_t status, const std::string &uri, const std::string &path) override; 43 }; 44 45 class MediaLibraryUnitTestUtils { 46 public: MediaLibraryUnitTestUtils()47 MediaLibraryUnitTestUtils() {} ~MediaLibraryUnitTestUtils()48 virtual ~MediaLibraryUnitTestUtils() {} 49 static bool IsValid(); 50 static void Init(); 51 static void InitRootDirs(); 52 static void CleanTestFiles(); 53 static void CleanBundlePermission(); 54 static std::shared_ptr<FileAsset> GetRootAsset(const std::string &dir); 55 static bool IsFileExists(const std::string filePath); 56 static bool GetFileAsset(const int fileId, std::shared_ptr<FileAsset> &fileAsset); 57 static bool CreateAlbum(std::string displayName, std::shared_ptr<FileAsset> parentAlbumAsset, 58 std::shared_ptr<FileAsset> &albumAsset); 59 static bool CreateFile(std::string displayName, std::shared_ptr<FileAsset> parentAlbumAsset, 60 std::shared_ptr<FileAsset> &fileAsset); 61 static bool CreateFileFS(const std::string& filePath); 62 static bool DeleteDir(const std::string &path, const std::string &dirId); 63 static void TrashFile(std::shared_ptr<FileAsset> &fileAsset); 64 static void RecoveryFile(std::shared_ptr<FileAsset> &fileAsset); 65 static void WaitForCallback(std::shared_ptr<TestScannerCallback> callback); 66 static int32_t GrantUriPermission(const int32_t fileId, const std::string &bundleName, 67 const std::string &mode, const int32_t tableType); 68 private: 69 static inline bool isValid_ = false; 70 static inline std::unordered_map<std::string, std::shared_ptr<FileAsset>> rootDirAssetMap_; 71 }; 72 } 73 } 74 75 #endif // MEDIALIBRARY_UNITTEST_UTILS_H