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: GravitySensor
83 * @tc.desc: Subscribe and unsubscribe gravity sensor
84 * @tc.type: FUNC
85 */
86 HWTEST_F(ScreenRotationControllerTest, GravitySensor, Function | SmallTest | Level3)
87 {
88 GravitySensorSubscriber::isGravitySensorSubscribed_ = true;
89 GravitySensorSubscriber::SubscribeGravitySensor();
90 ASSERT_EQ(true, GravitySensorSubscriber::isGravitySensorSubscribed_);
91
92 GravitySensorSubscriber::isGravitySensorSubscribed_ = false;
93 GravitySensorSubscriber::SubscribeGravitySensor();
94
95 GravitySensorSubscriber::isGravitySensorSubscribed_ = false;
96 GravitySensorSubscriber::UnsubscribeGravitySensor();
97
98 GravitySensorSubscriber::isGravitySensorSubscribed_ = true;
99 GravitySensorSubscriber::UnsubscribeGravitySensor();
100 }
101
102 /**
103 * @tc.name: ScreenRotationLocked
104 * @tc.desc: Set and get screen rotation locked
105 * @tc.type: FUNC
106 */
107 HWTEST_F(ScreenRotationControllerTest, ScreenRotationLocked, Function | SmallTest | Level3)
108 {
109 ScreenRotationController::SetScreenRotationLocked(false);
110 ASSERT_EQ(false, ScreenRotationController::IsScreenRotationLocked());
111
112 ScreenRotationController::SetScreenRotationLocked(true);
113 ASSERT_EQ(true, ScreenRotationController::IsScreenRotationLocked());
114 }
115
116 /**
117 * @tc.name: DefaultDeviceRotationOffset
118 * @tc.desc: Set default device rotation offset
119 * @tc.type: FUNC
120 */
121 HWTEST_F(ScreenRotationControllerTest, DefaultDeviceRotationOffset, Function | SmallTest | Level3)
122 {
123 ScreenRotationController::defaultDeviceRotationOffset_ = 90;
124
125 ScreenRotationController::SetDefaultDeviceRotationOffset(360);
126 ASSERT_EQ(90, ScreenRotationController::defaultDeviceRotationOffset_);
127
128 ScreenRotationController::SetDefaultDeviceRotationOffset(10);
129 ASSERT_EQ(90, ScreenRotationController::defaultDeviceRotationOffset_);
130
131 ScreenRotationController::SetDefaultDeviceRotationOffset(180);
132 ASSERT_EQ(180, ScreenRotationController::defaultDeviceRotationOffset_);
133 }
134
135 /**
136 * @tc.name: CheckCallbackTimeInterval
137 * @tc.desc: Check callbcak time interval
138 * @tc.type: FUNC
139 */
140 HWTEST_F(ScreenRotationControllerTest, CheckCallbackTimeInterval, Function | SmallTest | Level3)
141 {
142 std::chrono::milliseconds ms = std::chrono::time_point_cast<std::chrono::milliseconds>(
143 std::chrono::steady_clock::now()).time_since_epoch();
144 long currentTimeInMillitm = ms.count();
145
146 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm;
147 ASSERT_EQ(false, GravitySensorSubscriber::CheckCallbackTimeInterval());
148
149 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
150 ASSERT_EQ(true, GravitySensorSubscriber::CheckCallbackTimeInterval());
151 }
152
153 /**
154 * @tc.name: HandleGravitySensorEventCallback
155 * @tc.desc: Handel gravity sensor event callback
156 * @tc.type: FUNC
157 */
158 HWTEST_F(ScreenRotationControllerTest, HandleGravitySensorEventCallback, Function | SmallTest | Level3)
159 {
160 SensorEvent event;
161 GravityData data = {0.f, 0.f, 0.f};
162 event.sensorTypeId = SENSOR_TYPE_ID_NONE;
163 event.data = reinterpret_cast<uint8_t*>(&data);
164
165 std::chrono::milliseconds ms = std::chrono::time_point_cast<std::chrono::milliseconds>(
166 std::chrono::system_clock::now()).time_since_epoch();
167 long currentTimeInMillitm = ms.count();
168
169 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm;
170 GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
171
172 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
173 GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
174
175 event.sensorTypeId = SENSOR_TYPE_ID_GRAVITY;
176 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
177 GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
178
179 auto currentSensorRotationValue = ScreenRotationController::lastSensorRotationConverted_;
180 data.z = 1.f;
181 GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
182 GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
183 ASSERT_EQ(currentSensorRotationValue, ScreenRotationController::lastSensorRotationConverted_);
184 }
185
186 /**
187 * @tc.name: CalcRotationDegree
188 * @tc.desc: Calc rotation degree
189 * @tc.type: FUNC
190 */
191 HWTEST_F(ScreenRotationControllerTest, CalcRotationDegree, Function | SmallTest | Level3)
192 {
193 GravityData data0 = {0.f, 0.f, 0.f};
194 ASSERT_EQ(270, GravitySensorSubscriber::CalcRotationDegree(&data0));
195
196 GravityData data1 = {0.f, 0.f, 1.f};
197 ASSERT_EQ(-1, GravitySensorSubscriber::CalcRotationDegree(&data1));
198
199 GravityData data2 = {1.f, 1.f, 1.f};
200 ASSERT_EQ(315, GravitySensorSubscriber::CalcRotationDegree(&data2));
201 }
202
203 /**
204 * @tc.name: CalcTargetDisplayRotation
205 * @tc.desc: Calc target display rotation
206 * @tc.type: FUNC
207 */
208 HWTEST_F(ScreenRotationControllerTest, CalcTargetDisplayRotation, Function | SmallTest | Level3)
209 {
210 ScreenRotationController::ProcessRotationMapping();
211 ScreenRotationController::currentDisplayRotation_ = Rotation::ROTATION_0;
212
213 DeviceRotation deviceRitation = DeviceRotation::ROTATION_PORTRAIT;
214
215 Orientation orientation = Orientation::SENSOR;
216 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
217
218 orientation = Orientation::SENSOR_VERTICAL;
219 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
220
221 orientation = Orientation::SENSOR_HORIZONTAL;
222 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
223
224 orientation = Orientation::UNSPECIFIED;
225 ScreenRotationController::isScreenRotationLocked_ = true;
226 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
227 ScreenRotationController::isScreenRotationLocked_ = false;
228 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
229
230 orientation = Orientation::AUTO_ROTATION_RESTRICTED;
231 ScreenRotationController::isScreenRotationLocked_ = true;
232 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
233 ScreenRotationController::isScreenRotationLocked_ = false;
234 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
235
236 orientation = Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED;
237 ScreenRotationController::isScreenRotationLocked_ = true;
238 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
239 ScreenRotationController::isScreenRotationLocked_ = false;
240 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
241
242 orientation = Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED;
243 ScreenRotationController::isScreenRotationLocked_ = true;
244 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
245 ScreenRotationController::isScreenRotationLocked_ = false;
246 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
247
248 orientation = Orientation::VERTICAL;
249 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::CalcTargetDisplayRotation(orientation, deviceRitation));
250 }
251
252 /**
253 * @tc.name: ProcessAutoRotation
254 * @tc.desc: Process auto rotation
255 * @tc.type: FUNC
256 */
257 HWTEST_F(ScreenRotationControllerTest, ProcessAutoRotation, Function | SmallTest | Level3)
258 {
259 ScreenRotationController::currentDisplayRotation_ = Rotation::ROTATION_90;
260 ScreenRotationController::ProcessRotationMapping();
261
262 DeviceRotation deviceRitation = DeviceRotation::ROTATION_LANDSCAPE;
263 ASSERT_EQ(Rotation::ROTATION_90, ScreenRotationController::ProcessAutoRotationPortraitOrientation(deviceRitation));
264 deviceRitation = DeviceRotation::ROTATION_LANDSCAPE_INVERTED;
265 ASSERT_EQ(Rotation::ROTATION_90, ScreenRotationController::ProcessAutoRotationPortraitOrientation(deviceRitation));
266 deviceRitation = DeviceRotation::ROTATION_PORTRAIT_INVERTED;
267 ASSERT_EQ(Rotation::ROTATION_180, ScreenRotationController::ProcessAutoRotationPortraitOrientation(deviceRitation));
268
269 deviceRitation = DeviceRotation::ROTATION_PORTRAIT;
270 ASSERT_EQ(Rotation::ROTATION_90, ScreenRotationController::ProcessAutoRotationLandscapeOrientation(deviceRitation));
271 deviceRitation = DeviceRotation::ROTATION_PORTRAIT_INVERTED;
272 ASSERT_EQ(Rotation::ROTATION_90, ScreenRotationController::ProcessAutoRotationLandscapeOrientation(deviceRitation));
273 deviceRitation = DeviceRotation::ROTATION_LANDSCAPE;
274 ASSERT_EQ(Rotation::ROTATION_90, ScreenRotationController::ProcessAutoRotationLandscapeOrientation(deviceRitation));
275 }
276
277 /**
278 * @tc.name: CalcDeviceRotation
279 * @tc.desc: Calc device rotation
280 * @tc.type: FUNC
281 */
282 HWTEST_F(ScreenRotationControllerTest, CalcDeviceRotation, Function | SmallTest | Level3)
283 {
284 SensorRotation rotation = SensorRotation::INVALID;
285 ASSERT_EQ(DeviceRotation::INVALID, ScreenRotationController::CalcDeviceRotation(rotation));
286
287 rotation = SensorRotation::ROTATION_0;
288 ScreenRotationController::defaultDeviceRotationOffset_ = 0;
289 ScreenRotationController::defaultDeviceRotation_ = 0;
290 ASSERT_EQ(DeviceRotation::ROTATION_PORTRAIT, ScreenRotationController::CalcDeviceRotation(rotation));
291
292 rotation = SensorRotation::ROTATION_0;
293 ScreenRotationController::defaultDeviceRotationOffset_ = 90;
294 ScreenRotationController::defaultDeviceRotation_ = 0;
295 ASSERT_EQ(DeviceRotation::ROTATION_LANDSCAPE_INVERTED, ScreenRotationController::CalcDeviceRotation(rotation));
296
297 rotation = SensorRotation::ROTATION_0;
298 ScreenRotationController::defaultDeviceRotationOffset_ = 90;
299 ScreenRotationController::defaultDeviceRotation_ = 1;
300 ASSERT_EQ(DeviceRotation::ROTATION_PORTRAIT_INVERTED, ScreenRotationController::CalcDeviceRotation(rotation));
301
302 rotation = SensorRotation::ROTATION_0;
303 ScreenRotationController::defaultDeviceRotationOffset_ = 0;
304 ScreenRotationController::defaultDeviceRotation_ = 1;
305 ASSERT_EQ(DeviceRotation::ROTATION_LANDSCAPE, ScreenRotationController::CalcDeviceRotation(rotation));
306 }
307
308 /**
309 * @tc.name: IsSensorRelatedOrientation
310 * @tc.desc: Is sensor related orientation
311 * @tc.type: FUNC
312 */
313 HWTEST_F(ScreenRotationControllerTest, IsSensorRelatedOrientation, Function | SmallTest | Level3)
314 {
315 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::LOCKED));
316 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::UNSPECIFIED));
317 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::VERTICAL));
318 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::HORIZONTAL));
319 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::REVERSE_VERTICAL));
320 ASSERT_EQ(false, ScreenRotationController::IsSensorRelatedOrientation(Orientation::REVERSE_HORIZONTAL));
321 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(Orientation::SENSOR));
322 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(Orientation::SENSOR_VERTICAL));
323 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(Orientation::SENSOR_HORIZONTAL));
324 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(Orientation::AUTO_ROTATION_RESTRICTED));
325 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(
326 Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED));
327 ASSERT_EQ(true, ScreenRotationController::IsSensorRelatedOrientation(
328 Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED));
329 }
330
331 /**
332 * @tc.name: ProcessSwitchToSensorRelatedOrientation
333 * @tc.desc: Process switch to sensor related orientation
334 * @tc.type: FUNC
335 */
336 HWTEST_F(ScreenRotationControllerTest, ProcessSwitchToSensorRelatedOrientation, Function | SmallTest | Level3)
337 {
338 Orientation orientation = Orientation::SENSOR;
339 DeviceRotation deviceRitation = DeviceRotation::ROTATION_LANDSCAPE;
340
341 ScreenRotationController::lastOrientationType_ = Orientation::SENSOR;
342 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
343
344 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
345 ScreenRotationController::isScreenRotationLocked_ = true;
346 orientation = Orientation::AUTO_ROTATION_RESTRICTED;
347 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
348
349 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
350 ScreenRotationController::isScreenRotationLocked_ = false;
351 orientation = Orientation::AUTO_ROTATION_RESTRICTED;
352 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
353
354 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
355 orientation = Orientation::SENSOR;
356 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
357
358 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
359 ScreenRotationController::isScreenRotationLocked_ = true;
360 orientation = Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED;
361 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
362
363 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
364 ScreenRotationController::isScreenRotationLocked_ = false;
365 orientation = Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED;
366 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
367
368 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
369 orientation = Orientation::SENSOR_VERTICAL;
370 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
371
372 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
373 ScreenRotationController::isScreenRotationLocked_ = true;
374 orientation = Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED;
375 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
376
377 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
378 ScreenRotationController::isScreenRotationLocked_ = false;
379 orientation = Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED;
380 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
381
382 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
383 orientation = Orientation::SENSOR_HORIZONTAL;
384 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
385 ASSERT_EQ(Orientation::SENSOR_HORIZONTAL, ScreenRotationController::lastOrientationType_);
386
387 ScreenRotationController::lastOrientationType_ = Orientation::UNSPECIFIED;
388 orientation = Orientation::LOCKED;
389 ScreenRotationController::ProcessSwitchToSensorRelatedOrientation(orientation, deviceRitation);
390 ASSERT_EQ(Orientation::LOCKED, ScreenRotationController::lastOrientationType_);
391 }
392
393 /**
394 * @tc.name: ProcessSwitchToAutoRotation
395 * @tc.desc: Process switch to auto rotation
396 * @tc.type: FUNC
397 */
398 HWTEST_F(ScreenRotationControllerTest, ProcessSwitchToAutoRotation, Function | SmallTest | Level3)
399 {
400 auto defaultDisplayInfo = DisplayManagerService::GetInstance().GetDefaultDisplayInfo();
401 ASSERT_NE(nullptr, defaultDisplayInfo);
402
403 DeviceRotation deviceRotation = DeviceRotation::INVALID;
404 ScreenRotationController::ProcessSwitchToAutoRotation(deviceRotation);
405 deviceRotation = DeviceRotation::ROTATION_PORTRAIT;
406 ScreenRotationController::ProcessSwitchToAutoRotation(deviceRotation);
407 usleep(SLEEP_TIME_US);
408
409 auto displayRotationTarget = ScreenRotationController::ConvertDeviceToDisplayRotation(deviceRotation);
410 ASSERT_EQ(displayRotationTarget, defaultDisplayInfo->GetRotation());
411
412 deviceRotation = DeviceRotation::INVALID;
413 ScreenRotationController::ProcessSwitchToAutoRotationPortrait(deviceRotation);
414 deviceRotation = DeviceRotation::ROTATION_PORTRAIT;
415 ScreenRotationController::ProcessSwitchToAutoRotationPortrait(deviceRotation);
416 usleep(SLEEP_TIME_US);
417 defaultDisplayInfo = DisplayManagerService::GetInstance().GetDefaultDisplayInfo();
418 ASSERT_NE(nullptr, defaultDisplayInfo);
419 displayRotationTarget = ScreenRotationController::ConvertDeviceToDisplayRotation(deviceRotation);
420 ASSERT_EQ(displayRotationTarget, defaultDisplayInfo->GetRotation());
421
422 deviceRotation = DeviceRotation::INVALID;
423 ScreenRotationController::ProcessSwitchToAutoRotationLandscape(deviceRotation);
424 deviceRotation = DeviceRotation::ROTATION_LANDSCAPE;
425 ScreenRotationController::ProcessSwitchToAutoRotationLandscape(deviceRotation);
426
427 ScreenRotationController::ProcessSwitchToAutoRotationPortraitRestricted();
428 ScreenRotationController::ProcessSwitchToAutoRotationLandscapeRestricted();
429 }
430
431 /**
432 * @tc.name: CalcSensorRotation
433 * @tc.desc: Calc sensor rotation
434 * @tc.type: FUNC
435 */
436 HWTEST_F(ScreenRotationControllerTest, CalcSensorRotation, Function | SmallTest | Level3)
437 {
438 ASSERT_EQ(SensorRotation::INVALID, GravitySensorSubscriber::CalcSensorRotation(-30));
439 ASSERT_EQ(SensorRotation::ROTATION_0, GravitySensorSubscriber::CalcSensorRotation(15));
440 ASSERT_EQ(SensorRotation::ROTATION_0, GravitySensorSubscriber::CalcSensorRotation(345));
441 ASSERT_EQ(SensorRotation::ROTATION_90, GravitySensorSubscriber::CalcSensorRotation(75));
442 ASSERT_EQ(SensorRotation::ROTATION_180, GravitySensorSubscriber::CalcSensorRotation(180));
443 ASSERT_EQ(SensorRotation::ROTATION_270, GravitySensorSubscriber::CalcSensorRotation(270));
444 ASSERT_EQ(SensorRotation::ROTATION_0, GravitySensorSubscriber::CalcSensorRotation(600));
445 }
446
447 /**
448 * @tc.name: ConvertRotation
449 * @tc.desc: Convert rotation
450 * @tc.type: FUNC
451 */
452 HWTEST_F(ScreenRotationControllerTest, ConvertRotation, Function | SmallTest | Level3)
453 {
454 ScreenRotationController::sensorToDeviceRotationMap_.clear();
455 SensorRotation rotation = SensorRotation::INVALID;
456 ASSERT_EQ(DeviceRotation::INVALID, ScreenRotationController::ConvertSensorToDeviceRotation(rotation));
457 ASSERT_EQ(DeviceRotation::INVALID, ScreenRotationController::ConvertSensorToDeviceRotation(rotation));
458
459 DeviceRotation deviceRotation = DeviceRotation::INVALID;
460 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::ConvertDeviceToDisplayRotation(deviceRotation));
461
462 ScreenRotationController::deviceToDisplayRotationMap_.clear();
463 deviceRotation = DeviceRotation::ROTATION_PORTRAIT;
464 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::ConvertDeviceToDisplayRotation(deviceRotation));
465 ASSERT_EQ(Rotation::ROTATION_0, ScreenRotationController::ConvertDeviceToDisplayRotation(deviceRotation));
466 }
467
468 /**
469 * @tc.name: IsDeviceRotationVerticalOrHorizontal
470 * @tc.desc: Check device rotation
471 * @tc.type: FUNC
472 */
473 HWTEST_F(ScreenRotationControllerTest, IsDeviceRotationVerticalOrHorizontal, Function | SmallTest | Level3)
474 {
475 ASSERT_EQ(true, ScreenRotationController::IsDeviceRotationVertical(DeviceRotation::ROTATION_PORTRAIT));
476 ASSERT_EQ(true, ScreenRotationController::IsDeviceRotationVertical(DeviceRotation::ROTATION_PORTRAIT_INVERTED));
477 ASSERT_EQ(false, ScreenRotationController::IsDeviceRotationVertical(DeviceRotation::ROTATION_LANDSCAPE));
478
479 ASSERT_EQ(true, ScreenRotationController::IsDeviceRotationHorizontal(DeviceRotation::ROTATION_LANDSCAPE));
480 ASSERT_EQ(true, ScreenRotationController::IsDeviceRotationHorizontal(DeviceRotation::ROTATION_LANDSCAPE_INVERTED));
481 ASSERT_EQ(false, ScreenRotationController::IsDeviceRotationHorizontal(DeviceRotation::ROTATION_PORTRAIT));
482 }
483
484 /**
485 * @tc.name: ProcessOrientationSwitch
486 * @tc.desc: Process orientation switch
487 * @tc.type: FUNC
488 */
489 HWTEST_F(ScreenRotationControllerTest, ProcessOrientationSwitch, Function | SmallTest | Level3)
490 {
491 ScreenRotationController::ProcessOrientationSwitch(Orientation::UNSPECIFIED, true);
492 ScreenRotationController::ProcessOrientationSwitch(Orientation::VERTICAL, true);
493 ScreenRotationController::ProcessOrientationSwitch(Orientation::HORIZONTAL, false);
494 ScreenRotationController::ProcessOrientationSwitch(Orientation::REVERSE_VERTICAL, true);
495 ScreenRotationController::ProcessOrientationSwitch(Orientation::SENSOR, true);
496 ScreenRotationController::ProcessOrientationSwitch(Orientation::SENSOR_VERTICAL, true);
497 ScreenRotationController::ProcessOrientationSwitch(Orientation::SENSOR_HORIZONTAL, true);
498 ScreenRotationController::ProcessOrientationSwitch(Orientation::AUTO_ROTATION_RESTRICTED, true);
499 ScreenRotationController::ProcessOrientationSwitch(Orientation::AUTO_ROTATION_PORTRAIT_RESTRICTED, true);
500 ScreenRotationController::ProcessOrientationSwitch(Orientation::AUTO_ROTATION_LANDSCAPE_RESTRICTED, true);
501 ScreenRotationController::ProcessOrientationSwitch(Orientation::LOCKED, true);
502 ASSERT_EQ(Orientation::LOCKED, ScreenRotationController::lastOrientationType_);
503 }
504 #ifdef WM_SUBSCRIBE_MOTION_ENABLE
505 /**
506 * @tc.name: SubscribeMotionSensor
507 * @tc.desc: check function MotionSubscriber::SubscribeMotionSensor
508 * @tc.type: FUNC
509 */
510 HWTEST_F(ScreenRotationControllerTest, SubscribeMotionSensor, Function | SmallTest | Level3)
511 {
512 MotionSubscriber::isMotionSensorSubscribed_ = true;
513 MotionSubscriber::SubscribeMotionSensor();
514 ASSERT_EQ(true, MotionSubscriber::isMotionSensorSubscribed_);
515
516 MotionSubscriber::isMotionSensorSubscribed_ = false;
517 MotionSubscriber::SubscribeMotionSensor();
518 ASSERT_EQ(true, MotionSubscriber::isMotionSensorSubscribed_);
519
520 MotionSubscriber::isMotionSensorSubscribed_ = false;
521 MotionSubscriber::UnsubscribeMotionSensor();
522 ASSERT_EQ(false, MotionSubscriber::isMotionSensorSubscribed_);
523
524 MotionSubscriber::isMotionSensorSubscribed_ = true;
525 MotionSubscriber::UnsubscribeMotionSensor();
526 ASSERT_EQ(false, MotionSubscriber::isMotionSensorSubscribed_);
527 }
528
529 /**
530 * @tc.name: OnMotionChanged
531 * @tc.desc: check function RotationMotionEventCallback->SubscribeMotionSensor
532 * @tc.type: FUNC
533 */
534 HWTEST_F(ScreenRotationControllerTest, OnMotionChanged, Function | SmallTest | Level3)
535 {
536 bool needUnsubscribe = false;
537 if (MotionSubscriber::motionEventCallback_ == nullptr) {
538 needUnsubscribe = true;
539 MotionSubscriber::SubscribeMotionSensor();
540 }
541 ASSERT_NE(MotionSubscriber::motionEventCallback_, nullptr);
542 DeviceRotation currentRotation = ScreenRotationController::lastSensorRotationConverted_;
543 DeviceRotation motionRotation = DeviceRotation::INVALID;
544
545 MotionEvent motionData;
546
547 motionData.status = 0;
548 motionRotation = DeviceRotation::ROTATION_PORTRAIT;
549 MotionSubscriber::motionEventCallback_->OnMotionChanged(motionData);
550 ASSERT_EQ(motionRotation, ScreenRotationController::lastSensorRotationConverted_);
551
552 motionData.status = 1;
553 MotionSubscriber::motionEventCallback_->OnMotionChanged(motionData);
554 motionRotation = ScreenRotationController::IsDefaultDisplayRotationPortrait() ?
555 DeviceRotation::ROTATION_LANDSCAPE_INVERTED : DeviceRotation::ROTATION_LANDSCAPE;
556 ASSERT_EQ(motionRotation, ScreenRotationController::lastSensorRotationConverted_);
557
558 motionData.status = 2;
559 MotionSubscriber::motionEventCallback_->OnMotionChanged(motionData);
560 motionRotation = DeviceRotation::ROTATION_PORTRAIT_INVERTED;
561 ASSERT_EQ(motionRotation, ScreenRotationController::lastSensorRotationConverted_);
562
563 motionData.status = 3;
564 MotionSubscriber::motionEventCallback_->OnMotionChanged(motionData);
565 motionRotation = ScreenRotationController::IsDefaultDisplayRotationPortrait() ?
566 DeviceRotation::ROTATION_LANDSCAPE : DeviceRotation::ROTATION_LANDSCAPE_INVERTED;
567 ASSERT_EQ(motionRotation, ScreenRotationController::lastSensorRotationConverted_);
568
569 ScreenRotationController::HandleSensorEventInput(currentRotation);
570
571 if (needUnsubscribe) {
572 MotionSubscriber::UnsubscribeMotionSensor();
573 }
574 }
575 #endif
576 }
577 } // namespace Rosen
578 } // namespace OHOS
579