blob: 90b5a2ee57f009991f58b20ecb92db501d52fa0a [file] [log] [blame]
Jeff Sharkey3f391352011-06-05 17:42:53 -07001/*
2 * Copyright (C) 2011 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
17package com.android.server;
18
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070019import static android.content.Intent.ACTION_UID_REMOVED;
20import static android.content.Intent.EXTRA_UID;
Jeff Sharkey961e3042011-08-29 16:02:57 -070021import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070022import static android.net.ConnectivityManager.TYPE_MOBILE;
Jeff Sharkey3f391352011-06-05 17:42:53 -070023import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -070024import static android.net.ConnectivityManager.TYPE_WIMAX;
25import static android.net.NetworkStats.IFACE_ALL;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070026import static android.net.NetworkStats.SET_ALL;
27import static android.net.NetworkStats.SET_DEFAULT;
28import static android.net.NetworkStats.SET_FOREGROUND;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070029import static android.net.NetworkStats.TAG_NONE;
Jeff Sharkey3f391352011-06-05 17:42:53 -070030import static android.net.NetworkStats.UID_ALL;
Jeff Sharkey63d27a92011-08-03 17:04:22 -070031import static android.net.NetworkStatsHistory.FIELD_ALL;
Jeff Sharkey4e814c32011-07-14 20:37:37 -070032import static android.net.NetworkTemplate.buildTemplateMobileAll;
33import static android.net.NetworkTemplate.buildTemplateWifi;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070034import static android.net.TrafficStats.UID_REMOVED;
Jeff Sharkey905b5892011-09-30 15:19:49 -070035import static android.net.TrafficStats.UID_TETHERING;
Jeff Sharkey3f391352011-06-05 17:42:53 -070036import static android.text.format.DateUtils.DAY_IN_MILLIS;
37import static android.text.format.DateUtils.HOUR_IN_MILLIS;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070038import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
39import static android.text.format.DateUtils.WEEK_IN_MILLIS;
Jeff Sharkey3f391352011-06-05 17:42:53 -070040import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL;
41import static org.easymock.EasyMock.anyLong;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070042import static org.easymock.EasyMock.capture;
Jeff Sharkey3f391352011-06-05 17:42:53 -070043import static org.easymock.EasyMock.createMock;
44import static org.easymock.EasyMock.eq;
45import static org.easymock.EasyMock.expect;
46import static org.easymock.EasyMock.expectLastCall;
47import static org.easymock.EasyMock.isA;
48
49import android.app.AlarmManager;
50import android.app.IAlarmManager;
51import android.app.PendingIntent;
52import android.content.Intent;
53import android.net.IConnectivityManager;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070054import android.net.INetworkManagementEventObserver;
Jeff Sharkey3f391352011-06-05 17:42:53 -070055import android.net.LinkProperties;
56import android.net.NetworkInfo;
57import android.net.NetworkInfo.DetailedState;
58import android.net.NetworkState;
59import android.net.NetworkStats;
60import android.net.NetworkStatsHistory;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070061import android.net.NetworkTemplate;
Jeff Sharkey3f391352011-06-05 17:42:53 -070062import android.os.INetworkManagementService;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070063import android.telephony.TelephonyManager;
Jeff Sharkey3f391352011-06-05 17:42:53 -070064import android.test.AndroidTestCase;
65import android.test.suitebuilder.annotation.LargeTest;
66import android.util.TrustedTime;
67
68import com.android.server.net.NetworkStatsService;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070069import com.android.server.net.NetworkStatsService.NetworkStatsSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -070070
Jeff Sharkey8e9992a2011-08-23 18:37:23 -070071import org.easymock.Capture;
Jeff Sharkey3f391352011-06-05 17:42:53 -070072import org.easymock.EasyMock;
73
74import java.io.File;
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -070075
76import libcore.io.IoUtils;
Jeff Sharkey3f391352011-06-05 17:42:53 -070077
78/**
79 * Tests for {@link NetworkStatsService}.
80 */
81@LargeTest
82public class NetworkStatsServiceTest extends AndroidTestCase {
83 private static final String TAG = "NetworkStatsServiceTest";
84
85 private static final String TEST_IFACE = "test0";
Jeff Sharkey163e6442011-10-31 16:37:52 -070086 private static final String TEST_IFACE2 = "test1";
Jeff Sharkey3f391352011-06-05 17:42:53 -070087 private static final long TEST_START = 1194220800000L;
88
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070089 private static final String IMSI_1 = "310004";
90 private static final String IMSI_2 = "310260";
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070091
Jeff Sharkey4e814c32011-07-14 20:37:37 -070092 private static NetworkTemplate sTemplateWifi = buildTemplateWifi();
93 private static NetworkTemplate sTemplateImsi1 = buildTemplateMobileAll(IMSI_1);
94 private static NetworkTemplate sTemplateImsi2 = buildTemplateMobileAll(IMSI_2);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -070095
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -070096 private static final int UID_RED = 1001;
97 private static final int UID_BLUE = 1002;
98 private static final int UID_GREEN = 1003;
Jeff Sharkey39ebc212011-06-11 17:25:42 -070099
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700100 private long mElapsedRealtime;
101
Jeff Sharkey3f391352011-06-05 17:42:53 -0700102 private BroadcastInterceptingContext mServiceContext;
103 private File mStatsDir;
104
105 private INetworkManagementService mNetManager;
106 private IAlarmManager mAlarmManager;
107 private TrustedTime mTime;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700108 private NetworkStatsSettings mSettings;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700109 private IConnectivityManager mConnManager;
110
111 private NetworkStatsService mService;
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700112 private INetworkManagementEventObserver mNetworkObserver;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700113
114 @Override
115 public void setUp() throws Exception {
116 super.setUp();
117
118 mServiceContext = new BroadcastInterceptingContext(getContext());
119 mStatsDir = getContext().getFilesDir();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700120 if (mStatsDir.exists()) {
121 IoUtils.deleteContents(mStatsDir);
122 }
Jeff Sharkey3f391352011-06-05 17:42:53 -0700123
124 mNetManager = createMock(INetworkManagementService.class);
125 mAlarmManager = createMock(IAlarmManager.class);
126 mTime = createMock(TrustedTime.class);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700127 mSettings = createMock(NetworkStatsSettings.class);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700128 mConnManager = createMock(IConnectivityManager.class);
129
130 mService = new NetworkStatsService(
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700131 mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700132 mService.bindConnectivityManager(mConnManager);
133
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700134 mElapsedRealtime = 0L;
135
136 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700137 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700138 expectNetworkStatsSummary(buildEmptyStats());
139 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700140 expectSystemReady();
141
142 // catch INetworkManagementEventObserver during systemReady()
143 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
144 INetworkManagementEventObserver>();
145 mNetManager.registerObserver(capture(networkObserver));
146 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700147
148 replay();
149 mService.systemReady();
150 verifyAndReset();
151
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700152 mNetworkObserver = networkObserver.getValue();
153
Jeff Sharkey3f391352011-06-05 17:42:53 -0700154 }
155
156 @Override
157 public void tearDown() throws Exception {
158 for (File file : mStatsDir.listFiles()) {
159 file.delete();
160 }
161
162 mServiceContext = null;
163 mStatsDir = null;
164
165 mNetManager = null;
166 mAlarmManager = null;
167 mTime = null;
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700168 mSettings = null;
169 mConnManager = null;
Jeff Sharkey3f391352011-06-05 17:42:53 -0700170
171 mService = null;
172
173 super.tearDown();
174 }
175
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700176 public void testNetworkStatsWifi() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700177 // pretend that wifi network comes online; service should ask about full
178 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700179 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700180 expectDefaultSettings();
181 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700182 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700183 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700184 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700185
186 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700187 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700188
189 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700190 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700191 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700192
193 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700194 incrementCurrentTime(HOUR_IN_MILLIS);
195 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700196 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700197 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
198 .addIfaceValues(TEST_IFACE, 1024L, 1L, 2048L, 2L));
199 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700200 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700201
202 replay();
203 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700204
205 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700206 assertNetworkTotal(sTemplateWifi, 1024L, 1L, 2048L, 2L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700207 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700208
209 // and bump forward again, with counters going higher. this is
210 // important, since polling should correctly subtract last snapshot.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700211 incrementCurrentTime(DAY_IN_MILLIS);
212 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700213 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700214 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
215 .addIfaceValues(TEST_IFACE, 4096L, 4L, 8192L, 8L));
216 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700217 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700218
219 replay();
220 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700221
222 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700223 assertNetworkTotal(sTemplateWifi, 4096L, 4L, 8192L, 8L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700224 verifyAndReset();
225
Jeff Sharkey3f391352011-06-05 17:42:53 -0700226 }
227
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700228 public void testStatsRebootPersist() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700229 assertStatsFilesExist(false);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700230
231 // pretend that wifi network comes online; service should ask about full
232 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700233 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700234 expectDefaultSettings();
235 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700236 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700237 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700238 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700239
240 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700241 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700242
243 // verify service has empty history for wifi
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700244 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700245 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700246
247 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700248 incrementCurrentTime(HOUR_IN_MILLIS);
249 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700250 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700251 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
252 .addIfaceValues(TEST_IFACE, 1024L, 8L, 2048L, 16L));
253 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 2)
254 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
255 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
256 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
257 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 256L, 2L, 128L, 1L, 0L)
258 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 128L, 1L, 128L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700259 expectNetworkStatsPoll();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700260
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700261 mService.setUidForeground(UID_RED, false);
262 mService.incrementOperationCount(UID_RED, 0xFAAD, 4);
263 mService.setUidForeground(UID_RED, true);
264 mService.incrementOperationCount(UID_RED, 0xFAAD, 6);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700265
Jeff Sharkey3f391352011-06-05 17:42:53 -0700266 replay();
267 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
Jeff Sharkey3f391352011-06-05 17:42:53 -0700268
269 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700270 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700271 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
272 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
273 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
274 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700275 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700276
277 // graceful shutdown system, which should trigger persist of stats, and
278 // clear any values in memory.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700279 expectCurrentTime();
280 expectDefaultSettings();
281 replay();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700282 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN));
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700283 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700284
285 // talk with zombie service to assert stats have gone; and assert that
286 // we persisted them to file.
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700287 expectDefaultSettings();
288 replay();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700289 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700290 verifyAndReset();
291
292 assertStatsFilesExist(true);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700293
294 // boot through serviceReady() again
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700295 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700296 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700297 expectNetworkStatsSummary(buildEmptyStats());
298 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700299 expectSystemReady();
300
301 // catch INetworkManagementEventObserver during systemReady()
302 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
303 INetworkManagementEventObserver>();
304 mNetManager.registerObserver(capture(networkObserver));
305 expectLastCall().atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700306
307 replay();
308 mService.systemReady();
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700309
310 mNetworkObserver = networkObserver.getValue();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700311
312 // after systemReady(), we should have historical stats loaded again
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700313 assertNetworkTotal(sTemplateWifi, 1024L, 8L, 2048L, 16L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700314 assertUidTotal(sTemplateWifi, UID_RED, 1024L, 8L, 512L, 4L, 10);
315 assertUidTotal(sTemplateWifi, UID_RED, SET_DEFAULT, 512L, 4L, 256L, 2L, 4);
316 assertUidTotal(sTemplateWifi, UID_RED, SET_FOREGROUND, 512L, 4L, 256L, 2L, 6);
317 assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 1L, 128L, 1L, 0);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700318 verifyAndReset();
319
320 }
321
322 public void testStatsBucketResize() throws Exception {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700323 NetworkStatsHistory history = null;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700324
325 assertStatsFilesExist(false);
326
327 // pretend that wifi network comes online; service should ask about full
328 // network state, and poll any existing interfaces before updating.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700329 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700330 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
331 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700332 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700333 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700334 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700335
336 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700337 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700338 verifyAndReset();
339
340 // modify some number on wifi, and trigger poll event
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700341 incrementCurrentTime(2 * HOUR_IN_MILLIS);
342 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700343 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700344 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
345 .addIfaceValues(TEST_IFACE, 512L, 4L, 512L, 4L));
346 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700347 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700348
349 replay();
350 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
351
352 // verify service recorded history
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700353 history = mService.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700354 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700355 assertEquals(HOUR_IN_MILLIS, history.getBucketDuration());
356 assertEquals(2, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700357 verifyAndReset();
358
359 // now change bucket duration setting and trigger another poll with
360 // exact same values, which should resize existing buckets.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700361 expectCurrentTime();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700362 expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700363 expectNetworkStatsSummary(buildEmptyStats());
364 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700365 expectNetworkStatsPoll();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700366
367 replay();
368 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
369
370 // verify identical stats, but spread across 4 buckets now
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700371 history = mService.getHistoryForNetwork(sTemplateWifi, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700372 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, 512L, 4L, 512L, 4L, 0);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700373 assertEquals(30 * MINUTE_IN_MILLIS, history.getBucketDuration());
374 assertEquals(4, history.size());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700375 verifyAndReset();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700376
377 }
378
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700379 public void testUidStatsAcrossNetworks() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700380 // pretend first mobile network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700381 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700382 expectDefaultSettings();
383 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700384 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700385 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700386 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700387
388 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700389 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700390 verifyAndReset();
391
392 // create some traffic on first network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700393 incrementCurrentTime(HOUR_IN_MILLIS);
394 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700395 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700396 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
397 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
398 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
399 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
400 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
401 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700402 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700403
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700404 mService.incrementOperationCount(UID_RED, 0xF00D, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700405
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700406 replay();
407 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
408
409 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700410 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
411 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700412 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
413 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700414 verifyAndReset();
415
416 // now switch networks; this also tests that we're okay with interfaces
417 // disappearing, to verify we don't count backwards.
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700418 incrementCurrentTime(HOUR_IN_MILLIS);
419 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700420 expectDefaultSettings();
421 expectNetworkState(buildMobile3gState(IMSI_2));
Jeff Sharkey163e6442011-10-31 16:37:52 -0700422 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
423 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
424 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3)
425 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
426 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
427 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 512L, 4L, 0L, 0L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700428 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700429
430 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700431 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700432 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
433 verifyAndReset();
434
435 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700436 incrementCurrentTime(HOUR_IN_MILLIS);
437 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700438 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700439 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700440 .addIfaceValues(TEST_IFACE, 2176L, 17L, 1536L, 12L));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700441 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700442 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1536L, 12L, 512L, 4L, 0L)
443 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
444 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 640L, 5L, 1024L, 8L, 0L)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700445 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, 0xFAAD, 128L, 1L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700446 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700447
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700448 mService.incrementOperationCount(UID_BLUE, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700449
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700450 replay();
451 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
452
453 // verify original history still intact
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700454 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700455 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 512L, 4L, 10);
456 assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 4L, 0L, 0L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700457
458 // and verify new history also recorded under different template, which
459 // verifies that we didn't cross the streams.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700460 assertNetworkTotal(sTemplateImsi2, 128L, 1L, 1024L, 8L, 0);
461 assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0);
462 assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1L, 1024L, 8L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700463 verifyAndReset();
464
465 }
466
467 public void testUidRemovedIsMoved() throws Exception {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700468 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700469 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700470 expectDefaultSettings();
471 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700472 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700473 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700474 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700475
476 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700477 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700478 verifyAndReset();
479
480 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700481 incrementCurrentTime(HOUR_IN_MILLIS);
482 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700483 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700484 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
485 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
486 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
487 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
488 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
489 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
490 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700491 expectNetworkStatsPoll();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700492
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700493 mService.incrementOperationCount(UID_RED, 0xFAAD, 10);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700494
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700495 replay();
496 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
497
498 // verify service recorded history
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700499 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
500 assertUidTotal(sTemplateWifi, UID_RED, 16L, 1L, 16L, 1L, 10);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700501 assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 258L, 512L, 32L, 0);
502 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700503 verifyAndReset();
504
505 // now pretend two UIDs are uninstalled, which should migrate stats to
506 // special "removed" bucket.
Jeff Sharkey34c73ac2011-09-18 13:30:23 -0700507 expectCurrentTime();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700508 expectDefaultSettings();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700509 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
510 .addIfaceValues(TEST_IFACE, 4128L, 258L, 544L, 34L));
511 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
512 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L)
513 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xFAAD, 16L, 1L, 16L, 1L, 0L)
514 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 4096L, 258L, 512L, 32L, 0L)
515 .addValues(TEST_IFACE, UID_GREEN, SET_DEFAULT, TAG_NONE, 16L, 1L, 16L, 1L, 0L));
516 expectNetworkStatsPoll();
517
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700518 replay();
519 final Intent intent = new Intent(ACTION_UID_REMOVED);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700520 intent.putExtra(EXTRA_UID, UID_BLUE);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700521 mServiceContext.sendBroadcast(intent);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700522 intent.putExtra(EXTRA_UID, UID_RED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700523 mServiceContext.sendBroadcast(intent);
524
525 // existing uid and total should remain unchanged; but removed UID
526 // should be gone completely.
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700527 assertNetworkTotal(sTemplateWifi, 4128L, 258L, 544L, 34L, 0);
528 assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L, 0L, 0L, 0);
529 assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L, 0L, 0L, 0);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700530 assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 1L, 16L, 1L, 0);
531 assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 259L, 528L, 33L, 10);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700532 verifyAndReset();
533
534 }
535
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700536 public void testUid3g4gCombinedByTemplate() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700537 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700538 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700539 expectDefaultSettings();
540 expectNetworkState(buildMobile3gState(IMSI_1));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700541 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700542 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700543 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700544
545 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700546 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700547 verifyAndReset();
548
549 // create some traffic
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700550 incrementCurrentTime(HOUR_IN_MILLIS);
551 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700552 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700553 expectNetworkStatsSummary(buildEmptyStats());
554 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
555 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
556 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700557 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700558
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700559 mService.incrementOperationCount(UID_RED, 0xF00D, 5);
560
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700561 replay();
562 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
563
564 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700565 assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 8L, 1024L, 8L, 5);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700566 verifyAndReset();
567
568 // now switch over to 4g network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700569 incrementCurrentTime(HOUR_IN_MILLIS);
570 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700571 expectDefaultSettings();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700572 expectNetworkState(buildMobile4gState(TEST_IFACE2));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700573 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkey163e6442011-10-31 16:37:52 -0700574 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
575 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
576 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700577 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700578
579 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700580 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700581 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
582 verifyAndReset();
583
584 // create traffic on second network
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700585 incrementCurrentTime(HOUR_IN_MILLIS);
586 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700587 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700588 expectNetworkStatsSummary(buildEmptyStats());
589 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700590 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 1024L, 8L, 1024L, 8L, 0L)
591 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 512L, 4L, 512L, 4L, 0L)
592 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, TAG_NONE, 512L, 4L, 256L, 2L, 0L)
593 .addValues(TEST_IFACE2, UID_RED, SET_DEFAULT, 0xFAAD, 512L, 4L, 256L, 2L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700594 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700595
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700596 mService.incrementOperationCount(UID_RED, 0xFAAD, 5);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700597
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700598 replay();
599 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
600
601 // verify that ALL_MOBILE template combines both
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700602 assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 12L, 1280L, 10L, 10);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700603
604 verifyAndReset();
605
606 }
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700607
608 public void testSummaryForAllUid() throws Exception {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700609 // pretend that network comes online
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700610 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700611 expectDefaultSettings();
612 expectNetworkState(buildWifiState());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700613 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700614 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700615 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700616
617 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700618 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700619 verifyAndReset();
620
621 // create some traffic for two apps
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700622 incrementCurrentTime(HOUR_IN_MILLIS);
623 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700624 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700625 expectNetworkStatsSummary(buildEmptyStats());
626 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
627 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
628 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
629 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700630 expectNetworkStatsPoll();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700631
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700632 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700633
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700634 replay();
635 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
636
637 // verify service recorded history
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700638 assertUidTotal(sTemplateWifi, UID_RED, 50L, 5L, 50L, 5L, 1);
639 assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 8L, 512L, 4L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700640 verifyAndReset();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700641
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700642 // now create more traffic in next hour, but only for one app
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700643 incrementCurrentTime(HOUR_IN_MILLIS);
644 expectCurrentTime();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700645 expectDefaultSettings();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700646 expectNetworkStatsSummary(buildEmptyStats());
647 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700648 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 0L)
649 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 0L)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700650 .addValues(TEST_IFACE, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700651 expectNetworkStatsPoll();
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700652
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700653 replay();
654 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
655
656 // first verify entire history present
657 NetworkStats stats = mService.getSummaryForAllUid(
658 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700659 assertEquals(3, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700660 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 50L, 5L, 50L, 5L, 1);
661 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 10L, 1L, 10L, 1L, 1);
662 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 2048L, 16L, 1024L, 8L, 0);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700663
664 // now verify that recent history only contains one uid
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700665 final long currentTime = currentTimeMillis();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700666 stats = mService.getSummaryForAllUid(
667 sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700668 assertEquals(1, stats.size());
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700669 assertValues(stats, IFACE_ALL, UID_BLUE, SET_DEFAULT, TAG_NONE, 1024L, 8L, 512L, 4L, 0);
670
671 verifyAndReset();
672 }
673
674 public void testForegroundBackground() throws Exception {
675 // pretend that network comes online
676 expectCurrentTime();
677 expectDefaultSettings();
678 expectNetworkState(buildWifiState());
679 expectNetworkStatsSummary(buildEmptyStats());
Jeff Sharkeyb3d59572011-09-07 17:20:27 -0700680 expectNetworkStatsUidDetail(buildEmptyStats());
Jeff Sharkey905b5892011-09-30 15:19:49 -0700681 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700682
683 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700684 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700685 verifyAndReset();
686
687 // create some initial traffic
688 incrementCurrentTime(HOUR_IN_MILLIS);
689 expectCurrentTime();
690 expectDefaultSettings();
691 expectNetworkStatsSummary(buildEmptyStats());
692 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
693 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
694 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700695 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700696
697 mService.incrementOperationCount(UID_RED, 0xF00D, 1);
698
699 replay();
700 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
701
702 // verify service recorded history
703 assertUidTotal(sTemplateWifi, UID_RED, 128L, 2L, 128L, 2L, 1);
704 verifyAndReset();
705
706 // now switch to foreground
707 incrementCurrentTime(HOUR_IN_MILLIS);
708 expectCurrentTime();
709 expectDefaultSettings();
710 expectNetworkStatsSummary(buildEmptyStats());
711 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
712 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L)
713 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 0L)
714 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 0L)
715 .addValues(TEST_IFACE, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 0L));
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700716 expectNetworkStatsPoll();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700717
718 mService.setUidForeground(UID_RED, true);
719 mService.incrementOperationCount(UID_RED, 0xFAAD, 1);
720
721 replay();
722 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
723
724 // test that we combined correctly
725 assertUidTotal(sTemplateWifi, UID_RED, 160L, 4L, 160L, 4L, 2);
726
727 // verify entire history present
728 final NetworkStats stats = mService.getSummaryForAllUid(
729 sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true);
730 assertEquals(4, stats.size());
731 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 1);
732 assertValues(stats, IFACE_ALL, UID_RED, SET_DEFAULT, 0xF00D, 64L, 1L, 64L, 1L, 1);
733 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, TAG_NONE, 32L, 2L, 32L, 2L, 1);
734 assertValues(stats, IFACE_ALL, UID_RED, SET_FOREGROUND, 0xFAAD, 1L, 1L, 1L, 1L, 1);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700735
736 verifyAndReset();
737 }
738
Jeff Sharkey905b5892011-09-30 15:19:49 -0700739 public void testTethering() throws Exception {
740 // pretend first mobile network comes online
741 expectCurrentTime();
742 expectDefaultSettings();
743 expectNetworkState(buildMobile3gState(IMSI_1));
744 expectNetworkStatsSummary(buildEmptyStats());
745 expectNetworkStatsUidDetail(buildEmptyStats());
746 expectNetworkStatsPoll();
747
748 replay();
749 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
750 verifyAndReset();
751
752 // create some tethering traffic
753 incrementCurrentTime(HOUR_IN_MILLIS);
754 expectCurrentTime();
755 expectDefaultSettings();
756 expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1)
757 .addIfaceValues(TEST_IFACE, 2048L, 16L, 512L, 4L));
758 expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1)
759 .addValues(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, 128L, 2L, 128L, 2L, 0L));
760 final String[] tetherIfacePairs = new String[] { TEST_IFACE, "wlan0" };
761 expectNetworkStatsPoll(tetherIfacePairs, new NetworkStats(getElapsedRealtime(), 1)
762 .addValues(TEST_IFACE, UID_TETHERING, SET_DEFAULT, TAG_NONE, 1920L, 14L, 384L, 2L, 0L));
763
764 replay();
765 mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL));
766
767 // verify service recorded history
768 assertNetworkTotal(sTemplateImsi1, 2048L, 16L, 512L, 4L, 0);
769 assertUidTotal(sTemplateImsi1, UID_RED, 128L, 2L, 128L, 2L, 0);
770 assertUidTotal(sTemplateImsi1, UID_TETHERING, 1920L, 14L, 384L, 2L, 0);
771 verifyAndReset();
772
773 }
774
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700775 private void assertNetworkTotal(NetworkTemplate template, long rxBytes, long rxPackets,
776 long txBytes, long txPackets, int operations) {
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700777 final NetworkStatsHistory history = mService.getHistoryForNetwork(template, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700778 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
779 txPackets, operations);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700780 }
781
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700782 private void assertUidTotal(NetworkTemplate template, int uid, long rxBytes, long rxPackets,
783 long txBytes, long txPackets, int operations) {
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700784 assertUidTotal(template, uid, SET_ALL, rxBytes, rxPackets, txBytes, txPackets, operations);
785 }
786
787 private void assertUidTotal(NetworkTemplate template, int uid, int set, long rxBytes,
788 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey63d27a92011-08-03 17:04:22 -0700789 final NetworkStatsHistory history = mService.getHistoryForUid(
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700790 template, uid, set, TAG_NONE, FIELD_ALL);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700791 assertValues(history, Long.MIN_VALUE, Long.MAX_VALUE, rxBytes, rxPackets, txBytes,
792 txPackets, operations);
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700793 }
794
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700795 private void expectSystemReady() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700796 mAlarmManager.remove(isA(PendingIntent.class));
797 expectLastCall().anyTimes();
798
799 mAlarmManager.setInexactRepeating(
800 eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), isA(PendingIntent.class));
801 expectLastCall().atLeastOnce();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700802
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700803 mNetManager.setGlobalAlert(anyLong());
804 expectLastCall().atLeastOnce();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800805
806 expect(mNetManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700807 }
808
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700809 private void expectNetworkState(NetworkState... state) throws Exception {
810 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
811 }
812
813 private void expectNetworkStatsSummary(NetworkStats summary) throws Exception {
814 expect(mNetManager.getNetworkStatsSummary()).andReturn(summary).atLeastOnce();
815 }
816
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700817 private void expectNetworkStatsUidDetail(NetworkStats detail) throws Exception {
818 expect(mNetManager.getNetworkStatsUidDetail(eq(UID_ALL))).andReturn(detail).atLeastOnce();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700819 }
820
821 private void expectDefaultSettings() throws Exception {
822 expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS);
823 }
824
825 private void expectSettings(long persistThreshold, long bucketDuration, long maxHistory)
826 throws Exception {
827 expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes();
828 expect(mSettings.getPersistThreshold()).andReturn(persistThreshold).anyTimes();
829 expect(mSettings.getNetworkBucketDuration()).andReturn(bucketDuration).anyTimes();
830 expect(mSettings.getNetworkMaxHistory()).andReturn(maxHistory).anyTimes();
831 expect(mSettings.getUidBucketDuration()).andReturn(bucketDuration).anyTimes();
832 expect(mSettings.getUidMaxHistory()).andReturn(maxHistory).anyTimes();
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700833 expect(mSettings.getTagMaxHistory()).andReturn(maxHistory).anyTimes();
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700834 expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes();
835 }
836
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700837 private void expectCurrentTime() throws Exception {
Jeff Sharkey3f391352011-06-05 17:42:53 -0700838 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
839 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700840 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey3f391352011-06-05 17:42:53 -0700841 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
842 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
843 }
844
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700845 private void expectNetworkStatsPoll() throws Exception {
Jeff Sharkey905b5892011-09-30 15:19:49 -0700846 expectNetworkStatsPoll(new String[0], new NetworkStats(getElapsedRealtime(), 0));
847 }
848
849 private void expectNetworkStatsPoll(String[] tetherIfacePairs, NetworkStats tetherStats)
850 throws Exception {
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700851 mNetManager.setGlobalAlert(anyLong());
852 expectLastCall().anyTimes();
Jeff Sharkey905b5892011-09-30 15:19:49 -0700853 expect(mConnManager.getTetheredIfacePairs()).andReturn(tetherIfacePairs).anyTimes();
854 expect(mNetManager.getNetworkStatsTethering(eq(tetherIfacePairs)))
855 .andReturn(tetherStats).anyTimes();
Jeff Sharkey8e9992a2011-08-23 18:37:23 -0700856 }
857
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700858 private void assertStatsFilesExist(boolean exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700859 final File networkFile = new File(mStatsDir, "netstats.bin");
860 final File uidFile = new File(mStatsDir, "netstats_uid.bin");
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700861 if (exist) {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700862 assertTrue(networkFile.exists());
863 assertTrue(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700864 } else {
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700865 assertFalse(networkFile.exists());
866 assertFalse(uidFile.exists());
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700867 }
868 }
869
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700870 private static void assertValues(NetworkStats stats, String iface, int uid, int set,
871 int tag, long rxBytes, long rxPackets, long txBytes, long txPackets, int operations) {
872 final int i = stats.findIndex(iface, uid, set, tag);
Jeff Sharkeyd37948f2011-07-12 13:57:00 -0700873 final NetworkStats.Entry entry = stats.getValues(i, null);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700874 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
875 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
876 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
877 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
878 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700879 }
880
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700881 private static void assertValues(NetworkStatsHistory stats, long start, long end, long rxBytes,
882 long rxPackets, long txBytes, long txPackets, int operations) {
Jeff Sharkey434962e2011-07-12 20:20:56 -0700883 final NetworkStatsHistory.Entry entry = stats.getValues(start, end, null);
884 assertEquals("unexpected rxBytes", rxBytes, entry.rxBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700885 assertEquals("unexpected rxPackets", rxPackets, entry.rxPackets);
Jeff Sharkey434962e2011-07-12 20:20:56 -0700886 assertEquals("unexpected txBytes", txBytes, entry.txBytes);
Jeff Sharkeya63ba592011-07-19 23:47:12 -0700887 assertEquals("unexpected txPackets", txPackets, entry.txPackets);
888 assertEquals("unexpected operations", operations, entry.operations);
Jeff Sharkey434962e2011-07-12 20:20:56 -0700889 }
890
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700891 private static NetworkState buildWifiState() {
892 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
893 info.setDetailedState(DetailedState.CONNECTED, null, null);
894 final LinkProperties prop = new LinkProperties();
895 prop.setInterfaceName(TEST_IFACE);
896 return new NetworkState(info, prop, null);
897 }
898
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700899 private static NetworkState buildMobile3gState(String subscriberId) {
900 final NetworkInfo info = new NetworkInfo(
901 TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null);
902 info.setDetailedState(DetailedState.CONNECTED, null, null);
903 final LinkProperties prop = new LinkProperties();
904 prop.setInterfaceName(TEST_IFACE);
905 return new NetworkState(info, prop, null, subscriberId);
906 }
907
Jeff Sharkey163e6442011-10-31 16:37:52 -0700908 private static NetworkState buildMobile4gState(String iface) {
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700909 final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null);
910 info.setDetailedState(DetailedState.CONNECTED, null, null);
911 final LinkProperties prop = new LinkProperties();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700912 prop.setInterfaceName(iface);
Jeff Sharkeyd03fd3f2011-06-19 20:55:09 -0700913 return new NetworkState(info, prop, null);
914 }
915
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700916 private NetworkStats buildEmptyStats() {
917 return new NetworkStats(getElapsedRealtime(), 0);
918 }
919
920 private long getElapsedRealtime() {
921 return mElapsedRealtime;
922 }
923
924 private long startTimeMillis() {
925 return TEST_START;
926 }
927
928 private long currentTimeMillis() {
929 return startTimeMillis() + mElapsedRealtime;
930 }
931
932 private void incrementCurrentTime(long duration) {
933 mElapsedRealtime += duration;
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700934 }
935
Jeff Sharkey3f391352011-06-05 17:42:53 -0700936 private void replay() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700937 EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700938 }
939
940 private void verifyAndReset() {
Jeff Sharkey39ebc212011-06-11 17:25:42 -0700941 EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
942 EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager);
Jeff Sharkey3f391352011-06-05 17:42:53 -0700943 }
944}