1 /*
2  * Copyright (C) 2018 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.os;
18 
19 import androidx.test.filters.SmallTest;
20 
21 import junit.framework.TestCase;
22 
23 import static org.testng.Assert.assertThrows;
24 
25 public class BinderTest extends TestCase {
26     private static final int UID = 100;
27 
28     @SmallTest
testSetWorkSource()29     public void testSetWorkSource() throws Exception {
30         Binder.setCallingWorkSourceUid(UID);
31         assertEquals(UID, Binder.getCallingWorkSourceUid());
32     }
33 
34     @SmallTest
testClearWorkSource()35     public void testClearWorkSource() throws Exception {
36         Binder.setCallingWorkSourceUid(UID);
37         Binder.clearCallingWorkSource();
38         assertEquals(-1, Binder.getCallingWorkSourceUid());
39     }
40 
41     @SmallTest
testRestoreWorkSource()42     public void testRestoreWorkSource() throws Exception {
43         Binder.setCallingWorkSourceUid(UID);
44         long token = Binder.clearCallingWorkSource();
45         Binder.restoreCallingWorkSource(token);
46         assertEquals(UID, Binder.getCallingWorkSourceUid());
47     }
48 
49     @SmallTest
testGetCallingUidOrThrow_throws()50     public void testGetCallingUidOrThrow_throws() throws Exception {
51         assertThrows(IllegalStateException.class, () -> Binder.getCallingUidOrThrow());
52     }
53 
54     @SmallTest
testGetExtension()55     public void testGetExtension() throws Exception {
56         Binder binder = new Binder();
57         assertNull(binder.getExtension());
58 
59         IBinder extension = new Binder();
60         binder.setExtension(extension);
61         assertNotNull(binder.getExtension());
62         assertSame(binder.getExtension(), extension);
63 
64         binder.setExtension(null);
65         assertNull(binder.getExtension());
66     }
67 }
68