diff options
| author | Luke Huang <huangluke@google.com> | 2021-07-13 11:58:37 +0800 |
|---|---|---|
| committer | Remi NGUYEN VAN <reminv@google.com> | 2021-09-13 10:18:04 +0900 |
| commit | df75378ad46a20b1371a43a8930dc08a260492cd (patch) | |
| tree | 63170e26fa12501e0d188064266b17a38ce99a72 /tests/unit/java/com/android/server/NsdServiceTest.java | |
| parent | 2e0b45004c22adb7d9c1a1ebd06ec15148841331 (diff) | |
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
Diffstat (limited to 'tests/unit/java/com/android/server/NsdServiceTest.java')
| -rw-r--r-- | tests/unit/java/com/android/server/NsdServiceTest.java | 47 |
1 files changed, 42 insertions, 5 deletions
diff --git a/tests/unit/java/com/android/server/NsdServiceTest.java b/tests/unit/java/com/android/server/NsdServiceTest.java index e80a9380f6..4d2970af95 100644 --- a/tests/unit/java/com/android/server/NsdServiceTest.java +++ b/tests/unit/java/com/android/server/NsdServiceTest.java @@ -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); |
