Loading core/java/android/net/nsd/NsdManager.java +7 −7 Original line number Diff line number Diff line Loading @@ -21,25 +21,24 @@ import static com.android.internal.util.Preconditions.checkNotNull; import static com.android.internal.util.Preconditions.checkStringNotEmpty; import android.annotation.SdkConstant; import android.annotation.SystemService; import android.annotation.SdkConstant.SdkConstantType; import android.annotation.SystemService; import android.content.Context; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import android.os.Message; import android.os.RemoteException; import android.os.Messenger; import android.text.TextUtils; import android.os.RemoteException; import android.util.Log; import android.util.SparseArray; import java.util.concurrent.CountDownLatch; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.AsyncChannel; import com.android.internal.util.Protocol; import java.util.concurrent.CountDownLatch; /** * The Network Service Discovery Manager class provides the API to discover services * on a network. As an example, if device A and device B are connected over a Wi-Fi Loading Loading @@ -244,7 +243,7 @@ public final class NsdManager { return name; } private static int FIRST_LISTENER_KEY = 1; private static final int FIRST_LISTENER_KEY = 1; private final INsdManager mService; private final Context mContext; Loading Loading @@ -278,6 +277,7 @@ public final class NsdManager { @VisibleForTesting public void disconnect() { mAsyncChannel.disconnect(); mHandler.getLooper().quitSafely(); } /** Loading tests/net/java/android/net/nsd/NsdManagerTest.java +25 −5 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static com.android.internal.util.TestUtils.waitForIdleHandler; import android.os.HandlerThread; import android.os.Handler; Loading @@ -38,6 +39,7 @@ import android.support.test.runner.AndroidJUnit4; import android.os.Message; import android.os.Messenger; import com.android.internal.util.AsyncChannel; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; Loading @@ -56,6 +58,8 @@ public class NsdManagerTest { @Mock INsdManager mService; MockServiceHandler mServiceHandler; NsdManager mManager; long mTimeoutMs = 100; // non-final so that tests can adjust the value. @Before Loading @@ -64,11 +68,23 @@ public class NsdManagerTest { mServiceHandler = spy(MockServiceHandler.create(mContext)); when(mService.getMessenger()).thenReturn(new Messenger(mServiceHandler)); mManager = makeManager(); } @After public void tearDown() throws Exception { waitForIdleHandler(mServiceHandler, mTimeoutMs); mServiceHandler.chan.disconnect(); mServiceHandler.stop(); if (mManager != null) { mManager.disconnect(); } } @Test public void testResolveService() { NsdManager manager = makeManager(); NsdManager manager = mManager; NsdServiceInfo request = new NsdServiceInfo("a_name", "a_type"); NsdServiceInfo reply = new NsdServiceInfo("resolved_name", "resolved_type"); Loading @@ -88,7 +104,7 @@ public class NsdManagerTest { @Test public void testParallelResolveService() { NsdManager manager = makeManager(); NsdManager manager = mManager; NsdServiceInfo request = new NsdServiceInfo("a_name", "a_type"); NsdServiceInfo reply = new NsdServiceInfo("resolved_name", "resolved_type"); Loading @@ -111,7 +127,7 @@ public class NsdManagerTest { @Test public void testRegisterService() { NsdManager manager = makeManager(); NsdManager manager = mManager; NsdServiceInfo request1 = new NsdServiceInfo("a_name", "a_type"); NsdServiceInfo request2 = new NsdServiceInfo("another_name", "another_type"); Loading Loading @@ -170,7 +186,7 @@ public class NsdManagerTest { @Test public void testDiscoverService() { NsdManager manager = makeManager(); NsdManager manager = mManager; NsdServiceInfo reply1 = new NsdServiceInfo("a_name", "a_type"); NsdServiceInfo reply2 = new NsdServiceInfo("another_name", "a_type"); Loading Loading @@ -248,7 +264,7 @@ public class NsdManagerTest { @Test public void testInvalidCalls() { NsdManager manager = new NsdManager(mContext, mService); NsdManager manager = mManager; NsdManager.RegistrationListener listener1 = mock(NsdManager.RegistrationListener.class); NsdManager.DiscoveryListener listener2 = mock(NsdManager.DiscoveryListener.class); Loading Loading @@ -351,6 +367,10 @@ public class NsdManagerTest { } } public void stop() { getLooper().quitSafely(); } public static MockServiceHandler create(Context context) { HandlerThread t = new HandlerThread("mock-service-handler"); t.start(); Loading tests/net/java/com/android/server/NsdServiceTest.java +5 −0 Original line number Diff line number Diff line Loading @@ -95,6 +95,9 @@ public class NsdServiceTest { client2.disconnect(); verify(mDaemon, timeout(mTimeoutMs).times(1)).stop(); client1.disconnect(); client2.disconnect(); } @Test Loading Loading @@ -131,6 +134,8 @@ public class NsdServiceTest { // checks that request are cleaned verifyDaemonCommands("stop-register 2", "stop-discover 3", "stop-resolve 4"); client.disconnect(); } NsdService makeService() { Loading Loading
core/java/android/net/nsd/NsdManager.java +7 −7 Original line number Diff line number Diff line Loading @@ -21,25 +21,24 @@ import static com.android.internal.util.Preconditions.checkNotNull; import static com.android.internal.util.Preconditions.checkStringNotEmpty; import android.annotation.SdkConstant; import android.annotation.SystemService; import android.annotation.SdkConstant.SdkConstantType; import android.annotation.SystemService; import android.content.Context; import android.os.Handler; import android.os.HandlerThread; import android.os.Looper; import android.os.Message; import android.os.RemoteException; import android.os.Messenger; import android.text.TextUtils; import android.os.RemoteException; import android.util.Log; import android.util.SparseArray; import java.util.concurrent.CountDownLatch; import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.AsyncChannel; import com.android.internal.util.Protocol; import java.util.concurrent.CountDownLatch; /** * The Network Service Discovery Manager class provides the API to discover services * on a network. As an example, if device A and device B are connected over a Wi-Fi Loading Loading @@ -244,7 +243,7 @@ public final class NsdManager { return name; } private static int FIRST_LISTENER_KEY = 1; private static final int FIRST_LISTENER_KEY = 1; private final INsdManager mService; private final Context mContext; Loading Loading @@ -278,6 +277,7 @@ public final class NsdManager { @VisibleForTesting public void disconnect() { mAsyncChannel.disconnect(); mHandler.getLooper().quitSafely(); } /** Loading
tests/net/java/android/net/nsd/NsdManagerTest.java +25 −5 Original line number Diff line number Diff line Loading @@ -28,6 +28,7 @@ import static org.mockito.Mockito.timeout; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; import static com.android.internal.util.TestUtils.waitForIdleHandler; import android.os.HandlerThread; import android.os.Handler; Loading @@ -38,6 +39,7 @@ import android.support.test.runner.AndroidJUnit4; import android.os.Message; import android.os.Messenger; import com.android.internal.util.AsyncChannel; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; Loading @@ -56,6 +58,8 @@ public class NsdManagerTest { @Mock INsdManager mService; MockServiceHandler mServiceHandler; NsdManager mManager; long mTimeoutMs = 100; // non-final so that tests can adjust the value. @Before Loading @@ -64,11 +68,23 @@ public class NsdManagerTest { mServiceHandler = spy(MockServiceHandler.create(mContext)); when(mService.getMessenger()).thenReturn(new Messenger(mServiceHandler)); mManager = makeManager(); } @After public void tearDown() throws Exception { waitForIdleHandler(mServiceHandler, mTimeoutMs); mServiceHandler.chan.disconnect(); mServiceHandler.stop(); if (mManager != null) { mManager.disconnect(); } } @Test public void testResolveService() { NsdManager manager = makeManager(); NsdManager manager = mManager; NsdServiceInfo request = new NsdServiceInfo("a_name", "a_type"); NsdServiceInfo reply = new NsdServiceInfo("resolved_name", "resolved_type"); Loading @@ -88,7 +104,7 @@ public class NsdManagerTest { @Test public void testParallelResolveService() { NsdManager manager = makeManager(); NsdManager manager = mManager; NsdServiceInfo request = new NsdServiceInfo("a_name", "a_type"); NsdServiceInfo reply = new NsdServiceInfo("resolved_name", "resolved_type"); Loading @@ -111,7 +127,7 @@ public class NsdManagerTest { @Test public void testRegisterService() { NsdManager manager = makeManager(); NsdManager manager = mManager; NsdServiceInfo request1 = new NsdServiceInfo("a_name", "a_type"); NsdServiceInfo request2 = new NsdServiceInfo("another_name", "another_type"); Loading Loading @@ -170,7 +186,7 @@ public class NsdManagerTest { @Test public void testDiscoverService() { NsdManager manager = makeManager(); NsdManager manager = mManager; NsdServiceInfo reply1 = new NsdServiceInfo("a_name", "a_type"); NsdServiceInfo reply2 = new NsdServiceInfo("another_name", "a_type"); Loading Loading @@ -248,7 +264,7 @@ public class NsdManagerTest { @Test public void testInvalidCalls() { NsdManager manager = new NsdManager(mContext, mService); NsdManager manager = mManager; NsdManager.RegistrationListener listener1 = mock(NsdManager.RegistrationListener.class); NsdManager.DiscoveryListener listener2 = mock(NsdManager.DiscoveryListener.class); Loading Loading @@ -351,6 +367,10 @@ public class NsdManagerTest { } } public void stop() { getLooper().quitSafely(); } public static MockServiceHandler create(Context context) { HandlerThread t = new HandlerThread("mock-service-handler"); t.start(); Loading
tests/net/java/com/android/server/NsdServiceTest.java +5 −0 Original line number Diff line number Diff line Loading @@ -95,6 +95,9 @@ public class NsdServiceTest { client2.disconnect(); verify(mDaemon, timeout(mTimeoutMs).times(1)).stop(); client1.disconnect(); client2.disconnect(); } @Test Loading Loading @@ -131,6 +134,8 @@ public class NsdServiceTest { // checks that request are cleaned verifyDaemonCommands("stop-register 2", "stop-discover 3", "stop-resolve 4"); client.disconnect(); } NsdService makeService() { Loading