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.assertParcelSane; 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.DevSdkIgnoreRule.IgnoreUpTo; 31 import com.android.testutils.DevSdkIgnoreRunner; 32 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 36 import java.util.Map; 37 38 @IgnoreUpTo(Build.VERSION_CODES.R) 39 @RunWith(DevSdkIgnoreRunner.class) 40 @SmallTest 41 public class OemNetworkPreferencesTest { 42 43 private static final int TEST_PREF = OemNetworkPreferences.OEM_NETWORK_PREFERENCE_UNINITIALIZED; 44 private static final String TEST_PACKAGE = "com.google.apps.contacts"; 45 46 private final OemNetworkPreferences.Builder mBuilder = new OemNetworkPreferences.Builder(); 47 48 @Test testBuilderAddNetworkPreferenceRequiresNonNullPackageName()49 public void testBuilderAddNetworkPreferenceRequiresNonNullPackageName() { 50 assertThrows(NullPointerException.class, 51 () -> mBuilder.addNetworkPreference(null, TEST_PREF)); 52 } 53 54 @Test testBuilderRemoveNetworkPreferenceRequiresNonNullPackageName()55 public void testBuilderRemoveNetworkPreferenceRequiresNonNullPackageName() { 56 assertThrows(NullPointerException.class, 57 () -> mBuilder.clearNetworkPreference(null)); 58 } 59 60 @Test testGetNetworkPreferenceReturnsCorrectValue()61 public void testGetNetworkPreferenceReturnsCorrectValue() { 62 final int expectedNumberOfMappings = 1; 63 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 64 65 final Map<String, Integer> networkPreferences = 66 mBuilder.build().getNetworkPreferences(); 67 68 assertEquals(expectedNumberOfMappings, networkPreferences.size()); 69 assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); 70 } 71 72 @Test testGetNetworkPreferenceReturnsUnmodifiableValue()73 public void testGetNetworkPreferenceReturnsUnmodifiableValue() { 74 final String newPackage = "new.com.google.apps.contacts"; 75 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 76 77 final Map<String, Integer> networkPreferences = 78 mBuilder.build().getNetworkPreferences(); 79 80 assertThrows(UnsupportedOperationException.class, 81 () -> networkPreferences.put(newPackage, TEST_PREF)); 82 83 assertThrows(UnsupportedOperationException.class, 84 () -> networkPreferences.remove(TEST_PACKAGE)); 85 86 } 87 88 @Test testToStringReturnsCorrectValue()89 public void testToStringReturnsCorrectValue() { 90 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 91 92 final String networkPreferencesString = mBuilder.build().getNetworkPreferences().toString(); 93 94 assertTrue(networkPreferencesString.contains(Integer.toString(TEST_PREF))); 95 assertTrue(networkPreferencesString.contains(TEST_PACKAGE)); 96 } 97 98 @Test testOemNetworkPreferencesParcelable()99 public void testOemNetworkPreferencesParcelable() { 100 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 101 102 final OemNetworkPreferences prefs = mBuilder.build(); 103 104 assertParcelSane(prefs, 1 /* fieldCount */); 105 } 106 107 @Test testAddNetworkPreferenceOverwritesPriorPreference()108 public void testAddNetworkPreferenceOverwritesPriorPreference() { 109 final int newPref = OemNetworkPreferences.OEM_NETWORK_PREFERENCE_OEM_PAID; 110 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 111 Map<String, Integer> networkPreferences = 112 mBuilder.build().getNetworkPreferences(); 113 114 assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); 115 assertEquals(networkPreferences.get(TEST_PACKAGE).intValue(), TEST_PREF); 116 117 mBuilder.addNetworkPreference(TEST_PACKAGE, newPref); 118 networkPreferences = mBuilder.build().getNetworkPreferences(); 119 120 assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); 121 assertEquals(networkPreferences.get(TEST_PACKAGE).intValue(), newPref); 122 } 123 124 @Test testRemoveNetworkPreferenceRemovesValue()125 public void testRemoveNetworkPreferenceRemovesValue() { 126 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 127 Map<String, Integer> networkPreferences = 128 mBuilder.build().getNetworkPreferences(); 129 130 assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); 131 132 mBuilder.clearNetworkPreference(TEST_PACKAGE); 133 networkPreferences = mBuilder.build().getNetworkPreferences(); 134 135 assertFalse(networkPreferences.containsKey(TEST_PACKAGE)); 136 } 137 138 @Test testConstructorByOemNetworkPreferencesSetsValue()139 public void testConstructorByOemNetworkPreferencesSetsValue() { 140 mBuilder.addNetworkPreference(TEST_PACKAGE, TEST_PREF); 141 OemNetworkPreferences networkPreference = mBuilder.build(); 142 143 final Map<String, Integer> networkPreferences = 144 new OemNetworkPreferences 145 .Builder(networkPreference) 146 .build() 147 .getNetworkPreferences(); 148 149 assertTrue(networkPreferences.containsKey(TEST_PACKAGE)); 150 assertEquals(networkPreferences.get(TEST_PACKAGE).intValue(), TEST_PREF); 151 } 152 } 153