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.spy; 22 import static org.mockito.Mockito.verify; 23 import static org.mockito.Mockito.when; 24 25 import android.content.Context; 26 import android.os.UserManager; 27 28 import androidx.test.core.app.ApplicationProvider; 29 30 import com.android.settings.testutils.FakeFeatureFactory; 31 import com.android.settings.testutils.shadow.SettingsShadowResources; 32 import com.android.settingslib.dream.DreamBackend; 33 34 import org.junit.Before; 35 import org.junit.Test; 36 import org.junit.runner.RunWith; 37 import org.mockito.Answers; 38 import org.mockito.Mock; 39 import org.mockito.MockitoAnnotations; 40 import org.robolectric.RobolectricTestRunner; 41 import org.robolectric.annotation.Config; 42 import org.robolectric.util.ReflectionHelpers; 43 44 @RunWith(RobolectricTestRunner.class) 45 @Config(shadows = SettingsShadowResources.class) 46 public class WhenToDreamPickerTest { 47 48 private WhenToDreamPicker mPicker; 49 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 50 private DreamBackend mBackend; 51 @Mock 52 private UserManager mUserManager; 53 54 @Before setup()55 public void setup() { 56 MockitoAnnotations.initMocks(this); 57 final Context context = spy(ApplicationProvider.getApplicationContext()); 58 59 SettingsShadowResources.overrideResource( 60 com.android.internal.R.bool.config_dreamsEnabledOnBattery, 61 true); 62 63 when(context.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 64 FakeFeatureFactory.setupForTest(); 65 66 mPicker = spy(new WhenToDreamPicker()); 67 when(mPicker.getContext()).thenReturn(context); 68 mPicker.onAttach(context); 69 70 ReflectionHelpers.setField(mPicker, "mBackend", mBackend); 71 } 72 73 @Test getDefaultKeyReturnsCurrentWhenToDreamSetting()74 public void getDefaultKeyReturnsCurrentWhenToDreamSetting() { 75 when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.WHILE_CHARGING); 76 assertThat(mPicker.getDefaultKey()) 77 .isEqualTo(DreamSettings.getKeyFromSetting(DreamBackend.WHILE_CHARGING)); 78 79 when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.WHILE_DOCKED); 80 assertThat(mPicker.getDefaultKey()) 81 .isEqualTo(DreamSettings.getKeyFromSetting(DreamBackend.WHILE_DOCKED)); 82 83 when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.EITHER); 84 assertThat(mPicker.getDefaultKey()) 85 .isEqualTo(DreamSettings.getKeyFromSetting(DreamBackend.EITHER)); 86 87 when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.NEVER); 88 assertThat(mPicker.getDefaultKey()) 89 .isEqualTo(DreamSettings.getKeyFromSetting(DreamBackend.NEVER)); 90 } 91 92 @Test setDreamWhileCharging()93 public void setDreamWhileCharging() { 94 final String key = DreamSettings.getKeyFromSetting(DreamBackend.WHILE_CHARGING); 95 mPicker.setDefaultKey(key); 96 verify(mBackend).setWhenToDream(DreamBackend.WHILE_CHARGING); 97 } 98 99 @Test setDreamWhileDocked()100 public void setDreamWhileDocked() { 101 final String key = DreamSettings.getKeyFromSetting(DreamBackend.WHILE_DOCKED); 102 mPicker.setDefaultKey(key); 103 verify(mBackend).setWhenToDream(DreamBackend.WHILE_DOCKED); 104 } 105 106 @Test setDreamWhileChargingOrDocked()107 public void setDreamWhileChargingOrDocked() { 108 final String key = DreamSettings.getKeyFromSetting(DreamBackend.EITHER); 109 mPicker.setDefaultKey(key); 110 verify(mBackend).setWhenToDream(DreamBackend.EITHER); 111 } 112 113 @Test setDreamNever()114 public void setDreamNever() { 115 final String key = DreamSettings.getKeyFromSetting(DreamBackend.NEVER); 116 mPicker.setDefaultKey(key); 117 verify(mBackend).setWhenToDream(DreamBackend.NEVER); 118 } 119 } 120