From 849b81b7abc6334d04636850edda210637594f4d Mon Sep 17 00:00:00 2001 From: Hugo Benichi Date: Thu, 25 May 2017 13:42:31 +0900 Subject: @Ignore ConnectivityServiceTest#testRequestBenchmark Ignore the last remaining test in ConnectivityServiceTest with spurious failures. testRequestBenchmark has some intrinsic chances of failure due to the fact it attempts to assert elapsed time durations against a reference target. Bug: 32561414 Test: no functional change Change-Id: Ib25d76581b47997b2ef84df3e6a9fd9224b85d92 --- tests/net/java/com/android/server/ConnectivityServiceTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/net/java/com/android/server/ConnectivityServiceTest.java b/tests/net/java/com/android/server/ConnectivityServiceTest.java index 00b0f980d3cf..4fdbfe75da28 100644 --- a/tests/net/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/net/java/com/android/server/ConnectivityServiceTest.java @@ -97,6 +97,7 @@ import com.android.server.connectivity.NetworkMonitor; import com.android.server.connectivity.NetworkMonitor.CaptivePortalProbeResult; import com.android.server.net.NetworkPinner; +import org.junit.Ignore; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.mockito.Spy; @@ -2409,6 +2410,7 @@ public class ConnectivityServiceTest extends AndroidTestCase { mCm.unregisterNetworkCallback(fgCallback); } + @Ignore // This test has instrinsic chances of spurious failures: ignore for continuous testing. @SmallTest public void testRequestBenchmark() throws Exception { // TODO: turn this unit test into a real benchmarking test. -- cgit v1.2.3-59-g8ed1b From 03f6e1d18a495fb2fe691db7537e944aba1f13cc Mon Sep 17 00:00:00 2001 From: Hugo Benichi Date: Thu, 25 May 2017 13:49:32 +0900 Subject: NsdService: do not use ContentResolver directly This patch changes NsdService to call registerContentObserver in the ContentResolver class indirectly through NsdSettings. This allows to easily intercept it and mock it in unit tests, and solves test failures on the internal master branch where registerContentObserver uses final or static methods that cannot be worked around. Bug: 32561414 Bug: 62044295 Test: runtest -x frameworks/base/tests/net/../NsdServiceTest.java Change-Id: If4deb106de551746babb70196b20f21ece478850 --- .../core/java/com/android/server/NsdService.java | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/services/core/java/com/android/server/NsdService.java b/services/core/java/com/android/server/NsdService.java index 2158f60089d7..5f6bd59453ed 100644 --- a/services/core/java/com/android/server/NsdService.java +++ b/services/core/java/com/android/server/NsdService.java @@ -21,6 +21,7 @@ import android.content.ContentResolver; import android.content.Intent; import android.content.pm.PackageManager; import android.database.ContentObserver; +import android.net.Uri; import android.net.nsd.NsdServiceInfo; import android.net.nsd.DnsSdTxtRecord; import android.net.nsd.INsdManager; @@ -96,16 +97,15 @@ public class NsdService extends INsdManager.Stub { * Observes the NSD on/off setting, and takes action when changed. */ private void registerForNsdSetting() { - ContentObserver contentObserver = new ContentObserver(this.getHandler()) { + final ContentObserver contentObserver = new ContentObserver(this.getHandler()) { @Override public void onChange(boolean selfChange) { notifyEnabled(isNsdEnabled()); } }; - mContext.getContentResolver().registerContentObserver( - Settings.Global.getUriFor(Settings.Global.NSD_ON), - false, contentObserver); + final Uri uri = Settings.Global.getUriFor(Settings.Global.NSD_ON); + mNsdSettings.registerContentObserver(uri, contentObserver); } NsdStateMachine(String name, Handler handler) { @@ -885,13 +885,18 @@ public class NsdService extends INsdManager.Stub { } } + /** + * Interface which encapsulates dependencies of NsdService that are hard to mock, hard to + * override, or have side effects on global state in unit tests. + */ @VisibleForTesting public interface NsdSettings { boolean isEnabled(); void putEnabledStatus(boolean isEnabled); + void registerContentObserver(Uri uri, ContentObserver observer); static NsdSettings makeDefault(Context context) { - ContentResolver resolver = context.getContentResolver(); + final ContentResolver resolver = context.getContentResolver(); return new NsdSettings() { @Override public boolean isEnabled() { @@ -902,6 +907,11 @@ public class NsdService extends INsdManager.Stub { public void putEnabledStatus(boolean isEnabled) { Settings.Global.putInt(resolver, Settings.Global.NSD_ON, isEnabled ? 1 : 0); } + + @Override + public void registerContentObserver(Uri uri, ContentObserver observer) { + resolver.registerContentObserver(uri, false, observer); + } }; } } -- cgit v1.2.3-59-g8ed1b