1 /* 2 * Copyright (C) 2016 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 18 package com.android.settings.search; 19 20 import android.os.Parcel; 21 import android.util.ArrayMap; 22 import android.content.Context; 23 24 import com.android.settings.SettingsRobolectricTestRunner; 25 import com.android.settings.TestConfig; 26 import com.android.settings.search2.InlineSwitchPayload; 27 import com.android.settings.search2.ResultPayload; 28 import org.junit.Test; 29 import org.junit.runner.RunWith; 30 import org.robolectric.annotation.Config; 31 import org.robolectric.shadows.ShadowApplication; 32 33 import static com.google.common.truth.Truth.assertThat; 34 import static org.junit.Assert.fail; 35 36 @RunWith(SettingsRobolectricTestRunner.class) 37 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 38 public class InlineSwitchPayloadTest { 39 40 @Test testGetSwitch_EmptyMap_ExceptionThrown()41 public void testGetSwitch_EmptyMap_ExceptionThrown() { 42 final String uri = "test.com"; 43 final int source = ResultPayload.SettingsSource.SECURE; 44 45 final Context context = ShadowApplication.getInstance().getApplicationContext(); 46 InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, null); 47 try { 48 payload.getSwitchValue(context); 49 fail("Should have thrown exception for null map"); 50 } catch (IllegalStateException e) { 51 assertThat(e).isNotNull(); 52 } 53 } 54 55 @Test testGetSwitch_BadMap_ExceptionThrown()56 public void testGetSwitch_BadMap_ExceptionThrown() { 57 final String uri = "test.com"; 58 final int source = ResultPayload.SettingsSource.SECURE; 59 final ArrayMap<Integer, Boolean> map = new ArrayMap<>(); 60 61 final Context context = ShadowApplication.getInstance().getApplicationContext(); 62 InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, map); 63 try { 64 payload.getSwitchValue(context); 65 fail("Should have thrown exception for bad map"); 66 } catch (IllegalStateException e) { 67 assertThat(e).isNotNull(); 68 } 69 } 70 71 @Test testConstructor_DataRetained()72 public void testConstructor_DataRetained() { 73 final String uri = "test.com"; 74 final int type = ResultPayload.PayloadType.INLINE_SWITCH; 75 final int source = ResultPayload.SettingsSource.SECURE; 76 final ArrayMap<Integer, Boolean> map = new ArrayMap<>(); 77 map.put(1, true); 78 map.put(0, false); 79 80 InlineSwitchPayload payload = new InlineSwitchPayload(uri, source, map); 81 assertThat(payload.settingsUri).isEqualTo(uri); 82 assertThat(payload.inlineType).isEqualTo(type); 83 assertThat(payload.settingSource).isEqualTo(source); 84 assertThat(payload.valueMap.get(1)).isTrue(); 85 assertThat(payload.valueMap.get(0)).isFalse(); 86 } 87 88 @Test testParcelConstructor_DataRetained()89 public void testParcelConstructor_DataRetained() { 90 String uri = "test.com"; 91 int type = ResultPayload.PayloadType.INLINE_SWITCH; 92 int source = ResultPayload.SettingsSource.SECURE; 93 final ArrayMap<Integer, Boolean> map = new ArrayMap<>(); 94 map.put(1, true); 95 map.put(0, false); 96 97 Parcel parcel = Parcel.obtain(); 98 parcel.writeString(uri); 99 parcel.writeInt(type); 100 parcel.writeInt(source); 101 parcel.writeMap(map); 102 parcel.setDataPosition(0); 103 104 InlineSwitchPayload payload = InlineSwitchPayload.CREATOR.createFromParcel(parcel); 105 assertThat(payload.settingsUri).isEqualTo(uri); 106 assertThat(payload.inlineType).isEqualTo(type); 107 assertThat(payload.settingSource).isEqualTo(source); 108 assertThat(payload.valueMap.get(1)).isTrue(); 109 assertThat(payload.valueMap.get(0)).isFalse(); 110 } 111 112 113 } 114