diff options
author | Sohani Rao <sohanirao@google.com> | 2017-01-09 16:50:21 -0800 |
---|---|---|
committer | Sohani Rao <sohanirao@google.com> | 2017-01-25 12:22:25 -0800 |
commit | d9e0e830e4b4f3c8397460cf5e197345df62c802 (patch) | |
tree | b95f0db27ce1b81e949982a01989f7013ca47cac /service | |
parent | 6dada2c3bc67349ae08befd0f1fa76cf7e6ea028 (diff) |
Fix WifiScanningServiceImpl stand-alone unit test failure
WifiAsyncChannel and WifiHandler objects use lazy intialization for the
logging objects. WifiInjector.getInstance() may return a null object if
the unit test is run by itself or if earlier tests have not created it.
If the unit tests are able to update these instances with a logging
objects that are not null, then unit test can pass stand-alone.
- Add a method in FrameworkFacade to return new instances of
WifiAsyncChannel
- Update WifiAsyncChannel to expose setWifiLog() API for testing only
- Call this method on instances of WifiAsyncChannel returned by the mock
of FrameworkFacade factory to ensure mLog is not null
- Add a method in WifiScanningServiceImpl, also meant for testing only
to update WifiHandler with logging object
- Change unit tests to call this method after starting scanning service
Bug: 34127212
Test: WifiScanningServiceTest by itself and all unit tests pass
Change-Id: I0bc7a55d6d6862e2360bfb20897828e84358dd39
Merged-In: I0bc7a55d6d6862e2360bfb20897828e84358dd39
Diffstat (limited to 'service')
3 files changed, 31 insertions, 2 deletions
diff --git a/service/java/com/android/server/wifi/FrameworkFacade.java b/service/java/com/android/server/wifi/FrameworkFacade.java index 1c3c5e7ba..373e68bff 100644 --- a/service/java/com/android/server/wifi/FrameworkFacade.java +++ b/service/java/com/android/server/wifi/FrameworkFacade.java @@ -29,6 +29,8 @@ import android.os.ServiceManager; import android.provider.Settings; import android.telephony.CarrierConfigManager; +import com.android.server.wifi.util.WifiAsyncChannel; + /** * This class allows overriding objects with mocks to write unit tests */ @@ -113,4 +115,13 @@ public class FrameworkFacade { public int checkUidPermission(String permName, int uid) throws RemoteException { return AppGlobals.getPackageManager().checkUidPermission(permName, uid); } + + /** + * Create a new instance of WifiAsyncChannel + * @param tag String corresponding to the service creating the channel + * @return WifiAsyncChannel object created + */ + public WifiAsyncChannel makeWifiAsyncChannel(String tag) { + return new WifiAsyncChannel(tag); + } } diff --git a/service/java/com/android/server/wifi/scanner/WifiScanningServiceImpl.java b/service/java/com/android/server/wifi/scanner/WifiScanningServiceImpl.java index 54abce0b7..b34410aa0 100644 --- a/service/java/com/android/server/wifi/scanner/WifiScanningServiceImpl.java +++ b/service/java/com/android/server/wifi/scanner/WifiScanningServiceImpl.java @@ -46,12 +46,14 @@ import android.util.LocalLog; import android.util.Log; import android.util.Pair; +import com.android.internal.annotations.VisibleForTesting; import com.android.internal.app.IBatteryStats; import com.android.internal.util.AsyncChannel; import com.android.internal.util.Protocol; import com.android.internal.util.State; import com.android.internal.util.StateMachine; import com.android.server.wifi.Clock; +import com.android.server.wifi.FrameworkFacade; import com.android.server.wifi.WifiInjector; import com.android.server.wifi.WifiLog; import com.android.server.wifi.WifiMetrics; @@ -59,7 +61,6 @@ import com.android.server.wifi.WifiMetricsProto; import com.android.server.wifi.WifiNative; import com.android.server.wifi.WifiStateMachine; import com.android.server.wifi.scanner.ChannelHelper.ChannelCollection; -import com.android.server.wifi.util.WifiAsyncChannel; import com.android.server.wifi.util.WifiHandler; import java.io.FileDescriptor; @@ -152,7 +153,7 @@ public class WifiScanningServiceImpl extends IWifiScanner.Stub { return; } - AsyncChannel ac = new WifiAsyncChannel(TAG); + AsyncChannel ac = mFrameworkFacade.makeWifiAsyncChannel(TAG); ac.connected(mContext, this, msg.replyTo); client = new ExternalClientInfo(msg.sendingUid, msg.replyTo, ac); @@ -282,6 +283,7 @@ public class WifiScanningServiceImpl extends IWifiScanner.Stub { private final AlarmManager mAlarmManager; private final WifiMetrics mWifiMetrics; private final Clock mClock; + private final FrameworkFacade mFrameworkFacade; WifiScanningServiceImpl(Context context, Looper looper, WifiScannerImpl.WifiScannerImplFactory scannerImplFactory, IBatteryStats batteryStats, @@ -295,6 +297,7 @@ public class WifiScanningServiceImpl extends IWifiScanner.Stub { mWifiMetrics = wifiInjector.getWifiMetrics(); mClock = wifiInjector.getClock(); mLog = wifiInjector.makeLog(TAG); + mFrameworkFacade = wifiInjector.getFrameworkFacade(); mPreviousSchedule = null; } @@ -330,6 +333,15 @@ public class WifiScanningServiceImpl extends IWifiScanner.Stub { mPnoScanStateMachine.start(); } + /** + * Provide a way for unit tests to set valid log object in the WifiHandler + * @param log WifiLog object to assign to the clientHandler + */ + @VisibleForTesting + public void setWifiHandlerLogForTest(WifiLog log) { + mClientHandler.setWifiLog(log); + } + private static boolean isWorkSourceValid(WorkSource workSource) { return workSource != null && workSource.size() > 0 && workSource.get(0) >= 0; } diff --git a/service/java/com/android/server/wifi/util/WifiAsyncChannel.java b/service/java/com/android/server/wifi/util/WifiAsyncChannel.java index ccdfb522f..08025e3ea 100644 --- a/service/java/com/android/server/wifi/util/WifiAsyncChannel.java +++ b/service/java/com/android/server/wifi/util/WifiAsyncChannel.java @@ -19,6 +19,7 @@ package com.android.server.wifi.util; import android.annotation.NonNull; import android.os.Message; +import com.android.internal.annotations.VisibleForTesting; import com.android.internal.util.AsyncChannel; import com.android.server.wifi.WifiInjector; import com.android.server.wifi.WifiLog; @@ -95,4 +96,9 @@ public class WifiAsyncChannel extends AsyncChannel { .flush(); return replyMessage; } + + @VisibleForTesting + public void setWifiLog(WifiLog log) { + mLog = log; + } } |