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 /service | |
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 'service')
4 files changed, 31 insertions, 17 deletions
diff --git a/service/java/com/android/server/wifi/FrameworkFacade.java b/service/java/com/android/server/wifi/FrameworkFacade.java index 373e68bff..c1b5e8d1e 100644 --- a/service/java/com/android/server/wifi/FrameworkFacade.java +++ b/service/java/com/android/server/wifi/FrameworkFacade.java @@ -20,7 +20,9 @@ import android.app.AppGlobals; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.database.ContentObserver; import android.net.TrafficStats; +import android.net.Uri; import android.net.ip.IpManager; import android.os.Handler; import android.os.IBinder; @@ -57,6 +59,21 @@ public class FrameworkFacade { return Settings.Global.getString(context.getContentResolver(), name); } + /** + * Helper method for classes to register a ContentObserver + * {@see ContentResolver#registerContentObserver(Uri,boolean,ContentObserver)}. + * + * @param context + * @param uri + * @param notifyForDescendants + * @param contentObserver + */ + public void registerContentObserver(Context context, Uri uri, + boolean notifyForDescendants, ContentObserver contentObserver) { + context.getContentResolver().registerContentObserver(uri, notifyForDescendants, + contentObserver); + } + public IBinder getService(String serviceName) { return ServiceManager.getService(serviceName); } diff --git a/service/java/com/android/server/wifi/RecommendedNetworkEvaluator.java b/service/java/com/android/server/wifi/RecommendedNetworkEvaluator.java index 8b332f86a..80d1501a2 100644 --- a/service/java/com/android/server/wifi/RecommendedNetworkEvaluator.java +++ b/service/java/com/android/server/wifi/RecommendedNetworkEvaluator.java @@ -42,7 +42,6 @@ import android.util.LruCache; import android.util.Pair; import android.util.Slog; -import com.android.internal.annotations.VisibleForTesting; import com.android.server.wifi.util.ScanResultUtil; import java.io.FileDescriptor; @@ -63,7 +62,7 @@ public class RecommendedNetworkEvaluator implements WifiNetworkSelector.NetworkE private final NetworkScoreManager mNetworkScoreManager; private final WifiConfigManager mWifiConfigManager; private final LocalLog mLocalLog; - @VisibleForTesting final ContentObserver mContentObserver; + private final ContentObserver mContentObserver; private final RequestedScoreCache mRequestedScoreCache; private boolean mNetworkRecommendationsEnabled; @@ -84,7 +83,7 @@ public class RecommendedNetworkEvaluator implements WifiNetworkSelector.NetworkE Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED, 0) == 1; } }; - contentResolver.registerContentObserver( + frameworkFacade.registerContentObserver(context, Settings.Global.getUriFor(Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED), false /* notifyForDescendents */, mContentObserver); mContentObserver.onChange(false /* unused */); diff --git a/service/java/com/android/server/wifi/SavedNetworkEvaluator.java b/service/java/com/android/server/wifi/SavedNetworkEvaluator.java index bd13641fd..6ca88ba1a 100644 --- a/service/java/com/android/server/wifi/SavedNetworkEvaluator.java +++ b/service/java/com/android/server/wifi/SavedNetworkEvaluator.java @@ -27,7 +27,6 @@ import android.util.LocalLog; import android.util.Pair; import com.android.internal.R; -import com.android.internal.annotations.VisibleForTesting; import java.util.ArrayList; import java.util.Arrays; @@ -52,7 +51,7 @@ public class SavedNetworkEvaluator implements WifiNetworkSelector.NetworkEvaluat private final int mSecurityAward; private final int mNoInternetPenalty; private final int mThresholdSaturatedRssi24; - @VisibleForTesting final ContentObserver mContentObserver; + private final ContentObserver mContentObserver; private boolean mCurateSavedOpenNetworks; SavedNetworkEvaluator(final Context context, WifiConfigManager configManager, Clock clock, @@ -93,10 +92,10 @@ public class SavedNetworkEvaluator implements WifiNetworkSelector.NetworkEvaluat } }; mContentObserver.onChange(false /* selfChange*/); - context.getContentResolver().registerContentObserver( + frameworkFacade.registerContentObserver(context, Settings.Global.getUriFor(Settings.Global.CURATE_SAVED_OPEN_NETWORKS), false /* notifyForDescendents */, mContentObserver); - context.getContentResolver().registerContentObserver( + frameworkFacade.registerContentObserver(context, Settings.Global.getUriFor(Settings.Global.NETWORK_RECOMMENDATIONS_ENABLED), false /* notifyForDescendents */, mContentObserver); } diff --git a/service/java/com/android/server/wifi/WifiController.java b/service/java/com/android/server/wifi/WifiController.java index d43540217..ed82b2b2d 100644 --- a/service/java/com/android/server/wifi/WifiController.java +++ b/service/java/com/android/server/wifi/WifiController.java @@ -270,13 +270,13 @@ public class WifiController extends StateMachine { } }; - mContext.getContentResolver().registerContentObserver( - Settings.Global.getUriFor(Settings.Global.STAY_ON_WHILE_PLUGGED_IN), - false, contentObserver); + mFacade.registerContentObserver(mContext, + Settings.Global.getUriFor(Settings.Global.STAY_ON_WHILE_PLUGGED_IN), false, + contentObserver); } /** - * Observes settings changes to scan always mode. + * Observes settings changes to wifi idle time. */ private void registerForWifiIdleTimeChange(Handler handler) { ContentObserver contentObserver = new ContentObserver(handler) { @@ -286,9 +286,8 @@ public class WifiController extends StateMachine { } }; - mContext.getContentResolver().registerContentObserver( - Settings.Global.getUriFor(Settings.Global.WIFI_IDLE_MS), - false, contentObserver); + mFacade.registerContentObserver(mContext, + Settings.Global.getUriFor(Settings.Global.WIFI_IDLE_MS), false, contentObserver); } /** @@ -301,9 +300,9 @@ public class WifiController extends StateMachine { readWifiSleepPolicy(); } }; - mContext.getContentResolver().registerContentObserver( - Settings.Global.getUriFor(Settings.Global.WIFI_SLEEP_POLICY), - false, contentObserver); + mFacade.registerContentObserver(mContext, + Settings.Global.getUriFor(Settings.Global.WIFI_SLEEP_POLICY), false, + contentObserver); } /** |