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 | |
| 19 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
| 20 | import static android.net.ConnectivityManager.TYPE_WIFI; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 21 | import static android.net.NetworkStats.TAG_NONE; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 22 | import static android.net.NetworkStats.UID_ALL; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 23 | import static android.net.NetworkTemplate.MATCH_WIFI; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 24 | import static android.text.format.DateUtils.DAY_IN_MILLIS; |
| 25 | import static android.text.format.DateUtils.HOUR_IN_MILLIS; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 26 | import static android.text.format.DateUtils.MINUTE_IN_MILLIS; |
| 27 | import static android.text.format.DateUtils.WEEK_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 28 | import static com.android.server.net.NetworkStatsService.ACTION_NETWORK_STATS_POLL; |
| 29 | import static org.easymock.EasyMock.anyLong; |
| 30 | import static org.easymock.EasyMock.createMock; |
| 31 | import static org.easymock.EasyMock.eq; |
| 32 | import static org.easymock.EasyMock.expect; |
| 33 | import static org.easymock.EasyMock.expectLastCall; |
| 34 | import static org.easymock.EasyMock.isA; |
| 35 | |
| 36 | import android.app.AlarmManager; |
| 37 | import android.app.IAlarmManager; |
| 38 | import android.app.PendingIntent; |
| 39 | import android.content.Intent; |
| 40 | import android.net.IConnectivityManager; |
| 41 | import android.net.LinkProperties; |
| 42 | import android.net.NetworkInfo; |
| 43 | import android.net.NetworkInfo.DetailedState; |
| 44 | import android.net.NetworkState; |
| 45 | import android.net.NetworkStats; |
| 46 | import android.net.NetworkStatsHistory; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 47 | import android.net.NetworkTemplate; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 48 | import android.os.INetworkManagementService; |
| 49 | import android.test.AndroidTestCase; |
| 50 | import android.test.suitebuilder.annotation.LargeTest; |
| 51 | import android.util.TrustedTime; |
| 52 | |
| 53 | import com.android.server.net.NetworkStatsService; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 54 | import com.android.server.net.NetworkStatsService.NetworkStatsSettings; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 55 | |
| 56 | import org.easymock.EasyMock; |
| 57 | |
| 58 | import java.io.File; |
| 59 | |
| 60 | /** |
| 61 | * Tests for {@link NetworkStatsService}. |
| 62 | */ |
| 63 | @LargeTest |
| 64 | public class NetworkStatsServiceTest extends AndroidTestCase { |
| 65 | private static final String TAG = "NetworkStatsServiceTest"; |
| 66 | |
| 67 | private static final String TEST_IFACE = "test0"; |
| 68 | private static final long TEST_START = 1194220800000L; |
| 69 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 70 | private static NetworkTemplate sTemplateWifi = new NetworkTemplate(MATCH_WIFI, null); |
| 71 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 72 | private static final int TEST_UID_1 = 1001; |
| 73 | private static final int TEST_UID_2 = 1002; |
| 74 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 75 | private BroadcastInterceptingContext mServiceContext; |
| 76 | private File mStatsDir; |
| 77 | |
| 78 | private INetworkManagementService mNetManager; |
| 79 | private IAlarmManager mAlarmManager; |
| 80 | private TrustedTime mTime; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 81 | private NetworkStatsSettings mSettings; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 82 | private IConnectivityManager mConnManager; |
| 83 | |
| 84 | private NetworkStatsService mService; |
| 85 | |
| 86 | @Override |
| 87 | public void setUp() throws Exception { |
| 88 | super.setUp(); |
| 89 | |
| 90 | mServiceContext = new BroadcastInterceptingContext(getContext()); |
| 91 | mStatsDir = getContext().getFilesDir(); |
| 92 | |
| 93 | mNetManager = createMock(INetworkManagementService.class); |
| 94 | mAlarmManager = createMock(IAlarmManager.class); |
| 95 | mTime = createMock(TrustedTime.class); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 96 | mSettings = createMock(NetworkStatsSettings.class); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 97 | mConnManager = createMock(IConnectivityManager.class); |
| 98 | |
| 99 | mService = new NetworkStatsService( |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 100 | mServiceContext, mNetManager, mAlarmManager, mTime, mStatsDir, mSettings); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 101 | mService.bindConnectivityManager(mConnManager); |
| 102 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 103 | expectDefaultSettings(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 104 | expectSystemReady(); |
| 105 | |
| 106 | replay(); |
| 107 | mService.systemReady(); |
| 108 | verifyAndReset(); |
| 109 | |
| 110 | } |
| 111 | |
| 112 | @Override |
| 113 | public void tearDown() throws Exception { |
| 114 | for (File file : mStatsDir.listFiles()) { |
| 115 | file.delete(); |
| 116 | } |
| 117 | |
| 118 | mServiceContext = null; |
| 119 | mStatsDir = null; |
| 120 | |
| 121 | mNetManager = null; |
| 122 | mAlarmManager = null; |
| 123 | mTime = null; |
| 124 | |
| 125 | mService = null; |
| 126 | |
| 127 | super.tearDown(); |
| 128 | } |
| 129 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 130 | public void testSummaryStatsWifi() throws Exception { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 131 | long elapsedRealtime = 0; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 132 | |
| 133 | // pretend that wifi network comes online; service should ask about full |
| 134 | // network state, and poll any existing interfaces before updating. |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 135 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 136 | expectDefaultSettings(); |
| 137 | expectNetworkState(buildWifiState()); |
| 138 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 139 | |
| 140 | replay(); |
| 141 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 142 | |
| 143 | // verify service has empty history for wifi |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 144 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 145 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 146 | |
| 147 | // modify some number on wifi, and trigger poll event |
| 148 | elapsedRealtime += HOUR_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 149 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 150 | expectDefaultSettings(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 151 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 152 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 153 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 154 | |
| 155 | replay(); |
| 156 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 157 | |
| 158 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 159 | assertNetworkTotal(sTemplateWifi, 1024L, 2048L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 160 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 161 | |
| 162 | // and bump forward again, with counters going higher. this is |
| 163 | // important, since polling should correctly subtract last snapshot. |
| 164 | elapsedRealtime += DAY_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 165 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 166 | expectDefaultSettings(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 167 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 168 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 4096L, 8192L)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 169 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 170 | |
| 171 | replay(); |
| 172 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 173 | |
| 174 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 175 | assertNetworkTotal(sTemplateWifi, 4096L, 8192L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 176 | verifyAndReset(); |
| 177 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 180 | public void testStatsRebootPersist() throws Exception { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 181 | long elapsedRealtime = 0; |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 182 | assertStatsFilesExist(false); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 183 | |
| 184 | // pretend that wifi network comes online; service should ask about full |
| 185 | // network state, and poll any existing interfaces before updating. |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 186 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 187 | expectDefaultSettings(); |
| 188 | expectNetworkState(buildWifiState()); |
| 189 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 190 | |
| 191 | replay(); |
| 192 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 193 | |
| 194 | // verify service has empty history for wifi |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 195 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 196 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 197 | |
| 198 | // modify some number on wifi, and trigger poll event |
| 199 | elapsedRealtime += HOUR_IN_MILLIS; |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 200 | expectTime(TEST_START + elapsedRealtime); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 201 | expectDefaultSettings(); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 202 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 203 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 1024L, 2048L)); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 204 | expectNetworkStatsDetail(new NetworkStats(elapsedRealtime, 2) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 205 | .addEntry(TEST_IFACE, TEST_UID_1, TAG_NONE, 512L, 256L) |
| 206 | .addEntry(TEST_IFACE, TEST_UID_2, TAG_NONE, 128L, 128L)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 207 | |
| 208 | replay(); |
| 209 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 210 | |
| 211 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 212 | assertNetworkTotal(sTemplateWifi, 1024L, 2048L); |
| 213 | assertUidTotal(sTemplateWifi, TEST_UID_1, 512L, 256L); |
| 214 | assertUidTotal(sTemplateWifi, TEST_UID_2, 128L, 128L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 215 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 216 | |
| 217 | // graceful shutdown system, which should trigger persist of stats, and |
| 218 | // clear any values in memory. |
| 219 | mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SHUTDOWN)); |
| 220 | |
| 221 | // talk with zombie service to assert stats have gone; and assert that |
| 222 | // we persisted them to file. |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 223 | expectDefaultSettings(); |
| 224 | replay(); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 225 | assertNetworkTotal(sTemplateWifi, 0L, 0L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 226 | verifyAndReset(); |
| 227 | |
| 228 | assertStatsFilesExist(true); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 229 | |
| 230 | // boot through serviceReady() again |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 231 | expectDefaultSettings(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 232 | expectSystemReady(); |
| 233 | |
| 234 | replay(); |
| 235 | mService.systemReady(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 236 | |
| 237 | // after systemReady(), we should have historical stats loaded again |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 238 | assertNetworkTotal(sTemplateWifi, 1024L, 2048L); |
| 239 | assertUidTotal(sTemplateWifi, TEST_UID_1, 512L, 256L); |
| 240 | assertUidTotal(sTemplateWifi, TEST_UID_2, 128L, 128L); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 241 | verifyAndReset(); |
| 242 | |
| 243 | } |
| 244 | |
| 245 | public void testStatsBucketResize() throws Exception { |
| 246 | long elapsedRealtime = 0; |
| 247 | NetworkStatsHistory history = null; |
| 248 | long[] total = null; |
| 249 | |
| 250 | assertStatsFilesExist(false); |
| 251 | |
| 252 | // pretend that wifi network comes online; service should ask about full |
| 253 | // network state, and poll any existing interfaces before updating. |
| 254 | expectTime(TEST_START + elapsedRealtime); |
| 255 | expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); |
| 256 | expectNetworkState(buildWifiState()); |
| 257 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 258 | |
| 259 | replay(); |
| 260 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
| 261 | verifyAndReset(); |
| 262 | |
| 263 | // modify some number on wifi, and trigger poll event |
| 264 | elapsedRealtime += 2 * HOUR_IN_MILLIS; |
| 265 | expectTime(TEST_START + elapsedRealtime); |
| 266 | expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 267 | expectNetworkStatsSummary(new NetworkStats(elapsedRealtime, 1) |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 268 | .addEntry(TEST_IFACE, UID_ALL, TAG_NONE, 512L, 512L)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 269 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
| 270 | |
| 271 | replay(); |
| 272 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 273 | |
| 274 | // verify service recorded history |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 275 | history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 276 | total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 277 | assertEquals(512L, total[0]); |
| 278 | assertEquals(512L, total[1]); |
| 279 | assertEquals(HOUR_IN_MILLIS, history.bucketDuration); |
| 280 | assertEquals(2, history.bucketCount); |
| 281 | verifyAndReset(); |
| 282 | |
| 283 | // now change bucket duration setting and trigger another poll with |
| 284 | // exact same values, which should resize existing buckets. |
| 285 | expectTime(TEST_START + elapsedRealtime); |
| 286 | expectSettings(0L, 30 * MINUTE_IN_MILLIS, WEEK_IN_MILLIS); |
| 287 | expectNetworkStatsSummary(buildEmptyStats(elapsedRealtime)); |
| 288 | expectNetworkStatsDetail(buildEmptyStats(elapsedRealtime)); |
| 289 | |
| 290 | replay(); |
| 291 | mServiceContext.sendBroadcast(new Intent(ACTION_NETWORK_STATS_POLL)); |
| 292 | |
| 293 | // verify identical stats, but spread across 4 buckets now |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 294 | history = mService.getHistoryForNetwork(new NetworkTemplate(MATCH_WIFI, null)); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 295 | total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 296 | assertEquals(512L, total[0]); |
| 297 | assertEquals(512L, total[1]); |
| 298 | assertEquals(30 * MINUTE_IN_MILLIS, history.bucketDuration); |
| 299 | assertEquals(4, history.bucketCount); |
| 300 | verifyAndReset(); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 301 | |
| 302 | } |
| 303 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 304 | private void assertNetworkTotal(NetworkTemplate template, long rx, long tx) { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 305 | final NetworkStatsHistory history = mService.getHistoryForNetwork(template); |
| 306 | final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 307 | assertEquals(rx, total[0]); |
| 308 | assertEquals(tx, total[1]); |
| 309 | } |
| 310 | |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame^] | 311 | private void assertUidTotal(NetworkTemplate template, int uid, long rx, long tx) { |
| 312 | final NetworkStatsHistory history = mService.getHistoryForUid(template, uid); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 313 | final long[] total = history.getTotalData(Long.MIN_VALUE, Long.MAX_VALUE, null); |
| 314 | assertEquals(rx, total[0]); |
| 315 | assertEquals(tx, total[1]); |
| 316 | } |
| 317 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 318 | private void expectSystemReady() throws Exception { |
| 319 | mAlarmManager.remove(isA(PendingIntent.class)); |
| 320 | expectLastCall().anyTimes(); |
| 321 | |
| 322 | mAlarmManager.setInexactRepeating( |
| 323 | eq(AlarmManager.ELAPSED_REALTIME), anyLong(), anyLong(), isA(PendingIntent.class)); |
| 324 | expectLastCall().atLeastOnce(); |
| 325 | } |
| 326 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 327 | private void expectNetworkState(NetworkState... state) throws Exception { |
| 328 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
| 329 | } |
| 330 | |
| 331 | private void expectNetworkStatsSummary(NetworkStats summary) throws Exception { |
| 332 | expect(mNetManager.getNetworkStatsSummary()).andReturn(summary).atLeastOnce(); |
| 333 | } |
| 334 | |
| 335 | private void expectNetworkStatsDetail(NetworkStats detail) throws Exception { |
| 336 | expect(mNetManager.getNetworkStatsDetail()).andReturn(detail).atLeastOnce(); |
| 337 | } |
| 338 | |
| 339 | private void expectDefaultSettings() throws Exception { |
| 340 | expectSettings(0L, HOUR_IN_MILLIS, WEEK_IN_MILLIS); |
| 341 | } |
| 342 | |
| 343 | private void expectSettings(long persistThreshold, long bucketDuration, long maxHistory) |
| 344 | throws Exception { |
| 345 | expect(mSettings.getPollInterval()).andReturn(HOUR_IN_MILLIS).anyTimes(); |
| 346 | expect(mSettings.getPersistThreshold()).andReturn(persistThreshold).anyTimes(); |
| 347 | expect(mSettings.getNetworkBucketDuration()).andReturn(bucketDuration).anyTimes(); |
| 348 | expect(mSettings.getNetworkMaxHistory()).andReturn(maxHistory).anyTimes(); |
| 349 | expect(mSettings.getUidBucketDuration()).andReturn(bucketDuration).anyTimes(); |
| 350 | expect(mSettings.getUidMaxHistory()).andReturn(maxHistory).anyTimes(); |
| 351 | expect(mSettings.getTimeCacheMaxAge()).andReturn(DAY_IN_MILLIS).anyTimes(); |
| 352 | } |
| 353 | |
| 354 | private void expectTime(long currentTime) throws Exception { |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 355 | expect(mTime.forceRefresh()).andReturn(false).anyTimes(); |
| 356 | expect(mTime.hasCache()).andReturn(true).anyTimes(); |
| 357 | expect(mTime.currentTimeMillis()).andReturn(currentTime).anyTimes(); |
| 358 | expect(mTime.getCacheAge()).andReturn(0L).anyTimes(); |
| 359 | expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes(); |
| 360 | } |
| 361 | |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 362 | private void assertStatsFilesExist(boolean exist) { |
| 363 | final File summaryFile = new File(mStatsDir, "netstats.bin"); |
| 364 | final File detailFile = new File(mStatsDir, "netstats_uid.bin"); |
| 365 | if (exist) { |
| 366 | assertTrue(summaryFile.exists()); |
| 367 | assertTrue(detailFile.exists()); |
| 368 | } else { |
| 369 | assertFalse(summaryFile.exists()); |
| 370 | assertFalse(detailFile.exists()); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | private static NetworkState buildWifiState() { |
| 375 | final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null); |
| 376 | info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 377 | final LinkProperties prop = new LinkProperties(); |
| 378 | prop.setInterfaceName(TEST_IFACE); |
| 379 | return new NetworkState(info, prop, null); |
| 380 | } |
| 381 | |
| 382 | private static NetworkStats buildEmptyStats(long elapsedRealtime) { |
Jeff Sharkey | 4a97122 | 2011-06-11 22:16:55 -0700 | [diff] [blame] | 383 | return new NetworkStats(elapsedRealtime, 0); |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 384 | } |
| 385 | |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 386 | private void replay() { |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 387 | EasyMock.replay(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | private void verifyAndReset() { |
Jeff Sharkey | 39ebc21 | 2011-06-11 17:25:42 -0700 | [diff] [blame] | 391 | EasyMock.verify(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); |
| 392 | EasyMock.reset(mNetManager, mAlarmManager, mTime, mSettings, mConnManager); |
Jeff Sharkey | 3f39135 | 2011-06-05 17:42:53 -0700 | [diff] [blame] | 393 | } |
| 394 | } |