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