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 com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.content.Context; 24 import android.support.v7.preference.PreferenceScreen; 25 import android.view.View.OnClickListener; 26 import android.widget.Button; 27 28 import com.android.settings.R; 29 import com.android.settings.applications.LayoutPreference; 30 import com.android.settings.testutils.SettingsRobolectricTestRunner; 31 import com.android.settingslib.dream.DreamBackend; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Answers; 37 import org.mockito.ArgumentCaptor; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.util.ReflectionHelpers; 41 42 @RunWith(SettingsRobolectricTestRunner.class) 43 public class StartNowPreferenceControllerTest { 44 45 private StartNowPreferenceController mController; 46 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 47 private Context mContext; 48 @Mock 49 private PreferenceScreen mScreen; 50 @Mock 51 private LayoutPreference mLayoutPref; 52 @Mock 53 private Button mButton; 54 @Mock 55 private DreamBackend mBackend; 56 57 @Before 58 public void setup() { 59 MockitoAnnotations.initMocks(this); 60 61 mController = new StartNowPreferenceController(mContext); 62 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mLayoutPref); 63 when(mLayoutPref.findViewById(R.id.dream_start_now_button)).thenReturn(mButton); 64 65 ReflectionHelpers.setField(mController, "mBackend", mBackend); 66 } 67 68 @Test 69 public void setsOnClickListenerForStartNow() { 70 ArgumentCaptor<OnClickListener> captor = 71 ArgumentCaptor.forClass(Button.OnClickListener.class); 72 73 mController.displayPreference(mScreen); 74 verify(mButton).setOnClickListener(captor.capture()); 75 assertThat(captor.getValue()).isNotNull(); 76 } 77 78 @Test 79 public void buttonIsDisabledWhenNeverDreaming() { 80 when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.NEVER); 81 82 mController.updateState(mLayoutPref); 83 verify(mButton).setEnabled(false); 84 } 85 86 @Test 87 public void buttonIsEnabledWhenDreamIsAvailable() { 88 when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.EITHER); 89 90 mController.updateState(mLayoutPref); 91 verify(mButton).setEnabled(true); 92 } 93 } 94