• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 package com.android.settings.datausage;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import static org.mockito.Mockito.mock;
21 import static org.mockito.Mockito.verify;
22 
23 import android.content.Context;
24 import android.util.SparseIntArray;
25 
26 import com.android.settings.widget.UsageView;
27 import com.android.settingslib.net.NetworkCycleChartData;
28 import com.android.settingslib.net.NetworkCycleData;
29 
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.ArgumentCaptor;
34 import org.mockito.MockitoAnnotations;
35 import org.robolectric.RobolectricTestRunner;
36 import org.robolectric.RuntimeEnvironment;
37 
38 import java.util.ArrayList;
39 import java.util.List;
40 import java.util.concurrent.TimeUnit;
41 
42 @RunWith(RobolectricTestRunner.class)
43 public class ChartDataUsagePreferenceTest {
44 
45     // Test cycle start date, 20 Mar 2018 22:00: GMT
46     private static final long TIMESTAMP_START = 1521583200000L;
47     // Test bucket end date, 22 Mar 2018 00:00:00
48     private static final long TIMESTAMP_END = 1521676800000L;
49 
50     private List<NetworkCycleData> mNetworkCycleData;
51     private NetworkCycleChartData mNetworkCycleChartData;
52     private Context mContext;
53     private ChartDataUsagePreference mPreference;
54 
55     @Before
setUp()56     public void setUp() {
57         MockitoAnnotations.initMocks(this);
58 
59         mContext = RuntimeEnvironment.application;
60         mPreference = new ChartDataUsagePreference(mContext, null);
61     }
62 
63     @Test
calcPoints_dataAvailableFromCycleStart_shouldAddDataPointsOnly()64     public void calcPoints_dataAvailableFromCycleStart_shouldAddDataPointsOnly() {
65         final UsageView usageView = mock(UsageView.class);
66         final ArgumentCaptor<SparseIntArray> pointsCaptor =
67                 ArgumentCaptor.forClass(SparseIntArray.class);
68         createTestNetworkData();
69         mPreference.setNetworkCycleData(mNetworkCycleChartData);
70 
71         mPreference.calcPoints(usageView, mNetworkCycleData.subList(0, 5));
72 
73         verify(usageView).addPath(pointsCaptor.capture());
74         final SparseIntArray points = pointsCaptor.getValue();
75         // the point should be normal usage data
76         assertThat(points.valueAt(1)).isNotEqualTo(-1);
77     }
78 
79     @Test
calcPoints_dataNotAvailableAtCycleStart_shouldIndicateStartOfData()80     public void calcPoints_dataNotAvailableAtCycleStart_shouldIndicateStartOfData() {
81         final UsageView usageView = mock(UsageView.class);
82         final ArgumentCaptor<SparseIntArray> pointsCaptor =
83                 ArgumentCaptor.forClass(SparseIntArray.class);
84         createTestNetworkData();
85         mPreference.setNetworkCycleData(mNetworkCycleChartData);
86 
87         mPreference.calcPoints(usageView, mNetworkCycleData.subList(2, 7));
88 
89         verify(usageView).addPath(pointsCaptor.capture());
90         final SparseIntArray points = pointsCaptor.getValue();
91         // indicator that no data is available
92         assertThat(points.keyAt(1)).isEqualTo(points.keyAt(2) - 1);
93         assertThat(points.valueAt(1)).isEqualTo(-1);
94     }
95 
96     @Test
calcPoints_shouldNotDrawPointForFutureDate()97     public void calcPoints_shouldNotDrawPointForFutureDate() {
98         final UsageView usageView = mock(UsageView.class);
99         final ArgumentCaptor<SparseIntArray> pointsCaptor =
100             ArgumentCaptor.forClass(SparseIntArray.class);
101         final long tonight = System.currentTimeMillis() + TimeUnit.HOURS.toMillis(12);
102         mNetworkCycleData = new ArrayList<>();
103         // add test usage data for last 5 days
104         mNetworkCycleData.add(createNetworkCycleData(
105             tonight - TimeUnit.DAYS.toMillis(5), tonight - TimeUnit.DAYS.toMillis(4), 743823454L));
106         mNetworkCycleData.add(createNetworkCycleData(
107             tonight - TimeUnit.DAYS.toMillis(4), tonight - TimeUnit.DAYS.toMillis(3), 64396L));
108         mNetworkCycleData.add(createNetworkCycleData(
109             tonight - TimeUnit.DAYS.toMillis(3), tonight - TimeUnit.DAYS.toMillis(2), 2832L));
110         mNetworkCycleData.add(createNetworkCycleData(
111             tonight - TimeUnit.DAYS.toMillis(2), tonight - TimeUnit.DAYS.toMillis(1), 83849690L));
112         mNetworkCycleData.add(createNetworkCycleData(
113             tonight - TimeUnit.DAYS.toMillis(1), tonight, 1883657L));
114         // add dummy usage data for next 5 days
115         mNetworkCycleData.add(createNetworkCycleData(
116             tonight, tonight + TimeUnit.DAYS.toMillis(1), 0L));
117         mNetworkCycleData.add(createNetworkCycleData(
118             tonight + TimeUnit.DAYS.toMillis(1), tonight + TimeUnit.DAYS.toMillis(2), 0L));
119         mNetworkCycleData.add(createNetworkCycleData(
120             tonight + TimeUnit.DAYS.toMillis(2), tonight + TimeUnit.DAYS.toMillis(3), 0L));
121         mNetworkCycleData.add(createNetworkCycleData(
122             tonight + TimeUnit.DAYS.toMillis(3), tonight + TimeUnit.DAYS.toMillis(4), 0L));
123         mNetworkCycleData.add(createNetworkCycleData(
124             tonight + TimeUnit.DAYS.toMillis(4), tonight + TimeUnit.DAYS.toMillis(5), 0L));
125         mNetworkCycleData.add(createNetworkCycleData(
126             tonight + TimeUnit.DAYS.toMillis(5), tonight + TimeUnit.DAYS.toMillis(6), 0L));
127 
128         final NetworkCycleChartData.Builder builder = new NetworkCycleChartData.Builder();
129         builder.setUsageBuckets(mNetworkCycleData)
130             .setStartTime(tonight - TimeUnit.DAYS.toMillis(5))
131             .setEndTime(tonight + TimeUnit.DAYS.toMillis(6));
132         mNetworkCycleChartData = builder.build();
133         mPreference.setNetworkCycleData(mNetworkCycleChartData);
134 
135         mPreference.calcPoints(usageView, mNetworkCycleData);
136 
137         verify(usageView).addPath(pointsCaptor.capture());
138         final SparseIntArray points = pointsCaptor.getValue();
139         // should only have 7 points: 1 dummy point indicating the start of data, starting point 0,
140         // and 5 actual data point for each day
141         assertThat(points.size()).isEqualTo(7);
142         assertThat(points.keyAt(0)).isEqualTo(-1);
143         assertThat(points.keyAt(1)).isEqualTo(0);
144         assertThat(points.keyAt(2)).isEqualTo(TimeUnit.DAYS.toMinutes(1));
145         assertThat(points.keyAt(3)).isEqualTo(TimeUnit.DAYS.toMinutes(2));
146         assertThat(points.keyAt(4)).isEqualTo(TimeUnit.DAYS.toMinutes(3));
147         assertThat(points.keyAt(5)).isEqualTo(TimeUnit.DAYS.toMinutes(4));
148         assertThat(points.keyAt(6)).isEqualTo(TimeUnit.DAYS.toMinutes(5));
149     }
150 
createTestNetworkData()151     private void createTestNetworkData() {
152         mNetworkCycleData = new ArrayList<>();
153         // create 10 arbitrary network data
154         mNetworkCycleData.add(createNetworkCycleData(1521583200000L, 1521586800000L, 743823454L));
155         mNetworkCycleData.add(createNetworkCycleData(1521586800000L, 1521590400000L, 64396L));
156         mNetworkCycleData.add(createNetworkCycleData(1521590400000L, 1521655200000L, 2832L));
157         mNetworkCycleData.add(createNetworkCycleData(1521655200000L, 1521658800000L, 83849690L));
158         mNetworkCycleData.add(createNetworkCycleData(1521658800000L, 1521662400000L, 1883657L));
159         mNetworkCycleData.add(createNetworkCycleData(1521662400000L, 1521666000000L, 705259L));
160         mNetworkCycleData.add(createNetworkCycleData(1521666000000L, 1521669600000L, 216169L));
161         mNetworkCycleData.add(createNetworkCycleData(1521669600000L, 1521673200000L, 6069175L));
162         mNetworkCycleData.add(createNetworkCycleData(1521673200000L, 1521676800000L, 120389L));
163         mNetworkCycleData.add(createNetworkCycleData(1521676800000L, 1521678800000L, 29947L));
164 
165         final NetworkCycleChartData.Builder builder = new NetworkCycleChartData.Builder();
166         builder.setUsageBuckets(mNetworkCycleData)
167             .setStartTime(TIMESTAMP_START)
168             .setEndTime(TIMESTAMP_END);
169         mNetworkCycleChartData = builder.build();
170     }
171 
createNetworkCycleData(long start, long end, long usage)172     private NetworkCycleData createNetworkCycleData(long start, long end, long usage) {
173         return new NetworkCycleData.Builder()
174             .setStartTime(start).setEndTime(end).setTotalUsage(usage).build();
175     }
176 }
177