1 /*
2  * Copyright (C) 2018 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.car;
17 
18 import static android.car.drivingstate.CarDrivingStateEvent.DRIVING_STATE_IDLING;
19 import static android.car.drivingstate.CarDrivingStateEvent.DRIVING_STATE_MOVING;
20 import static android.car.drivingstate.CarDrivingStateEvent.DRIVING_STATE_PARKED;
21 import static android.car.drivingstate.CarUxRestrictionsManager.UX_RESTRICTION_MODE_BASELINE;
22 
23 import static com.google.common.truth.Truth.assertThat;
24 
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 
29 import android.car.CarOccupantZoneManager;
30 import android.car.drivingstate.CarUxRestrictions;
31 import android.car.drivingstate.CarUxRestrictionsConfiguration;
32 import android.content.Context;
33 import android.util.ArraySet;
34 
35 import androidx.test.ext.junit.runners.AndroidJUnit4;
36 import androidx.test.filters.MediumTest;
37 import androidx.test.platform.app.InstrumentationRegistry;
38 
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.xmlpull.v1.XmlPullParserException;
42 
43 import java.io.IOException;
44 import java.util.List;
45 import java.util.Set;
46 
47 @RunWith(AndroidJUnit4.class)
48 @MediumTest
49 public class CarUxRestrictionsConfigurationXmlParserTest {
50 
51     private static final String UX_RESTRICTION_MODE_PASSENGER = "passenger";
52 
getContext()53     private Context getContext() {
54         return InstrumentationRegistry.getInstrumentation().getTargetContext();
55     }
56 
57     @Test
testParsingDefaultConfiguration()58     public void testParsingDefaultConfiguration() throws IOException, XmlPullParserException {
59         CarUxRestrictionsConfigurationXmlParser.parse(getContext(), R.xml.car_ux_restrictions_map);
60     }
61 
62     @Test
testParsingParameters()63     public void testParsingParameters() throws IOException, XmlPullParserException {
64         CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse(
65                 getContext(), R.xml.ux_restrictions_only_parameters).get(0);
66 
67         CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f);
68         assertEquals(1, r.getMaxContentDepth());
69         assertEquals(1, r.getMaxCumulativeContentItems());
70         assertEquals(1, r.getMaxRestrictedStringLength());
71     }
72 
73     @Test
testParsingNonMovingState()74     public void testParsingNonMovingState() throws IOException, XmlPullParserException {
75         CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse(
76                 getContext(), R.xml.ux_restrictions_non_moving_state).get(0);
77 
78         CarUxRestrictions parked = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f);
79         assertFalse(parked.isRequiresDistractionOptimization());
80 
81         CarUxRestrictions idling = config.getUxRestrictions(DRIVING_STATE_IDLING, 0f);
82         assertTrue(idling.isRequiresDistractionOptimization());
83         assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, idling.getActiveRestrictions());
84     }
85 
86     @Test
testParsingMovingState_NoSpeedRange()87     public void testParsingMovingState_NoSpeedRange() throws IOException, XmlPullParserException {
88         CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse(
89                 getContext(), R.xml.ux_restrictions_moving_state_no_speed_range).get(0);
90 
91         CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_MOVING, 1f);
92         assertTrue(r.isRequiresDistractionOptimization());
93         assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, r.getActiveRestrictions());
94     }
95 
96     @Test
testParsingMovingState_SingleSpeedRange()97     public void testParsingMovingState_SingleSpeedRange()
98             throws IOException, XmlPullParserException {
99         CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse(
100                 getContext(), R.xml.ux_restrictions_moving_state_single_speed_range).get(0);
101 
102         CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_MOVING, 1f);
103         assertTrue(r.isRequiresDistractionOptimization());
104         assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, r.getActiveRestrictions());
105     }
106 
107     @Test
testParsingMovingState_MultiSpeedRange()108     public void testParsingMovingState_MultiSpeedRange()
109             throws IOException, XmlPullParserException {
110         CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse(
111                 getContext(), R.xml.ux_restrictions_moving_state_single_speed_range).get(0);
112 
113         CarUxRestrictions slow = config.getUxRestrictions(DRIVING_STATE_MOVING, 1f);
114         assertTrue(slow.isRequiresDistractionOptimization());
115         assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, slow.getActiveRestrictions());
116 
117         CarUxRestrictions fast = config.getUxRestrictions(DRIVING_STATE_MOVING, 6f);
118         assertTrue(fast.isRequiresDistractionOptimization());
119         assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, fast.getActiveRestrictions());
120     }
121 
122     @Test
testParsingPassengerState()123     public void testParsingPassengerState() throws IOException, XmlPullParserException {
124         CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse(
125                 getContext(), R.xml.ux_restrictions_passenger_mode).get(0);
126 
127         CarUxRestrictions moving = config.getUxRestrictions(
128                 DRIVING_STATE_MOVING, 1f, UX_RESTRICTION_MODE_PASSENGER);
129         assertFalse(moving.isRequiresDistractionOptimization());
130 
131         CarUxRestrictions idling = config.getUxRestrictions(
132                 DRIVING_STATE_IDLING, 0f, UX_RESTRICTION_MODE_PASSENGER);
133         assertFalse(idling.isRequiresDistractionOptimization());
134 
135         CarUxRestrictions parked = config.getUxRestrictions(
136                 DRIVING_STATE_PARKED, 0f, UX_RESTRICTION_MODE_PASSENGER);
137         assertFalse(parked.isRequiresDistractionOptimization());
138     }
139 
140     @Test
testParsingMultipleModes()141     public void testParsingMultipleModes() throws IOException, XmlPullParserException {
142         CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse(
143                 getContext(), R.xml.ux_restrictions_multi_mode).get(0);
144         CarUxRestrictions moving = config.getUxRestrictions(
145                 DRIVING_STATE_MOVING, 1f, "mode1");
146         assertFalse(moving.isRequiresDistractionOptimization());
147 
148         moving = config.getUxRestrictions(
149                 DRIVING_STATE_MOVING, 1f, "mode2");
150         assertFalse(moving.isRequiresDistractionOptimization());
151 
152         CarUxRestrictions idling = config.getUxRestrictions(
153                 DRIVING_STATE_IDLING, 0f, "mode1");
154         assertFalse(idling.isRequiresDistractionOptimization());
155 
156         idling = config.getUxRestrictions(
157                 DRIVING_STATE_IDLING, 0f, "mode2");
158         assertFalse(idling.isRequiresDistractionOptimization());
159 
160         CarUxRestrictions parked = config.getUxRestrictions(
161                 DRIVING_STATE_PARKED, 0f, "mode1");
162         assertFalse(parked.isRequiresDistractionOptimization());
163 
164         parked = config.getUxRestrictions(
165                 DRIVING_STATE_PARKED, 0f, "mode2");
166         assertFalse(parked.isRequiresDistractionOptimization());
167 
168         parked = config.getUxRestrictions(
169                 DRIVING_STATE_PARKED, 0f, "mode3");
170         assertFalse(parked.isRequiresDistractionOptimization());
171     }
172 
173     @Test
testParsingPassengerMode_ValuesInBaselineAreNotAffected()174     public void testParsingPassengerMode_ValuesInBaselineAreNotAffected()
175             throws IOException, XmlPullParserException {
176         CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse(
177                 getContext(), R.xml.ux_restrictions_passenger_mode).get(0);
178 
179         CarUxRestrictions moving = config.getUxRestrictions(
180                 DRIVING_STATE_MOVING, 1f, UX_RESTRICTION_MODE_BASELINE);
181         assertTrue(moving.isRequiresDistractionOptimization());
182         assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, moving.getActiveRestrictions());
183 
184         CarUxRestrictions idling = config.getUxRestrictions(
185                 DRIVING_STATE_IDLING, 0f, UX_RESTRICTION_MODE_BASELINE);
186         assertTrue(idling.isRequiresDistractionOptimization());
187         assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, idling.getActiveRestrictions());
188 
189         CarUxRestrictions parked = config.getUxRestrictions(
190                 DRIVING_STATE_PARKED, 0f, UX_RESTRICTION_MODE_BASELINE);
191         assertTrue(parked.isRequiresDistractionOptimization());
192         assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, parked.getActiveRestrictions());
193     }
194 
195     @Test
testParsingMultipleConfigurations()196     public void testParsingMultipleConfigurations()
197             throws IOException, XmlPullParserException {
198         List<CarUxRestrictionsConfiguration> configs =
199                 CarUxRestrictionsConfigurationXmlParser.parse(
200                         getContext(), R.xml.ux_restrictions_multiple_display_ports);
201 
202         assertThat(configs.size()).isEqualTo(2);
203 
204         // 1 and 2 are specified in test xml.
205         Set<Integer> expected = new ArraySet<>();
206         expected.add(1);
207         expected.add(2);
208         for (CarUxRestrictionsConfiguration config : configs) {
209             assertTrue(expected.contains(config.getPhysicalPort()));
210             assertThat(config.getOccupantZoneId()).isEqualTo(
211                     CarOccupantZoneManager.OccupantZoneInfo.INVALID_ZONE_ID);
212             assertThat(config.getDisplayType()).isEqualTo(
213                     CarOccupantZoneManager.DISPLAY_TYPE_UNKNOWN);
214         }
215     }
216 
217     @Test
testMultipleConfigurationsShareParameters()218     public void testMultipleConfigurationsShareParameters()
219             throws IOException, XmlPullParserException {
220         List<CarUxRestrictionsConfiguration> configs =
221                 CarUxRestrictionsConfigurationXmlParser.parse(
222                         getContext(), R.xml.ux_restrictions_multiple_display_ports);
223 
224         for (CarUxRestrictionsConfiguration config : configs) {
225             CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f);
226             assertEquals(1, r.getMaxContentDepth());
227             assertEquals(1, r.getMaxCumulativeContentItems());
228             assertEquals(1, r.getMaxRestrictedStringLength());
229         }
230     }
231 
232     @Test
testParsingConfigurationWithDisplayConfig()233     public void testParsingConfigurationWithDisplayConfig()
234             throws IOException, XmlPullParserException {
235         List<CarUxRestrictionsConfiguration> configs =
236                 CarUxRestrictionsConfigurationXmlParser.parse(
237                         getContext(), R.xml.ux_restrictions_display_config);
238 
239         assertThat(configs).hasSize(1);
240         CarUxRestrictionsConfiguration config = configs.get(0);
241         // occupant zone id and display type are specified in the xml file.
242         assertThat(config.getOccupantZoneId()).isEqualTo(1);
243         assertThat(config.getDisplayType()).isEqualTo(1);
244     }
245 
246     @Test
testParsingConfigurationMultipleRestrictionParameters()247     public void testParsingConfigurationMultipleRestrictionParameters()
248             throws IOException, XmlPullParserException {
249         List<CarUxRestrictionsConfiguration> configs =
250                 CarUxRestrictionsConfigurationXmlParser.parse(
251                         getContext(), R.xml.ux_restrictions_multiple_restriction_parameters);
252 
253         assertThat(configs).hasSize(1);
254         CarUxRestrictionsConfiguration config = configs.get(0);
255         CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f);
256         assertEquals(5, r.getMaxContentDepth());
257         assertEquals(5, r.getMaxCumulativeContentItems());
258         assertEquals(5, r.getMaxRestrictedStringLength());
259     }
260 
261     @Test
testParsingConfigurationMultipleRestrictionParametersLocalAndGlobal()262     public void testParsingConfigurationMultipleRestrictionParametersLocalAndGlobal()
263             throws IOException, XmlPullParserException {
264         List<CarUxRestrictionsConfiguration> configs =
265                 CarUxRestrictionsConfigurationXmlParser.parse(getContext(),
266                         R.xml.ux_restrictions_multiple_restriction_parameters_local_and_global);
267 
268         assertThat(configs).hasSize(2);
269 
270         CarUxRestrictionsConfiguration config = configs.get(1);
271         CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f);
272         assertEquals(1, r.getMaxContentDepth());
273         assertEquals(1, r.getMaxCumulativeContentItems());
274         assertEquals(1, r.getMaxRestrictedStringLength());
275 
276 
277         config = configs.get(0);
278         r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f);
279         assertEquals(5, r.getMaxContentDepth());
280         assertEquals(5, r.getMaxCumulativeContentItems());
281         assertEquals(5, r.getMaxRestrictedStringLength());
282     }
283 
284     @Test
testParsingConfigurationMultipleRestrictionParametersOnlyLocal()285     public void testParsingConfigurationMultipleRestrictionParametersOnlyLocal()
286             throws IOException, XmlPullParserException {
287         List<CarUxRestrictionsConfiguration> configs =
288                 CarUxRestrictionsConfigurationXmlParser.parse(
289                         getContext(),
290                         R.xml.ux_restrictions_multiple_restriction_parameters_only_local);
291 
292         assertThat(configs).hasSize(2);
293 
294         CarUxRestrictionsConfiguration config = configs.get(0);
295         CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f);
296         assertEquals(3, r.getMaxContentDepth());
297         assertEquals(3, r.getMaxCumulativeContentItems());
298         assertEquals(3, r.getMaxRestrictedStringLength());
299 
300 
301         config = configs.get(1);
302         r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f);
303         assertEquals(5, r.getMaxContentDepth());
304         assertEquals(5, r.getMaxCumulativeContentItems());
305         assertEquals(5, r.getMaxRestrictedStringLength());
306     }
307 
308     @Test
testParsingConfigurationMultipleRestrictionParametersMultipleDrivingState()309     public void testParsingConfigurationMultipleRestrictionParametersMultipleDrivingState()
310             throws IOException, XmlPullParserException {
311         List<CarUxRestrictionsConfiguration> configs =
312                 CarUxRestrictionsConfigurationXmlParser.parse(getContext(),
313                         R.xml.ux_restrictions_multiple_restriction_parameters_multiple_driving_state
314                 );
315 
316         assertThat(configs).hasSize(1);
317 
318         CarUxRestrictionsConfiguration config = configs.get(0);
319         CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f);
320         assertEquals(5, r.getMaxContentDepth());
321         assertEquals(5, r.getMaxCumulativeContentItems());
322         assertEquals(5, r.getMaxRestrictedStringLength());
323 
324         r = config.getUxRestrictions(DRIVING_STATE_IDLING, 0f);
325         assertEquals(5, r.getMaxContentDepth());
326         assertEquals(5, r.getMaxCumulativeContentItems());
327         assertEquals(5, r.getMaxRestrictedStringLength());
328     }
329 
330     @Test
testParsingConfigurationIncorrectRestrictionParameters()331     public void testParsingConfigurationIncorrectRestrictionParameters()
332             throws IOException, XmlPullParserException {
333         List<CarUxRestrictionsConfiguration> configs =
334                 CarUxRestrictionsConfigurationXmlParser.parse(
335                         getContext(), R.xml.ux_restrictions_incorrect_restriction_parameters);
336 
337         CarUxRestrictionsConfiguration config = configs.get(0);
338         CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f);
339 
340         assertEquals(1, r.getMaxContentDepth());
341         assertEquals(1, r.getMaxCumulativeContentItems());
342         assertEquals(1, r.getMaxRestrictedStringLength());
343     }
344 
345     @Test
testParsingConfigurationNoStringRestrictionParameters()346     public void testParsingConfigurationNoStringRestrictionParameters()
347             throws IOException, XmlPullParserException {
348         List<CarUxRestrictionsConfiguration> configs =
349                 CarUxRestrictionsConfigurationXmlParser.parse(
350                         getContext(), R.xml.ux_restrictions_no_string_restriction_parameters);
351 
352         CarUxRestrictionsConfiguration config = configs.get(0);
353         CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f);
354 
355         assertEquals(5, r.getMaxContentDepth());
356         assertEquals(5, r.getMaxCumulativeContentItems());
357         assertEquals(1, r.getMaxRestrictedStringLength());
358     }
359 }
360