diff options
author | Roshan Pius <rpius@google.com> | 2018-11-12 08:44:55 -0800 |
---|---|---|
committer | Roshan Pius <rpius@google.com> | 2018-11-20 13:02:19 -0800 |
commit | afb74fd63f1c9201c3ff046db1c0b546c028d6ce (patch) | |
tree | 520919ce200bf8a4c116d1ef224e470df38c4168 /tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java | |
parent | 139e7238779d1a0638ae42e9eb9a91d7249f753f (diff) | |
download | android_frameworks_opt_net_wifi-afb74fd63f1c9201c3ff046db1c0b546c028d6ce.tar.gz android_frameworks_opt_net_wifi-afb74fd63f1c9201c3ff046db1c0b546c028d6ce.tar.bz2 android_frameworks_opt_net_wifi-afb74fd63f1c9201c3ff046db1c0b546c028d6ce.zip |
WifiNetworkSuggestionsManager: Handle data persistence
Integrate the new store data to serialize/deserialize data
from persistent store.
Bug: 115504887
Test: ./frameworks/opt/net/wifi/tests/wifitests/runtests.sh
Change-Id: I280a28738051978ced008c6bf85624e76a2a15ba
Diffstat (limited to 'tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java')
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java | 175 |
1 files changed, 172 insertions, 3 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java index 9cbd192ee..3db1d916f 100644 --- a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java @@ -18,6 +18,7 @@ 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.assertNull; import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.*; @@ -41,8 +42,10 @@ import org.mockito.Mock; import org.mockito.MockitoAnnotations; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; /** @@ -57,11 +60,15 @@ public class WifiNetworkSuggestionsManagerTest { private @Mock Context mContext; private @Mock WifiPermissionsUtil mWifiPermissionsUtil; + private @Mock WifiInjector mWifiInjector; + private @Mock WifiConfigStore mWifiConfigStore; + private @Mock WifiConfigManager mWifiConfigManager; + private @Mock NetworkSuggestionStoreData mNetworkSuggestionStoreData; + private InOrder mInorder; private WifiNetworkSuggestionsManager mWifiNetworkSuggestionsManager; - private List<WifiNetworkSuggestion> mWifiNetworkSuggestionsList1; - private List<WifiNetworkSuggestion> mWifiNetworkSuggestionsList2; + private NetworkSuggestionStoreData.DataSource mDataSource; /** * Setup the mocks. @@ -71,8 +78,20 @@ public class WifiNetworkSuggestionsManagerTest { MockitoAnnotations.initMocks(this); mInorder = inOrder(mContext, mWifiPermissionsUtil); + + when(mWifiInjector.makeNetworkSuggestionStoreData(any())) + .thenReturn(mNetworkSuggestionStoreData); + mWifiNetworkSuggestionsManager = - new WifiNetworkSuggestionsManager(mContext, mWifiPermissionsUtil); + new WifiNetworkSuggestionsManager(mContext, mWifiInjector, mWifiPermissionsUtil, + mWifiConfigManager, mWifiConfigStore); + + ArgumentCaptor<NetworkSuggestionStoreData.DataSource> dataSourceArgumentCaptor = + ArgumentCaptor.forClass(NetworkSuggestionStoreData.DataSource.class); + + verify(mWifiInjector).makeNetworkSuggestionStoreData(dataSourceArgumentCaptor.capture()); + mDataSource = dataSourceArgumentCaptor.getValue(); + assertNotNull(mDataSource); } /** @@ -441,6 +460,156 @@ public class WifiNetworkSuggestionsManagerTest { } /** + * Verify triggering of config store write after successful addition of network suggestions. + */ + @Test + public void testAddNetworkSuggestionsConfigStoreWrite() { + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_1); + + List<WifiNetworkSuggestion> networkSuggestionList = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_PACKAGE_1)); + + // Verify config store interactions. + verify(mWifiConfigManager).saveToStore(true); + assertTrue(mDataSource.hasNewDataToSerialize()); + + Map<String, Set<WifiNetworkSuggestion>> networkSuggestionsMapToWrite = + mDataSource.toSerialize(); + assertEquals(1, networkSuggestionsMapToWrite.size()); + assertTrue(networkSuggestionsMapToWrite.keySet().contains(TEST_PACKAGE_1)); + Set<WifiNetworkSuggestion> networkSuggestionsToWrite = + networkSuggestionsMapToWrite.get(TEST_PACKAGE_1); + Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = + new HashSet<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + assertEquals(expectedAllNetworkSuggestions, networkSuggestionsToWrite); + + // Ensure that the new data flag has been reset after read. + assertFalse(mDataSource.hasNewDataToSerialize()); + } + + /** + * Verify triggering of config store write after successful removal of network suggestions. + */ + @Test + public void testRemoveNetworkSuggestionsConfigStoreWrite() { + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_1); + + List<WifiNetworkSuggestion> networkSuggestionList = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList, TEST_PACKAGE_1)); + assertTrue(mWifiNetworkSuggestionsManager.remove(networkSuggestionList, TEST_PACKAGE_1)); + + // Verify config store interactions. + verify(mWifiConfigManager, times(2)).saveToStore(true); + assertTrue(mDataSource.hasNewDataToSerialize()); + + Map<String, Set<WifiNetworkSuggestion>> networkSuggestionsMapToWrite = + mDataSource.toSerialize(); + assertTrue(networkSuggestionsMapToWrite.isEmpty()); + + // Ensure that the new data flag has been reset after read. + assertFalse(mDataSource.hasNewDataToSerialize()); + } + + /** + * Verify handling of initial config store read. + */ + @Test + public void testNetworkSuggestionsConfigStoreLoad() { + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_1); + Set<WifiNetworkSuggestion> networkSuggestionSet = + new HashSet<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + + mDataSource.fromDeserialized(new HashMap<String, Set<WifiNetworkSuggestion>>() {{ + put(TEST_PACKAGE_1, networkSuggestionSet); + }}); + + Set<WifiNetworkSuggestion> allNetworkSuggestions = + mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); + Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = + new HashSet<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); + + // Ensure we can lookup the network. + ScanDetail scanDetail = createScanDetailForNetwork(networkSuggestion.wifiConfiguration); + Set<WifiNetworkSuggestion> matchingNetworkSuggestions = + mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail); + Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = + new HashSet<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + assertEquals(expectedMatchingNetworkSuggestions, matchingNetworkSuggestions); + } + + /** + * Verify handling of config store read after user switch. + */ + @Test + public void testNetworkSuggestionsConfigStoreLoadAfterUserSwitch() { + WifiNetworkSuggestion networkSuggestion1 = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_1); + + Set<WifiNetworkSuggestion> networkSuggestionSet1 = + new HashSet<WifiNetworkSuggestion>() {{ + add(networkSuggestion1); + }}; + + // Read the store initially. + mDataSource.fromDeserialized(new HashMap<String, Set<WifiNetworkSuggestion>>() {{ + put(TEST_PACKAGE_1, networkSuggestionSet1); + }}); + + WifiNetworkSuggestion networkSuggestion2 = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_2); + Set<WifiNetworkSuggestion> networkSuggestionSet2 = + new HashSet<WifiNetworkSuggestion>() {{ + add(networkSuggestion2); + }}; + + // Now simulate user switch. + mDataSource.reset(); + mDataSource.fromDeserialized(new HashMap<String, Set<WifiNetworkSuggestion>>() {{ + put(TEST_PACKAGE_2, networkSuggestionSet2); + }}); + + Set<WifiNetworkSuggestion> allNetworkSuggestions = + mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); + Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = + new HashSet<WifiNetworkSuggestion>() {{ + add(networkSuggestion2); + }}; + assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); + + // Ensure we can lookup the new network. + ScanDetail scanDetail2 = createScanDetailForNetwork(networkSuggestion2.wifiConfiguration); + Set<WifiNetworkSuggestion> matchingNetworkSuggestions = + mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail2); + Set<WifiNetworkSuggestion> expectedMatchingNetworkSuggestions = + new HashSet<WifiNetworkSuggestion>() {{ + add(networkSuggestion2); + }}; + assertEquals(expectedMatchingNetworkSuggestions, matchingNetworkSuggestions); + + // Ensure that the previous network can no longer be looked up. + ScanDetail scanDetail1 = createScanDetailForNetwork(networkSuggestion1.wifiConfiguration); + assertNull(mWifiNetworkSuggestionsManager.getNetworkSuggestionsForScanDetail(scanDetail1)); + } + + /** * Creates a scan detail corresponding to the provided network values. */ private ScanDetail createScanDetailForNetwork(WifiConfiguration configuration) { |