Modify Nsd{Service,Manager}Test to conform to its change
(clean cherry-pick from downstream branch) Test: atest NsdManagerTest NsdServiceTest Bug: 191844585 Merged-In: I3cf658498bef5755dcb01127a94fff913b6e6298 Change-Id: I3cf658498bef5755dcb01127a94fff913b6e6298
This commit is contained in:
committed by
Remi NGUYEN VAN
parent
2e0b45004c
commit
df75378ad4
@@ -66,6 +66,7 @@ android_library {
|
||||
"framework-protos",
|
||||
"mockito-target-minus-junit4",
|
||||
"net-tests-utils",
|
||||
"platform-compat-test-rules",
|
||||
"platform-test-annotations",
|
||||
"service-connectivity-pre-jarjar",
|
||||
"services.core",
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package android.net.nsd;
|
||||
|
||||
import static libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
|
||||
import static libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.fail;
|
||||
@@ -27,6 +30,7 @@ import static org.mockito.Mockito.spy;
|
||||
import static org.mockito.Mockito.timeout;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
import android.compat.testing.PlatformCompatChangeRule;
|
||||
import android.content.Context;
|
||||
import android.os.Build;
|
||||
import android.os.Handler;
|
||||
@@ -44,7 +48,9 @@ import com.android.testutils.HandlerUtils;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
@@ -56,6 +62,9 @@ public class NsdManagerTest {
|
||||
|
||||
static final int PROTOCOL = NsdManager.PROTOCOL_DNS_SD;
|
||||
|
||||
@Rule
|
||||
public TestRule compatChangeRule = new PlatformCompatChangeRule();
|
||||
|
||||
@Mock Context mContext;
|
||||
@Mock INsdManager mService;
|
||||
MockServiceHandler mServiceHandler;
|
||||
@@ -70,8 +79,6 @@ public class NsdManagerTest {
|
||||
|
||||
mServiceHandler = spy(MockServiceHandler.create(mContext));
|
||||
doReturn(new Messenger(mServiceHandler)).when(mService).getMessenger();
|
||||
|
||||
mManager = makeManager();
|
||||
}
|
||||
|
||||
@After
|
||||
@@ -85,7 +92,76 @@ public class NsdManagerTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testResolveService() {
|
||||
@EnableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testResolveServiceS() {
|
||||
mManager = makeNsdManagerS();
|
||||
doTestResolveService();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testResolveServicePreS() {
|
||||
mManager = makeNsdManagerPreS();
|
||||
doTestResolveService();
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testDiscoverServiceS() {
|
||||
mManager = makeNsdManagerS();
|
||||
doTestDiscoverService();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testDiscoverServicePreS() {
|
||||
mManager = makeNsdManagerPreS();
|
||||
doTestDiscoverService();
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testParallelResolveServiceS() {
|
||||
mManager = makeNsdManagerS();
|
||||
doTestParallelResolveService();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testParallelResolveServicePreS() {
|
||||
mManager = makeNsdManagerPreS();
|
||||
doTestParallelResolveService();
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testInvalidCallsS() {
|
||||
mManager = makeNsdManagerS();
|
||||
doTestInvalidCalls();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testInvalidCallsPreS() {
|
||||
mManager = makeNsdManagerPreS();
|
||||
doTestInvalidCalls();
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testRegisterServiceS() {
|
||||
mManager = makeNsdManagerS();
|
||||
doTestRegisterService();
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testRegisterServicePreS() {
|
||||
mManager = makeNsdManagerPreS();
|
||||
doTestRegisterService();
|
||||
}
|
||||
|
||||
public void doTestResolveService() {
|
||||
NsdManager manager = mManager;
|
||||
|
||||
NsdServiceInfo request = new NsdServiceInfo("a_name", "a_type");
|
||||
@@ -104,8 +180,7 @@ public class NsdManagerTest {
|
||||
verify(listener, timeout(mTimeoutMs).times(1)).onServiceResolved(reply);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParallelResolveService() {
|
||||
public void doTestParallelResolveService() {
|
||||
NsdManager manager = mManager;
|
||||
|
||||
NsdServiceInfo request = new NsdServiceInfo("a_name", "a_type");
|
||||
@@ -127,8 +202,7 @@ public class NsdManagerTest {
|
||||
verify(listener2, timeout(mTimeoutMs).times(1)).onServiceResolved(reply);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRegisterService() {
|
||||
public void doTestRegisterService() {
|
||||
NsdManager manager = mManager;
|
||||
|
||||
NsdServiceInfo request1 = new NsdServiceInfo("a_name", "a_type");
|
||||
@@ -186,8 +260,7 @@ public class NsdManagerTest {
|
||||
//verify(listener2, timeout(mTimeoutMs).times(1)).onServiceUnregistered(request2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDiscoverService() {
|
||||
public void doTestDiscoverService() {
|
||||
NsdManager manager = mManager;
|
||||
|
||||
NsdServiceInfo reply1 = new NsdServiceInfo("a_name", "a_type");
|
||||
@@ -264,8 +337,7 @@ public class NsdManagerTest {
|
||||
verify(listener, timeout(mTimeoutMs).times(0)).onServiceFound(reply1);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidCalls() {
|
||||
public void doTestInvalidCalls() {
|
||||
NsdManager manager = mManager;
|
||||
|
||||
NsdManager.RegistrationListener listener1 = mock(NsdManager.RegistrationListener.class);
|
||||
@@ -326,10 +398,21 @@ public class NsdManagerTest {
|
||||
}
|
||||
}
|
||||
|
||||
NsdManager makeManager() {
|
||||
NsdManager makeNsdManagerS() {
|
||||
// Expect we'll get 2 AsyncChannel related msgs.
|
||||
return makeManager(2);
|
||||
}
|
||||
|
||||
NsdManager makeNsdManagerPreS() {
|
||||
// Expect we'll get 3 msgs. 2 AsyncChannel related msgs + 1 additional daemon startup msg.
|
||||
return makeManager(3);
|
||||
}
|
||||
|
||||
NsdManager makeManager(int expectedMsgCount) {
|
||||
NsdManager manager = new NsdManager(mContext, mService);
|
||||
// Acknowledge first two messages connecting the AsyncChannel.
|
||||
verify(mServiceHandler, timeout(mTimeoutMs).times(2)).handleMessage(any());
|
||||
verify(mServiceHandler, timeout(mTimeoutMs).times(expectedMsgCount)).handleMessage(any());
|
||||
|
||||
reset(mServiceHandler);
|
||||
assertNotNull(mServiceHandler.chan);
|
||||
return manager;
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package com.android.server;
|
||||
|
||||
import static libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges;
|
||||
import static libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.any;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
@@ -27,6 +30,7 @@ import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.compat.testing.PlatformCompatChangeRule;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.net.nsd.NsdManager;
|
||||
@@ -48,7 +52,9 @@ import com.android.testutils.HandlerUtils;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TestRule;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
@@ -67,6 +73,8 @@ public class NsdServiceTest {
|
||||
private static final long CLEANUP_DELAY_MS = 500;
|
||||
private static final long TIMEOUT_MS = 500;
|
||||
|
||||
@Rule
|
||||
public TestRule compatChangeRule = new PlatformCompatChangeRule();
|
||||
@Mock Context mContext;
|
||||
@Mock ContentResolver mResolver;
|
||||
@Mock NsdService.NsdSettings mSettings;
|
||||
@@ -94,6 +102,34 @@ public class NsdServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testPreSClients() {
|
||||
when(mSettings.isEnabled()).thenReturn(true);
|
||||
NsdService service = makeService();
|
||||
|
||||
// Pre S client connected, the daemon should be started.
|
||||
NsdManager client1 = connectClient(service);
|
||||
waitForIdle();
|
||||
verify(mDaemon, times(1)).maybeStart();
|
||||
verifyDaemonCommands("start-service");
|
||||
|
||||
NsdManager client2 = connectClient(service);
|
||||
waitForIdle();
|
||||
verify(mDaemon, times(1)).maybeStart();
|
||||
|
||||
client1.disconnect();
|
||||
// Still 1 client remains, daemon shouldn't be stopped.
|
||||
waitForIdle();
|
||||
verify(mDaemon, never()).maybeStop();
|
||||
|
||||
client2.disconnect();
|
||||
// All clients are disconnected, the daemon should be stopped.
|
||||
verifyDelayMaybeStopDaemon(CLEANUP_DELAY_MS);
|
||||
verifyDaemonCommands("stop-service");
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testNoDaemonStartedWhenClientsConnect() {
|
||||
when(mSettings.isEnabled()).thenReturn(true);
|
||||
|
||||
@@ -110,13 +146,12 @@ public class NsdServiceTest {
|
||||
waitForIdle();
|
||||
verify(mDaemon, never()).execute(any());
|
||||
|
||||
// If there is no active request, try to clean up the daemon
|
||||
// every time the client disconnects.
|
||||
client1.disconnect();
|
||||
// Still 1 client remains, daemon shouldn't be stopped.
|
||||
waitForIdle();
|
||||
verify(mDaemon, never()).maybeStop();
|
||||
|
||||
verifyDelayMaybeStopDaemon(CLEANUP_DELAY_MS);
|
||||
reset(mDaemon);
|
||||
client2.disconnect();
|
||||
// All clients are disconnected, stop the daemon after CLEANUP_DELAY_MS.
|
||||
verifyDelayMaybeStopDaemon(CLEANUP_DELAY_MS);
|
||||
|
||||
client1.disconnect();
|
||||
@@ -124,6 +159,7 @@ public class NsdServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testClientRequestsAreGCedAtDisconnection() {
|
||||
when(mSettings.isEnabled()).thenReturn(true);
|
||||
|
||||
@@ -169,6 +205,7 @@ public class NsdServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@EnableCompatChanges(NsdManager.RUN_NATIVE_NSD_ONLY_IF_LEGACY_APPS)
|
||||
public void testCleanupDelayNoRequestActive() {
|
||||
when(mSettings.isEnabled()).thenReturn(true);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user