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