• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 import static org.mockito.Mockito.when;
23 
24 import android.app.Activity;
25 import android.content.ComponentName;
26 import android.content.Context;
27 import android.os.UserManager;
28 
29 import com.android.settings.testutils.FakeFeatureFactory;
30 import com.android.settings.testutils.SettingsRobolectricTestRunner;
31 import com.android.settingslib.dream.DreamBackend;
32 import com.android.settingslib.dream.DreamBackend.DreamInfo;
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.util.ReflectionHelpers;
41 
42 import java.util.Collections;
43 
44 @RunWith(SettingsRobolectricTestRunner.class)
45 public class CurrentDreamPickerTest {
46 
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();
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(Collections.singletonList(mockInfo));
86 
87         mPicker.setDefaultKey(COMPONENT_KEY);
88 
89         verify(mBackend).setActiveDream(mockName);
90     }
91 }
92