diff options
author | Roshan Pius <rpius@google.com> | 2018-11-02 10:27:02 -0700 |
---|---|---|
committer | Roshan Pius <rpius@google.com> | 2018-11-16 16:30:33 -0800 |
commit | a58aceaee3672c6d22a677ec0e73c57b1c6be67d (patch) | |
tree | 0e157c4354427bb33d38397bceb960ffdeeff3b7 /tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java | |
parent | 48fa240fe94fb1c0e61dd810ca96e5e8a6274747 (diff) | |
download | android_frameworks_opt_net_wifi-a58aceaee3672c6d22a677ec0e73c57b1c6be67d.tar.gz android_frameworks_opt_net_wifi-a58aceaee3672c6d22a677ec0e73c57b1c6be67d.tar.bz2 android_frameworks_opt_net_wifi-a58aceaee3672c6d22a677ec0e73c57b1c6be67d.zip |
WifiNetworkSuggestionsManager: Add a module to manage suggestions
The module currently just handles the add/remove API for network
suggestions.
Bug: 115504887
Test: ./frameworks/opt/net/wifi/tests/wifitests/runtests.sh
Change-Id: I4f067644e77425b98c6e54ff471a80d8d5acd7c1
Diffstat (limited to 'tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java')
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java | 221 |
1 files changed, 221 insertions, 0 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java new file mode 100644 index 000000000..60c7ec2cb --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/WifiNetworkSuggestionsManagerTest.java @@ -0,0 +1,221 @@ +/* + * 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 android.net.wifi.WifiConfiguration; +import android.net.wifi.WifiNetworkSuggestion; +import android.test.suitebuilder.annotation.SmallTest; + + +import org.junit.Before; +import org.junit.Test; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Unit tests for {@link com.android.server.wifi.WifiNetworkSuggestionsManager}. + */ +@SmallTest +public class WifiNetworkSuggestionsManagerTest { + private static final String TEST_PACKAGE_1 = "com.test12345"; + private static final String TEST_PACKAGE_2 = "com.test54321"; + private static final int TEST_UID_1 = 5667; + private static final int TEST_UID_2 = 4537; + + private WifiNetworkSuggestionsManager mWifiNetworkSuggestionsManager; + private List<WifiNetworkSuggestion> mWifiNetworkSuggestionsList1; + private List<WifiNetworkSuggestion> mWifiNetworkSuggestionsList2; + + /** + * Setup the mocks. + */ + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + mWifiNetworkSuggestionsManager = new WifiNetworkSuggestionsManager(); + } + + /** + * Verify successful addition of network suggestions. + */ + @Test + public void testAddNetworkSuggestionsSuccess() { + WifiNetworkSuggestion networkSuggestion1 = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_1); + WifiNetworkSuggestion networkSuggestion2 = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_2); + + List<WifiNetworkSuggestion> networkSuggestionList1 = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion1); + }}; + List<WifiNetworkSuggestion> networkSuggestionList2 = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion2); + }}; + + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_PACKAGE_1)); + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_PACKAGE_2)); + + Set<WifiNetworkSuggestion> allNetworkSuggestions = + mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); + Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = + new HashSet<WifiNetworkSuggestion>() {{ + add(networkSuggestion1); + add(networkSuggestion2); + }}; + assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); + } + + /** + * Verify successful removal of network suggestions. + */ + @Test + public void testRemoveNetworkSuggestionsSuccess() { + WifiNetworkSuggestion networkSuggestion1 = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_1); + WifiNetworkSuggestion networkSuggestion2 = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_2); + + List<WifiNetworkSuggestion> networkSuggestionList1 = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion1); + }}; + List<WifiNetworkSuggestion> networkSuggestionList2 = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion2); + }}; + + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_PACKAGE_1)); + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_PACKAGE_2)); + + // Now remove all of them. + assertTrue(mWifiNetworkSuggestionsManager.remove(networkSuggestionList1, TEST_PACKAGE_1)); + assertTrue(mWifiNetworkSuggestionsManager.remove(networkSuggestionList2, TEST_PACKAGE_2)); + + assertTrue(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions().isEmpty()); + } + + /** + * Verify successful removal of all network suggestions. + */ + @Test + public void testRemoveAllNetworkSuggestionsSuccess() { + WifiNetworkSuggestion networkSuggestion1 = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_1); + WifiNetworkSuggestion networkSuggestion2 = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_2); + + List<WifiNetworkSuggestion> networkSuggestionList1 = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion1); + }}; + List<WifiNetworkSuggestion> networkSuggestionList2 = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion2); + }}; + + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_PACKAGE_1)); + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList2, TEST_PACKAGE_2)); + + // Now remove all of them by sending an empty list. + assertTrue(mWifiNetworkSuggestionsManager.remove(new ArrayList<>(), TEST_PACKAGE_1)); + assertTrue(mWifiNetworkSuggestionsManager.remove(new ArrayList<>(), TEST_PACKAGE_2)); + + assertTrue(mWifiNetworkSuggestionsManager.getAllNetworkSuggestions().isEmpty()); + } + + /** + * Verify successful replace (add,remove, add) of network suggestions. + */ + @Test + public void testReplaceNetworkSuggestionsSuccess() { + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_1); + + List<WifiNetworkSuggestion> networkSuggestionList1 = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_PACKAGE_1)); + assertTrue(mWifiNetworkSuggestionsManager.remove(networkSuggestionList1, TEST_PACKAGE_1)); + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_PACKAGE_1)); + + Set<WifiNetworkSuggestion> allNetworkSuggestions = + mWifiNetworkSuggestionsManager.getAllNetworkSuggestions(); + Set<WifiNetworkSuggestion> expectedAllNetworkSuggestions = + new HashSet<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + assertEquals(expectedAllNetworkSuggestions, allNetworkSuggestions); + } + + /** + * Verify that an attempt to modify networks that are already active is rejected. + */ + @Test + public void testAddNetworkSuggestionsFailureOnInPlaceModification() { + WifiNetworkSuggestion networkSuggestion = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_1); + List<WifiNetworkSuggestion> networkSuggestionList1 = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion); + }}; + + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_PACKAGE_1)); + + // Modify the original suggestion. + networkSuggestion.wifiConfiguration.meteredOverride = + WifiConfiguration.METERED_OVERRIDE_METERED; + + // Replace attempt should fail. + assertFalse(mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_PACKAGE_1)); + } + + /** + * Verify that an attempt to remove an invalid set of network suggestions is rejected. + */ + @Test + public void testRemoveNetworkSuggestionsFailureOnInvalid() { + WifiNetworkSuggestion networkSuggestion1 = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_1); + WifiNetworkSuggestion networkSuggestion2 = new WifiNetworkSuggestion( + WifiConfigurationTestUtil.createOpenNetwork(), false, false, TEST_UID_1); + + List<WifiNetworkSuggestion> networkSuggestionList1 = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion1); + }}; + List<WifiNetworkSuggestion> networkSuggestionList2 = + new ArrayList<WifiNetworkSuggestion>() {{ + add(networkSuggestion2); + }}; + assertTrue(mWifiNetworkSuggestionsManager.add(networkSuggestionList1, TEST_PACKAGE_1)); + // Remove should fail because the network list is different. + assertFalse(mWifiNetworkSuggestionsManager.remove(networkSuggestionList2, TEST_PACKAGE_1)); + } +} |