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