OSDN Git Service

Tint battery fix icon
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / homepage / contextualcards / slices / BatteryFixSliceTest.java
1 /*
2  * Copyright (C) 2018 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 com.google.common.truth.Truth.assertThat;
20
21 import static org.mockito.Mockito.mock;
22 import static org.mockito.Mockito.when;
23
24 import android.content.Context;
25 import android.net.Uri;
26
27 import androidx.slice.Slice;
28 import androidx.slice.SliceMetadata;
29 import androidx.slice.SliceProvider;
30 import androidx.slice.widget.SliceLiveData;
31
32 import com.android.internal.os.BatteryStatsHelper;
33 import com.android.settings.R;
34 import com.android.settings.fuelgauge.BatteryStatsHelperLoader;
35 import com.android.settings.fuelgauge.batterytip.BatteryTipLoader;
36 import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
37 import com.android.settings.fuelgauge.batterytip.tips.EarlyWarningTip;
38 import com.android.settings.fuelgauge.batterytip.tips.LowBatteryTip;
39 import com.android.settings.slices.SliceBackgroundWorker;
40
41 import org.junit.After;
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.MockitoAnnotations;
46 import org.robolectric.RobolectricTestRunner;
47 import org.robolectric.RuntimeEnvironment;
48 import org.robolectric.annotation.Config;
49 import org.robolectric.annotation.Implementation;
50 import org.robolectric.annotation.Implements;
51 import org.robolectric.annotation.Resetter;
52
53 import java.util.ArrayList;
54 import java.util.List;
55
56 @RunWith(RobolectricTestRunner.class)
57 @Config(shadows = {
58         BatteryFixSliceTest.ShadowBatteryStatsHelperLoader.class,
59         BatteryFixSliceTest.ShadowBatteryTipLoader.class
60 })
61 public class BatteryFixSliceTest {
62
63     private Context mContext;
64     private BatteryFixSlice mSlice;
65
66     @Before
67     public void setUp() {
68         MockitoAnnotations.initMocks(this);
69         mContext = RuntimeEnvironment.application;
70         mSlice = new BatteryFixSlice(mContext);
71
72         // Set-up specs for SliceMetadata.
73         SliceProvider.setSpecs(SliceLiveData.SUPPORTED_SPECS);
74     }
75
76     @After
77     public void tearDown() {
78         ShadowBatteryTipLoader.reset();
79         ShadowSliceBackgroundWorker.reset();
80         ShadowEarlyWarningTip.reset();
81     }
82
83     @Test
84     public void updateBatteryTipAvailabilityCache_hasImportantTip_shouldReturnTrue() {
85         final List<BatteryTip> tips = new ArrayList<>();
86         tips.add(new LowBatteryTip(BatteryTip.StateType.INVISIBLE, false, ""));
87         tips.add(new EarlyWarningTip(BatteryTip.StateType.NEW, false));
88         ShadowBatteryTipLoader.setBatteryTips(tips);
89
90         BatteryFixSlice.updateBatteryTipAvailabilityCache(mContext);
91
92         assertThat(BatteryFixSlice.isBatteryTipAvailableFromCache(mContext)).isTrue();
93     }
94
95     @Test
96     public void getSlice_unimportantSlice_shouldSkip() {
97         final List<BatteryTip> tips = new ArrayList<>();
98         tips.add(new LowBatteryTip(BatteryTip.StateType.INVISIBLE, false, ""));
99         tips.add(new EarlyWarningTip(BatteryTip.StateType.HANDLED, false));
100         ShadowBatteryTipLoader.setBatteryTips(tips);
101
102         BatteryFixSlice.updateBatteryTipAvailabilityCache(mContext);
103         final Slice slice = mSlice.getSlice();
104
105         assertThat(SliceMetadata.from(mContext, slice).isErrorSlice()).isTrue();
106     }
107
108     @Test
109     @Config(shadows = {
110             BatteryFixSliceTest.ShadowEarlyWarningTip.class,
111             BatteryFixSliceTest.ShadowSliceBackgroundWorker.class
112     })
113     public void getSlice_hasImportantTip_shouldTintIcon() {
114         final List<BatteryTip> tips = new ArrayList<>();
115         tips.add(new EarlyWarningTip(BatteryTip.StateType.NEW, false));
116         // Create fake cache data
117         ShadowBatteryTipLoader.setBatteryTips(tips);
118         BatteryFixSlice.updateBatteryTipAvailabilityCache(mContext);
119         // Create fake background worker data
120         BatteryFixSlice.BatteryTipWorker batteryTipWorker = mock(
121                 BatteryFixSlice.BatteryTipWorker.class);
122         when(batteryTipWorker.getResults()).thenReturn(tips);
123         ShadowSliceBackgroundWorker.setBatteryTipWorkerWorker(batteryTipWorker);
124
125         final Slice slice = mSlice.getSlice();
126
127         assertThat(ShadowEarlyWarningTip.isIconTintColorIdCalled()).isTrue();
128     }
129
130     @Implements(BatteryStatsHelperLoader.class)
131     public static class ShadowBatteryStatsHelperLoader {
132
133         @Implementation
134         protected BatteryStatsHelper loadInBackground() {
135             return null;
136         }
137     }
138
139     @Implements(BatteryTipLoader.class)
140     public static class ShadowBatteryTipLoader {
141
142         private static List<BatteryTip> sBatteryTips = new ArrayList<>();
143
144         @Resetter
145         public static void reset() {
146             sBatteryTips = new ArrayList<>();
147         }
148
149         @Implementation
150         protected List<BatteryTip> loadInBackground() {
151             return sBatteryTips;
152         }
153
154         private static void setBatteryTips(List<BatteryTip> tips) {
155             sBatteryTips = tips;
156         }
157     }
158
159     @Implements(SliceBackgroundWorker.class)
160     public static class ShadowSliceBackgroundWorker {
161
162         private static BatteryFixSlice.BatteryTipWorker sBatteryTipWorkerWorker;
163
164         @Resetter
165         public static void reset() {
166             sBatteryTipWorkerWorker = null;
167         }
168
169         @Implementation
170         protected static <T extends SliceBackgroundWorker> T getInstance(Uri uri) {
171             return (T) sBatteryTipWorkerWorker;
172         }
173
174         public static void setBatteryTipWorkerWorker(BatteryFixSlice.BatteryTipWorker worker) {
175             sBatteryTipWorkerWorker = worker;
176         }
177     }
178
179     @Implements(EarlyWarningTip.class)
180     public static class ShadowEarlyWarningTip {
181
182         private static boolean mIsGetIconTintColorIdCalled;
183
184         @Resetter
185         public static void reset() {
186             mIsGetIconTintColorIdCalled = false;
187         }
188
189         @Implementation
190         protected int getIconTintColorId() {
191             mIsGetIconTintColorIdCalled = true;
192             return R.color.battery_bad_color_light;
193         }
194
195         public static boolean isIconTintColorIdCalled() {
196             return mIsGetIconTintColorIdCalled;
197         }
198     }
199 }