1 /* 2 * Copyright (C) 2015, 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.aidl.tests; 18 19 import static org.hamcrest.core.Is.is; 20 import static org.hamcrest.core.IsNull.notNullValue; 21 import static org.junit.Assert.assertNotNull; 22 import static org.junit.Assert.assertThat; 23 import static org.junit.Assert.assertTrue; 24 import static org.junit.Assert.assertFalse; 25 import static org.junit.Assume.assumeTrue; 26 27 import android.aidl.tests.ByteEnum; 28 import android.aidl.tests.GenericStructuredParcelable; 29 import android.aidl.tests.INamedCallback; 30 import android.aidl.tests.ITestService; 31 import android.aidl.tests.IntEnum; 32 import android.aidl.tests.LongEnum; 33 import android.aidl.tests.SimpleParcelable; 34 import android.aidl.tests.StructuredParcelable; 35 import android.aidl.tests.Union; 36 import android.aidl.versioned.tests.IFooInterface; 37 import android.app.Activity; 38 import android.content.Context; 39 import android.content.Intent; 40 import android.os.Bundle; 41 import android.os.IBinder; 42 import android.os.ParcelFileDescriptor; 43 import android.os.PersistableBundle; 44 import android.os.RemoteException; 45 import android.os.ServiceManager; 46 import android.os.ServiceSpecificException; 47 import android.util.Log; 48 import java.io.File; 49 import java.io.FileDescriptor; 50 import java.io.FileInputStream; 51 import java.io.FileOutputStream; 52 import java.io.IOException; 53 import java.util.ArrayList; 54 import java.util.Arrays; 55 import java.util.Collections; 56 import java.util.HashMap; 57 import java.util.List; 58 import java.util.Map; 59 import org.junit.Before; 60 import org.junit.Test; 61 import org.junit.runner.RunWith; 62 import org.junit.runners.JUnit4; 63 64 @RunWith(JUnit4.class) 65 public class TestServiceClient { 66 private ITestService service; 67 private ICppJavaTests cpp_java_tests; 68 69 @Before setUp()70 public void setUp() throws RemoteException { 71 IBinder binder = ServiceManager.waitForService(ITestService.class.getName()); 72 assertNotNull(binder); 73 service = ITestService.Stub.asInterface(binder); 74 assertNotNull(service); 75 76 IBinder binder2 = service.GetCppJavaTests(); 77 if (binder2 != null) { 78 cpp_java_tests = ICppJavaTests.Stub.asInterface(binder2); 79 } 80 } 81 82 @Test testOneway()83 public void testOneway() throws RemoteException { 84 service.TestOneway(); 85 } 86 87 @Test testBooleanRepeat()88 public void testBooleanRepeat() throws RemoteException { 89 boolean query = true; 90 assertThat(service.RepeatBoolean(query), is(query)); 91 } 92 93 @Test testCharRepeat()94 public void testCharRepeat() throws RemoteException { 95 char query = 'A'; 96 assertThat(service.RepeatChar(query), is(query)); 97 } 98 99 @Test testByteRepeat()100 public void testByteRepeat() throws RemoteException { 101 byte query = -128; 102 assertThat(service.RepeatByte(query), is(query)); 103 } 104 105 @Test testIntRepeat()106 public void testIntRepeat() throws RemoteException { 107 int query = 1 << 30; 108 assertThat(service.RepeatInt(query), is(query)); 109 } 110 111 @Test testConstRepeat()112 public void testConstRepeat() throws RemoteException { 113 int query[] = {ITestService.TEST_CONSTANT, 114 ITestService.TEST_CONSTANT2, 115 ITestService.TEST_CONSTANT3, 116 ITestService.TEST_CONSTANT4, 117 ITestService.TEST_CONSTANT5, 118 ITestService.TEST_CONSTANT6, 119 ITestService.TEST_CONSTANT7, 120 ITestService.TEST_CONSTANT8}; 121 for (int i = 0; i < query.length; i++) { 122 assertThat(service.RepeatInt(query[i]), is(query[i])); 123 } 124 } 125 126 @Test testLongRepeat()127 public void testLongRepeat() throws RemoteException { 128 long query = 1L << 60; 129 assertThat(service.RepeatLong(query), is(query)); 130 } 131 132 @Test testFloatRepeat()133 public void testFloatRepeat() throws RemoteException { 134 float query = 1.0f/3.0f; 135 assertThat(service.RepeatFloat(query), is(query)); 136 } 137 138 @Test testDoubleRepeat()139 public void testDoubleRepeat() throws RemoteException { 140 double query = 1.0/3.0; 141 assertThat(service.RepeatDouble(query), is(query)); 142 } 143 144 @Test testByteEnumRepeat()145 public void testByteEnumRepeat() throws RemoteException { 146 byte query = ByteEnum.FOO; 147 assertThat(service.RepeatByteEnum(query), is(query)); 148 } 149 150 @Test testIntEnumRepeat()151 public void testIntEnumRepeat() throws RemoteException { 152 int query = IntEnum.FOO; 153 assertThat(service.RepeatIntEnum(query), is(query)); 154 } 155 156 @Test testLongEnumRepeat()157 public void testLongEnumRepeat() throws RemoteException { 158 long query = LongEnum.FOO; 159 assertThat(service.RepeatLongEnum(query), is(query)); 160 } 161 162 @Test testStringListRepeat()163 public void testStringListRepeat() throws RemoteException { 164 List<String> queries = Arrays.asList( 165 "not empty", "", "\0", 166 ITestService.STRING_TEST_CONSTANT, 167 ITestService.STRING_TEST_CONSTANT2); 168 for (String query : queries) { 169 assertThat(service.RepeatString(query), is(query)); 170 } 171 } 172 173 @Test testBooleanArrayReversal()174 public void testBooleanArrayReversal() throws RemoteException { 175 boolean[] input = {true, false, false, false}; 176 boolean echoed[] = new boolean[input.length]; 177 boolean[] reversed = service.ReverseBoolean(input, echoed); 178 assertThat(echoed, is(input)); 179 assertThat(reversed.length, is(input.length)); 180 for (int i = 0; i < input.length; ++i) { 181 int j = reversed.length - (1 + i); 182 assertThat(reversed[j], is(input[i])); 183 } 184 } 185 186 @Test testByteArrayReversal()187 public void testByteArrayReversal() throws RemoteException { 188 byte[] input = {0, 1, 2}; 189 byte echoed[] = new byte[input.length]; 190 byte[] reversed = service.ReverseByte(input, echoed); 191 assertThat(echoed, is(input)); 192 assertThat(reversed.length, is(input.length)); 193 for (int i = 0; i < input.length; ++i) { 194 int j = reversed.length - (1 + i); 195 assertThat(reversed[j], is(input[i])); 196 } 197 } 198 199 @Test testCharArrayReversal()200 public void testCharArrayReversal() throws RemoteException { 201 char[] input = {'A', 'B', 'C', 'D', 'E'}; 202 char echoed[] = new char[input.length]; 203 char[] reversed = service.ReverseChar(input, echoed); 204 assertThat(echoed, is(input)); 205 assertThat(reversed.length, is(input.length)); 206 for (int i = 0; i < input.length; ++i) { 207 int j = reversed.length - (1 + i); 208 assertThat(reversed[j], is(input[i])); 209 } 210 } 211 212 @Test testIntArrayReversal()213 public void testIntArrayReversal() throws RemoteException { 214 int[] input = {-1, 0, 1, 2, 3, 4, 5, 6}; 215 int echoed[] = new int[input.length]; 216 int[] reversed = service.ReverseInt(input, echoed); 217 assertThat(echoed, is(input)); 218 assertThat(reversed.length, is(input.length)); 219 for (int i = 0; i < input.length; ++i) { 220 int j = reversed.length - (1 + i); 221 assertThat(reversed[j], is(input[i])); 222 } 223 } 224 225 @Test testLongArrayReversal()226 public void testLongArrayReversal() throws RemoteException { 227 long[] input = {-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8}; 228 long echoed[] = new long[input.length]; 229 long[] reversed = service.ReverseLong(input, echoed); 230 assertThat(echoed, is(input)); 231 assertThat(reversed.length, is(input.length)); 232 for (int i = 0; i < input.length; ++i) { 233 int j = reversed.length - (1 + i); 234 assertThat(reversed[j], is(input[i])); 235 } 236 } 237 238 @Test testFloatArrayReversal()239 public void testFloatArrayReversal() throws RemoteException { 240 float[] input = {0.0f, 1.0f, -0.3f}; 241 float echoed[] = new float[input.length]; 242 float[] reversed = service.ReverseFloat(input, echoed); 243 assertThat(echoed, is(input)); 244 assertThat(reversed.length, is(input.length)); 245 for (int i = 0; i < input.length; ++i) { 246 int j = reversed.length - (1 + i); 247 assertThat(reversed[j], is(input[i])); 248 } 249 } 250 251 @Test testDoubleArrayReversal()252 public void testDoubleArrayReversal() throws RemoteException { 253 double[] input = {-1.0, -4.0, -2.0}; 254 double echoed[] = new double[input.length]; 255 double[] reversed = service.ReverseDouble(input, echoed); 256 assertThat(echoed, is(input)); 257 assertThat(reversed.length, is(input.length)); 258 for (int i = 0; i < input.length; ++i) { 259 int j = reversed.length - (1 + i); 260 assertThat(reversed[j], is(input[i])); 261 } 262 } 263 264 @Test testStringArrayReversal()265 public void testStringArrayReversal() throws RemoteException { 266 String[] input = {"For", "relaxing", "times"}; 267 String echoed[] = new String[input.length]; 268 String[] reversed = service.ReverseString(input, echoed); 269 assertThat(echoed, is(input)); 270 assertThat(reversed.length, is(input.length)); 271 for (int i = 0; i < input.length; ++i) { 272 int j = reversed.length - (1 + i); 273 assertThat(reversed[j], is(input[i])); 274 } 275 } 276 277 @Test testBinderExchange()278 public void testBinderExchange() throws RemoteException { 279 INamedCallback got = service.GetOtherTestService("Smythe"); 280 assertThat(got.GetName(), is("Smythe")); 281 282 assertThat(service.VerifyName(got, "Smythe"), is(true)); 283 } 284 285 @Test testListReversal()286 public void testListReversal() throws RemoteException { 287 List<String> input = Arrays.asList("Walk", "into", "Córdoba"); 288 List<String> echoed = new ArrayList<String>(); 289 List<String> reversed = service.ReverseStringList(input, echoed); 290 assertThat(echoed, is(input)); 291 292 Collections.reverse(input); 293 assertThat(reversed, is(input)); 294 } 295 296 @Test testRepeatGenericParcelable()297 public void testRepeatGenericParcelable() throws RemoteException { 298 assumeTrue(cpp_java_tests != null); 299 300 GenericStructuredParcelable<Integer, StructuredParcelable, Integer> input = 301 new GenericStructuredParcelable<Integer, StructuredParcelable, Integer>(); 302 GenericStructuredParcelable<Integer, StructuredParcelable, Integer> out_param = 303 new GenericStructuredParcelable<Integer, StructuredParcelable, Integer>(); 304 input.a = 41; 305 input.b = 42; 306 GenericStructuredParcelable<Integer, StructuredParcelable, Integer> testing = input; 307 assertThat(testing, is(input)); 308 GenericStructuredParcelable<Integer, StructuredParcelable, Integer> returned = 309 cpp_java_tests.RepeatGenericParcelable(input, out_param); 310 assertThat(out_param.a, is(input.a)); 311 assertThat(out_param.b, is(input.b)); 312 assertThat(returned.a, is(input.a)); 313 assertThat(returned.b, is(input.b)); 314 } 315 316 @Test testRepeatParcelable()317 public void testRepeatParcelable() throws RemoteException { 318 assumeTrue(cpp_java_tests != null); 319 320 SimpleParcelable input = new SimpleParcelable("foo", 42); 321 SimpleParcelable out_param = new SimpleParcelable(); 322 SimpleParcelable returned = cpp_java_tests.RepeatSimpleParcelable(input, out_param); 323 assertThat(out_param, is(input)); 324 assertThat(returned, is(input)); 325 } 326 327 @Test testReverseParcelable()328 public void testReverseParcelable() throws RemoteException { 329 assumeTrue(cpp_java_tests != null); 330 331 SimpleParcelable[] input = new SimpleParcelable[3]; 332 input[0] = new SimpleParcelable("a", 1); 333 input[1] = new SimpleParcelable("b", 2); 334 input[2] = new SimpleParcelable("c", 3); 335 SimpleParcelable[] repeated = new SimpleParcelable[3]; 336 SimpleParcelable[] reversed = cpp_java_tests.ReverseSimpleParcelables(input, repeated); 337 assertThat(repeated, is(input)); 338 assertThat(reversed.length, is(input.length)); 339 for (int i = 0, k = input.length - 1; i < input.length; ++i, --k) { 340 assertThat(reversed[k], is(input[i])); 341 } 342 } 343 344 @Test testRepeatEmptyPersistableBundle()345 public void testRepeatEmptyPersistableBundle() throws RemoteException { 346 assumeTrue(cpp_java_tests != null); 347 348 PersistableBundle emptyBundle = new PersistableBundle(); 349 PersistableBundle returned = cpp_java_tests.RepeatPersistableBundle(emptyBundle); 350 assertThat(returned.size(), is(emptyBundle.size())); 351 assertThat(returned.toString(), is(emptyBundle.toString())); 352 } 353 354 @Test testRepeatNonEmptyPersistableBundle()355 public void testRepeatNonEmptyPersistableBundle() throws RemoteException { 356 assumeTrue(cpp_java_tests != null); 357 358 PersistableBundle pb = new PersistableBundle(); 359 360 final String testBoolKey = "testBool"; 361 final String testIntKey = "testInt"; 362 final String testNestedIntKey = "testNestedInt"; 363 final String testLongKey = "testLong"; 364 final String testDoubleKey = "testDouble"; 365 final String testStringKey = "testString"; 366 final String testBoolArrayKey = "testBoolArray"; 367 final String testIntArrayKey = "testIntArray"; 368 final String testLongArrayKey = "testLongArray"; 369 final String testDoubleArrayKey = "testDoubleArray"; 370 final String testStringArrayKey = "testStringArray"; 371 final String testPersistableBundleKey = "testPersistableBundle"; 372 373 pb.putBoolean(testBoolKey, false); 374 pb.putInt(testIntKey, 33); 375 pb.putLong(testLongKey, 34359738368L); 376 pb.putDouble(testDoubleKey, 1.1); 377 pb.putString(testStringKey, new String("Woot!")); 378 pb.putBooleanArray(testBoolArrayKey, new boolean[] {true, false, true}); 379 pb.putIntArray(testIntArrayKey, new int[] {33, 44, 55, 142}); 380 pb.putLongArray(testLongArrayKey, new long[] {34L, 8371L, 34359738375L}); 381 pb.putDoubleArray(testDoubleArrayKey, new double[] {2.2, 5.4}); 382 pb.putStringArray(testStringArrayKey, new String[] {"hello", "world!"}); 383 PersistableBundle testNestedPersistableBundle = new PersistableBundle(); 384 testNestedPersistableBundle.putInt(testNestedIntKey, 345); 385 pb.putPersistableBundle(testPersistableBundleKey, testNestedPersistableBundle); 386 387 PersistableBundle ret = cpp_java_tests.RepeatPersistableBundle(pb); 388 389 assertThat(ret.size(), is(pb.size())); 390 assertThat(ret.getBoolean(testBoolKey), is(pb.getBoolean(testBoolKey))); 391 assertThat(ret.getInt(testIntKey), is(pb.getInt(testIntKey))); 392 assertThat(ret.getLong(testLongKey), is(pb.getLong(testLongKey))); 393 assertThat(ret.getDouble(testDoubleKey), is(pb.getDouble(testDoubleKey))); 394 assertThat(ret.getString(testStringKey), is(pb.getString(testStringKey))); 395 assertThat(ret.getBooleanArray(testBoolArrayKey), is(pb.getBooleanArray(testBoolArrayKey))); 396 assertThat(ret.getIntArray(testIntArrayKey), is(pb.getIntArray(testIntArrayKey))); 397 assertThat(ret.getLongArray(testLongArrayKey), is(pb.getLongArray(testLongArrayKey))); 398 assertThat(ret.getDoubleArray(testDoubleArrayKey), is(pb.getDoubleArray(testDoubleArrayKey))); 399 assertThat(ret.getStringArray(testStringArrayKey), is(pb.getStringArray(testStringArrayKey))); 400 401 PersistableBundle nested = ret.getPersistableBundle(testPersistableBundleKey); 402 assertThat(nested, is(notNullValue())); 403 assertThat(nested.getInt(testNestedIntKey), is(testNestedPersistableBundle.getInt(testNestedIntKey))); 404 } 405 406 @Test testReversePersistableBundleArray()407 public void testReversePersistableBundleArray() throws RemoteException { 408 assumeTrue(cpp_java_tests != null); 409 410 PersistableBundle[] input = new PersistableBundle[3]; 411 PersistableBundle first = new PersistableBundle(); 412 PersistableBundle second = new PersistableBundle(); 413 PersistableBundle third = new PersistableBundle(); 414 final String testIntKey = new String("testInt"); 415 final String testLongKey = new String("testLong"); 416 final String testDoubleKey = new String("testDouble"); 417 first.putInt(testIntKey, 1231); 418 second.putLong(testLongKey, 222222L); 419 third.putDouble(testDoubleKey, 10.8); 420 input[0] = first; 421 input[1] = second; 422 input[2] = third; 423 final int original_input_size = input.length; 424 425 PersistableBundle[] repeated = new PersistableBundle[input.length]; 426 PersistableBundle[] reversed = cpp_java_tests.ReversePersistableBundles(input, repeated); 427 428 assertThat(repeated.length, is(input.length)); 429 assertThat(input.length, is(original_input_size)); 430 assertThat(repeated[0].getInt(testIntKey), is(input[0].getInt(testIntKey))); 431 assertThat(repeated[1].getLong(testLongKey), is(input[1].getLong(testLongKey))); 432 assertThat(repeated[2].getDouble(testDoubleKey), is(input[2].getDouble(testDoubleKey))); 433 434 assertThat(reversed.length, is(input.length)); 435 assertThat(reversed[0].getInt(testIntKey), is(input[2].getInt(testIntKey))); 436 assertThat(reversed[1].getLong(testLongKey), is(input[1].getLong(testLongKey))); 437 assertThat(reversed[2].getDouble(testDoubleKey), is(input[0].getDouble(testDoubleKey))); 438 } 439 440 @Test testFileDescriptorPassing()441 public void testFileDescriptorPassing() throws RemoteException, IOException { 442 assumeTrue(cpp_java_tests != null); 443 444 String file = "/data/local/tmp/aidl-test-file"; 445 FileOutputStream fos = new FileOutputStream(file, false /*append*/); 446 447 FileDescriptor descriptor = fos.getFD(); 448 FileDescriptor journeyed = cpp_java_tests.RepeatFileDescriptor(descriptor); 449 fos.close(); 450 451 FileOutputStream journeyedStream = new FileOutputStream(journeyed); 452 453 String testData = "FrazzleSnazzleFlimFlamFlibbityGumboChops"; 454 byte[] output = testData.getBytes(); 455 journeyedStream.write(output); 456 journeyedStream.close(); 457 458 FileInputStream fis = new FileInputStream(file); 459 byte[] input = new byte[output.length]; 460 461 assertThat(fis.read(input), is(input.length)); 462 assertThat(input, is(output)); 463 } 464 465 @Test testParcelFileDescriptorPassing()466 public void testParcelFileDescriptorPassing() throws RemoteException, IOException { 467 String file = "/data/local/tmp/aidl-test-file"; 468 ParcelFileDescriptor descriptor = ParcelFileDescriptor.open( 469 new File(file), ParcelFileDescriptor.MODE_CREATE | 470 ParcelFileDescriptor.MODE_WRITE_ONLY); 471 ParcelFileDescriptor journeyed = service.RepeatParcelFileDescriptor(descriptor); 472 473 FileOutputStream journeyedStream = new ParcelFileDescriptor.AutoCloseOutputStream(journeyed); 474 475 String testData = "FrazzleSnazzleFlimFlamFlibbityGumboChops"; 476 byte[] output = testData.getBytes(); 477 journeyedStream.write(output); 478 journeyedStream.close(); 479 480 FileInputStream fis = new FileInputStream(file); 481 byte[] input = new byte[output.length]; 482 483 assertThat(fis.read(input), is(input.length)); 484 assertThat(input, is(output)); 485 } 486 487 @Test testServiceSpecificExceptions()488 public void testServiceSpecificExceptions() throws RemoteException { 489 for (int i = -1; i < 2; ++i) { 490 try { 491 service.ThrowServiceException(i); 492 } catch (ServiceSpecificException ex) { 493 assertThat(ex.errorCode, is(i)); 494 } 495 } 496 } 497 498 private static final List<String> utf8_queries = Arrays.asList( 499 "typical string", 500 "", 501 "\0\0\0", 502 // Java doesn't handle unicode code points above U+FFFF well. 503 new String(Character.toChars(0x1F701)) + "\u03A9"); 504 505 @Test testRepeatUtf8String()506 public void testRepeatUtf8String() throws RemoteException { 507 for (String query : utf8_queries) { 508 String response = service.RepeatUtf8CppString(query); 509 assertThat(response, is(query)); 510 } 511 } 512 513 @Test testReverseUtf8StringArray()514 public void testReverseUtf8StringArray() throws RemoteException { 515 String[] input = (String[])utf8_queries.toArray(); 516 String echoed[] = new String[input.length]; 517 518 String[] reversed = service.ReverseUtf8CppString(input, echoed); 519 520 assertThat(echoed, is(input)); 521 assertThat(reversed.length, is(input.length)); 522 for (int i = 0; i < input.length; ++i) { 523 int j = reversed.length - (1 + i); 524 assertThat(reversed[j], is(input[i])); 525 } 526 } 527 528 @Test testReverseNullableUtf8StringArray()529 public void testReverseNullableUtf8StringArray() throws RemoteException { 530 final List<String> utf8_queries_and_nulls = Arrays.asList( 531 "typical string", 532 null, 533 "", 534 "\0\0\0", 535 null, 536 // Java doesn't handle unicode code points above U+FFFF well. 537 new String(Character.toChars(0x1F701)) + "\u03A9"); 538 String[] input = (String[])utf8_queries_and_nulls.toArray(); 539 String echoed[] = new String[input.length]; 540 541 String[] reversed = service.ReverseNullableUtf8CppString(input, echoed); 542 543 assertThat(echoed, is(input)); 544 assertThat(reversed.length, is(input.length)); 545 for (int i = 0; i < input.length; ++i) { 546 int j = reversed.length - (1 + i); 547 if (input[i] == null && reversed[j] == null) { 548 continue; 549 } 550 assertThat(reversed[j], is(input[i])); 551 } 552 } 553 shouldBeTheSame(StructuredParcelable a, StructuredParcelable b)554 private void shouldBeTheSame(StructuredParcelable a, StructuredParcelable b) { 555 assertTrue(a.equals(b)); 556 assertTrue(b.equals(a)); 557 assertTrue(a.equals(a)); 558 assertTrue(b.equals(b)); 559 assertTrue(a.hashCode() == b.hashCode()); 560 } 561 shouldBeDifferent(StructuredParcelable a, StructuredParcelable b)562 private void shouldBeDifferent(StructuredParcelable a, StructuredParcelable b) { 563 assertFalse(a.equals(b)); 564 assertFalse(b.equals(a)); 565 assertFalse(a.hashCode() == b.hashCode()); 566 } 567 568 @Test testStructurecParcelableEquality()569 public void testStructurecParcelableEquality() { 570 StructuredParcelable p = new StructuredParcelable(); 571 p.shouldContainThreeFs = new int[] {1, 2, 3}; 572 p.shouldBeJerry = "Jerry"; 573 574 StructuredParcelable p2 = new StructuredParcelable(); 575 p2.shouldContainThreeFs = new int[] {1, 2, 3}; 576 p2.shouldBeJerry = "Jerry"; 577 shouldBeTheSame(p, p2); 578 579 StructuredParcelable p3 = new StructuredParcelable(); 580 p3.shouldContainThreeFs = new int[] {1, 2, 3, 4}; 581 p3.shouldBeJerry = "Jerry"; 582 shouldBeDifferent(p, p3); 583 584 StructuredParcelable p4 = new StructuredParcelable(); 585 p4.shouldContainThreeFs = new int[] {1, 2, 3}; 586 p4.shouldBeJerry = "Tom"; 587 shouldBeDifferent(p, p4); 588 } 589 590 @Test testStrucuturedParcelable()591 public void testStrucuturedParcelable() throws RemoteException { 592 final int kDesiredFValue = 17; 593 594 StructuredParcelable p = new StructuredParcelable(); 595 p.shouldContainThreeFs = new int[0]; 596 p.f = kDesiredFValue; 597 p.shouldBeJerry = ""; 598 p.shouldContainTwoByteFoos = new byte[2]; 599 p.shouldContainTwoIntFoos = new int[2]; 600 p.shouldContainTwoLongFoos = new long[2]; 601 602 // Check the default values 603 assertThat(p.stringDefaultsToFoo, is("foo")); 604 final byte byteFour = 4; 605 assertThat(p.byteDefaultsToFour, is(byteFour)); 606 assertThat(p.intDefaultsToFive, is(5)); 607 assertThat(p.longDefaultsToNegativeSeven, is(-7L)); 608 assertThat(p.booleanDefaultsToTrue, is(true)); 609 assertThat(p.charDefaultsToC, is('C')); 610 assertThat(p.floatDefaultsToPi, is(3.14f)); 611 assertThat(p.doubleWithDefault, is(-3.14e17)); 612 assertThat(p.arrayDefaultsTo123, is(new int[] {1, 2, 3})); 613 assertThat(p.arrayDefaultsToEmpty.length, is(0)); 614 assertThat(p.defaultWithFoo, is(IntEnum.FOO)); 615 616 service.FillOutStructuredParcelable(p); 617 618 // Check the returned values 619 assertThat(p.shouldContainThreeFs, is(new int[] {kDesiredFValue, kDesiredFValue, kDesiredFValue})); 620 assertThat(p.shouldBeJerry, is("Jerry")); 621 assertThat(p.shouldBeByteBar, is(ByteEnum.BAR)); 622 assertThat(p.shouldBeIntBar, is(IntEnum.BAR)); 623 assertThat(p.shouldBeLongBar, is(LongEnum.BAR)); 624 assertThat(p.shouldContainTwoByteFoos, is(new byte[] {ByteEnum.FOO, ByteEnum.FOO})); 625 assertThat(p.shouldContainTwoIntFoos, is(new int[] {IntEnum.FOO, IntEnum.FOO})); 626 assertThat(p.shouldContainTwoLongFoos, is(new long[] {LongEnum.FOO, LongEnum.FOO})); 627 assertThat(p.int32_min, is(Integer.MIN_VALUE)); 628 assertThat(p.int32_max, is(Integer.MAX_VALUE)); 629 assertThat(p.int64_max, is(Long.MAX_VALUE)); 630 assertThat(p.hexInt32_neg_1, is(-1)); 631 for (int ndx = 0; ndx < p.int32_1.length; ndx++) { 632 assertThat(p.int32_1[ndx], is(1)); 633 } 634 for (int ndx = 0; ndx < p.int64_1.length; ndx++) { 635 assertThat(p.int64_1[ndx], is(1L)); 636 } 637 assertThat(p.hexInt32_pos_1, is(1)); 638 assertThat(p.hexInt64_pos_1, is(1)); 639 assertThat(p.const_exprs_1, is(1)); 640 assertThat(p.const_exprs_2, is(1)); 641 assertThat(p.const_exprs_3, is(1)); 642 assertThat(p.const_exprs_4, is(1)); 643 assertThat(p.const_exprs_5, is(1)); 644 assertThat(p.const_exprs_6, is(1)); 645 assertThat(p.const_exprs_7, is(1)); 646 assertThat(p.const_exprs_8, is(1)); 647 assertThat(p.const_exprs_9, is(1)); 648 assertThat(p.const_exprs_10, is(1)); 649 650 assertThat( 651 p.shouldSetBit0AndBit2, is(StructuredParcelable.BIT0 | StructuredParcelable.BIT2)); 652 653 assertThat(p.u.getNs(), is(new int[] {1, 2, 3})); 654 assertThat(p.shouldBeConstS1.getS(), is(Union.S1)); 655 656 final String expected = "android.aidl.tests.StructuredParcelable{" 657 + "shouldContainThreeFs: [17, 17, 17], " 658 + "f: 17, " 659 + "shouldBeJerry: Jerry, " 660 + "shouldBeByteBar: 2, " 661 + "shouldBeIntBar: 2000, " 662 + "shouldBeLongBar: 200000000000, " 663 + "shouldContainTwoByteFoos: [1, 1], " 664 + "shouldContainTwoIntFoos: [1000, 1000], " 665 + "shouldContainTwoLongFoos: [100000000000, 100000000000], " 666 + "stringDefaultsToFoo: foo, " 667 + "byteDefaultsToFour: 4, " 668 + "intDefaultsToFive: 5, " 669 + "longDefaultsToNegativeSeven: -7, " 670 + "booleanDefaultsToTrue: true, " 671 + "charDefaultsToC: C, " 672 + "floatDefaultsToPi: 3.14, " 673 + "doubleWithDefault: -3.14E17, " 674 + "arrayDefaultsTo123: [1, 2, 3], " 675 + "arrayDefaultsToEmpty: [], " 676 + "boolDefault: false, " 677 + "byteDefault: 0, " 678 + "intDefault: 0, " 679 + "longDefault: 0, " 680 + "floatDefault: 0.0, " 681 + "doubleDefault: 0.0, " 682 + "checkDoubleFromFloat: 3.14, " 683 + "checkStringArray1: [a, b], " 684 + "checkStringArray2: [a, b], " 685 + "int32_min: -2147483648, " 686 + "int32_max: 2147483647, " 687 + "int64_max: 9223372036854775807, " 688 + "hexInt32_neg_1: -1, " 689 + "ibinder: null, " 690 + "int32_1: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, " 691 + "1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, " 692 + "1, 1, 1, 1], " 693 + "int64_1: [1, 1, 1, 1, 1, 1, 1, 1, 1, 1], " 694 + "hexInt32_pos_1: 1, " 695 + "hexInt64_pos_1: 1, " 696 + "const_exprs_1: 1, " 697 + "const_exprs_2: 1, " 698 + "const_exprs_3: 1, " 699 + "const_exprs_4: 1, " 700 + "const_exprs_5: 1, " 701 + "const_exprs_6: 1, " 702 + "const_exprs_7: 1, " 703 + "const_exprs_8: 1, " 704 + "const_exprs_9: 1, " 705 + "const_exprs_10: 1, " 706 + "addString1: hello world!, " 707 + "addString2: The quick brown fox jumps over the lazy dog., " 708 + "shouldSetBit0AndBit2: 5, " 709 + "u: android.aidl.tests.Union.ns([1, 2, 3]), " 710 + "shouldBeConstS1: android.aidl.tests.Union.s(a string constant in union), " 711 + "defaultWithFoo: 1000" 712 + "}"; 713 assertThat(p.toString(), is(expected)); 714 } 715 716 @Test testDefaultImpl()717 public void testDefaultImpl() throws RemoteException { 718 final int expectedArg = 100; 719 final int expectedReturnValue = 200; 720 721 boolean success = ITestService.Stub.setDefaultImpl(new ITestService.Default() { 722 @Override 723 public int UnimplementedMethod(int arg) throws RemoteException { 724 if (arg != expectedArg) { 725 throw new RemoteException("Argument for UnimplementedMethod is expected " 726 + " to be " + expectedArg + ", but got " + arg); 727 } 728 return expectedReturnValue; 729 } 730 }); 731 assertThat(success, is(true)); 732 733 int ret = service.UnimplementedMethod(expectedArg); 734 assertThat(ret, is(expectedReturnValue)); 735 } 736 737 @Test testToString()738 public void testToString() { 739 ParcelableForToString p = new ParcelableForToString(); 740 p.intValue = 10; 741 p.intArray = new int[]{20, 30}; 742 p.longValue = 100L; 743 p.longArray = new long[]{200L, 300L}; 744 p.doubleValue = 3.14d; 745 p.doubleArray = new double[]{1.1d, 1.2d}; 746 p.floatValue = 3.14f; 747 p.floatArray = new float[]{1.1f, 1.2f}; 748 p.byteValue = 3; 749 p.byteArray = new byte[]{5, 6}; 750 p.booleanValue = true; 751 p.booleanArray = new boolean[]{true, false}; 752 p.stringValue = "this is a string"; 753 p.stringArray = new String[]{"hello", "world"}; 754 p.stringList = Arrays.asList(new String[]{"alice", "bob"}); 755 OtherParcelableForToString op = new OtherParcelableForToString(); 756 op.field = "other"; 757 p.parcelableValue = op; 758 p.parcelableArray = new OtherParcelableForToString[]{op, op}; 759 p.enumValue = IntEnum.FOO; 760 p.enumArray = new int[]{IntEnum.FOO, IntEnum.BAR}; 761 p.nullArray = null; 762 p.nullList = null; 763 GenericStructuredParcelable<Integer, StructuredParcelable, Integer> gen = 764 new GenericStructuredParcelable<Integer, StructuredParcelable, Integer>(); 765 gen.a = 1; 766 gen.b = 2; 767 p.parcelableGeneric = gen; 768 p.unionValue = null; // for testing even though it is not @nullable in .aidl 769 770 final String expected = "android.aidl.tests.ParcelableForToString{" 771 + "intValue: 10, " 772 + "intArray: [20, 30], " 773 + "longValue: 100, " 774 + "longArray: [200, 300], " 775 + "doubleValue: 3.14, " 776 + "doubleArray: [1.1, 1.2], " 777 + "floatValue: 3.14, " 778 + "floatArray: [1.1, 1.2], " 779 + "byteValue: 3, " 780 + "byteArray: [5, 6], " 781 + "booleanValue: true, " 782 + "booleanArray: [true, false], " 783 + "stringValue: this is a string, " 784 + "stringArray: [hello, world], " 785 + "stringList: [alice, bob], " 786 + "parcelableValue: android.aidl.tests.OtherParcelableForToString{field: other}, " 787 + "parcelableArray: [" 788 + "android.aidl.tests.OtherParcelableForToString{field: other}, " 789 + "android.aidl.tests.OtherParcelableForToString{field: other}], " 790 + "enumValue: 1000, " 791 + "enumArray: [1000, 2000], " 792 + "nullArray: null, " 793 + "nullList: null, " 794 + "parcelableGeneric: android.aidl.tests.GenericStructuredParcelable{a: 1, b: 2}, " 795 + "unionValue: null" 796 + "}"; 797 798 assertThat(p.toString(), is(expected)); 799 } 800 801 @Test testRenamedInterface()802 public void testRenamedInterface() throws RemoteException { 803 IOldName oldAsOld = service.GetOldNameInterface(); 804 assertNotNull(oldAsOld); 805 assertThat(oldAsOld.DESCRIPTOR, is("android.aidl.tests.IOldName")); 806 assertThat(oldAsOld.RealName(), is("OldName")); 807 808 INewName newAsNew = service.GetNewNameInterface(); 809 assertNotNull(newAsNew); 810 assertThat(newAsNew.DESCRIPTOR, is("android.aidl.tests.IOldName")); 811 assertThat(oldAsOld.RealName(), is("OldName")); 812 813 IOldName newAsOld = IOldName.Stub.asInterface(service.GetNewNameInterface().asBinder()); 814 assertNotNull(newAsOld); 815 assertThat(newAsOld.DESCRIPTOR, is("android.aidl.tests.IOldName")); 816 assertThat(newAsOld.RealName(), is("NewName")); 817 818 INewName oldAsNew = INewName.Stub.asInterface(service.GetOldNameInterface().asBinder()); 819 assertNotNull(oldAsNew); 820 assertThat(oldAsNew.DESCRIPTOR, is("android.aidl.tests.IOldName")); 821 assertThat(oldAsNew.RealName(), is("OldName")); 822 } 823 824 @Test testReverseUnion()825 public void testReverseUnion() throws RemoteException { 826 assumeTrue(cpp_java_tests != null); 827 828 Union original = Union.ns(new int[] {1, 2, 3}); 829 Union repeated = new Union(); 830 831 Union reversed = cpp_java_tests.ReverseUnion(original, repeated); 832 833 assertNotNull(reversed); 834 assertThat(repeated.getNs(), is(new int[] {1, 2, 3})); 835 assertThat(reversed.getNs(), is(new int[] {3, 2, 1})); 836 } 837 } 838