1 /* 2 * Copyright (C) 2018 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.development; 18 19 import static org.mockito.ArgumentMatchers.eq; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.app.UiModeManager; 24 import android.content.Context; 25 import android.support.v7.preference.ListPreference; 26 import android.support.v7.preference.PreferenceScreen; 27 28 import com.android.settings.testutils.SettingsRobolectricTestRunner; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Mock; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RuntimeEnvironment; 36 37 @RunWith(SettingsRobolectricTestRunner.class) 38 public class DarkUIPreferenceControllerTest { 39 40 private Context mContext; 41 @Mock 42 private ListPreference mPreference; 43 @Mock 44 private PreferenceScreen mPreferenceScreen; 45 @Mock 46 private UiModeManager mUiModeManager; 47 private DarkUIPreferenceController mController; 48 49 @Before setup()50 public void setup() { 51 MockitoAnnotations.initMocks(this); 52 mContext = RuntimeEnvironment.application; 53 mController = new DarkUIPreferenceController(mContext, mUiModeManager); 54 when(mPreferenceScreen.findPreference(mController.getPreferenceKey())) 55 .thenReturn(mPreference); 56 mController.displayPreference(mPreferenceScreen); 57 } 58 59 @Test onPreferenceChanged_setAuto()60 public void onPreferenceChanged_setAuto() { 61 mController.onPreferenceChange(mPreference, "auto"); 62 verify(mUiModeManager).setNightMode(eq(UiModeManager.MODE_NIGHT_AUTO)); 63 } 64 65 @Test onPreferenceChanged_setNightMode()66 public void onPreferenceChanged_setNightMode() { 67 mController.onPreferenceChange(mPreference, "yes"); 68 verify(mUiModeManager).setNightMode(eq(UiModeManager.MODE_NIGHT_YES)); 69 } 70 71 @Test onPreferenceChanged_setDayMode()72 public void onPreferenceChanged_setDayMode() { 73 mController.onPreferenceChange(mPreference, "no"); 74 verify(mUiModeManager).setNightMode(eq(UiModeManager.MODE_NIGHT_NO)); 75 } 76 getCurrentMode()77 public int getCurrentMode() { 78 final UiModeManager uiModeManager = mContext.getSystemService(UiModeManager.class); 79 return uiModeManager.getNightMode(); 80 } 81 } 82