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; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 38 | import android.support.test.filters.SmallTest; |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 39 | import android.support.test.runner.AndroidJUnit4; |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 40 | import android.telephony.TelephonyManager; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 41 | |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 42 | import com.android.server.connectivity.NetworkNotificationManager.NotificationType; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 43 | |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 44 | import org.junit.Before; |
| 45 | import org.junit.Test; |
| 46 | import org.junit.runner.RunWith; |
| 47 | import org.mockito.ArgumentCaptor; |
| 48 | import org.mockito.Mock; |
| 49 | import org.mockito.MockitoAnnotations; |
| 50 | |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 51 | import java.util.ArrayList; |
| 52 | import java.util.Arrays; |
| 53 | import java.util.Collections; |
| 54 | import java.util.List; |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 55 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 56 | @RunWith(AndroidJUnit4.class) |
| 57 | @SmallTest |
| 58 | public class NetworkNotificationManagerTest { |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 59 | |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 60 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 82 | @Before |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 83 | 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 Benichi | a865790 | 2016-12-20 09:57:43 +0900 | [diff] [blame] | 93 | when(mNetworkInfo.getExtraInfo()).thenReturn("extra"); |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 94 | when(mResources.getColor(anyInt(), any())).thenReturn(0xFF607D8B); |
| 95 | |
| 96 | mManager = new NetworkNotificationManager(mCtx, mTelephonyManager, mNotificationManager); |
| 97 | } |
| 98 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 99 | @Test |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 100 | 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 Benichi | e6181b0 | 2017-03-23 12:32:27 +0900 | [diff] [blame] | 104 | for (int i = 0; i < types.size(); i++) { |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 105 | 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 Benichi | e6181b0 | 2017-03-23 12:32:27 +0900 | [diff] [blame] | 114 | List<Integer> idsToClear = new ArrayList<>(ids); |
| 115 | Collections.shuffle(idsToClear); |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 116 | for (int i = 0; i < ids.size(); i++) { |
Hugo Benichi | e6181b0 | 2017-03-23 12:32:27 +0900 | [diff] [blame] | 117 | mManager.clearNotification(idsToClear.get(i)); |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | for (int i = 0; i < ids.size(); i++) { |
Hugo Benichi | 84daaea | 2016-12-08 09:36:52 +0900 | [diff] [blame] | 121 | 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 Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 129 | @Test |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 130 | 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 Benichi | 84daaea | 2016-12-08 09:36:52 +0900 | [diff] [blame] | 138 | 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 Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 143 | @Test |
Hugo Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 144 | 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 Benichi | 5fcd050 | 2017-07-25 21:57:51 +0900 | [diff] [blame] | 153 | |
Hugo Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 154 | @Test |
Hugo Benichi | 5fcd050 | 2017-07-25 21:57:51 +0900 | [diff] [blame] | 155 | 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 Benichi | 4a0c5d7 | 2017-10-11 11:26:25 +0900 | [diff] [blame] | 176 | @Test |
Hugo Benichi | 5fcd050 | 2017-07-25 21:57:51 +0900 | [diff] [blame] | 177 | 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 | } |
lucaslin | d2e045e0 | 2019-01-24 15:55:30 +0800 | [diff] [blame] | 196 | |
| 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 Benichi | d3da17a | 2016-12-07 14:49:55 +0900 | [diff] [blame] | 246 | } |