1 /* 2 * Copyright (C) 2017 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 com.android.settings.display; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.Matchers.anyInt; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.provider.Settings; 28 import android.support.v14.preference.SwitchPreference; 29 30 import com.android.internal.hardware.AmbientDisplayConfiguration; 31 import com.android.settings.TestConfig; 32 import com.android.settings.search.InlinePayload; 33 import com.android.settings.search.InlineSwitchPayload; 34 import com.android.settings.testutils.SettingsRobolectricTestRunner; 35 import com.android.settings.testutils.shadow.ShadowSecureSettings; 36 37 import org.junit.Before; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 import org.mockito.Mock; 41 import org.mockito.MockitoAnnotations; 42 import org.robolectric.annotation.Config; 43 44 @RunWith(SettingsRobolectricTestRunner.class) 45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, 46 shadows = {ShadowSecureSettings.class}) 47 public class AmbientDisplayAlwaysOnPreferenceControllerTest { 48 49 @Mock Context mContext; 50 @Mock AmbientDisplayConfiguration mConfig; 51 @Mock SwitchPreference mSwitchPreference; 52 53 AmbientDisplayAlwaysOnPreferenceController mController; 54 boolean mCallbackInvoked; 55 56 @Before 57 public void setUp() throws Exception { 58 MockitoAnnotations.initMocks(this); 59 mController = new AmbientDisplayAlwaysOnPreferenceController(mContext, mConfig, 60 () -> { mCallbackInvoked = true; }); 61 } 62 63 @Test 64 public void updateState_enabled() throws Exception { 65 when(mConfig.alwaysOnEnabled(anyInt())) 66 .thenReturn(true); 67 68 mController.updateState(mSwitchPreference); 69 70 verify(mSwitchPreference).setChecked(true); 71 } 72 73 @Test 74 public void updateState_disabled() throws Exception { 75 when(mConfig.alwaysOnEnabled(anyInt())) 76 .thenReturn(false); 77 78 mController.updateState(mSwitchPreference); 79 80 verify(mSwitchPreference).setChecked(false); 81 } 82 83 @Test 84 public void onPreferenceChange_callback() throws Exception { 85 assertThat(mCallbackInvoked).isFalse(); 86 mController.onPreferenceChange(mSwitchPreference, true); 87 assertThat(mCallbackInvoked).isTrue(); 88 } 89 90 @Test 91 public void onPreferenceChange_enable() throws Exception { 92 mController.onPreferenceChange(mSwitchPreference, true); 93 94 assertThat(Settings.Secure.getInt(null, Settings.Secure.DOZE_ALWAYS_ON, -1)) 95 .isEqualTo(1); 96 } 97 98 @Test 99 public void onPreferenceChange_disable() throws Exception { 100 mController.onPreferenceChange(mSwitchPreference, false); 101 102 assertThat(Settings.Secure.getInt(null, Settings.Secure.DOZE_ALWAYS_ON, -1)) 103 .isEqualTo(0); 104 } 105 106 @Test 107 public void isAvailable_available() throws Exception { 108 when(mConfig.alwaysOnAvailableForUser(anyInt())) 109 .thenReturn(true); 110 111 assertThat(mController.isAvailable()).isTrue(); 112 } 113 114 @Test 115 public void isAvailable_unavailable() throws Exception { 116 when(mConfig.alwaysOnAvailableForUser(anyInt())) 117 .thenReturn(false); 118 119 assertThat(mController.isAvailable()).isFalse(); 120 } 121 122 @Test 123 public void testPreferenceController_ProperResultPayloadType() { 124 assertThat(mController.getResultPayload()).isInstanceOf(InlineSwitchPayload.class); 125 } 126 127 @Test 128 @Config(shadows = ShadowSecureSettings.class) 129 public void testSetValue_updatesCorrectly() { 130 int newValue = 1; 131 ContentResolver resolver = mContext.getContentResolver(); 132 Settings.Secure.putInt(resolver, Settings.Secure.DOZE_ALWAYS_ON, 0); 133 134 ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue); 135 int updatedValue = Settings.Secure.getInt(resolver, Settings.Secure.DOZE_ALWAYS_ON, 1); 136 137 assertThat(updatedValue).isEqualTo(newValue); 138 } 139 140 @Test 141 @Config(shadows = ShadowSecureSettings.class) 142 public void testGetValue_correctValueReturned() { 143 int currentValue = 1; 144 ContentResolver resolver = mContext.getContentResolver(); 145 Settings.Secure.putInt(resolver, Settings.Secure.DOZE_ALWAYS_ON, currentValue); 146 147 int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext); 148 149 assertThat(newValue).isEqualTo(currentValue); 150 } 151 } 152