OSDN Git Service

e07df0991c8f732261f8b9a8a8efbb06bb9ab846
[android-x86/packages-apps-Settings.git] / tests / robotests / src / com / android / settings / fuelgauge / batterytip / BatteryTipDialogFragmentTest.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.fuelgauge.batterytip;
18
19 import static com.google.common.truth.Truth.assertThat;
20
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 import static org.robolectric.Shadows.shadowOf;
24
25 import android.app.AlertDialog;
26 import android.content.Context;
27 import android.text.format.DateUtils;
28
29 import com.android.settings.R;
30 import com.android.settings.fuelgauge.Estimate;
31 import com.android.settings.fuelgauge.batterytip.tips.BatteryTip;
32 import com.android.settings.fuelgauge.batterytip.tips.HighUsageTip;
33 import com.android.settings.fuelgauge.batterytip.tips.RestrictAppTip;
34 import com.android.settings.fuelgauge.batterytip.tips.SummaryTip;
35 import com.android.settings.fuelgauge.batterytip.tips.UnrestrictAppTip;
36 import com.android.settings.testutils.FakeFeatureFactory;
37 import com.android.settings.testutils.SettingsRobolectricTestRunner;
38 import com.android.settings.testutils.shadow.ShadowUtils;
39
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.MockitoAnnotations;
44 import org.robolectric.Robolectric;
45 import org.robolectric.RuntimeEnvironment;
46 import org.robolectric.annotation.Config;
47 import org.robolectric.shadows.ShadowAlertDialog;
48 import org.robolectric.shadows.ShadowDialog;
49 import org.robolectric.util.FragmentTestUtil;
50
51 import java.util.ArrayList;
52 import java.util.List;
53
54 @RunWith(SettingsRobolectricTestRunner.class)
55 @Config(shadows = ShadowUtils.class)
56 public class BatteryTipDialogFragmentTest {
57
58     private static final String PACKAGE_NAME = "com.android.app";
59     private static final String DISPLAY_NAME = "app";
60     private static final long SCREEN_TIME_MS = DateUtils.HOUR_IN_MILLIS;
61     private static final long AVERAGE_TIME_MS = DateUtils.HOUR_IN_MILLIS;
62     private static final int METRICS_KEY = 1;
63
64     private BatteryTipDialogFragment mDialogFragment;
65     private Context mContext;
66     private HighUsageTip mHighUsageTip;
67     private RestrictAppTip mRestrictedOneAppTip;
68     private RestrictAppTip mRestrictTwoAppsTip;
69     private UnrestrictAppTip mUnrestrictAppTip;
70     private SummaryTip mSummaryTip;
71     private AppInfo mAppInfo;
72
73     @Before
74     public void setUp() {
75         MockitoAnnotations.initMocks(this);
76
77         mContext = spy(RuntimeEnvironment.application);
78         FakeFeatureFactory.setupForTest();
79         ShadowUtils.setApplicationLabel(PACKAGE_NAME, DISPLAY_NAME);
80
81         List<AppInfo> highUsageTips = new ArrayList<>();
82         mAppInfo = new AppInfo.Builder()
83                 .setScreenOnTimeMs(SCREEN_TIME_MS)
84                 .setPackageName(PACKAGE_NAME)
85                 .build();
86         highUsageTips.add(mAppInfo);
87         mHighUsageTip = new HighUsageTip(SCREEN_TIME_MS, highUsageTips);
88
89         final List<AppInfo> restrictApps = new ArrayList<>();
90         restrictApps.add(mAppInfo);
91         mRestrictedOneAppTip = new RestrictAppTip(BatteryTip.StateType.NEW,
92                 new ArrayList<>(restrictApps));
93         restrictApps.add(mAppInfo);
94         mRestrictTwoAppsTip = new RestrictAppTip(BatteryTip.StateType.NEW,
95                 new ArrayList<>(restrictApps));
96
97         mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, mAppInfo);
98         mSummaryTip = spy(new SummaryTip(BatteryTip.StateType.NEW,
99                 Estimate.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN));
100     }
101
102     @Test
103     public void testOnCreateDialog_highUsageTip_fireHighUsageDialog() {
104         Robolectric.getForegroundThreadScheduler().pause();
105
106         mDialogFragment = BatteryTipDialogFragment.newInstance(mHighUsageTip, METRICS_KEY);
107
108         FragmentTestUtil.startFragment(mDialogFragment);
109
110         Robolectric.getForegroundThreadScheduler().advanceToLastPostedRunnable();
111
112         final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
113         ShadowAlertDialog shadowDialog = shadowOf(dialog);
114
115         assertThat(shadowDialog.getMessage())
116                 .isEqualTo(mContext.getString(R.string.battery_tip_dialog_message, 1));
117     }
118
119     @Test
120     public void testOnCreateDialog_restrictOneAppTip_fireRestrictOneAppDialog() {
121         mDialogFragment = BatteryTipDialogFragment.newInstance(mRestrictedOneAppTip, METRICS_KEY);
122
123         FragmentTestUtil.startFragment(mDialogFragment);
124
125         final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
126         ShadowAlertDialog shadowDialog = shadowOf(dialog);
127
128         assertThat(shadowDialog.getTitle()).isEqualTo("Restrict app?");
129         assertThat(shadowDialog.getMessage())
130                 .isEqualTo(
131                         "To save battery, stop app from using battery in the background. This app"
132                                 + " may not work properly and notifications may be delayed.");
133     }
134
135     @Test
136     public void testOnCreateDialog_restrictTwoAppsTip_fireRestrictTwoAppsDialog() {
137         Robolectric.getForegroundThreadScheduler().pause();
138
139         mDialogFragment = BatteryTipDialogFragment.newInstance(mRestrictTwoAppsTip, METRICS_KEY);
140
141
142         FragmentTestUtil.startFragment(mDialogFragment);
143
144         Robolectric.getForegroundThreadScheduler().advanceToLastPostedRunnable();
145
146         final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
147         ShadowAlertDialog shadowDialog = shadowOf(dialog);
148
149         assertThat(shadowDialog.getTitle()).isEqualTo("Restrict 2 apps?");
150         assertThat(shadowDialog.getMessage())
151                 .isEqualTo(
152                         "To save battery, stop these apps from using battery in the background. "
153                                 + "Restricted apps may not work properly and notifications may be"
154                                 + " delayed.\n\nApps:");
155         assertThat(shadowDialog.getView()).isNotNull();
156     }
157
158     @Test
159     public void testOnCreateDialog_restrictSixAppsTip_fireRestrictSixAppsDialog() {
160         Robolectric.getForegroundThreadScheduler().pause();
161
162         final List<AppInfo> appInfos = new ArrayList<>();
163         for (int i = 0; i < 6; i++) {
164             appInfos.add(mAppInfo);
165         }
166         final RestrictAppTip restrictSixAppsTip = new RestrictAppTip(BatteryTip.StateType.NEW,
167                 appInfos);
168
169         mDialogFragment = BatteryTipDialogFragment.newInstance(restrictSixAppsTip, METRICS_KEY);
170
171         FragmentTestUtil.startFragment(mDialogFragment);
172
173         Robolectric.getForegroundThreadScheduler().advanceToLastPostedRunnable();
174
175         final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
176         ShadowAlertDialog shadowDialog = shadowOf(dialog);
177
178         assertThat(shadowDialog.getTitle()).isEqualTo("Restrict 6 apps?");
179         assertThat(shadowDialog.getMessage())
180                 .isEqualTo(
181                         "To save battery, stop these apps from using battery in the background. "
182                                 + "Restricted apps may not work properly and notifications may be"
183                                 + " delayed.\n\nApps:\napp, app, app, app, app, and app.");
184     }
185
186     @Test
187     public void testOnCreateDialog_unRestrictAppTip_fireUnRestrictDialog() {
188         mDialogFragment = BatteryTipDialogFragment.newInstance(mUnrestrictAppTip, METRICS_KEY);
189         ShadowUtils.setApplicationLabel(PACKAGE_NAME, DISPLAY_NAME);
190
191         FragmentTestUtil.startFragment(mDialogFragment);
192
193         final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
194         ShadowAlertDialog shadowDialog = shadowOf(dialog);
195
196         assertThat(shadowDialog.getTitle()).isEqualTo("Remove restriction?");
197         assertThat(shadowDialog.getMessage())
198                 .isEqualTo(mContext.getString(R.string.battery_tip_unrestrict_app_dialog_message));
199     }
200
201     @Test
202     public void testOnCreateDialog_summaryTipWithEstimation_fireDialogWithEstimation() {
203         doReturn(AVERAGE_TIME_MS).when(mSummaryTip).getAverageTimeMs();
204         mDialogFragment = BatteryTipDialogFragment.newInstance(mSummaryTip, METRICS_KEY);
205
206         FragmentTestUtil.startFragment(mDialogFragment);
207
208         final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
209         ShadowAlertDialog shadowDialog = shadowOf(dialog);
210
211         assertThat(shadowDialog.getMessage()).isEqualTo(
212                 "Based on your usage, your battery usually lasts about 1 hr when fully charged"
213                         + ".\n\nIf you need to extend your battery life, turn on Battery Saver.");
214     }
215
216     @Test
217     public void testOnCreateDialog_summaryTipWithoutEstimation_fireDialogWithoutEstimation() {
218         mDialogFragment = BatteryTipDialogFragment.newInstance(mSummaryTip, METRICS_KEY);
219
220         FragmentTestUtil.startFragment(mDialogFragment);
221
222         final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
223         ShadowAlertDialog shadowDialog = shadowOf(dialog);
224
225         assertThat(shadowDialog.getMessage()).isEqualTo(
226                 "If you need to extend your battery life, turn on Battery Saver");
227     }
228 }