Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server.connectivity; |
| 18 | |
Hugo Benichi | 5fcd050 | 2017-07-25 21:57:51 +0900 | [diff] [blame] | 19 | import static com.android.server.connectivity.NetworkNotificationManager.NotificationType.*; |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 20 | |
Hugo Benichi | 5fcd050 | 2017-07-25 21:57:51 +0900 | [diff] [blame] | 21 | import static org.mockito.Mockito.any; |
| 22 | import static org.mockito.Mockito.anyInt; |
| 23 | import static org.mockito.Mockito.eq; |
| 24 | import static org.mockito.Mockito.never; |
| 25 | import static org.mockito.Mockito.reset; |
| 26 | import static org.mockito.Mockito.times; |
| 27 | import static org.mockito.Mockito.verify; |
| 28 | import static org.mockito.Mockito.when; |
| 29 | |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 30 | import android.app.Notification; |
| 31 | import android.app.NotificationManager; |
| 32 | import android.content.Context; |
| 33 | import android.content.pm.ApplicationInfo; |
| 34 | import android.content.pm.PackageManager; |
| 35 | import android.content.res.Resources; |
| 36 | import android.net.NetworkCapabilities; |
| 37 | import android.net.NetworkInfo; |
| 38 | import android.telephony.TelephonyManager; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 39 | |
Brett Chabot | 1ae2aa6 | 2019-03-04 14:14:56 -0800 | [diff] [blame] | 40 | import androidx.test.filters.SmallTest; |
| 41 | import androidx.test.runner.AndroidJUnit4; |
| 42 | |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 43 | import com.android.server.connectivity.NetworkNotificationManager.NotificationType; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 44 | |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 45 | import org.junit.Before; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
| 48 | import org.mockito.ArgumentCaptor; |
| 49 | import org.mockito.Mock; |
| 50 | import org.mockito.MockitoAnnotations; |
| 51 | |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 52 | import java.util.ArrayList; |
| 53 | import java.util.Arrays; |
| 54 | import java.util.Collections; |
| 55 | import java.util.List; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 56 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 57 | @RunWith(AndroidJUnit4.class) |
| 58 | @SmallTest |
| 59 | public class NetworkNotificationManagerTest { |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 60 | |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 61 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 83 | @Before |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 84 | 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 Benichi | a865790 | 2016-12-20 09:57:43 +0900 | [diff] [blame] | 94 | when(mNetworkInfo.getExtraInfo()).thenReturn("extra"); |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 95 | when(mResources.getColor(anyInt(), any())).thenReturn(0xFF607D8B); |
| 96 | |
| 97 | mManager = new NetworkNotificationManager(mCtx, mTelephonyManager, mNotificationManager); |
| 98 | } |
| 99 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 100 | @Test |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 101 | 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 Benichi | e6181b0 | 2017-03-23 12:32:27 +0900 | [diff] [blame] | 105 | for (int i = 0; i < types.size(); i++) { |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 106 | 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 Benichi | e6181b0 | 2017-03-23 12:32:27 +0900 | [diff] [blame] | 115 | List<Integer> idsToClear = new ArrayList<>(ids); |
| 116 | Collections.shuffle(idsToClear); |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 117 | for (int i = 0; i < ids.size(); i++) { |
Hugo Benichi | e6181b0 | 2017-03-23 12:32:27 +0900 | [diff] [blame] | 118 | mManager.clearNotification(idsToClear.get(i)); |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | for (int i = 0; i < ids.size(); i++) { |
Hugo Benichi | 84daaea | 2016-12-08 09:36:52 +0900 | [diff] [blame] | 122 | 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 Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 130 | @Test |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 131 | 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 Benichi | 84daaea | 2016-12-08 09:36:52 +0900 | [diff] [blame] | 139 | 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 Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 142 | } |
| 143 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 144 | @Test |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 145 | 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 Benichi | 5fcd050 | 2017-07-25 21:57:51 +0900 | [diff] [blame] | 154 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 155 | @Test |
Hugo Benichi | 5fcd050 | 2017-07-25 21:57:51 +0900 | [diff] [blame] | 156 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 177 | @Test |
Hugo Benichi | 5fcd050 | 2017-07-25 21:57:51 +0900 | [diff] [blame] | 178 | 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 | } |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 197 | |
| 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 Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 247 | } |