1 /* 2 * Copyright (C) 2022, 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 23 import android.os.IBinder; 24 import android.os.RemoteException; 25 import android.os.ServiceManager; 26 import org.junit.Before; 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 import org.junit.runners.JUnit4; 30 31 @RunWith(JUnit4.class) 32 public class DelegatorTests { 33 static byte kFakeByte = 16; 34 static byte kExpectedByte = 12; 35 36 private class Custom extends ITestService.Delegator { Custom(ITestService impl)37 public Custom(ITestService impl) { super(impl); } 38 @Override RepeatByte(byte token)39 public byte RepeatByte(byte token) throws RemoteException { 40 return kFakeByte; 41 } 42 } 43 44 @Test testDelegator()45 public void testDelegator() throws RemoteException { 46 IBinder binder = ServiceManager.waitForService(ITestService.class.getName()); 47 assertNotNull(binder); 48 ITestService service = ITestService.Stub.asInterface(binder); 49 ITestService.Delegator delegator = new ITestService.Delegator(service); 50 assertThat(delegator.RepeatByte(kExpectedByte), is(kExpectedByte)); 51 } 52 53 @Test testCustomDelegator()54 public void testCustomDelegator() throws RemoteException { 55 IBinder binder = ServiceManager.waitForService(ITestService.class.getName()); 56 assertNotNull(binder); 57 ITestService service = ITestService.Stub.asInterface(binder); 58 ITestService.Delegator custom = new Custom(service); 59 assertThat(custom.RepeatByte(kExpectedByte), is(kFakeByte)); 60 } 61 62 @Test testAsBinderAsInterface()63 public void testAsBinderAsInterface() throws RemoteException { 64 IBinder binder = ServiceManager.waitForService(ITestService.class.getName()); 65 assertNotNull(binder); 66 ITestService service = ITestService.Stub.asInterface(binder); 67 ITestService.Delegator delegator = new ITestService.Delegator(service); 68 assertThat(ITestService.Delegator.asInterface(delegator.asBinder()).RepeatByte(kExpectedByte), 69 is(kExpectedByte)); 70 } 71 } 72