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.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.support.v7.preference.Preference; 27 28 import com.android.internal.hardware.AmbientDisplayConfiguration; 29 import com.android.settings.R; 30 import com.android.settings.testutils.SettingsRobolectricTestRunner; 31 import com.android.settings.TestConfig; 32 import com.android.settings.testutils.shadow.ShadowSecureSettings; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Mock; 38 import org.mockito.MockitoAnnotations; 39 import org.robolectric.annotation.Config; 40 41 @RunWith(SettingsRobolectricTestRunner.class) 42 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION, 43 shadows = {ShadowSecureSettings.class}) 44 public class AmbientDisplayPreferenceControllerTest { 45 46 @Mock Context mContext; 47 @Mock AmbientDisplayConfiguration mConfig; 48 @Mock Preference mPreference; 49 50 AmbientDisplayPreferenceController mController; 51 52 @Before 53 public void setUp() throws Exception { 54 MockitoAnnotations.initMocks(this); 55 mController = new AmbientDisplayPreferenceController(mContext, mConfig, "key"); 56 } 57 58 @Test 59 public void isAvailable_available() throws Exception { 60 when(mConfig.available()).thenReturn(true); 61 assertThat(mController.isAvailable()).isTrue(); 62 } 63 64 @Test 65 public void isAvailable_unavailable() throws Exception { 66 when(mConfig.available()).thenReturn(false); 67 assertThat(mController.isAvailable()).isFalse(); 68 } 69 70 @Test 71 public void updateState_alwaysOn() throws Exception { 72 when(mConfig.alwaysOnEnabled(anyInt())).thenReturn(true); 73 74 mController.updateState(mPreference); 75 76 verify(mPreference).setSummary(R.string.ambient_display_screen_summary_always_on); 77 } 78 79 @Test 80 public void updateState_notifications() throws Exception { 81 when(mConfig.alwaysOnEnabled(anyInt())).thenReturn(false); 82 when(mConfig.pulseOnNotificationEnabled(anyInt())).thenReturn(true); 83 84 mController.updateState(mPreference); 85 86 verify(mPreference).setSummary(R.string.ambient_display_screen_summary_notifications); 87 } 88 89 @Test 90 public void updateState_gestures() throws Exception { 91 when(mConfig.alwaysOnEnabled(anyInt())).thenReturn(false); 92 when(mConfig.pulseOnNotificationEnabled(anyInt())).thenReturn(false); 93 when(mConfig.enabled(anyInt())).thenReturn(true); 94 95 mController.updateState(mPreference); 96 97 verify(mPreference).setSummary(R.string.switch_on_text); 98 } 99 100 @Test 101 public void updateState_off() throws Exception { 102 when(mConfig.alwaysOnEnabled(anyInt())).thenReturn(false); 103 when(mConfig.pulseOnNotificationEnabled(anyInt())).thenReturn(false); 104 when(mConfig.pulseOnDoubleTapEnabled(anyInt())).thenReturn(false); 105 when(mConfig.pulseOnPickupEnabled(anyInt())).thenReturn(false); 106 107 mController.updateState(mPreference); 108 109 verify(mPreference).setSummary(R.string.switch_off_text); 110 } 111 112 @Test 113 public void getPreferenceKey() throws Exception { 114 assertThat(mController.getPreferenceKey()).isEqualTo("key"); 115 } 116 117 }