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