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