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.verify;
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 
25 import androidx.preference.Preference;
26 
27 import com.android.settingslib.dream.DreamBackend;
28 import com.android.settingslib.dream.DreamBackend.WhenToDream;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Answers;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.robolectric.RobolectricTestRunner;
37 import org.robolectric.util.ReflectionHelpers;
38 
39 @RunWith(RobolectricTestRunner.class)
40 public class WhenToDreamPreferenceControllerTest {
41 
42     private WhenToDreamPreferenceController mController;
43     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
44     private Context mContext;
45     @Mock
46     private DreamBackend mBackend;
47 
48     @Before
setup()49     public void setup() {
50         MockitoAnnotations.initMocks(this);
51         mController = new WhenToDreamPreferenceController(mContext);
52         ReflectionHelpers.setField(mController, "mBackend", mBackend);
53     }
54 
55     @Test
updateSummary()56     public void updateSummary() {
57         // Don't have to test the other settings because DreamSettings tests that all
58         // @WhenToDream values map to the correct ResId
59         final @WhenToDream int testSetting = DreamBackend.WHILE_CHARGING;
60         final Preference mockPref = mock(Preference.class);
61         when(mockPref.getContext()).thenReturn(mContext);
62         when(mBackend.getWhenToDreamSetting()).thenReturn(testSetting);
63         final String expectedString =
64                 mContext.getString(DreamSettings.getDreamSettingDescriptionResId(testSetting));
65 
66         mController.updateState(mockPref);
67         verify(mockPref).setSummary(expectedString);
68     }
69 }
70