diff options
author | Michael Plass <mplass@google.com> | 2018-03-22 15:03:51 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2018-03-22 15:03:51 +0000 |
commit | d544a93d339393752635f908bef977dd86c6b2e3 (patch) | |
tree | 959d21fd895055ea6361409c86b61a0a04658696 /tests | |
parent | 361b7b568bf89f6ad3ed3d95e3c6abe46305c925 (diff) | |
parent | 79545bef4e8d6c1d891f7396665b5c457bc31617 (diff) |
Merge changes from topics "65216267-score-experiment-settings-pi-dev", " 65216267-score-experiment-settings-pi-dev" into pi-dev
* changes:
[wifi_score_params] plumb to the various scorers
[wifi_score_params] elaborations
Use wifi ScoringParams for rssi thresholds
Centralize access to wifi framework scoring knobs
Diffstat (limited to 'tests')
7 files changed, 282 insertions, 6 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java index e5a522e96..ff5fbe894 100644 --- a/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SavedNetworkEvaluatorTest.java @@ -72,7 +72,8 @@ public class SavedNetworkEvaluatorTest { mThresholdSaturatedRssi5G = mResource.getInteger( R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz); - mSavedNetworkEvaluator = new SavedNetworkEvaluator(mContext, mWifiConfigManager, + mSavedNetworkEvaluator = new SavedNetworkEvaluator(mContext, + new ScoringParams(mContext), mWifiConfigManager, mClock, mLocalLog, mWifiConnectivityHelper); } diff --git a/tests/wifitests/src/com/android/server/wifi/ScoringParamsTest.java b/tests/wifitests/src/com/android/server/wifi/ScoringParamsTest.java new file mode 100644 index 000000000..6b3b720e9 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/ScoringParamsTest.java @@ -0,0 +1,265 @@ +/* + * Copyright (C) 2018 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyBoolean; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.content.Context; +import android.content.res.Resources; +import android.database.ContentObserver; +import android.os.Handler; +import android.provider.Settings; +import android.support.test.filters.SmallTest; + +import com.android.internal.R; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.Spy; + +/** + * Unit tests for {@link com.android.server.wifi.ScoringParams}. + */ +@SmallTest +public class ScoringParamsTest { + + ScoringParams mScoringParams; + + /** + * Sets up for unit test + */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + setUpResources(mResources); + when(mContext.getResources()).thenReturn(mResources); + mScoringParams = new ScoringParams(); + } + + /** + * Check that thresholds are properly ordered, and in range. + */ + private void checkThresholds(int frequency) { + assertTrue(-127 <= mScoringParams.getExitRssi(frequency)); + assertTrue(mScoringParams.getExitRssi(frequency) + <= mScoringParams.getEntryRssi(frequency)); + assertTrue(mScoringParams.getEntryRssi(frequency) + <= mScoringParams.getSufficientRssi(frequency)); + assertTrue(mScoringParams.getSufficientRssi(frequency) + <= mScoringParams.getGoodRssi(frequency)); + assertTrue(mScoringParams.getGoodRssi(frequency) < 0); + } + + /** + * Test basic constructor + */ + @Test + public void testBasicConstructor() throws Exception { + mScoringParams = new ScoringParams(); + checkThresholds(2412); + checkThresholds(5020); + assertEquals(15, mScoringParams.getHorizonSeconds()); + } + + /** + * Test toString + */ + @Test + public void testToString() throws Exception { + mScoringParams = new ScoringParams(); + String expect = "rssi2=-83:-80:-73:-60,rssi5=-80:-77:-70:-57,horizon=15"; + String actual = mScoringParams.toString(); + assertEquals(expect, actual); + } + + /** + * Test complete update + */ + @Test + public void testUpdateEverything() throws Exception { + mScoringParams = new ScoringParams(); + String params = "rssi2=-86:-84:-77:-10,rssi5=-88:-77:-66:-55,horizon=3"; + assertTrue(mScoringParams.update(params)); + assertEquals(params, mScoringParams.toString()); + } + + /** + * Test partial update + */ + @Test + public void testPartialUpdate() throws Exception { + mScoringParams = new ScoringParams(); + String before = mScoringParams.toString(); + String partial = "rssi5=-88:-77:-66:-55"; + assertFalse(before.contains(partial)); + assertTrue(mScoringParams.update(partial)); + String after = mScoringParams.toString(); + assertTrue(after + " should contain " + partial, after.contains(partial)); + } + + /** + * Test some failed updates + */ + @Test + public void testUpdateFail() throws Exception { + mScoringParams = new ScoringParams(); + String before = mScoringParams.toString(); + assertFalse(mScoringParams.update("word")); + assertFalse(mScoringParams.update("42")); + assertFalse(mScoringParams.update(" ")); + assertFalse(mScoringParams.update("horizon=flat")); + assertFalse(mScoringParams.update(",,,,,,,,,,,,,,,,,,")); + assertFalse(mScoringParams.update("rssi2=-86")); + assertFalse(mScoringParams.update("rssi2=-99:-88:-77:-66:-55")); + assertFalse(mScoringParams.update("rssi5=one:two:three:four")); + assertEquals(before, mScoringParams.toString()); + } + + /** + * Test that empty updates are OK + */ + @Test + public void testEmptyUpdate() throws Exception { + mScoringParams = new ScoringParams(); + String before = mScoringParams.toString(); + assertTrue(mScoringParams.update("")); + assertTrue(mScoringParams.update(null)); + assertEquals(before, mScoringParams.toString()); + } + + /** + * Tests for obtaining values from device configuration (config.xml) + */ + int mBad2GHz, mEntry2GHz, mSufficient2GHz, mGood2GHz; + int mBad5GHz, mEntry5GHz, mSufficient5GHz, mGood5GHz; + + @Mock Context mContext; + @Spy private MockResources mResources = new MockResources(); + + private int setupIntegerResource(int resourceName, int value) { + doReturn(value).when(mResources).getInteger(resourceName); + return value; + } + + /** + * Sets up resource values for testing + * + * See frameworks/base/core/res/res/values/config.xml + */ + private void setUpResources(Resources resources) { + mBad2GHz = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_24GHz, -88); + mEntry2GHz = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_entry_rssi_threshold_24GHz, -77); + mSufficient2GHz = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_24GHz, -66); + mGood2GHz = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_24GHz, -55); + mBad5GHz = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_bad_rssi_threshold_5GHz, -80); + mEntry5GHz = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_entry_rssi_threshold_5GHz, -70); + mSufficient5GHz = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_low_rssi_threshold_5GHz, -60); + mGood5GHz = setupIntegerResource( + R.integer.config_wifi_framework_wifi_score_good_rssi_threshold_5GHz, -50); + } + + /** + * Check that we get the config.xml values, if that's what we want + */ + @Test + public void testContextConstructor() throws Exception { + mScoringParams = new ScoringParams(mContext); + + assertEquals(mBad2GHz, mScoringParams.getExitRssi(2412)); + assertEquals(mEntry2GHz, mScoringParams.getEntryRssi(2480)); + assertEquals(mSufficient2GHz, mScoringParams.getSufficientRssi(2400)); + assertEquals(mGood2GHz, mScoringParams.getGoodRssi(2499)); + assertEquals(mGood2GHz, mScoringParams.getGoodRssi(ScoringParams.BAND2)); + + assertEquals(mBad5GHz, mScoringParams.getExitRssi(5000)); + assertEquals(mEntry5GHz, mScoringParams.getEntryRssi(5010)); + assertEquals(mSufficient5GHz, mScoringParams.getSufficientRssi(5100)); + assertEquals(mGood5GHz, mScoringParams.getGoodRssi(5678)); + assertEquals(mGood5GHz, mScoringParams.getGoodRssi(ScoringParams.BAND5)); + } + + /** + * Additional mocks for handling Settings + */ + @Mock FrameworkFacade mFrameworkFacade; + @Mock Handler mHandler; + + /** + * Test getting updates from Settings + * + * Exercises the ContentObserver notification path + */ + @Test + public void testFullConstructorWithUpdatesFromSettings() throws Exception { + ArgumentCaptor<ContentObserver> captor = ArgumentCaptor.forClass(ContentObserver.class); + when(mFrameworkFacade.getStringSetting(mContext, Settings.Global.WIFI_SCORE_PARAMS)) + .thenReturn(null); + mScoringParams = new ScoringParams(mContext, mFrameworkFacade, mHandler); + verify(mFrameworkFacade) + .registerContentObserver(eq(mContext), any(), anyBoolean(), captor.capture()); + + String before = mScoringParams.toString(); + String changed = before.replace('8', '9'); + assertFalse(changed.equals(before)); + + when(mFrameworkFacade.getStringSetting(mContext, Settings.Global.WIFI_SCORE_PARAMS)) + .thenReturn(changed); + captor.getValue().onChange(/*selfChange*/ false); + assertEquals(changed, mScoringParams.toString()); + + when(mFrameworkFacade.getStringSetting(mContext, Settings.Global.WIFI_SCORE_PARAMS)) + .thenReturn(""); + captor.getValue().onChange(/*selfChange*/ false); + assertEquals(before, mScoringParams.toString()); + } + + @Test + public void testBadSettings() throws Exception { + ArgumentCaptor<ContentObserver> captor = ArgumentCaptor.forClass(ContentObserver.class); + when(mFrameworkFacade.getStringSetting(mContext, Settings.Global.WIFI_SCORE_PARAMS)) + .thenReturn(null); + mScoringParams = new ScoringParams(mContext, mFrameworkFacade, mHandler); + verify(mFrameworkFacade) + .registerContentObserver(eq(mContext), any(), anyBoolean(), captor.capture()); + + String before = mScoringParams.toString(); + String garbage = "what??"; + + when(mFrameworkFacade.getStringSetting(mContext, Settings.Global.WIFI_SCORE_PARAMS)) + .thenReturn(garbage); + captor.getValue().onChange(/*selfChange*/ false); + assertEquals(before, mScoringParams.toString()); + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/VelocityBasedConnectedScoreTest.java b/tests/wifitests/src/com/android/server/wifi/VelocityBasedConnectedScoreTest.java index 6a72522e0..949350332 100644 --- a/tests/wifitests/src/com/android/server/wifi/VelocityBasedConnectedScoreTest.java +++ b/tests/wifitests/src/com/android/server/wifi/VelocityBasedConnectedScoreTest.java @@ -87,7 +87,8 @@ public class VelocityBasedConnectedScoreTest { mWifiInfo.setFrequency(2412); when(mContext.getResources()).thenReturn(mResources); mClock = new FakeClock(); - mVelocityBasedConnectedScore = new VelocityBasedConnectedScore(mContext, mClock); + mVelocityBasedConnectedScore = new VelocityBasedConnectedScore( + new ScoringParams(mContext), mClock); } /** diff --git a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java index 8b6c2fbe0..6eaf767d1 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiConnectivityManagerTest.java @@ -304,7 +304,9 @@ public class WifiConnectivityManagerTest { } WifiConnectivityManager createConnectivityManager() { - return new WifiConnectivityManager(mContext, mWifiStateMachine, mWifiScanner, + return new WifiConnectivityManager(mContext, + new ScoringParams(mContext), + mWifiStateMachine, mWifiScanner, mWifiConfigManager, mWifiInfo, mWifiNS, mWifiConnectivityHelper, mWifiLastResortWatchdog, mOpenNetworkNotifier, mCarrierNetworkNotifier, mCarrierNetworkConfig, mWifiMetrics, mLooper.getLooper(), mClock, mLocalLog, true, diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java index 1163b0772..6fab007b7 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSelectorTest.java @@ -65,7 +65,9 @@ public class WifiNetworkSelectorTest { setupWifiInfo(); mLocalLog = new LocalLog(512); - mWifiNetworkSelector = new WifiNetworkSelector(mContext, mWifiConfigManager, mClock, + mWifiNetworkSelector = new WifiNetworkSelector(mContext, + new ScoringParams(mContext), + mWifiConfigManager, mClock, mLocalLog); mWifiNetworkSelector.registerNetworkEvaluator(mDummyEvaluator, 1); mDummyEvaluator.setEvaluatorToSelectCandidate(true); @@ -179,6 +181,7 @@ public class WifiNetworkSelectorTest { // simulate a disconnected state when(mWifiInfo.is24GHz()).thenReturn(true); when(mWifiInfo.is5GHz()).thenReturn(false); + when(mWifiInfo.getFrequency()).thenReturn(2400); when(mWifiInfo.getRssi()).thenReturn(-70); when(mWifiInfo.getNetworkId()).thenReturn(WifiConfiguration.INVALID_NETWORK_ID); when(mWifiInfo.getBSSID()).thenReturn(null); @@ -361,6 +364,7 @@ public class WifiNetworkSelectorTest { when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); when(mWifiInfo.is24GHz()).thenReturn(false); when(mWifiInfo.is5GHz()).thenReturn(true); + when(mWifiInfo.getFrequency()).thenReturn(5000); when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + WifiNetworkSelector.MINIMUM_NETWORK_SELECTION_INTERVAL_MS + 2000); @@ -407,6 +411,7 @@ public class WifiNetworkSelectorTest { when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); when(mWifiInfo.is24GHz()).thenReturn(false); when(mWifiInfo.is5GHz()).thenReturn(true); + when(mWifiInfo.getFrequency()).thenReturn(5000); when(mWifiInfo.getRssi()).thenReturn(levels[0]); when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() @@ -483,6 +488,7 @@ public class WifiNetworkSelectorTest { when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); when(mWifiInfo.is24GHz()).thenReturn(true); when(mWifiInfo.is5GHz()).thenReturn(false); + when(mWifiInfo.getFrequency()).thenReturn(2400); when(mWifiInfo.getRssi()).thenReturn(levels[0]); when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + WifiNetworkSelector.MINIMUM_NETWORK_SELECTION_INTERVAL_MS + 2000); @@ -910,6 +916,7 @@ public class WifiNetworkSelectorTest { when(mWifiInfo.getBSSID()).thenReturn(bssids[0]); when(mWifiInfo.is24GHz()).thenReturn(!ScanResult.is5GHz(freqs[0])); when(mWifiInfo.is5GHz()).thenReturn(ScanResult.is5GHz(freqs[0])); + when(mWifiInfo.getFrequency()).thenReturn(freqs[0]); when(mClock.getElapsedSinceBootMillis()).thenReturn(SystemClock.elapsedRealtime() + WifiNetworkSelector.MINIMUM_NETWORK_SELECTION_INTERVAL_MS + 2000); diff --git a/tests/wifitests/src/com/android/server/wifi/WifiScoreReportTest.java b/tests/wifitests/src/com/android/server/wifi/WifiScoreReportTest.java index 1cef87a63..67c9a736b 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiScoreReportTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiScoreReportTest.java @@ -123,7 +123,7 @@ public class WifiScoreReportTest { int trimSize = 5; when(mContext.getResources()).thenReturn(mResources); mClock = new FakeClock(); - mWifiScoreReport = new WifiScoreReport(mContext, mClock); + mWifiScoreReport = new WifiScoreReport(new ScoringParams(mContext), mClock); } /** diff --git a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java index 0907c77ee..5d5ef3d9f 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiStateMachineTest.java @@ -416,7 +416,7 @@ public class WifiStateMachineTest { when(mWifiInjector.getWifiPermissionsWrapper()).thenReturn(mWifiPermissionsWrapper); when(mWifiInjector.getWakeupController()).thenReturn(mWakeupController); when(mWifiInjector.getScanRequestProxy()).thenReturn(mScanRequestProxy); - + when(mWifiInjector.getScoringParams()).thenReturn(new ScoringParams()); when(mWifiNative.setupInterfaceForClientMode(anyBoolean(), any())) .thenReturn(WIFI_IFACE_NAME); when(mWifiNative.initialize()).thenReturn(true); |