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.content.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.assertSame; 24 import static org.junit.Assert.assertTrue; 25 import static org.junit.Assert.fail; 26 27 import android.content.ContentValues; 28 import android.os.Parcel; 29 import android.platform.test.annotations.AppModeSdkSandbox; 30 31 import androidx.test.runner.AndroidJUnit4; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 import java.util.ArrayList; 38 import java.util.Map; 39 import java.util.Set; 40 41 @RunWith(AndroidJUnit4.class) 42 @AppModeSdkSandbox(reason = "Allow test in the SDK sandbox (does not prevent other modes).") 43 public class ContentValuesTest { 44 ContentValues mContentValues; 45 46 @Before setUp()47 public void setUp() throws Exception { 48 mContentValues = new ContentValues(); 49 } 50 51 @Test testConstructor()52 public void testConstructor() { 53 new ContentValues(); 54 new ContentValues(5); 55 new ContentValues(mContentValues); 56 57 try { 58 new ContentValues(-1); 59 fail("There should be a IllegalArgumentException thrown out."); 60 } catch (IllegalArgumentException e) { 61 // expected, test success. 62 } 63 64 try { 65 new ContentValues(null); 66 fail("There should be a NullPointerException thrown out."); 67 } catch (NullPointerException e) { 68 // expected, test success. 69 } 70 } 71 72 @Test testValueSet()73 public void testValueSet() { 74 Set<Map.Entry<String, Object>> map; 75 assertNotNull(map = mContentValues.valueSet()); 76 assertTrue(map.isEmpty()); 77 78 mContentValues.put("Long", 10L); 79 mContentValues.put("Integer", 201); 80 81 assertNotNull(map = mContentValues.valueSet()); 82 assertFalse(map.isEmpty()); 83 assertEquals(2, map.size()); 84 } 85 86 @Test testPutNull()87 public void testPutNull() { 88 mContentValues.putNull("key"); 89 assertNull(mContentValues.get("key")); 90 91 mContentValues.putNull("value"); 92 assertNull(mContentValues.get("value")); 93 94 mContentValues.putNull(""); 95 assertNull(mContentValues.get("")); 96 97 // input null as param 98 mContentValues.putNull(null); 99 } 100 101 @Test testGetAsLong()102 public void testGetAsLong() { 103 Long expected = 10L; 104 mContentValues.put("Long", expected); 105 assertEquals(expected, mContentValues.getAsLong("Long")); 106 107 expected = -1000L; 108 mContentValues.put("Long", expected); 109 assertEquals(expected, mContentValues.getAsLong("Long")); 110 111 // input null as param 112 assertNull(mContentValues.getAsLong(null)); 113 } 114 115 @Test testGetAsByte()116 public void testGetAsByte() { 117 Byte expected = 'a'; 118 mContentValues.put("Byte", expected); 119 assertEquals(expected, mContentValues.getAsByte("Byte")); 120 121 expected = 'z'; 122 mContentValues.put("Byte", expected); 123 assertEquals(expected, mContentValues.getAsByte("Byte")); 124 125 // input null as param 126 assertNull(mContentValues.getAsByte(null)); 127 } 128 129 @Test testGetAsInteger()130 public void testGetAsInteger() { 131 Integer expected = 20; 132 mContentValues.put("Integer", expected); 133 assertEquals(expected, mContentValues.getAsInteger("Integer")); 134 135 expected = -20000; 136 mContentValues.put("Integer", expected); 137 assertEquals(expected, mContentValues.getAsInteger("Integer")); 138 139 // input null as param 140 assertNull(mContentValues.getAsInteger(null)); 141 } 142 143 @Test testSize()144 public void testSize() { 145 assertEquals(0, mContentValues.size()); 146 147 mContentValues.put("Integer", 10); 148 mContentValues.put("Long", 10L); 149 assertEquals(2, mContentValues.size()); 150 151 mContentValues.put("String", "b"); 152 mContentValues.put("Boolean", false); 153 assertEquals(4, mContentValues.size()); 154 155 mContentValues.clear(); 156 assertEquals(0, mContentValues.size()); 157 } 158 159 @Test testGetAsShort()160 public void testGetAsShort() { 161 Short expected = 20; 162 mContentValues.put("Short", expected); 163 assertEquals(expected, mContentValues.getAsShort("Short")); 164 165 expected = -200; 166 mContentValues.put("Short", expected); 167 assertEquals(expected, mContentValues.getAsShort("Short")); 168 169 // input null as param 170 assertNull(mContentValues.getAsShort(null)); 171 } 172 173 @Test testHashCode()174 public void testHashCode() { 175 assertEquals(0, mContentValues.hashCode()); 176 177 mContentValues.put("Float", 2.2F); 178 mContentValues.put("Short", 12); 179 assertTrue(0 != mContentValues.hashCode()); 180 181 int hashcode = mContentValues.hashCode(); 182 mContentValues.remove("Short"); 183 assertTrue(hashcode != mContentValues.hashCode()); 184 185 mContentValues.put("Short", 12); 186 assertTrue(hashcode == mContentValues.hashCode()); 187 188 mContentValues.clear(); 189 assertEquals(0, mContentValues.hashCode()); 190 } 191 192 @Test testGetAsFloat()193 public void testGetAsFloat() { 194 Float expected = 1.0F; 195 mContentValues.put("Float", expected); 196 assertEquals(expected, mContentValues.getAsFloat("Float")); 197 198 expected = -5.5F; 199 mContentValues.put("Float", expected); 200 assertEquals(expected, mContentValues.getAsFloat("Float")); 201 202 // input null as param 203 assertNull(mContentValues.getAsFloat(null)); 204 } 205 206 @Test testGetAsBoolean()207 public void testGetAsBoolean() { 208 mContentValues.put("Boolean", true); 209 assertTrue(mContentValues.getAsBoolean("Boolean")); 210 211 mContentValues.put("Boolean", false); 212 assertFalse(mContentValues.getAsBoolean("Boolean")); 213 214 // input null as param 215 assertNull(mContentValues.getAsBoolean(null)); 216 } 217 218 @Test testToString()219 public void testToString() { 220 assertNotNull(mContentValues.toString()); 221 222 mContentValues.put("Float", 1.1F); 223 assertNotNull(mContentValues.toString()); 224 assertTrue(mContentValues.toString().length() > 0); 225 } 226 227 @Test testGet()228 public void testGet() { 229 Object expected = "android"; 230 mContentValues.put("Object", "android"); 231 assertSame(expected, mContentValues.get("Object")); 232 233 expected = 20; 234 mContentValues.put("Object", 20); 235 assertSame(expected, mContentValues.get("Object")); 236 237 // input null as params 238 assertNull(mContentValues.get(null)); 239 } 240 241 @Test testEquals()242 public void testEquals() { 243 mContentValues.put("Boolean", false); 244 mContentValues.put("String", "string"); 245 246 ContentValues cv = new ContentValues(); 247 cv.put("Boolean", false); 248 cv.put("String", "string"); 249 250 assertTrue(mContentValues.equals(cv)); 251 } 252 253 @Test testEqualsFailure()254 public void testEqualsFailure() { 255 // the target object is not an instance of ContentValues. 256 assertFalse(mContentValues.equals(new String())); 257 258 // the two object is not equals 259 mContentValues.put("Boolean", false); 260 mContentValues.put("String", "string"); 261 262 ContentValues cv = new ContentValues(); 263 cv.put("Boolean", true); 264 cv.put("String", "111"); 265 266 assertFalse(mContentValues.equals(cv)); 267 } 268 269 @Test testGetAsDouble()270 public void testGetAsDouble() { 271 Double expected = 10.2; 272 mContentValues.put("Double", expected); 273 assertEquals(expected, mContentValues.getAsDouble("Double")); 274 275 expected = -15.4; 276 mContentValues.put("Double", expected); 277 assertEquals(expected, mContentValues.getAsDouble("Double")); 278 279 // input null as params 280 assertNull(mContentValues.getAsDouble(null)); 281 } 282 283 @Test testPutString()284 public void testPutString() { 285 String expected = "cts"; 286 mContentValues.put("String", expected); 287 assertSame(expected, mContentValues.getAsString("String")); 288 289 expected = "android"; 290 mContentValues.put("String", expected); 291 assertSame(expected, mContentValues.getAsString("String")); 292 293 // input null as params 294 mContentValues.put(null, (String)null); 295 } 296 297 @Test testPutByte()298 public void testPutByte() { 299 Byte expected = 'a'; 300 mContentValues.put("Byte", expected); 301 assertSame(expected, mContentValues.getAsByte("Byte")); 302 303 expected = 'z'; 304 mContentValues.put("Byte", expected); 305 assertSame(expected, mContentValues.getAsByte("Byte")); 306 307 // input null as params 308 mContentValues.put(null, (Byte)null); 309 } 310 311 @Test testPutShort()312 public void testPutShort() { 313 Short expected = 20; 314 mContentValues.put("Short", expected); 315 assertEquals(expected, mContentValues.getAsShort("Short")); 316 317 expected = -200; 318 mContentValues.put("Short", expected); 319 assertEquals(expected, mContentValues.getAsShort("Short")); 320 321 // input null as params 322 mContentValues.put(null, (Short)null); 323 } 324 325 @Test testPutInteger()326 public void testPutInteger() { 327 Integer expected = 20; 328 mContentValues.put("Integer", expected); 329 assertEquals(expected, mContentValues.getAsInteger("Integer")); 330 331 expected = -20000; 332 mContentValues.put("Integer", expected); 333 assertEquals(expected, mContentValues.getAsInteger("Integer")); 334 335 // input null as params 336 mContentValues.put(null, (Integer)null); 337 } 338 339 @Test testPutLong()340 public void testPutLong() { 341 Long expected = 10L; 342 mContentValues.put("Long", expected); 343 assertEquals(expected, mContentValues.getAsLong("Long")); 344 345 expected = -1000L; 346 mContentValues.put("Long", expected); 347 assertEquals(expected, mContentValues.getAsLong("Long")); 348 349 // input null as params 350 mContentValues.put(null, (Long)null); 351 } 352 353 @Test testPutFloat()354 public void testPutFloat() { 355 Float expected = 1.0F; 356 mContentValues.put("Float", expected); 357 assertEquals(expected, mContentValues.getAsFloat("Float")); 358 359 expected = -5.5F; 360 mContentValues.put("Float", expected); 361 assertEquals(expected, mContentValues.getAsFloat("Float")); 362 363 // input null as params 364 mContentValues.put(null, (Float)null); 365 } 366 367 @Test testPutDouble()368 public void testPutDouble() { 369 Double expected = 10.2; 370 mContentValues.put("Double", expected); 371 assertEquals(expected, mContentValues.getAsDouble("Double")); 372 373 expected = -15.4; 374 mContentValues.put("Double", expected); 375 assertEquals(expected, mContentValues.getAsDouble("Double")); 376 377 // input null as params 378 mContentValues.put(null, (Double)null); 379 } 380 381 @Test testPutBoolean()382 public void testPutBoolean() { 383 // set the expected value 384 mContentValues.put("Boolean", true); 385 assertTrue(mContentValues.getAsBoolean("Boolean")); 386 387 mContentValues.put("Boolean", false); 388 assertFalse(mContentValues.getAsBoolean("Boolean")); 389 390 // input null as params 391 mContentValues.put(null, (Boolean)null); 392 } 393 394 @Test testPutByteArray()395 public void testPutByteArray() { 396 byte[] expected = new byte[] {'1', '2', '3', '4'}; 397 mContentValues.put("byte[]", expected); 398 assertSame(expected, mContentValues.getAsByteArray("byte[]")); 399 400 // input null as params 401 mContentValues.put(null, (byte[])null); 402 } 403 404 @Test testContainsKey()405 public void testContainsKey() { 406 mContentValues.put("Double", 10.2); 407 mContentValues.put("Float", 1.0F); 408 409 assertTrue(mContentValues.containsKey("Double")); 410 assertTrue(mContentValues.containsKey("Float")); 411 412 assertFalse(mContentValues.containsKey("abc")); 413 assertFalse(mContentValues.containsKey("cts")); 414 415 // input null as param 416 assertFalse(mContentValues.containsKey(null)); 417 } 418 419 @Test testClear()420 public void testClear() { 421 assertEquals(0, mContentValues.size()); 422 423 mContentValues.put("Double", 10.2); 424 mContentValues.put("Float", 1.0F); 425 assertEquals(2, mContentValues.size()); 426 427 mContentValues.clear(); 428 assertEquals(0, mContentValues.size()); 429 } 430 431 @Test 432 @SuppressWarnings("deprecation") testAccessStringArrayList()433 public void testAccessStringArrayList() { 434 // set the expected value 435 ArrayList<String> expected = new ArrayList<String>(); 436 expected.add(0, "cts"); 437 expected.add(1, "android"); 438 439 mContentValues.putStringArrayList("StringArrayList", expected); 440 assertSame(expected, mContentValues.getStringArrayList("StringArrayList")); 441 442 // input null as params 443 mContentValues.putStringArrayList(null, null); 444 assertNull(mContentValues.getStringArrayList(null)); 445 } 446 447 @Test testRemove()448 public void testRemove() { 449 assertEquals(0, mContentValues.size()); 450 451 mContentValues.put("Double", 10.2); 452 mContentValues.put("Float", 1.0F); 453 mContentValues.put("Integer", -11); 454 mContentValues.put("Boolean", false); 455 assertEquals(4, mContentValues.size()); 456 457 mContentValues.remove("Integer"); 458 assertEquals(3, mContentValues.size()); 459 460 mContentValues.remove("Double"); 461 assertEquals(2, mContentValues.size()); 462 463 mContentValues.remove("Boolean"); 464 assertEquals(1, mContentValues.size()); 465 466 mContentValues.remove("Float"); 467 assertEquals(0, mContentValues.size()); 468 469 // remove null 470 mContentValues.remove(null); 471 } 472 473 @Test testGetAsString()474 public void testGetAsString() { 475 String expected = "cts"; 476 mContentValues.put("String", expected); 477 assertSame(expected, mContentValues.getAsString("String")); 478 479 expected = "android"; 480 mContentValues.put("String", expected); 481 assertSame(expected, mContentValues.getAsString("String")); 482 483 // input null as param 484 assertNull(mContentValues.getAsString(null)); 485 } 486 487 @Test testGetAsByteArray()488 public void testGetAsByteArray() { 489 byte[] expected = new byte[] {'1', '2', '3', '4'}; 490 mContentValues.put("byte[]", expected); 491 assertSame(expected, mContentValues.getAsByteArray("byte[]")); 492 493 // input null as param 494 assertNull(mContentValues.getAsByteArray(null)); 495 } 496 497 @Test 498 @SuppressWarnings({ "unchecked" }) testWriteToParcel()499 public void testWriteToParcel() { 500 final ContentValues before = new ContentValues(); 501 before.put("Integer", -110); 502 before.put("String", "cts"); 503 before.put("Boolean", false); 504 505 Parcel p = Parcel.obtain(); 506 before.writeToParcel(p, 0); 507 p.setDataPosition(0); 508 509 final ContentValues after = ContentValues.CREATOR.createFromParcel(p); 510 assertEquals(3, after.size()); 511 assertEquals(-110, after.get("Integer")); 512 assertEquals("cts", after.get("String")); 513 assertEquals(false, after.get("Boolean")); 514 } 515 516 @Test testWriteToParcelFailure()517 public void testWriteToParcelFailure() { 518 try { 519 mContentValues.writeToParcel(null, -1); 520 fail("There should be a NullPointerException thrown out."); 521 } catch (NullPointerException e) { 522 // expected, test success. 523 } 524 } 525 526 @Test testDescribeContents()527 public void testDescribeContents() { 528 assertEquals(0, mContentValues.describeContents()); 529 } 530 531 @Test testPutAll()532 public void testPutAll() { 533 assertEquals(0, mContentValues.size()); 534 535 mContentValues.put("Integer", -11); 536 assertEquals(1, mContentValues.size()); 537 538 ContentValues cv = new ContentValues(); 539 cv.put("String", "cts"); 540 cv.put("Boolean", true); 541 assertEquals(2, cv.size()); 542 543 mContentValues.putAll(cv); 544 assertEquals(3, mContentValues.size()); 545 } 546 547 @Test testPutAllFailure()548 public void testPutAllFailure() { 549 try { 550 mContentValues.putAll(null); 551 fail("There should be a NullPointerException thrown out."); 552 } catch (NullPointerException e) { 553 // expected, test success. 554 } 555 } 556 557 @Test testIsEmpty()558 public void testIsEmpty() { 559 final ContentValues values = new ContentValues(); 560 assertTrue(values.isEmpty()); 561 values.put("k", "v"); 562 assertFalse(values.isEmpty()); 563 values.clear(); 564 assertTrue(values.isEmpty()); 565 } 566 } 567