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.ArgumentMatchers.anyInt; 22 import static org.mockito.Mockito.spy; 23 import static org.mockito.Mockito.when; 24 25 import android.content.ContentResolver; 26 import android.content.Context; 27 import android.provider.Settings; 28 29 import com.android.internal.hardware.AmbientDisplayConfiguration; 30 import com.android.settings.search.InlinePayload; 31 import com.android.settings.search.InlineSwitchPayload; 32 import com.android.settings.testutils.SettingsRobolectricTestRunner; 33 import com.android.settings.testutils.shadow.ShadowSecureSettings; 34 35 import org.junit.Before; 36 import org.junit.Test; 37 import org.junit.runner.RunWith; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RuntimeEnvironment; 41 import org.robolectric.annotation.Config; 42 43 @RunWith(SettingsRobolectricTestRunner.class) 44 @Config(shadows = ShadowSecureSettings.class) 45 public class AmbientDisplayAlwaysOnPreferenceControllerTest { 46 47 @Mock 48 private AmbientDisplayConfiguration mConfig; 49 50 private Context mContext; 51 52 private ContentResolver mContentResolver; 53 54 private AmbientDisplayAlwaysOnPreferenceController mController; 55 private boolean mCallbackInvoked; 56 57 @Before 58 public void setUp() throws Exception { 59 MockitoAnnotations.initMocks(this); 60 mContext = RuntimeEnvironment.application; 61 mContentResolver = mContext.getContentResolver(); 62 mController = new AmbientDisplayAlwaysOnPreferenceController(mContext, "key"); 63 mController.setConfig(mConfig); 64 mController.setCallback(() -> mCallbackInvoked = true); 65 } 66 67 @Test 68 public void getAvailabilityStatus_available() { 69 when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(true); 70 71 assertThat(mController.getAvailabilityStatus()).isEqualTo( 72 AmbientDisplayAlwaysOnPreferenceController.AVAILABLE); 73 } 74 75 @Test 76 public void getAvailabilityStatus_disabled_unsupported() { 77 when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(false); 78 79 assertThat(mController.getAvailabilityStatus()).isEqualTo( 80 AmbientDisplayAlwaysOnPreferenceController.UNSUPPORTED_ON_DEVICE); 81 } 82 83 @Test 84 public void isChecked_enabled() { 85 when(mConfig.alwaysOnEnabled(anyInt())).thenReturn(true); 86 87 assertThat(mController.isChecked()).isTrue(); 88 } 89 90 @Test 91 public void isChecked_disabled() { 92 when(mConfig.alwaysOnEnabled(anyInt())).thenReturn(false); 93 94 assertThat(mController.isChecked()).isFalse(); 95 } 96 97 @Test 98 public void setChecked_enabled() { 99 mController.setChecked(true); 100 101 assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ALWAYS_ON, -1)) 102 .isEqualTo(1); 103 } 104 105 @Test 106 public void setChecked_disabled() { 107 mController.setChecked(false); 108 109 assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ALWAYS_ON, -1)) 110 .isEqualTo(0); 111 } 112 113 @Test 114 public void onPreferenceChange_callback() { 115 assertThat(mCallbackInvoked).isFalse(); 116 mController.setChecked(true); 117 assertThat(mCallbackInvoked).isTrue(); 118 } 119 120 @Test 121 public void testPreferenceController_ProperResultPayloadType() { 122 when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(false); 123 mController = spy(mController); 124 125 assertThat(mController.getResultPayload()).isInstanceOf(InlineSwitchPayload.class); 126 } 127 128 @Test 129 public void testSetValue_updatesCorrectly() { 130 when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(false); 131 mController = spy(mController); 132 final int newValue = 1; 133 Settings.Secure.putInt(mContentResolver, Settings.Secure.DOZE_ALWAYS_ON, 0 /* value */); 134 135 ((InlinePayload) mController.getResultPayload()).setValue(mContext, newValue); 136 final int updatedValue = Settings.Secure. 137 getInt(mContentResolver, Settings.Secure.DOZE_ALWAYS_ON, 1 /* default */); 138 139 assertThat(updatedValue).isEqualTo(newValue); 140 } 141 142 @Test 143 public void testGetValue_correctValueReturned() { 144 when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(false); 145 mController = spy(mController); 146 final int currentValue = 1; 147 Settings.Secure.putInt(mContentResolver, Settings.Secure.DOZE_ALWAYS_ON, currentValue); 148 149 final int newValue = ((InlinePayload) mController.getResultPayload()).getValue(mContext); 150 151 assertThat(newValue).isEqualTo(currentValue); 152 } 153 154 @Test 155 public void isSliceableCorrectKey_returnsTrue() { 156 final AmbientDisplayAlwaysOnPreferenceController controller = 157 new AmbientDisplayAlwaysOnPreferenceController(mContext, 158 "ambient_display_always_on"); 159 assertThat(controller.isSliceable()).isTrue(); 160 } 161 162 @Test 163 public void isSliceableIncorrectKey_returnsFalse() { 164 final AmbientDisplayAlwaysOnPreferenceController controller = 165 new AmbientDisplayAlwaysOnPreferenceController(mContext, "bad_key"); 166 assertThat(controller.isSliceable()).isFalse(); 167 } 168 } 169