1 /*
2  * Copyright (C) 2022 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.server.wm;
18 
19 import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;
20 
21 import static com.android.server.wm.LetterboxConfiguration.LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_CENTER;
22 import static com.android.server.wm.LetterboxConfiguration.LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT;
23 import static com.android.server.wm.LetterboxConfiguration.LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_RIGHT;
24 import static com.android.server.wm.LetterboxConfiguration.LETTERBOX_VERTICAL_REACHABILITY_POSITION_BOTTOM;
25 import static com.android.server.wm.LetterboxConfiguration.LETTERBOX_VERTICAL_REACHABILITY_POSITION_CENTER;
26 import static com.android.server.wm.LetterboxConfiguration.LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP;
27 
28 import static org.mockito.ArgumentMatchers.anyInt;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34 
35 import android.content.Context;
36 import android.platform.test.annotations.Presubmit;
37 
38 import androidx.test.filters.SmallTest;
39 
40 import org.junit.Before;
41 import org.junit.Test;
42 
43 import java.util.Arrays;
44 import java.util.function.BiConsumer;
45 
46 /**
47  * Tests for the {@link LetterboxConfiguration} class.
48  *
49  * Build/Install/Run:
50  *  atest WmTests:LetterboxConfigurationTest
51  */
52 @SmallTest
53 @Presubmit
54 public class LetterboxConfigurationTest {
55 
56     private Context mContext;
57     private LetterboxConfiguration mLetterboxConfiguration;
58     private LetterboxConfigurationPersister mLetterboxConfigurationPersister;
59 
60     @Before
setUp()61     public void setUp() throws Exception {
62         mContext = getInstrumentation().getTargetContext();
63         mLetterboxConfigurationPersister = mock(LetterboxConfigurationPersister.class);
64         mLetterboxConfiguration = new LetterboxConfiguration(mContext,
65                 mLetterboxConfigurationPersister);
66     }
67 
68     @Test
test_whenReadingValues_storeIsInvoked()69     public void test_whenReadingValues_storeIsInvoked() {
70         for (boolean halfFoldPose : Arrays.asList(false, true)) {
71             mLetterboxConfiguration.getLetterboxPositionForHorizontalReachability(halfFoldPose);
72             verify(mLetterboxConfigurationPersister).getLetterboxPositionForHorizontalReachability(
73                     halfFoldPose);
74             mLetterboxConfiguration.getLetterboxPositionForVerticalReachability(halfFoldPose);
75             verify(mLetterboxConfigurationPersister).getLetterboxPositionForVerticalReachability(
76                     halfFoldPose);
77         }
78     }
79 
80     @Test
test_whenSettingValues_updateConfigurationIsInvoked()81     public void test_whenSettingValues_updateConfigurationIsInvoked() {
82         for (boolean halfFoldPose : Arrays.asList(false, true)) {
83             mLetterboxConfiguration.movePositionForHorizontalReachabilityToNextRightStop(
84                     halfFoldPose);
85             verify(mLetterboxConfigurationPersister).setLetterboxPositionForHorizontalReachability(
86                     eq(halfFoldPose), anyInt());
87             mLetterboxConfiguration.movePositionForVerticalReachabilityToNextBottomStop(
88                     halfFoldPose);
89             verify(mLetterboxConfigurationPersister).setLetterboxPositionForVerticalReachability(
90                     eq(halfFoldPose), anyInt());
91         }
92     }
93 
94     @Test
test_whenMovedHorizontally_updatePositionAccordingly()95     public void test_whenMovedHorizontally_updatePositionAccordingly() {
96         // Starting from center
97         assertForHorizontalMove(
98                 /* from */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_CENTER,
99                 /* expected */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT,
100                 /* expectedTime */ 1,
101                 /* halfFoldPose */ false,
102                 LetterboxConfiguration::movePositionForHorizontalReachabilityToNextLeftStop);
103         assertForHorizontalMove(
104                 /* from */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_CENTER,
105                 /* expected */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_RIGHT,
106                 /* expectedTime */ 1,
107                 /* halfFoldPose */ false,
108                 LetterboxConfiguration::movePositionForHorizontalReachabilityToNextRightStop);
109         // Starting from left
110         assertForHorizontalMove(
111                 /* from */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT,
112                 /* expected */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT,
113                 /* expectedTime */ 2,
114                 /* halfFoldPose */ false,
115                 LetterboxConfiguration::movePositionForHorizontalReachabilityToNextLeftStop);
116         assertForHorizontalMove(
117                 /* from */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT,
118                 /* expected */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_CENTER,
119                 /* expectedTime */ 1,
120                 /* halfFoldPose */ false,
121                 LetterboxConfiguration::movePositionForHorizontalReachabilityToNextRightStop);
122         // Starting from right
123         assertForHorizontalMove(
124                 /* from */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_RIGHT,
125                 /* expected */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_RIGHT,
126                 /* expectedTime */ 2,
127                 /* halfFoldPose */ false,
128                 LetterboxConfiguration::movePositionForHorizontalReachabilityToNextRightStop);
129         assertForHorizontalMove(
130                 /* from */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_RIGHT,
131                 /* expected */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_CENTER,
132                 /* expectedTime */ 2,
133                 /* halfFoldPose */ false,
134                 LetterboxConfiguration::movePositionForHorizontalReachabilityToNextLeftStop);
135         // Starting from left - book mode
136         assertForHorizontalMove(
137                 /* from */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT,
138                 /* expected */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT,
139                 /* expectedTime */ 1,
140                 /* halfFoldPose */ true,
141                 LetterboxConfiguration::movePositionForHorizontalReachabilityToNextLeftStop);
142         assertForHorizontalMove(
143                 /* from */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT,
144                 /* expected */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_RIGHT,
145                 /* expectedTime */ 1,
146                 /* halfFoldPose */ true,
147                 LetterboxConfiguration::movePositionForHorizontalReachabilityToNextRightStop);
148         // Starting from right - book mode
149         assertForHorizontalMove(
150                 /* from */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_RIGHT,
151                 /* expected */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_RIGHT,
152                 /* expectedTime */ 2,
153                 /* halfFoldPose */ true,
154                 LetterboxConfiguration::movePositionForHorizontalReachabilityToNextRightStop);
155         assertForHorizontalMove(
156                 /* from */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_RIGHT,
157                 /* expected */ LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT,
158                 /* expectedTime */ 2,
159                 /* halfFoldPose */ true,
160                 LetterboxConfiguration::movePositionForHorizontalReachabilityToNextLeftStop);
161     }
162 
163     @Test
test_whenMovedVertically_updatePositionAccordingly()164     public void test_whenMovedVertically_updatePositionAccordingly() {
165         // Starting from center
166         assertForVerticalMove(
167                 /* from */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_CENTER,
168                 /* expected */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_BOTTOM,
169                 /* expectedTime */ 1,
170                 /* halfFoldPose */ false,
171                 LetterboxConfiguration::movePositionForVerticalReachabilityToNextBottomStop);
172         assertForVerticalMove(
173                 /* from */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_CENTER,
174                 /* expected */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP,
175                 /* expectedTime */ 1,
176                 /* halfFoldPose */ false,
177                 LetterboxConfiguration::movePositionForVerticalReachabilityToNextTopStop);
178         // Starting from top
179         assertForVerticalMove(
180                 /* from */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP,
181                 /* expected */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_CENTER,
182                 /* expectedTime */ 1,
183                 /* halfFoldPose */ false,
184                 LetterboxConfiguration::movePositionForVerticalReachabilityToNextBottomStop);
185         assertForVerticalMove(
186                 /* from */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP,
187                 /* expected */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP,
188                 /* expectedTime */ 2,
189                 /* halfFoldPose */ false,
190                 LetterboxConfiguration::movePositionForVerticalReachabilityToNextTopStop);
191         // Starting from bottom
192         assertForVerticalMove(
193                 /* from */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_BOTTOM,
194                 /* expected */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_CENTER,
195                 /* expectedTime */ 2,
196                 /* halfFoldPose */ false,
197                 LetterboxConfiguration::movePositionForVerticalReachabilityToNextTopStop);
198         assertForVerticalMove(
199                 /* from */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_BOTTOM,
200                 /* expected */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_BOTTOM,
201                 /* expectedTime */ 2,
202                 /* halfFoldPose */ false,
203                 LetterboxConfiguration::movePositionForVerticalReachabilityToNextBottomStop);
204         // Starting from top - tabletop mode
205         assertForVerticalMove(
206                 /* from */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP,
207                 /* expected */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_BOTTOM,
208                 /* expectedTime */ 1,
209                 /* halfFoldPose */ true,
210                 LetterboxConfiguration::movePositionForVerticalReachabilityToNextBottomStop);
211         assertForVerticalMove(
212                 /* from */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP,
213                 /* expected */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP,
214                 /* expectedTime */ 1,
215                 /* halfFoldPose */ true,
216                 LetterboxConfiguration::movePositionForVerticalReachabilityToNextTopStop);
217         // Starting from bottom - tabletop mode
218         assertForVerticalMove(
219                 /* from */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_BOTTOM,
220                 /* expected */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP,
221                 /* expectedTime */ 2,
222                 /* halfFoldPose */ true,
223                 LetterboxConfiguration::movePositionForVerticalReachabilityToNextTopStop);
224         assertForVerticalMove(
225                 /* from */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_BOTTOM,
226                 /* expected */ LETTERBOX_VERTICAL_REACHABILITY_POSITION_BOTTOM,
227                 /* expectedTime */ 2,
228                 /* halfFoldPose */ true,
229                 LetterboxConfiguration::movePositionForVerticalReachabilityToNextBottomStop);
230     }
231 
assertForHorizontalMove(int from, int expected, int expectedTime, boolean halfFoldPose, BiConsumer<LetterboxConfiguration, Boolean> move)232     private void assertForHorizontalMove(int from, int expected, int expectedTime,
233             boolean halfFoldPose, BiConsumer<LetterboxConfiguration, Boolean> move) {
234         // We are in the current position
235         when(mLetterboxConfiguration.getLetterboxPositionForHorizontalReachability(halfFoldPose))
236                 .thenReturn(from);
237         move.accept(mLetterboxConfiguration, halfFoldPose);
238         verify(mLetterboxConfigurationPersister,
239                 times(expectedTime)).setLetterboxPositionForHorizontalReachability(halfFoldPose,
240                 expected);
241     }
242 
assertForVerticalMove(int from, int expected, int expectedTime, boolean halfFoldPose, BiConsumer<LetterboxConfiguration, Boolean> move)243     private void assertForVerticalMove(int from, int expected, int expectedTime,
244             boolean halfFoldPose, BiConsumer<LetterboxConfiguration, Boolean> move) {
245         // We are in the current position
246         when(mLetterboxConfiguration.getLetterboxPositionForVerticalReachability(halfFoldPose))
247                 .thenReturn(from);
248         move.accept(mLetterboxConfiguration, halfFoldPose);
249         verify(mLetterboxConfigurationPersister,
250                 times(expectedTime)).setLetterboxPositionForVerticalReachability(halfFoldPose,
251                 expected);
252     }
253 
254     @Test
test_letterboxPositionWhenReachabilityEnabledIsReset()255     public void test_letterboxPositionWhenReachabilityEnabledIsReset() {
256         // Check that horizontal reachability is set with correct arguments
257         mLetterboxConfiguration.resetPersistentLetterboxPositionForHorizontalReachability();
258         verify(mLetterboxConfigurationPersister).setLetterboxPositionForHorizontalReachability(
259                 false /* forBookMode */,
260                 LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_CENTER);
261         verify(mLetterboxConfigurationPersister).setLetterboxPositionForHorizontalReachability(
262                 true /* forBookMode */,
263                 LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT);
264 
265         // Check that vertical reachability is set with correct arguments
266         mLetterboxConfiguration.resetPersistentLetterboxPositionForVerticalReachability();
267         verify(mLetterboxConfigurationPersister).setLetterboxPositionForVerticalReachability(
268                 false /* forTabletopMode */,
269                 LETTERBOX_VERTICAL_REACHABILITY_POSITION_CENTER);
270         verify(mLetterboxConfigurationPersister).setLetterboxPositionForVerticalReachability(
271                 true /* forTabletopMode */,
272                 LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP);
273     }
274 
275     @Test
test_lettterboxPositionWhenReachabilityEnabledIsSet()276     public void test_lettterboxPositionWhenReachabilityEnabledIsSet() {
277         // Check that horizontal reachability is set with correct arguments
278         mLetterboxConfiguration.setPersistentLetterboxPositionForHorizontalReachability(
279                 false /* forBookMode */, LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT);
280         verify(mLetterboxConfigurationPersister).setLetterboxPositionForHorizontalReachability(
281                 false /* forBookMode */,
282                 LETTERBOX_HORIZONTAL_REACHABILITY_POSITION_LEFT);
283 
284         // Check that vertical reachability is set with correct arguments
285         mLetterboxConfiguration.setPersistentLetterboxPositionForVerticalReachability(
286                 false /* forTabletopMode */, LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP);
287         verify(mLetterboxConfigurationPersister).setLetterboxPositionForVerticalReachability(
288                 false /* forTabletopMode */,
289                 LETTERBOX_VERTICAL_REACHABILITY_POSITION_TOP);
290     }
291 }
292