1 /*
2  * Copyright (c) 2024 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 <vector>
17 #include <memory>
18 
19 #include <unistd.h>
20 
21 #include "device_manager.h"
22 #include <gtest/gtest.h>
23 #include "enumerator.h"
24 
25 #include "devicestatus_define.h"
26 #include "devicestatus_errors.h"
27 
28 #undef LOG_TAG
29 #define LOG_TAG "EnumeratorTest"
30 
31 namespace OHOS {
32 namespace Msdp {
33 namespace DeviceStatus {
34 using namespace testing::ext;
35 namespace {
36 constexpr int32_t TIME_WAIT_FOR_OP_MS { 20 };
37 const std::string TEST_DEV_NODE {"TestDeviceNode"};
38 } // namespace
39 
40 class EnumeratorTest : public testing::Test {
41 public:
42     void SetUp();
43     void TearDown();
44     static void SetUpTestCase();
45     static void TearDownTestCase(void);
46 };
SetUpTestCase()47 void EnumeratorTest::SetUpTestCase() {}
48 
TearDownTestCase()49 void EnumeratorTest::TearDownTestCase() {}
50 
SetUp()51 void EnumeratorTest::SetUp() {}
52 
TearDown()53 void EnumeratorTest::TearDown()
54 {
55     std::this_thread::sleep_for(std::chrono::milliseconds(TIME_WAIT_FOR_OP_MS));
56 }
57 
58 class TestDeviceMgr : public IDeviceMgr {
59 public:
60     TestDeviceMgr() = default;
61     ~TestDeviceMgr() = default;
AddDevice(const std::string & devNode)62     void AddDevice(const std::string &devNode) override
63     {
64         devMgr_.DeviceManager::AddDevice(devNode);
65     }
RemoveDevice(const std::string & devNode)66     void RemoveDevice(const std::string &devNode) override
67     {
68         devMgr_.DeviceManager::RemoveDevice(devNode);
69     }
70 private:
71     DeviceManager devMgr_;
72 };
73 
74 /**
75  * @tc.name: EnumeratorTest01
76  * @tc.desc: test SetDeviceMgr and AddDevice
77  * @tc.type: FUNC
78  * @tc.require:
79  */
80 HWTEST_F(EnumeratorTest, EnumeratorTest01, TestSize.Level1)
81 {
82     Enumerator enumerator;
83     std::shared_ptr<TestDeviceMgr> testDevMgr = std::make_shared<TestDeviceMgr>();
84     IDeviceMgr *deviceMgr = testDevMgr.get();
85     ASSERT_NO_FATAL_FAILURE(enumerator.SetDeviceMgr(deviceMgr));
86     ASSERT_NO_FATAL_FAILURE(enumerator.AddDevice(TEST_DEV_NODE));
87 }
88 
89 /**
90  * @tc.name: EnumeratorTest02
91  * @tc.desc: test ScanDevices and ScanAndAddDevices
92  * @tc.type: FUNC
93  * @tc.require:
94  */
95 HWTEST_F(EnumeratorTest, EnumeratorTest02, TestSize.Level1)
96 {
97     Enumerator enumerator;
98     ASSERT_NO_FATAL_FAILURE(enumerator.ScanDevices());
99     ASSERT_NO_FATAL_FAILURE(enumerator.ScanAndAddDevices());
100 }
101 } // namespace DeviceStatus
102 } // namespace Msdp
103 } // namespace OHOS