1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.statusbar.policy;
18 
19 import static org.mockito.ArgumentMatchers.anyBoolean;
20 import static org.mockito.ArgumentMatchers.anyInt;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.verifyZeroInteractions;
23 
24 import android.testing.AndroidTestingRunner;
25 import android.testing.TestableLooper;
26 
27 import androidx.test.filters.SmallTest;
28 
29 import com.android.internal.view.RotationPolicy;
30 import com.android.systemui.SysuiTestCase;
31 import com.android.systemui.util.wrapper.RotationPolicyWrapper;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 
40 @RunWith(AndroidTestingRunner.class)
41 @TestableLooper.RunWithLooper
42 @SmallTest
43 public class RotationLockControllerImplTest extends SysuiTestCase {
44 
45     private static final String[] DEFAULT_SETTINGS = new String[] {"0:0", "1:2"};
46 
47     @Mock RotationPolicyWrapper mRotationPolicyWrapper;
48     @Mock DeviceStateRotationLockSettingController mDeviceStateRotationLockSettingController;
49 
50     private ArgumentCaptor<RotationPolicy.RotationPolicyListener> mRotationPolicyListenerCaptor;
51 
52     @Before
setUp()53     public void setUp() {
54         MockitoAnnotations.initMocks(/* testClass= */ this);
55 
56         mRotationPolicyListenerCaptor =
57                 ArgumentCaptor.forClass(RotationPolicy.RotationPolicyListener.class);
58     }
59 
60     @Test
whenFlagOff_doesntInteractWithDeviceStateRotationController()61     public void whenFlagOff_doesntInteractWithDeviceStateRotationController() {
62         createRotationLockController(new String[0]);
63 
64         verifyZeroInteractions(mDeviceStateRotationLockSettingController);
65     }
66 
67     @Test
whenFlagOn_setListeningSetsListeningOnDeviceStateRotationController()68     public void whenFlagOn_setListeningSetsListeningOnDeviceStateRotationController() {
69         createRotationLockController();
70 
71         verify(mDeviceStateRotationLockSettingController).setListening(/* listening= */ true);
72     }
73 
74     @Test
whenFlagOn_deviceStateRotationControllerAddedToCallbacks()75     public void whenFlagOn_deviceStateRotationControllerAddedToCallbacks() {
76         createRotationLockController();
77         captureRotationPolicyListener().onChange();
78 
79         verify(mDeviceStateRotationLockSettingController)
80                 .onRotationLockStateChanged(anyBoolean(), anyBoolean());
81     }
82 
captureRotationPolicyListener()83     private RotationPolicy.RotationPolicyListener captureRotationPolicyListener() {
84         verify(mRotationPolicyWrapper)
85                 .registerRotationPolicyListener(mRotationPolicyListenerCaptor.capture(), anyInt());
86         return mRotationPolicyListenerCaptor.getValue();
87     }
88 
createRotationLockController()89     private void createRotationLockController() {
90         createRotationLockController(DEFAULT_SETTINGS);
91     }
92 
createRotationLockController(String[] deviceStateRotationLockDefaults)93     private void createRotationLockController(String[] deviceStateRotationLockDefaults) {
94         new RotationLockControllerImpl(
95                 mRotationPolicyWrapper,
96                 mDeviceStateRotationLockSettingController,
97                 deviceStateRotationLockDefaults);
98     }
99 }
100