1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.net; 18 19 import static com.android.testutils.MiscAsserts.assertThrows; 20 import static com.android.testutils.ParcelUtils.assertParcelingIsLossless; 21 22 import static org.junit.Assert.assertEquals; 23 import static org.junit.Assert.assertFalse; 24 import static org.junit.Assert.assertTrue; 25 26 import android.os.Build; 27 28 import androidx.test.filters.SmallTest; 29 30 import com.android.testutils.ConnectivityModuleTest; 31 import com.android.testutils.DevSdkIgnoreRule.IgnoreUpTo; 32 import com.android.testutils.DevSdkIgnoreRunner; 33 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 37 import java.util.Map; 38 39 @IgnoreUpTo(Build.VERSION_CODES.R) 40 @RunWith(DevSdkIgnoreRunner.class) 41 @SmallTest 42 @ConnectivityModuleTest 43 public class OemNetworkPreferencesTest { 44 45 private static final int TEST_PREF = OemNetworkPreferences.OEM_NETWORK_PREFERENCE_UNINITIALIZED; 46 private static final String TEST_PACKAGE = "com.google.apps.contacts"; 47 48 private final OemNetworkPreferences.Builder mBuilder = new OemNetworkPreferences.Builder(); 49 50 @Test testBuilderAddNetworkPreferenceRequiresNonNullPackageName()51 public void testBuilderAddNetworkPreferenceRequiresNonNullPackageName() { 52 assertThrows(NullPointerException.class, 53 () -> mBuilder.addNetworkPreference(null, TEST_PREF)); 54 } 55 56 @Test testBuilderRemoveNetworkPreferenceRequiresNonNullPackageName()57 public void testBuilderRemoveNetworkPreferenceRequiresNonNullPackageName() { 58 assertThrows(NullPointerException.class, 59 () -> mBuilder.clearNetworkPreference(null)); 60 } 61 62 @Test testGetNetworkPreferenceReturnsCorrectValue()63 public void testGetNetworkPreferenceReturnsCorrectValue() { 64 final int expectedNumberOfMappings = 1; 65 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 66 67 final Map<String, Integer> networkPreferences = 68 mBuilder.build().getNetworkPreferences(); 69 70 assertEquals(expectedNumberOfMappings, networkPreferences.size()); 71 assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); 72 } 73 74 @Test testGetNetworkPreferenceReturnsUnmodifiableValue()75 public void testGetNetworkPreferenceReturnsUnmodifiableValue() { 76 final String newPackage = "new.com.google.apps.contacts"; 77 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 78 79 final Map<String, Integer> networkPreferences = 80 mBuilder.build().getNetworkPreferences(); 81 82 assertThrows(UnsupportedOperationException.class, 83 () -> networkPreferences.put(newPackage, TEST_PREF)); 84 85 assertThrows(UnsupportedOperationException.class, 86 () -> networkPreferences.remove(TEST_PACKAGE)); 87 88 } 89 90 @Test testToStringReturnsCorrectValue()91 public void testToStringReturnsCorrectValue() { 92 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 93 94 final String networkPreferencesString = mBuilder.build().getNetworkPreferences().toString(); 95 96 assertTrue(networkPreferencesString.contains(Integer.toString(TEST_PREF))); 97 assertTrue(networkPreferencesString.contains(TEST_PACKAGE)); 98 } 99 100 @Test testOemNetworkPreferencesParcelable()101 public void testOemNetworkPreferencesParcelable() { 102 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 103 104 final OemNetworkPreferences prefs = mBuilder.build(); 105 106 assertParcelingIsLossless(prefs); 107 } 108 109 @Test testAddNetworkPreferenceOverwritesPriorPreference()110 public void testAddNetworkPreferenceOverwritesPriorPreference() { 111 final int newPref = OemNetworkPreferences.OEM_NETWORK_PREFERENCE_OEM_PAID; 112 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 113 Map<String, Integer> networkPreferences = 114 mBuilder.build().getNetworkPreferences(); 115 116 assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); 117 assertEquals(networkPreferences.get(TEST_PACKAGE).intValue(), TEST_PREF); 118 119 mBuilder.addNetworkPreference(TEST_PACKAGE, newPref); 120 networkPreferences = mBuilder.build().getNetworkPreferences(); 121 122 assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); 123 assertEquals(networkPreferences.get(TEST_PACKAGE).intValue(), newPref); 124 } 125 126 @Test testRemoveNetworkPreferenceRemovesValue()127 public void testRemoveNetworkPreferenceRemovesValue() { 128 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 129 Map<String, Integer> networkPreferences = 130 mBuilder.build().getNetworkPreferences(); 131 132 assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); 133 134 mBuilder.clearNetworkPreference(TEST_PACKAGE); 135 networkPreferences = mBuilder.build().getNetworkPreferences(); 136 137 assertFalse(networkPreferences.containsKey(TEST_PACKAGE)); 138 } 139 140 @Test testConstructorByOemNetworkPreferencesSetsValue()141 public void testConstructorByOemNetworkPreferencesSetsValue() { 142 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 143 OemNetworkPreferences networkPreference = mBuilder.build(); 144 145 final Map<String, Integer> networkPreferences = 146 new OemNetworkPreferences 147 .Builder(networkPreference) 148 .build() 149 .getNetworkPreferences(); 150 151 assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); 152 assertEquals(networkPreferences.get(TEST_PACKAGE).intValue(), TEST_PREF); 153 } 154 } 155