Files
app_Settings/tests/robotests/src/com/android/settings/dream/WhenToDreamPickerTest.java
Lucas Silva 0abc02c446 Fix WhenToDreamPickerTest and re-enable.
The test was previously failing because it was passing a mock context
which didn't support all the operations of a real context.

Bug: 216036457
Test: make -j64 RunSettingsRoboTests ROBOTEST_FILTER="com.android.settings.dream.WhenToDreamPickerTest"
Change-Id: I2c769658dc687ef26d184b7ce9065c18ab8b9b8d
2022-03-08 12:57:18 -05:00

112 lines
3.8 KiB
Java

/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.dream;
import static com.google.common.truth.Truth.assertThat;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.os.UserManager;
import androidx.test.core.app.ApplicationProvider;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settingslib.dream.DreamBackend;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.util.ReflectionHelpers;
@RunWith(RobolectricTestRunner.class)
public class WhenToDreamPickerTest {
private WhenToDreamPicker mPicker;
@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private DreamBackend mBackend;
@Mock
private UserManager mUserManager;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
final Context context = spy(ApplicationProvider.getApplicationContext());
when(context.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager);
FakeFeatureFactory.setupForTest();
mPicker = new WhenToDreamPicker();
mPicker.onAttach(context);
ReflectionHelpers.setField(mPicker, "mBackend", mBackend);
}
@Test
public void getDefaultKeyReturnsCurrentWhenToDreamSetting() {
when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.WHILE_CHARGING);
assertThat(mPicker.getDefaultKey())
.isEqualTo(DreamSettings.getKeyFromSetting(DreamBackend.WHILE_CHARGING));
when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.WHILE_DOCKED);
assertThat(mPicker.getDefaultKey())
.isEqualTo(DreamSettings.getKeyFromSetting(DreamBackend.WHILE_DOCKED));
when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.EITHER);
assertThat(mPicker.getDefaultKey())
.isEqualTo(DreamSettings.getKeyFromSetting(DreamBackend.EITHER));
when(mBackend.getWhenToDreamSetting()).thenReturn(DreamBackend.NEVER);
assertThat(mPicker.getDefaultKey())
.isEqualTo(DreamSettings.getKeyFromSetting(DreamBackend.NEVER));
}
@Test
public void setDreamWhileCharging() {
final String key = DreamSettings.getKeyFromSetting(DreamBackend.WHILE_CHARGING);
mPicker.setDefaultKey(key);
verify(mBackend).setWhenToDream(DreamBackend.WHILE_CHARGING);
}
@Test
public void setDreamWhileDocked() {
final String key = DreamSettings.getKeyFromSetting(DreamBackend.WHILE_DOCKED);
mPicker.setDefaultKey(key);
verify(mBackend).setWhenToDream(DreamBackend.WHILE_DOCKED);
}
@Test
public void setDreamWhileChargingOrDocked() {
final String key = DreamSettings.getKeyFromSetting(DreamBackend.EITHER);
mPicker.setDefaultKey(key);
verify(mBackend).setWhenToDream(DreamBackend.EITHER);
}
@Test
public void setDreamNever() {
final String key = DreamSettings.getKeyFromSetting(DreamBackend.NEVER);
mPicker.setDefaultKey(key);
verify(mBackend).setWhenToDream(DreamBackend.NEVER);
}
}