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 testParsingPassengerMode_ValuesInBaselineAreNotAffected()141 public void testParsingPassengerMode_ValuesInBaselineAreNotAffected() 142 throws IOException, XmlPullParserException { 143 CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse( 144 getContext(), R.xml.ux_restrictions_passenger_mode).get(0); 145 146 CarUxRestrictions moving = config.getUxRestrictions( 147 DRIVING_STATE_MOVING, 1f, UX_RESTRICTION_MODE_BASELINE); 148 assertTrue(moving.isRequiresDistractionOptimization()); 149 assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, moving.getActiveRestrictions()); 150 151 CarUxRestrictions idling = config.getUxRestrictions( 152 DRIVING_STATE_IDLING, 0f, UX_RESTRICTION_MODE_BASELINE); 153 assertTrue(idling.isRequiresDistractionOptimization()); 154 assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, idling.getActiveRestrictions()); 155 156 CarUxRestrictions parked = config.getUxRestrictions( 157 DRIVING_STATE_PARKED, 0f, UX_RESTRICTION_MODE_BASELINE); 158 assertTrue(parked.isRequiresDistractionOptimization()); 159 assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, parked.getActiveRestrictions()); 160 } 161 162 @Test testParsingMultipleConfigurations()163 public void testParsingMultipleConfigurations() 164 throws IOException, XmlPullParserException { 165 List<CarUxRestrictionsConfiguration> configs = 166 CarUxRestrictionsConfigurationXmlParser.parse( 167 getContext(), R.xml.ux_restrictions_multiple_display_ports); 168 169 assertEquals(configs.size(), 2); 170 171 // 1 and 2 are specified in test xml. 172 Set<Integer> expected = new ArraySet<>(); 173 expected.add(1); 174 expected.add(2); 175 for (CarUxRestrictionsConfiguration config : configs) { 176 assertTrue(expected.contains(config.getPhysicalPort())); 177 assertThat(config.getOccupantZoneId()).isEqualTo( 178 CarOccupantZoneManager.OccupantZoneInfo.INVALID_ZONE_ID); 179 assertThat(config.getDisplayType()).isEqualTo( 180 CarOccupantZoneManager.DISPLAY_TYPE_UNKNOWN); 181 } 182 } 183 184 @Test testMultipleConfigurationsShareParameters()185 public void testMultipleConfigurationsShareParameters() 186 throws IOException, XmlPullParserException { 187 List<CarUxRestrictionsConfiguration> configs = 188 CarUxRestrictionsConfigurationXmlParser.parse( 189 getContext(), R.xml.ux_restrictions_multiple_display_ports); 190 191 for (CarUxRestrictionsConfiguration config : configs) { 192 CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f); 193 assertEquals(1, r.getMaxContentDepth()); 194 assertEquals(1, r.getMaxCumulativeContentItems()); 195 assertEquals(1, r.getMaxRestrictedStringLength()); 196 } 197 } 198 199 @Test testParsingConfigurationWithDisplayConfig()200 public void testParsingConfigurationWithDisplayConfig() 201 throws IOException, XmlPullParserException { 202 List<CarUxRestrictionsConfiguration> configs = 203 CarUxRestrictionsConfigurationXmlParser.parse( 204 getContext(), R.xml.ux_restrictions_display_config); 205 206 assertThat(configs).hasSize(1); 207 CarUxRestrictionsConfiguration config = configs.get(0); 208 // occupant zone id and display type are specified in the xml file. 209 assertThat(config.getOccupantZoneId()).isEqualTo(1); 210 assertThat(config.getDisplayType()).isEqualTo(1); 211 } 212 } 213