1 /*
2  * Copyright (C) 2020, 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.junit.Assert.assertNotNull;
21 import static org.junit.Assert.assertThat;
22 import static org.junit.Assert.assertTrue;
23 
24 import android.aidl.versioned.tests.BazUnion;
25 import android.aidl.versioned.tests.Foo;
26 import android.aidl.versioned.tests.IFooInterface;
27 import android.os.IBinder;
28 import android.os.RemoteException;
29 import android.os.ServiceManager;
30 import org.junit.Before;
31 import org.junit.Rule;
32 import org.junit.Test;
33 import org.junit.rules.ExpectedException;
34 import org.junit.runner.RunWith;
35 import org.junit.runners.JUnit4;
36 
37 @RunWith(JUnit4.class)
38 public class TestVersionedInterface {
39     private IFooInterface service;
40 
41     @Before
setUp()42     public void setUp() {
43         IBinder binder = ServiceManager.waitForService(IFooInterface.class.getName());
44         assertNotNull(binder);
45         service = IFooInterface.Stub.asInterface(binder);
46         assertNotNull(service);
47     }
48 
49     @Test
testGetInterfaceVersion()50     public void testGetInterfaceVersion() throws RemoteException {
51         assertThat(service.getInterfaceVersion(), is(1));
52     }
53 
54     @Test
testGetInterfaceHash()55     public void testGetInterfaceHash() throws RemoteException {
56       assertThat(service.getInterfaceHash(), is("9e7be1859820c59d9d55dd133e71a3687b5d2e5b"));
57     }
58 
59     @Rule public ExpectedException expectedException = ExpectedException.none();
60 
61     @Test
testUnimplementedMethodTriggersException()62     public void testUnimplementedMethodTriggersException() throws RemoteException {
63       expectedException.expect(RemoteException.class);
64       expectedException.expectMessage("Method newApi is unimplemented.");
65 
66       service.newApi();
67     }
68 
69     @Test
testOldServerAcceptsUnionWithOldField()70     public void testOldServerAcceptsUnionWithOldField() throws RemoteException {
71       assertThat(service.acceptUnionAndReturnString(BazUnion.intNum(42)), is("42"));
72     }
73 
74     @Test
testUnknownUnionFieldTriggersException()75     public void testUnknownUnionFieldTriggersException() throws RemoteException {
76       expectedException.expect(IllegalArgumentException.class);
77 
78       service.acceptUnionAndReturnString(BazUnion.longNum(42L));
79     }
80 
81     @Test
testArrayOfPacelableWithNewField()82     public void testArrayOfPacelableWithNewField() throws RemoteException {
83       Foo[] foos = new Foo[42];
84       for (int i = 0; i < foos.length; i++) {
85         foos[i] = new Foo();
86       }
87       int length = service.returnsLengthOfFooArray(foos);
88       assertThat(length, is(foos.length));
89     }
90 
91     @Test
testReadDataCorrectlyAfterParcelableWithNewField()92     public void testReadDataCorrectlyAfterParcelableWithNewField() throws RemoteException {
93       Foo inFoo = new Foo();
94       Foo inoutFoo = new Foo();
95       Foo outFoo = new Foo();
96       int ret = service.ignoreParcelablesAndRepeatInt(inFoo, inoutFoo, outFoo, 43);
97       assertThat(ret, is(43));
98     }
99 }
100