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