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