• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "distributed_hardware_service_test.h"
17 
18 #include <chrono>
19 #include <thread>
20 #include <vector>
21 
22 #include "constants.h"
23 #include "dh_context.h"
24 #include "distributed_hardware_errno.h"
25 #include "distributed_hardware_log.h"
26 #define protected public
27 #define private public
28 #include "distributed_hardware_service.h"
29 #include "distributed_hardware_manager.h"
30 #include "task_board.h"
31 #undef private
32 #undef protected
33 #include "mock_publisher_listener.h"
34 
35 using namespace testing::ext;
36 
37 namespace OHOS {
38 namespace DistributedHardware {
39 namespace {
40     const int32_t ASID = 4801;
41     const DHTopic TOPIC = DHTopic::TOPIC_START_DSCREEN;
42     sptr<IPublisherListener> g_listener = sptr<IPublisherListener>(new MockPublisherListener());
43 
44     const std::u16string ARGS_H = u"-h";
45     const std::u16string ARGS_L = u"-l";
46     const std::u16string ARGS_E = u"-e";
47     const std::u16string ARGS_T = u"-t";
48     const std::u16string ARGS_C = u"-c";
49 }
SetUpTestCase(void)50 void DistributedHardwareServiceTest::SetUpTestCase(void) {}
51 
TearDownTestCase(void)52 void DistributedHardwareServiceTest::TearDownTestCase(void) {}
53 
SetUp()54 void DistributedHardwareServiceTest::SetUp() {}
55 
TearDown()56 void DistributedHardwareServiceTest::TearDown() {}
57 
58 /**
59  * @tc.name: register_publisher_listener_001
60  * @tc.desc: Verify the RegisterPublisherListener function
61  * @tc.type: FUNC
62  * @tc.require: AR000GHSJM
63  */
64 HWTEST_F(DistributedHardwareServiceTest, register_publisher_listener_001, TestSize.Level0)
65 {
66     DistributedHardwareService service(ASID, true);
67     auto ret = service.RegisterPublisherListener(TOPIC, g_listener);
68 
69     EXPECT_EQ(ret, DH_FWK_SUCCESS);
70 }
71 
72 /**
73  * @tc.name: register_publisher_listener_002
74  * @tc.desc: Verify the RegisterPublisherListener function
75  * @tc.type: FUNC
76  * @tc.require: AR000GHSJM
77  */
78 HWTEST_F(DistributedHardwareServiceTest, register_publisher_listener_002, TestSize.Level0)
79 {
80     DistributedHardwareService service(ASID, true);
81     sptr<IPublisherListener> listener = nullptr;
82     auto ret = service.RegisterPublisherListener(TOPIC, listener);
83     EXPECT_EQ(ret, DH_FWK_SUCCESS);
84 }
85 
86 /**
87  * @tc.name: unregister_publisher_listener_001
88  * @tc.desc: Verify the UnregisterPublisherListener function
89  * @tc.type: FUNC
90  * @tc.require: AR000GHSJM
91  */
92 HWTEST_F(DistributedHardwareServiceTest, unregister_publisher_listener_001, TestSize.Level0)
93 {
94     DistributedHardwareService service(ASID, true);
95     service.RegisterPublisherListener(TOPIC, g_listener);
96     auto ret = service.UnregisterPublisherListener(TOPIC, g_listener);
97 
98     EXPECT_EQ(ret, DH_FWK_SUCCESS);
99 }
100 
101 /**
102  * @tc.name: publish_message_001
103  * @tc.desc: Verify the PublishMessage function
104  * @tc.type: FUNC
105  * @tc.require: AR000GHSJM
106  */
107 HWTEST_F(DistributedHardwareServiceTest, publish_message_001, TestSize.Level0)
108 {
109     DistributedHardwareService service(ASID, true);
110     std::string msg;
111     service.RegisterPublisherListener(TOPIC, g_listener);
112     auto ret = service.PublishMessage(TOPIC, msg);
113 
114     EXPECT_EQ(ret, DH_FWK_SUCCESS);
115 }
116 
117 /**
118  * @tc.name: publish_message_002
119  * @tc.desc: Verify the PublishMessage function
120  * @tc.type: FUNC
121  * @tc.require: AR000GHSJM
122  */
123 HWTEST_F(DistributedHardwareServiceTest, publish_message_002, TestSize.Level0)
124 {
125     DistributedHardwareService service(ASID, true);
126     std::string msg = "msg";
127     service.RegisterPublisherListener(TOPIC, g_listener);
128     auto ret = service.PublishMessage(TOPIC, msg);
129 
130     EXPECT_EQ(ret, DH_FWK_SUCCESS);
131 }
132 
133 /**
134  * @tc.name: onStop_test_002
135  * @tc.desc: Verify the OnStop function
136  * @tc.type: FUNC
137  * @tc.require: AR000GHSJM
138  */
139 HWTEST_F(DistributedHardwareServiceTest, onStop_test_002, TestSize.Level0)
140 {
141     DistributedHardwareService service(ASID, true);
142     service.OnStop();
143 
144     EXPECT_EQ(service.state_, ServiceRunningState::STATE_NOT_START);
145 }
146 
147 /**
148  * @tc.name: dump_test_001
149  * @tc.desc: Verify the Dump function
150  * @tc.type: FUNC
151  * @tc.require: AR000GHSJM
152  */
153 HWTEST_F(DistributedHardwareServiceTest, dump_test_001, TestSize.Level0)
154 {
155     DistributedHardwareService service(ASID, true);
156     int32_t fd = 1;
157 
158     auto ret = service.Dump(fd, std::vector<std::u16string> { ARGS_H });
159     EXPECT_EQ(ret, DH_FWK_SUCCESS);
160     ret = service.Dump(fd, std::vector<std::u16string> { ARGS_L });
161     EXPECT_EQ(ret, DH_FWK_SUCCESS);
162     ret = service.Dump(fd, std::vector<std::u16string> { ARGS_E });
163     EXPECT_EQ(ret, DH_FWK_SUCCESS);
164     ret = service.Dump(fd, std::vector<std::u16string> { ARGS_T });
165     EXPECT_EQ(ret, DH_FWK_SUCCESS);
166     ret = service.Dump(fd, std::vector<std::u16string> { ARGS_C });
167     EXPECT_EQ(ret, DH_FWK_SUCCESS);
168 }
169 
170 /**
171  * @tc.name: OnStart_001
172  * @tc.desc: Verify the OnStart function
173  * @tc.type: FUNC
174  * @tc.require: AR000GHSJM
175  */
176 HWTEST_F(DistributedHardwareServiceTest, OnStart_001, TestSize.Level0)
177 {
178     DistributedHardwareService service(ASID, true);
179     service.state_ = ServiceRunningState::STATE_RUNNING;
180     service.OnStart();
181     service.OnStop();
182     EXPECT_EQ(ServiceRunningState::STATE_NOT_START, service.state_);
183 }
184 } // namespace DistributedHardware
185 } // namespace OHOS
186