• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.homepage.contextualcards.slices;
18 
19 import static android.content.res.Configuration.UI_MODE_NIGHT_NO;
20 import static android.content.res.Configuration.UI_MODE_NIGHT_YES;
21 
22 import static com.google.common.truth.Truth.assertThat;
23 
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27 
28 import android.app.UiModeManager;
29 import android.content.Context;
30 import android.net.Uri;
31 import android.os.BatteryManager;
32 import android.os.PowerManager;
33 
34 import androidx.slice.Slice;
35 import androidx.slice.SliceMetadata;
36 import androidx.slice.SliceProvider;
37 import androidx.slice.widget.SliceLiveData;
38 
39 import com.android.settings.R;
40 import com.android.settings.slices.CustomSliceRegistry;
41 import com.android.settings.slices.SlicesFeatureProviderImpl;
42 import com.android.settings.testutils.FakeFeatureFactory;
43 
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.Mock;
48 import org.mockito.MockitoAnnotations;
49 import org.robolectric.RobolectricTestRunner;
50 import org.robolectric.RuntimeEnvironment;
51 
52 @RunWith(RobolectricTestRunner.class)
53 public class DarkThemeSliceTest {
54     @Mock
55     private BatteryManager mBatteryManager;
56     @Mock
57     private PowerManager mPowerManager;
58     @Mock
59     private UiModeManager mUiModeManager;
60 
61     private Context mContext;
62     private DarkThemeSlice mDarkThemeSlice;
63     private FakeFeatureFactory mFeatureFactory;
64 
65     @Before
setUp()66     public void setUp() {
67         MockitoAnnotations.initMocks(this);
68         mContext = spy(RuntimeEnvironment.application);
69         mFeatureFactory = FakeFeatureFactory.setupForTest();
70         mFeatureFactory.slicesFeatureProvider = new SlicesFeatureProviderImpl();
71         mFeatureFactory.slicesFeatureProvider.newUiSession();
72         doReturn(mPowerManager).when(mContext).getSystemService(PowerManager.class);
73         when(mPowerManager.isPowerSaveMode()).thenReturn(false);
74         doReturn(mUiModeManager).when(mContext).getSystemService(UiModeManager.class);
75 
76         // Set-up specs for SliceMetadata.
77         SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
78         mDarkThemeSlice = new DarkThemeSlice(mContext);
79         mDarkThemeSlice.sKeepSliceShow = false;
80     }
81 
82     @Test
getUri_shouldBeDarkThemeSliceUri()83     public void getUri_shouldBeDarkThemeSliceUri() {
84         final Uri uri = mDarkThemeSlice.getUri();
85 
86         assertThat(uri).isEqualTo(CustomSliceRegistry.DARK_THEME_SLICE_URI);
87     }
88 
89     @Test
isAvailable_inDarkThemeMode_returnFalse()90     public void isAvailable_inDarkThemeMode_returnFalse() {
91         mContext.getResources().getConfiguration().uiMode = UI_MODE_NIGHT_YES;
92 
93         assertThat(mDarkThemeSlice.isAvailable(mContext)).isFalse();
94     }
95 
96     @Test
isAvailable_nonDarkTheme_autoNightMode_returnFalse()97     public void isAvailable_nonDarkTheme_autoNightMode_returnFalse() {
98         mContext.getResources().getConfiguration().uiMode = UI_MODE_NIGHT_NO;
99         setNightMode(UiModeManager.MODE_NIGHT_AUTO);
100 
101         assertThat(mDarkThemeSlice.isAvailable(mContext)).isFalse();
102     }
103 
104     @Test
isAvailable_nonDarkTheme_customNightMode_returnFalse()105     public void isAvailable_nonDarkTheme_customNightMode_returnFalse() {
106         mContext.getResources().getConfiguration().uiMode = UI_MODE_NIGHT_NO;
107         setNightMode(UiModeManager.MODE_NIGHT_CUSTOM);
108 
109         assertThat(mDarkThemeSlice.isAvailable(mContext)).isFalse();
110     }
111 
112     @Test
isAvailable_nonDarkThemeBatteryCapacityEq100_returnFalse()113     public void isAvailable_nonDarkThemeBatteryCapacityEq100_returnFalse() {
114         setBatteryCapacityLevel(100);
115         setNightMode(UiModeManager.MODE_NIGHT_NO);
116 
117         assertThat(mDarkThemeSlice.isAvailable(mContext)).isFalse();
118     }
119 
120     @Test
isAvailable_nonDarkThemeBatteryCapacityLt50_returnTrue()121     public void isAvailable_nonDarkThemeBatteryCapacityLt50_returnTrue() {
122         setBatteryCapacityLevel(40);
123         setNightMode(UiModeManager.MODE_NIGHT_NO);
124 
125         assertThat(mDarkThemeSlice.isAvailable(mContext)).isTrue();
126     }
127 
128     @Test
getSlice_batterySaver_returnErrorSlice()129     public void getSlice_batterySaver_returnErrorSlice() {
130         when(mPowerManager.isPowerSaveMode()).thenReturn(true);
131 
132         final Slice mediaSlice = mDarkThemeSlice.getSlice();
133         final SliceMetadata metadata = SliceMetadata.from(mContext, mediaSlice);
134         assertThat(metadata.isErrorSlice()).isTrue();
135     }
136 
137     @Test
getSlice_notAvailable_returnErrorSlice()138     public void getSlice_notAvailable_returnErrorSlice() {
139         mContext.getResources().getConfiguration().uiMode = UI_MODE_NIGHT_YES;
140 
141         final Slice mediaSlice = mDarkThemeSlice.getSlice();
142         final SliceMetadata metadata = SliceMetadata.from(mContext, mediaSlice);
143         assertThat(metadata.isErrorSlice()).isTrue();
144     }
145 
146     @Test
getSlice_newSession_notAvailable_returnErrorSlice()147     public void getSlice_newSession_notAvailable_returnErrorSlice() {
148         // previous displayed: yes
149         mDarkThemeSlice.sKeepSliceShow = true;
150         // Session: use original value + 1 to become a new session
151         mDarkThemeSlice.sActiveUiSession =
152                 mFeatureFactory.slicesFeatureProvider.getUiSessionToken() + 1;
153 
154         mContext.getResources().getConfiguration().uiMode = UI_MODE_NIGHT_YES;
155 
156         final Slice mediaSlice = mDarkThemeSlice.getSlice();
157         final SliceMetadata metadata = SliceMetadata.from(mContext, mediaSlice);
158         assertThat(metadata.isErrorSlice()).isTrue();
159     }
160 
161     @Test
getSlice_previouslyDisplayed_isAvailable_returnSlice()162     public void getSlice_previouslyDisplayed_isAvailable_returnSlice() {
163         mDarkThemeSlice.sActiveUiSession =
164                 mFeatureFactory.slicesFeatureProvider.getUiSessionToken();
165         mDarkThemeSlice.sKeepSliceShow = true;
166         setBatteryCapacityLevel(40);
167         setNightMode(UiModeManager.MODE_NIGHT_NO);
168 
169         assertThat(mDarkThemeSlice.getSlice()).isNotNull();
170     }
171 
172     @Test
getSlice_sliceNotClicked_notAvailable_returnErrorSlice()173     public void getSlice_sliceNotClicked_notAvailable_returnErrorSlice() {
174         mDarkThemeSlice.sSliceClicked = false;
175 
176         mContext.getResources().getConfiguration().uiMode = UI_MODE_NIGHT_YES;
177 
178         final Slice mediaSlice = mDarkThemeSlice.getSlice();
179         final SliceMetadata metadata = SliceMetadata.from(mContext, mediaSlice);
180         assertThat(metadata.isErrorSlice()).isTrue();
181     }
182 
183     @Test
getSlice_sliceClicked_isAvailable_returnSlice()184     public void getSlice_sliceClicked_isAvailable_returnSlice() {
185         mDarkThemeSlice.sSliceClicked = true;
186 
187         setBatteryCapacityLevel(40);
188         setNightMode(UiModeManager.MODE_NIGHT_NO);
189 
190         assertThat(mDarkThemeSlice.getSlice()).isNotNull();
191     }
192 
193     @Test
getSlice_isAvailable_returnSlice()194     public void getSlice_isAvailable_returnSlice() {
195         setBatteryCapacityLevel(40);
196         setNightMode(UiModeManager.MODE_NIGHT_NO);
197 
198         assertThat(mDarkThemeSlice.getSlice()).isNotNull();
199     }
200 
201     @Test
getSlice_isAvailable_showTitleSubtitle()202     public void getSlice_isAvailable_showTitleSubtitle() {
203         setBatteryCapacityLevel(40);
204         setNightMode(UiModeManager.MODE_NIGHT_NO);
205 
206         final Slice slice = mDarkThemeSlice.getSlice();
207         final SliceMetadata metadata = SliceMetadata.from(mContext, slice);
208         assertThat(metadata.getTitle()).isEqualTo(
209                 mContext.getString(R.string.dark_theme_slice_title));
210         assertThat(metadata.getSubtitle()).isEqualTo(
211                 mContext.getString(R.string.dark_theme_slice_subtitle));
212     }
213 
setBatteryCapacityLevel(int power_level)214     private void setBatteryCapacityLevel(int power_level) {
215         mContext.getResources().getConfiguration().uiMode = UI_MODE_NIGHT_NO;
216         doReturn(mBatteryManager).when(mContext).getSystemService(BatteryManager.class);
217         when(mBatteryManager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY))
218                 .thenReturn(power_level);
219     }
220 
setNightMode(int mode)221     private void setNightMode(int mode) {
222         when(mUiModeManager.getNightMode()).thenReturn(mode);
223     }
224 }
225