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 package com.android.systemui.complication;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.never;
22 import static org.mockito.Mockito.verify;
23 
24 import android.testing.AndroidTestingRunner;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import com.android.systemui.SysuiTestCase;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.util.Arrays;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.function.Consumer;
37 
38 @SmallTest
39 @RunWith(AndroidTestingRunner.class)
40 public class ComplicationLayoutParamsTest extends SysuiTestCase {
41     /**
42      * Ensures ComplicationLayoutParams cannot be constructed with improper position or direction.
43      */
44     @Test
testPositionValidation()45     public void testPositionValidation() {
46         final HashSet<Integer> invalidCombinations = new HashSet(Arrays.asList(
47                 ComplicationLayoutParams.POSITION_BOTTOM | ComplicationLayoutParams.POSITION_TOP,
48                 ComplicationLayoutParams.POSITION_END | ComplicationLayoutParams.POSITION_START
49         ));
50 
51         final int allPositions = ComplicationLayoutParams.POSITION_TOP
52                 | ComplicationLayoutParams.POSITION_START
53                 | ComplicationLayoutParams.POSITION_END
54                 | ComplicationLayoutParams.POSITION_BOTTOM;
55 
56         final HashSet<Integer> allDirections = new HashSet(Arrays.asList(
57                 ComplicationLayoutParams.DIRECTION_DOWN,
58                 ComplicationLayoutParams.DIRECTION_UP,
59                 ComplicationLayoutParams.DIRECTION_START,
60                 ComplicationLayoutParams.DIRECTION_END
61         ));
62 
63         final HashMap<Integer, Integer> invalidDirections = new HashMap<>();
64         invalidDirections.put(ComplicationLayoutParams.DIRECTION_DOWN,
65                 ComplicationLayoutParams.POSITION_BOTTOM);
66         invalidDirections.put(ComplicationLayoutParams.DIRECTION_UP,
67                 ComplicationLayoutParams.POSITION_TOP);
68         invalidDirections.put(ComplicationLayoutParams.DIRECTION_START,
69                 ComplicationLayoutParams.POSITION_START);
70         invalidDirections.put(ComplicationLayoutParams.DIRECTION_END,
71                 ComplicationLayoutParams.POSITION_END);
72 
73 
74         for (int position = 0; position <= allPositions; ++position) {
75             boolean properPosition = position != 0;
76             if (properPosition) {
77                 for (Integer combination : invalidCombinations) {
78                     if ((combination & position) == combination) {
79                         properPosition = false;
80                     }
81                 }
82             }
83             boolean exceptionEncountered = false;
84             for (Integer direction : allDirections) {
85                 final int invalidPosition = invalidDirections.get(direction);
86                 final boolean properDirection = (invalidPosition & position) != invalidPosition;
87 
88                 try {
89                     new ComplicationLayoutParams(
90                             100,
91                             100,
92                             position,
93                             direction,
94                             0);
95                 } catch (Exception e) {
96                     exceptionEncountered = true;
97                 }
98 
99                 assertThat((properPosition && properDirection) || exceptionEncountered).isTrue();
100             }
101         }
102     }
103 
104     /**
105      * Ensures unspecified margin uses default.
106      */
107     @Test
testDefaultMargin()108     public void testDefaultMargin() {
109         final ComplicationLayoutParams params = new ComplicationLayoutParams(
110                 100,
111                 100,
112                 ComplicationLayoutParams.POSITION_TOP,
113                 ComplicationLayoutParams.DIRECTION_DOWN,
114                 3);
115         assertThat(params.getDirectionalSpacing(10) == 10).isTrue();
116     }
117 
118     /**
119      * Ensures specified margin is used instead of default.
120      */
121     @Test
testSpecifiedMargin()122     public void testSpecifiedMargin() {
123         final ComplicationLayoutParams params = new ComplicationLayoutParams(
124                 100,
125                 100,
126                 ComplicationLayoutParams.POSITION_TOP,
127                 ComplicationLayoutParams.DIRECTION_DOWN,
128                 3,
129                 10);
130         assertThat(params.getDirectionalSpacing(5) == 10).isTrue();
131     }
132 
133     /**
134      * Ensures ComplicationLayoutParams is properly duplicated on copy construction.
135      */
136     @Test
testCopyConstruction()137     public void testCopyConstruction() {
138         final ComplicationLayoutParams params = new ComplicationLayoutParams(
139                 100,
140                 100,
141                 ComplicationLayoutParams.POSITION_TOP,
142                 ComplicationLayoutParams.DIRECTION_DOWN,
143                 3,
144                 10,
145                 20);
146         final ComplicationLayoutParams copy = new ComplicationLayoutParams(params);
147 
148         assertThat(copy.getDirection() == params.getDirection()).isTrue();
149         assertThat(copy.getPosition() == params.getPosition()).isTrue();
150         assertThat(copy.getWeight() == params.getWeight()).isTrue();
151         assertThat(copy.getDirectionalSpacing(0) == params.getDirectionalSpacing(1)).isTrue();
152         assertThat(copy.getConstraint() == params.getConstraint()).isTrue();
153         assertThat(copy.height == params.height).isTrue();
154         assertThat(copy.width == params.width).isTrue();
155     }
156 
157     /**
158      * Ensures ComplicationLayoutParams is properly duplicated on copy construction with unspecified
159      * margin.
160      */
161     @Test
testCopyConstructionWithUnspecifiedMargin()162     public void testCopyConstructionWithUnspecifiedMargin() {
163         final ComplicationLayoutParams params = new ComplicationLayoutParams(
164                 100,
165                 100,
166                 ComplicationLayoutParams.POSITION_TOP,
167                 ComplicationLayoutParams.DIRECTION_DOWN,
168                 3);
169         final ComplicationLayoutParams copy = new ComplicationLayoutParams(params);
170 
171         assertThat(copy.getDirection() == params.getDirection()).isTrue();
172         assertThat(copy.getPosition() == params.getPosition()).isTrue();
173         assertThat(copy.getWeight() == params.getWeight()).isTrue();
174         assertThat(copy.getDirectionalSpacing(1) == params.getDirectionalSpacing(1)).isTrue();
175         assertThat(copy.height == params.height).isTrue();
176         assertThat(copy.width == params.width).isTrue();
177     }
178 
179     /**
180      * Ensures that constraint is set correctly.
181      */
182     @Test
testConstraint()183     public void testConstraint() {
184         final ComplicationLayoutParams paramsWithoutConstraint = new ComplicationLayoutParams(
185                 100,
186                 100,
187                 ComplicationLayoutParams.POSITION_TOP,
188                 ComplicationLayoutParams.DIRECTION_DOWN,
189                 3,
190                 10);
191         assertThat(paramsWithoutConstraint.constraintSpecified()).isFalse();
192 
193         final int constraint = 10;
194         final ComplicationLayoutParams paramsWithConstraint = new ComplicationLayoutParams(
195                 100,
196                 100,
197                 ComplicationLayoutParams.POSITION_TOP,
198                 ComplicationLayoutParams.DIRECTION_DOWN,
199                 3,
200                 10,
201                 constraint);
202         assertThat(paramsWithConstraint.constraintSpecified()).isTrue();
203         assertThat(paramsWithConstraint.getConstraint()).isEqualTo(constraint);
204     }
205 
206     @Test
testIteratePositions()207     public void testIteratePositions() {
208         final int positions = ComplicationLayoutParams.POSITION_TOP
209                 | ComplicationLayoutParams.POSITION_START
210                 | ComplicationLayoutParams.POSITION_END;
211         final Consumer<Integer> consumer = mock(Consumer.class);
212 
213         ComplicationLayoutParams.iteratePositions(consumer, positions);
214 
215         verify(consumer).accept(ComplicationLayoutParams.POSITION_TOP);
216         verify(consumer).accept(ComplicationLayoutParams.POSITION_START);
217         verify(consumer).accept(ComplicationLayoutParams.POSITION_END);
218         verify(consumer, never()).accept(ComplicationLayoutParams.POSITION_BOTTOM);
219     }
220 }
221