• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @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     orientation = ScreenRotationController::lastOrientationType_;
473     ScreenRotationController::ProcessSwitchToSensorUnrelatedOrientation(orientation, false);
474     ASSERT_EQ(orientation, Orientation::REVERSE_HORIZONTAL);
475 }
476 
477 /**
478  * @tc.name: GetPreferredOrientation
479  * @tc.desc: GetPreferredOrientation
480  * @tc.type: FUNC
481  */
482 HWTEST_F(ScreenRotationControllerTest, GetPreferredOrientation, Function | SmallTest | Level3)
483 {
484     ScreenRotationController::defaultDisplayId_ = 1003;
485     auto ret = ScreenRotationController::GetPreferredOrientation();
486     ASSERT_EQ(ret, Orientation::UNSPECIFIED);
487     ScreenRotationController::defaultDisplayId_ = DisplayManagerServiceInner::GetInstance().GetDefaultDisplayId();
488 }
489 
490 /**
491  * @tc.name: ProcessSwitchToAutoRotationLandscapeRestricted
492  * @tc.desc: ProcessSwitchToAutoRotationLandscapeRestricted
493  * @tc.type: FUNC
494  */
495 HWTEST_F(ScreenRotationControllerTest, ProcessSwitchToAutoRotationLandscapeRestricted, Function | SmallTest | Level3)
496 {
497     ScreenRotationController::rotationLockedRotation_ =
498         ScreenRotationController::ConvertDeviceToDisplayRotation(DeviceRotation::ROTATION_LANDSCAPE);
499     ScreenRotationController::ProcessSwitchToAutoRotationLandscapeRestricted();
500     ASSERT_TRUE(ScreenRotationController::IsDisplayRotationHorizontal(
501     ScreenRotationController::rotationLockedRotation_));
502 }
503 
504 /**
505  * @tc.name: ConvertRotationToDisplayOrientation
506  * @tc.desc: ConvertRotationToDisplayOrientation
507  * @tc.type: FUNC
508  */
509 HWTEST_F(ScreenRotationControllerTest, ConvertRotationToDisplayOrientation, Function | SmallTest | Level3)
510 {
511     ASSERT_FALSE(ScreenRotationController::displayToDisplayOrientationMap_.empty());
512     Rotation rotation = Rotation::ROTATION_0;
513     ScreenRotationController::ConvertRotationToDisplayOrientation(rotation);
514     ScreenRotationController::displayToDisplayOrientationMap_.clear();
515     ASSERT_TRUE(ScreenRotationController::displayToDisplayOrientationMap_.empty());
516     ScreenRotationController::ConvertRotationToDisplayOrientation(rotation);
517 }
518 
519 #ifdef SENSOR_ENABLE
520 /**
521  * @tc.name: GravitySensor
522  * @tc.desc: Subscribe and unsubscribe gravity sensor
523  * @tc.type: FUNC
524  */
525 HWTEST_F(ScreenRotationControllerTest, GravitySensor, Function | SmallTest | Level3)
526 {
527     GravitySensorSubscriber::isGravitySensorSubscribed_ = true;
528     GravitySensorSubscriber::SubscribeGravitySensor();
529     ASSERT_EQ(true, GravitySensorSubscriber::isGravitySensorSubscribed_);
530 
531     GravitySensorSubscriber::isGravitySensorSubscribed_ = false;
532     GravitySensorSubscriber::SubscribeGravitySensor();
533 
534     GravitySensorSubscriber::isGravitySensorSubscribed_ = false;
535     GravitySensorSubscriber::UnsubscribeGravitySensor();
536 
537     GravitySensorSubscriber::isGravitySensorSubscribed_ = true;
538     GravitySensorSubscriber::UnsubscribeGravitySensor();
539 }
540 
541 /**
542  * @tc.name: CheckCallbackTimeInterval
543  * @tc.desc: Check callbcak time interval
544  * @tc.type: FUNC
545  */
546 HWTEST_F(ScreenRotationControllerTest, CheckCallbackTimeInterval, Function | SmallTest | Level3)
547 {
548     std::chrono::milliseconds ms = std::chrono::time_point_cast<std::chrono::milliseconds>(
549         std::chrono::steady_clock::now()).time_since_epoch();
550     long currentTimeInMillitm = ms.count();
551 
552     GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm;
553     ASSERT_EQ(false, GravitySensorSubscriber::CheckCallbackTimeInterval());
554 
555     GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
556     ASSERT_EQ(true, GravitySensorSubscriber::CheckCallbackTimeInterval());
557 }
558 
559 /**
560  * @tc.name: HandleGravitySensorEventCallback
561  * @tc.desc: Handel gravity sensor event callback
562  * @tc.type: FUNC
563  */
564 HWTEST_F(ScreenRotationControllerTest, HandleGravitySensorEventCallback, Function | SmallTest | Level3)
565 {
566     SensorEvent event;
567     GravityData data = {0.f, 0.f, 0.f};
568     event.sensorTypeId = SENSOR_TYPE_ID_NONE;
569     event.data = reinterpret_cast<uint8_t*>(&data);
570 
571     std::chrono::milliseconds ms = std::chrono::time_point_cast<std::chrono::milliseconds>(
572         std::chrono::system_clock::now()).time_since_epoch();
573     long currentTimeInMillitm = ms.count();
574 
575     GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm;
576     GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
577 
578     GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
579     GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
580 
581     event.sensorTypeId = SENSOR_TYPE_ID_GRAVITY;
582     GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
583     GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
584 
585     auto currentSensorRotationValue = ScreenRotationController::lastSensorRotationConverted_;
586     data.z = 1.f;
587     GravitySensorSubscriber::lastCallbackTime_ = currentTimeInMillitm - 200;
588     GravitySensorSubscriber::HandleGravitySensorEventCallback(&event);
589     ASSERT_EQ(currentSensorRotationValue, ScreenRotationController::lastSensorRotationConverted_);
590 }
591 
592 /**
593  * @tc.name: CalcRotationDegree
594  * @tc.desc: Calc rotation degree
595  * @tc.type: FUNC
596  */
597 HWTEST_F(ScreenRotationControllerTest, CalcRotationDegree, Function | SmallTest | Level3)
598 {
599     GravityData data0 = {0.f, 0.f, 0.f};
600     ASSERT_EQ(270, GravitySensorSubscriber::CalcRotationDegree(&data0));
601 
602     GravityData data1 = {0.f, 0.f, 1.f};
603     ASSERT_EQ(-1, GravitySensorSubscriber::CalcRotationDegree(&data1));
604 
605     GravityData data2 = {1.f, 1.f, 1.f};
606     ASSERT_EQ(315, GravitySensorSubscriber::CalcRotationDegree(&data2));
607 }
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(false, 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(true, MotionSubscriber::isMotionSensorSubscribed_);
649 }
650 #endif
651 }
652 } // namespace Rosen
653 } // namespace OHOS
654