Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 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 android.annotation.Nullable; |
Yo Chiang | b031685 | 2020-01-21 16:10:20 +0800 | [diff] [blame^] | 20 | import android.content.ComponentName; |
Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 21 | import android.content.Context; |
Yo Chiang | b031685 | 2020-01-21 16:10:20 +0800 | [diff] [blame^] | 22 | import android.content.Intent; |
| 23 | import android.content.ServiceConnection; |
Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 24 | import android.test.AndroidTestCase; |
Tadashi G. Takaoka | b4470f2 | 2019-01-15 18:29:15 +0900 | [diff] [blame] | 25 | |
| 26 | import androidx.test.filters.MediumTest; |
Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 27 | |
Yo Chiang | b031685 | 2020-01-21 16:10:20 +0800 | [diff] [blame^] | 28 | import java.util.concurrent.CountDownLatch; |
| 29 | import java.util.concurrent.TimeUnit; |
| 30 | |
Olivier Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 31 | public 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 Chiang | b031685 | 2020-01-21 16:10:20 +0800 | [diff] [blame^] | 95 | |
| 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 Gaillard | 510cdfc | 2018-09-17 09:35:32 +0100 | [diff] [blame] | 132 | } |