1 /*
2 * Copyright (c) 2022 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 <gtest/gtest.h>
17 #include <gmock/gmock.h>
18
19 #include "common/log.h"
20 #include "device_manager.h"
21 #include "hdi_device_v2_0.h"
22 #include "test/unittest/common/v2_0/mock_idevice.h"
23
24 using namespace testing;
25 using namespace testing::ext;
26 using namespace OHOS::NeuralNetworkRuntime;
27 namespace OHOS {
28 namespace NeuralNetworkRuntime {
29 namespace UnitTest {
30 class DeviceManagerTest : public testing::Test {
31 protected:
32 void MockInit(OHOS::sptr<V2_0::MockIDevice> device, const std::vector<int32_t>& typeVect,
33 const std::string& deviceName, const std::string& vendorName);
34 };
35
MockInit(OHOS::sptr<V2_0::MockIDevice> device,const std::vector<int32_t> & typeVect,const std::string & deviceName,const std::string & vendorName)36 void DeviceManagerTest::MockInit(OHOS::sptr<V2_0::MockIDevice> device, const std::vector<int32_t>& typeVect,
37 const std::string& deviceName, const std::string& vendorName)
38 {
39 const size_t typeSize = 4;
40 int index = 0;
41 EXPECT_EQ(typeSize, typeVect.size());
42 EXPECT_CALL(*device, GetDeviceName(::testing::_))
43 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(deviceName),
44 ::testing::Return(typeVect[index++])));
45
46 EXPECT_CALL(*device, GetVendorName(::testing::_))
47 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(vendorName),
48 ::testing::Return(typeVect[index++])));
49
50 V2_0::DeviceStatus deviceStatus = V2_0::DeviceStatus::AVAILABLE;
51 EXPECT_CALL(*device, GetDeviceStatus(::testing::_))
52 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(deviceStatus),
53 ::testing::Return(typeVect[index++])));
54
55 uint32_t majorVer = 1;
56 uint32_t minorVer = 0;
57 EXPECT_CALL(*device, GetVersion(::testing::_, ::testing::_))
58 .WillRepeatedly(::testing::DoAll(::testing::SetArgReferee<0>(majorVer), ::testing::SetArgReferee<1>(minorVer),
59 ::testing::Return(typeVect[index++])));
60 }
61
62 /**
63 * @tc.name: devicemanager_getalldeviceid_001
64 * @tc.desc: Verify the GetAllDeviceId function return deviceid list is not null.
65 * @tc.type: FUNC
66 */
67 HWTEST_F(DeviceManagerTest, devicemanager_getalldeviceid_001, TestSize.Level0)
68 {
69 auto &deviceManager = DeviceManager::GetInstance();
70 std::vector<size_t> idVect = deviceManager.GetAllDeviceId();
71 EXPECT_NE((size_t)0, idVect.size());
72
73 const std::string expectDeviceName = "MockDevice";
74 std::string deviceName = "";
75 std::shared_ptr<Device> retDevice = deviceManager.GetDevice(idVect[0]);
76 retDevice->GetDeviceName(deviceName);
77 EXPECT_EQ(deviceName, expectDeviceName);
78 }
79
80 /**
81 * @tc.name: devicemanager_getdevice_001
82 * @tc.desc: Verify the GetDevice function return nullptr in case of deviceId invalid.
83 * @tc.type: FUNC
84 */
85 HWTEST_F(DeviceManagerTest, devicemanager_getdevice_001, TestSize.Level0)
86 {
87 auto &deviceManager = DeviceManager::GetInstance();
88 const size_t deviceId = 1;
89 std::shared_ptr<Device> result = deviceManager.GetDevice(deviceId);
90 EXPECT_EQ(nullptr, result);
91 }
92
93 /**
94 * @tc.name: devicemanager_getdevice_002
95 * @tc.desc: Verify the GetDevice function validate device name return specified device name.
96 * @tc.type: FUNC
97 */
98 HWTEST_F(DeviceManagerTest, devicemanager_getdevice_002, TestSize.Level0)
99 {
100 auto &deviceManager = DeviceManager::GetInstance();
101 std::vector<size_t> idVect = deviceManager.GetAllDeviceId();
102 EXPECT_EQ((size_t)1, idVect.size());
103 size_t deviceId = idVect[0];
104 std::shared_ptr<Device> result = deviceManager.GetDevice(deviceId);
105 EXPECT_NE(nullptr, result);
106
107 const std::string expectDeviceNameA = "MockDevice";
108 std::string deviceName = "";
109 result->GetDeviceName(deviceName);
110 EXPECT_EQ(deviceName, expectDeviceNameA);
111 }
112
113 /**
114 * @tc.name: devicemanager_registerdevice_001
115 * @tc.desc: Verify the RegisterDevice function register repeatly.
116 * @tc.type: FUNC
117 */
118 HWTEST_F(DeviceManagerTest, devicemanager_registerdevice_001, TestSize.Level0)
119 {
120 std::vector<int32_t> typeVect = {HDF_SUCCESS, HDF_SUCCESS, HDF_SUCCESS, HDF_SUCCESS};
121 OHOS::sptr<V2_0::MockIDevice> device = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
122 EXPECT_NE(device.GetRefPtr(), nullptr);
123
124 std::string deviceName = "MockDevice";
125 std::string vendorName = "MockVendor";
126 MockInit(device, typeVect, deviceName, vendorName);
127
128 std::function<std::shared_ptr<HDIDeviceV2_0>()> creator =
__anon0c32916e0102()129 [&device]()->std::shared_ptr<HDIDeviceV2_0> {return std::make_shared<HDIDeviceV2_0>(device);};
130 auto& deviceManager = DeviceManager::GetInstance();
131 OH_NN_ReturnCode result = deviceManager.RegisterDevice(creator);
132 EXPECT_EQ(OH_NN_SUCCESS, result);
133 }
134
135 /**
136 * @tc.name: devicemanager_registerdevice_002
137 * @tc.desc: Verify the RegisterDevice function return invalid parameter.
138 * @tc.type: FUNC
139 */
140 HWTEST_F(DeviceManagerTest, devicemanager_registerdevice_002, TestSize.Level0)
141 {
142 std::function<std::shared_ptr<HDIDeviceV2_0>()> creator =
__anon0c32916e0202()143 []()->std::shared_ptr<HDIDeviceV2_0> {return nullptr;};
144 auto& deviceManager = DeviceManager::GetInstance();
145 OH_NN_ReturnCode result = deviceManager.RegisterDevice(creator);
146 EXPECT_EQ(OH_NN_INVALID_PARAMETER, result);
147 }
148
149 /**
150 * @tc.name: devicemanager_registerdevice_003
151 * @tc.desc: Verify the RegisterDevice function return unavailable device in case of device name invalid param.
152 * @tc.type: FUNC
153 */
154 HWTEST_F(DeviceManagerTest, devicemanager_registerdevice_003, TestSize.Level0)
155 {
156 std::vector<int32_t> typeVect = {HDF_FAILURE, HDF_SUCCESS, HDF_SUCCESS, HDF_SUCCESS};
157 OHOS::sptr<V2_0::MockIDevice> device = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
158 EXPECT_NE(device.GetRefPtr(), nullptr);
159
160 std::string deviceName = "MockDevice";
161 std::string vendorName = "MockVendor";
162 MockInit(device, typeVect, deviceName, vendorName);
163
164 std::function<std::shared_ptr<HDIDeviceV2_0>()> creator =
__anon0c32916e0302()165 [&device]()->std::shared_ptr<HDIDeviceV2_0> {return std::make_shared<HDIDeviceV2_0>(device);};
166 auto& deviceManager = DeviceManager::GetInstance();
167 OH_NN_ReturnCode result = deviceManager.RegisterDevice(creator);
168 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
169 }
170
171 /**
172 * @tc.name: devicemanager_registerdevice_004
173 * @tc.desc: Verify the RegisterDevice function return unavailable device in case of vendor name failure.
174 * @tc.type: FUNC
175 */
176 HWTEST_F(DeviceManagerTest, devicemanager_registerdevice_004, TestSize.Level0)
177 {
178 std::vector<int32_t> typeVect = {HDF_SUCCESS, HDF_FAILURE, HDF_SUCCESS, HDF_SUCCESS};
179 OHOS::sptr<V2_0::MockIDevice> device = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
180 EXPECT_NE(device.GetRefPtr(), nullptr);
181
182 std::string deviceName = "MockDevice";
183 std::string vendorName = "MockVendor";
184 MockInit(device, typeVect, deviceName, vendorName);
185
186 std::function<std::shared_ptr<HDIDeviceV2_0>()> creator =
__anon0c32916e0402()187 [&device]()->std::shared_ptr<HDIDeviceV2_0> {return std::make_shared<HDIDeviceV2_0>(device);};
188 auto& deviceManager = DeviceManager::GetInstance();
189 OH_NN_ReturnCode result = deviceManager.RegisterDevice(creator);
190 EXPECT_EQ(OH_NN_UNAVAILABLE_DEVICE, result);
191 }
192
193 /**
194 * @tc.name: devicemanager_registerdevice_005
195 * @tc.desc: Verify the RegisterDevice function return success.
196 * @tc.type: FUNC
197 */
198 HWTEST_F(DeviceManagerTest, devicemanager_registerdevice_005, TestSize.Level0)
199 {
200 std::vector<int32_t> typeVect = {HDF_SUCCESS, HDF_SUCCESS, HDF_SUCCESS, HDF_SUCCESS};
201 OHOS::sptr<V2_0::MockIDevice> device = OHOS::sptr<V2_0::MockIDevice>(new (std::nothrow) V2_0::MockIDevice());
202 EXPECT_NE(device.GetRefPtr(), nullptr);
203
204 std::string deviceName = "MockDeviceA";
205 std::string vendorName = "MockVendorA";
206 MockInit(device, typeVect, deviceName, vendorName);
207
208 std::function<std::shared_ptr<HDIDeviceV2_0>()> creator =
__anon0c32916e0502()209 [&device]()->std::shared_ptr<HDIDeviceV2_0> {return std::make_shared<HDIDeviceV2_0>(device);};
210 auto& deviceManager = DeviceManager::GetInstance();
211 OH_NN_ReturnCode result = deviceManager.RegisterDevice(creator);
212 EXPECT_EQ(OH_NN_SUCCESS, result);
213
214 std::vector<size_t> idVect = deviceManager.GetAllDeviceId();
215 EXPECT_NE((size_t)0, idVect.size());
216
217 const size_t expectDeviceId {std::hash<std::string> {} ("MockDeviceA_MockVendorA_v1_0")};
218 EXPECT_EQ(expectDeviceId, idVect[0]);
219
220 const std::string expectDeviceName = "MockDeviceA_MockVendorA_v1_0";
221 const std::string retDeviceName = deviceManager.GetDeviceName(idVect[0]);
222 EXPECT_EQ(retDeviceName, expectDeviceName);
223 }
224
225 /**
226 * @tc.name: devicemanager_getdevicename_001
227 * @tc.desc: Verify the GetDevice function return empty string in case of deviceid invalid.
228 * @tc.type: FUNC
229 */
230 HWTEST_F(DeviceManagerTest, devicemanager_getdevicename_001, TestSize.Level0)
231 {
232 auto &deviceManager = DeviceManager::GetInstance();
233 const size_t deviceId = 1;
234 std::string result = deviceManager.GetDeviceName(deviceId);
235 EXPECT_EQ("", result);
236 }
237 } // namespace UnitTest
238 } // namespace NeuralNetworkRuntime
239 } // namespace OHOS
240