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_dualfwk_restore_test.h"
17 #include "ability_context_impl.h"
18 #include "datashare_ext_ability.h"
19 #include "iservice_registry.h"
20 #include "ringtone_restore_napi.h"
21 #include "ringtone_restore_factory.h"
22 #include "ringtone_restore_type.h"
23 #include "ringtone_rdb_callbacks.h"
24
25 #define private public
26 #define protected public
27 #include "ringtone_dualfwk_restore.h"
28 #include "dualfwk_conf_loader.h"
29 #undef protected
30 #undef private
31 #include "ringtone_errno.h"
32 #include "ringtone_file_utils.h"
33 #include "ringtone_log.h"
34 #include "ringtone_rdbstore.h"
35 #include "datashare_helper.h"
36 using namespace std;
37 using namespace testing::ext;
38
39 namespace OHOS {
40 namespace Media {
41 const string TEST_BACKUP_PATH = "/data/test/backup";
42 const string TEST_BACKUP_DATA = "/data/local/tmp/test/Adara.ogg";
43 const string TEST_TOOL_QUERY_AUDIO = "datashare:///media/mediatool_audio_operation/tool_query_by_display_name";
44
45 unique_ptr<RingtoneDualFwkRestore> g_restoreDualFwkService = nullptr;
46
SetUpTestCase(void)47 void RingtoneDualFwkRestoreTest::SetUpTestCase(void)
48 {
49 auto stageContext = std::make_shared<AbilityRuntime::ContextImpl>();
50 auto abilityContextImpl = std::make_shared<OHOS::AbilityRuntime::AbilityContextImpl>();
51 abilityContextImpl->SetStageContext(stageContext);
52 shared_ptr<RingtoneUnistore> rUniStore = RingtoneRdbStore::GetInstance(abilityContextImpl);
53 int32_t ret = rUniStore->Init();
54 EXPECT_EQ(ret, E_OK);
55 g_restoreDualFwkService = std::make_unique<RingtoneDualFwkRestore>();
56 EXPECT_NE(g_restoreDualFwkService, nullptr);
57 int32_t res = g_restoreDualFwkService->Init(TEST_BACKUP_PATH);
58 // (void) res;
59 ASSERT_EQ(res, E_FAIL);
60
61 const string subPath = "/data/local/tmp/test";
62 EXPECT_EQ(RingtoneFileUtils::CreateDirectory(subPath), true);
63 EXPECT_EQ(RingtoneFileUtils::CreateFile(TEST_BACKUP_DATA), E_SUCCESS);
64 }
65
TearDownTestCase(void)66 void RingtoneDualFwkRestoreTest::TearDownTestCase(void)
67 {
68 g_restoreDualFwkService = nullptr;
69 system("rm -rf /data/local/tmp/test");
70 }
71
SetUp()72 void RingtoneDualFwkRestoreTest::SetUp() {}
73
TearDown()74 void RingtoneDualFwkRestoreTest::TearDown() {}
75
76 HWTEST_F(RingtoneDualFwkRestoreTest, ringtone_dualfwk_restore_test_0001, TestSize.Level0)
77 {
78 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0001 start");
79 EXPECT_NE(g_restoreDualFwkService->dualFwkSetting_, nullptr);
80 g_restoreDualFwkService->StartRestore();
81 std::unique_ptr<DualFwkConfRow> conf = std::make_unique<DualFwkConfRow>();
82 string confName = "alarm_alert_path";
83 const string confValue = TEST_BACKUP_DATA;
84 const string isSysSet = "true";
85 conf->name = confName;
86 conf->defaultSysSet = isSysSet;
87 conf->value = confValue;
88 auto ret = g_restoreDualFwkService->dualFwkSetting_->ProcessConfRow(conf);
89 EXPECT_EQ(ret, E_SUCCESS);
90 confName = "notification_sound";
91 conf->name = confName;
92 ret = g_restoreDualFwkService->dualFwkSetting_->ProcessConfRow(conf);
93 EXPECT_EQ(ret, E_SUCCESS);
94 confName = "notification_sound_set";
95 conf->name = confName;
96 ret = g_restoreDualFwkService->dualFwkSetting_->ProcessConfRow(conf);
97 EXPECT_EQ(ret, E_SUCCESS);
98 g_restoreDualFwkService->localRdb_ = RingtoneRdbStore::GetInstance()->GetRaw();
99 g_restoreDualFwkService->StartRestore();
100 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0001 end");
101 }
102
103 HWTEST_F(RingtoneDualFwkRestoreTest, ringtone_dualfwk_restore_test_0002, TestSize.Level0)
104 {
105 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0002 start");
106
107
108 shared_ptr<DualFwkConfLoader> confLoaderPtr = make_shared<DualFwkConfLoader>();
109 EXPECT_NE(confLoaderPtr, nullptr);
110 if (confLoaderPtr->Init() != E_OK) {
111 RINGTONE_ERR_LOG("Failed to initialize DualFwkConfLoader.");
112 return;
113 }
114 auto confVal = confLoaderPtr->GetConf("SETTINGSDATA_CreatedTime");
115 std::cout << "conf value of SETTINGSDATA_CreatedTime = " << confVal << std::endl;
116 DualFwkConf conf;
117 confLoaderPtr->Load(conf, RESTORE_SCENE_TYPE_DUAL_CLONE, "");
118 confLoaderPtr->ShowConf(conf);
119 confLoaderPtr->Load(conf, RESTORE_SCENE_TYPE_DUAL_UPGRADE, "");
120 confLoaderPtr->ShowConf(conf);
121 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0002 end");
122 }
123
124 HWTEST_F(RingtoneDualFwkRestoreTest, ringtone_dualfwk_restore_test_0003, TestSize.Level0)
125 {
126 NativeRdb::RdbStoreConfig config(RINGTONE_LIBRARY_DB_NAME);
127 config.SetPath("/data/app/el2/100/database/com.ohos.ringtonelibrary.ringtonelibrarydata/rdb/"
128 + RINGTONE_LIBRARY_DB_NAME);
129 config.SetBundleName("xx");
130 config.SetReadConSize(10);
131 config.SetSecurityLevel(NativeRdb::SecurityLevel::S3);
132
133 int32_t err;
134 RingtoneDataCallBack cb;
135 auto rdbStore = NativeRdb::RdbHelper::GetRdbStore(config, RINGTONE_RDB_VERSION, cb, err);
136 std::map<std::string, std::shared_ptr<FileInfo>> resultFromMedia;
137 std::map<std::string, std::vector<std::shared_ptr<FileInfo>>> resultFromRingtone;
138 auto restore = std::make_unique<RingtoneDualFwkRestoreClone>();
139 EXPECT_NE(restore, nullptr);
140 restore->Init("/");
141 restore->QueryRingToneDbForFileInfo(rdbStore, {"Creek.ogg", "Dawn.ogg", "Flourish.ogg"}, resultFromRingtone,
142 "display_name");
143 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0003 end");
144 }
145
146 HWTEST_F(RingtoneDualFwkRestoreTest, ringtone_dualfwk_restore_test_0004, TestSize.Level0)
147 {
148 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0004 start");
149 string backupPath = "";
150 int32_t res = g_restoreDualFwkService->Init(backupPath);
151 ASSERT_EQ(res, E_INVALID_ARGUMENTS);
152 backupPath = "/data/test";
153 res = g_restoreDualFwkService->Init(backupPath);
154 ASSERT_EQ(res, E_SUCCESS);
155
156 FileInfo info;
157 info.title = TITLE_DEFAULT;
158 g_restoreDualFwkService->UpdateRestoreFileInfo(info);
159 info.data = TEST_BACKUP_DATA;
160 bool result = g_restoreDualFwkService->OnPrepare(info, TEST_BACKUP_DATA);
161 EXPECT_EQ(result, true);
162 string data = "restore_OnPrepare_test_0001";
163 info.data = data;
164 const string destPath = "/restore_Init_test_0001/data";
165 result = g_restoreDualFwkService->OnPrepare(info, TEST_BACKUP_DATA);
166 EXPECT_EQ(result, false);
167 data = "createfile_001./data";
168 info.data = data;
169 result = g_restoreDualFwkService->OnPrepare(info, TEST_BACKUP_DATA);
170 EXPECT_EQ(result, false);
171 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0004 end");
172 }
173
174 HWTEST_F(RingtoneDualFwkRestoreTest, ringtone_dualfwk_restore_test_00045, TestSize.Level0)
175 {
176 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_00045 start");
177 auto obj = std::make_unique<RingtoneDualFwkRestoreClone>();
178 EXPECT_NE(obj, nullptr);
179 obj->LoadDualFwkConf("");
180 string xml = "setting_system";
181 auto ret = g_restoreDualFwkService->ParseDualFwkConf(xml);
182 EXPECT_EQ(ret, E_FAIL);
183 vector<FileInfo> infos;
184 g_restoreDualFwkService->OnFinished(infos);
185 xml = "/data/test/backup/setting_system.xml";
186 ret = g_restoreDualFwkService->ParseDualFwkConf(xml);
187 EXPECT_EQ(ret, E_SUCCESS);
188 std::unique_ptr<DualFwkConfLoader> dualLoader = std::make_unique<DualFwkConfLoader>();
189 DualFwkConf conf;
190 dualLoader->Load(conf, RESTORE_SCENE_TYPE_DUAL_CLONE, "");
191 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_00045 end");
192 }
193
194 HWTEST_F(RingtoneDualFwkRestoreTest, ringtone_dualfwk_restore_test_0006, TestSize.Level0)
195 {
196 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0006 start");
197 auto dualSetting = std::make_unique<DualFwkSoundSetting>();
198 EXPECT_NE(dualSetting, nullptr);
199 DualFwkConf conf;
200 string message = "Adara.ogg";
201 conf.messagePath = message;
202 conf.notificationSoundPath = message;
203 conf.ringtonePath = message;
204 conf.ringtone2Path = message;
205 conf.alarmAlertPath = message;
206 conf.messageSub1 = message;
207 dualSetting->ProcessConf(conf);
208 auto unConf = std::make_unique<DualFwkConfRow>();
209 auto ret = dualSetting->ProcessConfRow(unConf);
210 EXPECT_EQ(ret, E_ERR);
211 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0006 end");
212 }
213
214 HWTEST_F(RingtoneDualFwkRestoreTest, ringtone_dualfwk_restore_test_0007, TestSize.Level0)
215 {
216 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0007 start");
217 FileInfo info;
218 DualFwkSettingItem dualFwkSettingItem;
219 ASSERT_NE(g_restoreDualFwkService->dualFwkSetting_, nullptr);
220 dualFwkSettingItem.settingType = static_cast<int32_t>(ToneSettingType::TONE_SETTING_TYPE_MAX);
221 g_restoreDualFwkService->dualFwkSetting_->settings_.insert({1, dualFwkSettingItem});
222 g_restoreDualFwkService->BuildFileInfo();
223 dualFwkSettingItem.settingType = static_cast<int32_t>(ToneSettingType::TONE_SETTING_TYPE_ALARM);
224 g_restoreDualFwkService->BuildFileInfo();
225 dualFwkSettingItem.settingType = static_cast<int32_t>(ToneSettingType::TONE_SETTING_TYPE_RINGTONE);
226 g_restoreDualFwkService->BuildFileInfo();
227 dualFwkSettingItem.settingType = static_cast<int32_t>(ToneSettingType::TONE_SETTING_TYPE_SHOT);
228 g_restoreDualFwkService->BuildFileInfo();
229 dualFwkSettingItem.settingType = static_cast<int32_t>(ToneSettingType::TONE_SETTING_TYPE_NOTIFICATION);
230 g_restoreDualFwkService->BuildFileInfo();
231 g_restoreDualFwkService->dualFwkSetting_ = nullptr;
232 int32_t ret = g_restoreDualFwkService->StartRestore();
233 EXPECT_EQ(ret, E_ERR);
234 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0007 end");
235 }
236
237 HWTEST_F(RingtoneDualFwkRestoreTest, ringtone_dualfwk_restore_test_0008, TestSize.Level0)
238 {
239 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0008 start");
240 DualFwkSettingItem dualFwkSettingItem;
241 string backupPath = "/data/test";
242 int32_t res = g_restoreDualFwkService->Init(backupPath);
243 ASSERT_EQ(res, E_SUCCESS);
244 ASSERT_NE(g_restoreDualFwkService->dualFwkSetting_, nullptr);
245 dualFwkSettingItem.settingType = static_cast<int32_t>(ToneSettingType::TONE_SETTING_TYPE_MAX);
246 dualFwkSettingItem.isTitle = true;
247 g_restoreDualFwkService->dualFwkSetting_->settings_.insert({1, dualFwkSettingItem});
248 std::vector<std::string> displayNames = g_restoreDualFwkService->dualFwkSetting_->GetDisplayNames();
249 EXPECT_FALSE(displayNames.empty());
250 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0008 end");
251 }
252
253 HWTEST_F(RingtoneDualFwkRestoreTest, ringtone_dualfwk_restore_test_0009, TestSize.Level0)
254 {
255 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0009 start");
256 DualFwkSettingItem dualFwkSettingItem;
257 ASSERT_NE(g_restoreDualFwkService->dualFwkSetting_, nullptr);
258 std::unique_ptr<DualFwkConfRow> conf = nullptr;
259 int32_t ret = g_restoreDualFwkService->dualFwkSetting_->ProcessConfRow(conf);
260 EXPECT_EQ(ret, E_ERR);
261 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0009 end");
262 }
263
264 HWTEST_F(RingtoneDualFwkRestoreTest, ringtone_dualfwk_restore_test_0010, TestSize.Level0)
265 {
266 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0009 start");
267 EXPECT_NE(g_restoreDualFwkService->dualFwkSetting_, nullptr);
268 g_restoreDualFwkService->StartRestore();
269 std::unique_ptr<DualFwkConfRow> conf = std::make_unique<DualFwkConfRow>();
270 string confName = "ringtone_test";
271 const string confValue = TEST_BACKUP_DATA;
272 const string isSysSet = "true";
273 conf->name = confName;
274 conf->defaultSysSet = isSysSet;
275 conf->value = confValue;
276 auto ret = g_restoreDualFwkService->dualFwkSetting_->ProcessConfRow(conf);
277 EXPECT_EQ(ret, E_ERR);
278 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0009 end");
279 }
280
281 HWTEST_F(RingtoneDualFwkRestoreTest, ringtone_dualfwk_restore_test_0011, TestSize.Level0)
282 {
283 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0010 start");
284 NativeRdb::RdbStoreConfig config(RINGTONE_LIBRARY_DB_NAME);
285 config.SetPath("/data/app/el2/100/database/com.ohos.ringtonelibrary.ringtonelibrarydata/rdb/"
286 + RINGTONE_LIBRARY_DB_NAME);
287 config.SetBundleName("xx");
288 config.SetReadConSize(10);
289 config.SetSecurityLevel(NativeRdb::SecurityLevel::S3);
290
291 int32_t err;
292 RingtoneDataCallBack cb;
293 auto rdbStore = NativeRdb::RdbHelper::GetRdbStore(config, RINGTONE_RDB_VERSION, cb, err);
294 std::map<std::string, std::vector<std::shared_ptr<FileInfo>>> resultFromRingtone;
295 auto restore = std::make_unique<RingtoneDualFwkRestoreClone>();
296 EXPECT_NE(restore, nullptr);
297 restore->Init("/");
298 auto stageContext = std::make_shared<AbilityRuntime::ContextImpl>();
299 auto abilityContextImpl = std::make_shared<OHOS::AbilityRuntime::AbilityContextImpl>();
300 abilityContextImpl->SetStageContext(stageContext);
301 shared_ptr<RingtoneUnistore> rUniStore = RingtoneRdbStore::GetInstance(abilityContextImpl);
302 rUniStore->Stop();
303 int32_t ret = restore->QueryRingToneDbForFileInfo(rdbStore, {"Creek.ogg", "Dawn.ogg", "Flourish.ogg"},
304 resultFromRingtone, "display_name");
305 EXPECT_EQ(ret, E_FAIL);
306 ret = rUniStore->Init();
307 EXPECT_EQ(ret, E_OK);
308 RINGTONE_INFO_LOG("ringtone_dualfwk_restore_test_0010 start");
309 }
310 } // namespace Media
311 } // namespace OHOS