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 }
119
120 /**
121 * @tc.name: SetEnergy
122 * @tc.desc: test SetEnergy.
123 * @tc.type: FUNC
124 * @tc.require:
125 */
126 HWTEST_F(MotionSensorMonitorTest, SetEnergy, TestSize.Level1)
127 {
128 ConstraintEvalParam repeatedMotionParams{};
129 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
130 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
131 double energy = 10000;
132 repeatedMotionConstraint->SetEnergy(energy);
133 EXPECT_EQ(repeatedMotionConstraint->GetEnergy(), energy);
134 }
135
136 /**
137 * @tc.name: AssignAcclerometerSensorCallBack
138 * @tc.desc: test AssignAcclerometerSensorCallBack001.
139 * @tc.type: FUNC
140 * @tc.require:
141 */
142 HWTEST_F(MotionSensorMonitorTest, AssignAcclerometerSensorCallBack001, TestSize.Level1)
143 {
144 ConstraintEvalParam repeatedMotionParams{};
145 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
146 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
147 repeatedMotionConstraint->sensorUserMap_.clear();
148 repeatedMotionConstraint->AssignAcclerometerSensorCallBack();
149 }
150
151 /**
152 * @tc.name: AssignAcclerometerSensorCallBack
153 * @tc.desc: test AssignAcclerometerSensorCallBack002.
154 * @tc.type: FUNC
155 * @tc.require:
156 */
157 HWTEST_F(MotionSensorMonitorTest, AssignAcclerometerSensorCallBack002, TestSize.Level1)
158 {
159 ConstraintEvalParam repeatedMotionParams{};
160 repeatedMotionParams.isRepeatedDetection_ = false;
161 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
162 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
163 repeatedMotionConstraint->sensorUserMap_.clear();
164 repeatedMotionConstraint->sensorUserMap_.emplace(SENSOR_TYPE_ID_ACCELEROMETER, SensorUser{});
165 repeatedMotionConstraint->AssignAcclerometerSensorCallBack();
166 }
167
168 /**
169 * @tc.name: AssignAcclerometerSensorCallBack
170 * @tc.desc: test AssignAcclerometerSensorCallBack003.
171 * @tc.type: FUNC
172 * @tc.require:
173 */
174 HWTEST_F(MotionSensorMonitorTest, AssignAcclerometerSensorCallBack003, TestSize.Level1)
175 {
176 ConstraintEvalParam repeatedMotionParams{};
177 repeatedMotionParams.isRepeatedDetection_ = true;
178 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
179 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
180 repeatedMotionConstraint->sensorUserMap_.clear();
181 repeatedMotionConstraint->sensorUserMap_.emplace(SENSOR_TYPE_ID_ACCELEROMETER, SensorUser{});
182 repeatedMotionConstraint->AssignAcclerometerSensorCallBack();
183 }
184
185 /**
186 * @tc.name: AssignMotionSensorCallBack
187 * @tc.desc: test AssignMotionSensorCallBack001.
188 * @tc.type: FUNC
189 * @tc.require:
190 */
191 HWTEST_F(MotionSensorMonitorTest, AssignMotionSensorCallBack001, TestSize.Level1)
192 {
193 ConstraintEvalParam repeatedMotionParams{};
194 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
195 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
196 repeatedMotionConstraint->sensorUserMap_.clear();
197 repeatedMotionConstraint->sensorUserMap_.emplace(SENSOR_TYPE_ID_SIGNIFICANT_MOTION, SensorUser{});
198 repeatedMotionConstraint->AssignMotionSensorCallBack();
199 }
200
201 /**
202 * @tc.name: AssignMotionSensorCallBack
203 * @tc.desc: test AssignMotionSensorCallBack002.
204 * @tc.type: FUNC
205 * @tc.require:
206 */
207 HWTEST_F(MotionSensorMonitorTest, AssignMotionSensorCallBack002, TestSize.Level1)
208 {
209 ConstraintEvalParam repeatedMotionParams{};
210 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
211 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
212 repeatedMotionConstraint->sensorUserMap_.clear();
213 repeatedMotionConstraint->AssignMotionSensorCallBack();
214 }
215
216 /**
217 * @tc.name: StartMonitoring
218 * @tc.desc: test StartMonitoring.
219 * @tc.type: FUNC
220 * @tc.require:
221 */
222 HWTEST_F(MotionSensorMonitorTest, StartMonitoring, TestSize.Level1)
223 {
224 ConstraintEvalParam repeatedMotionParams{};
225 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
226 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
227 repeatedMotionConstraint->StartMonitoring();
228 }
229
230 /**
231 * @tc.name: StopMonitoring
232 * @tc.desc: test StopMonitoring.
233 * @tc.type: FUNC
234 * @tc.require:
235 */
236 HWTEST_F(MotionSensorMonitorTest, StopMonitoring, TestSize.Level1)
237 {
238 ConstraintEvalParam repeatedMotionParams{};
239 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
240 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
241 repeatedMotionConstraint->StopMonitoring();
242 }
243
244 /**
245 * @tc.name: StartSensor
246 * @tc.desc: test StartSensor.
247 * @tc.type: FUNC
248 * @tc.require:
249 */
250 HWTEST_F(MotionSensorMonitorTest, StartSensor, TestSize.Level1)
251 {
252 ConstraintEvalParam repeatedMotionParams{};
253 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
254 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
255 repeatedMotionConstraint->Init();
256 repeatedMotionConstraint->StartSensor();
257 }
258
259 /**
260 * @tc.name: StopSensor
261 * @tc.desc: test StopSensor.
262 * @tc.type: FUNC
263 * @tc.require:
264 */
265 HWTEST_F(MotionSensorMonitorTest, StopSensor, TestSize.Level1)
266 {
267 ConstraintEvalParam repeatedMotionParams{};
268 auto repeatedMotionConstraint = std::make_shared<MotionSensorMonitor>(
269 PERIODLY_TASK_DECTION_TIMEOUT, PERIODLY_TASK_REST_TIMEOUT, PERIODLY_TASK_TOTAL_TIMEOUT, repeatedMotionParams);
270 repeatedMotionConstraint->Init();
271 repeatedMotionConstraint->isMonitoring_ = true;
272 repeatedMotionConstraint->StopSensor();
273 }
274 } // namespace DevStandbyMgr
275 } // namespace OHOS
276