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: unregister_publisher_listener_001
74 * @tc.desc: Verify the UnregisterPublisherListener function
75 * @tc.type: FUNC
76 * @tc.require: AR000GHSJM
77 */
78 HWTEST_F(DistributedHardwareServiceTest, unregister_publisher_listener_001, TestSize.Level0)
79 {
80 DistributedHardwareService service(ASID, true);
81 service.RegisterPublisherListener(TOPIC, g_listener);
82 auto ret = service.UnregisterPublisherListener(TOPIC, g_listener);
83
84 EXPECT_EQ(ret, DH_FWK_SUCCESS);
85 }
86
87 /**
88 * @tc.name: publish_message_001
89 * @tc.desc: Verify the PublishMessage function
90 * @tc.type: FUNC
91 * @tc.require: AR000GHSJM
92 */
93 HWTEST_F(DistributedHardwareServiceTest, publish_message_001, TestSize.Level0)
94 {
95 DistributedHardwareService service(ASID, true);
96 std::string msg;
97 service.RegisterPublisherListener(TOPIC, g_listener);
98 auto ret = service.PublishMessage(TOPIC, msg);
99
100 EXPECT_EQ(ret, DH_FWK_SUCCESS);
101 }
102
103 /**
104 * @tc.name: onStop_test_002
105 * @tc.desc: Verify the OnStop function
106 * @tc.type: FUNC
107 * @tc.require: AR000GHSJM
108 */
109 HWTEST_F(DistributedHardwareServiceTest, onStop_test_002, TestSize.Level0)
110 {
111 DistributedHardwareService service(ASID, true);
112 service.OnStop();
113
114 EXPECT_EQ(service.state_, ServiceRunningState::STATE_NOT_START);
115 }
116
117 /**
118 * @tc.name: dump_test_001
119 * @tc.desc: Verify the Dump function
120 * @tc.type: FUNC
121 * @tc.require: AR000GHSJM
122 */
123 HWTEST_F(DistributedHardwareServiceTest, dump_test_001, TestSize.Level0)
124 {
125 DistributedHardwareService service(ASID, true);
126 int32_t fd = 1;
127
128 auto ret = service.Dump(fd, std::vector<std::u16string> { ARGS_H });
129 EXPECT_EQ(ret, DH_FWK_SUCCESS);
130 ret = service.Dump(fd, std::vector<std::u16string> { ARGS_L });
131 EXPECT_EQ(ret, DH_FWK_SUCCESS);
132 ret = service.Dump(fd, std::vector<std::u16string> { ARGS_E });
133 EXPECT_EQ(ret, DH_FWK_SUCCESS);
134 ret = service.Dump(fd, std::vector<std::u16string> { ARGS_T });
135 EXPECT_EQ(ret, DH_FWK_SUCCESS);
136 ret = service.Dump(fd, std::vector<std::u16string> { ARGS_C });
137 EXPECT_EQ(ret, DH_FWK_SUCCESS);
138 }
139
140 /**
141 * @tc.name: OnStart_001
142 * @tc.desc: Verify the OnStart function
143 * @tc.type: FUNC
144 * @tc.require: AR000GHSJM
145 */
146 HWTEST_F(DistributedHardwareServiceTest, OnStart_001, TestSize.Level0)
147 {
148 DistributedHardwareService service(ASID, true);
149 service.state_ = ServiceRunningState::STATE_RUNNING;
150 service.OnStart();
151 service.OnStop();
152 EXPECT_EQ(ServiceRunningState::STATE_NOT_START, service.state_);
153 }
154
155 /**
156 * @tc.name: Init_001
157 * @tc.desc: Verify the Init function
158 * @tc.type: FUNC
159 * @tc.require: AR000GHSJM
160 */
161 HWTEST_F(DistributedHardwareServiceTest, Init_001, TestSize.Level0)
162 {
163 DistributedHardwareService service(ASID, true);
164 service.registerToService_ = false;
165
166 EXPECT_EQ(false, service.Init());
167 }
168 } // namespace DistributedHardware
169 } // namespace OHOS
170