/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "UTTest_device_name_manager.h"
#include "bundle_mgr_mock.h"
#include "datashare_result_set_mock.h"
#include "dm_constants.h"
#include "dm_system_ability_manager_mock.h"

using namespace testing;
using namespace OHOS::DataShare;

namespace OHOS {
namespace DistributedHardware {
namespace {
constexpr int32_t DEFAULT_USER_ID = -1;
constexpr int32_t DEFAULT_VALUABLE_USER_ID = 0;
const std::string SETTINGS_GENERAL_DEVICE_NAME = "settings.general.device_name";
} // namespace
void DeviceNameManagerTest::SetUp()
{
    multipleUserConnector_ = std::make_shared<MultipleUserConnectorMock>();
    DmMultipleUserConnector::dmMultipleUserConnector = multipleUserConnector_;
    auto client = ISystemAbilityManagerClient::GetOrCreateSAMgrClient();
    client_ = std::static_pointer_cast<SystemAbilityManagerClientMock>(client);
    helper_ = DataShareHelperMock::GetOrCreateInstance();
}

void DeviceNameManagerTest::TearDown()
{
    DmMultipleUserConnector::dmMultipleUserConnector = nullptr;
    multipleUserConnector_ = nullptr;
    ISystemAbilityManagerClient::ReleaseSAMgrClient();
    client_ = nullptr;
    DataShareHelperMock::ReleaseInstance();
    helper_ = nullptr;
}

void DeviceNameManagerTest::SetUpTestCase()
{}

void DeviceNameManagerTest::TearDownTestCase()
{}

/**
 * @tc.name: Init_001
 * @tc.type: FUNC
 */
/**
 * @tc.name: InitDeviceNameWhenUserSwitch_001
 * @tc.type: FUNC
 */
/**
 * @tc.name: InitDeviceNameWhenUserSwitch_002
 * @tc.type: FUNC
 */
/**
 * @tc.name: InitDeviceNameWhenLogout_001
 * @tc.type: FUNC
 */
/**
 * @tc.name: InitDeviceNameWhenLogin_001
 * @tc.type: FUNC
 */
/**
 * @tc.name: InitDeviceNameWhenNickChange_001
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenNickChange_001, testing::ext::TestSize.Level1)
{
    ASSERT_TRUE(client_ != nullptr);
    ASSERT_TRUE(multipleUserConnector_ != nullptr);
    ASSERT_TRUE(helper_ != nullptr);

    std::string suffixName = "手机";
    auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
    auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
    EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
        .WillRepeatedly(Return(bundleMgr));
    EXPECT_CALL(*client_, GetSystemAbilityManager())
        .WillRepeatedly(Return(systemAbilityManager));
    EXPECT_CALL(*multipleUserConnector_, GetMainDisplayActiveUserId())
        .WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
    auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
    EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
    EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));

    EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK)));
    EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(
        DoAll(SetArgReferee<1>(suffixName), Return(DataShare::E_OK)));

    DeviceNameManager::GetInstance().InitDeviceNameWhenNickChange();
    DeviceNameManager::GetInstance().UnInit();
}

/**
 * @tc.name: InitDeviceNameWhenNameChange_001
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenNameChange_001, testing::ext::TestSize.Level1)
{
    ASSERT_TRUE(client_ != nullptr);
    ASSERT_TRUE(multipleUserConnector_ != nullptr);
    ASSERT_TRUE(helper_ != nullptr);

    std::string prefixName = "Mr.诸葛张三";
    auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
    auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
    EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_))
        .WillRepeatedly(Return(bundleMgr));
    EXPECT_CALL(*client_, GetSystemAbilityManager())
        .WillRepeatedly(Return(systemAbilityManager));
    EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(nullptr));
    EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
    EXPECT_CALL(*multipleUserConnector_, GetMainDisplayActiveUserId())
        .WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
    EXPECT_CALL(*multipleUserConnector_, GetAccountNickName(_)).WillRepeatedly(Return(prefixName));

    DeviceNameManager::GetInstance().InitDeviceNameWhenNameChange(DEFAULT_VALUABLE_USER_ID);
    DeviceNameManager::GetInstance().UnInit();
}

/**
 * @tc.name: GetLocalDisplayDeviceName_002
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_002, testing::ext::TestSize.Level2)
{
    EXPECT_CALL(*multipleUserConnector_, GetMainDisplayActiveUserId())
        .WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
    std::string output;
    int32_t maxNamelength = -1;
    auto result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(maxNamelength, output);
    EXPECT_EQ(result, ERR_DM_INPUT_PARA_INVALID);

    maxNamelength = 10;
    result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(maxNamelength, output);
    EXPECT_EQ(result, ERR_DM_INPUT_PARA_INVALID);

    maxNamelength = 101;
    result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(maxNamelength, output);
    EXPECT_EQ(result, ERR_DM_INPUT_PARA_INVALID);
}

HWTEST_F(DeviceNameManagerTest, ReleaseDataShareHelper_001, testing::ext::TestSize.Level1)
{
    std::shared_ptr<DataShareHelperMock> helper = nullptr;
    bool ret = DeviceNameManager::GetInstance().ReleaseDataShareHelper(helper);
    EXPECT_EQ(ret, false);
}

HWTEST_F(DeviceNameManagerTest, MakeUri_001, testing::ext::TestSize.Level1)
{
    std::string proxyUri = "";
    std::string key = "key";
    auto ret = DeviceNameManager::GetInstance().MakeUri(proxyUri, key);
    EXPECT_EQ(ret, Uri(proxyUri + "&key=" + key));
}

HWTEST_F(DeviceNameManagerTest, GetProxyUriStr_001, testing::ext::TestSize.Level1)
{
    std::string tableName = "SETTINGSDATA";
    int32_t userId = 12;
    auto ret = DeviceNameManager::GetInstance().GetProxyUriStr(tableName, userId);
    EXPECT_EQ(ret, "datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true");
}

HWTEST_F(DeviceNameManagerTest, GetProxyUriStr_002, testing::ext::TestSize.Level1)
{
    std::string tableName = "tableName";
    int32_t userId = 12;
    auto ret = DeviceNameManager::GetInstance().GetProxyUriStr(tableName, userId);
    EXPECT_EQ(ret, "datashare:///com.ohos.settingsdata/entry/settingsdata/tableName100?Proxy=true");
}

HWTEST_F(DeviceNameManagerTest, SetDeviceName_001, testing::ext::TestSize.Level1)
{
    std::string deviceName = "";
    auto ret = DeviceNameManager::GetInstance().SetDeviceName(deviceName);
    EXPECT_EQ(ret, ERR_DM_NAME_EMPTY);
}

HWTEST_F(DeviceNameManagerTest, SetDisplayDeviceName_001, testing::ext::TestSize.Level1)
{
    std::string deviceName = "";
    int32_t userId = 12;
    auto result = DeviceNameManager::GetInstance().SetDisplayDeviceName(deviceName, userId);
    EXPECT_EQ(result, ERR_DM_NAME_EMPTY);
}

HWTEST_F(DeviceNameManagerTest, SubstrByBytes_001, testing::ext::TestSize.Level1)
{
    std::string str = "str";
    int32_t maxNumBytes = 12;
    auto result = DeviceNameManager::GetInstance().SubstrByBytes(str, maxNumBytes);
    EXPECT_EQ(result, str);
}

HWTEST_F(DeviceNameManagerTest, SubstrByBytes_002, testing::ext::TestSize.Level1)
{
    std::string str = "HelloWorld";
    int32_t maxNumBytes = 5;
    auto result = DeviceNameManager::GetInstance().SubstrByBytes(str, maxNumBytes);
    EXPECT_EQ(result, "Hello");
}

HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_003, testing::ext::TestSize.Level1)
{
    std::string prefixName = "My";
    std::string suffixName = "Device";
    int32_t maxNameLength = 20;
    std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
        suffixName, maxNameLength);
    EXPECT_EQ(result, "My的Device");
}

HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_004, testing::ext::TestSize.Level1)
{
    std::string prefixName = "MyVeryLong";
    std::string suffixName = "DeviceName";
    int32_t maxNameLength = 15;
    std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
        suffixName, maxNameLength);
    EXPECT_EQ(result, "My...DeviceName");
}

HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_005, testing::ext::TestSize.Level1)
{
    std::string prefixName = "My";
    std::string suffixName = "Device";
    int32_t maxNameLength = 0;
    std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
        suffixName, maxNameLength);
    EXPECT_EQ(result, "My的Device");
}

HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_006, testing::ext::TestSize.Level1)
{
    std::string prefixName = "My";
    std::string suffixName = "VeryLongDeviceNameThatExceedsLimit";
    int32_t maxNameLength = 30;
    std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
        suffixName, maxNameLength);
    EXPECT_EQ(result, "My的VeryLongDevi...");
}

HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_007, testing::ext::TestSize.Level1)
{
    std::string prefixName = "";
    std::string suffixName = "Device";
    int32_t maxNameLength = 10;
    std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
        suffixName, maxNameLength);
    EXPECT_EQ(result, "Device");
}

HWTEST_F(DeviceNameManagerTest, GetLocalDisplayDeviceName_008, testing::ext::TestSize.Level1)
{
    std::string prefixName = "";
    std::string suffixName = "VeryLongDeviceName";
    int32_t maxNameLength = 10;
    std::string result = DeviceNameManager::GetInstance().GetLocalDisplayDeviceName(prefixName,
        suffixName, maxNameLength);
    EXPECT_EQ(result, "VeryLon...");
}

HWTEST_F(DeviceNameManagerTest, DependsIsReady_001, testing::ext::TestSize.Level1)
{
    bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
    DeviceNameManager::GetInstance().isDataShareReady_ = false;
    bool result = DeviceNameManager::GetInstance().DependsIsReady();
    EXPECT_FALSE(result);
    DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
}

HWTEST_F(DeviceNameManagerTest, DependsIsReady_002, testing::ext::TestSize.Level1)
{
    bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
    bool srcAccountSysReady = DeviceNameManager::GetInstance().isAccountSysReady_;
    DeviceNameManager::GetInstance().isDataShareReady_ = true;
    DeviceNameManager::GetInstance().isAccountSysReady_ = false;
    EXPECT_CALL(*multipleUserConnector_, GetCurrentAccountUserID()).WillRepeatedly(Return(DEFAULT_USER_ID));
    bool result = DeviceNameManager::GetInstance().DependsIsReady();
    EXPECT_FALSE(result);
    DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
    DeviceNameManager::GetInstance().isAccountSysReady_ = srcAccountSysReady;
}

HWTEST_F(DeviceNameManagerTest, DependsIsReady_003, testing::ext::TestSize.Level1)
{
    bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
    bool srcAccountSysReady = DeviceNameManager::GetInstance().isAccountSysReady_;
    sptr<IRemoteObject> srcRemoteObj = DeviceNameManager::GetInstance().remoteObj_;
    DeviceNameManager::GetInstance().isDataShareReady_ = true;
    DeviceNameManager::GetInstance().isAccountSysReady_ = false;
    DeviceNameManager::GetInstance().remoteObj_ = nullptr;
    EXPECT_CALL(*multipleUserConnector_, GetCurrentAccountUserID()).WillRepeatedly(Return(0));
    bool result = DeviceNameManager::GetInstance().DependsIsReady();
    EXPECT_TRUE(DeviceNameManager::GetInstance().isAccountSysReady_);
    EXPECT_FALSE(result);
    DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
    DeviceNameManager::GetInstance().isAccountSysReady_ = srcAccountSysReady;
    DeviceNameManager::GetInstance().remoteObj_ = srcRemoteObj;
}

/**
 * @tc.name: InitDeviceNameWhenSoftBusReady_001
 * @tc.desc: InitDeviceNameWhenSoftBusReady returns DM_OK when depends are not ready.
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenSoftBusReady_001, testing::ext::TestSize.Level1)
{
    bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
    DeviceNameManager::GetInstance().isDataShareReady_ = false;
    int32_t ret = DeviceNameManager::GetInstance().InitDeviceNameWhenSoftBusReady();
    EXPECT_EQ(ret, DM_OK);
    DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
}

/**
 * @tc.name: InitDeviceNameWhenUserSwitch_001
 * @tc.desc: InitDeviceNameWhenUserSwitch calls GetMainDisplayActiveUserId when depends are ready.
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenUserSwitch_001, testing::ext::TestSize.Level1)
{
    ASSERT_TRUE(client_ != nullptr);
    ASSERT_TRUE(multipleUserConnector_ != nullptr);
    ASSERT_TRUE(helper_ != nullptr);

    bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
    bool srcAccountSysReady = DeviceNameManager::GetInstance().isAccountSysReady_;
    sptr<IRemoteObject> srcRemoteObj = DeviceNameManager::GetInstance().remoteObj_;
    DeviceNameManager::GetInstance().isDataShareReady_ = true;
    DeviceNameManager::GetInstance().isAccountSysReady_ = true;
    DeviceNameManager::GetInstance().remoteObj_ = nullptr;

    auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
    auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
    EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_)).WillRepeatedly(Return(bundleMgr));
    EXPECT_CALL(*client_, GetSystemAbilityManager()).WillRepeatedly(Return(systemAbilityManager));
    EXPECT_CALL(*multipleUserConnector_, GetMainDisplayActiveUserId())
        .WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
    auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
    EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
    EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
    EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(0), Return(DataShare::E_OK)));

    int32_t ret = DeviceNameManager::GetInstance().InitDeviceNameWhenUserSwitch(100, 101);
    EXPECT_EQ(ret, DM_OK);

    DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
    DeviceNameManager::GetInstance().isAccountSysReady_ = srcAccountSysReady;
    DeviceNameManager::GetInstance().remoteObj_ = srcRemoteObj;
}

/**
 * @tc.name: InitDeviceNameWhenLogout_001
 * @tc.desc: InitDeviceNameWhenLogout returns DM_OK when depends are not ready.
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenLogout_001, testing::ext::TestSize.Level1)
{
    bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
    DeviceNameManager::GetInstance().isDataShareReady_ = false;
    int32_t ret = DeviceNameManager::GetInstance().InitDeviceNameWhenLogout();
    EXPECT_EQ(ret, DM_OK);
    DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
}

/**
 * @tc.name: InitDeviceNameWhenLogin_001
 * @tc.desc: InitDeviceNameWhenLogin returns DM_OK when depends are not ready.
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenLogin_001, testing::ext::TestSize.Level1)
{
    bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
    DeviceNameManager::GetInstance().isDataShareReady_ = false;
    int32_t ret = DeviceNameManager::GetInstance().InitDeviceNameWhenLogin();
    EXPECT_EQ(ret, DM_OK);
    DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
}

/**
 * @tc.name: InitDeviceNameWhenLanguageOrRegionChanged_001
 * @tc.desc: InitDeviceNameWhenLanguageOrRegionChanged returns DM_OK when depends are not ready.
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenLanguageOrRegionChanged_001, testing::ext::TestSize.Level1)
{
    bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
    DeviceNameManager::GetInstance().isDataShareReady_ = false;
    int32_t ret = DeviceNameManager::GetInstance().InitDeviceNameWhenLanguageOrRegionChanged();
    EXPECT_EQ(ret, DM_OK);
    DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
}

/**
 * @tc.name: GetUserDefinedDeviceName_001
 * @tc.desc: GetUserDefinedDeviceName calls GetMainDisplayActiveUserId to resolve the user id.
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, GetUserDefinedDeviceName_001, testing::ext::TestSize.Level1)
{
    ASSERT_TRUE(multipleUserConnector_ != nullptr);
    ASSERT_TRUE(helper_ != nullptr);

    EXPECT_CALL(*multipleUserConnector_, GetMainDisplayActiveUserId())
        .WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
    auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
    EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
    EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
    EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(0), Return(DataShare::E_OK)));

    std::string name = DeviceNameManager::GetInstance().GetUserDefinedDeviceName();
    EXPECT_TRUE(name.empty());
}

/**
 * @tc.name: ModifyUserDefinedName_001
 * @tc.desc: ModifyUserDefinedName returns ERR_DM_NAME_EMPTY when deviceName is empty.
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, ModifyUserDefinedName_001, testing::ext::TestSize.Level1)
{
    std::string deviceName = "";
    auto ret = DeviceNameManager::GetInstance().ModifyUserDefinedName(deviceName);
    EXPECT_EQ(ret, ERR_DM_NAME_EMPTY);
}

/**
 * @tc.name: RestoreLocalDeviceName_001
 * @tc.desc: RestoreLocalDeviceName calls GetMainDisplayActiveUserId and returns DM_OK.
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, RestoreLocalDeviceName_001, testing::ext::TestSize.Level1)
{
    ASSERT_TRUE(multipleUserConnector_ != nullptr);
    ASSERT_TRUE(helper_ != nullptr);

    EXPECT_CALL(*multipleUserConnector_, GetMainDisplayActiveUserId())
        .WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
    EXPECT_CALL(*helper_, Update(_, _, _)).WillRepeatedly(Return(1));
    EXPECT_CALL(*helper_, Insert(_, _)).WillRepeatedly(Return(1));
    EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));

    int32_t ret = DeviceNameManager::GetInstance().RestoreLocalDeviceName();
    EXPECT_EQ(ret, DM_OK);
}

/**
 * @tc.name: InitDeviceNameWhenNameChange_002
 * @tc.desc: InitDeviceNameWhenNameChange calls InitDeviceName which resolves userId via GetMainDisplayActiveUserId.
 * @tc.type: FUNC
 */
HWTEST_F(DeviceNameManagerTest, InitDeviceNameWhenNameChange_002, testing::ext::TestSize.Level1)
{
    ASSERT_TRUE(client_ != nullptr);
    ASSERT_TRUE(multipleUserConnector_ != nullptr);
    ASSERT_TRUE(helper_ != nullptr);

    bool srcDataShareReady = DeviceNameManager::GetInstance().isDataShareReady_;
    bool srcAccountSysReady = DeviceNameManager::GetInstance().isAccountSysReady_;
    sptr<IRemoteObject> srcRemoteObj = DeviceNameManager::GetInstance().remoteObj_;
    DeviceNameManager::GetInstance().isDataShareReady_ = true;
    DeviceNameManager::GetInstance().isAccountSysReady_ = true;
    DeviceNameManager::GetInstance().remoteObj_ = nullptr;

    std::string suffixName = "testDevice";
    auto bundleMgr = sptr<BundleMgrMock>(new (std::nothrow) BundleMgrMock());
    auto systemAbilityManager = sptr<SystemAbilityManagerMock>(new (std::nothrow) SystemAbilityManagerMock());
    EXPECT_CALL(*systemAbilityManager, GetSystemAbility(_)).WillRepeatedly(Return(bundleMgr));
    EXPECT_CALL(*client_, GetSystemAbilityManager()).WillRepeatedly(Return(systemAbilityManager));
    EXPECT_CALL(*multipleUserConnector_, GetMainDisplayActiveUserId())
        .WillRepeatedly(Return(DEFAULT_VALUABLE_USER_ID));
    auto resultSet = std::make_shared<DataShareResultSetMock>(nullptr);
    EXPECT_CALL(*helper_, Query(_, _, _, _)).WillRepeatedly(Return(resultSet));
    EXPECT_CALL(*helper_, Release()).WillRepeatedly(Return(true));
    EXPECT_CALL(*resultSet, GetRowCount(_)).WillRepeatedly(DoAll(SetArgReferee<0>(1), Return(DataShare::E_OK)));
    EXPECT_CALL(*resultSet, GetString(_, _)).WillRepeatedly(
        DoAll(SetArgReferee<1>(suffixName), Return(DataShare::E_OK)));

    int32_t passedUserId = 200;
    int32_t ret = DeviceNameManager::GetInstance().InitDeviceNameWhenNameChange(passedUserId);
    EXPECT_EQ(ret, DM_OK);

    DeviceNameManager::GetInstance().isDataShareReady_ = srcDataShareReady;
    DeviceNameManager::GetInstance().isAccountSysReady_ = srcAccountSysReady;
    DeviceNameManager::GetInstance().remoteObj_ = srcRemoteObj;
}
} // DistributedHardware
} // OHOS