Report unavailable data usage.
- when showing the data usage graph, if the first data point is equal to the first available data, insert a 0 usage point on the first day of available data, so that it will show a flat line for the days that do not have available data instead of showing a non-zero slope. Change-Id: Ibf305e292a65ab3310ea12efbb575e3a7e1b6012 Fixes: 76155147 Test: make RunSettingsRoboTests
This commit is contained in:
@@ -18,6 +18,7 @@ import android.content.Context;
|
|||||||
import android.net.NetworkPolicy;
|
import android.net.NetworkPolicy;
|
||||||
import android.net.NetworkStatsHistory;
|
import android.net.NetworkStatsHistory;
|
||||||
import android.net.TrafficStats;
|
import android.net.TrafficStats;
|
||||||
|
import android.support.annotation.VisibleForTesting;
|
||||||
import android.support.v7.preference.Preference;
|
import android.support.v7.preference.Preference;
|
||||||
import android.support.v7.preference.PreferenceViewHolder;
|
import android.support.v7.preference.PreferenceViewHolder;
|
||||||
import android.text.SpannableStringBuilder;
|
import android.text.SpannableStringBuilder;
|
||||||
@@ -88,7 +89,8 @@ public class ChartDataUsagePreference extends Preference {
|
|||||||
return (int) (Math.max(totalData, policyMax) / RESOLUTION);
|
return (int) (Math.max(totalData, policyMax) / RESOLUTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void calcPoints(UsageView chart) {
|
@VisibleForTesting
|
||||||
|
void calcPoints(UsageView chart) {
|
||||||
SparseIntArray points = new SparseIntArray();
|
SparseIntArray points = new SparseIntArray();
|
||||||
NetworkStatsHistory.Entry entry = null;
|
NetworkStatsHistory.Entry entry = null;
|
||||||
|
|
||||||
@@ -108,6 +110,9 @@ public class ChartDataUsagePreference extends Preference {
|
|||||||
// increment by current bucket total
|
// increment by current bucket total
|
||||||
totalData += entry.rxBytes + entry.txBytes;
|
totalData += entry.rxBytes + entry.txBytes;
|
||||||
|
|
||||||
|
if (i == 0) {
|
||||||
|
points.put(toInt(startTime - mStart) - 1, -1);
|
||||||
|
}
|
||||||
points.put(toInt(startTime - mStart + 1), (int) (totalData / RESOLUTION));
|
points.put(toInt(startTime - mStart + 1), (int) (totalData / RESOLUTION));
|
||||||
points.put(toInt(endTime - mStart), (int) (totalData / RESOLUTION));
|
points.put(toInt(endTime - mStart), (int) (totalData / RESOLUTION));
|
||||||
}
|
}
|
||||||
|
@@ -185,6 +185,10 @@ public class UsageGraph extends View {
|
|||||||
int x = paths.keyAt(i);
|
int x = paths.keyAt(i);
|
||||||
int y = paths.valueAt(i);
|
int y = paths.valueAt(i);
|
||||||
if (y == PATH_DELIM) {
|
if (y == PATH_DELIM) {
|
||||||
|
if (i == 1) {
|
||||||
|
localPaths.put(getX(x+1) - 1, getY(0));
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (i == paths.size() - 1 && skippedLastPoint) {
|
if (i == paths.size() - 1 && skippedLastPoint) {
|
||||||
// Add back skipped point to complete the path.
|
// Add back skipped point to complete the path.
|
||||||
localPaths.put(lx, ly);
|
localPaths.put(lx, ly);
|
||||||
|
@@ -0,0 +1,129 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2018 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.datausage;
|
||||||
|
|
||||||
|
import static com.google.common.truth.Truth.assertThat;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.spy;
|
||||||
|
import static org.mockito.Mockito.verify;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.net.NetworkStatsHistory;
|
||||||
|
import android.net.NetworkStatsHistory.Entry;
|
||||||
|
import android.util.SparseIntArray;
|
||||||
|
import com.android.settings.graph.UsageView;
|
||||||
|
import com.android.settings.testutils.SettingsRobolectricTestRunner;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.mockito.ArgumentCaptor;
|
||||||
|
import org.mockito.MockitoAnnotations;
|
||||||
|
import org.robolectric.RuntimeEnvironment;
|
||||||
|
|
||||||
|
@RunWith(SettingsRobolectricTestRunner.class)
|
||||||
|
public class ChartDataUsagePreferenceTest {
|
||||||
|
|
||||||
|
private static final long MILLIS_IN_ONE_HOUR = 60 * 60 * 1000;
|
||||||
|
private static final long MILLIS_IN_ONE_DAY = 24 * MILLIS_IN_ONE_HOUR;
|
||||||
|
|
||||||
|
private NetworkStatsHistory mNetworkStatsHistory;
|
||||||
|
private Context mContext;
|
||||||
|
private ChartDataUsagePreference mPreference;
|
||||||
|
private long mNow;
|
||||||
|
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
MockitoAnnotations.initMocks(this);
|
||||||
|
|
||||||
|
mContext = RuntimeEnvironment.application;
|
||||||
|
mPreference = new ChartDataUsagePreference(mContext, null);
|
||||||
|
mNow = System.currentTimeMillis();
|
||||||
|
mNetworkStatsHistory = spy(new NetworkStatsHistory(MILLIS_IN_ONE_HOUR, 10));
|
||||||
|
addTestNetworkEntries();
|
||||||
|
mPreference.setNetworkStats(mNetworkStatsHistory);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void calcPoints_notStartOfData_shouldAddDataPointsOnly() {
|
||||||
|
final long start = mNow - 20 * MILLIS_IN_ONE_DAY;
|
||||||
|
final long end = mNow - 5 * MILLIS_IN_ONE_DAY;
|
||||||
|
mPreference.setVisibleRange(start, end);
|
||||||
|
when(mNetworkStatsHistory.getIndexAfter(start)).thenReturn(2);
|
||||||
|
when(mNetworkStatsHistory.getIndexAfter(end)).thenReturn(7);
|
||||||
|
final UsageView usageView = mock(UsageView.class);
|
||||||
|
final ArgumentCaptor<SparseIntArray> pointsCaptor =
|
||||||
|
ArgumentCaptor.forClass(SparseIntArray.class);
|
||||||
|
|
||||||
|
mPreference.calcPoints(usageView);
|
||||||
|
|
||||||
|
verify(usageView).addPath(pointsCaptor.capture());
|
||||||
|
SparseIntArray points = pointsCaptor.getValue();
|
||||||
|
// the point should be normal usage data
|
||||||
|
assertThat(points.valueAt(1)).isNotEqualTo(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void calcPoints_startOfData_shouldIndicateStartOfData() {
|
||||||
|
final long start = mNow - 20 * MILLIS_IN_ONE_DAY;
|
||||||
|
final long end = mNow - 5 * MILLIS_IN_ONE_DAY;
|
||||||
|
mPreference.setVisibleRange(start, end);
|
||||||
|
when(mNetworkStatsHistory.getIndexAfter(start)).thenReturn(0);
|
||||||
|
when(mNetworkStatsHistory.getIndexAfter(end)).thenReturn(5);
|
||||||
|
final UsageView usageView = mock(UsageView.class);
|
||||||
|
final ArgumentCaptor<SparseIntArray> pointsCaptor =
|
||||||
|
ArgumentCaptor.forClass(SparseIntArray.class);
|
||||||
|
|
||||||
|
mPreference.calcPoints(usageView);
|
||||||
|
|
||||||
|
verify(usageView).addPath(pointsCaptor.capture());
|
||||||
|
SparseIntArray points = pointsCaptor.getValue();
|
||||||
|
// indicator that no data is available
|
||||||
|
assertThat(points.keyAt(1)).isEqualTo(points.keyAt(2) - 1);
|
||||||
|
assertThat(points.valueAt(1)).isEqualTo(-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addTestNetworkEntries() {
|
||||||
|
// create 10 arbitary network data
|
||||||
|
mNetworkStatsHistory.setValues(0, createEntry(1521583200000L, 743823454L, 16574289L));
|
||||||
|
mNetworkStatsHistory.setValues(1, createEntry(1521586800000L, 64396L, 160364L));
|
||||||
|
mNetworkStatsHistory.setValues(2, createEntry(1521590400000L, 2832L, 5299L));
|
||||||
|
mNetworkStatsHistory.setValues(3, createEntry(1521655200000L, 83849690L, 3558238L));
|
||||||
|
mNetworkStatsHistory.setValues(4, createEntry(1521658800000L, 1883657L, 353330L));
|
||||||
|
mNetworkStatsHistory.setValues(5, createEntry(1521662400000L, 705259L, 279065L));
|
||||||
|
mNetworkStatsHistory.setValues(5, createEntry(1521666000000L, 216169L, 155302L));
|
||||||
|
mNetworkStatsHistory.setValues(5, createEntry(1521669600000L, 6069175L, 427581L));
|
||||||
|
mNetworkStatsHistory.setValues(5, createEntry(1521673200000L, 120389L, 110807L));
|
||||||
|
mNetworkStatsHistory.setValues(5, createEntry(1521676800000L, 29947L, 73257L));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a network entry to be used to calculate the usage chart. In the calculation, we only
|
||||||
|
* need bucketStart, total bytes (rx + tx), and bucketDuration (which is set when we create
|
||||||
|
* the NetworkStatsHistory object). Other fields are ignored, so we don't initialize here.
|
||||||
|
* @param start the timestamp when this entry begins
|
||||||
|
* @param rx the total number of received bytes
|
||||||
|
* @param tx the total number of transmitted bytes
|
||||||
|
* @return the network entry with the corresponding start time and data usage
|
||||||
|
*/
|
||||||
|
private Entry createEntry(long start, long rx, long tx) {
|
||||||
|
Entry entry = new Entry();
|
||||||
|
entry.bucketStart = start;
|
||||||
|
entry.rxBytes = rx;
|
||||||
|
entry.txBytes = tx;
|
||||||
|
return entry;
|
||||||
|
}
|
||||||
|
}
|
@@ -59,7 +59,7 @@ public class UsageGraphTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCalculateLocalPaths_singlePath() {
|
public void calculateLocalPaths_singlePath() {
|
||||||
SparseIntArray paths = new SparseIntArray();
|
SparseIntArray paths = new SparseIntArray();
|
||||||
paths.append(0, 100);
|
paths.append(0, 100);
|
||||||
paths.append(500, 50);
|
paths.append(500, 50);
|
||||||
@@ -78,7 +78,7 @@ public class UsageGraphTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCalculateLocalPaths_multiplePaths() {
|
public void calculateLocalPaths_multiplePaths() {
|
||||||
SparseIntArray paths = new SparseIntArray();
|
SparseIntArray paths = new SparseIntArray();
|
||||||
paths.append(0, 100);
|
paths.append(0, 100);
|
||||||
paths.append(200, 75);
|
paths.append(200, 75);
|
||||||
@@ -109,7 +109,7 @@ public class UsageGraphTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCalculateLocalPaths_similarPointMiddle() {
|
public void calculateLocalPaths_similarPointMiddle() {
|
||||||
SparseIntArray paths = new SparseIntArray();
|
SparseIntArray paths = new SparseIntArray();
|
||||||
paths.append(0, 100);
|
paths.append(0, 100);
|
||||||
paths.append(1, 99); // This point should be omitted.
|
paths.append(1, 99); // This point should be omitted.
|
||||||
@@ -129,7 +129,7 @@ public class UsageGraphTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCalculateLocalPaths_similarPointEnd() {
|
public void calculateLocalPaths_similarPointEnd() {
|
||||||
SparseIntArray paths = new SparseIntArray();
|
SparseIntArray paths = new SparseIntArray();
|
||||||
paths.append(0, 100);
|
paths.append(0, 100);
|
||||||
paths.append(499, 51);
|
paths.append(499, 51);
|
||||||
@@ -149,4 +149,32 @@ public class UsageGraphTest {
|
|||||||
assertThat(localPaths.keyAt(3)).isEqualTo(1001);
|
assertThat(localPaths.keyAt(3)).isEqualTo(1001);
|
||||||
assertThat(localPaths.valueAt(3)).isEqualTo(-1);
|
assertThat(localPaths.valueAt(3)).isEqualTo(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void calculateLocalPaths_unavailableData_shouldInsertFlatPoint() {
|
||||||
|
SparseIntArray paths = new SparseIntArray();
|
||||||
|
paths.append(0, 0);
|
||||||
|
paths.append(199, -1);
|
||||||
|
paths.append(200, 25);
|
||||||
|
paths.append(300, 50);
|
||||||
|
paths.append(500, 75);
|
||||||
|
paths.append(501, -1);
|
||||||
|
|
||||||
|
SparseIntArray localPaths = new SparseIntArray();
|
||||||
|
mGraph.calculateLocalPaths(paths, localPaths);
|
||||||
|
|
||||||
|
assertThat(localPaths.size()).isEqualTo(6);
|
||||||
|
assertThat(localPaths.keyAt(0)).isEqualTo(0);
|
||||||
|
assertThat(localPaths.valueAt(0)).isEqualTo(200);
|
||||||
|
assertThat(localPaths.keyAt(1)).isEqualTo(399);
|
||||||
|
assertThat(localPaths.valueAt(1)).isEqualTo(200);
|
||||||
|
assertThat(localPaths.keyAt(2)).isEqualTo(400);
|
||||||
|
assertThat(localPaths.valueAt(2)).isEqualTo(150);
|
||||||
|
assertThat(localPaths.keyAt(3)).isEqualTo(600);
|
||||||
|
assertThat(localPaths.valueAt(3)).isEqualTo(100);
|
||||||
|
assertThat(localPaths.keyAt(4)).isEqualTo(1000);
|
||||||
|
assertThat(localPaths.valueAt(4)).isEqualTo(50);
|
||||||
|
assertThat(localPaths.keyAt(5)).isEqualTo(1001);
|
||||||
|
assertThat(localPaths.valueAt(5)).isEqualTo(-1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user