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