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 org.junit.Assert.assertEquals; 24 import static org.junit.Assert.assertFalse; 25 import static org.junit.Assert.assertTrue; 26 27 import android.car.drivingstate.CarUxRestrictions; 28 import android.car.drivingstate.CarUxRestrictionsConfiguration; 29 import android.content.Context; 30 import android.util.ArraySet; 31 32 import androidx.test.ext.junit.runners.AndroidJUnit4; 33 import androidx.test.filters.MediumTest; 34 import androidx.test.platform.app.InstrumentationRegistry; 35 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.xmlpull.v1.XmlPullParserException; 39 40 import java.io.IOException; 41 import java.util.List; 42 import java.util.Set; 43 44 @RunWith(AndroidJUnit4.class) 45 @MediumTest 46 public class CarUxRestrictionsConfigurationXmlParserTest { 47 48 private static final String UX_RESTRICTION_MODE_PASSENGER = "passenger"; 49 getContext()50 private Context getContext() { 51 return InstrumentationRegistry.getInstrumentation().getTargetContext(); 52 } 53 54 @Test testParsingDefaultConfiguration()55 public void testParsingDefaultConfiguration() throws IOException, XmlPullParserException { 56 CarUxRestrictionsConfigurationXmlParser.parse(getContext(), R.xml.car_ux_restrictions_map); 57 } 58 59 @Test testParsingParameters()60 public void testParsingParameters() throws IOException, XmlPullParserException { 61 CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse( 62 getContext(), R.xml.ux_restrictions_only_parameters).get(0); 63 64 CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f); 65 assertEquals(1, r.getMaxContentDepth()); 66 assertEquals(1, r.getMaxCumulativeContentItems()); 67 assertEquals(1, r.getMaxRestrictedStringLength()); 68 } 69 70 @Test testParsingNonMovingState()71 public void testParsingNonMovingState() throws IOException, XmlPullParserException { 72 CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse( 73 getContext(), R.xml.ux_restrictions_non_moving_state).get(0); 74 75 CarUxRestrictions parked = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f); 76 assertFalse(parked.isRequiresDistractionOptimization()); 77 78 CarUxRestrictions idling = config.getUxRestrictions(DRIVING_STATE_IDLING, 0f); 79 assertTrue(idling.isRequiresDistractionOptimization()); 80 assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, idling.getActiveRestrictions()); 81 } 82 83 @Test testParsingMovingState_NoSpeedRange()84 public void testParsingMovingState_NoSpeedRange() throws IOException, XmlPullParserException { 85 CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse( 86 getContext(), R.xml.ux_restrictions_moving_state_no_speed_range).get(0); 87 88 CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_MOVING, 1f); 89 assertTrue(r.isRequiresDistractionOptimization()); 90 assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, r.getActiveRestrictions()); 91 } 92 93 @Test testParsingMovingState_SingleSpeedRange()94 public void testParsingMovingState_SingleSpeedRange() 95 throws IOException, XmlPullParserException { 96 CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse( 97 getContext(), R.xml.ux_restrictions_moving_state_single_speed_range).get(0); 98 99 CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_MOVING, 1f); 100 assertTrue(r.isRequiresDistractionOptimization()); 101 assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, r.getActiveRestrictions()); 102 } 103 104 @Test testParsingMovingState_MultiSpeedRange()105 public void testParsingMovingState_MultiSpeedRange() 106 throws IOException, XmlPullParserException { 107 CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse( 108 getContext(), R.xml.ux_restrictions_moving_state_single_speed_range).get(0); 109 110 CarUxRestrictions slow = config.getUxRestrictions(DRIVING_STATE_MOVING, 1f); 111 assertTrue(slow.isRequiresDistractionOptimization()); 112 assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, slow.getActiveRestrictions()); 113 114 CarUxRestrictions fast = config.getUxRestrictions(DRIVING_STATE_MOVING, 6f); 115 assertTrue(fast.isRequiresDistractionOptimization()); 116 assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, fast.getActiveRestrictions()); 117 } 118 119 @Test testParsingPassengerState()120 public void testParsingPassengerState() throws IOException, XmlPullParserException { 121 CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse( 122 getContext(), R.xml.ux_restrictions_passenger_mode).get(0); 123 124 CarUxRestrictions moving = config.getUxRestrictions( 125 DRIVING_STATE_MOVING, 1f, UX_RESTRICTION_MODE_PASSENGER); 126 assertFalse(moving.isRequiresDistractionOptimization()); 127 128 CarUxRestrictions idling = config.getUxRestrictions( 129 DRIVING_STATE_IDLING, 0f, UX_RESTRICTION_MODE_PASSENGER); 130 assertFalse(idling.isRequiresDistractionOptimization()); 131 132 CarUxRestrictions parked = config.getUxRestrictions( 133 DRIVING_STATE_PARKED, 0f, UX_RESTRICTION_MODE_PASSENGER); 134 assertFalse(parked.isRequiresDistractionOptimization()); 135 } 136 137 @Test testParsingPassengerMode_ValuesInBaselineAreNotAffected()138 public void testParsingPassengerMode_ValuesInBaselineAreNotAffected() 139 throws IOException, XmlPullParserException { 140 CarUxRestrictionsConfiguration config = CarUxRestrictionsConfigurationXmlParser.parse( 141 getContext(), R.xml.ux_restrictions_passenger_mode).get(0); 142 143 CarUxRestrictions moving = config.getUxRestrictions( 144 DRIVING_STATE_MOVING, 1f, UX_RESTRICTION_MODE_BASELINE); 145 assertTrue(moving.isRequiresDistractionOptimization()); 146 assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, moving.getActiveRestrictions()); 147 148 CarUxRestrictions idling = config.getUxRestrictions( 149 DRIVING_STATE_IDLING, 0f, UX_RESTRICTION_MODE_BASELINE); 150 assertTrue(idling.isRequiresDistractionOptimization()); 151 assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, idling.getActiveRestrictions()); 152 153 CarUxRestrictions parked = config.getUxRestrictions( 154 DRIVING_STATE_PARKED, 0f, UX_RESTRICTION_MODE_BASELINE); 155 assertTrue(parked.isRequiresDistractionOptimization()); 156 assertEquals(CarUxRestrictions.UX_RESTRICTIONS_NO_VIDEO, parked.getActiveRestrictions()); 157 } 158 159 @Test testParsingMultipleConfigurations()160 public void testParsingMultipleConfigurations() 161 throws IOException, XmlPullParserException { 162 List<CarUxRestrictionsConfiguration> configs = 163 CarUxRestrictionsConfigurationXmlParser.parse( 164 getContext(), R.xml.ux_restrictions_multiple_display_ports); 165 166 assertEquals(configs.size(), 2); 167 168 // 1 and 2 are specified in test xml. 169 Set<Byte> expected = new ArraySet<>(); 170 expected.add((byte) 1); 171 expected.add((byte) 2); 172 for (CarUxRestrictionsConfiguration config : configs) { 173 assertTrue(expected.contains(config.getPhysicalPort())); 174 } 175 } 176 177 @Test testMultipleConfigurationsShareParameters()178 public void testMultipleConfigurationsShareParameters() 179 throws IOException, XmlPullParserException { 180 List<CarUxRestrictionsConfiguration> configs = 181 CarUxRestrictionsConfigurationXmlParser.parse( 182 getContext(), R.xml.ux_restrictions_multiple_display_ports); 183 184 for (CarUxRestrictionsConfiguration config : configs) { 185 CarUxRestrictions r = config.getUxRestrictions(DRIVING_STATE_PARKED, 0f); 186 assertEquals(1, r.getMaxContentDepth()); 187 assertEquals(1, r.getMaxCumulativeContentItems()); 188 assertEquals(1, r.getMaxRestrictedStringLength()); 189 } 190 } 191 } 192