diff options
author | Rebecca Silberstein <silberst@google.com> | 2017-03-03 22:12:33 -0800 |
---|---|---|
committer | Rebecca Silberstein <silberst@google.com> | 2017-03-06 12:44:41 -0800 |
commit | 2a70519406937402c761a6ce418c15e3b5598688 (patch) | |
tree | d400e4bfcf2889cbb9023b57ad4b07a524e2f711 /tests | |
parent | c7ddffabcd191c2bfd251b3d5d321e6e8f1d0bd3 (diff) |
FrameworkFacade: add helper to ease testing
WifiController, RecommendedNetworkEvaluator and SavedNetworkEvaluator
all use ContentObservers in their implementations. The tests for these
classes started failing when a change was made to include the Context
object when registering ContentObservers. This changed caused the tests
for these methods to fail due to a mix of mocks and actual objects.
This CL adds a helper to FrameworkFacade that allow the tests to pass,
and even better, updates them to use ArgumentCaptors to get a handle on
the ContentObservers. This will allow the tests to be enhanced and
easily call the observers to test functionality for changes related to
the observers.
Bug: 35908602
Test: frameworks/base/wifi/tests/runtests.sh
Test: frameworks/opt/net/wifi/tests/wifitests/runtests.sh
Change-Id: I2e4a39febf575c1335cec57b9e21e28cb3e0fffd
Diffstat (limited to 'tests')
3 files changed, 46 insertions, 12 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/RecommendedNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/RecommendedNetworkEvaluatorTest.java index f53711701..ef4f0a3fe 100644 --- a/tests/wifitests/src/com/android/server/wifi/RecommendedNetworkEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/RecommendedNetworkEvaluatorTest.java @@ -24,20 +24,16 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertSame; import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyString; -import static org.mockito.Mockito.any; -import static org.mockito.Mockito.never; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyZeroInteractions; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; import android.content.ContentResolver; import android.content.Context; +import android.database.ContentObserver; import android.net.NetworkKey; import android.net.NetworkScoreManager; import android.net.RecommendationRequest; import android.net.RecommendationResult; +import android.net.Uri; import android.net.WifiKey; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; @@ -78,6 +74,7 @@ public class RecommendedNetworkEvaluatorTest { private WifiConfiguration mTrustedWifiConfiguration; private WifiConfiguration mUntrustedWifiConfiguration; private WifiConfiguration mEphemeralWifiConfiguration; + private ContentObserver mContentObserver; @Mock private Context mContext; @Mock private ContentResolver mContentResolver; @@ -122,9 +119,16 @@ public class RecommendedNetworkEvaluatorTest { when(mFrameworkFacade.getLongSetting(mContext, Settings.Global.RECOMMENDED_NETWORK_EVALUATOR_CACHE_EXPIRY_MS, dayMillis)) .thenReturn(dayMillis); + + ArgumentCaptor<ContentObserver> observerCaptor = + ArgumentCaptor.forClass(ContentObserver.class); mRecommendedNetworkEvaluator = new RecommendedNetworkEvaluator(mContext, mContentResolver, Looper.getMainLooper(), mFrameworkFacade, mNetworkScoreManager, mWifiConfigManager, new LocalLog(0)); + verify(mFrameworkFacade).registerContentObserver(eq(mContext), any(Uri.class), eq(false), + observerCaptor.capture()); + mContentObserver = observerCaptor.getValue(); + reset(mNetworkScoreManager); when(mWifiConfigManager.getSavedNetworkForScanDetailAndCache(mTrustedScanDetail)) @@ -146,7 +150,7 @@ public class RecommendedNetworkEvaluatorTest { Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0)) .thenReturn(0); - mRecommendedNetworkEvaluator.mContentObserver.onChange(false /* unused */); + mContentObserver.onChange(false /* unused */); mRecommendedNetworkEvaluator.update(scanDetails); @@ -212,7 +216,7 @@ public class RecommendedNetworkEvaluatorTest { Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0)) .thenReturn(0); - mRecommendedNetworkEvaluator.mContentObserver.onChange(false /* unused */); + mContentObserver.onChange(false /* unused */); mRecommendedNetworkEvaluator.evaluateNetworks(null, null, null, false, false, null); diff --git a/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java index 4a35d5718..dcd2b8e18 100644 --- a/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java @@ -25,6 +25,8 @@ import static org.mockito.Mockito.*; import android.content.ContentResolver; import android.content.Context; import android.content.res.Resources; +import android.database.ContentObserver; +import android.net.Uri; import android.net.wifi.ScanResult; import android.net.wifi.WifiConfiguration; import android.os.Looper; @@ -38,6 +40,7 @@ import com.android.server.wifi.WifiNetworkSelectorTestUtil.ScanDetailsAndWifiCon import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -76,8 +79,16 @@ public class SavedNetworkEvaluatorTest { mThresholdSaturatedRssi5G = mResource.getInteger( R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz); + ArgumentCaptor<ContentObserver> observerCaptor = + ArgumentCaptor.forClass(ContentObserver.class); + mSavedNetworkEvaluator = new SavedNetworkEvaluator(mContext, mWifiConfigManager, mClock, null, Looper.getMainLooper(), mFrameworkFacade); + verify(mFrameworkFacade, times(2)).registerContentObserver(eq(mContext), any(Uri.class), + eq(false), observerCaptor.capture()); + // SavedNetworkEvaluator uses a single ContentObserver for two registrations, we only need + // to get this object once. + mContentObserver = observerCaptor.getValue(); } /** Cleans up test. */ @@ -99,6 +110,7 @@ public class SavedNetworkEvaluatorTest { private int mThresholdQualifiedRssi5G; private int mThresholdSaturatedRssi2G; private int mThresholdSaturatedRssi5G; + private ContentObserver mContentObserver; private static final String TAG = "Saved Network Evaluator Unit Test"; private void setupContext() { @@ -194,7 +206,7 @@ public class SavedNetworkEvaluatorTest { Settings.Global.CURATE_SAVED_OPEN_NETWORKS, 0)).thenReturn(1); when(mFrameworkFacade.getIntegerSetting(mContext, Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0)).thenReturn(1); - mSavedNetworkEvaluator.mContentObserver.onChange(false); + mContentObserver.onChange(false); ScanDetailsAndWifiConfigs scanDetailsAndConfigs = WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, @@ -256,7 +268,7 @@ public class SavedNetworkEvaluatorTest { Settings.Global.CURATE_SAVED_OPEN_NETWORKS, 0)).thenReturn(1); when(mFrameworkFacade.getIntegerSetting(mContext, Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0)).thenReturn(1); - mSavedNetworkEvaluator.mContentObserver.onChange(false); + mContentObserver.onChange(false); ScanDetailsAndWifiConfigs scanDetailsAndConfigs = WifiNetworkSelectorTestUtil.setupScanDetailsAndConfigStore(ssids, bssids, diff --git a/tests/wifitests/src/com/android/server/wifi/WifiControllerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiControllerTest.java index e69394897..d0aa6ffae 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiControllerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiControllerTest.java @@ -32,6 +32,8 @@ import static org.mockito.Mockito.*; import android.content.ContentResolver; import android.content.Context; +import android.database.ContentObserver; +import android.net.Uri; import android.os.WorkSource; import android.os.test.TestLooper; import android.test.suitebuilder.annotation.SmallTest; @@ -43,6 +45,7 @@ import com.android.internal.util.StateMachine; import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.MockitoAnnotations; @@ -50,6 +53,7 @@ import org.mockito.MockitoAnnotations; import java.io.ByteArrayOutputStream; import java.io.PrintWriter; import java.lang.reflect.Method; +import java.util.List; /** * Test WifiController for changes in and out of ECM and SoftAP modes. @@ -86,6 +90,11 @@ public class WifiControllerTest { @Mock WifiSettingsStore mSettingsStore; @Mock WifiStateMachine mWifiStateMachine; @Mock WifiLockManager mWifiLockManager; + @Mock ContentResolver mContentResolver; + + ContentObserver mStayAwakeObserver; + ContentObserver mWifiIdleTimeObserver; + ContentObserver mWifiSleepPolicyObserver; WifiController mWifiController; @@ -97,10 +106,19 @@ public class WifiControllerTest { initializeSettingsStore(); - when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class)); + when(mContext.getContentResolver()).thenReturn(mContentResolver); + ArgumentCaptor<ContentObserver> observerCaptor = + ArgumentCaptor.forClass(ContentObserver.class); mWifiController = new WifiController(mContext, mWifiStateMachine, mSettingsStore, mWifiLockManager, mLooper.getLooper(), mFacade); + verify(mFacade, times(3)).registerContentObserver(eq(mContext), any(Uri.class), eq(false), + observerCaptor.capture()); + + List<ContentObserver> observers = observerCaptor.getAllValues(); + mStayAwakeObserver = observers.get(0); + mWifiIdleTimeObserver = observers.get(1); + mWifiSleepPolicyObserver = observers.get(2); mWifiController.start(); mLooper.dispatchAll(); |