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 
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.Context;
25 
26 import com.android.settings.R;
27 import com.android.settingslib.dream.DreamBackend;
28 import com.android.settingslib.dream.DreamBackend.WhenToDream;
29 
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 
33 import java.util.Arrays;
34 import java.util.List;
35 import org.robolectric.RobolectricTestRunner;
36 
37 @RunWith(RobolectricTestRunner.class)
38 public class DreamSettingsTest {
39 
40     private static final List<String> KEYS = Arrays.asList(
41         DreamSettings.WHILE_CHARGING_ONLY,
42         DreamSettings.WHILE_DOCKED_ONLY,
43         DreamSettings.EITHER_CHARGING_OR_DOCKED,
44         DreamSettings.NEVER_DREAM
45     );
46 
47     private static final @WhenToDream int[] SETTINGS = {
48         DreamBackend.WHILE_CHARGING,
49         DreamBackend.WHILE_DOCKED,
50         DreamBackend.EITHER,
51         DreamBackend.NEVER,
52     };
53 
54     private static final int[] RES_IDS = {
55         R.string.screensaver_settings_summary_sleep,
56         R.string.screensaver_settings_summary_dock,
57         R.string.screensaver_settings_summary_either_long,
58         R.string.screensaver_settings_summary_never
59     };
60 
61     @Test
getSettingFromPrefKey()62     public void getSettingFromPrefKey() {
63         for (int i = 0; i < KEYS.size(); i++) {
64             assertThat(DreamSettings.getSettingFromPrefKey(KEYS.get(i))).isEqualTo(SETTINGS[i]);
65         }
66         // Default case
67         assertThat(DreamSettings.getSettingFromPrefKey("garbage value"))
68                 .isEqualTo(DreamBackend.NEVER);
69     }
70 
71     @Test
getKeyFromSetting()72     public void getKeyFromSetting() {
73         for (int i = 0; i < SETTINGS.length; i++) {
74             assertThat(DreamSettings.getKeyFromSetting(SETTINGS[i])).isEqualTo(KEYS.get(i));
75         }
76         // Default
77         assertThat(DreamSettings.getKeyFromSetting(-1))
78                 .isEqualTo(DreamSettings.NEVER_DREAM);
79     }
80 
81     @Test
getDreamSettingDescriptionResId()82     public void getDreamSettingDescriptionResId() {
83         for (int i = 0; i < SETTINGS.length; i++) {
84             assertThat(DreamSettings.getDreamSettingDescriptionResId(SETTINGS[i]))
85                     .isEqualTo(RES_IDS[i]);
86         }
87         // Default
88         assertThat(DreamSettings.getDreamSettingDescriptionResId(-1))
89                 .isEqualTo(R.string.screensaver_settings_summary_never);
90     }
91 
92     @Test
summaryText_whenDreamsAreOff()93     public void summaryText_whenDreamsAreOff() {
94         DreamBackend mockBackend = mock(DreamBackend.class);
95         Context mockContext = mock(Context.class);
96         when(mockBackend.isEnabled()).thenReturn(false);
97 
98         assertThat(DreamSettings.getSummaryTextFromBackend(mockBackend, mockContext))
99                 .isEqualTo(mockContext.getString(R.string.screensaver_settings_summary_off));
100     }
101 
102     @Test
summaryTest_WhenDreamsAreOn()103     public void summaryTest_WhenDreamsAreOn() {
104         final String fakeName = "test_name";
105         DreamBackend mockBackend = mock(DreamBackend.class);
106         Context mockContext = mock(Context.class);
107         when(mockBackend.isEnabled()).thenReturn(true);
108         when(mockBackend.getActiveDreamName()).thenReturn(fakeName);
109 
110         assertThat(DreamSettings.getSummaryTextFromBackend(mockBackend, mockContext))
111                 .isEqualTo(fakeName);
112     }
113 }
114