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.spy;
20 import static org.mockito.Mockito.verify;
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.widget.Button;
25 
26 import androidx.preference.PreferenceScreen;
27 
28 import com.android.settings.R;
29 import com.android.settingslib.dream.DreamBackend;
30 import com.android.settingslib.widget.LayoutPreference;
31 
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.RobolectricTestRunner;
38 import org.robolectric.RuntimeEnvironment;
39 import org.robolectric.util.ReflectionHelpers;
40 
41 @RunWith(RobolectricTestRunner.class)
42 public class StartNowPreferenceControllerTest {
43 
44     private StartNowPreferenceController mController;
45     private Context mContext;
46 
47     @Mock
48     private PreferenceScreen mScreen;
49     @Mock
50     private LayoutPreference mLayoutPref;
51     @Mock
52     private Button mButton;
53     @Mock
54     private DreamBackend mBackend;
55 
56     @Before
setup()57     public void setup() {
58         MockitoAnnotations.initMocks(this);
59 
60         mContext = spy(RuntimeEnvironment.application);
61         mController = new StartNowPreferenceController(mContext, "key");
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
updateState_neverDreaming_buttonShouldDidabled()69     public void updateState_neverDreaming_buttonShouldDidabled() {
70         when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.NEVER);
71         mController.displayPreference(mScreen);
72 
73         mController.updateState(mLayoutPref);
74 
75         verify(mButton).setEnabled(false);
76     }
77 
78     @Test
updateState_dreamIsAvailable_buttonShouldEnabled()79     public void updateState_dreamIsAvailable_buttonShouldEnabled() {
80         when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.EITHER);
81         mController.displayPreference(mScreen);
82 
83         mController.updateState(mLayoutPref);
84 
85         verify(mButton).setEnabled(true);
86     }
87 }
88