diff options
5 files changed, 38 insertions, 25 deletions
diff --git a/service/java/com/android/server/wifi/WifiConnectivityManager.java b/service/java/com/android/server/wifi/WifiConnectivityManager.java index 5210adcaa..0576ba566 100644 --- a/service/java/com/android/server/wifi/WifiConnectivityManager.java +++ b/service/java/com/android/server/wifi/WifiConnectivityManager.java @@ -465,19 +465,19 @@ public class WifiConnectivityManager { */ WifiConnectivityManager(Context context, WifiStateMachine stateMachine, WifiScanner scanner, WifiConfigManager configManager, WifiInfo wifiInfo, - WifiNetworkSelector networkSelector, WifiInjector wifiInjector, Looper looper, - boolean enable) { + WifiNetworkSelector networkSelector, WifiLastResortWatchdog wifiLastResortWatchdog, + WifiMetrics wifiMetrics, Looper looper, Clock clock, boolean enable) { mStateMachine = stateMachine; mScanner = scanner; mConfigManager = configManager; mWifiInfo = wifiInfo; mNetworkSelector = networkSelector; mLocalLog = networkSelector.getLocalLog(); - mWifiLastResortWatchdog = wifiInjector.getWifiLastResortWatchdog(); - mWifiMetrics = wifiInjector.getWifiMetrics(); + mWifiLastResortWatchdog = wifiLastResortWatchdog; + mWifiMetrics = wifiMetrics; mAlarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); mEventHandler = new Handler(looper); - mClock = wifiInjector.getClock(); + mClock = clock; mConnectionAttemptTimeStamps = new LinkedList<>(); mMin5GHzRssi = context.getResources().getInteger( diff --git a/service/java/com/android/server/wifi/WifiInjector.java b/service/java/com/android/server/wifi/WifiInjector.java index bfe6446af..63787dfb0 100644 --- a/service/java/com/android/server/wifi/WifiInjector.java +++ b/service/java/com/android/server/wifi/WifiInjector.java @@ -21,6 +21,7 @@ import android.net.wifi.IApInterface; import android.net.wifi.IWifiScanner; import android.net.wifi.IWificond; import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiInfo; import android.net.wifi.WifiScanner; import android.os.HandlerThread; import android.os.IBinder; @@ -358,6 +359,23 @@ public class WifiInjector { return mWifiNetworkSelector; } + /** + * Obtain a new instance of WifiConnectivityManager. + * + * Create and return a new WifiConnectivityManager. + * @param wifiInfo WifiInfo object for updating wifi state. + * @param hasConnectionRequests boolean indicating if WifiConnectivityManager to start + * immediately based on connection requests. + */ + public WifiConnectivityManager makeWifiConnectivityManager(WifiInfo wifiInfo, + boolean hasConnectionRequests) { + return new WifiConnectivityManager(mContext, mWifiStateMachine, getWifiScanner(), + mWifiConfigManager, wifiInfo, mWifiNetworkSelector, + mWifiLastResortWatchdog, mWifiMetrics, + mWifiStateMachineHandlerThread.getLooper(), mClock, + hasConnectionRequests); + } + public WifiPermissionsUtil getWifiPermissionsUtil() { return mWifiPermissionsUtil; } diff --git a/service/java/com/android/server/wifi/WifiStateMachine.java b/service/java/com/android/server/wifi/WifiStateMachine.java index ad13d00a0..952300636 100644 --- a/service/java/com/android/server/wifi/WifiStateMachine.java +++ b/service/java/com/android/server/wifi/WifiStateMachine.java @@ -4137,10 +4137,9 @@ public class WifiStateMachine extends StateMachine implements WifiNative.WifiRss mWifiScanner = mWifiInjector.getWifiScanner(); synchronized (mWifiReqCountLock) { - mWifiConnectivityManager = new WifiConnectivityManager(mContext, - WifiStateMachine.this, mWifiScanner, mWifiConfigManager, mWifiInfo, - mWifiNetworkSelector, mWifiInjector, - getHandler().getLooper(), hasConnectionRequests()); + mWifiConnectivityManager = + mWifiInjector.makeWifiConnectivityManager(mWifiInfo, + hasConnectionRequests()); mWifiConnectivityManager.setUntrustedConnectionAllowed(mUntrustedReqCount > 0); mWifiConnectivityManager.handleScreenStateChanged(mScreenOn); } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index 1c33a2dda..2a57a6a78 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -50,6 +50,8 @@ import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; import java.nio.charset.StandardCharsets; import java.util.ArrayList; @@ -66,7 +68,7 @@ public class WifiConnectivityManagerTest { */ @Before public void setUp() throws Exception { - mWifiInjector = mockWifiInjector(); + MockitoAnnotations.initMocks(this); mResource = mockResource(); mAlarmManager = new TestAlarmManager(); mContext = mockContext(); @@ -100,10 +102,9 @@ public class WifiConnectivityManagerTest { private ScanData mScanData; private WifiConfigManager mWifiConfigManager; private WifiInfo mWifiInfo; - private Clock mClock = mock(Clock.class); - private WifiLastResortWatchdog mWifiLastResortWatchdog; - private WifiMetrics mWifiMetrics; - private WifiInjector mWifiInjector; + @Mock private Clock mClock; + @Mock private WifiLastResortWatchdog mWifiLastResortWatchdog; + @Mock private WifiMetrics mWifiMetrics; private MockResources mResources; private static final int CANDIDATE_NETWORK_ID = 0; @@ -249,19 +250,10 @@ public class WifiConnectivityManagerTest { return wifiConfigManager; } - WifiInjector mockWifiInjector() { - WifiInjector wifiInjector = mock(WifiInjector.class); - mWifiLastResortWatchdog = mock(WifiLastResortWatchdog.class); - mWifiMetrics = mock(WifiMetrics.class); - when(wifiInjector.getWifiLastResortWatchdog()).thenReturn(mWifiLastResortWatchdog); - when(wifiInjector.getWifiMetrics()).thenReturn(mWifiMetrics); - when(wifiInjector.getClock()).thenReturn(mClock); - return wifiInjector; - } - WifiConnectivityManager createConnectivityManager() { return new WifiConnectivityManager(mContext, mWifiStateMachine, mWifiScanner, - mWifiConfigManager, mWifiInfo, mWifiNS, mWifiInjector, mLooper.getLooper(), true); + mWifiConfigManager, mWifiInfo, mWifiNS, mWifiLastResortWatchdog, + mWifiMetrics, mLooper.getLooper(), mClock, true); } /** diff --git a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java index 7fac3ba5e..349f26983 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java @@ -37,6 +37,7 @@ import android.net.wifi.IWificond; import android.net.wifi.ScanResult; import android.net.wifi.SupplicantState; import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.net.wifi.WifiScanner; import android.net.wifi.WifiSsid; @@ -318,6 +319,7 @@ public class WifiStateMachineTest { @Mock IBinder mClientInterfaceBinder; @Mock WifiConfigManager mWifiConfigManager; @Mock WifiSupplicantControl mWifiSupplicantControl; + @Mock WifiConnectivityManager mWifiConnectivityManager; public WifiStateMachineTest() throws Exception { } @@ -349,6 +351,8 @@ public class WifiStateMachineTest { when(mWifiInjector.getWifiSupplicantControl()).thenReturn(mWifiSupplicantControl); when(mWifiInjector.getWifiScanner()).thenReturn(mWifiScanner); when(mWifiInjector.getWifiNetworkSelector()).thenReturn(mock(WifiNetworkSelector.class)); + when(mWifiInjector.makeWifiConnectivityManager(any(WifiInfo.class), anyBoolean())) + .thenReturn(mWifiConnectivityManager); when(mWifiNative.getInterfaceName()).thenReturn("mockWlan"); when(mWifiSupplicantControl.getFrameworkNetworkId(anyInt())).thenReturn(0); |