• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "UTTest_device_name_manager.h"
17 #include "bundle_mgr_mock.h"
18 #include "datashare_result_set_mock.h"
19 #include "dm_constants.h"
20 #include "dm_system_ability_manager_mock.h"
21 
22 using namespace testing;
23 using namespace OHOS::DataShare;
24 
25 namespace OHOS {
26 namespace DistributedHardware {
27 namespace {
28 constexpr int32_t DEFAULT_USER_ID = -1;
29 constexpr int32_t DEFAULT_VALUABLE_USER_ID = 0;
30 const std::string SETTINGS_GENERAL_DEVICE_NAME = "settings.general.device_name";
31 } // namespace
SetUp()32 void DeviceNameManagerTest::SetUp()
33 {
34     multipleUserConnector_ = std::make_shared<MultipleUserConnectorMock>();
35     DmMultipleUserConnector::dmMultipleUserConnector = multipleUserConnector_;
36     auto client = ISystemAbilityManagerClient::GetOrCreateSAMgrClient();
37     client_ = std::static_pointer_cast<SystemAbilityManagerClientMock>(client);
38     helper_ = DataShareHelperMock::GetOrCreateInstance();
39 }
40 
TearDown()41 void DeviceNameManagerTest::TearDown()
42 {
43     DmMultipleUserConnector::dmMultipleUserConnector = nullptr;
44     multipleUserConnector_ = nullptr;
45     ISystemAbilityManagerClient::ReleaseSAMgrClient();
46     client_ = nullptr;
47     DataShareHelperMock::ReleaseInstance();
48     helper_ = nullptr;
49 }
50 
SetUpTestCase()51 void DeviceNameManagerTest::SetUpTestCase()
52 {}
53 
TearDownTestCase()54 void DeviceNameManagerTest::TearDownTestCase()
55 {}
56 
57 /**
58  * @tc.name: Init_001
59  * @tc.type: FUNC
60  */
61 HWTEST_F(DeviceNameManagerTest, Init_001, testing::ext::TestSize.Level1)
62 {
63     ASSERT_TRUE(client_ != nullptr);
64     ASSERT_TRUE(multipleUserConnector_ != nullptr);
65     ASSERT_TRUE(helper_ != nullptr);
66 
67     std::string subffixName = "手机";
68     auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
69     auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
70     EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
71         .WillRepeatedly(Return(bundleMgr));
72     EXPECT_CALL(*client_, GetSystemAbilityManager())
73         .WillRepeatedly(Return(systemAbilityManager));
74     EXPECT_CALL(*multipleUserConnector_, GetCurrentAccountUserID()).WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
75     auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
76     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
77     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
78 
79     EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK)));
80     EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(
81         DoAll(SetArgReferee<1>(subffixName), Return(DataShare::E_OK)));
82 
83     DeviceNameManager::GetInstance().InitDeviceNameWhenSoftBusReady();
84     DeviceNameManager::GetInstance().UnInit();
85 }
86 
87 /**
88  * @tc.name: InitDeviceNameWhenUserSwitch_001
89  * @tc.type: FUNC
90  */
91 HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenUserSwitch_001, testing::ext::TestSize.Level1)
92 {
93     ASSERT_TRUE(client_ != nullptr);
94     ASSERT_TRUE(multipleUserConnector_ != nullptr);
95     ASSERT_TRUE(helper_ != nullptr);
96 
97     std::string subffixName = "手机";
98     int32_t curUserId = DEFAULT_VALUABLE_USER_ID + 1;
99     int32_t preUserId = DEFAULT_VALUABLE_USER_ID;
100     auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
101     auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
102     EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
103         .WillRepeatedly(Return(bundleMgr));
104     EXPECT_CALL(*client_, GetSystemAbilityManager())
105         .WillRepeatedly(Return(systemAbilityManager));
106     EXPECT_CALL(*multipleUserConnector_, GetCurrentAccountUserID()).WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
107     auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
108     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
109     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
110 
111     EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK)));
112     EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(
113         DoAll(SetArgReferee<1>(subffixName), Return(DataShare::E_OK)));
114 
115     DeviceNameManager::GetInstance().InitDeviceNameWhenSoftBusReady();
116     DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(curUserId, preUserId);
117     DeviceNameManager::GetInstance().UnInit();
118 }
119 
120 /**
121  * @tc.name: InitDeviceNameWhenUserSwitch_002
122  * @tc.type: FUNC
123  */
124 HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenUserSwitch_002, testing::ext::TestSize.Level2)
125 {
126     ASSERT_TRUE(client_ != nullptr);
127     ASSERT_TRUE(helper_ != nullptr);
128 
129     std::string subffixName = "手机";
130     int32_t curUserId = DEFAULT_USER_ID;
131     int32_t preUserId = DEFAULT_USER_ID;
132 
133     auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
134     auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
135     EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
136         .WillRepeatedly(Return(bundleMgr));
137     EXPECT_CALL(*client_, GetSystemAbilityManager())
138         .WillRepeatedly(Return(systemAbilityManager));
139     auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
140     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
141     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
142 
143     EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK)));
144     EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(
145         DoAll(SetArgReferee<1>(subffixName), Return(DataShare::E_OK)));
146 
147     DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(curUserId, preUserId);
148     DeviceNameManager::GetInstance().UnInit();
149 }
150 
151 /**
152  * @tc.name: InitDeviceNameWhenLogout_001
153  * @tc.type: FUNC
154  */
155 HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenLogout_001, testing::ext::TestSize.Level1)
156 {
157     ASSERT_TRUE(client_ != nullptr);
158     ASSERT_TRUE(multipleUserConnector_ != nullptr);
159     ASSERT_TRUE(helper_ != nullptr);
160 
161     std::string subffixName = "手机";
162     auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
163     auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
164     EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
165         .WillRepeatedly(Return(bundleMgr));
166     EXPECT_CALL(*client_, GetSystemAbilityManager())
167         .WillRepeatedly(Return(systemAbilityManager));
168     EXPECT_CALL(*multipleUserConnector_, GetCurrentAccountUserID()).WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
169     auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
170     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
171     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
172 
173     EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK)));
174     EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(
175         DoAll(SetArgReferee<1>(subffixName), Return(DataShare::E_OK)));
176 
177     DeviceNameManager::GetInstance().InitDeviceNameWhenLogout();
178     DeviceNameManager::GetInstance().UnInit();
179 }
180 
181 /**
182  * @tc.name: InitDeviceNameWhenLogin_001
183  * @tc.type: FUNC
184  */
185 HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenLogin_001, testing::ext::TestSize.Level1)
186 {
187     ASSERT_TRUE(client_ != nullptr);
188     ASSERT_TRUE(multipleUserConnector_ != nullptr);
189     ASSERT_TRUE(helper_ != nullptr);
190 
191     std::string subffixName = "手机";
192     auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
193     auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
194     EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
195         .WillRepeatedly(Return(bundleMgr));
196     EXPECT_CALL(*client_, GetSystemAbilityManager())
197         .WillRepeatedly(Return(systemAbilityManager));
198     EXPECT_CALL(*multipleUserConnector_, GetCurrentAccountUserID()).WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
199     auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
200     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
201     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
202 
203     EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK)));
204     EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(
205         DoAll(SetArgReferee<1>(subffixName), Return(DataShare::E_OK)));
206 
207     DeviceNameManager::GetInstance().InitDeviceNameWhenLogin();
208     DeviceNameManager::GetInstance().UnInit();
209 }
210 
211 /**
212  * @tc.name: InitDeviceNameWhenNickChange_001
213  * @tc.type: FUNC
214  */
215 HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenNickChange_001, testing::ext::TestSize.Level1)
216 {
217     ASSERT_TRUE(client_ != nullptr);
218     ASSERT_TRUE(multipleUserConnector_ != nullptr);
219     ASSERT_TRUE(helper_ != nullptr);
220 
221     std::string subffixName = "手机";
222     auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
223     auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
224     EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
225         .WillRepeatedly(Return(bundleMgr));
226     EXPECT_CALL(*client_, GetSystemAbilityManager())
227         .WillRepeatedly(Return(systemAbilityManager));
228     EXPECT_CALL(*multipleUserConnector_, GetCurrentAccountUserID()).WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
229     auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
230     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
231     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
232 
233     EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK)));
234     EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(
235         DoAll(SetArgReferee<1>(subffixName), Return(DataShare::E_OK)));
236 
237     DeviceNameManager::GetInstance().InitDeviceNameWhenNickChange();
238     DeviceNameManager::GetInstance().UnInit();
239 }
240 
241 /**
242  * @tc.name: InitDeviceNameWhenNameChange_001
243  * @tc.type: FUNC
244  */
245 HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenNameChange_001, testing::ext::TestSize.Level1)
246 {
247     ASSERT_TRUE(client_ != nullptr);
248     ASSERT_TRUE(multipleUserConnector_ != nullptr);
249     ASSERT_TRUE(helper_ != nullptr);
250 
251     std::string prefixName = "Mr.诸葛张三";
252     auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
253     auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
254     EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
255         .WillRepeatedly(Return(bundleMgr));
256     EXPECT_CALL(*client_, GetSystemAbilityManager())
257         .WillRepeatedly(Return(systemAbilityManager));
258     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(nullptr));
259     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
260     EXPECT_CALL(*multipleUserConnector_, GetAccountNickName(_)).WillRepeatedly(Return(prefixName));
261 
262     DeviceNameManager::GetInstance().InitDeviceNameWhenNameChange(DEFAULT_VALUABLE_USER_ID);
263     DeviceNameManager::GetInstance().UnInit();
264 }
265 
266 /**
267  * @tc.name: GetLocalDisplayDeviceName_001
268  * @tc.type: FUNC
269  */
270 HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_001, testing::ext::TestSize.Level1)
271 {
272     ASSERT_TRUE(client_ != nullptr);
273     ASSERT_TRUE(multipleUserConnector_ != nullptr);
274     ASSERT_TRUE(helper_ != nullptr);
275 
276     size_t getStringInvokeCount = 0;
277     auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
278     auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
279     EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
280         .WillRepeatedly(Return(bundleMgr));
281     EXPECT_CALL(*client_, GetSystemAbilityManager())
282         .WillRepeatedly(Return(systemAbilityManager));
283     EXPECT_CALL(*multipleUserConnector_, GetCurrentAccountUserID()).WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
284     auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
285     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
286     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
287 
288     EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK)));
289     EXPECT_CALL(*resultSet, GoToRow(_)).Times(AtLeast(1));
__anon6792a3cb0202(int, std::string &output) 290     EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(Invoke([&getStringInvokeCount](int, std::string &output) {
291         std::string subffixName = "OH-3.2";
292         if (getStringInvokeCount++ > 0) {
293             output = subffixName;
294         }
295         return DM_OK;
296     }));
297     EXPECT_CALL(*resultSet, Close()).Times(AtLeast(1));
298     std::string prefixName = "Mr.诸葛张三";
299     EXPECT_CALL(*multipleUserConnector_, GetAccountNickName(_)).WillRepeatedly(Return(prefixName));
300     for (size_t i = 0; i < 2; ++i) {
301         int32_t maxNamelength = 24;
302         std::string output;
303         auto result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(maxNamelength, output);
304         EXPECT_EQ(result, DM_OK);
305     }
306 }
307 
308 /**
309  * @tc.name: GetLocalDisplayDeviceName_002
310  * @tc.type: FUNC
311  */
312 HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_002, testing::ext::TestSize.Level2)
313 {
314     std::string output;
315     int32_t maxNamelength = -1;
316     auto result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(maxNamelength, output);
317     EXPECT_EQ(result, ERR_DM_INPUT_PARA_INVALID);
318 
319     maxNamelength = 10;
320     result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(maxNamelength, output);
321     EXPECT_EQ(result, ERR_DM_INPUT_PARA_INVALID);
322 
323     maxNamelength = 101;
324     result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(maxNamelength, output);
325     EXPECT_EQ(result, ERR_DM_INPUT_PARA_INVALID);
326 }
327 
328 HWTEST_F(DeviceNameManagerTest, ReleaseDataShareHelper_001, testing::ext::TestSize.Level1)
329 {
330     std::shared_ptr<DataShareHelperMock> helper = nullptr;
331     bool ret = DeviceNameManager::GetInstance().ReleaseDataShareHelper(helper);
332     EXPECT_EQ(ret, false);
333 }
334 
335 HWTEST_F(DeviceNameManagerTest, ReleaseDataShareHelper_002, testing::ext::TestSize.Level1)
336 {
337     ASSERT_TRUE(helper_ != nullptr);
338     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(false));
339     bool ret = DeviceNameManager::GetInstance().ReleaseDataShareHelper(helper_);
340     EXPECT_EQ(ret, false);
341 }
342 
343 HWTEST_F(DeviceNameManagerTest, ReleaseDataShareHelper_003, testing::ext::TestSize.Level1)
344 {
345     ASSERT_TRUE(helper_ != nullptr);
346     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
347     bool ret = DeviceNameManager::GetInstance().ReleaseDataShareHelper(helper_);
348     EXPECT_EQ(ret, true);
349 }
350 
351 HWTEST_F(DeviceNameManagerTest, MakeUri_001, testing::ext::TestSize.Level1)
352 {
353     std::string proxyUri = "";
354     std::string key = "key";
355     auto ret = DeviceNameManager::GetInstance().MakeUri(proxyUri, key);
356     EXPECT_EQ(ret, Uri(proxyUri + "&key=" + key));
357 }
358 
359 HWTEST_F(DeviceNameManagerTest, GetProxyUriStr_001, testing::ext::TestSize.Level1)
360 {
361     std::string tableName = "SETTINGSDATA";
362     int32_t userId = 12;
363     auto ret = DeviceNameManager::GetInstance().GetProxyUriStr(tableName, userId);
364     EXPECT_EQ(ret, "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true");
365 }
366 
367 HWTEST_F(DeviceNameManagerTest, GetProxyUriStr_002, testing::ext::TestSize.Level1)
368 {
369     std::string tableName = "tableName";
370     int32_t userId = 12;
371     auto ret = DeviceNameManager::GetInstance().GetProxyUriStr(tableName, userId);
372     EXPECT_EQ(ret, "datashare:///com.ohos.settingsdata/entry/settingsdata/tableName100?Proxy=true");
373 }
374 
375 HWTEST_F(DeviceNameManagerTest, SetValue_001, testing::ext::TestSize.Level1)
376 {
377     ASSERT_TRUE(helper_ != nullptr);
378 
379     std::string tableName = "tableName";
380     int32_t userId = 12;
381     std::string key = "key";
382     std::string value = "value";
383     int32_t ret = -1;
384     EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret));
385     EXPECT_CALL(*helper_, Insert(_, _)).WillRepeatedly(Return(ret));
386     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
387 
388     auto result = DeviceNameManager::GetInstance().SetValue(tableName, userId, key, value);
389     EXPECT_EQ(result, -1);
390 }
391 
392 HWTEST_F(DeviceNameManagerTest, SetValue_002, testing::ext::TestSize.Level1)
393 {
394     ASSERT_TRUE(helper_ != nullptr);
395 
396     std::string tableName = "tableName";
397     int32_t userId = 12;
398     std::string key = "key";
399     std::string value = "value";
400     int32_t ret = 1;
401     EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret));
402     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
403 
404     auto result = DeviceNameManager::GetInstance().SetValue(tableName, userId, key, value);
405     EXPECT_EQ(result, 1);
406 }
407 
408 HWTEST_F(DeviceNameManagerTest, GetValue_001, testing::ext::TestSize.Level1)
409 {
410     ASSERT_TRUE(helper_ != nullptr);
411 
412     std::string tableName = "tableName";
413     int32_t userId = 12;
414     std::string key = "key";
415     std::string value = "value";
416     std::shared_ptr<DataShareResultSet> resultSet = nullptr;
417     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
418     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
419 
420     auto result = DeviceNameManager::GetInstance().GetValue(tableName, userId, key, value);
421     EXPECT_EQ(result, ERR_DM_POINT_NULL);
422 }
423 
424 HWTEST_F(DeviceNameManagerTest, GetValue_002, testing::ext::TestSize.Level1)
425 {
426     ASSERT_TRUE(helper_ != nullptr);
427 
428     std::string tableName = "tableName";
429     int32_t userId = 12;
430     std::string key = "key";
431     std::string value = "value";
432     auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
433     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
434     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
435 
436     EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK)));
437     EXPECT_CALL(*resultSet, GoToRow(_)).Times(AtLeast(1));
438     EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(Return(DataShare::E_OK));
439     EXPECT_CALL(*resultSet, Close()).Times(AtLeast(1));
440     auto result = DeviceNameManager::GetInstance().GetValue(tableName, userId, key, value);
441     EXPECT_EQ(result, DM_OK);
442 }
443 
444 HWTEST_F(DeviceNameManagerTest, GetValue_003, testing::ext::TestSize.Level1)
445 {
446     ASSERT_TRUE(helper_ != nullptr);
447 
448     std::string tableName = "tableName";
449     int32_t userId = 12;
450     std::string key = "key";
451     std::string value = "value";
452     auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
453     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
454     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
455 
456     EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK)));
457     EXPECT_CALL(*resultSet, GoToRow(_)).Times(AtLeast(1));
458     EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(Return(6666));
459     EXPECT_CALL(*resultSet, Close()).Times(AtLeast(1));
460     auto result = DeviceNameManager::GetInstance().GetValue(tableName, userId, key, value);
461     EXPECT_EQ(result, 6666);
462 }
463 
464 HWTEST_F(DeviceNameManagerTest, GetValue_004, testing::ext::TestSize.Level1)
465 {
466     ASSERT_TRUE(helper_ != nullptr);
467 
468     std::string tableName = "tableName";
469     int32_t userId = 12;
470     std::string key = "key";
471     std::string value = "value";
472     auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
473     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
474     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
475 
476     EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(0), Return(DataShare::E_OK)));
477     EXPECT_CALL(*resultSet, Close()).Times(AtLeast(1));
478     auto result = DeviceNameManager::GetInstance().GetValue(tableName, userId, key, value);
479     EXPECT_EQ(result, DM_OK);
480 }
481 
482 HWTEST_F(DeviceNameManagerTest, GetRemoteObj_001, testing::ext::TestSize.Level1)
483 {
484     ASSERT_TRUE(client_ != nullptr);
485     auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
486     auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
487     EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
488         .WillRepeatedly(Return(bundleMgr));
489     EXPECT_CALL(*client_, GetSystemAbilityManager())
490         .WillRepeatedly(Return(systemAbilityManager));
491     auto ret = DeviceNameManager::GetInstance().GetRemoteObj();
492     EXPECT_NE(ret, nullptr);
493 }
494 
495 HWTEST_F(DeviceNameManagerTest, SetDeviceName_001, testing::ext::TestSize.Level1)
496 {
497     std::string deviceName = "";
498     auto ret = DeviceNameManager::GetInstance().SetDeviceName(deviceName);
499     EXPECT_EQ(ret, ERR_DM_NAME_EMPTY);
500 }
501 
502 HWTEST_F(DeviceNameManagerTest, SetDeviceName_002, testing::ext::TestSize.Level1)
503 {
504     ASSERT_TRUE(helper_ != nullptr);
505     std::string deviceName = "deviceName";
506     int32_t ret = 1;
507     EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret));
508     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
509     auto result = DeviceNameManager::GetInstance().SetDeviceName(deviceName);
510     EXPECT_EQ(result, 1);
511 }
512 
513 HWTEST_F(DeviceNameManagerTest, GetDeviceName_001, testing::ext::TestSize.Level1)
514 {
515     ASSERT_TRUE(helper_ != nullptr);
516 
517     std::string deviceName = "deviceName";
518     std::shared_ptr<DataShareResultSet> resultSet = nullptr;
519     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
520     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
521 
522     auto result = DeviceNameManager::GetInstance().GetDeviceName(deviceName);
523     EXPECT_EQ(result, ERR_DM_POINT_NULL);
524 }
525 
526 HWTEST_F(DeviceNameManagerTest, SetDisplayDeviceName_001, testing::ext::TestSize.Level1)
527 {
528     std::string deviceName = "";
529     int32_t userId = 12;
530     auto result = DeviceNameManager::GetInstance().SetDisplayDeviceName(deviceName, userId);
531     EXPECT_EQ(result, ERR_DM_NAME_EMPTY);
532 }
533 
534 HWTEST_F(DeviceNameManagerTest, SetDisplayDeviceName_002, testing::ext::TestSize.Level1)
535 {
536     ASSERT_TRUE(helper_ != nullptr);
537     std::string deviceName = "deviceName";
538     int32_t userId = 12;
539     int32_t ret = 1;
540     EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret));
541     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
542 
543     auto result = DeviceNameManager::GetInstance().SetDisplayDeviceName(deviceName, userId);
544     EXPECT_EQ(result, 1);
545 }
546 
547 HWTEST_F(DeviceNameManagerTest, SetDisplayDeviceNameState_001, testing::ext::TestSize.Level1)
548 {
549     ASSERT_TRUE(helper_ != nullptr);
550     std::string state = "state";
551     int32_t userId = 12;
552     int32_t ret = 1;
553     EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret));
554     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
555 
556     auto result = DeviceNameManager::GetInstance().SetDisplayDeviceNameState(state, userId);
557     EXPECT_EQ(result, 1);
558 }
559 
560 HWTEST_F(DeviceNameManagerTest, GetDisplayDeviceName_001, testing::ext::TestSize.Level1)
561 {
562     ASSERT_TRUE(helper_ != nullptr);
563     std::string deviceName = "deviceName";
564     int32_t userId = 12;
565 
566     std::shared_ptr<DataShareResultSet> resultSet = nullptr;
567     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
568     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
569 
570     auto result = DeviceNameManager::GetInstance().GetDisplayDeviceName(userId, deviceName);
571     EXPECT_EQ(result, ERR_DM_POINT_NULL);
572 }
573 
574 HWTEST_F(DeviceNameManagerTest, SetUserDefinedDeviceName_001, testing::ext::TestSize.Level1)
575 {
576     ASSERT_TRUE(helper_ != nullptr);
577     std::string deviceName = "deviceName";
578     int32_t userId = 12;
579 
580     int32_t ret = 1;
581     EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(ret));
582     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
583 
584     auto result = DeviceNameManager::GetInstance().SetUserDefinedDeviceName(deviceName, userId);
585     EXPECT_EQ(result, 1);
586 }
587 
588 HWTEST_F(DeviceNameManagerTest, SubstrByBytes_001, testing::ext::TestSize.Level1)
589 {
590     std::string str = "str";
591     int32_t maxNumBytes = 12;
592     auto result = DeviceNameManager::GetInstance().SubstrByBytes(str, maxNumBytes);
593     EXPECT_EQ(result, str);
594 }
595 
596 HWTEST_F(DeviceNameManagerTest, SubstrByBytes_002, testing::ext::TestSize.Level1)
597 {
598     std::string str = "HelloWorld";
599     int32_t maxNumBytes = 5;
600     auto result = DeviceNameManager::GetInstance().SubstrByBytes(str, maxNumBytes);
601     EXPECT_EQ(result, "Hello");
602 }
603 
604 HWTEST_F(DeviceNameManagerTest, GetUserDefinedDeviceName_001, testing::ext::TestSize.Level1)
605 {
606     ASSERT_TRUE(helper_ != nullptr);
607     std::string deviceName = "deviceName";
608     int32_t userId = 12;
609     std::shared_ptr<DataShareResultSet> resultSet = nullptr;
610     EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
611     EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
612     auto result = DeviceNameManager::GetInstance().GetUserDefinedDeviceName(userId, deviceName);
613     EXPECT_EQ(result, ERR_DM_POINT_NULL);
614 }
615 
616 HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_003, testing::ext::TestSize.Level1)
617 {
618     std::string prefixName = "My";
619     std::string subffixName = "Device";
620     int32_t maxNameLength = 20;
621     std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
622         subffixName, maxNameLength);
623     EXPECT_EQ(result, "My的Device");
624 }
625 
626 HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_004, testing::ext::TestSize.Level1)
627 {
628     std::string prefixName = "MyVeryLong";
629     std::string subffixName = "DeviceName";
630     int32_t maxNameLength = 15;
631     std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
632         subffixName, maxNameLength);
633     EXPECT_EQ(result, "My...DeviceName");
634 }
635 
636 HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_005, testing::ext::TestSize.Level1)
637 {
638     std::string prefixName = "My";
639     std::string subffixName = "Device";
640     int32_t maxNameLength = 0;
641     std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
642         subffixName, maxNameLength);
643     EXPECT_EQ(result, "My的Device");
644 }
645 
646 HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_006, testing::ext::TestSize.Level1)
647 {
648     std::string prefixName = "My";
649     std::string subffixName = "VeryLongDeviceNameThatExceedsLimit";
650     int32_t maxNameLength = 30;
651     std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
652         subffixName, maxNameLength);
653     EXPECT_EQ(result, "My的VeryLongDevi...");
654 }
655 
656 HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_007, testing::ext::TestSize.Level1)
657 {
658     std::string prefixName = "";
659     std::string subffixName = "Device";
660     int32_t maxNameLength = 10;
661     std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
662         subffixName, maxNameLength);
663     EXPECT_EQ(result, "Device");
664 }
665 
666 HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_008, testing::ext::TestSize.Level1)
667 {
668     std::string prefixName = "";
669     std::string subffixName = "VeryLongDeviceName";
670     int32_t maxNameLength = 10;
671     std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
672         subffixName, maxNameLength);
673     EXPECT_EQ(result, "VeryLon...");
674 }
675 
676 HWTEST_F(DeviceNameManagerTest, DependsIsReady_001, testing::ext::TestSize.Level1)
677 {
678     bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
679     DeviceNameManager::GetInstance().isDataShareReady_ = false;
680     bool result = DeviceNameManager::GetInstance().DependsIsReady();
681     EXPECT_FALSE(result);
682     DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
683 }
684 
685 HWTEST_F(DeviceNameManagerTest, DependsIsReady_002, testing::ext::TestSize.Level1)
686 {
687     bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
688     bool srcAccountSysReady = DeviceNameManager::GetInstance().isAccountSysReady_;
689     DeviceNameManager::GetInstance().isDataShareReady_ = true;
690     DeviceNameManager::GetInstance().isAccountSysReady_ = false;
691     EXPECT_CALL(*multipleUserConnector_, GetCurrentAccountUserID()).WillRepeatedly(Return(DEFAULT_USER_ID));
692     bool result = DeviceNameManager::GetInstance().DependsIsReady();
693     EXPECT_FALSE(result);
694     DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
695     DeviceNameManager::GetInstance().isAccountSysReady_ = srcAccountSysReady;
696 }
697 
698 HWTEST_F(DeviceNameManagerTest, DependsIsReady_003, testing::ext::TestSize.Level1)
699 {
700     bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
701     bool srcAccountSysReady = DeviceNameManager::GetInstance().isAccountSysReady_;
702     sptr<IRemoteObject> srcRemoteObj = DeviceNameManager::GetInstance().remoteObj_;
703     DeviceNameManager::GetInstance().isDataShareReady_ = true;
704     DeviceNameManager::GetInstance().isAccountSysReady_ = false;
705     DeviceNameManager::GetInstance().remoteObj_ = nullptr;
706     EXPECT_CALL(*multipleUserConnector_, GetCurrentAccountUserID()).WillRepeatedly(Return(0));
707     bool result = DeviceNameManager::GetInstance().DependsIsReady();
708     EXPECT_TRUE(DeviceNameManager::GetInstance().isAccountSysReady_);
709     EXPECT_FALSE(result);
710     DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
711     DeviceNameManager::GetInstance().isAccountSysReady_ = srcAccountSysReady;
712     DeviceNameManager::GetInstance().remoteObj_ = srcRemoteObj;
713 }
714 } // DistributedHardware
715 } // OHOS
716