blob: 273b8fc3773b837c68f2d3fde03b22f2f5bf161f [file] [log] [blame]
Hugo Benichid3da17a2016-12-07 14:49:55 +09001/*
2 * Copyright (C) 2016 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 com.android.server.connectivity;
18
Hugo Benichi5fcd0502017-07-25 21:57:51 +090019import static com.android.server.connectivity.NetworkNotificationManager.NotificationType.*;
lucaslind2e045e02019-01-24 15:55:30 +080020
Hugo Benichi5fcd0502017-07-25 21:57:51 +090021import static org.mockito.Mockito.any;
22import static org.mockito.Mockito.anyInt;
23import static org.mockito.Mockito.eq;
24import static org.mockito.Mockito.never;
25import static org.mockito.Mockito.reset;
26import static org.mockito.Mockito.times;
27import static org.mockito.Mockito.verify;
28import static org.mockito.Mockito.when;
29
Hugo Benichid3da17a2016-12-07 14:49:55 +090030import android.app.Notification;
31import android.app.NotificationManager;
32import android.content.Context;
33import android.content.pm.ApplicationInfo;
34import android.content.pm.PackageManager;
35import android.content.res.Resources;
36import android.net.NetworkCapabilities;
37import android.net.NetworkInfo;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090038import android.support.test.filters.SmallTest;
lucaslind2e045e02019-01-24 15:55:30 +080039import android.support.test.runner.AndroidJUnit4;
Hugo Benichid3da17a2016-12-07 14:49:55 +090040import android.telephony.TelephonyManager;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090041
Hugo Benichid3da17a2016-12-07 14:49:55 +090042import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090043
lucaslind2e045e02019-01-24 15:55:30 +080044import org.junit.Before;
45import org.junit.Test;
46import org.junit.runner.RunWith;
47import org.mockito.ArgumentCaptor;
48import org.mockito.Mock;
49import org.mockito.MockitoAnnotations;
50
Hugo Benichid3da17a2016-12-07 14:49:55 +090051import java.util.ArrayList;
52import java.util.Arrays;
53import java.util.Collections;
54import java.util.List;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090055
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090056@RunWith(AndroidJUnit4.class)
57@SmallTest
58public class NetworkNotificationManagerTest {
Hugo Benichid3da17a2016-12-07 14:49:55 +090059
Hugo Benichid3da17a2016-12-07 14:49:55 +090060 static final NetworkCapabilities CELL_CAPABILITIES = new NetworkCapabilities();
61 static final NetworkCapabilities WIFI_CAPABILITIES = new NetworkCapabilities();
62 static {
63 CELL_CAPABILITIES.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
64 CELL_CAPABILITIES.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
65
66 WIFI_CAPABILITIES.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
67 WIFI_CAPABILITIES.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
68 }
69
70 @Mock Context mCtx;
71 @Mock Resources mResources;
72 @Mock PackageManager mPm;
73 @Mock TelephonyManager mTelephonyManager;
74 @Mock NotificationManager mNotificationManager;
75 @Mock NetworkAgentInfo mWifiNai;
76 @Mock NetworkAgentInfo mCellNai;
77 @Mock NetworkInfo mNetworkInfo;
78 ArgumentCaptor<Notification> mCaptor;
79
80 NetworkNotificationManager mManager;
81
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090082 @Before
Hugo Benichid3da17a2016-12-07 14:49:55 +090083 public void setUp() {
84 MockitoAnnotations.initMocks(this);
85 mCaptor = ArgumentCaptor.forClass(Notification.class);
86 mWifiNai.networkCapabilities = WIFI_CAPABILITIES;
87 mWifiNai.networkInfo = mNetworkInfo;
88 mCellNai.networkCapabilities = CELL_CAPABILITIES;
89 mCellNai.networkInfo = mNetworkInfo;
90 when(mCtx.getResources()).thenReturn(mResources);
91 when(mCtx.getPackageManager()).thenReturn(mPm);
92 when(mCtx.getApplicationInfo()).thenReturn(new ApplicationInfo());
Hugo Benichia8657902016-12-20 09:57:43 +090093 when(mNetworkInfo.getExtraInfo()).thenReturn("extra");
Hugo Benichid3da17a2016-12-07 14:49:55 +090094 when(mResources.getColor(anyInt(), any())).thenReturn(0xFF607D8B);
95
96 mManager = new NetworkNotificationManager(mCtx, mTelephonyManager, mNotificationManager);
97 }
98
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090099 @Test
Hugo Benichid3da17a2016-12-07 14:49:55 +0900100 public void testNotificationsShownAndCleared() {
101 final int NETWORK_ID_BASE = 100;
102 List<NotificationType> types = Arrays.asList(NotificationType.values());
103 List<Integer> ids = new ArrayList<>(types.size());
Hugo Benichie6181b02017-03-23 12:32:27 +0900104 for (int i = 0; i < types.size(); i++) {
Hugo Benichid3da17a2016-12-07 14:49:55 +0900105 ids.add(NETWORK_ID_BASE + i);
106 }
107 Collections.shuffle(ids);
108 Collections.shuffle(types);
109
110 for (int i = 0; i < ids.size(); i++) {
111 mManager.showNotification(ids.get(i), types.get(i), mWifiNai, mCellNai, null, false);
112 }
113
Hugo Benichie6181b02017-03-23 12:32:27 +0900114 List<Integer> idsToClear = new ArrayList<>(ids);
115 Collections.shuffle(idsToClear);
Hugo Benichid3da17a2016-12-07 14:49:55 +0900116 for (int i = 0; i < ids.size(); i++) {
Hugo Benichie6181b02017-03-23 12:32:27 +0900117 mManager.clearNotification(idsToClear.get(i));
Hugo Benichid3da17a2016-12-07 14:49:55 +0900118 }
119
120 for (int i = 0; i < ids.size(); i++) {
Hugo Benichi84daaea2016-12-08 09:36:52 +0900121 final int id = ids.get(i);
122 final int eventId = types.get(i).eventId;
123 final String tag = NetworkNotificationManager.tagFor(id);
124 verify(mNotificationManager, times(1)).notifyAsUser(eq(tag), eq(eventId), any(), any());
125 verify(mNotificationManager, times(1)).cancelAsUser(eq(tag), eq(eventId), any());
Hugo Benichid3da17a2016-12-07 14:49:55 +0900126 }
127 }
128
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900129 @Test
Hugo Benichid3da17a2016-12-07 14:49:55 +0900130 public void testNoInternetNotificationsNotShownForCellular() {
131 mManager.showNotification(100, NO_INTERNET, mCellNai, mWifiNai, null, false);
132 mManager.showNotification(101, LOST_INTERNET, mCellNai, mWifiNai, null, false);
133
134 verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), any());
135
136 mManager.showNotification(102, NO_INTERNET, mWifiNai, mCellNai, null, false);
137
Hugo Benichi84daaea2016-12-08 09:36:52 +0900138 final int eventId = NO_INTERNET.eventId;
139 final String tag = NetworkNotificationManager.tagFor(102);
140 verify(mNotificationManager, times(1)).notifyAsUser(eq(tag), eq(eventId), any(), any());
Hugo Benichid3da17a2016-12-07 14:49:55 +0900141 }
142
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900143 @Test
Hugo Benichid3da17a2016-12-07 14:49:55 +0900144 public void testNotificationsNotShownIfNoInternetCapability() {
145 mWifiNai.networkCapabilities = new NetworkCapabilities();
146 mWifiNai.networkCapabilities .addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
147 mManager.showNotification(102, NO_INTERNET, mWifiNai, mCellNai, null, false);
148 mManager.showNotification(103, LOST_INTERNET, mWifiNai, mCellNai, null, false);
149 mManager.showNotification(104, NETWORK_SWITCH, mWifiNai, mCellNai, null, false);
150
151 verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), any());
152 }
Hugo Benichi5fcd0502017-07-25 21:57:51 +0900153
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900154 @Test
Hugo Benichi5fcd0502017-07-25 21:57:51 +0900155 public void testDuplicatedNotificationsNoInternetThenSignIn() {
156 final int id = 101;
157 final String tag = NetworkNotificationManager.tagFor(id);
158
159 // Show first NO_INTERNET
160 mManager.showNotification(id, NO_INTERNET, mWifiNai, mCellNai, null, false);
161 verify(mNotificationManager, times(1))
162 .notifyAsUser(eq(tag), eq(NO_INTERNET.eventId), any(), any());
163
164 // Captive portal detection triggers SIGN_IN a bit later, clearing the previous NO_INTERNET
165 mManager.showNotification(id, SIGN_IN, mWifiNai, mCellNai, null, false);
166 verify(mNotificationManager, times(1))
167 .cancelAsUser(eq(tag), eq(NO_INTERNET.eventId), any());
168 verify(mNotificationManager, times(1))
169 .notifyAsUser(eq(tag), eq(SIGN_IN.eventId), any(), any());
170
171 // Network disconnects
172 mManager.clearNotification(id);
173 verify(mNotificationManager, times(1)).cancelAsUser(eq(tag), eq(SIGN_IN.eventId), any());
174 }
175
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900176 @Test
Hugo Benichi5fcd0502017-07-25 21:57:51 +0900177 public void testDuplicatedNotificationsSignInThenNoInternet() {
178 final int id = 101;
179 final String tag = NetworkNotificationManager.tagFor(id);
180
181 // Show first SIGN_IN
182 mManager.showNotification(id, SIGN_IN, mWifiNai, mCellNai, null, false);
183 verify(mNotificationManager, times(1))
184 .notifyAsUser(eq(tag), eq(SIGN_IN.eventId), any(), any());
185 reset(mNotificationManager);
186
187 // NO_INTERNET arrives after, but is ignored.
188 mManager.showNotification(id, NO_INTERNET, mWifiNai, mCellNai, null, false);
189 verify(mNotificationManager, never()).cancelAsUser(any(), anyInt(), any());
190 verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), any());
191
192 // Network disconnects
193 mManager.clearNotification(id);
194 verify(mNotificationManager, times(1)).cancelAsUser(eq(tag), eq(SIGN_IN.eventId), any());
195 }
lucaslind2e045e02019-01-24 15:55:30 +0800196
197 @Test
198 public void testSameLevelNotifications() {
199 final int id = 101;
200 final String tag = NetworkNotificationManager.tagFor(id);
201
202 mManager.showNotification(id, LOGGED_IN, mWifiNai, mCellNai, null, false);
203 verify(mNotificationManager, times(1))
204 .notifyAsUser(eq(tag), eq(LOGGED_IN.eventId), any(), any());
205
206 mManager.showNotification(id, LOST_INTERNET, mWifiNai, mCellNai, null, false);
207 verify(mNotificationManager, times(1))
208 .notifyAsUser(eq(tag), eq(LOST_INTERNET.eventId), any(), any());
209 }
210
211 @Test
212 public void testClearNotificationByType() {
213 final int id = 101;
214 final String tag = NetworkNotificationManager.tagFor(id);
215
216 // clearNotification(int id, NotificationType notifyType) will check if given type is equal
217 // to previous type or not. If they are equal then clear the notification; if they are not
218 // equal then return.
219
220 mManager.showNotification(id, LOGGED_IN, mWifiNai, mCellNai, null, false);
221 verify(mNotificationManager, times(1))
222 .notifyAsUser(eq(tag), eq(LOGGED_IN.eventId), any(), any());
223
224 // Previous notification is LOGGED_IN and given type is LOGGED_IN too. The notification
225 // should be cleared.
226 mManager.clearNotification(id, LOGGED_IN);
227 verify(mNotificationManager, times(1))
228 .cancelAsUser(eq(tag), eq(LOGGED_IN.eventId), any());
229
230 mManager.showNotification(id, LOGGED_IN, mWifiNai, mCellNai, null, false);
231 verify(mNotificationManager, times(2))
232 .notifyAsUser(eq(tag), eq(LOGGED_IN.eventId), any(), any());
233
234 // LOST_INTERNET notification popup after LOGGED_IN notification.
235 mManager.showNotification(id, LOST_INTERNET, mWifiNai, mCellNai, null, false);
236 verify(mNotificationManager, times(1))
237 .notifyAsUser(eq(tag), eq(LOST_INTERNET.eventId), any(), any());
238
239 // Previous notification is LOST_INTERNET and given type is LOGGED_IN. The notification
240 // shouldn't be cleared.
241 mManager.clearNotification(id, LOGGED_IN);
242 // LOST_INTERNET shouldn't be cleared.
243 verify(mNotificationManager, never())
244 .cancelAsUser(eq(tag), eq(LOST_INTERNET.eventId), any());
245 }
Hugo Benichid3da17a2016-12-07 14:49:55 +0900246}