1 /*
2 * Copyright (c) 2024 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 "gtest/gtest.h"
17 #include "motion_sensor_monitor.h"
18 #include "common_constant.h"
19 #include "state_manager_adapter.h"
20 #include "constraint_manager_adapter.h"
21 #include "standby_config_manager.h"
22 #include "standby_service_impl.h"
23 #include "dark_state.h"
24
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace DevStandbyMgr {
29 class MotionSensorMonitorTest : public testing::Test {
30 public:
31 static void SetUpTestCase();
32 static void TearDownTestCase();
33 void SetUp() override;
TearDown()34 void TearDown() override {}
35 };
36
TearDownTestCase()37 void MotionSensorMonitorTest::TearDownTestCase() {}
38
SetUpTestCase()39 void MotionSensorMonitorTest::SetUpTestCase() {}
40
SetUp()41 void MotionSensorMonitorTest::SetUp()
42 {
43 StandbyServiceImpl::GetInstance()->standbyStateManager_ = std::make_shared<StateManagerAdapter>();
44 StandbyServiceImpl::GetInstance()->constraintManager_ = std::make_shared<ConstraintManagerAdapter>();
45 StandbyServiceImpl::GetInstance()->Init();
46 }
47
48 /**
49 * @tc.name: Init
50 * @tc.desc: test Init.
51 * @tc.type: FUNC
52 * @tc.require:
53 */
54 HWTEST_F(MotionSensorMonitorTest, Init, TestSize.Level1)
55 {
56 ConstraintEvalParam repeatedMotionParams{};
57 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
58 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
59 bool ret = repeatedMotionConstraint->Init();
60 EXPECT_EQ(ret, true);
61 }
62
63 /**
64 * @tc.name: AcceleromterCallback
65 * @tc.desc: test AcceleromterCallback.
66 * @tc.type: FUNC
67 * @tc.require:
68 */
69 HWTEST_F(MotionSensorMonitorTest, AcceleromterCallback, TestSize.Level1)
70 {
71 ConstraintEvalParam repeatedMotionParams{};
72 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
73 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
74 StandbyConfigManager::GetInstance()->standbyParaMap_[MOTION_THREADSHOLD] = 0;
75 SensorEvent event;
76 GravityData data = { 0, 0, 0 };
77 event.sensorTypeId = SENSOR_TYPE_ID_NONE;
78 event.data = reinterpret_cast<uint8_t *>(&data);
79 repeatedMotionConstraint->energy_ = 10000;
80 repeatedMotionConstraint->AcceleromterCallback(&event);
81 repeatedMotionConstraint->AcceleromterCallback(nullptr);
82 }
83
84 /**
85 * @tc.name: RepeatAcceleromterCallback
86 * @tc.desc: test RepeatAcceleromterCallback.
87 * @tc.type: FUNC
88 * @tc.require:
89 */
90 HWTEST_F(MotionSensorMonitorTest, RepeatAcceleromterCallback, TestSize.Level1)
91 {
92 ConstraintEvalParam repeatedMotionParams{};
93 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
94 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
95 StandbyConfigManager::GetInstance()->standbyParaMap_[MOTION_THREADSHOLD] = 0;
96 SensorEvent event;
97 GravityData data = { 0, 0, 0 };
98 event.sensorTypeId = SENSOR_TYPE_ID_NONE;
99 event.data = reinterpret_cast<uint8_t *>(&data);
100 repeatedMotionConstraint->RepeatAcceleromterCallback(&event);
101 repeatedMotionConstraint->energy_ = 10000;
102 repeatedMotionConstraint->RepeatAcceleromterCallback(&event);
103 repeatedMotionConstraint->RepeatAcceleromterCallback(nullptr);
104 }
105
106 /**
107 * @tc.name: MotionSensorCallback
108 * @tc.desc: test MotionSensorCallback.
109 * @tc.type: FUNC
110 * @tc.require:
111 */
112 HWTEST_F(MotionSensorMonitorTest, MotionSensorCallback, TestSize.Level1)
113 {
114 ConstraintEvalParam repeatedMotionParams{};
115 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
116 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
117 repeatedMotionConstraint->MotionSensorCallback(nullptr);
118 EXPECT_EQ(StandbyServiceImpl::GetInstance()->GetStateManager()->isEvalution_, false);
119 }
120
121 /**
122 * @tc.name: SetEnergy
123 * @tc.desc: test SetEnergy.
124 * @tc.type: FUNC
125 * @tc.require:
126 */
127 HWTEST_F(MotionSensorMonitorTest, SetEnergy, TestSize.Level1)
128 {
129 ConstraintEvalParam repeatedMotionParams{};
130 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
131 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
132 double energy = 10000;
133 repeatedMotionConstraint->SetEnergy(energy);
134 EXPECT_EQ(repeatedMotionConstraint->GetEnergy(), energy);
135 }
136
137 /**
138 * @tc.name: AssignAcclerometerSensorCallBack
139 * @tc.desc: test AssignAcclerometerSensorCallBack001.
140 * @tc.type: FUNC
141 * @tc.require:
142 */
143 HWTEST_F(MotionSensorMonitorTest, AssignAcclerometerSensorCallBack001, TestSize.Level1)
144 {
145 ConstraintEvalParam repeatedMotionParams{};
146 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
147 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
148 repeatedMotionConstraint->sensorUserMap_.clear();
149 repeatedMotionConstraint->AssignAcclerometerSensorCallBack();
150 }
151
152 /**
153 * @tc.name: AssignAcclerometerSensorCallBack
154 * @tc.desc: test AssignAcclerometerSensorCallBack002.
155 * @tc.type: FUNC
156 * @tc.require:
157 */
158 HWTEST_F(MotionSensorMonitorTest, AssignAcclerometerSensorCallBack002, TestSize.Level1)
159 {
160 ConstraintEvalParam repeatedMotionParams{};
161 repeatedMotionParams.isRepeatedDetection_ = false;
162 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
163 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
164 repeatedMotionConstraint->sensorUserMap_.clear();
165 repeatedMotionConstraint->sensorUserMap_.emplace(SENSOR_TYPE_ID_ACCELEROMETER, SensorUser{});
166 repeatedMotionConstraint->AssignAcclerometerSensorCallBack();
167 }
168
169 /**
170 * @tc.name: AssignAcclerometerSensorCallBack
171 * @tc.desc: test AssignAcclerometerSensorCallBack003.
172 * @tc.type: FUNC
173 * @tc.require:
174 */
175 HWTEST_F(MotionSensorMonitorTest, AssignAcclerometerSensorCallBack003, TestSize.Level1)
176 {
177 ConstraintEvalParam repeatedMotionParams{};
178 repeatedMotionParams.isRepeatedDetection_ = true;
179 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
180 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
181 repeatedMotionConstraint->sensorUserMap_.clear();
182 repeatedMotionConstraint->sensorUserMap_.emplace(SENSOR_TYPE_ID_ACCELEROMETER, SensorUser{});
183 repeatedMotionConstraint->AssignAcclerometerSensorCallBack();
184 }
185
186 /**
187 * @tc.name: AssignMotionSensorCallBack
188 * @tc.desc: test AssignMotionSensorCallBack001.
189 * @tc.type: FUNC
190 * @tc.require:
191 */
192 HWTEST_F(MotionSensorMonitorTest, AssignMotionSensorCallBack001, TestSize.Level1)
193 {
194 ConstraintEvalParam repeatedMotionParams{};
195 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
196 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
197 repeatedMotionConstraint->sensorUserMap_.clear();
198 repeatedMotionConstraint->sensorUserMap_.emplace(SENSOR_TYPE_ID_SIGNIFICANT_MOTION, SensorUser{});
199 repeatedMotionConstraint->AssignMotionSensorCallBack();
200 }
201
202 /**
203 * @tc.name: AssignMotionSensorCallBack
204 * @tc.desc: test AssignMotionSensorCallBack002.
205 * @tc.type: FUNC
206 * @tc.require:
207 */
208 HWTEST_F(MotionSensorMonitorTest, AssignMotionSensorCallBack002, TestSize.Level1)
209 {
210 ConstraintEvalParam repeatedMotionParams{};
211 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
212 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
213 repeatedMotionConstraint->sensorUserMap_.clear();
214 repeatedMotionConstraint->AssignMotionSensorCallBack();
215 }
216
217 /**
218 * @tc.name: StartMonitoring
219 * @tc.desc: test StartMonitoring.
220 * @tc.type: FUNC
221 * @tc.require:
222 */
223 HWTEST_F(MotionSensorMonitorTest, StartMonitoring, TestSize.Level1)
224 {
225 ConstraintEvalParam repeatedMotionParams{};
226 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
227 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
228 repeatedMotionConstraint->StartMonitoring();
229 }
230
231 /**
232 * @tc.name: StopMonitoring
233 * @tc.desc: test StopMonitoring.
234 * @tc.type: FUNC
235 * @tc.require:
236 */
237 HWTEST_F(MotionSensorMonitorTest, StopMonitoring, TestSize.Level1)
238 {
239 ConstraintEvalParam repeatedMotionParams{};
240 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
241 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
242 repeatedMotionConstraint->StopMonitoring();
243 }
244
245 /**
246 * @tc.name: StartSensor
247 * @tc.desc: test StartSensor.
248 * @tc.type: FUNC
249 * @tc.require:
250 */
251 HWTEST_F(MotionSensorMonitorTest, StartSensor, TestSize.Level1)
252 {
253 ConstraintEvalParam repeatedMotionParams{};
254 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
255 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
256 repeatedMotionConstraint->Init();
257 repeatedMotionConstraint->StartSensor();
258 }
259
260 /**
261 * @tc.name: StopSensor
262 * @tc.desc: test StopSensor.
263 * @tc.type: FUNC
264 * @tc.require:
265 */
266 HWTEST_F(MotionSensorMonitorTest, StopSensor, TestSize.Level1)
267 {
268 ConstraintEvalParam repeatedMotionParams{};
269 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
270 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
271 repeatedMotionConstraint->Init();
272 repeatedMotionConstraint->isMonitoring_ = true;
273 repeatedMotionConstraint->StopSensor();
274 }
275 } // namespace DevStandbyMgr
276 } // namespace OHOS
277