• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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_manager_service_impl_first.h"
17 #include "softbus_error_code.h"
18 #include "common_event_support.h"
19 #include "deviceprofile_connector.h"
20 #include "distributed_device_profile_client.h"
21 
22 using namespace testing;
23 using namespace testing::ext;
24 using namespace OHOS::DistributedDeviceProfile;
25 namespace OHOS {
26 namespace DistributedHardware {
SetUp()27 void DeviceManagerServiceImplFirstTest::SetUp()
28 {
29     if (deviceManagerServiceImpl_ == nullptr) {
30         deviceManagerServiceImpl_ = std::make_shared<DeviceManagerServiceImpl>();
31     }
32     deviceManagerServiceImpl_->Initialize(listener_);
33 }
34 
TearDown()35 void DeviceManagerServiceImplFirstTest::TearDown()
36 {
37 }
38 
SetUpTestCase()39 void DeviceManagerServiceImplFirstTest::SetUpTestCase()
40 {
41     DmDeviceProfileConnector::dmDeviceProfileConnector = deviceProfileConnectorMock_;
42     DmMultipleUserConnector::dmMultipleUserConnector = multipleUserConnectorMock_;
43 }
44 
TearDownTestCase()45 void DeviceManagerServiceImplFirstTest::TearDownTestCase()
46 {
47     DmDeviceProfileConnector::dmDeviceProfileConnector = nullptr;
48     deviceProfileConnectorMock_ = nullptr;
49     DmMultipleUserConnector::dmMultipleUserConnector = nullptr;
50     multipleUserConnectorMock_ = nullptr;
51 }
52 
53 namespace {
54 HWTEST_F(DeviceManagerServiceImplFirstTest, GetDeviceIdAndUserId_101, testing::ext::TestSize.Level0)
55 {
56     int32_t userId = 1;
57     std::string accountId = "accountId";
58     auto ret = deviceManagerServiceImpl_->GetDeviceIdAndUserId(userId, accountId);
59     EXPECT_TRUE(ret.empty());
60 
61     ret = deviceManagerServiceImpl_->GetDeviceIdAndUserId(userId);
62     EXPECT_TRUE(ret.empty());
63 
64     std::string localUdid = "deviceId";
65     int32_t localUserId = 123456;
66     std::string peerUdid = "remoteUdid";
67     int32_t peerUserId = 1;
68     EXPECT_CALL(*deviceProfileConnectorMock_, DeleteAclForAccountLogOut(_, _, _, _)).WillOnce(Return(true));
69     if (deviceManagerServiceImpl_->softbusConnector_ == nullptr) {
70         deviceManagerServiceImpl_->Initialize(listener_);
71     }
72 
73     if (deviceManagerServiceImpl_->deviceStateMgr_ == nullptr) {
74         deviceManagerServiceImpl_->Initialize(listener_);
75     }
76     deviceManagerServiceImpl_->HandleIdentAccountLogout(localUdid, localUserId, peerUdid, peerUserId);
77 
78     std::vector<uint32_t> foregroundUserIds;
79     std::vector<uint32_t> backgroundUserIds;
80     std::string remoteUdid = "deviceId";
81     EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(_)).WillOnce(Return(ERR_DM_FAILED));
82     deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, false);
83 
84     std::vector<int32_t> localUserIds;
85     localUserIds.push_back(101);
86     localUserIds.push_back(102);
87     EXPECT_CALL(*multipleUserConnectorMock_, GetForegroundUserIds(_))
88         .WillOnce(DoAll(SetArgReferee<0>(localUserIds), Return(DM_OK)));
89     deviceManagerServiceImpl_->HandleSyncUserIdEvent(foregroundUserIds, backgroundUserIds, remoteUdid, false);
90 }
91 } // namespace
92 } // namespace DistributedHardware
93 } // namespace OHOS
94