Network Switching Notifications: add unit tests am: 01f7228f5c am: 56f1281888

am: cf5f5785c9

Change-Id: I978e49ba1302ca3d791c59555b5a143cefb62c06
This commit is contained in:
Hugo Benichi
2016-08-30 20:31:34 +00:00
committed by android-build-merger
2 changed files with 280 additions and 16 deletions

View File

@@ -17,14 +17,11 @@
package com.android.server.connectivity;
import android.app.PendingIntent;
import android.net.ConnectivityManager;
import android.net.NetworkCapabilities;
import android.net.Uri;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
@@ -33,6 +30,7 @@ import android.util.SparseBooleanArray;
import java.util.Arrays;
import java.util.HashMap;
import com.android.internal.annotations.VisibleForTesting;
import com.android.internal.util.MessageUtils;
import com.android.server.connectivity.NetworkNotificationManager;
import com.android.server.connectivity.NetworkNotificationManager.NotificationType;
@@ -52,13 +50,15 @@ public class LingerMonitor {
private static final boolean VDBG = false;
private static final String TAG = LingerMonitor.class.getSimpleName();
private static final HashMap<String, Integer> sTransportNames = makeTransportToNameMap();
private static final Intent CELLULAR_SETTINGS = new Intent().setComponent(new ComponentName(
private static final HashMap<String, Integer> TRANSPORT_NAMES = makeTransportToNameMap();
@VisibleForTesting
public static final Intent CELLULAR_SETTINGS = new Intent().setComponent(new ComponentName(
"com.android.settings", "com.android.settings.Settings$DataUsageSummaryActivity"));
private static final int NOTIFY_TYPE_NONE = 0;
private static final int NOTIFY_TYPE_NOTIFICATION = 1;
private static final int NOTIFY_TYPE_TOAST = 2;
@VisibleForTesting
public static final int NOTIFY_TYPE_NONE = 0;
public static final int NOTIFY_TYPE_NOTIFICATION = 1;
public static final int NOTIFY_TYPE_TOAST = 2;
private static SparseArray<String> sNotifyTypeNames = MessageUtils.findMessageNames(
new Class[] { LingerMonitor.class }, new String[]{ "NOTIFY_TYPE_" });
@@ -106,7 +106,8 @@ public class LingerMonitor {
return mEverNotified.get(nai.network.netId, false);
}
private boolean isNotificationEnabled(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
@VisibleForTesting
public boolean isNotificationEnabled(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
// TODO: Evaluate moving to CarrierConfigManager.
String[] notifySwitches = mContext.getResources().getStringArray(
com.android.internal.R.array.config_networkNotifySwitches);
@@ -122,8 +123,8 @@ public class LingerMonitor {
Log.e(TAG, "Invalid network switch notification configuration: " + notifySwitch);
continue;
}
int fromTransport = sTransportNames.get("TRANSPORT_" + transports[0]);
int toTransport = sTransportNames.get("TRANSPORT_" + transports[1]);
int fromTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[0]);
int toTransport = TRANSPORT_NAMES.get("TRANSPORT_" + transports[1]);
if (hasTransport(fromNai, fromTransport) && hasTransport(toNai, toTransport)) {
return true;
}
@@ -133,12 +134,14 @@ public class LingerMonitor {
}
private void showNotification(NetworkAgentInfo fromNai, NetworkAgentInfo toNai) {
PendingIntent pendingIntent = PendingIntent.getActivityAsUser(
mContext, 0, CELLULAR_SETTINGS, PendingIntent.FLAG_CANCEL_CURRENT, null,
UserHandle.CURRENT);
mNotifier.showNotification(fromNai.network.netId, NotificationType.NETWORK_SWITCH,
fromNai, toNai, pendingIntent, true);
fromNai, toNai, createNotificationIntent(), true);
}
@VisibleForTesting
protected PendingIntent createNotificationIntent() {
return PendingIntent.getActivityAsUser(mContext, 0, CELLULAR_SETTINGS,
PendingIntent.FLAG_CANCEL_CURRENT, null, UserHandle.CURRENT);
}
// Removes any notification that was put up as a result of switching to nai.

View File

@@ -0,0 +1,261 @@
/*
* 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;
}
}
}