blob: 55108e7ac298b637197d2171a7d52b42580dfd4b [file] [log] [blame]
/*
* Copyright (C) 2016, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.connectivity;
import android.app.PendingIntent;
import android.content.Context;
import android.content.res.Resources;
import android.net.ConnectivityManager;
import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.NetworkInfo;
import android.net.NetworkMisc;
import com.android.internal.R;
import com.android.server.ConnectivityService;
import com.android.server.connectivity.NetworkNotificationManager;
import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
import junit.framework.TestCase;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.anyInt;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class LingerMonitorTest extends TestCase {
static final String CELLULAR = "CELLULAR";
static final String WIFI = "WIFI";
LingerMonitor mMonitor;
@Mock ConnectivityService mConnService;
@Mock Context mCtx;
@Mock NetworkMisc mMisc;
@Mock NetworkNotificationManager mNotifier;
@Mock Resources mResources;
public void setUp() {
MockitoAnnotations.initMocks(this);
when(mCtx.getResources()).thenReturn(mResources);
when(mCtx.getPackageName()).thenReturn("com.android.server.connectivity");
when(mConnService.createNetworkMonitor(any(), any(), any(), any())).thenReturn(null);
mMonitor = new TestableLingerMonitor(mCtx, mNotifier);
}
public void testTransitions() {
setNotificationSwitch(transition(WIFI, CELLULAR));
NetworkAgentInfo nai1 = wifiNai(100);
NetworkAgentInfo nai2 = cellNai(101);
assertTrue(mMonitor.isNotificationEnabled(nai1, nai2));
assertFalse(mMonitor.isNotificationEnabled(nai2, nai1));
}
public void testNotificationOnLinger() {
setNotificationSwitch(transition(WIFI, CELLULAR));
setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
NetworkAgentInfo from = wifiNai(100);
NetworkAgentInfo to = cellNai(101);
mMonitor.noteLingerDefaultNetwork(from, to);
verifyNotification(from, to);
}
public void testToastOnLinger() {
setNotificationSwitch(transition(WIFI, CELLULAR));
setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
NetworkAgentInfo from = wifiNai(100);
NetworkAgentInfo to = cellNai(101);
mMonitor.noteLingerDefaultNetwork(from, to);
verifyToast(from, to);
}
public void testNotificationClearedAfterDisconnect() {
setNotificationSwitch(transition(WIFI, CELLULAR));
setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
NetworkAgentInfo from = wifiNai(100);
NetworkAgentInfo to = cellNai(101);
mMonitor.noteLingerDefaultNetwork(from, to);
verifyNotification(from, to);
mMonitor.noteDisconnect(to);
verify(mNotifier, times(1)).clearNotification(100);
}
public void testNotificationClearedAfterSwitchingBack() {
setNotificationSwitch(transition(WIFI, CELLULAR));
setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
NetworkAgentInfo from = wifiNai(100);
NetworkAgentInfo to = cellNai(101);
mMonitor.noteLingerDefaultNetwork(from, to);
verifyNotification(from, to);
mMonitor.noteLingerDefaultNetwork(to, from);
verify(mNotifier, times(1)).clearNotification(100);
}
public void testUniqueToast() {
setNotificationSwitch(transition(WIFI, CELLULAR));
setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
NetworkAgentInfo from = wifiNai(100);
NetworkAgentInfo to = cellNai(101);
mMonitor.noteLingerDefaultNetwork(from, to);
verifyToast(from, to);
mMonitor.noteLingerDefaultNetwork(to, from);
verify(mNotifier, times(1)).clearNotification(100);
mMonitor.noteLingerDefaultNetwork(from, to);
verifyToast(from, to);
}
public void testUniqueNotification() {
setNotificationSwitch(transition(WIFI, CELLULAR));
setNotificationType(LingerMonitor.NOTIFY_TYPE_NOTIFICATION);
NetworkAgentInfo from = wifiNai(100);
NetworkAgentInfo to = cellNai(101);
mMonitor.noteLingerDefaultNetwork(from, to);
verifyNotification(from, to);
mMonitor.noteLingerDefaultNetwork(to, from);
verify(mNotifier, times(1)).clearNotification(100);
mMonitor.noteLingerDefaultNetwork(from, to);
verifyNotification(from, to);
}
public void testIgnoreUnvalidatedNetworks() {
setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
setNotificationSwitch(transition(WIFI, CELLULAR));
NetworkAgentInfo from = wifiNai(100);
NetworkAgentInfo to = cellNai(101);
from.everValidated = false;
mMonitor.noteLingerDefaultNetwork(from, to);
verifyNoNotifications();
}
public void testNoNotificationType() {
setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
setNotificationSwitch();
NetworkAgentInfo from = wifiNai(100);
NetworkAgentInfo to = cellNai(101);
mMonitor.noteLingerDefaultNetwork(from, to);
verifyNoNotifications();
}
public void testNoTransitionToNotify() {
setNotificationType(LingerMonitor.NOTIFY_TYPE_NONE);
setNotificationSwitch(transition(WIFI, CELLULAR));
NetworkAgentInfo from = wifiNai(100);
NetworkAgentInfo to = cellNai(101);
mMonitor.noteLingerDefaultNetwork(from, to);
verifyNoNotifications();
}
public void testDifferentTransitionToNotify() {
setNotificationType(LingerMonitor.NOTIFY_TYPE_TOAST);
setNotificationSwitch(transition(CELLULAR, WIFI));
NetworkAgentInfo from = wifiNai(100);
NetworkAgentInfo to = cellNai(101);
mMonitor.noteLingerDefaultNetwork(from, to);
verifyNoNotifications();
}
void setNotificationSwitch(String... transitions) {
when(mResources.getStringArray(R.array.config_networkNotifySwitches))
.thenReturn(transitions);
}
String transition(String from, String to) {
return from + "-" + to;
}
void setNotificationType(int type) {
when(mResources.getInteger(R.integer.config_networkNotifySwitchType)).thenReturn(type);
}
void verifyNoToast() {
verify(mNotifier, never()).showToast(any(), any());
}
void verifyNoNotification() {
verify(mNotifier, never())
.showNotification(anyInt(), any(), any(), any(), any(), anyBoolean());
}
void verifyNoNotifications() {
verifyNoToast();
verifyNoNotification();
verify(mNotifier, never()).clearNotification(anyInt());
}
void verifyToast(NetworkAgentInfo from, NetworkAgentInfo to) {
verifyNoNotification();
verify(mNotifier, times(1)).showToast(from, to);
}
void verifyNotification(NetworkAgentInfo from, NetworkAgentInfo to) {
verifyNoToast();
verify(mNotifier, times(1)).showNotification(eq(from.network.netId),
eq(NotificationType.NETWORK_SWITCH), eq(from), eq(to), any(), eq(true));
}
NetworkAgentInfo nai(int netId, int transport, int networkType, String networkTypeName) {
NetworkInfo info = new NetworkInfo(networkType, 0, networkTypeName, "");
NetworkCapabilities caps = new NetworkCapabilities();
caps.addCapability(0);
caps.addTransportType(transport);
NetworkAgentInfo nai = new NetworkAgentInfo(null, null, new Network(netId), info, null,
caps, 50, mCtx, null, mMisc, null, mConnService);
nai.everValidated = true;
return nai;
}
NetworkAgentInfo wifiNai(int netId) {
return nai(netId, NetworkCapabilities.TRANSPORT_WIFI,
ConnectivityManager.TYPE_WIFI, WIFI);
}
NetworkAgentInfo cellNai(int netId) {
return nai(netId, NetworkCapabilities.TRANSPORT_CELLULAR,
ConnectivityManager.TYPE_MOBILE, CELLULAR);
}
public static class TestableLingerMonitor extends LingerMonitor {
public TestableLingerMonitor(Context c, NetworkNotificationManager n) {
super(c, n);
}
@Override protected PendingIntent createNotificationIntent() {
return null;
}
}
}