• 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.fuelgauge;
18 
19 import static com.android.settings.SettingsActivity.EXTRA_SHOW_FRAGMENT_ARGUMENTS;
20 import static com.google.common.truth.Truth.assertThat;
21 import static org.mockito.Matchers.anyInt;
22 import static org.mockito.Matchers.eq;
23 import static org.mockito.Mockito.doAnswer;
24 import static org.mockito.Mockito.doReturn;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27 import static org.mockito.Mockito.verify;
28 
29 import android.content.Intent;
30 import android.content.pm.PackageManager;
31 import android.graphics.drawable.Drawable;
32 import android.support.v7.preference.Preference;
33 import android.support.v7.preference.PreferenceCategory;
34 import android.support.v7.preference.PreferenceGroup;
35 import android.support.v7.preference.PreferenceManager;
36 import android.util.IconDrawableFactory;
37 
38 import com.android.settings.SettingsActivity;
39 import com.android.settings.fuelgauge.anomaly.Anomaly;
40 import com.android.settings.testutils.SettingsRobolectricTestRunner;
41 
42 import org.junit.Before;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.ArgumentCaptor;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 import org.mockito.invocation.InvocationOnMock;
49 import org.mockito.stubbing.Answer;
50 import org.robolectric.RuntimeEnvironment;
51 
52 import java.util.ArrayList;
53 import java.util.List;
54 
55 @RunWith(SettingsRobolectricTestRunner.class)
56 public class PowerUsageAnomalyDetailsTest {
57 
58     private static final String NAME_APP_1 = "app1";
59     private static final String NAME_APP_2 = "app2";
60     private static final String NAME_APP_3 = "app3";
61     private static final String PACKAGE_NAME_1 = "com.android.app1";
62     private static final String PACKAGE_NAME_2 = "com.android.app2";
63     private static final String PACKAGE_NAME_3 = "com.android.app3";
64 
65     @Mock
66     private SettingsActivity mSettingsActivity;
67     @Mock
68     private PreferenceManager mPreferenceManager;
69     @Mock
70     private Drawable mDrawable1;
71     @Mock
72     private Drawable mDrawable2;
73     @Mock
74     private Drawable mDrawable3;
75     @Mock
76     private PackageManager mPackageManager;
77     @Mock
78     private IconDrawableFactory mIconDrawableFactory;
79 
80     private PowerUsageAnomalyDetails mFragment;
81     private PreferenceGroup mAbnormalListGroup;
82     private List<Anomaly> mAnomalyList;
83 
84     @Before
setUp()85     public void setUp() {
86         MockitoAnnotations.initMocks(this);
87 
88         mAbnormalListGroup = spy(new PreferenceCategory(RuntimeEnvironment.application));
89 
90         mAnomalyList = new ArrayList<>();
91         Anomaly anomaly1 = new Anomaly.Builder()
92                 .setType(Anomaly.AnomalyType.WAKE_LOCK)
93                 .setPackageName(PACKAGE_NAME_1)
94                 .setDisplayName(NAME_APP_1)
95                 .build();
96         mAnomalyList.add(anomaly1);
97         Anomaly anomaly2 = new Anomaly.Builder()
98                 .setType(Anomaly.AnomalyType.WAKEUP_ALARM)
99                 .setPackageName(PACKAGE_NAME_2)
100                 .setDisplayName(NAME_APP_2)
101                 .build();
102         mAnomalyList.add(anomaly2);
103         Anomaly anomaly3 = new Anomaly.Builder()
104                 .setType(Anomaly.AnomalyType.BLUETOOTH_SCAN)
105                 .setPackageName(PACKAGE_NAME_3)
106                 .setDisplayName(NAME_APP_3)
107                 .build();
108         mAnomalyList.add(anomaly3);
109 
110         mFragment = spy(new PowerUsageAnomalyDetails());
111         mFragment.mAbnormalListGroup = mAbnormalListGroup;
112         mFragment.mAnomalies = mAnomalyList;
113         mFragment.mBatteryUtils = new BatteryUtils(RuntimeEnvironment.application);
114         mFragment.mPackageManager = mPackageManager;
115         mFragment.mIconDrawableFactory = mIconDrawableFactory;
116         when(mFragment.getPreferenceManager()).thenReturn(mPreferenceManager);
117         when(mPreferenceManager.getContext()).thenReturn(RuntimeEnvironment.application);
118     }
119 
120     @Test
testRefreshUi_displayCorrectTitleAndSummary()121     public void testRefreshUi_displayCorrectTitleAndSummary() {
122         final List<Preference> testPreferences = new ArrayList<>();
123         final ArgumentCaptor<Preference> preferenceCaptor =
124             ArgumentCaptor.forClass(Preference.class);
125         Answer<Void> prefCallable = new Answer<Void>() {
126             @Override
127             public Void answer(InvocationOnMock invocation) throws Throwable {
128                 testPreferences.add(preferenceCaptor.getValue());
129                 return null;
130             }
131         };
132         doAnswer(prefCallable).when(mAbnormalListGroup).addPreference(preferenceCaptor.capture());
133 
134         mFragment.refreshUi();
135 
136         final Preference wakelockPreference = testPreferences.get(0);
137         assertThat(wakelockPreference.getTitle()).isEqualTo(NAME_APP_1);
138         assertThat(wakelockPreference.getSummary()).isEqualTo("Keeping device awake");
139         final Preference wakeupPreference = testPreferences.get(1);
140         assertThat(wakeupPreference.getTitle()).isEqualTo(NAME_APP_2);
141         assertThat(wakeupPreference.getSummary()).isEqualTo("Waking up device in background");
142         final Preference bluetoothPreference = testPreferences.get(2);
143         assertThat(bluetoothPreference.getTitle()).isEqualTo(NAME_APP_3);
144         assertThat(bluetoothPreference.getSummary()).isEqualTo("Requesting location frequently");
145     }
146 
147     @Test
testRefreshUi_iconCorrect()148     public void testRefreshUi_iconCorrect() {
149         doReturn(mDrawable1).when(mFragment).getBadgedIcon(eq(PACKAGE_NAME_1), anyInt());
150         doReturn(mDrawable2).when(mFragment).getBadgedIcon(eq(PACKAGE_NAME_2), anyInt());
151         doReturn(mDrawable3).when(mFragment).getBadgedIcon(eq(PACKAGE_NAME_3), anyInt());
152 
153         final List<Drawable> testIcons = new ArrayList<>();
154         final ArgumentCaptor<Preference> preferenceCaptor = ArgumentCaptor.forClass(
155                 Preference.class);
156         Answer<Void> prefCallable = new Answer<Void>() {
157             @Override
158             public Void answer(InvocationOnMock invocation) throws Throwable {
159                 testIcons.add(preferenceCaptor.getValue().getIcon());
160                 return null;
161             }
162         };
163         doAnswer(prefCallable).when(mAbnormalListGroup).addPreference(preferenceCaptor.capture());
164 
165         mFragment.refreshUi();
166 
167         assertThat(testIcons).containsExactly(mDrawable1, mDrawable2, mDrawable3);
168     }
169 
170     @Test
testStartBatteryAbnormalPage_dataCorrect()171     public void testStartBatteryAbnormalPage_dataCorrect() {
172         final ArgumentCaptor<Intent> intent = ArgumentCaptor.forClass(Intent.class);
173 
174         PowerUsageAnomalyDetails.startBatteryAbnormalPage(mSettingsActivity, mFragment,
175                 mAnomalyList);
176 
177         verify(mSettingsActivity).startActivity(intent.capture());
178         assertThat(intent.getValue().getBundleExtra(EXTRA_SHOW_FRAGMENT_ARGUMENTS)
179                 .getParcelableArrayList(PowerUsageAnomalyDetails.EXTRA_ANOMALY_LIST))
180                 .isEqualTo(mAnomalyList);
181     }
182 }
183