• 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 
16 
17 #include <gtest/gtest.h>
18 
19 #include <devmgr_hdi.h>
20 #include <osal_time.h>
21 #include <servmgr_hdi.h>
22 
23 #define HDF_LOG_TAG   driver_manager
24 
25 namespace OHOS {
26 using namespace testing::ext;
27 
28 static constexpr const char *TEST_SERVICE_NAME = "sample_driver_service";
29 
30 class DevMgrTest : public testing::Test {
31 public:
32     static void SetUpTestCase();
33     static void TearDownTestCase();
34     void SetUp();
35     void TearDown();
36     static const uint32_t waitTime = 30;
37     static const uint32_t timeout = 200;
38     static struct HDIServiceManager *servmgr;
39     static struct HDIDeviceManager *devmgr;
40 };
41 
42 struct HDIServiceManager *DevMgrTest::servmgr = nullptr;
43 struct HDIDeviceManager *DevMgrTest::devmgr = nullptr;
44 
SetUpTestCase()45 void DevMgrTest::SetUpTestCase()
46 {
47     servmgr = HDIServiceManagerGet();
48     devmgr = HDIDeviceManagerGet();
49 }
50 
TearDownTestCase()51 void DevMgrTest::TearDownTestCase()
52 {
53 }
54 
SetUp()55 void DevMgrTest::SetUp()
56 {
57 }
58 
TearDown()59 void DevMgrTest::TearDown()
60 {
61 }
62 
63 /*
64 * @tc.name: DriverLoaderTest
65 * @tc.desc: driver load test
66 * @tc.type: FUNC
67 * @tc.require: AR000DT1TK
68 */
69 HWTEST_F(DevMgrTest, DriverLoaderTest, TestSize.Level1)
70 {
71     ASSERT_TRUE(servmgr != nullptr);
72     ASSERT_TRUE(devmgr != nullptr);
73 
74     int ret = devmgr->LoadDevice(devmgr, TEST_SERVICE_NAME);
75     ASSERT_EQ(ret, HDF_SUCCESS);
76 
77     struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
78     uint32_t cnt = 0;
79     while (sampleService == nullptr && cnt < timeout) {
80         OsalMSleep(waitTime);
81         sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
82         cnt++;
83     }
84 
85     ASSERT_TRUE(sampleService != nullptr);
86 }
87 /*
88 * @tc.name: DriverUnLoaderTest
89 * @tc.desc: driver unload test
90 * @tc.type: FUNC
91 * @tc.require: AR000DT1TK
92 */
93 HWTEST_F(DevMgrTest, DriverUnLoaderTest, TestSize.Level1)
94 {
95     ASSERT_TRUE(servmgr != nullptr);
96     ASSERT_TRUE(devmgr != nullptr);
97 
98     int ret = devmgr->UnloadDevice(devmgr, TEST_SERVICE_NAME);
99     ASSERT_EQ(ret, HDF_SUCCESS);
100 
101     uint32_t cnt = 0;
102     struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
103     while (sampleService != nullptr && cnt < timeout) {
104         OsalMSleep(waitTime);
105         sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
106         cnt++;
107     }
108 
109     ASSERT_TRUE(sampleService == nullptr);
110 }
111 
112 HWTEST_F(DevMgrTest, DriverTest, TestSize.Level1)
113 {
114     ASSERT_TRUE(servmgr != nullptr);
115     ASSERT_TRUE(devmgr != nullptr);
116     int ret;
117     constexpr int loop = 100;
118 
119     for (int i = 0; i < loop; i++) {
120         ret = devmgr->LoadDevice(devmgr, TEST_SERVICE_NAME);
121         ASSERT_EQ(ret, HDF_SUCCESS);
122         uint32_t cnt = 0;
123         struct HdfRemoteService *sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
124         while (sampleService == nullptr && cnt < timeout) {
125             OsalMSleep(waitTime);
126             sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
127             cnt++;
128         }
129         ASSERT_TRUE(sampleService != nullptr);
130 
131         ret = devmgr->UnloadDevice(devmgr, TEST_SERVICE_NAME);
132         ASSERT_EQ(ret, HDF_SUCCESS);
133         cnt = 0;
134         sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
135         while (sampleService != nullptr && cnt < timeout) {
136             OsalMSleep(waitTime);
137             sampleService = servmgr->GetService(servmgr, TEST_SERVICE_NAME);
138             cnt++;
139         }
140         ASSERT_TRUE(sampleService == nullptr);
141     }
142 }
143 } // namespace OHOS
144