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.when; 23 24 import android.content.ContentResolver; 25 import android.content.Context; 26 import android.hardware.display.AmbientDisplayConfiguration; 27 import android.provider.Settings; 28 29 import com.android.settings.testutils.shadow.ShadowSecureSettings; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.RobolectricTestRunner; 37 import org.robolectric.RuntimeEnvironment; 38 import org.robolectric.annotation.Config; 39 40 @RunWith(RobolectricTestRunner.class) 41 @Config(shadows = ShadowSecureSettings.class) 42 public class AmbientDisplayAlwaysOnPreferenceControllerTest { 43 44 @Mock 45 private AmbientDisplayConfiguration mConfig; 46 47 private Context mContext; 48 49 private ContentResolver mContentResolver; 50 51 private AmbientDisplayAlwaysOnPreferenceController mController; 52 private boolean mCallbackInvoked; 53 54 @Before setUp()55 public void setUp() throws Exception { 56 MockitoAnnotations.initMocks(this); 57 mContext = RuntimeEnvironment.application; 58 mContentResolver = mContext.getContentResolver(); 59 mController = new AmbientDisplayAlwaysOnPreferenceController(mContext, "key"); 60 mController.setConfig(mConfig); 61 mController.setCallback(() -> mCallbackInvoked = true); 62 } 63 64 @Test getAvailabilityStatus_available()65 public void getAvailabilityStatus_available() { 66 when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(true); 67 68 assertThat(mController.getAvailabilityStatus()).isEqualTo( 69 AmbientDisplayAlwaysOnPreferenceController.AVAILABLE); 70 } 71 72 @Test getAvailabilityStatus_disabled_unsupported()73 public void getAvailabilityStatus_disabled_unsupported() { 74 when(mConfig.alwaysOnAvailableForUser(anyInt())).thenReturn(false); 75 76 assertThat(mController.getAvailabilityStatus()).isEqualTo( 77 AmbientDisplayAlwaysOnPreferenceController.UNSUPPORTED_ON_DEVICE); 78 } 79 80 @Test isChecked_enabled()81 public void isChecked_enabled() { 82 when(mConfig.alwaysOnEnabled(anyInt())).thenReturn(true); 83 84 assertThat(mController.isChecked()).isTrue(); 85 } 86 87 @Test isChecked_disabled()88 public void isChecked_disabled() { 89 when(mConfig.alwaysOnEnabled(anyInt())).thenReturn(false); 90 91 assertThat(mController.isChecked()).isFalse(); 92 } 93 94 @Test setChecked_enabled()95 public void setChecked_enabled() { 96 mController.setChecked(true); 97 98 assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ALWAYS_ON, -1)) 99 .isEqualTo(1); 100 } 101 102 @Test setChecked_disabled()103 public void setChecked_disabled() { 104 mController.setChecked(false); 105 106 assertThat(Settings.Secure.getInt(mContentResolver, Settings.Secure.DOZE_ALWAYS_ON, -1)) 107 .isEqualTo(0); 108 } 109 110 @Test onPreferenceChange_callback()111 public void onPreferenceChange_callback() { 112 assertThat(mCallbackInvoked).isFalse(); 113 mController.setChecked(true); 114 assertThat(mCallbackInvoked).isTrue(); 115 } 116 117 @Test isSliceableCorrectKey_returnsTrue()118 public void isSliceableCorrectKey_returnsTrue() { 119 final AmbientDisplayAlwaysOnPreferenceController controller = 120 new AmbientDisplayAlwaysOnPreferenceController(mContext, 121 "ambient_display_always_on"); 122 assertThat(controller.isSliceable()).isTrue(); 123 } 124 125 @Test isSliceableIncorrectKey_returnsFalse()126 public void isSliceableIncorrectKey_returnsFalse() { 127 final AmbientDisplayAlwaysOnPreferenceController controller = 128 new AmbientDisplayAlwaysOnPreferenceController(mContext, "bad_key"); 129 assertThat(controller.isSliceable()).isFalse(); 130 } 131 132 @Test isPublicSlice_returnTrue()133 public void isPublicSlice_returnTrue() { 134 assertThat(mController.isPublicSlice()).isTrue(); 135 } 136 } 137