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