blob: 95807638f6c479d5c3ba320edbaa70b614911604 [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;
38import android.telephony.TelephonyManager;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090039
Brett Chabot1ae2aa62019-03-04 14:14:56 -080040import androidx.test.filters.SmallTest;
41import androidx.test.runner.AndroidJUnit4;
42
Hugo Benichid3da17a2016-12-07 14:49:55 +090043import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090044
lucaslind2e045e02019-01-24 15:55:30 +080045import org.junit.Before;
46import org.junit.Test;
47import org.junit.runner.RunWith;
48import org.mockito.ArgumentCaptor;
49import org.mockito.Mock;
50import org.mockito.MockitoAnnotations;
51
Hugo Benichid3da17a2016-12-07 14:49:55 +090052import java.util.ArrayList;
53import java.util.Arrays;
54import java.util.Collections;
55import java.util.List;
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090056
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090057@RunWith(AndroidJUnit4.class)
58@SmallTest
59public class NetworkNotificationManagerTest {
Hugo Benichid3da17a2016-12-07 14:49:55 +090060
Hugo Benichid3da17a2016-12-07 14:49:55 +090061 static final NetworkCapabilities CELL_CAPABILITIES = new NetworkCapabilities();
62 static final NetworkCapabilities WIFI_CAPABILITIES = new NetworkCapabilities();
63 static {
64 CELL_CAPABILITIES.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
65 CELL_CAPABILITIES.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
66
67 WIFI_CAPABILITIES.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
68 WIFI_CAPABILITIES.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
69 }
70
71 @Mock Context mCtx;
72 @Mock Resources mResources;
73 @Mock PackageManager mPm;
74 @Mock TelephonyManager mTelephonyManager;
75 @Mock NotificationManager mNotificationManager;
76 @Mock NetworkAgentInfo mWifiNai;
77 @Mock NetworkAgentInfo mCellNai;
78 @Mock NetworkInfo mNetworkInfo;
79 ArgumentCaptor<Notification> mCaptor;
80
81 NetworkNotificationManager mManager;
82
Hugo Benichi4a0c5d72017-10-11 11:26:25 +090083 @Before
Hugo Benichid3da17a2016-12-07 14:49:55 +090084 public void setUp() {
85 MockitoAnnotations.initMocks(this);
86 mCaptor = ArgumentCaptor.forClass(Notification.class);
87 mWifiNai.networkCapabilities = WIFI_CAPABILITIES;
88 mWifiNai.networkInfo = mNetworkInfo;
89 mCellNai.networkCapabilities = CELL_CAPABILITIES;
90 mCellNai.networkInfo = mNetworkInfo;
91 when(mCtx.getResources()).thenReturn(mResources);
92 when(mCtx.getPackageManager()).thenReturn(mPm);
93 when(mCtx.getApplicationInfo()).thenReturn(new ApplicationInfo());
Hugo Benichia8657902016-12-20 09:57:43 +090094 when(mNetworkInfo.getExtraInfo()).thenReturn("extra");
Hugo Benichid3da17a2016-12-07 14:49:55 +090095 when(mResources.getColor(anyInt(), any())).thenReturn(0xFF607D8B);
96
97 mManager = new NetworkNotificationManager(mCtx, mTelephonyManager, mNotificationManager);
98 }
99
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900100 @Test
Hugo Benichid3da17a2016-12-07 14:49:55 +0900101 public void testNotificationsShownAndCleared() {
102 final int NETWORK_ID_BASE = 100;
103 List<NotificationType> types = Arrays.asList(NotificationType.values());
104 List<Integer> ids = new ArrayList<>(types.size());
Hugo Benichie6181b02017-03-23 12:32:27 +0900105 for (int i = 0; i < types.size(); i++) {
Hugo Benichid3da17a2016-12-07 14:49:55 +0900106 ids.add(NETWORK_ID_BASE + i);
107 }
108 Collections.shuffle(ids);
109 Collections.shuffle(types);
110
111 for (int i = 0; i < ids.size(); i++) {
112 mManager.showNotification(ids.get(i), types.get(i), mWifiNai, mCellNai, null, false);
113 }
114
Hugo Benichie6181b02017-03-23 12:32:27 +0900115 List<Integer> idsToClear = new ArrayList<>(ids);
116 Collections.shuffle(idsToClear);
Hugo Benichid3da17a2016-12-07 14:49:55 +0900117 for (int i = 0; i < ids.size(); i++) {
Hugo Benichie6181b02017-03-23 12:32:27 +0900118 mManager.clearNotification(idsToClear.get(i));
Hugo Benichid3da17a2016-12-07 14:49:55 +0900119 }
120
121 for (int i = 0; i < ids.size(); i++) {
Hugo Benichi84daaea2016-12-08 09:36:52 +0900122 final int id = ids.get(i);
123 final int eventId = types.get(i).eventId;
124 final String tag = NetworkNotificationManager.tagFor(id);
125 verify(mNotificationManager, times(1)).notifyAsUser(eq(tag), eq(eventId), any(), any());
126 verify(mNotificationManager, times(1)).cancelAsUser(eq(tag), eq(eventId), any());
Hugo Benichid3da17a2016-12-07 14:49:55 +0900127 }
128 }
129
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900130 @Test
Hugo Benichid3da17a2016-12-07 14:49:55 +0900131 public void testNoInternetNotificationsNotShownForCellular() {
132 mManager.showNotification(100, NO_INTERNET, mCellNai, mWifiNai, null, false);
133 mManager.showNotification(101, LOST_INTERNET, mCellNai, mWifiNai, null, false);
134
135 verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), any());
136
137 mManager.showNotification(102, NO_INTERNET, mWifiNai, mCellNai, null, false);
138
Hugo Benichi84daaea2016-12-08 09:36:52 +0900139 final int eventId = NO_INTERNET.eventId;
140 final String tag = NetworkNotificationManager.tagFor(102);
141 verify(mNotificationManager, times(1)).notifyAsUser(eq(tag), eq(eventId), any(), any());
Hugo Benichid3da17a2016-12-07 14:49:55 +0900142 }
143
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900144 @Test
Hugo Benichid3da17a2016-12-07 14:49:55 +0900145 public void testNotificationsNotShownIfNoInternetCapability() {
146 mWifiNai.networkCapabilities = new NetworkCapabilities();
147 mWifiNai.networkCapabilities .addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
148 mManager.showNotification(102, NO_INTERNET, mWifiNai, mCellNai, null, false);
149 mManager.showNotification(103, LOST_INTERNET, mWifiNai, mCellNai, null, false);
150 mManager.showNotification(104, NETWORK_SWITCH, mWifiNai, mCellNai, null, false);
151
152 verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), any());
153 }
Hugo Benichi5fcd0502017-07-25 21:57:51 +0900154
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900155 @Test
Hugo Benichi5fcd0502017-07-25 21:57:51 +0900156 public void testDuplicatedNotificationsNoInternetThenSignIn() {
157 final int id = 101;
158 final String tag = NetworkNotificationManager.tagFor(id);
159
160 // Show first NO_INTERNET
161 mManager.showNotification(id, NO_INTERNET, mWifiNai, mCellNai, null, false);
162 verify(mNotificationManager, times(1))
163 .notifyAsUser(eq(tag), eq(NO_INTERNET.eventId), any(), any());
164
165 // Captive portal detection triggers SIGN_IN a bit later, clearing the previous NO_INTERNET
166 mManager.showNotification(id, SIGN_IN, mWifiNai, mCellNai, null, false);
167 verify(mNotificationManager, times(1))
168 .cancelAsUser(eq(tag), eq(NO_INTERNET.eventId), any());
169 verify(mNotificationManager, times(1))
170 .notifyAsUser(eq(tag), eq(SIGN_IN.eventId), any(), any());
171
172 // Network disconnects
173 mManager.clearNotification(id);
174 verify(mNotificationManager, times(1)).cancelAsUser(eq(tag), eq(SIGN_IN.eventId), any());
175 }
176
Hugo Benichi4a0c5d72017-10-11 11:26:25 +0900177 @Test
Hugo Benichi5fcd0502017-07-25 21:57:51 +0900178 public void testDuplicatedNotificationsSignInThenNoInternet() {
179 final int id = 101;
180 final String tag = NetworkNotificationManager.tagFor(id);
181
182 // Show first SIGN_IN
183 mManager.showNotification(id, SIGN_IN, mWifiNai, mCellNai, null, false);
184 verify(mNotificationManager, times(1))
185 .notifyAsUser(eq(tag), eq(SIGN_IN.eventId), any(), any());
186 reset(mNotificationManager);
187
188 // NO_INTERNET arrives after, but is ignored.
189 mManager.showNotification(id, NO_INTERNET, mWifiNai, mCellNai, null, false);
190 verify(mNotificationManager, never()).cancelAsUser(any(), anyInt(), any());
191 verify(mNotificationManager, never()).notifyAsUser(any(), anyInt(), any(), any());
192
193 // Network disconnects
194 mManager.clearNotification(id);
195 verify(mNotificationManager, times(1)).cancelAsUser(eq(tag), eq(SIGN_IN.eventId), any());
196 }
lucaslind2e045e02019-01-24 15:55:30 +0800197
198 @Test
199 public void testSameLevelNotifications() {
200 final int id = 101;
201 final String tag = NetworkNotificationManager.tagFor(id);
202
203 mManager.showNotification(id, LOGGED_IN, mWifiNai, mCellNai, null, false);
204 verify(mNotificationManager, times(1))
205 .notifyAsUser(eq(tag), eq(LOGGED_IN.eventId), any(), any());
206
207 mManager.showNotification(id, LOST_INTERNET, mWifiNai, mCellNai, null, false);
208 verify(mNotificationManager, times(1))
209 .notifyAsUser(eq(tag), eq(LOST_INTERNET.eventId), any(), any());
210 }
211
212 @Test
213 public void testClearNotificationByType() {
214 final int id = 101;
215 final String tag = NetworkNotificationManager.tagFor(id);
216
217 // clearNotification(int id, NotificationType notifyType) will check if given type is equal
218 // to previous type or not. If they are equal then clear the notification; if they are not
219 // equal then return.
220
221 mManager.showNotification(id, LOGGED_IN, mWifiNai, mCellNai, null, false);
222 verify(mNotificationManager, times(1))
223 .notifyAsUser(eq(tag), eq(LOGGED_IN.eventId), any(), any());
224
225 // Previous notification is LOGGED_IN and given type is LOGGED_IN too. The notification
226 // should be cleared.
227 mManager.clearNotification(id, LOGGED_IN);
228 verify(mNotificationManager, times(1))
229 .cancelAsUser(eq(tag), eq(LOGGED_IN.eventId), any());
230
231 mManager.showNotification(id, LOGGED_IN, mWifiNai, mCellNai, null, false);
232 verify(mNotificationManager, times(2))
233 .notifyAsUser(eq(tag), eq(LOGGED_IN.eventId), any(), any());
234
235 // LOST_INTERNET notification popup after LOGGED_IN notification.
236 mManager.showNotification(id, LOST_INTERNET, mWifiNai, mCellNai, null, false);
237 verify(mNotificationManager, times(1))
238 .notifyAsUser(eq(tag), eq(LOST_INTERNET.eventId), any(), any());
239
240 // Previous notification is LOST_INTERNET and given type is LOGGED_IN. The notification
241 // shouldn't be cleared.
242 mManager.clearNotification(id, LOGGED_IN);
243 // LOST_INTERNET shouldn't be cleared.
244 verify(mNotificationManager, never())
245 .cancelAsUser(eq(tag), eq(LOST_INTERNET.eventId), any());
246 }
Hugo Benichid3da17a2016-12-07 14:49:55 +0900247}