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 static android.provider.Settings.System.SCREEN_BRIGHTNESS_MODE; 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.content.ContentResolver; 24 import android.content.Context; 25 import android.content.Intent; 26 import android.os.Parcel; 27 import android.provider.Settings; 28 29 import com.android.settings.search.ResultPayload.Availability; 30 import com.android.settings.search.ResultPayload.SettingsSource; 31 import com.android.settings.testutils.SettingsRobolectricTestRunner; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.robolectric.RuntimeEnvironment; 37 38 @RunWith(SettingsRobolectricTestRunner.class) 39 public class InlineSwitchPayloadTest { 40 41 private static final String DUMMY_SETTING = "inline_test"; 42 private static final int STANDARD_ON = 1; 43 private static final int FLIPPED_ON = 0; 44 45 private Context mContext; 46 47 @Before 48 public void setUp() { 49 mContext = RuntimeEnvironment.application; 50 } 51 52 @Test 53 public void testConstructor_DataRetained() { 54 final String uri = "test.com"; 55 final int type = ResultPayload.PayloadType.INLINE_SWITCH; 56 final int source = SettingsSource.SECURE; 57 final String intentKey = "key"; 58 final String intentVal = "value"; 59 final Intent intent = new Intent(); 60 intent.putExtra(intentKey, intentVal); 61 62 InlineSwitchPayload payload = 63 new InlineSwitchPayload(uri, source, 1, intent, true, 1 /* default */); 64 final Intent retainedIntent = payload.getIntent(); 65 assertThat(payload.getKey()).isEqualTo(uri); 66 assertThat(payload.getType()).isEqualTo(type); 67 assertThat(payload.mSettingSource).isEqualTo(source); 68 assertThat(payload.isStandard()).isTrue(); 69 assertThat(payload.getAvailability()).isEqualTo(ResultPayload.Availability.AVAILABLE); 70 assertThat(retainedIntent.getStringExtra(intentKey)).isEqualTo(intentVal); 71 } 72 73 @Test 74 public void testParcelConstructor_DataRetained() { 75 String uri = "test.com"; 76 int type = ResultPayload.PayloadType.INLINE_SWITCH; 77 int source = SettingsSource.SECURE; 78 final String intentKey = "key"; 79 final String intentVal = "value"; 80 final Intent intent = new Intent(); 81 intent.putExtra(intentKey, intentVal); 82 Parcel parcel = Parcel.obtain(); 83 parcel.writeParcelable(intent, 0); 84 parcel.writeString(uri); 85 parcel.writeInt(source); 86 parcel.writeInt(InlineSwitchPayload.TRUE); 87 parcel.writeInt(InlineSwitchPayload.TRUE); 88 parcel.writeInt(InlineSwitchPayload.TRUE); 89 parcel.setDataPosition(0); 90 91 InlineSwitchPayload payload = InlineSwitchPayload.CREATOR.createFromParcel(parcel); 92 93 final Intent builtIntent = payload.getIntent(); 94 assertThat(payload.getKey()).isEqualTo(uri); 95 assertThat(payload.getType()).isEqualTo(type); 96 assertThat(payload.mSettingSource).isEqualTo(source); 97 assertThat(payload.isStandard()).isTrue(); 98 assertThat(payload.getAvailability()).isEqualTo(Availability.AVAILABLE); 99 assertThat(builtIntent.getStringExtra(intentKey)).isEqualTo(intentVal); 100 } 101 102 @Test 103 public void testGetSystem_flippedSetting_returnsFlippedValue() { 104 // Stores 1s as 0s, and vis versa 105 InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM, 106 FLIPPED_ON, null /* intent */, true, 1 /* default */); 107 int currentValue = 1; 108 Settings.System.putInt(mContext.getContentResolver(), DUMMY_SETTING, currentValue); 109 110 int newValue = payload.getValue(mContext); 111 112 assertThat(newValue).isEqualTo(1 - currentValue); 113 } 114 115 @Test 116 public void testSetSystem_flippedSetting_updatesToFlippedValue() { 117 // Stores 1s as 0s, and vis versa 118 InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM, 119 FLIPPED_ON, null /* intent */, true, 1 /* default */); 120 int newValue = 1; 121 ContentResolver resolver = mContext.getContentResolver(); 122 Settings.System.putInt(resolver, SCREEN_BRIGHTNESS_MODE, newValue); 123 124 payload.setValue(mContext, newValue); 125 int updatedValue = Settings.System.getInt(resolver, DUMMY_SETTING, -1); 126 127 assertThat(updatedValue).isEqualTo(1 - newValue); 128 } 129 130 @Test(expected = IllegalArgumentException.class) 131 public void testSetSystem_negativeValue_ThrowsError() { 132 InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM, 133 STANDARD_ON, null /* intent */, true, 1 /* default */); 134 135 payload.setValue(mContext, -1); 136 } 137 138 @Test(expected = IllegalArgumentException.class) 139 public void testSetSystem_highValue_ThrowsError() { 140 InlineSwitchPayload payload = new InlineSwitchPayload(DUMMY_SETTING, SettingsSource.SYSTEM, 141 STANDARD_ON, null /* intent */, true, 1 /* default */); 142 143 payload.setValue(mContext, 2); 144 } 145 } 146