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.ComponentName; 21 import android.content.Context; 22 import android.os.UserManager; 23 import com.android.settings.testutils.SettingsRobolectricTestRunner; 24 import com.android.settings.TestConfig; 25 import com.android.settings.testutils.FakeFeatureFactory; 26 import com.android.settingslib.dream.DreamBackend; 27 import com.android.settingslib.dream.DreamBackend.DreamInfo; 28 import java.util.ArrayList; 29 import java.util.Arrays; 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.mockito.Answers; 34 import org.mockito.Mock; 35 import org.mockito.MockitoAnnotations; 36 import org.robolectric.annotation.Config; 37 import org.robolectric.util.ReflectionHelpers; 38 39 import static com.google.common.truth.Truth.assertThat; 40 import static org.mockito.Mockito.mock; 41 import static org.mockito.Mockito.verify; 42 import static org.mockito.Mockito.when; 43 44 @RunWith(SettingsRobolectricTestRunner.class) 45 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 46 public class CurrentDreamPickerTest { 47 private static String COMPONENT_KEY = "mocked_component_name_string"; 48 49 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 50 private DreamBackend mBackend; 51 @Mock(answer = Answers.RETURNS_DEEP_STUBS) 52 private Activity mActivity; 53 @Mock 54 private UserManager mUserManager; 55 private CurrentDreamPicker mPicker; 56 57 @Before setup()58 public void setup() { 59 MockitoAnnotations.initMocks(this); 60 when(mActivity.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 61 FakeFeatureFactory.setupForTest(mActivity); 62 63 mPicker = new CurrentDreamPicker(); 64 mPicker.onAttach((Context)mActivity); 65 66 ReflectionHelpers.setField(mPicker, "mBackend", mBackend); 67 } 68 69 @Test getDefaultShouldReturnActiveDream()70 public void getDefaultShouldReturnActiveDream() { 71 ComponentName mockComponentName = mock(ComponentName.class); 72 when(mockComponentName.flattenToString()).thenReturn(COMPONENT_KEY); 73 when(mBackend.getActiveDream()).thenReturn(mockComponentName); 74 75 assertThat(mPicker.getDefaultKey()).isEqualTo(COMPONENT_KEY); 76 } 77 78 @Test setDefaultShouldUpdateActiveDream()79 public void setDefaultShouldUpdateActiveDream() { 80 DreamInfo mockInfo = mock(DreamInfo.class); 81 ComponentName mockName = mock(ComponentName.class); 82 83 mockInfo.componentName = mockName; 84 when(mockName.flattenToString()).thenReturn(COMPONENT_KEY); 85 when(mBackend.getDreamInfos()).thenReturn(new ArrayList<>(Arrays.asList(mockInfo))); 86 87 mPicker.setDefaultKey(COMPONENT_KEY); 88 89 verify(mBackend).setActiveDream(mockName); 90 } 91 } 92