diff options
author | Roshan Pius <rpius@google.com> | 2020-02-19 16:28:43 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2020-02-19 16:28:43 +0000 |
commit | e533890ccc347526e4f95686a1e40fb402dbdebe (patch) | |
tree | d646a4937b559d4a8a71ddaded8168990928b7e8 /tests | |
parent | ec328129b11d7111971b8e75c5a2b75520d59389 (diff) | |
parent | 471cd9f675ccff5d3176673a0ea57ab150679725 (diff) |
Merge changes from topics "ap_shutdown_timeout_flag", "scan_always_available_API"
* changes:
WifiSettingStore: Migrate to settings config store
WifiSettingConfigStore: Add a new config store data source
ScoringParams: Stop using @hide Settings.Global value
WifiService: Add API for scan always available toggle
WifiApConfigStore: Store the shutdown enabled flag in config store
Diffstat (limited to 'tests')
10 files changed, 423 insertions, 149 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/ScanRequestProxyTest.java b/tests/wifitests/src/com/android/server/wifi/ScanRequestProxyTest.java index 7c3cc9cdf..41b6b66a9 100644 --- a/tests/wifitests/src/com/android/server/wifi/ScanRequestProxyTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ScanRequestProxyTest.java @@ -20,6 +20,7 @@ import static android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREG import static com.android.server.wifi.ScanRequestProxy.SCAN_REQUEST_THROTTLE_MAX_IN_TIME_WINDOW_FG_APPS; import static com.android.server.wifi.ScanRequestProxy.SCAN_REQUEST_THROTTLE_TIME_WINDOW_FG_APPS_MS; +import static com.android.server.wifi.WifiSettingsConfigStore.WIFI_SCAN_THROTTLE_ENABLED; import static org.junit.Assert.*; import static org.mockito.Mockito.*; @@ -38,7 +39,6 @@ import android.os.IBinder; import android.os.UserHandle; import android.os.WorkSource; import android.os.test.TestLooper; -import android.provider.Settings; import androidx.test.filters.SmallTest; @@ -85,7 +85,7 @@ public class ScanRequestProxyTest extends WifiBaseTest { @Mock private WifiPermissionsUtil mWifiPermissionsUtil; @Mock private WifiMetrics mWifiMetrics; @Mock private Clock mClock; - @Mock private FrameworkFacade mFrameworkFacade; + @Mock private WifiSettingsConfigStore mWifiSettingsConfigStore; @Mock private WifiNetworkSuggestionsManager mWifiNetworkSuggestionsManager; @Mock private IScanResultsCallback mScanResultsCallback; @Mock private IScanResultsCallback mAnotherScanResultsCallback; @@ -138,14 +138,13 @@ public class ScanRequestProxyTest extends WifiBaseTest { new int[]{WifiScanner.WIFI_BAND_BOTH_WITH_DFS}); // Scan throttling is enabled by default. - when(mFrameworkFacade.getIntegerSetting( - eq(mContext), eq(Settings.Global.WIFI_SCAN_THROTTLE_ENABLED), anyInt())) - .thenReturn(1); + when(mWifiSettingsConfigStore.getBoolean(eq(WIFI_SCAN_THROTTLE_ENABLED), anyBoolean())) + .thenReturn(true); mLooper = new TestLooper(); mScanRequestProxy = new ScanRequestProxy(mContext, mAppOps, mActivityManager, mWifiInjector, mWifiConfigManager, mWifiPermissionsUtil, mWifiMetrics, mClock, - mFrameworkFacade, new Handler(mLooper.getLooper())); + new Handler(mLooper.getLooper()), mWifiSettingsConfigStore); when(mScanResultsCallback.asBinder()).thenReturn(mBinder); when(mAnotherScanResultsCallback.asBinder()).thenReturn(mAnotherBinder); } @@ -644,10 +643,8 @@ public class ScanRequestProxyTest extends WifiBaseTest { public void testSuccessiveScanRequestFromSameAppWhenThrottlingIsDisabledNotThrottled() { // Triggers the scan throttle setting registration. testEnableScanning(); - // Disable scan throttling & invoke the content observer callback. mScanRequestProxy.setScanThrottleEnabled(false); - verify(mFrameworkFacade).setIntegerSetting( - eq(mContext), eq(Settings.Global.WIFI_SCAN_THROTTLE_ENABLED), anyInt()); + verify(mWifiSettingsConfigStore).putBoolean(WIFI_SCAN_THROTTLE_ENABLED, false); long firstRequestMs = 782; when(mClock.getElapsedSinceBootMillis()).thenReturn(firstRequestMs); diff --git a/tests/wifitests/src/com/android/server/wifi/ScoringParamsTest.java b/tests/wifitests/src/com/android/server/wifi/ScoringParamsTest.java index 4bb67a8f4..3c07bce1c 100644 --- a/tests/wifitests/src/com/android/server/wifi/ScoringParamsTest.java +++ b/tests/wifitests/src/com/android/server/wifi/ScoringParamsTest.java @@ -19,19 +19,12 @@ 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.net.wifi.WifiInfo; -import android.os.Handler; -import android.provider.Settings; import androidx.test.filters.SmallTest; @@ -39,7 +32,6 @@ import com.android.wifi.resources.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; @@ -328,57 +320,4 @@ public class ScoringParamsTest extends WifiBaseTest { assertEquals(mGood6GHz, mScoringParams.getGoodRssi(6275)); assertEquals(mGood6GHz, mScoringParams.getGoodRssi(ScoringParams.BAND6)); } - - /** - * 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/SoftApBackupRestoreTest.java b/tests/wifitests/src/com/android/server/wifi/SoftApBackupRestoreTest.java index 331ba92e4..c7565c6ca 100644 --- a/tests/wifitests/src/com/android/server/wifi/SoftApBackupRestoreTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SoftApBackupRestoreTest.java @@ -21,22 +21,32 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.*; import static org.mockito.Mockito.*; +import android.content.Context; import android.net.MacAddress; import android.net.wifi.SoftApConfiguration; import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiOemMigrationHook; import android.util.BackupUtils; import androidx.test.filters.SmallTest; +import com.android.dx.mockito.inline.extended.ExtendedMockito; import com.android.server.wifi.util.ApConfigUtil; +import org.junit.After; import org.junit.Before; import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.mockito.MockitoSession; +import org.mockito.quality.Strictness; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; /** * Unit tests for {@link com.android.server.wifi.SoftApBackupRestore}. @@ -44,15 +54,25 @@ import java.util.ArrayList; @SmallTest public class SoftApBackupRestoreTest extends WifiBaseTest { + @Mock private Context mContext; + @Mock private WifiOemMigrationHook.SettingsMigrationData mOemMigrationData; private SoftApBackupRestore mSoftApBackupRestore; + private final ArrayList<MacAddress> mTestBlockedList = new ArrayList<>(); + private final ArrayList<MacAddress> mTestAllowedList = new ArrayList<>(); private static final int LAST_WIFICOFIGURATION_BACKUP_VERSION = 3; private static final boolean TEST_CLIENTCONTROLENABLE = false; private static final int TEST_MAXNUMBEROFCLIENTS = 10; private static final int TEST_SHUTDOWNTIMEOUTMILLIS = 600_000; - private static final ArrayList<MacAddress> TEST_BLOCKEDLIST = new ArrayList<>(); private static final String TEST_BLOCKED_CLIENT = "11:22:33:44:55:66"; - private static final ArrayList<MacAddress> TEST_ALLOWEDLIST = new ArrayList<>(); private static final String TEST_ALLOWED_CLIENT = "aa:bb:cc:dd:ee:ff"; + private static final String TEST_SSID = "TestAP"; + private static final String TEST_PASSPHRASE = "TestPskPassphrase"; + private static final int TEST_SECURITY = SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION; + private static final int TEST_BAND = SoftApConfiguration.BAND_5GHZ; + private static final int TEST_CHANNEL = 40; + private static final boolean TEST_HIDDEN = false; + + MockitoSession mSession; /** * Asserts that the WifiConfigurations equal to SoftApConfiguration. @@ -76,10 +96,24 @@ public class SoftApBackupRestoreTest extends WifiBaseTest { assertEquals(backup.hiddenSSID, restore.isHiddenSsid()); } - @Before public void setUp() throws Exception { - mSoftApBackupRestore = new SoftApBackupRestore(); + MockitoAnnotations.initMocks(this); + + mSession = ExtendedMockito.mockitoSession() + .mockStatic(WifiOemMigrationHook.class, withSettings().lenient()) + .strictness(Strictness.LENIENT) + .startMocking(); + when(WifiOemMigrationHook.loadFromSettings(any(Context.class))) + .thenReturn(mOemMigrationData); + when(mOemMigrationData.isSoftApTimeoutEnabled()).thenReturn(true); + + mSoftApBackupRestore = new SoftApBackupRestore(mContext); + } + + @After + public void tearDown() throws Exception { + mSession.finishMocking(); } /** @@ -169,6 +203,7 @@ public class SoftApBackupRestoreTest extends WifiBaseTest { configBuilder.setPassphrase("TestPskPassphrase", SoftApConfiguration.SECURITY_TYPE_WPA3_SAE); configBuilder.setHiddenSsid(true); + configBuilder.setAutoShutdownEnabled(true); SoftApConfiguration config = configBuilder.build(); byte[] data = mSoftApBackupRestore.retrieveBackupDataFromSoftApConfiguration(config); @@ -190,6 +225,7 @@ public class SoftApBackupRestoreTest extends WifiBaseTest { configBuilder.setPassphrase("TestPskPassphrase", SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION); configBuilder.setHiddenSsid(true); + configBuilder.setAutoShutdownEnabled(false); SoftApConfiguration config = configBuilder.build(); byte[] data = mSoftApBackupRestore.retrieveBackupDataFromSoftApConfiguration(config); @@ -203,20 +239,19 @@ public class SoftApBackupRestoreTest extends WifiBaseTest { * Verifies that the serialization/de-serialization for wpa3-sae-transition softap config. */ @Test - public void testSoftApConfigBackupAndRestoreWithMaxShutDonwClientList() throws Exception { - TEST_BLOCKEDLIST.add(MacAddress.fromString(TEST_BLOCKED_CLIENT)); - TEST_ALLOWEDLIST.add(MacAddress.fromString(TEST_ALLOWED_CLIENT)); + public void testSoftApConfigBackupAndRestoreWithMaxShutDownClientList() throws Exception { + mTestBlockedList.add(MacAddress.fromString(TEST_BLOCKED_CLIENT)); + mTestAllowedList.add(MacAddress.fromString(TEST_ALLOWED_CLIENT)); SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder(); - configBuilder.setSsid("TestAP"); + configBuilder.setSsid(TEST_SSID); configBuilder.setBand(SoftApConfiguration.BAND_5GHZ); configBuilder.setChannel(40, SoftApConfiguration.BAND_5GHZ); - configBuilder.setPassphrase("TestPskPassphrase", - SoftApConfiguration.SECURITY_TYPE_WPA3_SAE_TRANSITION); - configBuilder.setHiddenSsid(true); + configBuilder.setPassphrase(TEST_PASSPHRASE, TEST_SECURITY); + configBuilder.setHiddenSsid(TEST_HIDDEN); configBuilder.setMaxNumberOfClients(TEST_MAXNUMBEROFCLIENTS); configBuilder.setShutdownTimeoutMillis(TEST_SHUTDOWNTIMEOUTMILLIS); configBuilder.enableClientControlByUser(TEST_CLIENTCONTROLENABLE); - configBuilder.setClientList(TEST_BLOCKEDLIST, TEST_ALLOWEDLIST); + configBuilder.setClientList(mTestBlockedList, mTestAllowedList); SoftApConfiguration config = configBuilder.build(); byte[] data = mSoftApBackupRestore.retrieveBackupDataFromSoftApConfiguration(config); @@ -225,4 +260,74 @@ public class SoftApBackupRestoreTest extends WifiBaseTest { assertThat(config).isEqualTo(restoredConfig); } + + /** + * Verifies that the restore of version 5 backup data will read the auto shutdown enable/disable + * tag from {@link WifiOemMigrationHook#loadFromSettings(Context)} + */ + @Test + public void testSoftApConfigRestoreFromVersion5() throws Exception { + mTestBlockedList.add(MacAddress.fromString(TEST_BLOCKED_CLIENT)); + mTestAllowedList.add(MacAddress.fromString(TEST_ALLOWED_CLIENT)); + SoftApConfiguration.Builder configBuilder = new SoftApConfiguration.Builder(); + configBuilder.setSsid(TEST_SSID); + configBuilder.setBand(TEST_BAND); + configBuilder.setChannel(TEST_CHANNEL, TEST_BAND); + configBuilder.setPassphrase(TEST_PASSPHRASE, TEST_SECURITY); + configBuilder.setHiddenSsid(TEST_HIDDEN); + configBuilder.setMaxNumberOfClients(TEST_MAXNUMBEROFCLIENTS); + configBuilder.setShutdownTimeoutMillis(TEST_SHUTDOWNTIMEOUTMILLIS); + configBuilder.enableClientControlByUser(TEST_CLIENTCONTROLENABLE); + configBuilder.setClientList(mTestBlockedList, mTestAllowedList); + + // Toggle on when migrating. + when(mOemMigrationData.isSoftApTimeoutEnabled()).thenReturn(true); + SoftApConfiguration expectedConfig = configBuilder.setAutoShutdownEnabled(true).build(); + SoftApConfiguration restoredConfig = + mSoftApBackupRestore.retrieveSoftApConfigurationFromBackupData( + retrieveVersion5BackupDataFromSoftApConfiguration(expectedConfig)); + assertEquals(expectedConfig, restoredConfig); + + // Toggle off when migrating. + when(mOemMigrationData.isSoftApTimeoutEnabled()).thenReturn(false); + expectedConfig = configBuilder.setAutoShutdownEnabled(false).build(); + restoredConfig = mSoftApBackupRestore.retrieveSoftApConfigurationFromBackupData( + retrieveVersion5BackupDataFromSoftApConfiguration(expectedConfig)); + assertEquals(expectedConfig, restoredConfig); + } + + /** + * This is a copy of the old serialization code (version 5) + * + * Changes: Version = 5, AutoShutdown tag is missing. + */ + private byte[] retrieveVersion5BackupDataFromSoftApConfiguration(SoftApConfiguration config) + throws Exception { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + DataOutputStream out = new DataOutputStream(baos); + + out.writeInt(5); + BackupUtils.writeString(out, config.getSsid()); + out.writeInt(config.getBand()); + out.writeInt(config.getChannel()); + BackupUtils.writeString(out, config.getPassphrase()); + out.writeInt(config.getSecurityType()); + out.writeBoolean(config.isHiddenSsid()); + out.writeInt(config.getMaxNumberOfClients()); + out.writeInt(config.getShutdownTimeoutMillis()); + out.writeBoolean(config.isClientControlByUserEnabled()); + writeMacAddressList(out, config.getBlockedClientList()); + writeMacAddressList(out, config.getAllowedClientList()); + return baos.toByteArray(); + } + + private void writeMacAddressList(DataOutputStream out, List<MacAddress> macList) + throws IOException { + out.writeInt(macList.size()); + Iterator<MacAddress> iterator = macList.iterator(); + while (iterator.hasNext()) { + byte[] mac = iterator.next().toByteArray(); + out.write(mac, 0, 6); + } + } } diff --git a/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java b/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java index 7b4c1cee6..e37e3a71e 100644 --- a/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SoftApManagerTest.java @@ -54,9 +54,7 @@ import android.app.test.TestAlarmManager; import android.content.Context; import android.content.Intent; import android.content.res.Resources; -import android.database.ContentObserver; import android.net.MacAddress; -import android.net.Uri; import android.net.wifi.SoftApCapability; import android.net.wifi.SoftApConfiguration; import android.net.wifi.SoftApConfiguration.Builder; @@ -111,9 +109,8 @@ public class SoftApManagerTest extends WifiBaseTest { SoftApInfo.CHANNEL_WIDTH_20MHZ_NOHT; private static final int TEST_AP_BANDWIDTH_IN_SOFTAPINFO = SoftApInfo.CHANNEL_WIDTH_20MHZ_NOHT; private static final int[] EMPTY_CHANNEL_ARRAY = {}; - private final SoftApConfiguration mDefaultApConfig = createDefaultApConfig(); + private SoftApConfiguration mDefaultApConfig = createDefaultApConfig(); - private ContentObserver mContentObserver; private TestLooper mLooper; private TestAlarmManager mAlarmManager; private SoftApInfo mTestSoftApInfo; @@ -1473,9 +1470,10 @@ public class SoftApManagerTest extends WifiBaseTest { mTestSoftApCapability); startSoftApAndVerifyEnabled(apConfig); - when(mFrameworkFacade.getIntegerSetting( - mContext, Settings.Global.SOFT_AP_TIMEOUT_ENABLED, 1)).thenReturn(0); - mContentObserver.onChange(false); + SoftApConfiguration newConfig = new SoftApConfiguration.Builder(mDefaultApConfig) + .setAutoShutdownEnabled(false) + .build(); + mSoftApManager.updateConfiguration(newConfig); mLooper.dispatchAll(); // Verify timer is canceled @@ -1485,16 +1483,18 @@ public class SoftApManagerTest extends WifiBaseTest { @Test public void schedulesTimeoutTimerOnTimeoutToggleChangeWhenNoClients() throws Exception { // start with timeout toggle disabled - when(mFrameworkFacade.getIntegerSetting( - mContext, Settings.Global.SOFT_AP_TIMEOUT_ENABLED, 1)).thenReturn(0); + mDefaultApConfig = new SoftApConfiguration.Builder(mDefaultApConfig) + .setAutoShutdownEnabled(false) + .build(); SoftApModeConfiguration apConfig = new SoftApModeConfiguration(WifiManager.IFACE_IP_MODE_TETHERED, null, mTestSoftApCapability); startSoftApAndVerifyEnabled(apConfig); - when(mFrameworkFacade.getIntegerSetting( - mContext, Settings.Global.SOFT_AP_TIMEOUT_ENABLED, 1)).thenReturn(1); - mContentObserver.onChange(false); + SoftApConfiguration newConfig = new SoftApConfiguration.Builder(mDefaultApConfig) + .setAutoShutdownEnabled(true) + .build(); + mSoftApManager.updateConfiguration(newConfig); mLooper.dispatchAll(); // Verify timer is scheduled @@ -1505,8 +1505,9 @@ public class SoftApManagerTest extends WifiBaseTest { @Test public void doesNotScheduleTimeoutTimerOnStartWhenTimeoutIsDisabled() throws Exception { // start with timeout toggle disabled - when(mFrameworkFacade.getIntegerSetting( - mContext, Settings.Global.SOFT_AP_TIMEOUT_ENABLED, 1)).thenReturn(0); + mDefaultApConfig = new SoftApConfiguration.Builder(mDefaultApConfig) + .setAutoShutdownEnabled(false) + .build(); SoftApModeConfiguration apConfig = new SoftApModeConfiguration(WifiManager.IFACE_IP_MODE_TETHERED, null, mTestSoftApCapability); @@ -1521,8 +1522,9 @@ public class SoftApManagerTest extends WifiBaseTest { public void doesNotScheduleTimeoutTimerWhenAllClientsDisconnectButTimeoutIsDisabled() throws Exception { // start with timeout toggle disabled - when(mFrameworkFacade.getIntegerSetting( - mContext, Settings.Global.SOFT_AP_TIMEOUT_ENABLED, 1)).thenReturn(0); + mDefaultApConfig = new SoftApConfiguration.Builder(mDefaultApConfig) + .setAutoShutdownEnabled(false) + .build(); SoftApModeConfiguration apConfig = new SoftApModeConfiguration(WifiManager.IFACE_IP_MODE_TETHERED, null, mTestSoftApCapability); @@ -1541,18 +1543,6 @@ public class SoftApManagerTest extends WifiBaseTest { } @Test - public void unregistersSettingsObserverOnStop() throws Exception { - SoftApModeConfiguration apConfig = - new SoftApModeConfiguration(WifiManager.IFACE_IP_MODE_TETHERED, null, - mTestSoftApCapability); - startSoftApAndVerifyEnabled(apConfig); - mSoftApManager.stop(); - mLooper.dispatchAll(); - - verify(mFrameworkFacade).unregisterContentObserver(eq(mContext), eq(mContentObserver)); - } - - @Test public void resetsFactoryMacWhenRandomizationOff() throws Exception { Builder configBuilder = new SoftApConfiguration.Builder(); configBuilder.setBand(SoftApConfiguration.BAND_2GHZ); @@ -1751,8 +1741,6 @@ public class SoftApManagerTest extends WifiBaseTest { .build(); } - ArgumentCaptor<ContentObserver> observerCaptor = ArgumentCaptor.forClass( - ContentObserver.class); ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); when(mWifiNative.setupInterfaceForSoftApMode(any())) @@ -1787,9 +1775,6 @@ public class SoftApManagerTest extends WifiBaseTest { softApConfig.getTargetMode()); verify(mListener).onStarted(); verify(mWifiMetrics).addSoftApUpChangedEvent(true, softApConfig.getTargetMode()); - verify(mFrameworkFacade).registerContentObserver(eq(mContext), any(Uri.class), eq(true), - observerCaptor.capture()); - mContentObserver = observerCaptor.getValue(); } private void checkApStateChangedBroadcast(Intent intent, int expectedCurrentState, diff --git a/tests/wifitests/src/com/android/server/wifi/SoftApStoreDataTest.java b/tests/wifitests/src/com/android/server/wifi/SoftApStoreDataTest.java index 62e20cc18..f3317fdd6 100644 --- a/tests/wifitests/src/com/android/server/wifi/SoftApStoreDataTest.java +++ b/tests/wifitests/src/com/android/server/wifi/SoftApStoreDataTest.java @@ -17,20 +17,27 @@ package com.android.server.wifi; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import static org.mockito.Mockito.withSettings; +import android.content.Context; import android.net.MacAddress; import android.net.wifi.SoftApConfiguration; import android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiOemMigrationHook; import android.util.Xml; import androidx.test.filters.SmallTest; +import com.android.dx.mockito.inline.extended.ExtendedMockito; import com.android.internal.util.FastXmlSerializer; import com.android.server.wifi.util.WifiConfigStoreEncryptionUtil; @@ -40,6 +47,8 @@ import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.mockito.MockitoSession; +import org.mockito.quality.Strictness; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlSerializer; @@ -63,6 +72,7 @@ public class SoftApStoreDataTest extends WifiBaseTest { private static final int TEST_OLD_BAND = WifiConfiguration.AP_BAND_ANY; private static final int TEST_SECURITY = SoftApConfiguration.SECURITY_TYPE_WPA2_PSK; private static final boolean TEST_CLIENT_CONTROL_BY_USER = false; + private static final boolean TEST_AUTO_SHUTDOWN_ENABLED = true; private static final int TEST_MAX_NUMBER_OF_CLIENTS = 10; private static final int TEST_SHUTDOWN_TIMEOUT_MILLIS = 600_000; private static final ArrayList<MacAddress> TEST_BLOCKEDLIST = new ArrayList<>(); @@ -97,6 +107,8 @@ public class SoftApStoreDataTest extends WifiBaseTest { + TEST_MAX_NUMBER_OF_CLIENTS + "\" />\n" + "<boolean name=\"ClientControlByUser\" value=\"" + TEST_CLIENT_CONTROL_BY_USER + "\" />\n" + + "<boolean name=\"AutoShutdownEnabled\" value=\"" + + TEST_AUTO_SHUTDOWN_ENABLED + "\" />\n" + "<int name=\"ShutdownTimeoutMillis\" value=\"" + TEST_SHUTDOWN_TIMEOUT_MILLIS + "\" />\n" + "<BlockedClientList>\n" @@ -106,14 +118,46 @@ public class SoftApStoreDataTest extends WifiBaseTest { + "<string name=\"ClientMacAddress\">" + TEST_ALLOWED_CLIENT + "</string>\n" + "</AllowedClientList>\n"; + private static final String TEST_SOFTAP_CONFIG_XML_STRING_WITH_ALL_CONFIG_EXCEPT_AUTO_SHUTDOWN = + "<string name=\"SSID\">" + TEST_SSID + "</string>\n" + + "<int name=\"ApBand\" value=\"" + TEST_BAND + "\" />\n" + + "<int name=\"Channel\" value=\"" + TEST_CHANNEL + "\" />\n" + + "<boolean name=\"HiddenSSID\" value=\"" + TEST_HIDDEN + "\" />\n" + + "<int name=\"SecurityType\" value=\"" + TEST_SECURITY + "\" />\n" + + "<string name=\"Passphrase\">" + TEST_PASSPHRASE + "</string>\n" + + "<int name=\"MaxNumberOfClients\" value=\"" + + TEST_MAX_NUMBER_OF_CLIENTS + "\" />\n" + + "<boolean name=\"ClientControlByUser\" value=\"" + + TEST_CLIENT_CONTROL_BY_USER + "\" />\n" + + "<int name=\"ShutdownTimeoutMillis\" value=\"" + + TEST_SHUTDOWN_TIMEOUT_MILLIS + "\" />\n" + + "<BlockedClientList>\n" + + "<string name=\"ClientMacAddress\">" + TEST_BLOCKED_CLIENT + "</string>\n" + + "</BlockedClientList>\n" + + "<AllowedClientList>\n" + + "<string name=\"ClientMacAddress\">" + TEST_ALLOWED_CLIENT + "</string>\n" + + "</AllowedClientList>\n"; + + @Mock private Context mContext; @Mock SoftApStoreData.DataSource mDataSource; @Mock WifiOemConfigStoreMigrationDataHolder mWifiOemConfigStoreMigrationDataHolder; + @Mock private WifiOemMigrationHook.SettingsMigrationData mOemMigrationData; + MockitoSession mSession; SoftApStoreData mSoftApStoreData; @Before public void setUp() throws Exception { MockitoAnnotations.initMocks(this); - mSoftApStoreData = new SoftApStoreData(mDataSource, mWifiOemConfigStoreMigrationDataHolder); + mSession = ExtendedMockito.mockitoSession() + .mockStatic(WifiOemMigrationHook.class, withSettings().lenient()) + .strictness(Strictness.LENIENT) + .startMocking(); + when(WifiOemMigrationHook.loadFromSettings(any(Context.class))) + .thenReturn(mOemMigrationData); + when(mOemMigrationData.isSoftApTimeoutEnabled()).thenReturn(true); + + mSoftApStoreData = + new SoftApStoreData(mContext, mDataSource, mWifiOemConfigStoreMigrationDataHolder); TEST_BLOCKEDLIST.add(MacAddress.fromString(TEST_BLOCKED_CLIENT)); TEST_ALLOWEDLIST.add(MacAddress.fromString(TEST_ALLOWED_CLIENT)); } @@ -125,6 +169,7 @@ public class SoftApStoreDataTest extends WifiBaseTest { public void cleanup() { TEST_BLOCKEDLIST.clear(); TEST_ALLOWEDLIST.clear(); + mSession.finishMocking(); } /** @@ -150,6 +195,7 @@ public class SoftApStoreDataTest extends WifiBaseTest { softApConfigBuilder.setBand(TEST_BAND); softApConfigBuilder.enableClientControlByUser(TEST_CLIENT_CONTROL_BY_USER); softApConfigBuilder.setMaxNumberOfClients(TEST_MAX_NUMBER_OF_CLIENTS); + softApConfigBuilder.setAutoShutdownEnabled(true); softApConfigBuilder.setShutdownTimeoutMillis(TEST_SHUTDOWN_TIMEOUT_MILLIS); softApConfigBuilder.setClientList(TEST_BLOCKEDLIST, TEST_ALLOWEDLIST); return softApConfigBuilder.build(); @@ -229,6 +275,7 @@ public class SoftApStoreDataTest extends WifiBaseTest { assertEquals(softApConfig.getBand(), TEST_BAND); assertEquals(softApConfig.isClientControlByUserEnabled(), TEST_CLIENT_CONTROL_BY_USER); assertEquals(softApConfig.getMaxNumberOfClients(), TEST_MAX_NUMBER_OF_CLIENTS); + assertTrue(softApConfig.isAutoShutdownEnabled()); assertEquals(softApConfig.getShutdownTimeoutMillis(), TEST_SHUTDOWN_TIMEOUT_MILLIS); assertEquals(softApConfig.getBlockedClientList(), TEST_BLOCKEDLIST); assertEquals(softApConfig.getAllowedClientList(), TEST_ALLOWEDLIST); @@ -419,4 +466,37 @@ public class SoftApStoreDataTest extends WifiBaseTest { assertEquals(softApConfig.getAllowedClientList(), TEST_ALLOWEDLIST); } + /** + * Verify that the store data is deserialized correctly using the predefined test XML data + * when the auto shutdown tag is retrieved from + * {@link WifiOemMigrationHook.loadFromSettings(Context)}. + * + * @throws Exception + */ + @Test + public void deserializeSoftApWithNoAutoShutdownTag() throws Exception { + + // Toggle on when migrating. + when(mOemMigrationData.isSoftApTimeoutEnabled()).thenReturn(true); + deserializeData( + TEST_SOFTAP_CONFIG_XML_STRING_WITH_ALL_CONFIG_EXCEPT_AUTO_SHUTDOWN.getBytes()); + ArgumentCaptor<SoftApConfiguration> softapConfigCaptor = + ArgumentCaptor.forClass(SoftApConfiguration.class); + verify(mDataSource).fromDeserialized(softapConfigCaptor.capture()); + SoftApConfiguration softApConfig = softapConfigCaptor.getValue(); + assertNotNull(softApConfig); + assertEquals(softApConfig.getSsid(), TEST_SSID); + assertTrue(softApConfig.isAutoShutdownEnabled()); + + // Toggle off when migrating. + when(mOemMigrationData.isSoftApTimeoutEnabled()).thenReturn(false); + deserializeData( + TEST_SOFTAP_CONFIG_XML_STRING_WITH_ALL_CONFIG_EXCEPT_AUTO_SHUTDOWN.getBytes()); + verify(mDataSource, times(2)).fromDeserialized(softapConfigCaptor.capture()); + softApConfig = softapConfigCaptor.getValue(); + assertNotNull(softApConfig); + assertEquals(softApConfig.getSsid(), TEST_SSID); + assertFalse(softApConfig.isAutoShutdownEnabled()); + } + } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java index a289171b4..586ab3222 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiServiceImplTest.java @@ -37,6 +37,7 @@ import static android.net.wifi.WifiManager.WIFI_STATE_DISABLED; import static com.android.dx.mockito.inline.extended.ExtendedMockito.mockitoSession; import static com.android.server.wifi.LocalOnlyHotspotRequestInfo.HOTSPOT_NO_ERROR; +import static com.android.server.wifi.WifiSettingsConfigStore.WIFI_VERBOSE_LOGGING_ENABLED; import static com.google.common.truth.Truth.assertThat; @@ -304,6 +305,7 @@ public class WifiServiceImplTest extends WifiBaseTest { @Mock ISuggestionConnectionStatusListener mSuggestionConnectionStatusListener; @Mock IOnWifiActivityEnergyInfoListener mOnWifiActivityEnergyInfoListener; @Mock IWifiConnectedNetworkScorer mWifiConnectedNetworkScorer; + @Mock WifiSettingsConfigStore mWifiSettingsConfigStore; WifiLog mLog = new LogcatLog(TAG); @@ -374,6 +376,7 @@ public class WifiServiceImplTest extends WifiBaseTest { .thenReturn(mock(WifiNetworkScoreCache.class)); when(mWifiInjector.getWifiThreadRunner()) .thenReturn(new WifiThreadRunner(new Handler(mLooper.getLooper()))); + when(mWifiInjector.getSettingsConfigStore()).thenReturn(mWifiSettingsConfigStore); when(mClientModeImpl.syncStartSubscriptionProvisioning(anyInt(), any(OsuProvider.class), any(IProvisioningCallback.class), any())).thenReturn(true); // Create an OSU provider that can be provisioned via an open OSU AP @@ -1194,6 +1197,7 @@ public class WifiServiceImplTest extends WifiBaseTest { when(mSettingsStore.isWifiToggleEnabled()).thenReturn(false); mWifiServiceImpl.checkAndStartWifi(); mLooper.dispatchAll(); + verify(mWifiConfigManager).loadFromStore(); verify(mActiveModeWarden).start(); verify(mActiveModeWarden, never()).wifiToggled(); } @@ -1211,6 +1215,7 @@ public class WifiServiceImplTest extends WifiBaseTest { anyInt(), anyInt())).thenReturn(PackageManager.PERMISSION_GRANTED); mWifiServiceImpl.checkAndStartWifi(); mLooper.dispatchAll(); + verify(mWifiConfigManager).loadFromStore(); verify(mActiveModeWarden).start(); } @@ -3374,6 +3379,7 @@ public class WifiServiceImplTest extends WifiBaseTest { // before invocation. reset(mClientModeImpl); mWifiServiceImpl.enableVerboseLogging(1); + verify(mWifiSettingsConfigStore).putBoolean(WIFI_VERBOSE_LOGGING_ENABLED, true); verify(mClientModeImpl).enableVerboseLogging(anyInt()); } @@ -3390,6 +3396,8 @@ public class WifiServiceImplTest extends WifiBaseTest { // before invocation. reset(mClientModeImpl); mWifiServiceImpl.enableVerboseLogging(1); + verify(mWifiSettingsConfigStore, never()).putBoolean( + WIFI_VERBOSE_LOGGING_ENABLED, anyBoolean()); verify(mClientModeImpl, never()).enableVerboseLogging(anyInt()); } @@ -5036,7 +5044,6 @@ public class WifiServiceImplTest extends WifiBaseTest { mWifiServiceImpl.handleBootCompleted(); mLooper.dispatchAll(); - verify(mWifiConfigManager).loadFromStore(); verify(mPasspointManager).initializeProvisioner(any()); verify(mClientModeImpl).handleBootCompleted(); } @@ -5580,4 +5587,29 @@ public class WifiServiceImplTest extends WifiBaseTest { mLooper.stopAutoDispatchAndIgnoreExceptions(); verify(mWakeupController).isEnabled(); } + + @Test + public void testSetScanAlwaysAvailableWithNetworkSettingsPermission() { + doNothing().when(mContext) + .enforceCallingOrSelfPermission(eq(android.Manifest.permission.NETWORK_SETTINGS), + eq("WifiService")); + mWifiServiceImpl.setScanAlwaysAvailable(true); + verify(mSettingsStore).handleWifiScanAlwaysAvailableToggled(true); + verify(mActiveModeWarden).scanAlwaysModeChanged(); + + mWifiServiceImpl.setScanAlwaysAvailable(false); + verify(mSettingsStore).handleWifiScanAlwaysAvailableToggled(false); + verify(mActiveModeWarden, times(2)).scanAlwaysModeChanged(); + } + + @Test(expected = SecurityException.class) + public void testSetScanAlwaysAvailableWithNoNetworkSettingsPermission() { + doThrow(new SecurityException()).when(mContext) + .enforceCallingOrSelfPermission(eq(android.Manifest.permission.NETWORK_SETTINGS), + eq("WifiService")); + + mWifiServiceImpl.setScanAlwaysAvailable(true); + verify(mSettingsStore, never()).handleWifiScanAlwaysAvailableToggled(anyBoolean()); + verify(mActiveModeWarden, never()).scanAlwaysModeChanged(); + } } diff --git a/tests/wifitests/src/com/android/server/wifi/WifiSettingsConfigStoreTest.java b/tests/wifitests/src/com/android/server/wifi/WifiSettingsConfigStoreTest.java new file mode 100644 index 000000000..22cb7f435 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/WifiSettingsConfigStoreTest.java @@ -0,0 +1,143 @@ +/* + * Copyright (C) 2019 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 com.android.server.wifi.WifiSettingsConfigStore.WIFI_VERBOSE_LOGGING_ENABLED; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.validateMockitoUsage; +import static org.mockito.Mockito.verify; + +import android.content.Context; +import android.os.Handler; +import android.os.test.TestLooper; +import android.util.Xml; + +import androidx.test.filters.SmallTest; + +import com.android.internal.util.FastXmlSerializer; +import com.android.server.wifi.util.XmlUtil; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.xmlpull.v1.XmlPullParser; +import org.xmlpull.v1.XmlSerializer; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + + +/** + * Unit tests for {@link com.android.server.wifi.WifiSettingsConfigStore}. + */ +@SmallTest +public class WifiSettingsConfigStoreTest extends WifiBaseTest { + @Mock + private Context mContext; + @Mock + private WifiConfigStore mWifiConfigStore; + @Mock + private WifiConfigManager mWifiConfigManager; + + private TestLooper mLooper; + private WifiSettingsConfigStore mWifiSettingsConfigStore; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + mLooper = new TestLooper(); + mWifiSettingsConfigStore = + new WifiSettingsConfigStore(mContext, new Handler(mLooper.getLooper()), + mWifiConfigManager, mWifiConfigStore); + } + + /** + * Called after each test + */ + @After + public void cleanup() { + validateMockitoUsage(); + } + + @Test + public void testSetterGetter() { + assertFalse(mWifiSettingsConfigStore.getBoolean(WIFI_VERBOSE_LOGGING_ENABLED, false)); + mWifiSettingsConfigStore.putBoolean(WIFI_VERBOSE_LOGGING_ENABLED, true); + mLooper.dispatchAll(); + assertTrue(mWifiSettingsConfigStore.getBoolean(WIFI_VERBOSE_LOGGING_ENABLED, false)); + verify(mWifiConfigManager).saveToStore(true); + } + + @Test + public void testChangeListener() { + WifiSettingsConfigStore.OnSettingsChangedListener listener = mock( + WifiSettingsConfigStore.OnSettingsChangedListener.class); + mWifiSettingsConfigStore.registerChangeListener(WIFI_VERBOSE_LOGGING_ENABLED, listener, + new Handler(mLooper.getLooper())); + + mWifiSettingsConfigStore.putBoolean(WIFI_VERBOSE_LOGGING_ENABLED, true); + mLooper.dispatchAll(); + + verify(listener).onSettingsChanged(WIFI_VERBOSE_LOGGING_ENABLED, true); + } + + @Test + public void testSaveAndLoadFromStore() throws Exception { + ArgumentCaptor<WifiConfigStore.StoreData> storeDataCaptor = ArgumentCaptor.forClass( + WifiConfigStore.StoreData.class); + verify(mWifiConfigStore).registerStoreData(storeDataCaptor.capture()); + assertNotNull(storeDataCaptor.getValue()); + + XmlPullParser in = createSettingsTestXmlForParsing(WIFI_VERBOSE_LOGGING_ENABLED, true); + + storeDataCaptor.getValue().resetData(); + storeDataCaptor.getValue().deserializeData(in, in.getDepth(), -1, null); + + assertTrue(mWifiSettingsConfigStore.getBoolean(WIFI_VERBOSE_LOGGING_ENABLED, false)); + } + + private XmlPullParser createSettingsTestXmlForParsing(String key, Object value) + throws Exception { + Map<String, Object> settings = new HashMap<>(); + // Serialize + settings.put(key, value); + final XmlSerializer out = new FastXmlSerializer(); + final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); + out.setOutput(outputStream, StandardCharsets.UTF_8.name()); + XmlUtil.writeDocumentStart(out, "Test"); + XmlUtil.writeNextValue(out, "Values", settings); + XmlUtil.writeDocumentEnd(out, "Test"); + + // Start Deserializing + final XmlPullParser in = Xml.newPullParser(); + ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); + in.setInput(inputStream, StandardCharsets.UTF_8.name()); + XmlUtil.gotoDocumentStart(in, "Test"); + return in; + } +} diff --git a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareServiceImplTest.java index 3e4089056..9675ad89a 100644 --- a/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/aware/WifiAwareServiceImplTest.java @@ -19,11 +19,9 @@ package com.android.server.wifi.aware; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; @@ -51,8 +49,8 @@ import android.util.SparseIntArray; import androidx.test.filters.SmallTest; -import com.android.server.wifi.FrameworkFacade; import com.android.server.wifi.WifiBaseTest; +import com.android.server.wifi.WifiSettingsConfigStore; import com.android.server.wifi.util.NetdWrapper; import com.android.server.wifi.util.WifiPermissionsUtil; import com.android.server.wifi.util.WifiPermissionsWrapper; @@ -103,7 +101,7 @@ public class WifiAwareServiceImplTest extends WifiBaseTest { @Mock private WifiPermissionsUtil mWifiPermissionsUtil; @Mock private WifiPermissionsWrapper mPermissionsWrapperMock; @Mock - FrameworkFacade mFrameworkFacade; + WifiSettingsConfigStore mWifiSettingsConfigStore; /** * Using instead of spy to avoid native crash failures - possibly due to @@ -135,8 +133,6 @@ public class WifiAwareServiceImplTest extends WifiBaseTest { mMockLooper = new TestLooper(); when(mHandlerThreadMock.getLooper()).thenReturn(mMockLooper.getLooper()); - doNothing().when(mFrameworkFacade).registerContentObserver(eq(mContextMock), any(), - anyBoolean(), any()); AppOpsManager appOpsMock = mock(AppOpsManager.class); when(mContextMock.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(appOpsMock); @@ -152,7 +148,8 @@ public class WifiAwareServiceImplTest extends WifiBaseTest { mDut = new WifiAwareServiceImplSpy(mContextMock); mDut.fakeUid = mDefaultUid; mDut.start(mHandlerThreadMock, mAwareStateManagerMock, mWifiAwareShellCommandMock, - mAwareMetricsMock, mWifiPermissionsUtil, mPermissionsWrapperMock, mFrameworkFacade, + mAwareMetricsMock, mWifiPermissionsUtil, mPermissionsWrapperMock, + mWifiSettingsConfigStore, mock(WifiAwareNativeManager.class), mock(WifiAwareNativeApi.class), mock(WifiAwareNativeCallback.class), mock(NetdWrapper.class)); mMockLooper.dispatchAll(); diff --git a/tests/wifitests/src/com/android/server/wifi/p2p/WifiP2pServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/p2p/WifiP2pServiceImplTest.java index 5f22974a4..adfa025ca 100644 --- a/tests/wifitests/src/com/android/server/wifi/p2p/WifiP2pServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/p2p/WifiP2pServiceImplTest.java @@ -16,6 +16,9 @@ package com.android.server.wifi.p2p; +import static com.android.server.wifi.WifiSettingsConfigStore.WIFI_P2P_DEVICE_NAME; +import static com.android.server.wifi.WifiSettingsConfigStore.WIFI_P2P_PENDING_FACTORY_RESET; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotNull; @@ -70,7 +73,6 @@ import android.os.Messenger; import android.os.UserHandle; import android.os.UserManager; import android.os.test.TestLooper; -import android.provider.Settings; import androidx.test.filters.SmallTest; @@ -79,6 +81,7 @@ import com.android.server.wifi.FrameworkFacade; import com.android.server.wifi.HalDeviceManager; import com.android.server.wifi.WifiBaseTest; import com.android.server.wifi.WifiInjector; +import com.android.server.wifi.WifiSettingsConfigStore; import com.android.server.wifi.proto.nano.WifiMetricsProto.P2pConnectionEvent; import com.android.server.wifi.util.NetdWrapper; import com.android.server.wifi.util.WifiPermissionsUtil; @@ -141,6 +144,7 @@ public class WifiP2pServiceImplTest extends WifiBaseTest { @Mock WifiInjector mWifiInjector; @Mock WifiManager mMockWifiManager; @Mock WifiPermissionsUtil mWifiPermissionsUtil; + @Mock WifiSettingsConfigStore mWifiSettingsConfigStore; @Mock WifiPermissionsWrapper mWifiPermissionsWrapper; @Mock WifiP2pNative mWifiNative; @Mock WifiP2pServiceInfo mTestWifiP2pServiceInfo; @@ -706,10 +710,10 @@ public class WifiP2pServiceImplTest extends WifiBaseTest { when(mContext.getPackageManager()).thenReturn(mPackageManager); when(mContext.getResources()).thenReturn(mResources); - when(mFrameworkFacade.getStringSetting(any(), - eq(Settings.Global.WIFI_P2P_DEVICE_NAME))).thenReturn(thisDeviceName); - when(mFrameworkFacade.getIntegerSetting(any(), - eq(Settings.Global.WIFI_P2P_PENDING_FACTORY_RESET), eq(0))).thenReturn(0); + when(mWifiSettingsConfigStore.getString(eq(WIFI_P2P_DEVICE_NAME), any())) + .thenReturn(thisDeviceName); + when(mWifiSettingsConfigStore.getBoolean( + eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(false))).thenReturn(false); when(mHandlerThread.getLooper()).thenReturn(mLooper.getLooper()); if (supported) { when(mPackageManager.hasSystemFeature(eq(PackageManager.FEATURE_WIFI_DIRECT))) @@ -727,6 +731,7 @@ public class WifiP2pServiceImplTest extends WifiBaseTest { when(mWifiInjector.getWifiP2pNative()).thenReturn(mWifiNative); when(mWifiInjector.getWifiP2pServiceHandlerThread()).thenReturn(mHandlerThread); when(mWifiInjector.getWifiPermissionsUtil()).thenReturn(mWifiPermissionsUtil); + when(mWifiInjector.getSettingsConfigStore()).thenReturn(mWifiSettingsConfigStore); // enable all permissions, disable specific permissions in tests when(mWifiPermissionsUtil.checkNetworkSettingsPermission(anyInt())).thenReturn(true); when(mWifiPermissionsUtil.checkNetworkStackPermission(anyInt())).thenReturn(true); @@ -2723,13 +2728,11 @@ public class WifiP2pServiceImplTest extends WifiBaseTest { mTestThisDevice.deviceName = "another-name"; when(mWifiNative.setDeviceName(anyString())).thenReturn(true); - when(mFrameworkFacade.setStringSetting( - any(), eq(Settings.Global.WIFI_P2P_DEVICE_NAME), anyString())).thenReturn(true); sendSetDeviceNameMsg(mClientMessenger, mTestThisDevice); verify(mWifiNative).setDeviceName(eq(mTestThisDevice.deviceName)); verify(mWifiNative).setP2pSsidPostfix(eq("-" + mTestThisDevice.deviceName)); - verify(mFrameworkFacade).setStringSetting(eq(mContext), - eq(Settings.Global.WIFI_P2P_DEVICE_NAME), eq(mTestThisDevice.deviceName)); + verify(mWifiSettingsConfigStore).putString( + eq(WIFI_P2P_DEVICE_NAME), eq(mTestThisDevice.deviceName)); checkSendThisDeviceChangedBroadcast(); verify(mClientHandler).sendMessage(mMessageCaptor.capture()); Message message = mMessageCaptor.getValue(); @@ -3172,8 +3175,7 @@ public class WifiP2pServiceImplTest extends WifiBaseTest { eq(UserManager.DISALLOW_NETWORK_RESET), any()); verify(mUserManager).hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any()); verify(mWifiNative, atLeastOnce()).p2pListNetworks(any()); - verify(mFrameworkFacade).setIntegerSetting(eq(mContext), - eq(Settings.Global.WIFI_P2P_PENDING_FACTORY_RESET), eq(0)); + verify(mWifiSettingsConfigStore).putBoolean(eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(false)); verify(mClientHandler).sendMessage(mMessageCaptor.capture()); Message message = mMessageCaptor.getValue(); assertEquals(WifiP2pManager.FACTORY_RESET_SUCCEEDED, message.what); @@ -3201,15 +3203,14 @@ public class WifiP2pServiceImplTest extends WifiBaseTest { eq(UserManager.DISALLOW_NETWORK_RESET), any()); verify(mUserManager).hasUserRestrictionForUser(eq(UserManager.DISALLOW_CONFIG_WIFI), any()); verify(mWifiNative, never()).p2pListNetworks(any()); - verify(mFrameworkFacade).setIntegerSetting(eq(mContext), - eq(Settings.Global.WIFI_P2P_PENDING_FACTORY_RESET), eq(1)); + verify(mWifiSettingsConfigStore).putBoolean(eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(true)); verify(mClientHandler).sendMessage(mMessageCaptor.capture()); Message message = mMessageCaptor.getValue(); assertEquals(WifiP2pManager.FACTORY_RESET_SUCCEEDED, message.what); // Move to enabled state - when(mFrameworkFacade.getIntegerSetting(eq(mContext), - eq(Settings.Global.WIFI_P2P_PENDING_FACTORY_RESET), anyInt())).thenReturn(1); + when(mWifiSettingsConfigStore.getBoolean( + eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(false))).thenReturn(true); forceP2pEnabled(mClient1); verify(mWifiInjector, times(2)).getUserManager(); verify(mPackageManager, times(2)).getNameForUid(anyInt()); @@ -3219,10 +3220,8 @@ public class WifiP2pServiceImplTest extends WifiBaseTest { verify(mUserManager, times(2)).hasUserRestrictionForUser( eq(UserManager.DISALLOW_CONFIG_WIFI), any()); verify(mWifiNative, atLeastOnce()).p2pListNetworks(any()); - verify(mFrameworkFacade).getIntegerSetting(eq(mContext), - eq(Settings.Global.WIFI_P2P_PENDING_FACTORY_RESET), eq(0)); - verify(mFrameworkFacade).setIntegerSetting(eq(mContext), - eq(Settings.Global.WIFI_P2P_PENDING_FACTORY_RESET), eq(0)); + verify(mWifiSettingsConfigStore).getBoolean(eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(false)); + verify(mWifiSettingsConfigStore).putBoolean(eq(WIFI_P2P_PENDING_FACTORY_RESET), eq(false)); checkSendP2pPersistentGroupsChangedBroadcast(); } diff --git a/tests/wifitests/src/com/android/server/wifi/rtt/RttServiceImplTest.java b/tests/wifitests/src/com/android/server/wifi/rtt/RttServiceImplTest.java index e5c922d5a..ec08f4100 100644 --- a/tests/wifitests/src/com/android/server/wifi/rtt/RttServiceImplTest.java +++ b/tests/wifitests/src/com/android/server/wifi/rtt/RttServiceImplTest.java @@ -31,7 +31,6 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.nullable; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; @@ -72,9 +71,9 @@ import android.util.Pair; import androidx.test.filters.SmallTest; import com.android.server.wifi.Clock; -import com.android.server.wifi.FrameworkFacade; import com.android.server.wifi.MockResources; import com.android.server.wifi.WifiBaseTest; +import com.android.server.wifi.WifiSettingsConfigStore; import com.android.server.wifi.proto.nano.WifiMetricsProto; import com.android.server.wifi.util.WifiPermissionsUtil; import com.android.wifi.resources.R; @@ -156,7 +155,7 @@ public class RttServiceImplTest extends WifiBaseTest { public IRttCallback mockCallback; @Mock - FrameworkFacade mFrameworkFacade; + WifiSettingsConfigStore mWifiSettingsConfigStore; /** * Using instead of spy to avoid native crash failures - possibly due to @@ -196,8 +195,6 @@ public class RttServiceImplTest extends WifiBaseTest { R.integer.config_wifiRttBackgroundExecGapMs, BACKGROUND_PROCESS_EXEC_GAP_MS); mAlarmManager = new TestAlarmManager(); - doNothing().when(mFrameworkFacade).registerContentObserver(eq(mockContext), any(), - anyBoolean(), any()); when(mockContext.getSystemService(Context.ALARM_SERVICE)) .thenReturn(mAlarmManager.getAlarmManager()); mInOrder = inOrder(mAlarmManager.getAlarmManager(), mockContext); @@ -225,7 +222,7 @@ public class RttServiceImplTest extends WifiBaseTest { doAnswer(mBinderUnlinkToDeathCounter).when(mockIbinder).unlinkToDeath(any(), anyInt()); mDut.start(mMockLooper.getLooper(), mockClock, mockAwareManager, mockNative, - mockMetrics, mockPermissionUtil, mFrameworkFacade); + mockMetrics, mockPermissionUtil, mWifiSettingsConfigStore); mMockLooper.dispatchAll(); ArgumentCaptor<BroadcastReceiver> bcastRxCaptor = ArgumentCaptor.forClass( BroadcastReceiver.class); |