1 /* 2 * Copyright (C) 2008 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.util.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertNull; 23 import static org.junit.Assert.assertTrue; 24 25 import android.platform.test.ravenwood.RavenwoodRule; 26 import android.util.DisplayMetrics; 27 import android.util.TypedValue; 28 29 import androidx.test.filters.SmallTest; 30 import androidx.test.runner.AndroidJUnit4; 31 32 import org.junit.Rule; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 @SmallTest 37 @RunWith(AndroidJUnit4.class) 38 public class TypedValueTest { 39 @Rule 40 public final RavenwoodRule mRavenwood = new RavenwoodRule(); 41 42 @Test testConstructor()43 public void testConstructor() { 44 new TypedValue(); 45 } 46 47 @Test testGetFloat()48 public void testGetFloat() { 49 final float expected = Float.intBitsToFloat(99); 50 TypedValue tv = new TypedValue(); 51 tv.data = 99; 52 assertEquals(expected, tv.getFloat(), 0.0f); 53 } 54 55 @Test testComplexToDimension()56 public void testComplexToDimension() { 57 DisplayMetrics dm = new DisplayMetrics(); 58 dm.density = 1.1f; 59 dm.heightPixels = 100; 60 dm.scaledDensity = 2.1f; 61 dm.xdpi = 200f; 62 dm.ydpi = 300f; 63 final float expected = TypedValue.applyDimension((10 >> TypedValue.COMPLEX_UNIT_SHIFT) 64 & TypedValue.COMPLEX_UNIT_MASK, TypedValue.complexToFloat(10), dm); 65 66 assertEquals(expected, TypedValue.complexToDimension(10, dm), 0.0f); 67 assertEquals((int)expected, TypedValue.complexToDimensionPixelOffset(10, dm)); 68 } 69 70 @Test testSetTo()71 public void testSetTo() { 72 TypedValue tv1 = new TypedValue(); 73 TypedValue tv2 = new TypedValue(); 74 tv1.assetCookie = 1; 75 tv1.changingConfigurations = 2; 76 tv1.data = 3; 77 tv1.resourceId = 4; 78 tv1.string = "test"; 79 tv1.type = 5; 80 81 tv2.setTo(tv1); 82 83 assertEquals(1, tv2.assetCookie); 84 assertEquals(-1, tv2.changingConfigurations); 85 assertEquals(3, tv2.data); 86 assertEquals(4, tv2.resourceId); 87 assertEquals("test", tv2.string); 88 assertEquals(5, tv2.type); 89 } 90 91 @Test testGetFraction()92 public void testGetFraction() { 93 // set the expected value 94 final float expected = TypedValue.complexToFraction(10, 1.1f, 2.1f) ; 95 TypedValue tv = new TypedValue(); 96 tv.data = 10; 97 assertEquals(expected, tv.getFraction(1.1f, 2.1f), 0.0f); 98 } 99 100 @Test testComplexToDimensionPixelSize()101 public void testComplexToDimensionPixelSize() { 102 DisplayMetrics dm = new DisplayMetrics(); 103 dm.density = 1.1f; 104 dm.heightPixels = 100; 105 dm.scaledDensity = 2.1f; 106 dm.xdpi = 200f; 107 dm.ydpi = 300f; 108 109 // static function just calculate and return it. 110 assertEquals(1, TypedValue.complexToDimensionPixelSize(1000, dm)); 111 assertEquals(1, TypedValue.complexToDimensionPixelSize(9999, dm)); 112 assertEquals(1, TypedValue.complexToDimensionPixelSize(5000, dm)); 113 assertEquals(102, TypedValue.complexToDimensionPixelSize(3333, dm)); 114 assertEquals(1, TypedValue.complexToDimensionPixelSize(2222, dm)); 115 assertEquals(1, TypedValue.complexToDimensionPixelSize(1500, dm)); 116 assertEquals(0, TypedValue.complexToDimensionPixelSize(10, dm)); 117 118 } 119 120 @Test testComplexToFraction()121 public void testComplexToFraction() { 122 final int data1 = 1; 123 final float base1 = 1.1f; 124 final float pbase1 = 2.2f; 125 final float expected1 = 0.0f; 126 127 final int data2 = 100000; 128 final float base2 = 1.1f; 129 final float pbase2 = 2.2f; 130 final float expected2 = 0.013092041f; 131 assertEquals(expected1, TypedValue.complexToFraction(data1, base1, pbase1), 0.0f); 132 assertEquals(expected2, TypedValue.complexToFraction(data2, base2, pbase2), 0.0f); 133 } 134 135 @Test testToString()136 public void testToString() { 137 TypedValue tv = new TypedValue(); 138 tv.assetCookie = 1; 139 tv.changingConfigurations = 2; 140 tv.data = 3; 141 tv.resourceId = 4; 142 tv.string = "test"; 143 tv.type = 5; 144 assertNotNull(tv.toString()); 145 146 tv.type = 3; 147 assertNotNull(tv.toString()); 148 } 149 150 @Test testApplyDimension()151 public void testApplyDimension() { 152 DisplayMetrics dm = new DisplayMetrics(); 153 dm.density = 1.1f; 154 dm.heightPixels = 100; 155 dm.scaledDensity = 2.1f; 156 dm.xdpi = 200f; 157 dm.ydpi = 300f; 158 159 assertEquals(10.0f, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 10, dm), 0.0f); 160 assertEquals(10 * dm.density, TypedValue 161 .applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10, dm), 0.0f); 162 assertEquals(10 * dm.scaledDensity, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 163 10, dm), 0.0f); 164 assertEquals(10 * dm.xdpi * (1.0f/72), 165 TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, 10, dm), 0.0f); 166 assertEquals(10 * dm.xdpi, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, 10, dm), 167 0.0f); 168 assertEquals(10 * dm.xdpi * (1.0f / 25.4f), TypedValue.applyDimension( 169 TypedValue.COMPLEX_UNIT_MM, 10, dm), 0.0f); 170 171 assertEquals(0.0f, TypedValue.applyDimension(-1, 10, dm), 0.0f); 172 } 173 174 @Test testCoerceToString1()175 public void testCoerceToString1() { 176 TypedValue tv = new TypedValue(); 177 tv.assetCookie = 1; 178 tv.changingConfigurations = 2; 179 tv.data = 3; 180 tv.resourceId = 4; 181 tv.string = "test"; 182 tv.type = 5; 183 184 assertNotNull(tv.coerceToString()); 185 186 tv.type = 3; 187 assertNotNull(tv.coerceToString()); 188 } 189 190 @Test testCoerceToString2()191 public void testCoerceToString2() { 192 assertNull(TypedValue.coerceToString(TypedValue.TYPE_NULL, 10)); 193 assertNotNull(TypedValue.coerceToString(TypedValue.TYPE_REFERENCE, 10)); 194 assertNotNull(TypedValue.coerceToString(TypedValue.TYPE_ATTRIBUTE, 10)); 195 assertNotNull(TypedValue.coerceToString(TypedValue.TYPE_FLOAT, 10)); 196 197 assertNotNull(TypedValue.coerceToString(TypedValue.TYPE_DIMENSION, 2)); 198 assertNotNull(TypedValue.coerceToString(TypedValue.TYPE_FRACTION, 1)); 199 assertNotNull(TypedValue.coerceToString(TypedValue.TYPE_INT_HEX, 10)); 200 assertNotNull(TypedValue.coerceToString(TypedValue.TYPE_INT_BOOLEAN, 1)); 201 assertNotNull(TypedValue.coerceToString(TypedValue.TYPE_INT_BOOLEAN, 0)); 202 assertNotNull(TypedValue.coerceToString(TypedValue.TYPE_FIRST_COLOR_INT, 10)); 203 assertNotNull(TypedValue.coerceToString(0x11, 10)); 204 assertNull(TypedValue.coerceToString(-1, 10)); 205 } 206 207 @Test testComplexToFloat()208 public void testComplexToFloat() { 209 final int complex1 = 1; 210 final float expected1 = 0.0f; 211 final int complex2 = 17; 212 final float expected2 = 0.0f; 213 final int complex3 = 9999; 214 final float expected3 = 39.0f; 215 assertEquals(expected1, TypedValue.complexToFloat(complex1), 0.0f); 216 assertEquals(expected2, TypedValue.complexToFloat(complex2), 0.0f); 217 assertEquals(expected3, TypedValue.complexToFloat(complex3), 0.0f); 218 } 219 220 @Test testGetDimension()221 public void testGetDimension() { 222 DisplayMetrics dm = new DisplayMetrics(); 223 dm.density = 1.1f; 224 dm.heightPixels = 100; 225 dm.scaledDensity = 2.1f; 226 dm.xdpi = 200f; 227 dm.ydpi = 300f; 228 229 TypedValue tv = new TypedValue(); 230 tv.data = 10; 231 tv.getDimension(dm); 232 233 assertEquals(TypedValue.complexToDimension(10, dm), tv.getDimension(dm), 0.0f); 234 } 235 236 @Test testGetComplexUnit()237 public void testGetComplexUnit() { 238 TypedValue tv = new TypedValue(); 239 tv.data = 256; 240 assertEquals(TypedValue.COMPLEX_UNIT_PX, tv.getComplexUnit()); 241 tv.data = 257; 242 assertEquals(TypedValue.COMPLEX_UNIT_DIP, tv.getComplexUnit()); 243 tv.data = 258; 244 assertEquals(TypedValue.COMPLEX_UNIT_SP, tv.getComplexUnit()); 245 tv.data = 259; 246 assertEquals(TypedValue.COMPLEX_UNIT_PT, tv.getComplexUnit()); 247 tv.data = 260; 248 assertEquals(TypedValue.COMPLEX_UNIT_IN, tv.getComplexUnit()); 249 tv.data = 261; 250 assertEquals(TypedValue.COMPLEX_UNIT_MM, tv.getComplexUnit()); 251 tv.data = 21474864; 252 assertEquals(TypedValue.COMPLEX_UNIT_FRACTION, tv.getComplexUnit()); 253 tv.data = 21474865; 254 assertEquals(TypedValue.COMPLEX_UNIT_FRACTION_PARENT, tv.getComplexUnit()); 255 } 256 257 @Test testIsColorType()258 public void testIsColorType() { 259 TypedValue tv = new TypedValue(); 260 tv.type = TypedValue.TYPE_INT_COLOR_ARGB8; 261 assertTrue(tv.isColorType()); 262 tv.type = TypedValue.TYPE_INT_COLOR_RGB8; 263 assertTrue(tv.isColorType()); 264 tv.type = TypedValue.TYPE_INT_COLOR_ARGB4; 265 assertTrue(tv.isColorType()); 266 tv.type = TypedValue.TYPE_INT_COLOR_RGB4; 267 assertTrue(tv.isColorType()); 268 269 tv.type = TypedValue.TYPE_INT_HEX; 270 assertFalse(tv.isColorType()); 271 tv.type = TypedValue.TYPE_FLOAT; 272 assertFalse(tv.isColorType()); 273 tv.type = TypedValue.TYPE_ATTRIBUTE; 274 assertFalse(tv.isColorType()); 275 tv.type = TypedValue.TYPE_NULL; 276 assertFalse(tv.isColorType()); 277 } 278 } 279