1 /*
2 * Copyright (c) 2022-2023 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 "devicestatus_agent_test.h"
17
18 #include "devicestatus_define.h"
19
20 #undef LOG_TAG
21 #define LOG_TAG "DeviceStatusAgentTest"
22
23 namespace OHOS {
24 namespace Msdp {
25 namespace DeviceStatus {
26 using namespace testing::ext;
27 namespace {
28 std::shared_ptr<DeviceStatusAgent> g_agent1;
29 std::shared_ptr<DeviceStatusAgent> g_agent2;
30 } // namespace
31
32 Type DeviceStatusAgentTest::g_agentTest = Type::TYPE_INVALID;
33
SetUpTestCase()34 void DeviceStatusAgentTest::SetUpTestCase() {}
35
TearDownTestCase()36 void DeviceStatusAgentTest::TearDownTestCase() {}
37
SetUp()38 void DeviceStatusAgentTest::SetUp()
39 {
40 g_agent1 = std::make_shared<DeviceStatusAgent>();
41 g_agent2 = std::make_shared<DeviceStatusAgent>();
42 }
43
TearDown()44 void DeviceStatusAgentTest::TearDown() {}
45
OnEventResult(const Data & devicestatusData)46 bool DeviceStatusAgentListenerMockFirstClient::OnEventResult(
47 const Data& devicestatusData)
48 {
49 GTEST_LOG_(INFO) << "agent type: " << devicestatusData.type;
50 GTEST_LOG_(INFO) << "agent value: " << devicestatusData.value;
51 EXPECT_TRUE(devicestatusData.type == DeviceStatusAgentTest::g_agentTest &&
52 (devicestatusData.value >= OnChangedValue::VALUE_INVALID &&
53 devicestatusData.value <= OnChangedValue::VALUE_EXIT));
54 return true;
55 }
56
OnEventResult(const Data & devicestatusData)57 bool DeviceStatusAgentListenerMockSecondClient::OnEventResult(
58 const Data& devicestatusData)
59 {
60 GTEST_LOG_(INFO) << "agent type: " << devicestatusData.type;
61 GTEST_LOG_(INFO) << "agent value: " << devicestatusData.value;
62 EXPECT_TRUE(devicestatusData.type == DeviceStatusAgentTest::g_agentTest &&
63 (devicestatusData.value >= OnChangedValue::VALUE_INVALID &&
64 devicestatusData.value <= OnChangedValue::VALUE_EXIT));
65 return true;
66 }
67
68 /**
69 * @tc.name: DeviceStatusAgentTest001
70 * @tc.desc: test subscribing lid open event
71 * @tc.type: FUNC
72 */
73 HWTEST_F(DeviceStatusAgentTest, DeviceStatusAgentTest001, TestSize.Level1)
74 {
75 CALL_TEST_DEBUG;
76 g_agentTest = Type::TYPE_STILL;
77 Type type = g_agentTest;
78 std::shared_ptr<DeviceStatusAgentListenerMockFirstClient> agentEvent =
79 std::make_shared<DeviceStatusAgentListenerMockFirstClient>();
80 int32_t ret = g_agent1->SubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT,
81 ReportLatencyNs::LONG, agentEvent);
82 EXPECT_TRUE(ret == RET_OK);
83 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
84 sleep(2);
85 g_agent1->UnsubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT);
86 }
87
88 /**
89 * @tc.name: DeviceStatusAgentTest002
90 * @tc.desc: test subscribing lid open event repeatedly
91 * @tc.type: FUNC
92 */
93 HWTEST_F(DeviceStatusAgentTest, DeviceStatusAgentTest002, TestSize.Level1)
94 {
95 CALL_TEST_DEBUG;
96 g_agentTest = Type::TYPE_STILL;
97 Type type = g_agentTest;
98 std::shared_ptr<DeviceStatusAgentListenerMockFirstClient> agentEvent =
99 std::make_shared<DeviceStatusAgentListenerMockFirstClient>();
100 int32_t ret = g_agent1->SubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT,
101 ReportLatencyNs::LONG, agentEvent);
102 EXPECT_TRUE(ret == RET_OK);
103 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
104 sleep(2);
105 ret = g_agent1->UnsubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT);
106 EXPECT_TRUE(ret == RET_OK);
107 GTEST_LOG_(INFO) << "Open and close the lid, and event will not report";
108 sleep(2);
109 ret = g_agent1->SubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT,
110 ReportLatencyNs::LONG, agentEvent);
111 EXPECT_TRUE(ret == RET_OK);
112 GTEST_LOG_(INFO) << "Open and close the lid, and event will report again";
113 sleep(2);
114 g_agent1->UnsubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT);
115 }
116
117 /**
118 * @tc.name: DeviceStatusAgentTest003
119 * @tc.desc: test subscribing lid open event for 2 client
120 * @tc.type: FUNC
121 */
122 HWTEST_F(DeviceStatusAgentTest, DeviceStatusAgentTest003, TestSize.Level1)
123 {
124 CALL_TEST_DEBUG;
125 g_agentTest = Type::TYPE_STILL;
126 Type type = g_agentTest;
127 std::shared_ptr<DeviceStatusAgentListenerMockFirstClient> agentEvent1 =
128 std::make_shared<DeviceStatusAgentListenerMockFirstClient>();
129 std::shared_ptr<DeviceStatusAgentListenerMockSecondClient> agentEvent2 =
130 std::make_shared<DeviceStatusAgentListenerMockSecondClient>();
131 int32_t ret = g_agent1->SubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT,
132 ReportLatencyNs::LONG, agentEvent1);
133 EXPECT_TRUE(ret == RET_OK);
134 ret = g_agent2->SubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT,
135 ReportLatencyNs::LONG, agentEvent2);
136 EXPECT_TRUE(ret == RET_OK);
137 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
138 sleep(2);
139 GTEST_LOG_(INFO) << "Unsubscribe agentEvent1";
140 g_agent1->UnsubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT);
141 sleep(2);
142 GTEST_LOG_(INFO) << "Unsubscribe agentEvent2";
143 g_agent2->UnsubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT);
144 }
145
146 /**
147 * @tc.name: DeviceStatusAgentTest004
148 * @tc.desc: test subscribing lid open event
149 * @tc.type: FUNC
150 */
151 HWTEST_F(DeviceStatusAgentTest, DeviceStatusAgentTest004, TestSize.Level1)
152 {
153 CALL_TEST_DEBUG;
154 std::shared_ptr<DeviceStatusAgentListenerMockFirstClient> agentEvent =
155 std::make_shared<DeviceStatusAgentListenerMockFirstClient>();
156 int32_t ret = g_agent1->SubscribeAgentEvent(Type::TYPE_INVALID, ActivityEvent::ENTER_EXIT,
157 ReportLatencyNs::LONG, agentEvent);
158 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
159 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
160 sleep(2);
161 ret = g_agent1->UnsubscribeAgentEvent(Type::TYPE_INVALID, ActivityEvent::ENTER_EXIT);
162 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
163 }
164
165 /**
166 * @tc.name: DeviceStatusAgentTest005
167 * @tc.desc: test subscribing lid open event
168 * @tc.type: FUNC
169 */
170 HWTEST_F(DeviceStatusAgentTest, DeviceStatusAgentTest005, TestSize.Level1)
171 {
172 CALL_TEST_DEBUG;
173 std::shared_ptr<DeviceStatusAgentListenerMockFirstClient> agentEvent =
174 std::make_shared<DeviceStatusAgentListenerMockFirstClient>();
175 int32_t ret = g_agent1->SubscribeAgentEvent(static_cast<Type>(10), ActivityEvent::ENTER_EXIT,
176 ReportLatencyNs::LONG, agentEvent);
177 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
178 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
179 sleep(2);
180 ret = g_agent1->UnsubscribeAgentEvent(Type::TYPE_INVALID, ActivityEvent::ENTER_EXIT);
181 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
182 }
183
184 /**
185 * @tc.name: DeviceStatusAgentTest006
186 * @tc.desc: test subscribing lid open event for 2 client
187 * @tc.type: FUNC
188 */
189 HWTEST_F(DeviceStatusAgentTest, DeviceStatusAgentTest006, TestSize.Level1)
190 {
191 CALL_TEST_DEBUG;
192 g_agentTest = Type::TYPE_STILL;
193 Type type = g_agentTest;
194 std::shared_ptr<DeviceStatusAgentListenerMockFirstClient> agentEvent =
195 std::make_shared<DeviceStatusAgentListenerMockFirstClient>();
196 int32_t ret = g_agent1->SubscribeAgentEvent(Type::TYPE_INVALID, ActivityEvent::ENTER_EXIT,
197 ReportLatencyNs::LONG, agentEvent);
198 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
199 ret = g_agent1->SubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT,
200 ReportLatencyNs::LONG, agentEvent);
201 EXPECT_TRUE(ret == RET_OK);
202 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
203 sleep(2);
204 ret = g_agent1->UnsubscribeAgentEvent(Type::TYPE_INVALID, ActivityEvent::ENTER_EXIT);
205 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
206 ret = g_agent1->UnsubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT);
207 EXPECT_TRUE(ret == RET_OK);
208 }
209
210 /**
211 * @tc.name: DeviceStatusAgentTest007
212 * @tc.desc: test subscribing lid open event
213 * @tc.type: FUNC
214 */
215 HWTEST_F(DeviceStatusAgentTest, DeviceStatusAgentTest007, TestSize.Level1)
216 {
217 CALL_TEST_DEBUG;
218 std::shared_ptr<DeviceStatusAgentListenerMockFirstClient> agentEvent = nullptr;
219 int32_t ret = g_agent1->SubscribeAgentEvent(Type::TYPE_STILL, ActivityEvent::ENTER_EXIT,
220 ReportLatencyNs::LONG, agentEvent);
221 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
222 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
223 }
224
225 /**
226 * @tc.name: DeviceStatusAgentTest008
227 * @tc.desc: test subscribing lid open event for 3 client
228 * @tc.type: FUNC
229 */
230 HWTEST_F(DeviceStatusAgentTest, DeviceStatusAgentTest008, TestSize.Level1)
231 {
232 CALL_TEST_DEBUG;
233 g_agentTest = Type::TYPE_HORIZONTAL_POSITION;
234 Type type = g_agentTest;
235 std::shared_ptr<DeviceStatusAgentListenerMockFirstClient> agentEvent =
236 std::make_shared<DeviceStatusAgentListenerMockFirstClient>();
237 std::shared_ptr<DeviceStatusAgentListenerMockFirstClient> agentEvent1 = nullptr;
238 int32_t ret = g_agent1->SubscribeAgentEvent(Type::TYPE_INVALID, ActivityEvent::ENTER_EXIT,
239 ReportLatencyNs::LONG, agentEvent);
240 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
241 ret = g_agent1->SubscribeAgentEvent(static_cast<Type>(10), ActivityEvent::ENTER_EXIT, ReportLatencyNs::LONG,
242 agentEvent);
243 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
244 ret = g_agent1->SubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT,
245 ReportLatencyNs::LONG, agentEvent1);
246 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
247 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
248 sleep(2);
249 ret = g_agent1->UnsubscribeAgentEvent(Type::TYPE_INVALID, ActivityEvent::ENTER_EXIT);
250 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
251 ret = g_agent1->UnsubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT);
252 EXPECT_TRUE(ret == RET_OK);
253 ret = g_agent1->UnsubscribeAgentEvent(static_cast<Type>(10), ActivityEvent::ENTER_EXIT);
254 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
255 }
256
257 /**
258 * @tc.name: DeviceStatusAgentTest009
259 * @tc.desc: test subscribing lid open event
260 * @tc.type: FUNC
261 */
262 HWTEST_F(DeviceStatusAgentTest, DeviceStatusAgentTest009, TestSize.Level1)
263 {
264 CALL_TEST_DEBUG;
265 g_agentTest = Type::TYPE_STILL;
266 Type type = g_agentTest;
267 std::shared_ptr<DeviceStatusAgentListenerMockFirstClient> agentEvent =
268 std::make_shared<DeviceStatusAgentListenerMockFirstClient>();
269 int32_t ret = g_agent1->SubscribeAgentEvent(type, ActivityEvent::ENTER_EXIT,
270 ReportLatencyNs::LONG, agentEvent);
271 EXPECT_TRUE(ret == RET_OK);
272
273 sptr<IRemoteDevStaCallback> callback = new (std::nothrow) DeviceStatusAgent::DeviceStatusAgentCallback(g_agent1);
274 ASSERT_TRUE(callback != nullptr);
275 Data devicestatusData = {
276 Type::TYPE_STILL,
277 OnChangedValue::VALUE_ENTER
278 };
279 callback->OnDeviceStatusChanged(devicestatusData);
280 }
281 } // namespace DeviceStatus
282 } // namespace Msdp
283 } // namespace OHOS
284