2 * Copyright (C) 2018 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.settings.fuelgauge.batterytip;
19 import static com.google.common.truth.Truth.assertThat;
21 import static org.mockito.Mockito.doReturn;
22 import static org.mockito.Mockito.spy;
23 import static org.robolectric.Shadows.shadowOf;
25 import android.app.AlertDialog;
26 import android.content.Context;
27 import android.text.format.DateUtils;
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;
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;
51 import java.util.ArrayList;
52 import java.util.List;
54 @RunWith(SettingsRobolectricTestRunner.class)
55 @Config(shadows = ShadowUtils.class)
56 public class BatteryTipDialogFragmentTest {
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;
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;
75 MockitoAnnotations.initMocks(this);
77 mContext = spy(RuntimeEnvironment.application);
78 FakeFeatureFactory.setupForTest();
79 ShadowUtils.setApplicationLabel(PACKAGE_NAME, DISPLAY_NAME);
81 List<AppInfo> highUsageTips = new ArrayList<>();
82 mAppInfo = new AppInfo.Builder()
83 .setScreenOnTimeMs(SCREEN_TIME_MS)
84 .setPackageName(PACKAGE_NAME)
86 highUsageTips.add(mAppInfo);
87 mHighUsageTip = new HighUsageTip(SCREEN_TIME_MS, highUsageTips);
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));
97 mUnrestrictAppTip = new UnrestrictAppTip(BatteryTip.StateType.NEW, mAppInfo);
98 mSummaryTip = spy(new SummaryTip(BatteryTip.StateType.NEW,
99 Estimate.AVERAGE_TIME_TO_DISCHARGE_UNKNOWN));
103 public void testOnCreateDialog_highUsageTip_fireHighUsageDialog() {
104 Robolectric.getForegroundThreadScheduler().pause();
106 mDialogFragment = BatteryTipDialogFragment.newInstance(mHighUsageTip, METRICS_KEY);
108 FragmentTestUtil.startFragment(mDialogFragment);
110 Robolectric.getForegroundThreadScheduler().advanceToLastPostedRunnable();
112 final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
113 ShadowAlertDialog shadowDialog = shadowOf(dialog);
115 assertThat(shadowDialog.getMessage())
116 .isEqualTo(mContext.getString(R.string.battery_tip_dialog_message, 1));
120 public void testOnCreateDialog_restrictOneAppTip_fireRestrictOneAppDialog() {
121 mDialogFragment = BatteryTipDialogFragment.newInstance(mRestrictedOneAppTip, METRICS_KEY);
123 FragmentTestUtil.startFragment(mDialogFragment);
125 final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
126 ShadowAlertDialog shadowDialog = shadowOf(dialog);
128 assertThat(shadowDialog.getTitle()).isEqualTo("Restrict app?");
129 assertThat(shadowDialog.getMessage())
131 "To save battery, stop app from using battery in the background. This app"
132 + " may not work properly and notifications may be delayed.");
136 public void testOnCreateDialog_restrictTwoAppsTip_fireRestrictTwoAppsDialog() {
137 Robolectric.getForegroundThreadScheduler().pause();
139 mDialogFragment = BatteryTipDialogFragment.newInstance(mRestrictTwoAppsTip, METRICS_KEY);
142 FragmentTestUtil.startFragment(mDialogFragment);
144 Robolectric.getForegroundThreadScheduler().advanceToLastPostedRunnable();
146 final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
147 ShadowAlertDialog shadowDialog = shadowOf(dialog);
149 assertThat(shadowDialog.getTitle()).isEqualTo("Restrict 2 apps?");
150 assertThat(shadowDialog.getMessage())
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();
159 public void testOnCreateDialog_restrictSixAppsTip_fireRestrictSixAppsDialog() {
160 Robolectric.getForegroundThreadScheduler().pause();
162 final List<AppInfo> appInfos = new ArrayList<>();
163 for (int i = 0; i < 6; i++) {
164 appInfos.add(mAppInfo);
166 final RestrictAppTip restrictSixAppsTip = new RestrictAppTip(BatteryTip.StateType.NEW,
169 mDialogFragment = BatteryTipDialogFragment.newInstance(restrictSixAppsTip, METRICS_KEY);
171 FragmentTestUtil.startFragment(mDialogFragment);
173 Robolectric.getForegroundThreadScheduler().advanceToLastPostedRunnable();
175 final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
176 ShadowAlertDialog shadowDialog = shadowOf(dialog);
178 assertThat(shadowDialog.getTitle()).isEqualTo("Restrict 6 apps?");
179 assertThat(shadowDialog.getMessage())
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.");
187 public void testOnCreateDialog_unRestrictAppTip_fireUnRestrictDialog() {
188 mDialogFragment = BatteryTipDialogFragment.newInstance(mUnrestrictAppTip, METRICS_KEY);
189 ShadowUtils.setApplicationLabel(PACKAGE_NAME, DISPLAY_NAME);
191 FragmentTestUtil.startFragment(mDialogFragment);
193 final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
194 ShadowAlertDialog shadowDialog = shadowOf(dialog);
196 assertThat(shadowDialog.getTitle()).isEqualTo("Remove restriction?");
197 assertThat(shadowDialog.getMessage())
198 .isEqualTo(mContext.getString(R.string.battery_tip_unrestrict_app_dialog_message));
202 public void testOnCreateDialog_summaryTipWithEstimation_fireDialogWithEstimation() {
203 doReturn(AVERAGE_TIME_MS).when(mSummaryTip).getAverageTimeMs();
204 mDialogFragment = BatteryTipDialogFragment.newInstance(mSummaryTip, METRICS_KEY);
206 FragmentTestUtil.startFragment(mDialogFragment);
208 final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
209 ShadowAlertDialog shadowDialog = shadowOf(dialog);
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.");
217 public void testOnCreateDialog_summaryTipWithoutEstimation_fireDialogWithoutEstimation() {
218 mDialogFragment = BatteryTipDialogFragment.newInstance(mSummaryTip, METRICS_KEY);
220 FragmentTestUtil.startFragment(mDialogFragment);
222 final AlertDialog dialog = (AlertDialog) ShadowDialog.getLatestDialog();
223 ShadowAlertDialog shadowDialog = shadowOf(dialog);
225 assertThat(shadowDialog.getMessage()).isEqualTo(
226 "If you need to extend your battery life, turn on Battery Saver");