Add abnormal app page for battery settings
1. Add abnormal app page 2. Add strings for abnormal app page 3. Change AnomalyPreferenceController to open abnormal app page, when there are more than one anomaly. 4. Add AnomalyPreference, who stores a reference of anomaly. Also rename AnomalyPreferenceController to AnomalySummaryPreferenceController because this controller is not used to handle AnomalyPreference. Following cls will add summary and icon for each abnormal app. Bug: 37681665 Test: RunSettingsRoboTests Change-Id: I4266f906476ff8daccd962572c8cfa99f948080a
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.fuelgauge;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
|
||||
import static org.mockito.Matchers.any;
|
||||
import static org.mockito.Matchers.anyInt;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.spy;
|
||||
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.support.v7.preference.Preference;
|
||||
import android.support.v7.preference.PreferenceCategory;
|
||||
import android.support.v7.preference.PreferenceGroup;
|
||||
import android.support.v7.preference.PreferenceManager;
|
||||
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.fuelgauge.anomaly.Anomaly;
|
||||
import com.android.settings.fuelgauge.anomaly.AnomalyPreference;
|
||||
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.mockito.invocation.InvocationOnMock;
|
||||
import org.mockito.stubbing.Answer;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class PowerUsageAnomalyDetailsTest {
|
||||
private static final String NAME_APP_1 = "app1";
|
||||
private static final String NAME_APP_2 = "app2";
|
||||
|
||||
@Mock
|
||||
private SettingsActivity mSettingsActivity;
|
||||
@Mock
|
||||
private PreferenceManager mPreferenceManager;
|
||||
private Context mContext;
|
||||
private PowerUsageAnomalyDetails mFragment;
|
||||
private PreferenceGroup mAbnormalListGroup;
|
||||
private Bundle mBundle;
|
||||
private List<Anomaly> mAnomalyList;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mAbnormalListGroup = spy(new PreferenceCategory(mContext));
|
||||
|
||||
mAnomalyList = new ArrayList<>();
|
||||
Anomaly anomaly1 = new Anomaly.Builder()
|
||||
.setType(Anomaly.AnomalyType.WAKE_LOCK)
|
||||
.setDisplayName(NAME_APP_1)
|
||||
.build();
|
||||
mAnomalyList.add(anomaly1);
|
||||
Anomaly anomaly2 = new Anomaly.Builder()
|
||||
.setType(Anomaly.AnomalyType.WAKE_LOCK)
|
||||
.setDisplayName(NAME_APP_2)
|
||||
.build();
|
||||
mAnomalyList.add(anomaly2);
|
||||
|
||||
mFragment = spy(new PowerUsageAnomalyDetails());
|
||||
mFragment.mAbnormalListGroup = mAbnormalListGroup;
|
||||
mFragment.mAnomalies = mAnomalyList;
|
||||
doReturn(mPreferenceManager).when(mFragment).getPreferenceManager();
|
||||
doReturn(mContext).when(mPreferenceManager).getContext();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRefreshUi_dataCorrect() {
|
||||
final List<Anomaly> testAnomalyList = new ArrayList<>();
|
||||
final ArgumentCaptor<Preference> preferenceCaptor = ArgumentCaptor.forClass(
|
||||
Preference.class);
|
||||
Answer<Void> prefCallable = new Answer<Void>() {
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Throwable {
|
||||
testAnomalyList.add(
|
||||
((AnomalyPreference) preferenceCaptor.getValue()).getAnomaly());
|
||||
return null;
|
||||
}
|
||||
};
|
||||
doAnswer(prefCallable).when(mAbnormalListGroup).addPreference(preferenceCaptor.capture());
|
||||
|
||||
mFragment.refreshUi();
|
||||
|
||||
assertThat(testAnomalyList).containsExactlyElementsIn(mAnomalyList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testStartBatteryAbnormalPage_dataCorrect() {
|
||||
final ArgumentCaptor<Bundle> bundleCaptor = ArgumentCaptor.forClass(Bundle.class);
|
||||
Answer<Void> bundleCallable = new Answer<Void>() {
|
||||
@Override
|
||||
public Void answer(InvocationOnMock invocation) throws Exception {
|
||||
mBundle = bundleCaptor.getValue();
|
||||
return null;
|
||||
}
|
||||
};
|
||||
doAnswer(bundleCallable).when(mSettingsActivity).startPreferencePanelAsUser(any(),
|
||||
anyString(),
|
||||
bundleCaptor.capture(), anyInt(), any(), any());
|
||||
|
||||
PowerUsageAnomalyDetails.startBatteryAbnormalPage(mSettingsActivity, mFragment,
|
||||
mAnomalyList);
|
||||
|
||||
assertThat(mBundle.getParcelableArrayList(
|
||||
PowerUsageAnomalyDetails.EXTRA_ANOMALY_LIST)).isEqualTo(mAnomalyList);
|
||||
}
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.android.settings.fuelgauge.anomaly;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import android.content.Context;
|
||||
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class AnomalyPreferenceTest {
|
||||
@Anomaly.AnomalyType
|
||||
private static final int ANOMALY_TYPE = Anomaly.AnomalyType.WAKE_LOCK;
|
||||
private static final String PACKAGE_NAME = "com.android.app";
|
||||
private static final String DISPLAY_NAME = "app";
|
||||
private static final int UID = 111;
|
||||
|
||||
private Context mContext;
|
||||
private Anomaly mAnomaly;
|
||||
private AnomalyPreference mAnomalyPreference;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
|
||||
mAnomaly = new Anomaly.Builder()
|
||||
.setType(ANOMALY_TYPE)
|
||||
.setPackageName(PACKAGE_NAME)
|
||||
.setDisplayName(DISPLAY_NAME)
|
||||
.setUid(UID)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAnomalyPreference_containsCorrectData() {
|
||||
mAnomalyPreference = new AnomalyPreference(mContext, mAnomaly);
|
||||
|
||||
assertThat(mAnomalyPreference.getTitle()).isEqualTo(DISPLAY_NAME);
|
||||
}
|
||||
}
|
@@ -25,28 +25,15 @@ import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.robolectric.Shadows.shadowOf;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Fragment;
|
||||
import android.app.FragmentManager;
|
||||
import android.app.FragmentTransaction;
|
||||
import android.content.Context;
|
||||
import android.content.DialogInterface;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.support.v14.preference.PreferenceFragment;
|
||||
import android.support.v7.preference.Preference;
|
||||
|
||||
import com.android.settings.R;
|
||||
import com.android.settings.Settings;
|
||||
import com.android.settings.SettingsActivity;
|
||||
import com.android.settings.SettingsRobolectricTestRunner;
|
||||
import com.android.settings.TestConfig;
|
||||
import com.android.settings.core.PreferenceController;
|
||||
import com.android.settings.dashboard.DashboardFragment;
|
||||
import com.android.settings.fuelgauge.ButtonActionDialogFragmentTest;
|
||||
import com.android.settings.testutils.shadow.SettingsShadowResources;
|
||||
import com.android.settings.testutils.shadow.ShadowDynamicIndexableContentMonitor;
|
||||
import com.android.settings.testutils.shadow.ShadowEventLogWriter;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -56,16 +43,13 @@ import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.robolectric.RuntimeEnvironment;
|
||||
import org.robolectric.annotation.Config;
|
||||
import org.robolectric.shadows.ShadowAlertDialog;
|
||||
import org.robolectric.shadows.ShadowDialog;
|
||||
import org.robolectric.util.FragmentTestUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@RunWith(SettingsRobolectricTestRunner.class)
|
||||
@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION)
|
||||
public class AnomalyPreferenceControllerTest {
|
||||
public class AnomalySummaryPreferenceControllerTest {
|
||||
@Anomaly.AnomalyType
|
||||
private static final int ANOMALY_TYPE = Anomaly.AnomalyType.WAKE_LOCK;
|
||||
private static final String PACKAGE_NAME = "com.android.app";
|
||||
@@ -77,7 +61,9 @@ public class AnomalyPreferenceControllerTest {
|
||||
private FragmentManager mFragmentManager;
|
||||
@Mock
|
||||
private FragmentTransaction mFragmentTransaction;
|
||||
private AnomalyPreferenceController mAnomalyPreferenceController;
|
||||
@Mock
|
||||
private SettingsActivity mSettingsActivity;
|
||||
private AnomalySummaryPreferenceController mAnomalySummaryPreferenceController;
|
||||
private Preference mPreference;
|
||||
private Context mContext;
|
||||
private List<Anomaly> mAnomalyList;
|
||||
@@ -88,7 +74,7 @@ public class AnomalyPreferenceControllerTest {
|
||||
|
||||
mContext = RuntimeEnvironment.application;
|
||||
mPreference = new Preference(mContext);
|
||||
mPreference.setKey(AnomalyPreferenceController.ANOMALY_KEY);
|
||||
mPreference.setKey(AnomalySummaryPreferenceController.ANOMALY_KEY);
|
||||
when(mFragment.getPreferenceManager().findPreference(any())).thenReturn(mPreference);
|
||||
when(mFragment.getFragmentManager()).thenReturn(mFragmentManager);
|
||||
when(mFragmentManager.beginTransaction()).thenReturn(mFragmentTransaction);
|
||||
@@ -101,22 +87,23 @@ public class AnomalyPreferenceControllerTest {
|
||||
.build();
|
||||
mAnomalyList.add(anomaly);
|
||||
|
||||
mAnomalyPreferenceController = new AnomalyPreferenceController(mFragment);
|
||||
mAnomalySummaryPreferenceController = new AnomalySummaryPreferenceController(
|
||||
mSettingsActivity, mFragment);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUpdateAnomalyPreference_hasCorrectData() {
|
||||
mAnomalyPreferenceController.updateAnomalyPreference(mAnomalyList);
|
||||
public void testUpdateHighUsagePreference_hasCorrectData() {
|
||||
mAnomalySummaryPreferenceController.updateHighUsagePreference(mAnomalyList);
|
||||
|
||||
//add more test when this method is complete
|
||||
assertThat(mAnomalyPreferenceController.mAnomalies).isEqualTo(mAnomalyList);
|
||||
assertThat(mAnomalySummaryPreferenceController.mAnomalies).isEqualTo(mAnomalyList);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOnPreferenceTreeClick_oneAnomaly_showDialog() {
|
||||
mAnomalyPreferenceController.mAnomalies = mAnomalyList;
|
||||
mAnomalySummaryPreferenceController.mAnomalies = mAnomalyList;
|
||||
|
||||
mAnomalyPreferenceController.onPreferenceTreeClick(mPreference);
|
||||
mAnomalySummaryPreferenceController.onPreferenceTreeClick(mPreference);
|
||||
|
||||
verify(mFragmentManager).beginTransaction();
|
||||
verify(mFragmentTransaction).add(any(), anyString());
|
Reference in New Issue
Block a user