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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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.Level0)
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));
__anon8c8f2c690202(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 } // DistributedHardware
328 } // OHOS
329