Add anomaly icon for PowerGaugePreference

When the app contains anomaly, we should show anomaly icon(triangle
alert).

This cl also extracts a new method refreshAppListGroup from refreshUi.
New method is used to only refresh appListGroup. Reason for this action:

1. Improve performance(partial refresh)
2. We init AnomalyLoader in refreshUi, invoke refreshUi in
onLoadFinish will create infinite loop.

Bug: 36924669
Test: RunSettingsRoboTests
Change-Id: If54c89349e21763ca714123ac6ae884bd0f6a377
This commit is contained in:
jackqdyulei
2017-05-01 13:03:49 -07:00
parent b45a9fe0a9
commit 4cb19e74b3
5 changed files with 185 additions and 14 deletions

View File

@@ -0,0 +1,103 @@
/*
* 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.Mockito.doNothing;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.VectorDrawable;
import android.os.PowerManager;
import android.support.v7.preference.PreferenceViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settings.SettingsRobolectricTestRunner;
import com.android.settings.TestConfig;
import com.android.settings.core.lifecycle.Lifecycle;
import com.android.settings.widget.MasterSwitchPreference;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
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 PowerGaugePreferenceTest {
private static final String SUBTITLE = "Summary";
private static final String CONTENT_DESCRIPTION = "Content description";
private Context mContext;
private PowerGaugePreference mPowerGaugePreference;
private View mRootView;
private View mWidgetView;
private PreferenceViewHolder mPreferenceViewHolder;
@Before
public void setUp() {
MockitoAnnotations.initMocks(this);
mContext = RuntimeEnvironment.application;
mRootView = LayoutInflater.from(mContext).inflate(R.layout.preference_material_settings,
null);
mWidgetView = LayoutInflater.from(mContext).inflate(R.layout.preference_widget_summary,
null);
((LinearLayout) mRootView.findViewById(android.R.id.widget_frame)).addView(mWidgetView);
mPreferenceViewHolder = PreferenceViewHolder.createInstanceForTests(mRootView);
mPowerGaugePreference = new PowerGaugePreference(mContext);
}
@Test
public void testOnBindViewHolder_bindSubtitle() {
mPowerGaugePreference.setSubtitle(SUBTITLE);
mPowerGaugePreference.onBindViewHolder(mPreferenceViewHolder);
assertThat(((TextView) mPreferenceViewHolder.findViewById(
R.id.widget_summary)).getText()).isEqualTo(SUBTITLE);
}
@Test
public void testOnBindViewHolder_bindAnomalyIcon() {
mPowerGaugePreference.shouldShowAnomalyIcon(true);
mPowerGaugePreference.onBindViewHolder(mPreferenceViewHolder);
final Drawable[] drawables = ((TextView) mPreferenceViewHolder.findViewById(
R.id.widget_summary)).getCompoundDrawablesRelative();
assertThat(drawables[0]).isInstanceOf(VectorDrawable.class);
}
@Test
public void testOnBindViewHolder_bindContentDescription() {
mPowerGaugePreference.setContentDescription(CONTENT_DESCRIPTION);
mPowerGaugePreference.onBindViewHolder(mPreferenceViewHolder);
assertThat(mPreferenceViewHolder.findViewById(android.R.id.title).getContentDescription())
.isEqualTo(CONTENT_DESCRIPTION);
}
}

View File

@@ -21,6 +21,7 @@ import android.os.PowerManager;
import android.os.Process;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.util.SparseArray;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
@@ -37,6 +38,7 @@ import com.android.settings.TestConfig;
import com.android.settings.Utils;
import com.android.settings.applications.LayoutPreference;
import com.android.settings.core.PreferenceController;
import com.android.settings.fuelgauge.anomaly.Anomaly;
import com.android.settings.testutils.FakeFeatureFactory;
import com.android.settings.testutils.shadow.SettingsShadowResources;
import com.android.settings.testutils.shadow.ShadowDynamicIndexableContentMonitor;
@@ -91,6 +93,7 @@ public class PowerUsageSummaryTest {
private static final String STUB_STRING = "stub_string";
private static final int BATTERY_LEVEL = 55;
private static final int UID = 123;
private static final int UID_2 = 234;
private static final int POWER_MAH = 100;
private static final long REMAINING_TIME_US = 100000;
private static final long TIME_SINCE_LAST_FULL_CHARGE_MS = 120 * 60 * 1000;
@@ -148,6 +151,7 @@ public class PowerUsageSummaryTest {
private PowerGaugePreference mPreference;
private PowerGaugePreference mScreenUsagePref;
private PowerGaugePreference mLastFullChargePref;
private SparseArray<List<Anomaly>> mAnomalySparseArray;
@Before
public void setUp() {
@@ -464,6 +468,24 @@ public class PowerUsageSummaryTest {
assertThat(preferenceScreenKeys).containsAllIn(preferenceKeys);
}
@Test
public void testUpdateAnomalySparseArray() {
mFragment.mAnomalySparseArray = new SparseArray<>();
final List<Anomaly> anomalies = new ArrayList<>();
final Anomaly anomaly1 = new Anomaly.Builder().setUid(UID).build();
final Anomaly anomaly2 = new Anomaly.Builder().setUid(UID).build();
final Anomaly anomaly3 = new Anomaly.Builder().setUid(UID_2).build();
anomalies.add(anomaly1);
anomalies.add(anomaly2);
anomalies.add(anomaly3);
mFragment.updateAnomalySparseArray(anomalies);
assertThat(mFragment.mAnomalySparseArray.get(UID)).containsExactly(anomaly1, anomaly2);
assertThat(mFragment.mAnomalySparseArray.get(UID_2)).containsExactly(anomaly3);
}
public static class TestFragment extends PowerUsageSummary {
private Context mContext;