1 /*
2 * Copyright (c) 2022 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
18 #include "display_manager_service.h"
19 #include "sensor_connector.h"
20 #include "screen_rotation_controller.h"
21
22 using namespace testing;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Rosen {
27 namespace {
28 constexpr uint32_t SLEEP_TIME_US = 2000000;
29 }
30 class ScreenRotationControllerTest : public testing::Test {
31 public:
32 static void SetUpTestCase();
33 static void TearDownTestCase();
34 void SetUp() override;
35 void TearDown() override;
36 };
37
SetUpTestCase()38 void ScreenRotationControllerTest::SetUpTestCase()
39 {
40 DisplayManagerService::GetInstance().abstractScreenController_->defaultRsScreenId_ = 0;
41 DisplayManagerService::GetInstance().abstractScreenController_->screenIdManager_.rs2DmsScreenIdMap_.clear();
42 DisplayManagerService::GetInstance().abstractScreenController_->screenIdManager_.rs2DmsScreenIdMap_ = {
43 {0, 0}
44 };
45 DisplayManagerService::GetInstance().abstractScreenController_->screenIdManager_.dms2RsScreenIdMap_.clear();
46 DisplayManagerService::GetInstance().abstractScreenController_->screenIdManager_.dms2RsScreenIdMap_ = {
47 {0, 0}
48 };
49
50 std::string name = "testDisplay";
51 sptr<SupportedScreenModes> info = new SupportedScreenModes();
52 info->width_ = 100; // 100 width
53 info->height_ = 200; // 200 height
54 sptr<AbstractScreen> absScreen = new AbstractScreen(DisplayManagerService::GetInstance().abstractScreenController_,
55 name, 0, 0);
56 absScreen->activeIdx_ = 0;
57 absScreen->modes_.clear();
58 absScreen->modes_ = { { info } };
59 sptr<AbstractDisplay> absDisplay = new AbstractDisplay(0, info, absScreen);
60 DisplayManagerService::GetInstance().abstractDisplayController_->abstractDisplayMap_ = {
61 {0, absDisplay}
62 };
63 DisplayManagerService::GetInstance().abstractScreenController_->dmsScreenMap_ = {
64 {0, absScreen}
65 };
66 }
67
TearDownTestCase()68 void ScreenRotationControllerTest::TearDownTestCase()
69 {
70 }
71
SetUp()72 void ScreenRotationControllerTest::SetUp()
73 {
74 }
75
TearDown()76 void ScreenRotationControllerTest::TearDown()
77 {
78 }
79
80 namespace {
81 /**
82 * @tc.name: ScreenRotationLocked
83 * @tc.desc: Set and get screen rotation locked
84 * @tc.type: FUNC
85 */
86 HWTEST_F(ScreenRotationControllerTest, ScreenRotationLocked, Function | SmallTest | Level3)
87 {
88 ScreenRotationController::SetScreenRotationLocked(false);
89 ASSERT_EQ(false, ScreenRotationController::IsScreenRotationLocked());
90
91 ScreenRotationController::SetScreenRotationLocked(true);
92 ASSERT_EQ(true, ScreenRotationController::IsScreenRotationLocked());
93 }
94
95 /**
96 * @tc.name: DefaultDeviceRotationOffset
97 * @tc.desc: Set default device rotation offset
98 * @tc.type: FUNC
99 */
100 HWTEST_F(ScreenRotationControllerTest, DefaultDeviceRotationOffset, Function | SmallTest | Level3)
101 {
102 ScreenRotationController::defaultDeviceRotationOffset_ = 90;
103
104 ScreenRotationController::SetDefaultDeviceRotationOffset(-90);
105 ASSERT_EQ(90, ScreenRotationController::defaultDeviceRotationOffset_);
106
107 ScreenRotationController::SetDefaultDeviceRotationOffset(-100);
108 ASSERT_EQ(90, ScreenRotationController::defaultDeviceRotationOffset_);
109
110 ScreenRotationController::SetDefaultDeviceRotationOffset(360);
111 ASSERT_EQ(90, ScreenRotationController::defaultDeviceRotationOffset_);
112
113 ScreenRotationController::SetDefaultDeviceRotationOffset(10);
114 ASSERT_EQ(90, ScreenRotationController::defaultDeviceRotationOffset_);
115
116 ScreenRotationController::SetDefaultDeviceRotationOffset(180);
117 ASSERT_EQ(180, ScreenRotationController::defaultDeviceRotationOffset_);
118 }
119
120
121 /**
122 * @tc.name: CalcTargetDisplayRotation
123 * @tc.desc: Calc target display rotation
124 * @tc.type: FUNC
125 */
126 HWTEST_F(ScreenRotationControllerTest, CalcTargetDisplayRotation, Function | SmallTest | Level3)
127 {
128 ScreenRotationController::ProcessRotationMapping();
129 ScreenRotationController::currentDisplayRotation_ = Rotation::ROTATION_0;
130
131 DeviceRotation deviceRitation = DeviceRotation::ROTATION_PORTRAIT;
132
133 Orientation orientation = Orientation::SENSOR;
134 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
135
136 orientation = Orientation::SENSOR_VERTICAL;
137 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
138
139 orientation = Orientation::SENSOR_HORIZONTAL;
140 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
141
142 orientation = Orientation::UNSPECIFIED;
143 ScreenRotationController::isScreenRotationLocked_ = true;
144 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
145 ScreenRotationController::isScreenRotationLocked_ = false;
146 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
147
148 orientation = Orientation::AUTO_ROTATION_RESTRICTED;
149 ScreenRotationController::isScreenRotationLocked_ = true;
150 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
151 ScreenRotationController::isScreenRotationLocked_ = false;
152 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
153
154 orientation = Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED;
155 ScreenRotationController::isScreenRotationLocked_ = true;
156 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
157 ScreenRotationController::isScreenRotationLocked_ = false;
158 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
159
160 orientation = Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED;
161 ScreenRotationController::isScreenRotationLocked_ = true;
162 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
163 ScreenRotationController::isScreenRotationLocked_ = false;
164 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
165
166 orientation = Orientation::VERTICAL;
167 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
168 }
169
170 /**
171 * @tc.name: ProcessAutoRotation
172 * @tc.desc: Process auto rotation
173 * @tc.type: FUNC
174 */
175 HWTEST_F(ScreenRotationControllerTest, ProcessAutoRotation, Function | SmallTest | Level3)
176 {
177 ScreenRotationController::currentDisplayRotation_ = Rotation::ROTATION_90;
178 ScreenRotationController::ProcessRotationMapping();
179
180 DeviceRotation deviceRitation = DeviceRotation::ROTATION_LANDSCAPE;
181 ASSERT_EQ(Rotation::ROTATION_90, ScreenRotationController::ProcessAutoRotationPortraitOrientation(deviceRitation));
182 deviceRitation = DeviceRotation::ROTATION_LANDSCAPE_INVERTED;
183 ASSERT_EQ(Rotation::ROTATION_90, ScreenRotationController::ProcessAutoRotationPortraitOrientation(deviceRitation));
184 deviceRitation = DeviceRotation::ROTATION_PORTRAIT_INVERTED;
185 ASSERT_EQ(Rotation::ROTATION_180, ScreenRotationController::ProcessAutoRotationPortraitOrientation(deviceRitation));
186
187 deviceRitation = DeviceRotation::ROTATION_PORTRAIT;
188 ASSERT_EQ(Rotation::ROTATION_90, ScreenRotationController::ProcessAutoRotationLandscapeOrientation(deviceRitation));
189 deviceRitation = DeviceRotation::ROTATION_PORTRAIT_INVERTED;
190 ASSERT_EQ(Rotation::ROTATION_90, ScreenRotationController::ProcessAutoRotationLandscapeOrientation(deviceRitation));
191 deviceRitation = DeviceRotation::ROTATION_LANDSCAPE;
192 ASSERT_EQ(Rotation::ROTATION_90, ScreenRotationController::ProcessAutoRotationLandscapeOrientation(deviceRitation));
193 }
194
195 /**
196 * @tc.name: CalcDeviceRotation
197 * @tc.desc: Calc device rotation
198 * @tc.type: FUNC
199 */
200 HWTEST_F(ScreenRotationControllerTest, CalcDeviceRotation, Function | SmallTest | Level3)
201 {
202 SensorRotation rotation = SensorRotation::INVALID;
203 ASSERT_EQ(DeviceRotation::INVALID, ScreenRotationController::CalcDeviceRotation(rotation));
204
205 rotation = SensorRotation::ROTATION_0;
206 ScreenRotationController::defaultDeviceRotationOffset_ = 0;
207 ScreenRotationController::defaultDeviceRotation_ = 0;
208 ASSERT_EQ(DeviceRotation::ROTATION_PORTRAIT, ScreenRotationController::CalcDeviceRotation(rotation));
209
210 rotation = SensorRotation::ROTATION_0;
211 ScreenRotationController::defaultDeviceRotationOffset_ = 90;
212 ScreenRotationController::defaultDeviceRotation_ = 0;
213 ASSERT_EQ(DeviceRotation::ROTATION_LANDSCAPE_INVERTED, ScreenRotationController::CalcDeviceRotation(rotation));
214
215 rotation = SensorRotation::ROTATION_0;
216 ScreenRotationController::defaultDeviceRotationOffset_ = 90;
217 ScreenRotationController::defaultDeviceRotation_ = 1;
218 ASSERT_EQ(DeviceRotation::ROTATION_PORTRAIT_INVERTED, ScreenRotationController::CalcDeviceRotation(rotation));
219
220 rotation = SensorRotation::ROTATION_0;
221 ScreenRotationController::defaultDeviceRotationOffset_ = 0;
222 ScreenRotationController::defaultDeviceRotation_ = 1;
223 ASSERT_EQ(DeviceRotation::ROTATION_LANDSCAPE, ScreenRotationController::CalcDeviceRotation(rotation));
224 }
225
226 /**
227 * @tc.name: IsSensorRelatedOrientation
228 * @tc.desc: Is sensor related orientation
229 * @tc.type: FUNC
230 */
231 HWTEST_F(ScreenRotationControllerTest, IsSensorRelatedOrientation, Function | SmallTest | Level3)
232 {
233 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::LOCKED));
234 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::UNSPECIFIED));
235 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::VERTICAL));
236 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::HORIZONTAL));
237 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::REVERSE_VERTICAL));
238 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::REVERSE_HORIZONTAL));
239 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(Orientation::SENSOR));
240 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(Orientation::SENSOR_VERTICAL));
241 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(Orientation::SENSOR_HORIZONTAL));
242 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(Orientation::AUTO_ROTATION_RESTRICTED));
243 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(
244 Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED));
245 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(
246 Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED));
247 }
248
249 /**
250 * @tc.name: ProcessSwitchToSensorRelatedOrientation
251 * @tc.desc: Process switch to sensor related orientation
252 * @tc.type: FUNC
253 */
254 HWTEST_F(ScreenRotationControllerTest, ProcessSwitchToSensorRelatedOrientation, Function | SmallTest | Level3)
255 {
256 Orientation orientation = Orientation::SENSOR;
257 DeviceRotation deviceRitation = DeviceRotation::ROTATION_LANDSCAPE;
258
259 ScreenRotationController::lastOrientationType_ = Orientation::SENSOR;
260 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
261
262 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
263 ScreenRotationController::isScreenRotationLocked_ = true;
264 orientation = Orientation::AUTO_ROTATION_RESTRICTED;
265 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
266
267 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
268 ScreenRotationController::isScreenRotationLocked_ = false;
269 orientation = Orientation::AUTO_ROTATION_RESTRICTED;
270 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
271
272 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
273 orientation = Orientation::SENSOR;
274 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
275
276 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
277 ScreenRotationController::isScreenRotationLocked_ = true;
278 orientation = Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED;
279 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
280
281 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
282 ScreenRotationController::isScreenRotationLocked_ = false;
283 orientation = Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED;
284 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
285
286 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
287 orientation = Orientation::SENSOR_VERTICAL;
288 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
289
290 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
291 ScreenRotationController::isScreenRotationLocked_ = true;
292 orientation = Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED;
293 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
294
295 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
296 ScreenRotationController::isScreenRotationLocked_ = false;
297 orientation = Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED;
298 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
299
300 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
301 orientation = Orientation::SENSOR_HORIZONTAL;
302 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
303 ASSERT_EQ(Orientation::SENSOR_HORIZONTAL, ScreenRotationController::lastOrientationType_);
304
305 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
306 orientation = Orientation::LOCKED;
307 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
308 ASSERT_EQ(Orientation::LOCKED, ScreenRotationController::lastOrientationType_);
309 }
310
311 /**
312 * @tc.name: ProcessSwitchToAutoRotation
313 * @tc.desc: Process switch to auto rotation
314 * @tc.type: FUNC
315 */
316 HWTEST_F(ScreenRotationControllerTest, ProcessSwitchToAutoRotation, Function | SmallTest | Level3)
317 {
318 auto defaultDisplayInfo = DisplayManagerService::GetInstance().GetDefaultDisplayInfo();
319 ASSERT_NE(nullptr, defaultDisplayInfo);
320
321 DeviceRotation deviceRotation = DeviceRotation::INVALID;
322 ScreenRotationController::ProcessSwitchToAutoRotation(deviceRotation);
323 deviceRotation = DeviceRotation::ROTATION_PORTRAIT;
324 ScreenRotationController::ProcessSwitchToAutoRotation(deviceRotation);
325 usleep(SLEEP_TIME_US);
326
327 auto displayRotationTarget = ScreenRotationController::ConvertDeviceToDisplayRotation(deviceRotation);
328 ASSERT_EQ(displayRotationTarget, defaultDisplayInfo->GetRotation());
329
330 deviceRotation = DeviceRotation::INVALID;
331 ScreenRotationController::ProcessSwitchToAutoRotationPortrait(deviceRotation);
332 deviceRotation = DeviceRotation::ROTATION_PORTRAIT;
333 ScreenRotationController::ProcessSwitchToAutoRotationPortrait(deviceRotation);
334 usleep(SLEEP_TIME_US);
335 defaultDisplayInfo = DisplayManagerService::GetInstance().GetDefaultDisplayInfo();
336 ASSERT_NE(nullptr, defaultDisplayInfo);
337 displayRotationTarget = ScreenRotationController::ConvertDeviceToDisplayRotation(deviceRotation);
338 ASSERT_EQ(displayRotationTarget, defaultDisplayInfo->GetRotation());
339
340 deviceRotation = DeviceRotation::INVALID;
341 ScreenRotationController::ProcessSwitchToAutoRotationLandscape(deviceRotation);
342 deviceRotation = DeviceRotation::ROTATION_LANDSCAPE;
343 ScreenRotationController::ProcessSwitchToAutoRotationLandscape(deviceRotation);
344
345 ScreenRotationController::ProcessSwitchToAutoRotationPortraitRestricted();
346 ScreenRotationController::ProcessSwitchToAutoRotationLandscapeRestricted();
347 }
348
349 /**
350 * @tc.name: ConvertRotation
351 * @tc.desc: Convert rotation
352 * @tc.type: FUNC
353 */
354 HWTEST_F(ScreenRotationControllerTest, ConvertRotation, Function | SmallTest | Level3)
355 {
356 ScreenRotationController::sensorToDeviceRotationMap_.clear();
357 SensorRotation rotation = SensorRotation::INVALID;
358 ASSERT_EQ(DeviceRotation::INVALID, ScreenRotationController::ConvertSensorToDeviceRotation(rotation));
359 ASSERT_EQ(DeviceRotation::INVALID, ScreenRotationController::ConvertSensorToDeviceRotation(rotation));
360
361 DeviceRotation deviceRotation = DeviceRotation::INVALID;
362 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::ConvertDeviceToDisplayRotation(deviceRotation));
363
364 ScreenRotationController::deviceToDisplayRotationMap_.clear();
365 deviceRotation = DeviceRotation::ROTATION_PORTRAIT;
366 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::ConvertDeviceToDisplayRotation(deviceRotation));
367 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::ConvertDeviceToDisplayRotation(deviceRotation));
368 }
369
370 /**
371 * @tc.name: IsDeviceRotationVerticalOrHorizontal
372 * @tc.desc: Check device rotation
373 * @tc.type: FUNC
374 */
375 HWTEST_F(ScreenRotationControllerTest, IsDeviceRotationVerticalOrHorizontal, Function | SmallTest | Level3)
376 {
377 ASSERT_EQ(true, ScreenRotationController::IsDeviceRotationVertical(DeviceRotation::ROTATION_PORTRAIT));
378 ASSERT_EQ(true, ScreenRotationController::IsDeviceRotationVertical(DeviceRotation::ROTATION_PORTRAIT_INVERTED));
379 ASSERT_EQ(false, ScreenRotationController::IsDeviceRotationVertical(DeviceRotation::ROTATION_LANDSCAPE));
380
381 ASSERT_EQ(true, ScreenRotationController::IsDeviceRotationHorizontal(DeviceRotation::ROTATION_LANDSCAPE));
382 ASSERT_EQ(true, ScreenRotationController::IsDeviceRotationHorizontal(DeviceRotation::ROTATION_LANDSCAPE_INVERTED));
383 ASSERT_EQ(false, ScreenRotationController::IsDeviceRotationHorizontal(DeviceRotation::ROTATION_PORTRAIT));
384 }
385
386 /**
387 * @tc.name: ProcessOrientationSwitch
388 * @tc.desc: Process orientation switch
389 * @tc.type: FUNC
390 */
391 HWTEST_F(ScreenRotationControllerTest, ProcessOrientationSwitch, Function | SmallTest | Level3)
392 {
393 ScreenRotationController::ProcessOrientationSwitch(Orientation::UNSPECIFIED, true);
394 ScreenRotationController::ProcessOrientationSwitch(Orientation::VERTICAL, true);
395 ScreenRotationController::ProcessOrientationSwitch(Orientation::HORIZONTAL, false);
396 ScreenRotationController::ProcessOrientationSwitch(Orientation::REVERSE_VERTICAL, true);
397 ScreenRotationController::ProcessOrientationSwitch(Orientation::SENSOR, true);
398 ScreenRotationController::ProcessOrientationSwitch(Orientation::SENSOR_VERTICAL, true);
399 ScreenRotationController::ProcessOrientationSwitch(Orientation::SENSOR_HORIZONTAL, true);
400 ScreenRotationController::ProcessOrientationSwitch(Orientation::AUTO_ROTATION_RESTRICTED, true);
401 ScreenRotationController::ProcessOrientationSwitch(Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED, true);
402 ScreenRotationController::ProcessOrientationSwitch(Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED, true);
403 ScreenRotationController::ProcessOrientationSwitch(Orientation::LOCKED, true);
404 ASSERT_EQ(Orientation::LOCKED, ScreenRotationController::lastOrientationType_);
405 }
406
407 /**
408 * @tc.name: HandleSensorEventInput
409 * @tc.desc: HandleSensorEventInput
410 * @tc.type: FUNC
411 */
412 HWTEST_F(ScreenRotationControllerTest, HandleSensorEventInput, Function | SmallTest | Level3)
413 {
414 DeviceRotation deviceRotation = DeviceRotation::INVALID;
415 ScreenRotationController::HandleSensorEventInput(deviceRotation);
416
417 deviceRotation = DeviceRotation::ROTATION_PORTRAIT;
418 ScreenRotationController::HandleSensorEventInput(deviceRotation);
419
420 ASSERT_EQ(deviceRotation, DeviceRotation::ROTATION_PORTRAIT);
421 }
422
423 /**
424 * @tc.name: IsDisplayRotationVertical
425 * @tc.desc: Check device rotation
426 * @tc.type: FUNC
427 */
428 HWTEST_F(ScreenRotationControllerTest, IsDisplayRotationVertical, Function | SmallTest | Level3)
429 {
430 ASSERT_EQ(true, ScreenRotationController::IsDisplayRotationVertical(Rotation::ROTATION_0));
431 ASSERT_EQ(false, ScreenRotationController::IsDisplayRotationVertical(Rotation::ROTATION_90));
432 ASSERT_EQ(false, ScreenRotationController::IsDisplayRotationVertical(Rotation::ROTATION_270));
433
434 ASSERT_EQ(false, ScreenRotationController::IsDisplayRotationHorizontal(Rotation::ROTATION_0));
435 ASSERT_EQ(true, ScreenRotationController::IsDisplayRotationHorizontal(Rotation::ROTATION_90));
436 ASSERT_EQ(true, ScreenRotationController::IsDisplayRotationHorizontal(Rotation::ROTATION_270));
437 }
438
439 /**
440 * @tc.name: ProcessSwitchToSensorUnrelatedOrientation
441 * @tc.desc: ProcessSwitchToSensorUnrelatedOrientation
442 * @tc.type: FUNC
443 */
444 HWTEST_F(ScreenRotationControllerTest, ProcessSwitchToSensorUnrelatedOrientation, Function | SmallTest | Level3)
445 {
446 Orientation orientation = Orientation::UNSPECIFIED;
447 ScreenRotationController::ProcessSwitchToSensorUnrelatedOrientation(orientation, false);
448 orientation = Orientation::SENSOR;
449 ScreenRotationController::ProcessSwitchToSensorUnrelatedOrientation(orientation, false);
450 ASSERT_EQ(orientation, Orientation::SENSOR);
451
452 orientation = Orientation::UNSPECIFIED;
453 ScreenRotationController::ProcessSwitchToSensorUnrelatedOrientation(orientation, false);
454 ASSERT_EQ(orientation, Orientation::UNSPECIFIED);
455
456 orientation = Orientation::VERTICAL;
457 ScreenRotationController::ProcessSwitchToSensorUnrelatedOrientation(orientation, false);
458 ASSERT_EQ(orientation, Orientation::VERTICAL);
459
460 orientation = Orientation::REVERSE_VERTICAL;
461 ScreenRotationController::ProcessSwitchToSensorUnrelatedOrientation(orientation, false);
462 ASSERT_EQ(orientation, Orientation::REVERSE_VERTICAL);
463
464 orientation = Orientation::HORIZONTAL;
465 ScreenRotationController::ProcessSwitchToSensorUnrelatedOrientation(orientation, false);
466 ASSERT_EQ(orientation, Orientation::HORIZONTAL);
467
468 orientation = Orientation::REVERSE_HORIZONTAL;
469 ScreenRotationController::ProcessSwitchToSensorUnrelatedOrientation(orientation, false);
470 ASSERT_EQ(orientation, Orientation::REVERSE_HORIZONTAL);
471
472 }
473
474 #ifdef SENSOR_ENABLE
475 /**
476 * @tc.name: GravitySensor
477 * @tc.desc: Subscribe and unsubscribe gravity sensor
478 * @tc.type: FUNC
479 */
480 HWTEST_F(ScreenRotationControllerTest, GravitySensor, Function | SmallTest | Level3)
481 {
482 GravitySensorSubscriber::isGravitySensorSubscribed_ = true;
483 GravitySensorSubscriber::SubscribeGravitySensor();
484 ASSERT_EQ(true, GravitySensorSubscriber::isGravitySensorSubscribed_);
485
486 GravitySensorSubscriber::isGravitySensorSubscribed_ = false;
487 GravitySensorSubscriber::SubscribeGravitySensor();
488
489 GravitySensorSubscriber::isGravitySensorSubscribed_ = false;
490 GravitySensorSubscriber::UnsubscribeGravitySensor();
491
492 GravitySensorSubscriber::isGravitySensorSubscribed_ = true;
493 GravitySensorSubscriber::UnsubscribeGravitySensor();
494 }
495
496 /**
497 * @tc.name: CheckCallbackTimeInterval
498 * @tc.desc: Check callbcak time interval
499 * @tc.type: FUNC
500 */
501 HWTEST_F(ScreenRotationControllerTest, CheckCallbackTimeInterval, Function | SmallTest | Level3)
502 {
503 std::chrono::milliseconds ms = std::chrono::time_point_cast<std::chrono::milliseconds>(
504 std::chrono::steady_clock::now()).time_since_epoch();
505 long currentTimeInMillitm = ms.count();
506
507 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm;
508 ASSERT_EQ(false, GravitySensorSubscriber::CheckCallbackTimeInterval());
509
510 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
511 ASSERT_EQ(true, GravitySensorSubscriber::CheckCallbackTimeInterval());
512 }
513
514 /**
515 * @tc.name: HandleGravitySensorEventCallback
516 * @tc.desc: Handel gravity sensor event callback
517 * @tc.type: FUNC
518 */
519 HWTEST_F(ScreenRotationControllerTest, HandleGravitySensorEventCallback, Function | SmallTest | Level3)
520 {
521 SensorEvent event;
522 GravityData data = {0.f, 0.f, 0.f};
523 event.sensorTypeId = SENSOR_TYPE_ID_NONE;
524 event.data = reinterpret_cast<uint8_t*>(&data);
525
526 std::chrono::milliseconds ms = std::chrono::time_point_cast<std::chrono::milliseconds>(
527 std::chrono::system_clock::now()).time_since_epoch();
528 long currentTimeInMillitm = ms.count();
529
530 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm;
531 GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
532
533 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
534 GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
535
536 event.sensorTypeId = SENSOR_TYPE_ID_GRAVITY;
537 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
538 GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
539
540 auto currentSensorRotationValue = ScreenRotationController::lastSensorRotationConverted_;
541 data.z = 1.f;
542 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
543 GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
544 ASSERT_EQ(currentSensorRotationValue, ScreenRotationController::lastSensorRotationConverted_);
545 }
546
547 /**
548 * @tc.name: CalcRotationDegree
549 * @tc.desc: Calc rotation degree
550 * @tc.type: FUNC
551 */
552 HWTEST_F(ScreenRotationControllerTest, CalcRotationDegree, Function | SmallTest | Level3)
553 {
554 GravityData data0 = {0.f, 0.f, 0.f};
555 ASSERT_EQ(270, GravitySensorSubscriber::CalcRotationDegree(&data0));
556
557 GravityData data1 = {0.f, 0.f, 1.f};
558 ASSERT_EQ(-1, GravitySensorSubscriber::CalcRotationDegree(&data1));
559
560 GravityData data2 = {1.f, 1.f, 1.f};
561 ASSERT_EQ(315, GravitySensorSubscriber::CalcRotationDegree(&data2));
562 }
563 /**
564 * @tc.name: CalcSensorRotation
565 * @tc.desc: Calc sensor rotation
566 * @tc.type: FUNC
567 */
568 HWTEST_F(ScreenRotationControllerTest, CalcSensorRotation, Function | SmallTest | Level3)
569 {
570 ASSERT_EQ(SensorRotation::INVALID, GravitySensorSubscriber::CalcSensorRotation(-30));
571 ASSERT_EQ(SensorRotation::ROTATION_0, GravitySensorSubscriber::CalcSensorRotation(15));
572 ASSERT_EQ(SensorRotation::ROTATION_0, GravitySensorSubscriber::CalcSensorRotation(345));
573 ASSERT_EQ(SensorRotation::ROTATION_90, GravitySensorSubscriber::CalcSensorRotation(75));
574 ASSERT_EQ(SensorRotation::ROTATION_180, GravitySensorSubscriber::CalcSensorRotation(180));
575 ASSERT_EQ(SensorRotation::ROTATION_270, GravitySensorSubscriber::CalcSensorRotation(270));
576 ASSERT_EQ(SensorRotation::ROTATION_0, GravitySensorSubscriber::CalcSensorRotation(600));
577 }
578 #endif
579
580 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
581 /**
582 * @tc.name: SubscribeMotionSensor
583 * @tc.desc: check function MotionSubscriber::SubscribeMotionSensor
584 * @tc.type: FUNC
585 */
586 HWTEST_F(ScreenRotationControllerTest, SubscribeMotionSensor, Function | SmallTest | Level3)
587 {
588 MotionSubscriber::isMotionSensorSubscribed_ = true;
589 MotionSubscriber::SubscribeMotionSensor();
590 ASSERT_EQ(true, MotionSubscriber::isMotionSensorSubscribed_);
591
592 MotionSubscriber::isMotionSensorSubscribed_ = false;
593 MotionSubscriber::SubscribeMotionSensor();
594 ASSERT_EQ(true, MotionSubscriber::isMotionSensorSubscribed_);
595
596 MotionSubscriber::isMotionSensorSubscribed_ = false;
597 MotionSubscriber::UnsubscribeMotionSensor();
598 ASSERT_EQ(false, MotionSubscriber::isMotionSensorSubscribed_);
599
600 MotionSubscriber::isMotionSensorSubscribed_ = true;
601 MotionSubscriber::UnsubscribeMotionSensor();
602 ASSERT_EQ(false, MotionSubscriber::isMotionSensorSubscribed_);
603 }
604
605 /**
606 * @tc.name: OnMotionChanged
607 * @tc.desc: check function RotationMotionEventCallback->SubscribeMotionSensor
608 * @tc.type: FUNC
609 */
610 HWTEST_F(ScreenRotationControllerTest, OnMotionChanged, Function | SmallTest | Level3)
611 {
612 bool needUnsubscribe = false;
613 if (MotionSubscriber::motionEventCallback_ == nullptr) {
614 needUnsubscribe = true;
615 MotionSubscriber::SubscribeMotionSensor();
616 }
617 ASSERT_NE(MotionSubscriber::motionEventCallback_, nullptr);
618 DeviceRotation currentRotation = ScreenRotationController::lastSensorRotationConverted_;
619 DeviceRotation motionRotation = DeviceRotation::INVALID;
620
621 MotionEvent motionData;
622
623 motionData.status = 0;
624 motionRotation = DeviceRotation::ROTATION_PORTRAIT;
625 MotionSubscriber::motionEventCallback_->OnMotionChanged(motionData);
626 ASSERT_EQ(motionRotation, ScreenRotationController::lastSensorRotationConverted_);
627
628 motionData.status = 1;
629 MotionSubscriber::motionEventCallback_->OnMotionChanged(motionData);
630 motionRotation = ScreenRotationController::IsDefaultDisplayRotationPortrait() ?
631 DeviceRotation::ROTATION_LANDSCAPE_INVERTED : DeviceRotation::ROTATION_LANDSCAPE;
632 ASSERT_EQ(motionRotation, ScreenRotationController::lastSensorRotationConverted_);
633
634 motionData.status = 2;
635 MotionSubscriber::motionEventCallback_->OnMotionChanged(motionData);
636 motionRotation = DeviceRotation::ROTATION_PORTRAIT_INVERTED;
637 ASSERT_EQ(motionRotation, ScreenRotationController::lastSensorRotationConverted_);
638
639 motionData.status = 3;
640 MotionSubscriber::motionEventCallback_->OnMotionChanged(motionData);
641 motionRotation = ScreenRotationController::IsDefaultDisplayRotationPortrait() ?
642 DeviceRotation::ROTATION_LANDSCAPE : DeviceRotation::ROTATION_LANDSCAPE_INVERTED;
643 ASSERT_EQ(motionRotation, ScreenRotationController::lastSensorRotationConverted_);
644
645 ScreenRotationController::HandleSensorEventInput(currentRotation);
646
647 if (needUnsubscribe) {
648 MotionSubscriber::UnsubscribeMotionSensor();
649 }
650 }
651 #endif
652 }
653 } // namespace Rosen
654 } // namespace OHOS
655