blob: 3567d17ea8742c6e94710fad938cd357bc763a13 [file] [log] [blame]
Olivier Gaillard510cdfc2018-09-17 09:35:32 +01001/*
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
17package android.os;
18
19import android.annotation.Nullable;
Yo Chiangb0316852020-01-21 16:10:20 +080020import android.content.ComponentName;
Olivier Gaillard510cdfc2018-09-17 09:35:32 +010021import android.content.Context;
Yo Chiangb0316852020-01-21 16:10:20 +080022import android.content.Intent;
23import android.content.ServiceConnection;
Olivier Gaillard510cdfc2018-09-17 09:35:32 +010024import android.test.AndroidTestCase;
Tadashi G. Takaokab4470f22019-01-15 18:29:15 +090025
26import androidx.test.filters.MediumTest;
Olivier Gaillard510cdfc2018-09-17 09:35:32 +010027
Yo Chiangb0316852020-01-21 16:10:20 +080028import java.util.concurrent.CountDownLatch;
29import java.util.concurrent.TimeUnit;
30
Olivier Gaillard510cdfc2018-09-17 09:35:32 +010031public class BinderProxyTest extends AndroidTestCase {
32 private static class CountingListener implements Binder.ProxyTransactListener {
33 int mStartedCount;
34 int mEndedCount;
35
36 public Object onTransactStarted(IBinder binder, int transactionCode) {
37 mStartedCount++;
38 return null;
39 }
40
41 public void onTransactEnded(@Nullable Object session) {
42 mEndedCount++;
43 }
44 };
45
46 private PowerManager mPowerManager;
47
48 /**
49 * Setup any common data for the upcoming tests.
50 */
51 @Override
52 public void setUp() throws Exception {
53 super.setUp();
54 mPowerManager = (PowerManager) mContext.getSystemService(Context.POWER_SERVICE);
55 }
56
57 @MediumTest
58 public void testNoListener() throws Exception {
59 CountingListener listener = new CountingListener();
60 Binder.setProxyTransactListener(listener);
61 Binder.setProxyTransactListener(null);
62
63 mPowerManager.isInteractive();
64
65 assertEquals(0, listener.mStartedCount);
66 assertEquals(0, listener.mEndedCount);
67 }
68
69 @MediumTest
70 public void testListener() throws Exception {
71 CountingListener listener = new CountingListener();
72 Binder.setProxyTransactListener(listener);
73
74 mPowerManager.isInteractive();
75
76 assertEquals(1, listener.mStartedCount);
77 assertEquals(1, listener.mEndedCount);
78 }
79
80 @MediumTest
81 public void testSessionPropagated() throws Exception {
82 Binder.setProxyTransactListener(new Binder.ProxyTransactListener() {
83 public Object onTransactStarted(IBinder binder, int transactionCode) {
84 return "foo";
85 }
86
87 public void onTransactEnded(@Nullable Object session) {
88 assertEquals("foo", session);
89 }
90 });
91
92 // Check it does not throw..
93 mPowerManager.isInteractive();
94 }
Yo Chiangb0316852020-01-21 16:10:20 +080095
96 private IBinder mRemoteBinder = null;
97
98 @MediumTest
99 public void testGetExtension() throws Exception {
100 final CountDownLatch bindLatch = new CountDownLatch(1);
101 ServiceConnection connection =
102 new ServiceConnection() {
103 @Override
104 public void onServiceConnected(ComponentName name, IBinder service) {
105 mRemoteBinder = service;
106 bindLatch.countDown();
107 }
108
109 @Override
110 public void onServiceDisconnected(ComponentName name) {}
111 };
112 try {
113 mContext.bindService(
114 new Intent(mContext, BinderProxyService.class),
115 connection,
116 Context.BIND_AUTO_CREATE);
117 if (!bindLatch.await(500, TimeUnit.MILLISECONDS)) {
118 fail(
119 "Timed out while binding service: "
120 + BinderProxyService.class.getSimpleName());
121 }
122 assertTrue(mRemoteBinder instanceof BinderProxy);
123 assertNotNull(mRemoteBinder);
124
125 IBinder extension = mRemoteBinder.getExtension();
126 assertNotNull(extension);
127 assertTrue(extension.pingBinder());
128 } finally {
129 mContext.unbindService(connection);
130 }
131 }
Olivier Gaillard510cdfc2018-09-17 09:35:32 +0100132}