Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 19 | import static android.content.Intent.ACTION_UID_REMOVED; |
| 20 | import static android.content.Intent.EXTRA_UID; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TYPE_MOBILE; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 23 | import static android.net.ConnectivityManager.TYPE_WIFI; |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 24 | import static android.net.ConnectivityManager.TYPE_WIMAX; |
| 25 | import static android.net.NetworkStats.IFACE_ALL; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 26 | import static android.net.NetworkStats.TAG_NONE; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 27 | import static android.net.NetworkStats.UID_ALL; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 28 | import static android.net.NetworkTemplate.MATCH_MOBILE_ALL; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 29 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 30 | import static android.net.TrafficStats.UID_REMOVED; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 31 | import static android.text.format.DateUtils.DAY_IN_MILLIS; |
| 32 | import static android.text.format.DateUtils.HOUR_IN_MILLIS; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 33 | import static android.text.format.DateUtils.MINUTE_IN_MILLIS; |
| 34 | import static android.text.format.DateUtils.WEEK_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 35 | import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL; |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 36 | import static com.android.server.net.NetworkStatsService.packUidAndTag; |
| 37 | import static com.android.server.net.NetworkStatsService.unpackTag; |
| 38 | import static com.android.server.net.NetworkStatsService.unpackUid; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 39 | import static org.easymock.EasyMock.anyLong; |
| 40 | import static org.easymock.EasyMock.createMock; |
| 41 | import static org.easymock.EasyMock.eq; |
| 42 | import static org.easymock.EasyMock.expect; |
| 43 | import static org.easymock.EasyMock.expectLastCall; |
| 44 | import static org.easymock.EasyMock.isA; |
| 45 | |
| 46 | import android.app.AlarmManager; |
| 47 | import android.app.IAlarmManager; |
| 48 | import android.app.PendingIntent; |
| 49 | import android.content.Intent; |
| 50 | import android.net.IConnectivityManager; |
| 51 | import android.net.LinkProperties; |
| 52 | import android.net.NetworkInfo; |
| 53 | import android.net.NetworkInfo.DetailedState; |
| 54 | import android.net.NetworkState; |
| 55 | import android.net.NetworkStats; |
| 56 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 57 | import android.net.NetworkTemplate; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 58 | import android.os.INetworkManagementService; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 59 | import android.telephony.TelephonyManager; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 60 | import android.test.AndroidTestCase; |
| 61 | import android.test.suitebuilder.annotation.LargeTest; |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 62 | import android.util.Log; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 63 | import android.util.TrustedTime; |
| 64 | |
| 65 | import com.android.server.net.NetworkStatsService; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 66 | import com.android.server.net.NetworkStatsService.NetworkStatsSettings; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 67 | |
| 68 | import org.easymock.EasyMock; |
| 69 | |
| 70 | import java.io.File; |
| 71 | |
| 72 | /** |
| 73 | * Tests for {@link NetworkStatsService}. |
| 74 | */ |
| 75 | @LargeTest |
| 76 | public class NetworkStatsServiceTest extends AndroidTestCase { |
| 77 | private static final String TAG = "NetworkStatsServiceTest"; |
| 78 | |
| 79 | private static final String TEST_IFACE = "test0"; |
| 80 | private static final long TEST_START = 1194220800000L; |
| 81 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 82 | private static final String IMSI_1 = "310004"; |
| 83 | private static final String IMSI_2 = "310260"; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 84 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 85 | private static NetworkTemplate sTemplateWifi = new NetworkTemplate(MATCH_WIFI, null); |
| 86 | private static NetworkTemplate sTemplateImsi1 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_1); |
| 87 | private static NetworkTemplate sTemplateImsi2 = new NetworkTemplate(MATCH_MOBILE_ALL, IMSI_2); |
| 88 | |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 89 | private static final int UID_RED = 1001; |
| 90 | private static final int UID_BLUE = 1002; |
| 91 | private static final int UID_GREEN = 1003; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 92 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 93 | private BroadcastInterceptingContext mServiceContext; |
| 94 | private File mStatsDir; |
| 95 | |
| 96 | private INetworkManagementService mNetManager; |
| 97 | private IAlarmManager mAlarmManager; |
| 98 | private TrustedTime mTime; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 99 | private NetworkStatsSettings mSettings; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 100 | private IConnectivityManager mConnManager; |
| 101 | |
| 102 | private NetworkStatsService mService; |
| 103 | |
| 104 | @Override |
| 105 | public void setUp() throws Exception { |
| 106 | super.setUp(); |
| 107 | |
| 108 | mServiceContext = new BroadcastInterceptingContext(getContext()); |
| 109 | mStatsDir = getContext().getFilesDir(); |
| 110 | |
| 111 | mNetManager = createMock(INetworkManagementService.class); |
| 112 | mAlarmManager = createMock(IAlarmManager.class); |
| 113 | mTime = createMock(TrustedTime.class); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 114 | mSettings = createMock(NetworkStatsSettings.class); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 115 | mConnManager = createMock(IConnectivityManager.class); |
| 116 | |
| 117 | mService = new NetworkStatsService( |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 118 | mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 119 | mService.bindConnectivityManager(mConnManager); |
| 120 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 121 | expectDefaultSettings(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 122 | expectSystemReady(); |
| 123 | |
| 124 | replay(); |
| 125 | mService.systemReady(); |
| 126 | verifyAndReset(); |
| 127 | |
| 128 | } |
| 129 | |
| 130 | @Override |
| 131 | public void tearDown() throws Exception { |
| 132 | for (File file : mStatsDir.listFiles()) { |
| 133 | file.delete(); |
| 134 | } |
| 135 | |
| 136 | mServiceContext = null; |
| 137 | mStatsDir = null; |
| 138 | |
| 139 | mNetManager = null; |
| 140 | mAlarmManager = null; |
| 141 | mTime = null; |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 142 | mSettings = null; |
| 143 | mConnManager = null; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 144 | |
| 145 | mService = null; |
| 146 | |
| 147 | super.tearDown(); |
| 148 | } |
| 149 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 150 | public void testNetworkStatsWifi() throws Exception { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 151 | long elapsedRealtime = 0; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 152 | |
| 153 | // pretend that wifi network comes online; service should ask about full |
| 154 | // network state, and poll any existing interfaces before updating. |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 155 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 156 | expectDefaultSettings(); |
| 157 | expectNetworkState(buildWifiState()); |
| 158 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 159 | |
| 160 | replay(); |
| 161 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 162 | |
| 163 | // verify service has empty history for wifi |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 164 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 165 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 166 | |
| 167 | // modify some number on wifi, and trigger poll event |
| 168 | elapsedRealtime += HOUR_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 169 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 170 | expectDefaultSettings(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 171 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 172 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 173 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 174 | |
| 175 | replay(); |
| 176 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 177 | |
| 178 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 179 | assertNetworkTotal(sTemplateWifi, 1024L, 2048L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 180 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 181 | |
| 182 | // and bump forward again, with counters going higher. this is |
| 183 | // important, since polling should correctly subtract last snapshot. |
| 184 | elapsedRealtime += DAY_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 185 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 186 | expectDefaultSettings(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 187 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 188 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 4096L, 8192L)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 189 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 190 | |
| 191 | replay(); |
| 192 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 193 | |
| 194 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 195 | assertNetworkTotal(sTemplateWifi, 4096L, 8192L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 196 | verifyAndReset(); |
| 197 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 198 | } |
| 199 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 200 | public void testStatsRebootPersist() throws Exception { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 201 | long elapsedRealtime = 0; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 202 | assertStatsFilesExist(false); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 203 | |
| 204 | // pretend that wifi network comes online; service should ask about full |
| 205 | // network state, and poll any existing interfaces before updating. |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 206 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 207 | expectDefaultSettings(); |
| 208 | expectNetworkState(buildWifiState()); |
| 209 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 210 | |
| 211 | replay(); |
| 212 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 213 | |
| 214 | // verify service has empty history for wifi |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 215 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 216 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 217 | |
| 218 | // modify some number on wifi, and trigger poll event |
| 219 | elapsedRealtime += HOUR_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 220 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 221 | expectDefaultSettings(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 222 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 223 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L)); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 224 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 2) |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 225 | .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 512L, 256L) |
| 226 | .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 128L, 128L)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 227 | |
| 228 | replay(); |
| 229 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 230 | |
| 231 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 232 | assertNetworkTotal(sTemplateWifi, 1024L, 2048L); |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 233 | assertUidTotal(sTemplateWifi, UID_RED, 512L, 256L); |
| 234 | assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 128L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 235 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 236 | |
| 237 | // graceful shutdown system, which should trigger persist of stats, and |
| 238 | // clear any values in memory. |
| 239 | mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN)); |
| 240 | |
| 241 | // talk with zombie service to assert stats have gone; and assert that |
| 242 | // we persisted them to file. |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 243 | expectDefaultSettings(); |
| 244 | replay(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 245 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 246 | verifyAndReset(); |
| 247 | |
| 248 | assertStatsFilesExist(true); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 249 | |
| 250 | // boot through serviceReady() again |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 251 | expectDefaultSettings(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 252 | expectSystemReady(); |
| 253 | |
| 254 | replay(); |
| 255 | mService.systemReady(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 256 | |
| 257 | // after systemReady(), we should have historical stats loaded again |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 258 | assertNetworkTotal(sTemplateWifi, 1024L, 2048L); |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 259 | assertUidTotal(sTemplateWifi, UID_RED, 512L, 256L); |
| 260 | assertUidTotal(sTemplateWifi, UID_BLUE, 128L, 128L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 261 | verifyAndReset(); |
| 262 | |
| 263 | } |
| 264 | |
| 265 | public void testStatsBucketResize() throws Exception { |
| 266 | long elapsedRealtime = 0; |
| 267 | NetworkStatsHistory history = null; |
| 268 | long[] total = null; |
| 269 | |
| 270 | assertStatsFilesExist(false); |
| 271 | |
| 272 | // pretend that wifi network comes online; service should ask about full |
| 273 | // network state, and poll any existing interfaces before updating. |
| 274 | expectTime(TEST_START + elapsedRealtime); |
| 275 | expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); |
| 276 | expectNetworkState(buildWifiState()); |
| 277 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 278 | |
| 279 | replay(); |
| 280 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 281 | verifyAndReset(); |
| 282 | |
| 283 | // modify some number on wifi, and trigger poll event |
| 284 | elapsedRealtime += 2 * HOUR_IN_MILLIS; |
| 285 | expectTime(TEST_START + elapsedRealtime); |
| 286 | expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 287 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 288 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 512L, 512L)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 289 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
| 290 | |
| 291 | replay(); |
| 292 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 293 | |
| 294 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 295 | history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 296 | total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 297 | assertEquals(512L, total[0]); |
| 298 | assertEquals(512L, total[1]); |
| 299 | assertEquals(HOUR_IN_MILLIS, history.bucketDuration); |
| 300 | assertEquals(2, history.bucketCount); |
| 301 | verifyAndReset(); |
| 302 | |
| 303 | // now change bucket duration setting and trigger another poll with |
| 304 | // exact same values, which should resize existing buckets. |
| 305 | expectTime(TEST_START + elapsedRealtime); |
| 306 | expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS); |
| 307 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 308 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
| 309 | |
| 310 | replay(); |
| 311 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 312 | |
| 313 | // verify identical stats, but spread across 4 buckets now |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 314 | history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 315 | total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 316 | assertEquals(512L, total[0]); |
| 317 | assertEquals(512L, total[1]); |
| 318 | assertEquals(30 * MINUTE_IN_MILLIS, history.bucketDuration); |
| 319 | assertEquals(4, history.bucketCount); |
| 320 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 321 | |
| 322 | } |
| 323 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 324 | public void testUidStatsAcrossNetworks() throws Exception { |
| 325 | long elapsedRealtime = 0; |
| 326 | |
| 327 | // pretend first mobile network comes online |
| 328 | expectTime(TEST_START + elapsedRealtime); |
| 329 | expectDefaultSettings(); |
| 330 | expectNetworkState(buildMobile3gState(IMSI_1)); |
| 331 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 332 | |
| 333 | replay(); |
| 334 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 335 | verifyAndReset(); |
| 336 | |
| 337 | // create some traffic on first network |
| 338 | elapsedRealtime += HOUR_IN_MILLIS; |
| 339 | expectTime(TEST_START + elapsedRealtime); |
| 340 | expectDefaultSettings(); |
| 341 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
| 342 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 2048L, 512L)); |
| 343 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 3) |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 344 | .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 1536L, 512L) |
| 345 | .addEntry(TEST_IFACE, UID_RED, 0xF00D, 512L, 512L) |
| 346 | .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 512L, 0L)); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 347 | |
| 348 | replay(); |
| 349 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 350 | |
| 351 | // verify service recorded history |
| 352 | assertNetworkTotal(sTemplateImsi1, 2048L, 512L); |
| 353 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 354 | assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 512L); |
| 355 | assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 0L); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 356 | verifyAndReset(); |
| 357 | |
| 358 | // now switch networks; this also tests that we're okay with interfaces |
| 359 | // disappearing, to verify we don't count backwards. |
| 360 | elapsedRealtime += HOUR_IN_MILLIS; |
| 361 | expectTime(TEST_START + elapsedRealtime); |
| 362 | expectDefaultSettings(); |
| 363 | expectNetworkState(buildMobile3gState(IMSI_2)); |
| 364 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 365 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
| 366 | |
| 367 | replay(); |
| 368 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 369 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 370 | verifyAndReset(); |
| 371 | |
| 372 | // create traffic on second network |
| 373 | elapsedRealtime += HOUR_IN_MILLIS; |
| 374 | expectTime(TEST_START + elapsedRealtime); |
| 375 | expectDefaultSettings(); |
| 376 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
| 377 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 128L, 1024L)); |
| 378 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 379 | .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 128L, 1024L)); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 380 | |
| 381 | replay(); |
| 382 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 383 | |
| 384 | // verify original history still intact |
| 385 | assertNetworkTotal(sTemplateImsi1, 2048L, 512L); |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 386 | assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 512L); |
| 387 | assertUidTotal(sTemplateImsi1, UID_BLUE, 512L, 0L); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 388 | |
| 389 | // and verify new history also recorded under different template, which |
| 390 | // verifies that we didn't cross the streams. |
| 391 | assertNetworkTotal(sTemplateImsi2, 128L, 1024L); |
| 392 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 393 | assertUidTotal(sTemplateImsi2, UID_BLUE, 128L, 1024L); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 394 | verifyAndReset(); |
| 395 | |
| 396 | } |
| 397 | |
| 398 | public void testUidRemovedIsMoved() throws Exception { |
| 399 | long elapsedRealtime = 0; |
| 400 | |
| 401 | // pretend that network comes online |
| 402 | expectTime(TEST_START + elapsedRealtime); |
| 403 | expectDefaultSettings(); |
| 404 | expectNetworkState(buildWifiState()); |
| 405 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 406 | |
| 407 | replay(); |
| 408 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 409 | verifyAndReset(); |
| 410 | |
| 411 | // create some traffic |
| 412 | elapsedRealtime += HOUR_IN_MILLIS; |
| 413 | expectTime(TEST_START + elapsedRealtime); |
| 414 | expectDefaultSettings(); |
| 415 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
| 416 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 4128L, 544L)); |
| 417 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 418 | .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 16L, 16L) |
| 419 | .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 4096L, 512L) |
| 420 | .addEntry(TEST_IFACE, UID_GREEN, TAG_NONE, 16L, 16L)); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 421 | |
| 422 | replay(); |
| 423 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 424 | |
| 425 | // verify service recorded history |
| 426 | assertNetworkTotal(sTemplateWifi, 4128L, 544L); |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 427 | assertUidTotal(sTemplateWifi, UID_RED, 16L, 16L); |
| 428 | assertUidTotal(sTemplateWifi, UID_BLUE, 4096L, 512L); |
| 429 | assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 16L); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 430 | verifyAndReset(); |
| 431 | |
| 432 | // now pretend two UIDs are uninstalled, which should migrate stats to |
| 433 | // special "removed" bucket. |
| 434 | expectDefaultSettings(); |
| 435 | replay(); |
| 436 | final Intent intent = new Intent(ACTION_UID_REMOVED); |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 437 | intent.putExtra(EXTRA_UID, UID_BLUE); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 438 | mServiceContext.sendBroadcast(intent); |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 439 | intent.putExtra(EXTRA_UID, UID_RED); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 440 | mServiceContext.sendBroadcast(intent); |
| 441 | |
| 442 | // existing uid and total should remain unchanged; but removed UID |
| 443 | // should be gone completely. |
| 444 | assertNetworkTotal(sTemplateWifi, 4128L, 544L); |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 445 | assertUidTotal(sTemplateWifi, UID_RED, 0L, 0L); |
| 446 | assertUidTotal(sTemplateWifi, UID_BLUE, 0L, 0L); |
| 447 | assertUidTotal(sTemplateWifi, UID_GREEN, 16L, 16L); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 448 | assertUidTotal(sTemplateWifi, UID_REMOVED, 4112L, 528L); |
| 449 | verifyAndReset(); |
| 450 | |
| 451 | } |
| 452 | |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 453 | public void testUid3g4gCombinedByTemplate() throws Exception { |
| 454 | long elapsedRealtime = 0; |
| 455 | |
| 456 | // pretend that network comes online |
| 457 | expectTime(TEST_START + elapsedRealtime); |
| 458 | expectDefaultSettings(); |
| 459 | expectNetworkState(buildMobile3gState(IMSI_1)); |
| 460 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 461 | |
| 462 | replay(); |
| 463 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 464 | verifyAndReset(); |
| 465 | |
| 466 | // create some traffic |
| 467 | elapsedRealtime += HOUR_IN_MILLIS; |
| 468 | expectTime(TEST_START + elapsedRealtime); |
| 469 | expectDefaultSettings(); |
| 470 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 471 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) |
| 472 | .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 1024L, 1024L) |
| 473 | .addEntry(TEST_IFACE, UID_RED, 0xF00D, 512L, 512L)); |
| 474 | |
| 475 | replay(); |
| 476 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 477 | |
| 478 | // verify service recorded history |
| 479 | assertUidTotal(sTemplateImsi1, UID_RED, 1024L, 1024L); |
| 480 | verifyAndReset(); |
| 481 | |
| 482 | // now switch over to 4g network |
| 483 | elapsedRealtime += HOUR_IN_MILLIS; |
| 484 | expectTime(TEST_START + elapsedRealtime); |
| 485 | expectDefaultSettings(); |
| 486 | expectNetworkState(buildMobile4gState()); |
| 487 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 488 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
| 489 | |
| 490 | replay(); |
| 491 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 492 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 493 | verifyAndReset(); |
| 494 | |
| 495 | // create traffic on second network |
| 496 | elapsedRealtime += HOUR_IN_MILLIS; |
| 497 | expectTime(TEST_START + elapsedRealtime); |
| 498 | expectDefaultSettings(); |
| 499 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 500 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) |
| 501 | .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 512L, 256L)); |
| 502 | |
| 503 | replay(); |
| 504 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 505 | |
| 506 | // verify that ALL_MOBILE template combines both |
| 507 | assertUidTotal(sTemplateImsi1, UID_RED, 1536L, 1280L); |
| 508 | |
| 509 | verifyAndReset(); |
| 510 | |
| 511 | } |
| 512 | |
| 513 | public void testPackedUidAndTag() throws Exception { |
| 514 | assertEquals(0x0000000000000000L, packUidAndTag(0, 0x0)); |
| 515 | assertEquals(0x000003E900000000L, packUidAndTag(1001, 0x0)); |
| 516 | assertEquals(0x000003E90000F00DL, packUidAndTag(1001, 0xF00D)); |
| 517 | |
| 518 | long packed; |
| 519 | packed = packUidAndTag(Integer.MAX_VALUE, Integer.MIN_VALUE); |
| 520 | assertEquals(Integer.MAX_VALUE, unpackUid(packed)); |
| 521 | assertEquals(Integer.MIN_VALUE, unpackTag(packed)); |
| 522 | |
| 523 | packed = packUidAndTag(Integer.MIN_VALUE, Integer.MAX_VALUE); |
| 524 | assertEquals(Integer.MIN_VALUE, unpackUid(packed)); |
| 525 | assertEquals(Integer.MAX_VALUE, unpackTag(packed)); |
| 526 | |
| 527 | packed = packUidAndTag(10005, 0xFFFFFFFF); |
| 528 | assertEquals(10005, unpackUid(packed)); |
| 529 | assertEquals(0xFFFFFFFF, unpackTag(packed)); |
| 530 | |
| 531 | } |
| 532 | |
| 533 | public void testSummaryForAllUid() throws Exception { |
| 534 | long elapsedRealtime = 0; |
| 535 | |
| 536 | // pretend that network comes online |
| 537 | expectTime(TEST_START + elapsedRealtime); |
| 538 | expectDefaultSettings(); |
| 539 | expectNetworkState(buildWifiState()); |
| 540 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 541 | |
| 542 | replay(); |
| 543 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 544 | verifyAndReset(); |
| 545 | |
| 546 | // create some traffic for two apps |
| 547 | elapsedRealtime += HOUR_IN_MILLIS; |
| 548 | expectTime(TEST_START + elapsedRealtime); |
| 549 | expectDefaultSettings(); |
| 550 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 551 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) |
| 552 | .addEntry(TEST_IFACE, UID_RED, TAG_NONE, 50L, 50L) |
| 553 | .addEntry(TEST_IFACE, UID_RED, 0xF00D, 10L, 10L) |
| 554 | .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 1024L, 512L)); |
| 555 | |
| 556 | replay(); |
| 557 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 558 | |
| 559 | // verify service recorded history |
| 560 | assertUidTotal(sTemplateWifi, UID_RED, 50L, 50L); |
| 561 | assertUidTotal(sTemplateWifi, UID_BLUE, 1024L, 512L); |
| 562 | verifyAndReset(); |
| 563 | |
| 564 | // now create more traffic in next hour, but only for one app |
| 565 | elapsedRealtime += HOUR_IN_MILLIS; |
| 566 | expectTime(TEST_START + elapsedRealtime); |
| 567 | expectDefaultSettings(); |
| 568 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 569 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 1) |
| 570 | .addEntry(TEST_IFACE, UID_BLUE, TAG_NONE, 2048L, 1024L)); |
| 571 | |
| 572 | replay(); |
| 573 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 574 | |
| 575 | // first verify entire history present |
| 576 | NetworkStats stats = mService.getSummaryForAllUid( |
| 577 | sTemplateWifi, Long.MIN_VALUE, Long.MAX_VALUE, true); |
| 578 | assertEquals(3, stats.size); |
| 579 | assertStatsEntry(stats, 0, IFACE_ALL, UID_RED, TAG_NONE, 50L, 50L); |
| 580 | assertStatsEntry(stats, 1, IFACE_ALL, UID_RED, 0xF00D, 10L, 10L); |
| 581 | assertStatsEntry(stats, 2, IFACE_ALL, UID_BLUE, TAG_NONE, 2048L, 1024L); |
| 582 | |
| 583 | // now verify that recent history only contains one uid |
| 584 | final long currentTime = TEST_START + elapsedRealtime; |
| 585 | stats = mService.getSummaryForAllUid( |
| 586 | sTemplateWifi, currentTime - HOUR_IN_MILLIS, currentTime, true); |
| 587 | assertEquals(1, stats.size); |
| 588 | assertStatsEntry(stats, 0, IFACE_ALL, UID_BLUE, TAG_NONE, 1024L, 512L); |
| 589 | |
| 590 | verifyAndReset(); |
| 591 | } |
| 592 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 593 | private void assertNetworkTotal(NetworkTemplate template, long rx, long tx) { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 594 | final NetworkStatsHistory history = mService.getHistoryForNetwork(template); |
| 595 | final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 596 | assertEquals(rx, total[0]); |
| 597 | assertEquals(tx, total[1]); |
| 598 | } |
| 599 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 600 | private void assertUidTotal(NetworkTemplate template, int uid, long rx, long tx) { |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 601 | final NetworkStatsHistory history = mService.getHistoryForUid(template, uid, TAG_NONE); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 602 | final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 603 | assertEquals(rx, total[0]); |
| 604 | assertEquals(tx, total[1]); |
| 605 | } |
| 606 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 607 | private void expectSystemReady() throws Exception { |
| 608 | mAlarmManager.remove(isA(PendingIntent.class)); |
| 609 | expectLastCall().anyTimes(); |
| 610 | |
| 611 | mAlarmManager.setInexactRepeating( |
| 612 | eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), isA(PendingIntent.class)); |
| 613 | expectLastCall().atLeastOnce(); |
| 614 | } |
| 615 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 616 | private void expectNetworkState(NetworkState... state) throws Exception { |
| 617 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
| 618 | } |
| 619 | |
| 620 | private void expectNetworkStatsSummary(NetworkStats summary) throws Exception { |
| 621 | expect(mNetManager.getNetworkStatsSummary()).andReturn(summary).atLeastOnce(); |
| 622 | } |
| 623 | |
| 624 | private void expectNetworkStatsDetail(NetworkStats detail) throws Exception { |
| 625 | expect(mNetManager.getNetworkStatsDetail()).andReturn(detail).atLeastOnce(); |
| 626 | } |
| 627 | |
| 628 | private void expectDefaultSettings() throws Exception { |
| 629 | expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); |
| 630 | } |
| 631 | |
| 632 | private void expectSettings(long persistThreshold, long bucketDuration, long maxHistory) |
| 633 | throws Exception { |
| 634 | expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes(); |
| 635 | expect(mSettings.getPersistThreshold()).andReturn(persistThreshold).anyTimes(); |
| 636 | expect(mSettings.getNetworkBucketDuration()).andReturn(bucketDuration).anyTimes(); |
| 637 | expect(mSettings.getNetworkMaxHistory()).andReturn(maxHistory).anyTimes(); |
| 638 | expect(mSettings.getUidBucketDuration()).andReturn(bucketDuration).anyTimes(); |
| 639 | expect(mSettings.getUidMaxHistory()).andReturn(maxHistory).anyTimes(); |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 640 | expect(mSettings.getTagMaxHistory()).andReturn(maxHistory).anyTimes(); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 641 | expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes(); |
| 642 | } |
| 643 | |
| 644 | private void expectTime(long currentTime) throws Exception { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 645 | expect(mTime.forceRefresh()).andReturn(false).anyTimes(); |
| 646 | expect(mTime.hasCache()).andReturn(true).anyTimes(); |
| 647 | expect(mTime.currentTimeMillis()).andReturn(currentTime).anyTimes(); |
| 648 | expect(mTime.getCacheAge()).andReturn(0L).anyTimes(); |
| 649 | expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes(); |
| 650 | } |
| 651 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 652 | private void assertStatsFilesExist(boolean exist) { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 653 | final File networkFile = new File(mStatsDir, "netstats.bin"); |
| 654 | final File uidFile = new File(mStatsDir, "netstats_uid.bin"); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 655 | if (exist) { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 656 | assertTrue(networkFile.exists()); |
| 657 | assertTrue(uidFile.exists()); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 658 | } else { |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 659 | assertFalse(networkFile.exists()); |
| 660 | assertFalse(uidFile.exists()); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 661 | } |
| 662 | } |
| 663 | |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 664 | private static void assertStatsEntry( |
| 665 | NetworkStats stats, int i, String iface, int uid, int tag, long rx, long tx) { |
| 666 | assertEquals(iface, stats.iface[i]); |
| 667 | assertEquals(uid, stats.uid[i]); |
| 668 | assertEquals(tag, stats.tag[i]); |
| 669 | assertEquals(rx, stats.rx[i]); |
| 670 | assertEquals(tx, stats.tx[i]); |
| 671 | } |
| 672 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 673 | private static NetworkState buildWifiState() { |
| 674 | final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null); |
| 675 | info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 676 | final LinkProperties prop = new LinkProperties(); |
| 677 | prop.setInterfaceName(TEST_IFACE); |
| 678 | return new NetworkState(info, prop, null); |
| 679 | } |
| 680 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 681 | private static NetworkState buildMobile3gState(String subscriberId) { |
| 682 | final NetworkInfo info = new NetworkInfo( |
| 683 | TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null); |
| 684 | info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 685 | final LinkProperties prop = new LinkProperties(); |
| 686 | prop.setInterfaceName(TEST_IFACE); |
| 687 | return new NetworkState(info, prop, null, subscriberId); |
| 688 | } |
| 689 | |
Jeff Sharkey | d03fd3f | 2011-06-19 20:55:09 -0700 | [diff] [blame^] | 690 | private static NetworkState buildMobile4gState() { |
| 691 | final NetworkInfo info = new NetworkInfo(TYPE_WIMAX, 0, null, null); |
| 692 | info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 693 | final LinkProperties prop = new LinkProperties(); |
| 694 | prop.setInterfaceName(TEST_IFACE); |
| 695 | return new NetworkState(info, prop, null); |
| 696 | } |
| 697 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 698 | private static NetworkStats buildEmptyStats(long elapsedRealtime) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 699 | return new NetworkStats(elapsedRealtime, 0); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 700 | } |
| 701 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 702 | private void replay() { |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 703 | EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | private void verifyAndReset() { |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 707 | EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); |
| 708 | EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 709 | } |
| 710 | } |