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_common.h"
19
20 using namespace testing::ext;
21 using namespace OHOS::Msdp;
22 using namespace OHOS;
23 using namespace std;
24
25 static std::shared_ptr<DeviceStatusAgent> agent1_;
26 static std::shared_ptr<DeviceStatusAgent> agent2_;
27
SetUpTestCase()28 void DevicestatusAgentTest::SetUpTestCase() {}
29
TearDownTestCase()30 void DevicestatusAgentTest::TearDownTestCase() {}
31
SetUp()32 void DevicestatusAgentTest::SetUp()
33 {
34 agent1_ = std::make_shared<DeviceStatusAgent>();
35 agent2_ = std::make_shared<DeviceStatusAgent>();
36 }
37
TearDown()38 void DevicestatusAgentTest::TearDown() {}
39
OnEventResult(const DevicestatusDataUtils::DevicestatusData & devicestatusData)40 bool DevicestatusAgentListenerMockFirstClient::OnEventResult(
41 const DevicestatusDataUtils::DevicestatusData& devicestatusData)
42 {
43 GTEST_LOG_(INFO) << "agent type: " << devicestatusData.type;
44 GTEST_LOG_(INFO) << "agent value: " << devicestatusData.value;
45 EXPECT_TRUE(devicestatusData.type > DevicestatusDataUtils::DevicestatusType::TYPE_INVALID
46 && devicestatusData.type < DevicestatusDataUtils::DevicestatusType::TYPE_MAX);
47 return true;
48 }
49
OnEventResult(const DevicestatusDataUtils::DevicestatusData & devicestatusData)50 bool DevicestatusAgentListenerMockSecondClient::OnEventResult(
51 const DevicestatusDataUtils::DevicestatusData& devicestatusData)
52 {
53 GTEST_LOG_(INFO) << "agent type: " << devicestatusData.type;
54 GTEST_LOG_(INFO) << "agent value: " << devicestatusData.value;
55 EXPECT_TRUE(devicestatusData.type > DevicestatusDataUtils::DevicestatusType::TYPE_INVALID
56 && devicestatusData.type < DevicestatusDataUtils::DevicestatusType::TYPE_MAX);
57 return true;
58 }
59
60 namespace {
61 /**
62 * @tc.name: DevicestatusAgentTest001
63 * @tc.desc: test subscribing lid open event
64 * @tc.type: FUNC
65 */
66 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest001, TestSize.Level1)
67 {
68 GTEST_LOG_(INFO) << "DevicestatusAgentTest001 start";
69 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
70 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
71 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent);
72 EXPECT_TRUE(ret == ERR_OK);
73 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
74 sleep(2);
75 agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
76 GTEST_LOG_(INFO) << "DevicestatusAgentTest001 end";
77 }
78
79 /**
80 * @tc.name: DevicestatusAgentTest002
81 * @tc.desc: test subscribing lid open event repeatedly
82 * @tc.type: FUNC
83 */
84 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest002, TestSize.Level1)
85 {
86 GTEST_LOG_(INFO) << "DevicestatusAgentTest002 start";
87 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
88 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
89 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent);
90 EXPECT_TRUE(ret == ERR_OK);
91 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
92 sleep(2);
93 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
94 EXPECT_TRUE(ret == ERR_OK);
95 GTEST_LOG_(INFO) << "Open and close the lid, and event will not report";
96 sleep(2);
97 ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent);
98 EXPECT_TRUE(ret == ERR_OK);
99 GTEST_LOG_(INFO) << "Open and close the lid, and event will report again";
100 sleep(2);
101 agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
102 GTEST_LOG_(INFO) << "DevicestatusAgentTest002 end";
103 }
104
105 /**
106 * @tc.name: DevicestatusAgentTest003
107 * @tc.desc: test subscribing lid open event for 2 client
108 * @tc.type: FUNC
109 */
110 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest003, TestSize.Level1)
111 {
112 GTEST_LOG_(INFO) << "DevicestatusAgentTest003 start";
113 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent1 =
114 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
115 std::shared_ptr<DevicestatusAgentListenerMockSecondClient> agentEvent2 =
116 std::make_shared<DevicestatusAgentListenerMockSecondClient>();
117 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent1);
118 EXPECT_TRUE(ret == ERR_OK);
119 ret = agent2_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent2);
120 EXPECT_TRUE(ret == ERR_OK);
121 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
122 sleep(2);
123 GTEST_LOG_(INFO) << "UnSubscribe agentEvent1";
124 agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
125 sleep(2);
126 GTEST_LOG_(INFO) << "UnSubscribe agentEvent2";
127 agent2_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
128 GTEST_LOG_(INFO) << "DevicestatusAgentTest003 end";
129 }
130
131 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest004, TestSize.Level1)
132 {
133 GTEST_LOG_(INFO) << "DevicestatusAgentTest004 start";
134 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
135 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
136 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_CAR_BLUETOOTH, agentEvent);
137 EXPECT_TRUE(ret == ERR_OK);
138 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
139 sleep(2);
140 agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_CAR_BLUETOOTH);
141 GTEST_LOG_(INFO) << "DevicestatusAgentTest004 end";
142 }
143
144 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest005, TestSize.Level1)
145 {
146 GTEST_LOG_(INFO) << "DevicestatusAgentTest005 start";
147 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
148 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
149 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_RELATIVE_STILL, agentEvent);
150 EXPECT_TRUE(ret == ERR_OK);
151 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
152 sleep(2);
153 agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_RELATIVE_STILL);
154 GTEST_LOG_(INFO) << "DevicestatusAgentTest005 end";
155 }
156
157 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest006, TestSize.Level1)
158 {
159 GTEST_LOG_(INFO) << "DevicestatusAgentTest006 start";
160 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
161 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
162 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_STILL, agentEvent);
163 EXPECT_TRUE(ret == ERR_OK);
164 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
165 sleep(2);
166 agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_STILL);
167 GTEST_LOG_(INFO) << "DevicestatusAgentTest006 end";
168 }
169
170 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest007, TestSize.Level1)
171 {
172 GTEST_LOG_(INFO) << "DevicestatusAgentTest007 start";
173 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
174 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
175 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_INVALID, agentEvent);
176 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
177 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
178 sleep(2);
179 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_INVALID);
180 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
181 GTEST_LOG_(INFO) << "DevicestatusAgentTest007 end";
182 }
183
184 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest008, TestSize.Level1)
185 {
186 GTEST_LOG_(INFO) << "DevicestatusAgentTest008 start";
187 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
188 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
189 int32_t ret = agent1_->SubscribeAgentEvent(static_cast<DevicestatusDataUtils::DevicestatusType>(10), agentEvent);
190 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
191 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
192 sleep(2);
193 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_INVALID);
194 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
195 GTEST_LOG_(INFO) << "DevicestatusAgentTest008 end";
196 }
197
198 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest009, TestSize.Level1)
199 {
200 GTEST_LOG_(INFO) << "DevicestatusAgentTest009 start";
201 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
202 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
203 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_INVALID, agentEvent);
204 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
205 ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_STILL, agentEvent);
206 EXPECT_TRUE(ret == ERR_OK);
207 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
208 sleep(2);
209 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_INVALID);
210 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
211 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_STILL);
212 EXPECT_TRUE(ret == ERR_OK);
213 GTEST_LOG_(INFO) << "DevicestatusAgentTest009 end";
214 }
215
216 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest010, TestSize.Level1)
217 {
218 GTEST_LOG_(INFO) << "DevicestatusAgentTest010 start";
219 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent = nullptr;
220 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent);
221 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
222 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
223 GTEST_LOG_(INFO) << "DevicestatusAgentTest010 end";
224 }
225
226 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest011, TestSize.Level1)
227 {
228 GTEST_LOG_(INFO) << "DevicestatusAgentTest011 start";
229 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
230 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
231 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent1 = nullptr;
232 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_INVALID, agentEvent);
233 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
234 ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_STILL, agentEvent);
235 EXPECT_TRUE(ret == ERR_OK);
236 ret = agent1_->SubscribeAgentEvent(static_cast<DevicestatusDataUtils::DevicestatusType>(10), agentEvent);
237 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
238 ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_RELATIVE_STILL, agentEvent);
239 EXPECT_TRUE(ret == ERR_OK);
240 ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_CAR_BLUETOOTH, agentEvent1);
241 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
242 ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent);
243 EXPECT_TRUE(ret == ERR_OK);
244 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
245 sleep(2);
246 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_INVALID);
247 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
248 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_STILL);
249 EXPECT_TRUE(ret == ERR_OK);
250 ret = agent1_->UnSubscribeAgentEvent(static_cast<DevicestatusDataUtils::DevicestatusType>(10));
251 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
252 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_RELATIVE_STILL);
253 EXPECT_TRUE(ret == ERR_OK);
254 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_CAR_BLUETOOTH);
255 EXPECT_TRUE(ret == ERR_OK);
256 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN);
257 EXPECT_TRUE(ret == ERR_OK);
258 GTEST_LOG_(INFO) << "DevicestatusAgentTest011 end";
259 }
260
261 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest012, TestSize.Level1)
262 {
263 GTEST_LOG_(INFO) << "DevicestatusAgentTest012 start";
264 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
265 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
266 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent1 = nullptr;
267 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_INVALID, agentEvent);
268 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
269 ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_STILL, agentEvent);
270 EXPECT_TRUE(ret == ERR_OK);
271 ret = agent1_->SubscribeAgentEvent(static_cast<DevicestatusDataUtils::DevicestatusType>(10), agentEvent);
272 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
273 ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_CAR_BLUETOOTH, agentEvent1);
274 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
275 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
276 sleep(2);
277 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_INVALID);
278 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
279 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_STILL);
280 EXPECT_TRUE(ret == ERR_OK);
281 ret = agent1_->UnSubscribeAgentEvent(static_cast<DevicestatusDataUtils::DevicestatusType>(10));
282 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
283 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_RELATIVE_STILL);
284 EXPECT_TRUE(ret == ERR_OK);
285 GTEST_LOG_(INFO) << "DevicestatusAgentTest012 end";
286 }
287
288 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest013, TestSize.Level1)
289 {
290 GTEST_LOG_(INFO) << "DevicestatusAgentTest013 start";
291 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
292 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
293 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent1 = nullptr;
294 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_INVALID, agentEvent);
295 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
296 ret = agent1_->SubscribeAgentEvent(static_cast<DevicestatusDataUtils::DevicestatusType>(10), agentEvent);
297 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
298 ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_STILL, agentEvent1);
299 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
300 GTEST_LOG_(INFO) << "Open and close the lid, and event will report";
301 sleep(2);
302 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_INVALID);
303 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
304 ret = agent1_->UnSubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_STILL);
305 EXPECT_TRUE(ret == ERR_OK);
306 ret = agent1_->UnSubscribeAgentEvent(static_cast<DevicestatusDataUtils::DevicestatusType>(10));
307 EXPECT_TRUE(ret == ERR_INVALID_VALUE);
308 GTEST_LOG_(INFO) << "DevicestatusAgentTest013 end";
309 }
310
311 HWTEST_F (DevicestatusAgentTest, DevicestatusAgentTest014, TestSize.Level1)
312 {
313 GTEST_LOG_(INFO) << "DevicestatusAgentTest014 start";
314 std::shared_ptr<DevicestatusAgentListenerMockFirstClient> agentEvent =
315 std::make_shared<DevicestatusAgentListenerMockFirstClient>();
316 int32_t ret = agent1_->SubscribeAgentEvent(DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN, agentEvent);
317 EXPECT_TRUE(ret == ERR_OK);
318
319 sptr<IdevicestatusCallback> callback = new DeviceStatusAgent::DeviceStatusAgentCallback(agent1_);
320 DevicestatusDataUtils::DevicestatusData devicestatusData = {
321 DevicestatusDataUtils::DevicestatusType::TYPE_LID_OPEN,
322 DevicestatusDataUtils::DevicestatusValue::VALUE_ENTER
323 };
324 callback->OnDevicestatusChanged(devicestatusData);
325 GTEST_LOG_(INFO) << "DevicestatusAgentTest014 end";
326 }
327 }
328