Merge "DO NOT MERGE: Migrate NetworkPolicyManagerServiceTest to JUnit 4."
This commit is contained in:
@@ -34,6 +34,12 @@ import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT;
|
||||
import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT_SNOOZED;
|
||||
import static com.android.server.net.NetworkPolicyManagerService.TYPE_WARNING;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.fail;
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyBoolean;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
@@ -74,7 +80,8 @@ import android.os.Binder;
|
||||
import android.os.INetworkManagementService;
|
||||
import android.os.PowerManagerInternal;
|
||||
import android.os.UserHandle;
|
||||
import android.test.AndroidTestCase;
|
||||
import android.support.test.InstrumentationRegistry;
|
||||
import android.support.test.runner.AndroidJUnit4;
|
||||
import android.text.format.Time;
|
||||
import android.util.Log;
|
||||
import android.util.TrustedTime;
|
||||
@@ -87,6 +94,11 @@ import libcore.io.IoUtils;
|
||||
|
||||
import com.google.common.util.concurrent.AbstractFuture;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
@@ -106,7 +118,8 @@ import java.util.concurrent.TimeoutException;
|
||||
/**
|
||||
* Tests for {@link NetworkPolicyManagerService}.
|
||||
*/
|
||||
public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
@RunWith(AndroidJUnit4.class)
|
||||
public class NetworkPolicyManagerServiceTest {
|
||||
private static final String TAG = "NetworkPolicyManagerServiceTest";
|
||||
|
||||
private static final long TEST_START = 1194220800000L;
|
||||
@@ -117,7 +130,6 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
|
||||
private BroadcastInterceptingContext mServiceContext;
|
||||
private File mPolicyDir;
|
||||
private List<Class<?>> mLocalServices = new ArrayList<>();
|
||||
|
||||
private @Mock IActivityManager mActivityManager;
|
||||
private @Mock INetworkStatsService mStatsService;
|
||||
@@ -125,7 +137,6 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
private @Mock TrustedTime mTime;
|
||||
private @Mock IConnectivityManager mConnManager;
|
||||
private @Mock INotificationManager mNotifManager;
|
||||
private @Mock UsageStatsManagerInternal mUsageStats;
|
||||
private @Mock PackageManager mPackageManager;
|
||||
|
||||
private IUidObserver mUidObserver;
|
||||
@@ -147,12 +158,20 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
|
||||
private static final String PKG_NAME_A = "name.is.A,pkg.A";
|
||||
|
||||
public void setUp() throws Exception {
|
||||
super.setUp();
|
||||
@BeforeClass
|
||||
public static void registerLocalServices() {
|
||||
addLocalServiceMock(PowerManagerInternal.class);
|
||||
addLocalServiceMock(DeviceIdleController.LocalService.class);
|
||||
final UsageStatsManagerInternal usageStats =
|
||||
addLocalServiceMock(UsageStatsManagerInternal.class);
|
||||
when(usageStats.getIdleUidsForUser(anyInt())).thenReturn(new int[]{});
|
||||
}
|
||||
|
||||
@Before
|
||||
public void callSystemReady() throws Exception {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
final Context context = getContext();
|
||||
final Context context = InstrumentationRegistry.getContext();
|
||||
|
||||
setCurrentTimeMillis(TEST_START);
|
||||
|
||||
@@ -184,10 +203,6 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
}
|
||||
}).when(mActivityManager).registerUidObserver(any(), anyInt());
|
||||
|
||||
addLocalServiceMock(PowerManagerInternal.class);
|
||||
addLocalServiceMock(DeviceIdleController.LocalService.class);
|
||||
addLocalServiceMock(UsageStatsManagerInternal.class, mUsageStats);
|
||||
|
||||
mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mStatsService,
|
||||
mNetworkManager, mTime, mPolicyDir, true);
|
||||
mService.bindConnectivityManager(mConnManager);
|
||||
@@ -217,7 +232,6 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
when(mPackageManager.getApplicationInfoAsUser(anyString(), anyInt(), anyInt()))
|
||||
.thenReturn(new ApplicationInfo());
|
||||
when(mPackageManager.getPackagesForUid(UID_A)).thenReturn(new String[] {PKG_NAME_A});
|
||||
when(mUsageStats.getIdleUidsForUser(anyInt())).thenReturn(new int[]{});
|
||||
when(mNetworkManager.isBandwidthControlEnabled()).thenReturn(true);
|
||||
expectCurrentTime();
|
||||
|
||||
@@ -231,28 +245,17 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
mNetworkObserver = networkObserver.getValue();
|
||||
}
|
||||
|
||||
public void tearDown() throws Exception {
|
||||
@After
|
||||
public void removeFiles() throws Exception {
|
||||
for (File file : mPolicyDir.listFiles()) {
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
mServiceContext = null;
|
||||
mPolicyDir = null;
|
||||
|
||||
mActivityManager = null;
|
||||
mStatsService = null;
|
||||
mTime = null;
|
||||
|
||||
mService = null;
|
||||
|
||||
// TODO: must remove services, otherwise next test will fail.
|
||||
// JUnit4 would avoid that hack by using a static setup.
|
||||
removeLocalServiceMocks();
|
||||
|
||||
// Added by NetworkPolicyManagerService's constructor.
|
||||
@After
|
||||
public void unregisterLocalServices() throws Exception {
|
||||
// Registered by NetworkPolicyManagerService's constructor.
|
||||
LocalServices.removeServiceForTest(NetworkPolicyManagerInternal.class);
|
||||
|
||||
super.tearDown();
|
||||
}
|
||||
|
||||
// NOTE: testPolicyChangeTriggersListener() and testUidForeground() are too superficial, they
|
||||
@@ -260,6 +263,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
// different modes (Data Saver, Battery Saver, Doze, App idle, etc...).
|
||||
// These scenarios are extensively tested on CTS' HostsideRestrictBackgroundNetworkTests.
|
||||
|
||||
@Test
|
||||
public void testPolicyChangeTriggersListener() throws Exception {
|
||||
mPolicyListener.expect().onRestrictBackgroundBlacklistChanged(anyInt(), anyBoolean());
|
||||
|
||||
@@ -269,6 +273,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
mPolicyListener.waitAndVerify().onRestrictBackgroundBlacklistChanged(APP_ID_A, true);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUidForeground() throws Exception {
|
||||
// push all uids into background
|
||||
mUidObserver.onUidStateChanged(UID_A, ActivityManager.PROCESS_STATE_SERVICE);
|
||||
@@ -288,6 +293,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
assertTrue(mService.isUidForeground(UID_B));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLastCycleBoundaryThisMonth() throws Exception {
|
||||
// assume cycle day of "5th", which should be in same month
|
||||
final long currentTime = parseTime("2007-11-14T00:00:00.000Z");
|
||||
@@ -299,6 +305,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
assertTimeEquals(expectedCycle, actualCycle);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLastCycleBoundaryLastMonth() throws Exception {
|
||||
// assume cycle day of "20th", which should be in last month
|
||||
final long currentTime = parseTime("2007-11-14T00:00:00.000Z");
|
||||
@@ -310,6 +317,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
assertTimeEquals(expectedCycle, actualCycle);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLastCycleBoundaryThisMonthFebruary() throws Exception {
|
||||
// assume cycle day of "30th" in february; should go to january
|
||||
final long currentTime = parseTime("2007-02-14T00:00:00.000Z");
|
||||
@@ -321,6 +329,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
assertTimeEquals(expectedCycle, actualCycle);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLastCycleBoundaryLastMonthFebruary() throws Exception {
|
||||
// assume cycle day of "30th" in february, which should clamp
|
||||
final long currentTime = parseTime("2007-03-14T00:00:00.000Z");
|
||||
@@ -332,6 +341,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
assertTimeEquals(expectedCycle, actualCycle);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCycleBoundaryLeapYear() throws Exception {
|
||||
final NetworkPolicy policy = new NetworkPolicy(
|
||||
sTemplateWifi, 29, TIMEZONE_UTC, 1024L, 1024L, false);
|
||||
@@ -355,6 +365,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
computeNextCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextCycleTimezoneAfterUtc() throws Exception {
|
||||
// US/Central is UTC-6
|
||||
final NetworkPolicy policy = new NetworkPolicy(
|
||||
@@ -363,6 +374,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextCycleTimezoneBeforeUtc() throws Exception {
|
||||
// Israel is UTC+2
|
||||
final NetworkPolicy policy = new NetworkPolicy(
|
||||
@@ -371,6 +383,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNextCycleSane() throws Exception {
|
||||
final NetworkPolicy policy = new NetworkPolicy(
|
||||
sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
|
||||
@@ -386,6 +399,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLastCycleSane() throws Exception {
|
||||
final NetworkPolicy policy = new NetworkPolicy(
|
||||
sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
|
||||
@@ -401,6 +415,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCycleTodayJanuary() throws Exception {
|
||||
final NetworkPolicy policy = new NetworkPolicy(
|
||||
sTemplateWifi, 14, "US/Pacific", 1024L, 1024L, false);
|
||||
@@ -420,6 +435,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
computeLastCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLastCycleBoundaryDST() throws Exception {
|
||||
final long currentTime = parseTime("1989-01-02T07:30:00.000");
|
||||
final long expectedCycle = parseTime("1988-12-03T02:00:00.000Z");
|
||||
@@ -430,6 +446,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
assertTimeEquals(expectedCycle, actualCycle);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNetworkPolicyAppliedCycleLastMonth() throws Exception {
|
||||
NetworkState[] state = null;
|
||||
NetworkStats stats = null;
|
||||
@@ -472,6 +489,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
verifySetInterfaceQuota(TEST_IFACE, (2 * MB_IN_BYTES) - 512);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverWarningLimitNotification() throws Exception {
|
||||
NetworkState[] state = null;
|
||||
NetworkStats stats = null;
|
||||
@@ -580,6 +598,7 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMeteredNetworkWithoutLimit() throws Exception {
|
||||
NetworkState[] state = null;
|
||||
NetworkStats stats = null;
|
||||
@@ -741,31 +760,12 @@ public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
|
||||
/**
|
||||
* Creates a mock and registers it to {@link LocalServices}.
|
||||
*/
|
||||
private <T> T addLocalServiceMock(Class<T> clazz) {
|
||||
private static <T> T addLocalServiceMock(Class<T> clazz) {
|
||||
final T mock = mock(clazz);
|
||||
return addLocalServiceMock(clazz, mock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a mock to {@link LocalServices}.
|
||||
*/
|
||||
private <T> T addLocalServiceMock(Class<T> clazz, T mock) {
|
||||
LocalServices.addService(clazz, mock);
|
||||
mLocalServices.add(clazz);
|
||||
return mock;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregisters all mocks from {@link LocalServices}.
|
||||
*/
|
||||
private void removeLocalServiceMocks() {
|
||||
for (Class<?> clazz : mLocalServices) {
|
||||
Log.d(TAG, "removeLocalServiceMock(): " + clazz.getName());
|
||||
LocalServices.removeServiceForTest(clazz);
|
||||
}
|
||||
mLocalServices.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom Mockito answer used to verify async {@link INetworkPolicyListener} calls.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user