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.dream; 18 19 import static org.mockito.Mockito.mock; 20 import static org.mockito.Mockito.spy; 21 import static org.mockito.Mockito.verify; 22 import static org.mockito.Mockito.when; 23 24 import android.content.Context; 25 26 import androidx.preference.PreferenceScreen; 27 28 import com.android.settingslib.dream.DreamBackend; 29 import com.android.settingslib.widget.MainSwitchPreference; 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.util.ReflectionHelpers; 39 40 @RunWith(RobolectricTestRunner.class) 41 public class StartNowPreferenceControllerTest { 42 43 private StartNowPreferenceController mController; 44 private Context mContext; 45 46 @Mock 47 private PreferenceScreen mScreen; 48 @Mock 49 private MainSwitchPreference mPref; 50 @Mock 51 private DreamBackend mBackend; 52 53 @Before setup()54 public void setup() { 55 MockitoAnnotations.initMocks(this); 56 57 mContext = spy(RuntimeEnvironment.application); 58 mController = new StartNowPreferenceController(mContext, "key"); 59 mPref = mock(MainSwitchPreference.class); 60 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPref); 61 62 ReflectionHelpers.setField(mController, "mBackend", mBackend); 63 } 64 65 @Test displayPreference_shouldAddOnSwitchChangeListener()66 public void displayPreference_shouldAddOnSwitchChangeListener() { 67 mController.displayPreference(mScreen); 68 69 verify(mPref).addOnSwitchChangeListener(mController); 70 } 71 72 @Test updateState_neverDreaming_preferenceShouldDidabled()73 public void updateState_neverDreaming_preferenceShouldDidabled() { 74 when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.NEVER); 75 mController.displayPreference(mScreen); 76 77 mController.updateState(mPref); 78 79 verify(mPref).setEnabled(false); 80 } 81 82 @Test updateState_dreamIsAvailable_preferenceShouldEnabled()83 public void updateState_dreamIsAvailable_preferenceShouldEnabled() { 84 when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.EITHER); 85 mController.displayPreference(mScreen); 86 87 mController.updateState(mPref); 88 89 verify(mPref).setEnabled(true); 90 } 91 } 92