• 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.content.ComponentName;
25 import android.content.Context;
26 
27 import com.android.settings.testutils.SettingsRobolectricTestRunner;
28 import com.android.settings.widget.GearPreference;
29 import com.android.settingslib.dream.DreamBackend;
30 import com.android.settingslib.dream.DreamBackend.DreamInfo;
31 
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Answers;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.Mock;
38 import org.mockito.MockitoAnnotations;
39 import org.robolectric.util.ReflectionHelpers;
40 
41 import java.util.ArrayList;
42 import java.util.Collections;
43 
44 @RunWith(SettingsRobolectricTestRunner.class)
45 public class CurrentDreamPreferenceControllerTest {
46 
47     private CurrentDreamPreferenceController mController;
48     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
49     private DreamBackend mBackend;
50     @Mock(answer = Answers.RETURNS_DEEP_STUBS)
51     private Context mContext;
52     @Mock
53     private DreamInfo mDreamInfo;
54 
55     @Before
setup()56     public void setup() {
57         MockitoAnnotations.initMocks(this);
58 
59         mController = new CurrentDreamPreferenceController(mContext);
60         ReflectionHelpers.setField(mController, "mBackend", mBackend);
61     }
62 
63     @Test
isDisabledIfNoDreamsAvailable()64     public void isDisabledIfNoDreamsAvailable() {
65         when(mBackend.getDreamInfos()).thenReturn(new ArrayList<>(0));
66 
67         assertThat(mController.isAvailable()).isFalse();
68     }
69 
70     @Test
isEnabledIfDreamsAvailable()71     public void isEnabledIfDreamsAvailable() {
72         when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mDreamInfo));
73 
74         assertThat(mController.isAvailable()).isTrue();
75     }
76 
77     @Test
gearShowsIfActiveDreamInfoHasOptions()78     public void gearShowsIfActiveDreamInfoHasOptions() {
79         mDreamInfo.settingsComponentName = mock(ComponentName.class);
80         mDreamInfo.isActive = true;
81 
82         when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mDreamInfo));
83 
84         GearPreference mockPref = mock(GearPreference.class);
85         ArgumentCaptor<GearPreference.OnGearClickListener> captor =
86                 ArgumentCaptor.forClass(GearPreference.OnGearClickListener.class);
87 
88         // verify that updateState sets a non-null gear click listener
89         mController.updateState(mockPref);
90         verify(mockPref).setOnGearClickListener(captor.capture());
91         captor.getAllValues().forEach(listener -> assertThat(listener).isNotNull());
92     }
93 
94     @Test
gearHidesIfActiveDreamInfoHasNoOptions()95     public void gearHidesIfActiveDreamInfoHasNoOptions() {
96         mDreamInfo.settingsComponentName = null;
97         mDreamInfo.isActive = true;
98 
99         when(mBackend.getDreamInfos()).thenReturn(Collections.singletonList(mDreamInfo));
100 
101         GearPreference mockPref = mock(GearPreference.class);
102         ArgumentCaptor<GearPreference.OnGearClickListener> captor =
103                 ArgumentCaptor.forClass(GearPreference.OnGearClickListener.class);
104 
105         // setting a null onGearClickListener removes the gear from view
106         mController.updateState(mockPref);
107         verify(mockPref).setOnGearClickListener(captor.capture());
108         captor.getAllValues().forEach(listener -> assertThat(listener).isNull());
109     }
110 }
111