Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.net.util; |
| 18 | |
| 19 | import static org.junit.Assert.assertEquals; |
| 20 | import static org.mockito.Mockito.reset; |
| 21 | |
| 22 | import android.content.Context; |
| 23 | import android.content.Intent; |
| 24 | import android.content.IntentFilter; |
| 25 | import android.os.Handler; |
| 26 | import android.os.Looper; |
| 27 | import android.os.UserHandle; |
| 28 | |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 29 | import androidx.test.filters.SmallTest; |
| 30 | import androidx.test.runner.AndroidJUnit4; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 31 | |
| 32 | import com.android.internal.util.test.BroadcastInterceptingContext; |
| 33 | |
| 34 | import org.junit.After; |
| 35 | import org.junit.Before; |
| 36 | import org.junit.BeforeClass; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 37 | import org.junit.Test; |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 38 | import org.junit.runner.RunWith; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 39 | import org.mockito.Mock; |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 40 | import org.mockito.MockitoAnnotations; |
| 41 | |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 42 | @RunWith(AndroidJUnit4.class) |
| 43 | @SmallTest |
| 44 | public class VersionedBroadcastListenerTest { |
| 45 | private static final String TAG = VersionedBroadcastListenerTest.class.getSimpleName(); |
| 46 | private static final String ACTION_TEST = "action.test.happy.broadcasts"; |
| 47 | |
| 48 | @Mock private Context mContext; |
| 49 | private BroadcastInterceptingContext mServiceContext; |
| 50 | private Handler mHandler; |
| 51 | private VersionedBroadcastListener mListener; |
| 52 | private int mCallbackCount; |
| 53 | |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 54 | private void doCallback() { |
| 55 | mCallbackCount++; |
| 56 | } |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 57 | |
| 58 | private class MockContext extends BroadcastInterceptingContext { |
| 59 | MockContext(Context base) { |
| 60 | super(base); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @BeforeClass |
| 65 | public static void setUpBeforeClass() throws Exception { |
| 66 | if (Looper.myLooper() == null) { |
| 67 | Looper.prepare(); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | @Before public void setUp() throws Exception { |
| 72 | MockitoAnnotations.initMocks(this); |
| 73 | reset(mContext); |
| 74 | mServiceContext = new MockContext(mContext); |
| 75 | mHandler = new Handler(Looper.myLooper()); |
| 76 | mCallbackCount = 0; |
| 77 | final IntentFilter filter = new IntentFilter(); |
| 78 | filter.addAction(ACTION_TEST); |
| 79 | mListener = new VersionedBroadcastListener( |
| 80 | TAG, mServiceContext, mHandler, filter, (Intent intent) -> doCallback()); |
| 81 | } |
| 82 | |
| 83 | @After public void tearDown() throws Exception { |
| 84 | if (mListener != null) { |
| 85 | mListener.stopListening(); |
| 86 | mListener = null; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | private void sendBroadcast() { |
| 91 | final Intent intent = new Intent(ACTION_TEST); |
| 92 | mServiceContext.sendStickyBroadcastAsUser(intent, UserHandle.ALL); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testBasicListening() { |
| 97 | assertEquals(0, mCallbackCount); |
| 98 | mListener.startListening(); |
| 99 | for (int i = 0; i < 5; i++) { |
| 100 | sendBroadcast(); |
markchien | 0df2ebc4 | 2019-09-30 14:40:57 +0800 | [diff] [blame] | 101 | assertEquals(i + 1, mCallbackCount); |
Erik Kline | de63772 | 2017-10-12 22:16:01 +0900 | [diff] [blame] | 102 | } |
| 103 | mListener.stopListening(); |
| 104 | } |
| 105 | |
| 106 | @Test |
| 107 | public void testBroadcastsBeforeStartAreIgnored() { |
| 108 | assertEquals(0, mCallbackCount); |
| 109 | for (int i = 0; i < 5; i++) { |
| 110 | sendBroadcast(); |
| 111 | assertEquals(0, mCallbackCount); |
| 112 | } |
| 113 | |
| 114 | mListener.startListening(); |
| 115 | sendBroadcast(); |
| 116 | assertEquals(1, mCallbackCount); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void testBroadcastsAfterStopAreIgnored() { |
| 121 | mListener.startListening(); |
| 122 | sendBroadcast(); |
| 123 | assertEquals(1, mCallbackCount); |
| 124 | mListener.stopListening(); |
| 125 | |
| 126 | for (int i = 0; i < 5; i++) { |
| 127 | sendBroadcast(); |
| 128 | assertEquals(1, mCallbackCount); |
| 129 | } |
| 130 | } |
| 131 | } |