1 /* 2 * Copyright (C) 2018 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file 5 * except in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the 10 * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 11 * KIND, either express or implied. See the License for the specific language governing 12 * permissions and limitations under the License. 13 */ 14 15 package com.android.settings.display; 16 17 import static com.google.common.truth.Truth.assertThat; 18 19 import static org.mockito.Matchers.anyString; 20 import static org.mockito.Mockito.when; 21 22 import android.content.Context; 23 import android.provider.Settings.Secure; 24 import android.support.v7.preference.PreferenceScreen; 25 26 import android.view.View; 27 import com.android.settings.R; 28 import com.android.settings.applications.LayoutPreference; 29 30 import com.android.settings.testutils.SettingsRobolectricTestRunner; 31 import com.android.settings.testutils.shadow.SettingsShadowResources; 32 import org.junit.Before; 33 import org.junit.Test; 34 import org.junit.runner.RunWith; 35 import org.mockito.Mock; 36 import org.mockito.MockitoAnnotations; 37 import org.robolectric.RuntimeEnvironment; 38 import org.robolectric.annotation.Config; 39 40 @RunWith(SettingsRobolectricTestRunner.class) 41 @Config(shadows = SettingsShadowResources.class) 42 public class NightDisplayActivationPreferenceControllerTest { 43 44 @Mock 45 private PreferenceScreen mScreen; 46 private LayoutPreference mPreference; 47 private Context mContext; 48 private NightDisplayActivationPreferenceController mController; 49 50 @Before setUp()51 public void setUp() { 52 MockitoAnnotations.initMocks(this); 53 mContext = RuntimeEnvironment.application; 54 mPreference = new LayoutPreference(mContext, R.layout.night_display_activation_button); 55 when(mScreen.findPreference(anyString())).thenReturn(mPreference); 56 mController = new NightDisplayActivationPreferenceController(mContext, 57 "night_display_activation"); 58 mController.displayPreference(mScreen); 59 } 60 61 @Test isAvailable_configuredAvailable()62 public void isAvailable_configuredAvailable() { 63 SettingsShadowResources.overrideResource( 64 com.android.internal.R.bool.config_nightDisplayAvailable, true); 65 assertThat(mController.isAvailable()).isTrue(); 66 } 67 68 @Test isAvailable_configuredUnavailable()69 public void isAvailable_configuredUnavailable() { 70 SettingsShadowResources.overrideResource( 71 com.android.internal.R.bool.config_nightDisplayAvailable, false); 72 assertThat(mController.isAvailable()).isFalse(); 73 } 74 75 @Test isSliceableCorrectKey_returnsTrue()76 public void isSliceableCorrectKey_returnsTrue() { 77 final NightDisplayActivationPreferenceController controller = 78 new NightDisplayActivationPreferenceController(mContext,"night_display_activated"); 79 assertThat(controller.isSliceable()).isTrue(); 80 } 81 82 @Test isSliceableIncorrectKey_returnsFalse()83 public void isSliceableIncorrectKey_returnsFalse() { 84 final NightDisplayActivationPreferenceController controller = 85 new NightDisplayActivationPreferenceController(mContext, "bad_key"); 86 assertThat(controller.isSliceable()).isFalse(); 87 } 88 89 @Test onClick_activates()90 public void onClick_activates() { 91 Secure.putInt(mContext.getContentResolver(), Secure.NIGHT_DISPLAY_ACTIVATED, 0); 92 93 final View view = mPreference.findViewById(R.id.night_display_turn_on_button); 94 assertThat(view.getVisibility()).isEqualTo(View.VISIBLE); 95 view.performClick(); 96 97 assertThat(Secure.getInt(mContext.getContentResolver(), Secure.NIGHT_DISPLAY_ACTIVATED, -1)) 98 .isEqualTo(1); 99 } 100 101 @Test onClick_deactivates()102 public void onClick_deactivates() { 103 Secure.putInt(mContext.getContentResolver(), Secure.NIGHT_DISPLAY_ACTIVATED, 1); 104 105 final View view = mPreference.findViewById(R.id.night_display_turn_on_button); 106 assertThat(view.getVisibility()).isEqualTo(View.VISIBLE); 107 view.performClick(); 108 109 assertThat(Secure.getInt(mContext.getContentResolver(), Secure.NIGHT_DISPLAY_ACTIVATED, -1)) 110 .isEqualTo(0); 111 } 112 } 113