Add a common test library.

This is the most common test library for Connectivity tests. It is
meant to be usable in framework tests, network stack tests, CTS,
GTS. To achieve that, it can only depend on framework classes.

Bug: none
Test: NetworkMonitorTest
Test: NsdManagerTest
Test: ConnectivityServiceTest
Test: OffloadControllerTest
Test: NetworkStatsObserversTest
Test: NetworkStatsServiceTest
(all the touched classes)

Change-Id: Ic47cbe7ba0e407145fa6bc49bb2adb3c5937dbc4
This commit is contained in:
Chalard Jean
2019-05-28 16:48:32 +09:00
parent ce2f2fe2e8
commit e343bcc913
5 changed files with 17 additions and 30 deletions

View File

@@ -68,10 +68,6 @@ import static android.net.NetworkPolicyManager.RULE_REJECT_ALL;
import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
import static android.net.RouteInfo.RTN_UNREACHABLE;
import static com.android.internal.util.TestUtils.waitForIdleHandler;
import static com.android.internal.util.TestUtils.waitForIdleLooper;
import static com.android.internal.util.TestUtils.waitForIdleSerialExecutor;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
@@ -196,6 +192,7 @@ import com.android.server.connectivity.Tethering;
import com.android.server.connectivity.Vpn;
import com.android.server.net.NetworkPinner;
import com.android.server.net.NetworkPolicyManagerInternal;
import com.android.testutils.HandlerUtilsKt;
import org.junit.After;
import org.junit.Before;
@@ -375,19 +372,19 @@ public class ConnectivityServiceTest {
public void waitForIdle(int timeoutMsAsInt) {
long timeoutMs = timeoutMsAsInt;
waitForIdleHandler(mService.mHandlerThread, timeoutMs);
HandlerUtilsKt.waitForIdle(mService.mHandlerThread, timeoutMs);
waitForIdle(mCellNetworkAgent, timeoutMs);
waitForIdle(mWiFiNetworkAgent, timeoutMs);
waitForIdle(mEthernetNetworkAgent, timeoutMs);
waitForIdleHandler(mService.mHandlerThread, timeoutMs);
waitForIdleLooper(ConnectivityThread.getInstanceLooper(), timeoutMs);
HandlerUtilsKt.waitForIdle(mService.mHandlerThread, timeoutMs);
HandlerUtilsKt.waitForIdle(ConnectivityThread.get(), timeoutMs);
}
public void waitForIdle(MockNetworkAgent agent, long timeoutMs) {
if (agent == null) {
return;
}
waitForIdleHandler(agent.mHandlerThread, timeoutMs);
HandlerUtilsKt.waitForIdle(agent.mHandlerThread, timeoutMs);
}
private void waitForIdle() {
@@ -1220,7 +1217,7 @@ public class ConnectivityServiceTest {
}
public void waitForIdle(int timeoutMs) {
waitForIdleHandler(mHandlerThread, timeoutMs);
HandlerUtilsKt.waitForIdle(mHandlerThread, timeoutMs);
}
public void waitForIdle() {
@@ -4107,7 +4104,7 @@ public class ConnectivityServiceTest {
}
public void assertNoCallback() {
waitForIdleSerialExecutor(mExecutor, TIMEOUT_MS);
HandlerUtilsKt.waitForIdleSerialExecutor(mExecutor, TIMEOUT_MS);
CallbackValue cv = mCallbacks.peek();
assertNull("Unexpected callback: " + cv, cv);
}

View File

@@ -28,8 +28,6 @@ import static android.net.NetworkTemplate.buildTemplateWifiWildcard;
import static android.net.TrafficStats.MB_IN_BYTES;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
import static com.android.internal.util.TestUtils.waitForIdleHandler;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
@@ -56,6 +54,7 @@ import androidx.test.runner.AndroidJUnit4;
import com.android.internal.net.VpnInfo;
import com.android.server.net.NetworkStatsServiceTest.LatchedHandler;
import com.android.testutils.HandlerUtilsKt;
import org.junit.Before;
import org.junit.Test;
@@ -457,7 +456,7 @@ public class NetworkStatsObserversTest {
}
private void waitForObserverToIdle() {
waitForIdleHandler(mObserverHandlerThread, WAIT_TIMEOUT_MS);
waitForIdleHandler(mHandler, WAIT_TIMEOUT_MS);
HandlerUtilsKt.waitForIdle(mObserverHandlerThread, WAIT_TIMEOUT_MS);
HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT_MS);
}
}

View File

@@ -52,7 +52,6 @@ import static android.text.format.DateUtils.HOUR_IN_MILLIS;
import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
import static android.text.format.DateUtils.WEEK_IN_MILLIS;
import static com.android.internal.util.TestUtils.waitForIdleHandler;
import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
import static org.junit.Assert.assertEquals;
@@ -105,6 +104,7 @@ import com.android.internal.util.ArrayUtils;
import com.android.internal.util.test.BroadcastInterceptingContext;
import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
import com.android.server.net.NetworkStatsService.NetworkStatsSettings.Config;
import com.android.testutils.HandlerUtilsKt;
import libcore.io.IoUtils;
@@ -1321,8 +1321,6 @@ public class NetworkStatsServiceTest {
expectNetworkStatsSummary(buildEmptyStats());
expectNetworkStatsUidDetail(buildEmptyStats());
// Register and verify request and that binder was called
DataUsageRequest request =
mService.registerUsageCallback(mServiceContext.getOpPackageName(), inputRequest,
@@ -1334,14 +1332,11 @@ public class NetworkStatsServiceTest {
// Send dummy message to make sure that any previous message has been handled
mHandler.sendMessage(mHandler.obtainMessage(-1));
waitForIdleHandler(mHandler, WAIT_TIMEOUT);
HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT);
// Make sure that the caller binder gets connected
verify(mBinder).linkToDeath(any(IBinder.DeathRecipient.class), anyInt());
// modify some number on wifi, and trigger poll event
// not enough traffic to call data usage callback
incrementCurrentTime(HOUR_IN_MILLIS);
@@ -1674,7 +1669,7 @@ public class NetworkStatsServiceTest {
mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
// Send dummy message to make sure that any previous message has been handled
mHandler.sendMessage(mHandler.obtainMessage(-1));
waitForIdleHandler(mHandler, WAIT_TIMEOUT);
HandlerUtilsKt.waitForIdle(mHandler, WAIT_TIMEOUT);
}
static class LatchedHandler extends Handler {