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.ArgumentMatchers.anyString; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.mock; 24 import static org.mockito.Mockito.verify; 25 import static org.mockito.Mockito.when; 26 27 import android.content.Context; 28 import android.os.Bundle; 29 30 import androidx.preference.Preference; 31 import androidx.preference.PreferenceManager; 32 import androidx.preference.PreferenceScreen; 33 import androidx.test.core.app.ApplicationProvider; 34 35 import com.android.settings.R; 36 import com.android.settings.testutils.shadow.ShadowFragment; 37 import com.android.settingslib.dream.DreamBackend; 38 import com.android.settingslib.dream.DreamBackend.WhenToDream; 39 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.mockito.ArgumentCaptor; 43 import org.mockito.Captor; 44 import org.mockito.Mock; 45 import org.mockito.MockitoAnnotations; 46 import org.robolectric.RobolectricTestRunner; 47 import org.robolectric.annotation.Config; 48 49 import java.util.Arrays; 50 import java.util.HashMap; 51 import java.util.List; 52 53 @Config(shadows = ShadowFragment.class) 54 @RunWith(RobolectricTestRunner.class) 55 public class DreamSettingsTest { 56 57 private static final List<String> KEYS = Arrays.asList( 58 DreamSettings.WHILE_CHARGING_ONLY, 59 DreamSettings.WHILE_DOCKED_ONLY, 60 DreamSettings.EITHER_CHARGING_OR_DOCKED, 61 DreamSettings.NEVER_DREAM 62 ); 63 64 @WhenToDream 65 private static final int[] SETTINGS = { 66 DreamBackend.WHILE_CHARGING, 67 DreamBackend.WHILE_DOCKED, 68 DreamBackend.EITHER, 69 DreamBackend.NEVER, 70 }; 71 72 private static final int[] RES_IDS = { 73 R.string.screensaver_settings_summary_sleep, 74 R.string.screensaver_settings_summary_dock, 75 R.string.screensaver_settings_summary_either_long, 76 R.string.screensaver_settings_summary_never 77 }; 78 79 private static final int[] RES_IDS_NO_BATTERY = { 80 R.string.screensaver_settings_summary_sleep, 81 R.string.screensaver_settings_summary_dock_and_charging, 82 R.string.screensaver_settings_summary_either_long, 83 R.string.screensaver_settings_summary_never 84 }; 85 86 @Mock 87 private Preference mDreamPickerPref; 88 @Mock 89 private Preference mComplicationsTogglePref; 90 @Mock 91 private DreamPickerController mDreamPickerController; 92 @Captor 93 private ArgumentCaptor<DreamPickerController.Callback> mDreamPickerCallbackCaptor; 94 95 @Test getSettingFromPrefKey()96 public void getSettingFromPrefKey() { 97 for (int i = 0; i < KEYS.size(); i++) { 98 assertThat(DreamSettings.getSettingFromPrefKey(KEYS.get(i))).isEqualTo(SETTINGS[i]); 99 } 100 // Default case 101 assertThat(DreamSettings.getSettingFromPrefKey("garbage value")) 102 .isEqualTo(DreamBackend.NEVER); 103 } 104 105 @Test getKeyFromSetting()106 public void getKeyFromSetting() { 107 for (int i = 0; i < SETTINGS.length; i++) { 108 assertThat(DreamSettings.getKeyFromSetting(SETTINGS[i])).isEqualTo(KEYS.get(i)); 109 } 110 // Default 111 assertThat(DreamSettings.getKeyFromSetting(-1)) 112 .isEqualTo(DreamSettings.NEVER_DREAM); 113 } 114 115 @Test getDreamSettingDescriptionResId()116 public void getDreamSettingDescriptionResId() { 117 for (int i = 0; i < SETTINGS.length; i++) { 118 assertThat(DreamSettings.getDreamSettingDescriptionResId( 119 SETTINGS[i], /* enabledOnBattery= */ false)) 120 .isEqualTo(RES_IDS_NO_BATTERY[i]); 121 assertThat(DreamSettings.getDreamSettingDescriptionResId( 122 SETTINGS[i], /* enabledOnBattery= */ true)) 123 .isEqualTo(RES_IDS[i]); 124 } 125 // Default 126 assertThat(DreamSettings.getDreamSettingDescriptionResId(-1, /* enabledOnBattery= */ false)) 127 .isEqualTo(R.string.screensaver_settings_summary_never); 128 assertThat(DreamSettings.getDreamSettingDescriptionResId(-1, /* enabledOnBattery= */ true)) 129 .isEqualTo(R.string.screensaver_settings_summary_never); 130 } 131 132 @Test summaryText_whenDreamsAreOff()133 public void summaryText_whenDreamsAreOff() { 134 final String fakeSummaryOff = "test dream off"; 135 final DreamBackend mockBackend = mock(DreamBackend.class); 136 final Context mockContext = mock(Context.class); 137 when(mockBackend.isEnabled()).thenReturn(false); 138 when(mockContext.getString(R.string.screensaver_settings_summary_off)).thenReturn( 139 fakeSummaryOff); 140 141 assertThat(DreamSettings.getSummaryTextFromBackend(mockBackend, mockContext)).isEqualTo( 142 fakeSummaryOff); 143 } 144 145 @Test summaryTest_WhenDreamsAreOn()146 public void summaryTest_WhenDreamsAreOn() { 147 final String fakeName = "test_name"; 148 final DreamBackend mockBackend = mock(DreamBackend.class); 149 final Context mockContext = mock(Context.class); 150 when(mockBackend.isEnabled()).thenReturn(true); 151 when(mockBackend.getActiveDreamName()).thenReturn(fakeName); 152 when(mockContext.getString(eq(R.string.screensaver_settings_summary_on), anyString())) 153 .thenAnswer(i -> i.getArgument(1) + " test dream is on"); 154 155 assertThat(DreamSettings.getSummaryTextFromBackend(mockBackend, mockContext)).isEqualTo( 156 fakeName + " test dream is on"); 157 } 158 159 @Test complicationsToggle_addAndRemoveActiveDreamChangeCallback()160 public void complicationsToggle_addAndRemoveActiveDreamChangeCallback() { 161 MockitoAnnotations.initMocks(this); 162 163 final Context context = ApplicationProvider.getApplicationContext(); 164 final DreamSettings dreamSettings = prepareDreamSettings(context); 165 166 dreamSettings.onAttach(context); 167 dreamSettings.onCreate(Bundle.EMPTY); 168 verify(mDreamPickerController).addCallback(mDreamPickerCallbackCaptor.capture()); 169 170 dreamSettings.onDestroy(); 171 verify(mDreamPickerController).removeCallback(mDreamPickerCallbackCaptor.getValue()); 172 } 173 174 @Test complicationsToggle_showWhenDreamSupportsComplications()175 public void complicationsToggle_showWhenDreamSupportsComplications() { 176 MockitoAnnotations.initMocks(this); 177 178 final Context context = ApplicationProvider.getApplicationContext(); 179 final DreamSettings dreamSettings = prepareDreamSettings(context); 180 181 // Active dream supports complications 182 final DreamBackend.DreamInfo activeDream = new DreamBackend.DreamInfo(); 183 activeDream.supportsComplications = true; 184 when(mDreamPickerController.getActiveDreamInfo()).thenReturn(activeDream); 185 186 dreamSettings.onAttach(context); 187 dreamSettings.onCreate(Bundle.EMPTY); 188 189 // Verify dream complications toggle is visible 190 verify(mComplicationsTogglePref).setVisible(true); 191 } 192 193 @Test complicationsToggle_hideWhenDreamDoesNotSupportComplications()194 public void complicationsToggle_hideWhenDreamDoesNotSupportComplications() { 195 MockitoAnnotations.initMocks(this); 196 197 final Context context = ApplicationProvider.getApplicationContext(); 198 final DreamSettings dreamSettings = prepareDreamSettings(context); 199 200 // Active dream does not support complications 201 final DreamBackend.DreamInfo activeDream = new DreamBackend.DreamInfo(); 202 activeDream.supportsComplications = false; 203 when(mDreamPickerController.getActiveDreamInfo()).thenReturn(activeDream); 204 205 dreamSettings.onAttach(context); 206 dreamSettings.onCreate(Bundle.EMPTY); 207 208 // Verify dream complications toggle is hidden 209 verify(mComplicationsTogglePref).setVisible(false); 210 } 211 212 @Test complicationsToggle_showWhenSwitchToDreamSupportsComplications()213 public void complicationsToggle_showWhenSwitchToDreamSupportsComplications() { 214 MockitoAnnotations.initMocks(this); 215 216 final Context context = ApplicationProvider.getApplicationContext(); 217 final DreamSettings dreamSettings = prepareDreamSettings(context); 218 219 // Active dream does not support complications 220 final DreamBackend.DreamInfo activeDream = new DreamBackend.DreamInfo(); 221 activeDream.supportsComplications = false; 222 when(mDreamPickerController.getActiveDreamInfo()).thenReturn(activeDream); 223 224 dreamSettings.onAttach(context); 225 dreamSettings.onCreate(Bundle.EMPTY); 226 227 // Verify dream complications toggle is hidden 228 verify(mComplicationsTogglePref).setVisible(false); 229 verify(mDreamPickerController).addCallback(mDreamPickerCallbackCaptor.capture()); 230 231 // Active dream changes to one that supports complications 232 activeDream.supportsComplications = true; 233 mDreamPickerCallbackCaptor.getValue().onActiveDreamChanged(); 234 235 // Verify dream complications toggle is shown 236 verify(mComplicationsTogglePref).setVisible(true); 237 } 238 prepareDreamSettings(Context context)239 private DreamSettings prepareDreamSettings(Context context) { 240 final TestDreamSettings dreamSettings = new TestDreamSettings(context); 241 when(mDreamPickerController.getPreferenceKey()).thenReturn(DreamPickerController.PREF_KEY); 242 when(mDreamPickerPref.getExtras()).thenReturn(new Bundle()); 243 when(mDreamPickerPref.getKey()).thenReturn(DreamPickerController.PREF_KEY); 244 when(mComplicationsTogglePref.getKey()).thenReturn( 245 DreamComplicationPreferenceController.PREF_KEY); 246 247 dreamSettings.addPreference(DreamPickerController.PREF_KEY, mDreamPickerPref); 248 dreamSettings.addPreference(DreamComplicationPreferenceController.PREF_KEY, 249 mComplicationsTogglePref); 250 dreamSettings.setDreamPickerController(mDreamPickerController); 251 252 return dreamSettings; 253 } 254 255 private static class TestDreamSettings extends DreamSettings { 256 257 private final Context mContext; 258 private final PreferenceManager mPreferenceManager; 259 260 private final HashMap<String, Preference> mPreferences = new HashMap<>(); 261 TestDreamSettings(Context context)262 TestDreamSettings(Context context) { 263 super(); 264 mContext = context; 265 mPreferenceManager = new PreferenceManager(context); 266 mPreferenceManager.setPreferences(mPreferenceManager.createPreferenceScreen(context)); 267 } 268 269 @Override getPreferenceScreenResId()270 public int getPreferenceScreenResId() { 271 return R.xml.placeholder_prefs; 272 } 273 274 @Override getPreferenceScreen()275 public PreferenceScreen getPreferenceScreen() { 276 return mPreferenceManager.getPreferenceScreen(); 277 } 278 279 @Override getPreferenceManager()280 public PreferenceManager getPreferenceManager() { 281 return mPreferenceManager; 282 } 283 284 @Override getContext()285 public Context getContext() { 286 return mContext; 287 } 288 289 @Override findPreference(CharSequence key)290 public <T extends Preference> T findPreference(CharSequence key) { 291 return (T) mPreferences.get(key); 292 } 293 addPreference(String key, Preference preference)294 void addPreference(String key, Preference preference) { 295 mPreferences.put(key, preference); 296 } 297 } 298 } 299