blob: 167b6c29fd10611cb4e3f58cc0f828141aa7cada [file] [log] [blame]
Jeff Sharkey9599cc52011-05-22 14:59:31 -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 Sharkey21c9c452011-06-07 12:26:43 -070022import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070025import static android.net.NetworkPolicyManager.POLICY_NONE;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070026import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070027import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070028import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
Jeff Sharkeycd2ca402011-06-10 15:14:07 -070029import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070030import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkey241dde22012-02-03 14:50:07 -080031import static android.net.TrafficStats.KB_IN_BYTES;
32import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070033import static android.text.format.DateUtils.DAY_IN_MILLIS;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070034import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
Jeff Sharkey9bf31502012-03-09 17:07:21 -080035import static android.text.format.Time.TIMEZONE_UTC;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070036import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT;
37import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT_SNOOZED;
38import static com.android.server.net.NetworkPolicyManagerService.TYPE_WARNING;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -070039import static org.easymock.EasyMock.anyInt;
Jeff Sharkey0cf6de02012-05-04 15:03:30 -070040import static org.easymock.EasyMock.anyLong;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070041import static org.easymock.EasyMock.aryEq;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070042import static org.easymock.EasyMock.capture;
43import static org.easymock.EasyMock.createMock;
44import static org.easymock.EasyMock.eq;
45import static org.easymock.EasyMock.expect;
46import static org.easymock.EasyMock.expectLastCall;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -070047import static org.easymock.EasyMock.isA;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070048
49import android.app.IActivityManager;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -070050import android.app.INotificationManager;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070051import android.app.IProcessObserver;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070052import android.app.Notification;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070053import android.content.Intent;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070054import android.content.pm.PackageInfo;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070055import android.content.pm.PackageManager;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070056import android.content.pm.Signature;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070057import android.net.ConnectivityManager;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070058import android.net.IConnectivityManager;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070059import android.net.INetworkManagementEventObserver;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070060import android.net.INetworkPolicyListener;
Jeff Sharkey75279902011-05-24 18:39:45 -070061import android.net.INetworkStatsService;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070062import android.net.LinkProperties;
63import android.net.NetworkInfo;
64import android.net.NetworkInfo.DetailedState;
65import android.net.NetworkPolicy;
66import android.net.NetworkState;
67import android.net.NetworkStats;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070068import android.net.NetworkTemplate;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070069import android.os.Binder;
Ashish Sharma50fd36d2011-06-15 19:34:53 -070070import android.os.INetworkManagementService;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070071import android.os.IPowerManager;
Jeff Sharkey163e6442011-10-31 16:37:52 -070072import android.os.MessageQueue.IdleHandler;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070073import android.os.UserHandle;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070074import android.test.AndroidTestCase;
75import android.test.mock.MockPackageManager;
76import android.test.suitebuilder.annotation.LargeTest;
77import android.test.suitebuilder.annotation.Suppress;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070078import android.text.format.Time;
79import android.util.TrustedTime;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070080
81import com.android.server.net.NetworkPolicyManagerService;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070082import com.google.common.util.concurrent.AbstractFuture;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070083
84import org.easymock.Capture;
85import org.easymock.EasyMock;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070086import org.easymock.IAnswer;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070087
Jeff Sharkey21c9c452011-06-07 12:26:43 -070088import java.io.File;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070089import java.util.LinkedHashSet;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070090import java.util.concurrent.ExecutionException;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070091import java.util.concurrent.Future;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070092import java.util.concurrent.TimeUnit;
93import java.util.concurrent.TimeoutException;
Jeff Sharkey163e6442011-10-31 16:37:52 -070094import java.util.logging.Handler;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070095
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -070096import libcore.io.IoUtils;
97
Jeff Sharkey9599cc52011-05-22 14:59:31 -070098/**
99 * Tests for {@link NetworkPolicyManagerService}.
100 */
101@LargeTest
102public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
103 private static final String TAG = "NetworkPolicyManagerServiceTest";
104
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700105 private static final long TEST_START = 1194220800000L;
106 private static final String TEST_IFACE = "test0";
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700107 private static final String TEST_SSID = "AndroidAP";
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700108
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700109 private static NetworkTemplate sTemplateWifi = NetworkTemplate.buildTemplateWifi(TEST_SSID);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700110
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700111 private BroadcastInterceptingContext mServiceContext;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700112 private File mPolicyDir;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700113
114 private IActivityManager mActivityManager;
115 private IPowerManager mPowerManager;
Jeff Sharkey75279902011-05-24 18:39:45 -0700116 private INetworkStatsService mStatsService;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700117 private INetworkManagementService mNetworkManager;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700118 private INetworkPolicyListener mPolicyListener;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700119 private TrustedTime mTime;
120 private IConnectivityManager mConnManager;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700121 private INotificationManager mNotifManager;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700122
123 private NetworkPolicyManagerService mService;
124 private IProcessObserver mProcessObserver;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700125 private INetworkManagementEventObserver mNetworkObserver;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700126
127 private Binder mStubBinder = new Binder();
128
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700129 private long mStartTime;
130 private long mElapsedRealtime;
131
Jeff Sharkeycae04a22012-03-22 15:18:52 -0700132 private static final int USER_ID = 0;
Jeff Sharkeycae04a22012-03-22 15:18:52 -0700133
134 private static final int APP_ID_A = android.os.Process.FIRST_APPLICATION_UID + 800;
135 private static final int APP_ID_B = android.os.Process.FIRST_APPLICATION_UID + 801;
136
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700137 private static final int UID_A = UserHandle.getUid(USER_ID, APP_ID_A);
138 private static final int UID_B = UserHandle.getUid(USER_ID, APP_ID_B);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700139
140 private static final int PID_1 = 400;
141 private static final int PID_2 = 401;
142 private static final int PID_3 = 402;
143
144 @Override
145 public void setUp() throws Exception {
146 super.setUp();
147
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700148 setCurrentTimeMillis(TEST_START);
149
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700150 // intercept various broadcasts, and pretend that uids have packages
151 mServiceContext = new BroadcastInterceptingContext(getContext()) {
152 @Override
153 public PackageManager getPackageManager() {
154 return new MockPackageManager() {
155 @Override
156 public String[] getPackagesForUid(int uid) {
157 return new String[] { "com.example" };
158 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700159
160 @Override
161 public PackageInfo getPackageInfo(String packageName, int flags) {
162 final PackageInfo info = new PackageInfo();
163 final Signature signature;
164 if ("android".equals(packageName)) {
165 signature = new Signature("F00D");
166 } else {
167 signature = new Signature("DEAD");
168 }
169 info.signatures = new Signature[] { signature };
170 return info;
171 }
Jeff Sharkeycae04a22012-03-22 15:18:52 -0700172
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700173 };
174 }
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700175
176 @Override
177 public void startActivity(Intent intent) {
178 // ignored
179 }
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700180 };
181
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700182 mPolicyDir = getContext().getFilesDir();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700183 if (mPolicyDir.exists()) {
184 IoUtils.deleteContents(mPolicyDir);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700185 }
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700186
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700187 mActivityManager = createMock(IActivityManager.class);
188 mPowerManager = createMock(IPowerManager.class);
Jeff Sharkey75279902011-05-24 18:39:45 -0700189 mStatsService = createMock(INetworkStatsService.class);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700190 mNetworkManager = createMock(INetworkManagementService.class);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700191 mPolicyListener = createMock(INetworkPolicyListener.class);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700192 mTime = createMock(TrustedTime.class);
193 mConnManager = createMock(IConnectivityManager.class);
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700194 mNotifManager = createMock(INotificationManager.class);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700195
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700196 mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mPowerManager,
197 mStatsService, mNetworkManager, mTime, mPolicyDir, true);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700198 mService.bindConnectivityManager(mConnManager);
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700199 mService.bindNotificationManager(mNotifManager);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700200
201 // RemoteCallbackList needs a binder to use as key
202 expect(mPolicyListener.asBinder()).andReturn(mStubBinder).atLeastOnce();
203 replay();
204 mService.registerListener(mPolicyListener);
205 verifyAndReset();
206
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700207 // catch IProcessObserver during systemReady()
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700208 final Capture<IProcessObserver> processObserver = new Capture<IProcessObserver>();
209 mActivityManager.registerProcessObserver(capture(processObserver));
210 expectLastCall().atLeastOnce();
211
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700212 // catch INetworkManagementEventObserver during systemReady()
213 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
214 INetworkManagementEventObserver>();
215 mNetworkManager.registerObserver(capture(networkObserver));
216 expectLastCall().atLeastOnce();
217
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700218 // expect to answer screen status during systemReady()
219 expect(mPowerManager.isScreenOn()).andReturn(true).atLeastOnce();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700220 expect(mNetworkManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700221 expectCurrentTime();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700222
223 replay();
224 mService.systemReady();
225 verifyAndReset();
226
227 mProcessObserver = processObserver.getValue();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700228 mNetworkObserver = networkObserver.getValue();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700229
230 }
231
232 @Override
233 public void tearDown() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700234 for (File file : mPolicyDir.listFiles()) {
235 file.delete();
236 }
237
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700238 mServiceContext = null;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700239 mPolicyDir = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700240
241 mActivityManager = null;
242 mPowerManager = null;
Jeff Sharkey75279902011-05-24 18:39:45 -0700243 mStatsService = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700244 mPolicyListener = null;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700245 mTime = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700246
247 mService = null;
248 mProcessObserver = null;
249
250 super.tearDown();
251 }
252
253 @Suppress
254 public void testPolicyChangeTriggersBroadcast() throws Exception {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700255 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700256
257 // change background policy and expect broadcast
258 final Future<Intent> backgroundChanged = mServiceContext.nextBroadcastIntent(
259 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
260
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700261 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700262
263 backgroundChanged.get();
264 }
265
266 public void testPidForegroundCombined() throws Exception {
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800267 IdleFuture idle;
268
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700269 // push all uid into background
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800270 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700271 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
272 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
273 mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800274 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700275 assertFalse(mService.isUidForeground(UID_A));
276 assertFalse(mService.isUidForeground(UID_B));
277
278 // push one of the shared pids into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800279 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700280 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800281 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700282 assertTrue(mService.isUidForeground(UID_A));
283 assertFalse(mService.isUidForeground(UID_B));
284
285 // and swap another uid into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800286 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700287 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
288 mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800289 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700290 assertFalse(mService.isUidForeground(UID_A));
291 assertTrue(mService.isUidForeground(UID_B));
292
293 // push both pid into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800294 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700295 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
296 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800297 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700298 assertTrue(mService.isUidForeground(UID_A));
299
300 // pull one out, should still be foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800301 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700302 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800303 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700304 assertTrue(mService.isUidForeground(UID_A));
305
306 // pull final pid out, should now be background
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800307 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700308 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800309 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700310 assertFalse(mService.isUidForeground(UID_A));
311 }
312
313 public void testScreenChangesRules() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700314 Future<Void> future;
315
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700316 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700317 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700318 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700319 replay();
320 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700321 future.get();
322 verifyAndReset();
323
324 // push strict policy for foreground uid, verify ALLOW rule
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700325 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700326 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700327 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
328 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700329 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700330 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700331 verifyAndReset();
332
333 // now turn screen off and verify REJECT rule
334 expect(mPowerManager.isScreenOn()).andReturn(false).atLeastOnce();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700335 expectSetUidNetworkRules(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700336 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700337 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700338 replay();
339 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_OFF));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700340 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700341 verifyAndReset();
342
343 // and turn screen back on, verify ALLOW rule restored
344 expect(mPowerManager.isScreenOn()).andReturn(true).atLeastOnce();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700345 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700346 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700347 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700348 replay();
349 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_ON));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700350 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700351 verifyAndReset();
352 }
353
354 public void testPolicyNone() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700355 Future<Void> future;
356
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700357 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700358 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700359 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
360 replay();
361 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
362 future.get();
363 verifyAndReset();
364
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700365 // POLICY_NONE should RULE_ALLOW in foreground
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700366 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700367 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700368 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700369 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700370 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700371 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700372 verifyAndReset();
373
374 // POLICY_NONE should RULE_ALLOW in background
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700375 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700376 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700377 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700378 replay();
379 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700380 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700381 verifyAndReset();
382 }
383
384 public void testPolicyReject() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700385 Future<Void> future;
386
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700387 // POLICY_REJECT should RULE_ALLOW in background
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700388 expectSetUidNetworkRules(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700389 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700390 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700391 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700392 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700393 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700394 verifyAndReset();
395
396 // POLICY_REJECT should RULE_ALLOW in foreground
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700397 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700398 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700399 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700400 replay();
401 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700402 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700403 verifyAndReset();
404
405 // POLICY_REJECT should RULE_REJECT in background
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700406 expectSetUidNetworkRules(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700407 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700408 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700409 replay();
410 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700411 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700412 verifyAndReset();
413 }
414
415 public void testPolicyRejectAddRemove() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700416 Future<Void> future;
417
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700418 // POLICY_NONE should have RULE_ALLOW in background
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700419 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700420 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700421 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700422 replay();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700423 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700424 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700425 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700426 verifyAndReset();
427
428 // adding POLICY_REJECT should cause RULE_REJECT
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700429 expectSetUidNetworkRules(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700430 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700431 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700432 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700433 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700434 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700435 verifyAndReset();
436
437 // removing POLICY_REJECT should return us to RULE_ALLOW
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700438 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700439 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700440 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700441 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700442 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700443 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700444 verifyAndReset();
445 }
446
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700447 public void testLastCycleBoundaryThisMonth() throws Exception {
448 // assume cycle day of "5th", which should be in same month
449 final long currentTime = parseTime("2007-11-14T00:00:00.000Z");
450 final long expectedCycle = parseTime("2007-11-05T00:00:00.000Z");
451
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700452 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800453 sTemplateWifi, 5, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700454 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700455 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700456 }
457
458 public void testLastCycleBoundaryLastMonth() throws Exception {
459 // assume cycle day of "20th", which should be in last month
460 final long currentTime = parseTime("2007-11-14T00:00:00.000Z");
461 final long expectedCycle = parseTime("2007-10-20T00:00:00.000Z");
462
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700463 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800464 sTemplateWifi, 20, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700465 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700466 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700467 }
468
469 public void testLastCycleBoundaryThisMonthFebruary() throws Exception {
470 // assume cycle day of "30th" in february; should go to january
471 final long currentTime = parseTime("2007-02-14T00:00:00.000Z");
472 final long expectedCycle = parseTime("2007-01-30T00:00:00.000Z");
473
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700474 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800475 sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700476 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700477 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700478 }
479
480 public void testLastCycleBoundaryLastMonthFebruary() throws Exception {
481 // assume cycle day of "30th" in february, which should clamp
482 final long currentTime = parseTime("2007-03-14T00:00:00.000Z");
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700483 final long expectedCycle = parseTime("2007-02-28T23:59:59.000Z");
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700484
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700485 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800486 sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700487 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700488 assertTimeEquals(expectedCycle, actualCycle);
489 }
490
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800491 public void testCycleBoundaryLeapYear() throws Exception {
492 final NetworkPolicy policy = new NetworkPolicy(
493 sTemplateWifi, 29, TIMEZONE_UTC, 1024L, 1024L, false);
494
495 assertTimeEquals(parseTime("2012-01-29T00:00:00.000Z"),
496 computeNextCycleBoundary(parseTime("2012-01-14T00:00:00.000Z"), policy));
497 assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"),
498 computeNextCycleBoundary(parseTime("2012-02-14T00:00:00.000Z"), policy));
499 assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"),
500 computeLastCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy));
501 assertTimeEquals(parseTime("2012-03-29T00:00:00.000Z"),
502 computeNextCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy));
503
504 assertTimeEquals(parseTime("2007-01-29T00:00:00.000Z"),
505 computeNextCycleBoundary(parseTime("2007-01-14T00:00:00.000Z"), policy));
506 assertTimeEquals(parseTime("2007-02-28T23:59:59.000Z"),
507 computeNextCycleBoundary(parseTime("2007-02-14T00:00:00.000Z"), policy));
508 assertTimeEquals(parseTime("2007-02-28T23:59:59.000Z"),
509 computeLastCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy));
510 assertTimeEquals(parseTime("2007-03-29T00:00:00.000Z"),
511 computeNextCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy));
512 }
513
514 public void testNextCycleTimezoneAfterUtc() throws Exception {
515 // US/Central is UTC-6
516 final NetworkPolicy policy = new NetworkPolicy(
517 sTemplateWifi, 10, "US/Central", 1024L, 1024L, false);
518 assertTimeEquals(parseTime("2012-01-10T06:00:00.000Z"),
519 computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
520 }
521
522 public void testNextCycleTimezoneBeforeUtc() throws Exception {
523 // Israel is UTC+2
524 final NetworkPolicy policy = new NetworkPolicy(
525 sTemplateWifi, 10, "Israel", 1024L, 1024L, false);
526 assertTimeEquals(parseTime("2012-01-09T22:00:00.000Z"),
527 computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
528 }
529
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700530 public void testNextCycleSane() throws Exception {
531 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800532 sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700533 final LinkedHashSet<Long> seen = new LinkedHashSet<Long>();
534
535 // walk forwards, ensuring that cycle boundaries don't get stuck
536 long currentCycle = computeNextCycleBoundary(parseTime("2011-08-01T00:00:00.000Z"), policy);
537 for (int i = 0; i < 128; i++) {
538 long nextCycle = computeNextCycleBoundary(currentCycle, policy);
539 assertEqualsFuzzy(DAY_IN_MILLIS * 30, nextCycle - currentCycle, DAY_IN_MILLIS * 3);
540 assertUnique(seen, nextCycle);
541 currentCycle = nextCycle;
542 }
543 }
544
545 public void testLastCycleSane() throws Exception {
546 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800547 sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700548 final LinkedHashSet<Long> seen = new LinkedHashSet<Long>();
549
550 // walk backwards, ensuring that cycle boundaries look sane
551 long currentCycle = computeLastCycleBoundary(parseTime("2011-08-04T00:00:00.000Z"), policy);
552 for (int i = 0; i < 128; i++) {
553 long lastCycle = computeLastCycleBoundary(currentCycle, policy);
554 assertEqualsFuzzy(DAY_IN_MILLIS * 30, currentCycle - lastCycle, DAY_IN_MILLIS * 3);
555 assertUnique(seen, lastCycle);
556 currentCycle = lastCycle;
557 }
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700558 }
559
560 public void testNetworkPolicyAppliedCycleLastMonth() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700561 NetworkState[] state = null;
562 NetworkStats stats = null;
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700563 Future<Void> future;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700564
565 final long TIME_FEB_15 = 1171497600000L;
566 final long TIME_MAR_10 = 1173484800000L;
567 final int CYCLE_DAY = 15;
568
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700569 setCurrentTimeMillis(TIME_MAR_10);
570
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700571 // first, pretend that wifi network comes online. no policy active,
572 // which means we shouldn't push limit to interface.
573 state = new NetworkState[] { buildWifi() };
574 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700575 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700576 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700577 expectAdvisePersistThreshold();
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700578 future = expectMeteredIfacesChanged();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700579
580 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700581 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700582 future.get();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700583 verifyAndReset();
584
585 // now change cycle to be on 15th, and test in early march, to verify we
586 // pick cycle day in previous month.
587 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700588 expectCurrentTime();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700589
590 // pretend that 512 bytes total have happened
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700591 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700592 .addIfaceValues(TEST_IFACE, 256L, 2L, 256L, 2L);
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700593 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, TIME_MAR_10))
594 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800595 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700596
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700597 // TODO: consider making strongly ordered mock
598 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700599 expectSetInterfaceQuota(TEST_IFACE, (2 * MB_IN_BYTES) - 512);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700600
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700601 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700602 expectAdvisePersistThreshold();
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700603 future = expectMeteredIfacesChanged(TEST_IFACE);
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700604
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700605 replay();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700606 setNetworkPolicies(new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800607 sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 * MB_IN_BYTES, 2 * MB_IN_BYTES, false));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700608 future.get();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700609 verifyAndReset();
610 }
611
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700612 public void testUidRemovedPolicyCleared() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700613 Future<Void> future;
614
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700615 // POLICY_REJECT should RULE_REJECT in background
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700616 expectSetUidNetworkRules(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700617 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700618 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700619 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700620 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700621 future.get();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700622 verifyAndReset();
623
624 // uninstall should clear RULE_REJECT
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700625 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700626 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700627 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700628 replay();
629 final Intent intent = new Intent(ACTION_UID_REMOVED);
630 intent.putExtra(EXTRA_UID, UID_A);
631 mServiceContext.sendBroadcast(intent);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700632 future.get();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700633 verifyAndReset();
634 }
635
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700636 public void testOverWarningLimitNotification() throws Exception {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700637 NetworkState[] state = null;
638 NetworkStats stats = null;
639 Future<Void> future;
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800640 Future<String> tagFuture;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700641
642 final long TIME_FEB_15 = 1171497600000L;
643 final long TIME_MAR_10 = 1173484800000L;
644 final int CYCLE_DAY = 15;
645
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700646 setCurrentTimeMillis(TIME_MAR_10);
647
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700648 // assign wifi policy
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700649 state = new NetworkState[] {};
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700650 stats = new NetworkStats(getElapsedRealtime(), 1)
651 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700652
653 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700654 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700655 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700656 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
657 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800658 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700659
660 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700661 expectAdvisePersistThreshold();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700662 future = expectMeteredIfacesChanged();
663
664 replay();
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800665 setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1
666 * MB_IN_BYTES, 2 * MB_IN_BYTES, false));
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700667 future.get();
668 verifyAndReset();
669 }
670
671 // bring up wifi network
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700672 incrementCurrentTime(MINUTE_IN_MILLIS);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700673 state = new NetworkState[] { buildWifi() };
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700674 stats = new NetworkStats(getElapsedRealtime(), 1)
675 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700676
677 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700678 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700679 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700680 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
681 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800682 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700683
684 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700685 expectSetInterfaceQuota(TEST_IFACE, 2 * MB_IN_BYTES);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700686
687 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700688 expectAdvisePersistThreshold();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700689 future = expectMeteredIfacesChanged(TEST_IFACE);
690
691 replay();
Jeff Sharkey961e3042011-08-29 16:02:57 -0700692 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE));
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700693 future.get();
694 verifyAndReset();
695 }
696
697 // go over warning, which should kick notification
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700698 incrementCurrentTime(MINUTE_IN_MILLIS);
699 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700700 .addIfaceValues(TEST_IFACE, 1536 * KB_IN_BYTES, 15L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700701
702 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700703 expectCurrentTime();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700704 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
705 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800706 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700707
708 expectForceUpdate();
709 expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800710 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700711
712 replay();
713 mNetworkObserver.limitReached(null, TEST_IFACE);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800714 assertNotificationType(TYPE_WARNING, tagFuture.get());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700715 verifyAndReset();
716 }
717
718 // go over limit, which should kick notification and dialog
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700719 incrementCurrentTime(MINUTE_IN_MILLIS);
720 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700721 .addIfaceValues(TEST_IFACE, 5 * MB_IN_BYTES, 512L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700722
723 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700724 expectCurrentTime();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700725 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
726 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800727 expectPolicyDataEnable(TYPE_WIFI, false);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700728
729 expectForceUpdate();
730 expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800731 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700732
733 replay();
734 mNetworkObserver.limitReached(null, TEST_IFACE);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800735 assertNotificationType(TYPE_LIMIT, tagFuture.get());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700736 verifyAndReset();
737 }
738
739 // now snooze policy, which should remove quota
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700740 incrementCurrentTime(MINUTE_IN_MILLIS);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700741
742 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700743 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700744 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700745 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
746 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800747 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700748
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700749 // snoozed interface still has high quota so background data is
750 // still restricted.
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700751 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700752 expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE);
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700753 expectAdvisePersistThreshold();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800754 expectMeteredIfacesChanged(TEST_IFACE);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700755
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800756 future = expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800757 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700758
759 replay();
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800760 mService.snoozeLimit(sTemplateWifi);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800761 assertNotificationType(TYPE_LIMIT_SNOOZED, tagFuture.get());
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800762 future.get();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700763 verifyAndReset();
764 }
765 }
766
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800767 public void testMeteredNetworkWithoutLimit() throws Exception {
768 NetworkState[] state = null;
769 NetworkStats stats = null;
770 Future<Void> future;
771 Future<String> tagFuture;
772
773 final long TIME_FEB_15 = 1171497600000L;
774 final long TIME_MAR_10 = 1173484800000L;
775 final int CYCLE_DAY = 15;
776
777 setCurrentTimeMillis(TIME_MAR_10);
778
779 // bring up wifi network with metered policy
780 state = new NetworkState[] { buildWifi() };
781 stats = new NetworkStats(getElapsedRealtime(), 1)
782 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
783
784 {
785 expectCurrentTime();
786 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700787 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
788 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800789 expectPolicyDataEnable(TYPE_WIFI, true);
790
791 expectRemoveInterfaceQuota(TEST_IFACE);
792 expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE);
793
794 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700795 expectAdvisePersistThreshold();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800796 future = expectMeteredIfacesChanged(TEST_IFACE);
797
798 replay();
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800799 setNetworkPolicies(new NetworkPolicy(
800 sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED,
801 true));
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800802 future.get();
803 verifyAndReset();
804 }
805 }
806
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700807 private static long parseTime(String time) {
808 final Time result = new Time();
809 result.parse3339(time);
810 return result.toMillis(true);
811 }
812
Jeff Sharkeyaf11d482011-06-13 00:14:31 -0700813 private void setNetworkPolicies(NetworkPolicy... policies) {
814 mService.setNetworkPolicies(policies);
815 }
816
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700817 private static NetworkState buildWifi() {
818 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
819 info.setDetailedState(DetailedState.CONNECTED, null, null);
820 final LinkProperties prop = new LinkProperties();
821 prop.setInterfaceName(TEST_IFACE);
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700822 return new NetworkState(info, prop, null, null, TEST_SSID);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700823 }
824
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700825 private void expectCurrentTime() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700826 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
827 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700828 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700829 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
830 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
831 }
832
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700833 private void expectForceUpdate() throws Exception {
834 mStatsService.forceUpdate();
835 expectLastCall().atLeastOnce();
836 }
837
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800838 private Future<Void> expectClearNotifications() throws Exception {
839 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700840 mNotifManager.cancelNotificationWithTag(
841 isA(String.class), isA(String.class), anyInt(), anyInt());
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800842 expectLastCall().andAnswer(future).anyTimes();
843 return future;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700844 }
845
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800846 private Future<String> expectEnqueueNotification() throws Exception {
847 final FutureCapture<String> tag = new FutureCapture<String>();
848 mNotifManager.enqueueNotificationWithTag(isA(String.class), capture(tag.capture), anyInt(),
Dianne Hackborn41203752012-08-31 14:05:51 -0700849 isA(Notification.class), isA(int[].class), UserHandle.myUserId());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700850 return tag;
851 }
852
853 private void expectSetInterfaceQuota(String iface, long quotaBytes) throws Exception {
854 mNetworkManager.setInterfaceQuota(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700855 expectLastCall().atLeastOnce();
856 }
857
858 private void expectRemoveInterfaceQuota(String iface) throws Exception {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700859 mNetworkManager.removeInterfaceQuota(iface);
860 expectLastCall().atLeastOnce();
861 }
862
863 private void expectSetInterfaceAlert(String iface, long alertBytes) throws Exception {
864 mNetworkManager.setInterfaceAlert(iface, alertBytes);
865 expectLastCall().atLeastOnce();
866 }
867
868 private void expectRemoveInterfaceAlert(String iface) throws Exception {
869 mNetworkManager.removeInterfaceAlert(iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700870 expectLastCall().atLeastOnce();
871 }
872
873 private void expectSetUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces)
874 throws Exception {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700875 mNetworkManager.setUidNetworkRules(uid, rejectOnQuotaInterfaces);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700876 expectLastCall().atLeastOnce();
877 }
878
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700879 private void expectSetUidForeground(int uid, boolean uidForeground) throws Exception {
880 mStatsService.setUidForeground(uid, uidForeground);
881 expectLastCall().atLeastOnce();
882 }
883
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700884 private Future<Void> expectRulesChanged(int uid, int policy) throws Exception {
885 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700886 mPolicyListener.onUidRulesChanged(eq(uid), eq(policy));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700887 expectLastCall().andAnswer(future);
888 return future;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700889 }
890
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700891 private Future<Void> expectMeteredIfacesChanged(String... ifaces) throws Exception {
892 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700893 mPolicyListener.onMeteredIfacesChanged(aryEq(ifaces));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700894 expectLastCall().andAnswer(future);
895 return future;
896 }
897
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800898 private Future<Void> expectPolicyDataEnable(int type, boolean enabled) throws Exception {
899 final FutureAnswer future = new FutureAnswer();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700900 mConnManager.setPolicyDataEnable(type, enabled);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800901 expectLastCall().andAnswer(future);
902 return future;
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700903 }
904
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700905 private void expectAdvisePersistThreshold() throws Exception {
906 mStatsService.advisePersistThreshold(anyLong());
907 expectLastCall().anyTimes();
908 }
909
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800910 private static class TestAbstractFuture<T> extends AbstractFuture<T> {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700911 @Override
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800912 public T get() throws InterruptedException, ExecutionException {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700913 try {
914 return get(5, TimeUnit.SECONDS);
915 } catch (TimeoutException e) {
916 throw new RuntimeException(e);
917 }
918 }
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800919 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700920
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800921 private static class FutureAnswer extends TestAbstractFuture<Void> implements IAnswer<Void> {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700922 @Override
923 public Void answer() {
924 set(null);
925 return null;
926 }
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700927 }
928
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800929 private static class FutureCapture<T> extends TestAbstractFuture<T> {
930 public Capture<T> capture = new Capture<T>() {
931 @Override
932 public void setValue(T value) {
933 super.setValue(value);
934 set(value);
935 }
936 };
937 }
938
Jeff Sharkey163e6442011-10-31 16:37:52 -0700939 private static class IdleFuture extends AbstractFuture<Void> implements IdleHandler {
940 @Override
941 public Void get() throws InterruptedException, ExecutionException {
942 try {
943 return get(5, TimeUnit.SECONDS);
944 } catch (TimeoutException e) {
945 throw new RuntimeException(e);
946 }
947 }
948
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700949 @Override
Jeff Sharkey163e6442011-10-31 16:37:52 -0700950 public boolean queueIdle() {
951 set(null);
952 return false;
953 }
954 }
955
956 /**
957 * Wait until {@link #mService} internal {@link Handler} is idle.
958 */
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800959 private IdleFuture expectIdle() {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700960 final IdleFuture future = new IdleFuture();
961 mService.addIdleHandler(future);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800962 return future;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700963 }
964
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700965 private static void assertTimeEquals(long expected, long actual) {
966 if (expected != actual) {
967 fail("expected " + formatTime(expected) + " but was actually " + formatTime(actual));
968 }
969 }
970
971 private static String formatTime(long millis) {
972 final Time time = new Time(Time.TIMEZONE_UTC);
973 time.set(millis);
974 return time.format3339(false);
975 }
976
977 private static void assertEqualsFuzzy(long expected, long actual, long fuzzy) {
978 final long low = expected - fuzzy;
979 final long high = expected + fuzzy;
980 if (actual < low || actual > high) {
981 fail("value " + actual + " is outside [" + low + "," + high + "]");
982 }
983 }
984
985 private static void assertUnique(LinkedHashSet<Long> seen, Long value) {
986 if (!seen.add(value)) {
987 fail("found duplicate time " + value + " in series " + seen.toString());
988 }
989 }
990
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700991 private static void assertNotificationType(int expected, String actualTag) {
992 assertEquals(
993 Integer.toString(expected), actualTag.substring(actualTag.lastIndexOf(':') + 1));
994 }
995
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700996 private long getElapsedRealtime() {
997 return mElapsedRealtime;
998 }
999
1000 private void setCurrentTimeMillis(long currentTimeMillis) {
1001 mStartTime = currentTimeMillis;
1002 mElapsedRealtime = 0L;
1003 }
1004
1005 private long currentTimeMillis() {
1006 return mStartTime + mElapsedRealtime;
1007 }
1008
1009 private void incrementCurrentTime(long duration) {
1010 mElapsedRealtime += duration;
1011 }
1012
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001013 private void replay() {
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001014 EasyMock.replay(mActivityManager, mPowerManager, mStatsService, mPolicyListener,
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001015 mNetworkManager, mTime, mConnManager, mNotifManager);
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001016 }
1017
1018 private void verifyAndReset() {
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001019 EasyMock.verify(mActivityManager, mPowerManager, mStatsService, mPolicyListener,
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001020 mNetworkManager, mTime, mConnManager, mNotifManager);
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001021 EasyMock.reset(mActivityManager, mPowerManager, mStatsService, mPolicyListener,
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001022 mNetworkManager, mTime, mConnManager, mNotifManager);
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001023 }
1024}