1 /*
2  * Copyright (C) 2024 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 "ringtone_unittest_utils.h"
17 
18 #include <fstream>
19 
20 #include "ability_context_impl.h"
21 #include "abs_rdb_predicates.h"
22 #include "ringtone_data_manager.h"
23 #include "ringtone_db_const.h"
24 #include "ringtone_fetch_result.h"
25 #include "ringtone_file_utils.h"
26 #include "ringtone_log.h"
27 #include "ringtone_mimetype_utils.h"
28 #include "ringtone_scanner_utils.h"
29 
30 using namespace std;
31 using namespace OHOS::DataShare;
32 using namespace OHOS::AppExecFwk;
33 namespace OHOS {
34 namespace Media {
35 
Init()36 void RingtoneUnitTestUtils::Init()
37 {
38     auto stageContext = std::make_shared<AbilityRuntime::ContextImpl>();
39     auto abilityContextImpl = std::make_shared<OHOS::AbilityRuntime::AbilityContextImpl>();
40     abilityContextImpl->SetStageContext(stageContext);
41     auto ret = RingtoneDataManager::GetInstance()->Init(abilityContextImpl);
42     CHECK_AND_RETURN_LOG(ret == E_OK, "InitMediaLibraryMgr failed, ret: %{public}d", ret);
43     isValid_ = true;
44 }
45 
InitRootDirs()46 void RingtoneUnitTestUtils::InitRootDirs()
47 {
48     system("mkdir /storage/cloud/files");
49     system("mkdir /storage/cloud/files/Ringtone");
50 }
51 
CleanTestFiles()52 void RingtoneUnitTestUtils::CleanTestFiles()
53 {
54     system("rm -rf /storage/cloud/files/");
55 }
56 
CreateFileFS(const string & filePath)57 bool RingtoneUnitTestUtils::CreateFileFS(const string &filePath)
58 {
59     bool errCode = false;
60 
61     if (filePath.empty()) {
62         return errCode;
63     }
64 
65     ofstream file(filePath);
66     if (!file) {
67         RINGTONE_ERR_LOG("Output file path could not be created");
68         return errCode;
69     }
70 
71     const mode_t CHOWN_RW_UG = 0660;
72     if (chmod(filePath.c_str(), CHOWN_RW_UG) == 0) {
73         errCode = true;
74     }
75 
76     file.close();
77 
78     return errCode;
79 }
80 
WaitForCallback(shared_ptr<TestRingtoneScannerCallback> callback)81 void RingtoneUnitTestUtils::WaitForCallback(shared_ptr<TestRingtoneScannerCallback> callback)
82 {
83     std::mutex mutex;
84     std::unique_lock<std::mutex> lock(mutex);
85     const int waitSeconds = 10;
86     callback->condVar_.wait_until(lock, std::chrono::system_clock::now() + std::chrono::seconds(waitSeconds));
87 }
88 
TestRingtoneScannerCallback()89 TestRingtoneScannerCallback::TestRingtoneScannerCallback() : status_(-1) {}
90 
OnScanFinished(const int32_t status,const std::string & uri,const std::string & path)91 int32_t TestRingtoneScannerCallback::OnScanFinished(const int32_t status, const std::string &uri,
92     const std::string &path)
93 {
94     status_ = status;
95     condVar_.notify_all();
96     return E_OK;
97 }
98 }
99 }
100