• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 #include <iostream>
16 
17 #include "dm_log.h"
18 #include "dm_constants.h"
19 #include "dm_adapter_manager.h"
20 #include "ipc_notify_device_state_req.h"
21 #include "ipc_notify_auth_result_req.h"
22 #include "ipc_notify_verify_auth_result_req.h"
23 #include "dm_device_state_manager.h"
24 #include "ipc_notify_device_found_req.h"
25 #include "ipc_notify_discover_result_req.h"
26 #include "hichain_connector.h"
27 #include "UTTest_dm_device_state_manager.h"
28 
29 namespace OHOS {
30 namespace DistributedHardware {
SetUp()31 void DmDeviceStateManagerTest::SetUp()
32 {
33 }
34 
TearDown()35 void DmDeviceStateManagerTest::TearDown()
36 {
37 }
38 
SetUpTestCase()39 void DmDeviceStateManagerTest::SetUpTestCase()
40 {
41 }
42 
TearDownTestCase()43 void DmDeviceStateManagerTest::TearDownTestCase()
44 {
45 }
46 namespace {
47 std::shared_ptr<HiChainConnector> hiChainConnector_ = std::make_shared<HiChainConnector>();
48 std::shared_ptr<SoftbusConnector> softbusConnector = std::make_shared<SoftbusConnector>();
49 std::shared_ptr<DeviceManagerServiceListener> listener_ = std::make_shared<DeviceManagerServiceListener>();
50 std::shared_ptr<DmDeviceStateManager> dmDeviceStateManager =
51     std::make_shared<DmDeviceStateManager>(softbusConnector, listener_, hiChainConnector_);
52 
53 /**
54  * @tc.name: DmDeviceStateManager_001
55  * @tc.desc: set DmDeviceStateManager to tne new pointer,and it's not nullptr
56  * @tc.type: FUNC
57  * @tc.require: AR000GHSJK
58  */
59 HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_001, testing::ext::TestSize.Level0)
60 {
61     std::shared_ptr<DmDeviceStateManager> p = std::make_shared<DmDeviceStateManager>(softbusConnector, listener_,
62         hiChainConnector_);
63     ASSERT_NE(p, nullptr);
64 }
65 
66 /**
67  * @tc.name: DmDeviceStateManager_002
68  * @tc.desc: set DmDeviceStateManager to tne new pointer,it's not nullptr and delete it
69  * @tc.type: FUNC
70  * @tc.require: AR000GHSJK
71  */
72 HWTEST_F(DmDeviceStateManagerTest, DmDeviceStateManager_002, testing::ext::TestSize.Level0)
73 {
74     std::shared_ptr<DmDeviceStateManager> p = std::make_shared<DmDeviceStateManager>(softbusConnector, listener_,
75         hiChainConnector_);
76     p.reset();
77     EXPECT_EQ(p, nullptr);
78 }
79 
80 /**
81  * @tc.name: OnDeviceOnline_001
82  * @tc.desc: set info.deviceId to some para, and return it
83  * @tc.type: FUNC
84  * @tc.require: AR000GHSJK
85  */
86 HWTEST_F(DmDeviceStateManagerTest, OnDeviceOnline_001, testing::ext::TestSize.Level0)
87 {
88     std::string pkgName = "123";
89     DmDeviceInfo info;
90     strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
91     dmDeviceStateManager->OnDeviceOnline(pkgName, info);
92     std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
93         std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
94     DmDeviceInfo ret = pReq->GetDeviceInfo();
95     int result = strcmp(info.deviceId, ret.deviceId);
96     EXPECT_EQ(result, 0);
97 }
98 
99 /**
100  * @tc.name: OnDeviceOffline_001
101  * @tc.desc: set info.deviceId to some para, and return it
102  * @tc.type: FUNC
103  * @tc.require: AR000GHSJK
104  */
105 HWTEST_F(DmDeviceStateManagerTest, OnDeviceOffline_001, testing::ext::TestSize.Level0)
106 {
107     std::string pkgName;
108     DmDeviceInfo info;
109     strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
110     dmDeviceStateManager->OnDeviceOffline(pkgName, info);
111     std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
112         std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
113     DmDeviceInfo ret = pReq->GetDeviceInfo();
114     int result = strcmp(info.deviceId, ret.deviceId);
115     EXPECT_EQ(result, 0);
116 }
117 
118 /**
119  * @tc.name: OnDeviceChanged_001
120  * @tc.desc: set info.deviceId to some para, and return it
121  * @tc.type: FUNC
122  * @tc.require: AR000GHSJK
123  */
124 HWTEST_F(DmDeviceStateManagerTest, OnDeviceChanged_001, testing::ext::TestSize.Level0)
125 {
126     std::string pkgName;
127     DmDeviceInfo info;
128     strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
129     dmDeviceStateManager->OnDeviceChanged(pkgName, info);
130     std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
131         std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
132     DmDeviceInfo ret = pReq->GetDeviceInfo();
133     int result = strcmp(info.deviceId, ret.deviceId);
134     EXPECT_EQ(result, 0);
135 }
136 
137 /**
138  * @tc.name: OnDeviceReady_001
139  * @tc.desc: set info.deviceId to some para, and return it
140  * @tc.type: FUNC
141  * @tc.require: AR000GHSJK
142  */
143 HWTEST_F(DmDeviceStateManagerTest, OnProfileReady_001, testing::ext::TestSize.Level0)
144 {
145     std::string pkgName;
146     std::string deviceId;
147     DmDeviceInfo info;
148     strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
149     dmDeviceStateManager->OnProfileReady(pkgName, deviceId);
150     std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
151         std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
152     DmDeviceInfo ret = pReq->GetDeviceInfo();
153     int result = strcmp(info.deviceId, ret.deviceId);
154     EXPECT_EQ(result, 0);
155 }
156 
157 /**
158  * @tc.name: OnDeviceReady_001
159  * @tc.desc: set info.deviceId to 123,and call OnDeviceReady ,change info.deviceId to 4
160  * @tc.type: FUNC
161  * @tc.require: AR000GHSJK
162  */
163 HWTEST_F(DmDeviceStateManagerTest, OnDeviceReady_001, testing::ext::TestSize.Level0)
164 {
165     std::string pkgName;
166     DmDeviceInfo info;
167     strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
168     dmDeviceStateManager->OnDeviceReady(pkgName, info);
169     std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
170         std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
171     DmDeviceInfo ret = pReq->GetDeviceInfo();
172     int result = strcmp(info.deviceId, ret.deviceId);
173     EXPECT_EQ(result, 0);
174 }
175 
176 /**
177  * @tc.name: OnDeviceChanged_001
178  * @tc.desc: set info.deviceId to 123,and call OnDeviceChanged ,change info.deviceId to 4
179  * @tc.type: FUNC
180  * @tc.require: AR000GHSJK
181  */
182 HWTEST_F(DmDeviceStateManagerTest, OnDeviceChanged_002, testing::ext::TestSize.Level0)
183 {
184     std::string pkgName;
185     DmDeviceInfo info;
186     strcpy_s(info.deviceId, DM_MAX_DEVICE_ID_LEN, "123");
187     dmDeviceStateManager->OnDeviceChanged(pkgName, info);
188     std::shared_ptr<IpcNotifyDeviceStateReq> pReq =
189         std::static_pointer_cast<IpcNotifyDeviceStateReq>(listener_->ipcServerListener_.req_);
190     DmDeviceInfo ret = pReq->GetDeviceInfo();
191     int result = strcmp(info.deviceId, ret.deviceId);
192     EXPECT_EQ(result, 0);
193 }
194 
195 /**
196  * @tc.name: RegisterSoftbusStateCallback_001
197  * @tc.desc: call  RegisterSoftbusStateCallback and return DM_OK
198  * @tc.type: FUNC
199  * @tc.require: AR000GHSJK
200  */
201 HWTEST_F(DmDeviceStateManagerTest, RegisterSoftbusStateCallback_001, testing::ext::TestSize.Level0)
202 {
203     int ret = dmDeviceStateManager->RegisterSoftbusStateCallback();
204     EXPECT_EQ(ret, DM_OK);
205 }
206 } // namespace
207 } // namespace DistributedHardware
208 } // namespace OHOS
209