• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2016, 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.junit.Assert.assertNotNull;
20 import static org.junit.Assert.assertThat;
21 import static org.junit.Assert.fail;
22 import static org.junit.Assume.assumeTrue;
23 import static org.hamcrest.core.Is.is;
24 import static org.hamcrest.core.IsNull.nullValue;
25 import static org.hamcrest.core.IsNull.notNullValue;
26 
27 import android.os.IBinder;
28 import android.os.RemoteException;
29 import android.os.ServiceManager;
30 import java.util.ArrayList;
31 import java.util.List;
32 
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runners.JUnit4;
36 import org.junit.runner.RunWith;
37 
38 @RunWith(JUnit4.class)
39 public class NullableTests {
40     private ITestService mService;
41     private ICppJavaTests mCppJavaTests;
42 
43     @Before
setUp()44     public void setUp() throws RemoteException {
45         IBinder binder = ServiceManager.waitForService(ITestService.class.getName());
46         assertNotNull(binder);
47         mService = ITestService.Stub.asInterface(binder);
48         assertNotNull(mService);
49 
50         IBinder binder2 = mService.GetCppJavaTests();
51         if (binder2 != null) {
52             mCppJavaTests = ICppJavaTests.Stub.asInterface(binder2);
53         }
54     }
55 
56     @Test
testExpectNpeWithNullString()57     public void testExpectNpeWithNullString() throws RemoteException {
58         try {
59             String response = mService.RepeatString(null);
60         } catch (NullPointerException ex) {
61             return;
62         }
63         if (mService.getBackendType() != BackendType.JAVA) {
64           fail("NullPointerException was expected, but wasn't thrown");
65         }
66     }
67 
68     @Test
testExpectNpeWithNullBinder()69     public void testExpectNpeWithNullBinder() throws RemoteException {
70         try {
71             mService.TakesAnIBinder(null);
72         } catch (NullPointerException ex) {
73             return;
74         }
75 
76         if (mService.getBackendType() != BackendType.JAVA) {
77           fail("NullPointerException was expected, but wasn't thrown");
78         }
79     }
80 
81     @Test
testExpectNpeWithNullBinderList()82     public void testExpectNpeWithNullBinderList() throws RemoteException {
83         assumeTrue(mCppJavaTests != null);
84 
85         List<IBinder> listWithNulls = new ArrayList<IBinder>();
86         listWithNulls.add(null);
87         try {
88             mCppJavaTests.TakesAnIBinderList(listWithNulls);
89         } catch (NullPointerException ex) {
90             return;
91         }
92         if (mService.getBackendType() != BackendType.JAVA) {
93           fail("NullPointerException was expected, but wasn't thrown");
94         }
95     }
96 
97     @Test
testNullableIInterface()98     public void testNullableIInterface() throws RemoteException {
99         INamedCallback callback  = mService.GetCallback(false);
100         assertThat(callback, is(notNullValue()));
101 
102         callback = mService.GetCallback(true);
103         assertThat(callback, is(nullValue()));
104     }
105 }
106