1 /* 2 * Copyright (C) 2019 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 android.content.Context; 20 import androidx.fragment.app.Fragment; 21 import com.android.settings.display.darkmode.DarkModePreference; 22 import org.junit.Before; 23 import org.junit.Test; 24 import org.junit.runner.RunWith; 25 import org.mockito.Mock; 26 import org.mockito.MockitoAnnotations; 27 import org.robolectric.RobolectricTestRunner; 28 import org.robolectric.RuntimeEnvironment; 29 30 import static com.google.common.truth.Truth.assertThat; 31 import static org.mockito.Mockito.doReturn; 32 import static org.mockito.Mockito.spy; 33 34 @RunWith(RobolectricTestRunner.class) 35 public class DarkUIPreferenceControllerTest { 36 37 private DarkUIPreferenceController mController; 38 private Context mContext; 39 @Mock 40 private Fragment mFragment; 41 42 @Before setUp()43 public void setUp() { 44 MockitoAnnotations.initMocks(this); 45 mContext = spy(RuntimeEnvironment.application); 46 mController = spy(new DarkUIPreferenceController(mContext, "dark_ui_mode")); 47 mController.setParentFragment(mFragment); 48 mController.mPreference = new DarkModePreference(mContext, null /* AttributeSet attrs */); 49 mController.onStart(); 50 } 51 52 @Test batterySaverToggles_disabledStateUpdates()53 public void batterySaverToggles_disabledStateUpdates() { 54 doReturn(true).when(mController).isPowerSaveMode(); 55 mController.updateEnabledStateIfNeeded(); 56 assertThat(mController.mPreference.isEnabled()).isFalse(); 57 58 doReturn(false).when(mController).isPowerSaveMode(); 59 mController.updateEnabledStateIfNeeded(); 60 assertThat(mController.mPreference.isEnabled()).isTrue(); 61 62 doReturn(true).when(mController).isPowerSaveMode(); 63 mController.updateEnabledStateIfNeeded(); 64 assertThat(mController.mPreference.isEnabled()).isFalse(); 65 } 66 } 67