1 /* 2 * Copyright (C) 2016 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 android.car.apitest; 18 19 import android.car.hardware.CarPropertyConfig; 20 import android.graphics.Point; 21 22 /** 23 * Unit tests for {@link CarPropertyConfig} 24 */ 25 public class CarPropertyConfigTest extends CarPropertyTestBase { 26 27 public void testCarPropertyConfigBuilder() { 28 createFloatPropertyConfig(); 29 } 30 31 private CarPropertyConfig<Float> createFloatPropertyConfig() { 32 CarPropertyConfig<Float> config = CarPropertyConfig 33 .newBuilder(Float.class, PROPERTY_ID, CAR_AREA_TYPE) 34 .addArea(WINDOW_DRIVER) 35 .addAreaConfig(WINDOW_PASSENGER, 10f, 20f) 36 .build(); 37 38 assertEquals(PROPERTY_ID, config.getPropertyId()); 39 assertEquals(CAR_AREA_TYPE, config.getAreaType()); 40 assertEquals(Float.class, config.getPropertyType()); 41 assertEquals(2, config.getAreaCount()); 42 43 // We didn't assign any restrictions to WINDOW_DRIVER area. 44 assertNull(config.getMinValue(WINDOW_DRIVER)); 45 assertNull(config.getMaxValue(WINDOW_DRIVER)); 46 47 assertEquals(10f, config.getMinValue(WINDOW_PASSENGER)); 48 assertEquals(20f, config.getMaxValue(WINDOW_PASSENGER)); 49 50 return config; 51 } 52 53 public void testWriteReadFloat() { 54 CarPropertyConfig<Float> config = createFloatPropertyConfig(); 55 56 writeToParcel(config); 57 CarPropertyConfig<Float> configRead = readFromParcel(); 58 59 assertEquals(PROPERTY_ID, configRead.getPropertyId()); 60 assertEquals(CAR_AREA_TYPE, configRead.getAreaType()); 61 assertEquals(Float.class, configRead.getPropertyType()); 62 assertEquals(2, configRead.getAreaCount()); 63 64 // We didn't assign any restrictions to WINDOW_DRIVER area. 65 assertNull(configRead.getMinValue(WINDOW_DRIVER)); 66 assertNull(configRead.getMaxValue(WINDOW_DRIVER)); 67 68 assertEquals(10f, configRead.getMinValue(WINDOW_PASSENGER)); 69 assertEquals(20f, configRead.getMaxValue(WINDOW_PASSENGER)); 70 } 71 72 public void testWriteReadIntegerArray() { 73 CarPropertyConfig<Integer[]> config = CarPropertyConfig 74 .newBuilder(Integer[].class, PROPERTY_ID, CAR_AREA_TYPE) 75 // We can specify min/max values per each element. 76 .addAreaConfig(WINDOW_DRIVER, new Integer[] {10, 20, 30}, new Integer[0]) 77 .addArea(WINDOW_PASSENGER) 78 .build(); 79 80 writeToParcel(config); 81 CarPropertyConfig<Integer[]> configRead = readFromParcel(); 82 83 assertEquals(PROPERTY_ID, configRead.getPropertyId()); 84 assertEquals(CAR_AREA_TYPE, configRead.getAreaType()); 85 assertEquals(Integer[].class, configRead.getPropertyType()); 86 assertEquals(2, configRead.getAreaCount()); 87 88 // We didn't assign any restrictions to WINDOW_DRIVER area. 89 assertNull(configRead.getMinValue(WINDOW_PASSENGER)); 90 assertNull(configRead.getMaxValue(WINDOW_PASSENGER)); 91 92 assertEquals(3, configRead.getMinValue(WINDOW_DRIVER).length); 93 assertEquals(20, (int) configRead.getMinValue(WINDOW_DRIVER)[1]); 94 assertEquals(0, configRead.getMaxValue(WINDOW_DRIVER).length); 95 } 96 97 public void testWriteReadUnexpectedType() { 98 CarPropertyConfig<Float> config = createFloatPropertyConfig(); 99 100 writeToParcel(config); 101 102 try { 103 CarPropertyConfig<Integer> integerConfig = readFromParcel(); 104 Integer value = integerConfig.getMinValue(WINDOW_PASSENGER); 105 fail(String.valueOf(value)); 106 } catch (ClassCastException expected) { 107 // Expected. Wrote float, attempted to read integer. 108 } 109 110 // Type casting from raw CarPropertyConfig should be fine, just sanity check. 111 CarPropertyConfig rawTypeConfig = readFromParcel(); 112 assertEquals(10f, rawTypeConfig.getMinValue(WINDOW_PASSENGER)); 113 114 try { 115 int intValue = (Integer) rawTypeConfig.getMinValue(WINDOW_PASSENGER); 116 fail(String.valueOf(intValue)); 117 } catch (ClassCastException expected) { 118 // Expected. Wrote float, attempted to read integer. 119 } 120 } 121 122 public void testWriteReadArbitraryParcelable() { 123 Point maxPoint = new Point(10, 20); 124 CarPropertyConfig<Point> config = CarPropertyConfig 125 .newBuilder(Point.class, PROPERTY_ID, CAR_AREA_TYPE) 126 .addAreaConfig(WINDOW_DRIVER, null, maxPoint) 127 .build(); 128 129 assertNull(config.getMinValue()); 130 assertNotNull(config.toString(), config.getMaxValue()); 131 assertEquals("Value: " + config.toString(), 10, config.getMaxValue().x); 132 133 writeToParcel(config); 134 135 CarPropertyConfig<Point> configRead = readFromParcel(); 136 assertNotNull(configRead); 137 assertEquals(Point.class, configRead.getPropertyType()); 138 assertNull(configRead.getMinValue()); 139 assertEquals(10, configRead.getMaxValue().x); 140 } 141 } 142