blob: 5a9b6e380ea9aa848d7a552741c919018c4d0d09 [file] [log] [blame]
Erik Klinede637722017-10-12 22:16:01 +09001/*
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
17package android.net.util;
18
19import static org.junit.Assert.assertEquals;
20import static org.mockito.Mockito.reset;
21
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.os.Handler;
26import android.os.Looper;
27import android.os.UserHandle;
28
Brett Chabot1ae2aa62019-03-04 14:14:56 -080029import androidx.test.filters.SmallTest;
30import androidx.test.runner.AndroidJUnit4;
Erik Klinede637722017-10-12 22:16:01 +090031
32import com.android.internal.util.test.BroadcastInterceptingContext;
33
34import org.junit.After;
35import org.junit.Before;
36import org.junit.BeforeClass;
Erik Klinede637722017-10-12 22:16:01 +090037import org.junit.Test;
Brett Chabot1ae2aa62019-03-04 14:14:56 -080038import org.junit.runner.RunWith;
Erik Klinede637722017-10-12 22:16:01 +090039import org.mockito.Mock;
Erik Klinede637722017-10-12 22:16:01 +090040import org.mockito.MockitoAnnotations;
41
Erik Klinede637722017-10-12 22:16:01 +090042@RunWith(AndroidJUnit4.class)
43@SmallTest
44public 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
markchien0df2ebc42019-09-30 14:40:57 +080054 private void doCallback() {
55 mCallbackCount++;
56 }
Erik Klinede637722017-10-12 22:16:01 +090057
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();
markchien0df2ebc42019-09-30 14:40:57 +0800101 assertEquals(i + 1, mCallbackCount);
Erik Klinede637722017-10-12 22:16:01 +0900102 }
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}