blob: 541be3dad376c1e85a883bf11281146f27d1054e [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;
Erik Klinef851d6d2015-04-20 16:03:48 +090021import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070022import static android.net.ConnectivityManager.TYPE_WIFI;
Jeff Sharkeyf2bead52016-07-06 17:11:48 -060023import static android.net.NetworkPolicy.CYCLE_NONE;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070024import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070025import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070026import static android.net.NetworkPolicyManager.POLICY_NONE;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070027import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070028import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070029import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
Jeff Sharkeycd2ca402011-06-10 15:14:07 -070030import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070031import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkey241dde22012-02-03 14:50:07 -080032import static android.net.TrafficStats.KB_IN_BYTES;
33import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070034import static android.text.format.DateUtils.DAY_IN_MILLIS;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070035import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
Jeff Sharkey9bf31502012-03-09 17:07:21 -080036import static android.text.format.Time.TIMEZONE_UTC;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070037import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT;
38import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT_SNOOZED;
39import static com.android.server.net.NetworkPolicyManagerService.TYPE_WARNING;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -070040import static org.easymock.EasyMock.anyInt;
Jeff Sharkey0cf6de02012-05-04 15:03:30 -070041import static org.easymock.EasyMock.anyLong;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070042import static org.easymock.EasyMock.aryEq;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070043import static org.easymock.EasyMock.capture;
44import static org.easymock.EasyMock.createMock;
45import static org.easymock.EasyMock.eq;
46import static org.easymock.EasyMock.expect;
47import static org.easymock.EasyMock.expectLastCall;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -070048import static org.easymock.EasyMock.isA;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070049
50import android.app.IActivityManager;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -070051import android.app.INotificationManager;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070052import android.app.IProcessObserver;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070053import android.app.Notification;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070054import android.content.Intent;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070055import android.content.pm.PackageInfo;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070056import android.content.pm.PackageManager;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070057import android.content.pm.Signature;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070058import android.net.ConnectivityManager;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070059import android.net.IConnectivityManager;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070060import android.net.INetworkManagementEventObserver;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070061import android.net.INetworkPolicyListener;
Jeff Sharkey75279902011-05-24 18:39:45 -070062import android.net.INetworkStatsService;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070063import android.net.LinkProperties;
64import android.net.NetworkInfo;
65import android.net.NetworkInfo.DetailedState;
66import android.net.NetworkPolicy;
67import android.net.NetworkState;
68import android.net.NetworkStats;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070069import android.net.NetworkTemplate;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070070import android.os.Binder;
Ashish Sharma50fd36d2011-06-15 19:34:53 -070071import android.os.INetworkManagementService;
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 Sharkeyf2bead52016-07-06 17:11:48 -060089import java.util.Calendar;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070090import java.util.LinkedHashSet;
Jeff Sharkeyf2bead52016-07-06 17:11:48 -060091import java.util.TimeZone;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070092import java.util.concurrent.ExecutionException;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070093import java.util.concurrent.Future;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070094import java.util.concurrent.TimeUnit;
95import java.util.concurrent.TimeoutException;
Jeff Sharkey163e6442011-10-31 16:37:52 -070096import java.util.logging.Handler;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070097
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -070098import libcore.io.IoUtils;
99
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700100/**
101 * Tests for {@link NetworkPolicyManagerService}.
102 */
103@LargeTest
104public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
105 private static final String TAG = "NetworkPolicyManagerServiceTest";
106
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700107 private static final long TEST_START = 1194220800000L;
108 private static final String TEST_IFACE = "test0";
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700109 private static final String TEST_SSID = "AndroidAP";
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700110
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700111 private static NetworkTemplate sTemplateWifi = NetworkTemplate.buildTemplateWifi(TEST_SSID);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700112
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700113 private BroadcastInterceptingContext mServiceContext;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700114 private File mPolicyDir;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700115
116 private IActivityManager mActivityManager;
Jeff Sharkey75279902011-05-24 18:39:45 -0700117 private INetworkStatsService mStatsService;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700118 private INetworkManagementService mNetworkManager;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700119 private INetworkPolicyListener mPolicyListener;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700120 private TrustedTime mTime;
121 private IConnectivityManager mConnManager;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700122 private INotificationManager mNotifManager;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700123
124 private NetworkPolicyManagerService mService;
125 private IProcessObserver mProcessObserver;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700126 private INetworkManagementEventObserver mNetworkObserver;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700127
128 private Binder mStubBinder = new Binder();
129
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700130 private long mStartTime;
131 private long mElapsedRealtime;
132
Jeff Sharkeycae04a22012-03-22 15:18:52 -0700133 private static final int USER_ID = 0;
Jeff Sharkeycae04a22012-03-22 15:18:52 -0700134
135 private static final int APP_ID_A = android.os.Process.FIRST_APPLICATION_UID + 800;
136 private static final int APP_ID_B = android.os.Process.FIRST_APPLICATION_UID + 801;
137
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700138 private static final int UID_A = UserHandle.getUid(USER_ID, APP_ID_A);
139 private static final int UID_B = UserHandle.getUid(USER_ID, APP_ID_B);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700140
141 private static final int PID_1 = 400;
142 private static final int PID_2 = 401;
143 private static final int PID_3 = 402;
144
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600145 public void _setUp() throws Exception {
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700146 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);
Jeff Sharkey75279902011-05-24 18:39:45 -0700188 mStatsService = createMock(INetworkStatsService.class);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700189 mNetworkManager = createMock(INetworkManagementService.class);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700190 mPolicyListener = createMock(INetworkPolicyListener.class);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700191 mTime = createMock(TrustedTime.class);
192 mConnManager = createMock(IConnectivityManager.class);
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700193 mNotifManager = createMock(INotificationManager.class);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700194
Felipe Lemef8dd7b42016-08-10 13:00:32 -0700195 mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager,
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700196 mStatsService, mNetworkManager, mTime, mPolicyDir, true);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700197 mService.bindConnectivityManager(mConnManager);
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700198 mService.bindNotificationManager(mNotifManager);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700199
200 // RemoteCallbackList needs a binder to use as key
201 expect(mPolicyListener.asBinder()).andReturn(mStubBinder).atLeastOnce();
202 replay();
203 mService.registerListener(mPolicyListener);
204 verifyAndReset();
205
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700206 // catch IProcessObserver during systemReady()
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700207 final Capture<IProcessObserver> processObserver = new Capture<IProcessObserver>();
208 mActivityManager.registerProcessObserver(capture(processObserver));
209 expectLastCall().atLeastOnce();
210
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700211 // catch INetworkManagementEventObserver during systemReady()
212 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
213 INetworkManagementEventObserver>();
214 mNetworkManager.registerObserver(capture(networkObserver));
215 expectLastCall().atLeastOnce();
216
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700217 expect(mNetworkManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700218 expectCurrentTime();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700219
220 replay();
221 mService.systemReady();
222 verifyAndReset();
223
224 mProcessObserver = processObserver.getValue();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700225 mNetworkObserver = networkObserver.getValue();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700226
227 }
228
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600229 public void _tearDown() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700230 for (File file : mPolicyDir.listFiles()) {
231 file.delete();
232 }
233
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700234 mServiceContext = null;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700235 mPolicyDir = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700236
237 mActivityManager = null;
Jeff Sharkey75279902011-05-24 18:39:45 -0700238 mStatsService = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700239 mPolicyListener = null;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700240 mTime = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700241
242 mService = null;
243 mProcessObserver = null;
244
245 super.tearDown();
246 }
247
248 @Suppress
249 public void testPolicyChangeTriggersBroadcast() throws Exception {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700250 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700251
252 // change background policy and expect broadcast
253 final Future<Intent> backgroundChanged = mServiceContext.nextBroadcastIntent(
254 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
255
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700256 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700257
258 backgroundChanged.get();
259 }
260
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600261 @Suppress
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700262 public void testPidForegroundCombined() throws Exception {
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800263 IdleFuture idle;
264
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700265 // push all uid into background
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800266 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700267 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
268 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
269 mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800270 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700271 assertFalse(mService.isUidForeground(UID_A));
272 assertFalse(mService.isUidForeground(UID_B));
273
274 // push one of the shared pids into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800275 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700276 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800277 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700278 assertTrue(mService.isUidForeground(UID_A));
279 assertFalse(mService.isUidForeground(UID_B));
280
281 // and swap another uid into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800282 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700283 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
284 mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800285 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700286 assertFalse(mService.isUidForeground(UID_A));
287 assertTrue(mService.isUidForeground(UID_B));
288
289 // push both pid into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800290 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700291 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
292 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800293 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700294 assertTrue(mService.isUidForeground(UID_A));
295
296 // pull one out, should still be foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800297 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700298 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800299 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700300 assertTrue(mService.isUidForeground(UID_A));
301
302 // pull final pid out, should now be background
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800303 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700304 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800305 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700306 assertFalse(mService.isUidForeground(UID_A));
307 }
308
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600309 @Suppress
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700310 public void testPolicyNone() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700311 Future<Void> future;
312
Felipe Leme65be3022016-03-22 14:53:13 -0700313 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700314 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700315 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
316 replay();
317 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
318 future.get();
319 verifyAndReset();
320
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700321 // POLICY_NONE should RULE_ALLOW in foreground
Felipe Leme65be3022016-03-22 14:53:13 -0700322 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700323 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700324 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700325 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700326 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700327 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700328 verifyAndReset();
329
330 // POLICY_NONE should RULE_ALLOW in background
Felipe Leme65be3022016-03-22 14:53:13 -0700331 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700332 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700333 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700334 replay();
335 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700336 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700337 verifyAndReset();
338 }
339
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600340 @Suppress
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700341 public void testPolicyReject() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700342 Future<Void> future;
343
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700344 // POLICY_REJECT should RULE_ALLOW in background
Felipe Leme65be3022016-03-22 14:53:13 -0700345 expectSetUidMeteredNetworkBlacklist(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700346 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700347 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700348 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700349 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700350 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700351 verifyAndReset();
352
353 // POLICY_REJECT should RULE_ALLOW in foreground
Felipe Leme65be3022016-03-22 14:53:13 -0700354 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700355 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700356 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700357 replay();
358 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700359 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700360 verifyAndReset();
361
362 // POLICY_REJECT should RULE_REJECT in background
Felipe Leme65be3022016-03-22 14:53:13 -0700363 expectSetUidMeteredNetworkBlacklist(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700364 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700365 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700366 replay();
367 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700368 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700369 verifyAndReset();
370 }
371
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600372 @Suppress
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700373 public void testPolicyRejectAddRemove() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700374 Future<Void> future;
375
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700376 // POLICY_NONE should have RULE_ALLOW in background
Felipe Leme65be3022016-03-22 14:53:13 -0700377 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700378 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700379 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700380 replay();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700381 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700382 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700383 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700384 verifyAndReset();
385
386 // adding POLICY_REJECT should cause RULE_REJECT
Felipe Leme65be3022016-03-22 14:53:13 -0700387 expectSetUidMeteredNetworkBlacklist(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700388 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700389 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700390 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700391 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700392 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700393 verifyAndReset();
394
395 // removing POLICY_REJECT should return us to RULE_ALLOW
Felipe Leme65be3022016-03-22 14:53:13 -0700396 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700397 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700398 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700399 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700400 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700401 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700402 verifyAndReset();
403 }
404
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700405 public void testLastCycleBoundaryThisMonth() throws Exception {
406 // assume cycle day of "5th", which should be in same month
407 final long currentTime = parseTime("2007-11-14T00:00:00.000Z");
408 final long expectedCycle = parseTime("2007-11-05T00:00:00.000Z");
409
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700410 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800411 sTemplateWifi, 5, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700412 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700413 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700414 }
415
416 public void testLastCycleBoundaryLastMonth() throws Exception {
417 // assume cycle day of "20th", which should be in last month
418 final long currentTime = parseTime("2007-11-14T00:00:00.000Z");
419 final long expectedCycle = parseTime("2007-10-20T00:00:00.000Z");
420
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700421 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800422 sTemplateWifi, 20, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700423 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700424 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700425 }
426
427 public void testLastCycleBoundaryThisMonthFebruary() throws Exception {
428 // assume cycle day of "30th" in february; should go to january
429 final long currentTime = parseTime("2007-02-14T00:00:00.000Z");
430 final long expectedCycle = parseTime("2007-01-30T00:00:00.000Z");
431
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700432 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800433 sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700434 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700435 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700436 }
437
438 public void testLastCycleBoundaryLastMonthFebruary() throws Exception {
439 // assume cycle day of "30th" in february, which should clamp
440 final long currentTime = parseTime("2007-03-14T00:00:00.000Z");
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700441 final long expectedCycle = parseTime("2007-02-28T23:59:59.000Z");
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700442
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700443 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800444 sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700445 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700446 assertTimeEquals(expectedCycle, actualCycle);
447 }
448
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800449 public void testCycleBoundaryLeapYear() throws Exception {
450 final NetworkPolicy policy = new NetworkPolicy(
451 sTemplateWifi, 29, TIMEZONE_UTC, 1024L, 1024L, false);
452
453 assertTimeEquals(parseTime("2012-01-29T00:00:00.000Z"),
454 computeNextCycleBoundary(parseTime("2012-01-14T00:00:00.000Z"), policy));
455 assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"),
456 computeNextCycleBoundary(parseTime("2012-02-14T00:00:00.000Z"), policy));
457 assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"),
458 computeLastCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy));
459 assertTimeEquals(parseTime("2012-03-29T00:00:00.000Z"),
460 computeNextCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy));
461
462 assertTimeEquals(parseTime("2007-01-29T00:00:00.000Z"),
463 computeNextCycleBoundary(parseTime("2007-01-14T00:00:00.000Z"), policy));
464 assertTimeEquals(parseTime("2007-02-28T23:59:59.000Z"),
465 computeNextCycleBoundary(parseTime("2007-02-14T00:00:00.000Z"), policy));
466 assertTimeEquals(parseTime("2007-02-28T23:59:59.000Z"),
467 computeLastCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy));
468 assertTimeEquals(parseTime("2007-03-29T00:00:00.000Z"),
469 computeNextCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy));
470 }
471
472 public void testNextCycleTimezoneAfterUtc() throws Exception {
473 // US/Central is UTC-6
474 final NetworkPolicy policy = new NetworkPolicy(
475 sTemplateWifi, 10, "US/Central", 1024L, 1024L, false);
476 assertTimeEquals(parseTime("2012-01-10T06:00:00.000Z"),
477 computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
478 }
479
480 public void testNextCycleTimezoneBeforeUtc() throws Exception {
481 // Israel is UTC+2
482 final NetworkPolicy policy = new NetworkPolicy(
483 sTemplateWifi, 10, "Israel", 1024L, 1024L, false);
484 assertTimeEquals(parseTime("2012-01-09T22:00:00.000Z"),
485 computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
486 }
487
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700488 public void testNextCycleSane() throws Exception {
489 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800490 sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700491 final LinkedHashSet<Long> seen = new LinkedHashSet<Long>();
492
493 // walk forwards, ensuring that cycle boundaries don't get stuck
494 long currentCycle = computeNextCycleBoundary(parseTime("2011-08-01T00:00:00.000Z"), policy);
495 for (int i = 0; i < 128; i++) {
496 long nextCycle = computeNextCycleBoundary(currentCycle, policy);
497 assertEqualsFuzzy(DAY_IN_MILLIS * 30, nextCycle - currentCycle, DAY_IN_MILLIS * 3);
498 assertUnique(seen, nextCycle);
499 currentCycle = nextCycle;
500 }
501 }
502
503 public void testLastCycleSane() throws Exception {
504 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800505 sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700506 final LinkedHashSet<Long> seen = new LinkedHashSet<Long>();
507
508 // walk backwards, ensuring that cycle boundaries look sane
509 long currentCycle = computeLastCycleBoundary(parseTime("2011-08-04T00:00:00.000Z"), policy);
510 for (int i = 0; i < 128; i++) {
511 long lastCycle = computeLastCycleBoundary(currentCycle, policy);
512 assertEqualsFuzzy(DAY_IN_MILLIS * 30, currentCycle - lastCycle, DAY_IN_MILLIS * 3);
513 assertUnique(seen, lastCycle);
514 currentCycle = lastCycle;
515 }
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700516 }
517
Jeff Sharkey15399052013-01-15 14:15:53 -0800518 public void testCycleTodayJanuary() throws Exception {
519 final NetworkPolicy policy = new NetworkPolicy(
520 sTemplateWifi, 14, "US/Pacific", 1024L, 1024L, false);
521
522 assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"),
523 computeNextCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy));
524 assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"),
525 computeNextCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy));
526 assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"),
527 computeNextCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy));
528
529 assertTimeEquals(parseTime("2012-12-14T00:00:00.000-08:00"),
530 computeLastCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy));
531 assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"),
532 computeLastCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy));
533 assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"),
534 computeLastCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy));
535 }
536
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600537 public void testLastCycleBoundaryDST() throws Exception {
538 final long currentTime = parseTime("1989-01-02T07:30:00.000");
539 final long expectedCycle = parseTime("1988-12-03T02:00:00.000Z");
540
541 final NetworkPolicy policy = new NetworkPolicy(
542 sTemplateWifi, 3, "America/Argentina/Buenos_Aires", 1024L, 1024L, false);
543 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
544 assertTimeEquals(expectedCycle, actualCycle);
545 }
546
547 @Suppress
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700548 public void testNetworkPolicyAppliedCycleLastMonth() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700549 NetworkState[] state = null;
550 NetworkStats stats = null;
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700551 Future<Void> future;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700552
553 final long TIME_FEB_15 = 1171497600000L;
554 final long TIME_MAR_10 = 1173484800000L;
555 final int CYCLE_DAY = 15;
556
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700557 setCurrentTimeMillis(TIME_MAR_10);
558
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700559 // first, pretend that wifi network comes online. no policy active,
560 // which means we shouldn't push limit to interface.
561 state = new NetworkState[] { buildWifi() };
562 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700563 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700564 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700565 expectAdvisePersistThreshold();
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700566 future = expectMeteredIfacesChanged();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700567
568 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900569 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700570 future.get();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700571 verifyAndReset();
572
573 // now change cycle to be on 15th, and test in early march, to verify we
574 // pick cycle day in previous month.
575 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700576 expectCurrentTime();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700577
578 // pretend that 512 bytes total have happened
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700579 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700580 .addIfaceValues(TEST_IFACE, 256L, 2L, 256L, 2L);
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700581 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, TIME_MAR_10))
582 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800583 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700584
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700585 // TODO: consider making strongly ordered mock
586 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700587 expectSetInterfaceQuota(TEST_IFACE, (2 * MB_IN_BYTES) - 512);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700588
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700589 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700590 expectAdvisePersistThreshold();
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700591 future = expectMeteredIfacesChanged(TEST_IFACE);
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700592
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700593 replay();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700594 setNetworkPolicies(new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800595 sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 * MB_IN_BYTES, 2 * MB_IN_BYTES, false));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700596 future.get();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700597 verifyAndReset();
598 }
599
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600600 @Suppress
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700601 public void testUidRemovedPolicyCleared() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700602 Future<Void> future;
603
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700604 // POLICY_REJECT should RULE_REJECT in background
Felipe Leme65be3022016-03-22 14:53:13 -0700605 expectSetUidMeteredNetworkBlacklist(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700606 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700607 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700608 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700609 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700610 future.get();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700611 verifyAndReset();
612
613 // uninstall should clear RULE_REJECT
Felipe Leme65be3022016-03-22 14:53:13 -0700614 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700615 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700616 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700617 replay();
618 final Intent intent = new Intent(ACTION_UID_REMOVED);
619 intent.putExtra(EXTRA_UID, UID_A);
620 mServiceContext.sendBroadcast(intent);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700621 future.get();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700622 verifyAndReset();
623 }
624
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600625 @Suppress
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700626 public void testOverWarningLimitNotification() throws Exception {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700627 NetworkState[] state = null;
628 NetworkStats stats = null;
629 Future<Void> future;
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800630 Future<String> tagFuture;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700631
632 final long TIME_FEB_15 = 1171497600000L;
633 final long TIME_MAR_10 = 1173484800000L;
634 final int CYCLE_DAY = 15;
635
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700636 setCurrentTimeMillis(TIME_MAR_10);
637
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700638 // assign wifi policy
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700639 state = new NetworkState[] {};
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700640 stats = new NetworkStats(getElapsedRealtime(), 1)
641 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700642
643 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700644 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700645 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700646 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
647 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800648 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700649
650 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700651 expectAdvisePersistThreshold();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700652 future = expectMeteredIfacesChanged();
653
654 replay();
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800655 setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1
656 * MB_IN_BYTES, 2 * MB_IN_BYTES, false));
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700657 future.get();
658 verifyAndReset();
659 }
660
661 // bring up wifi network
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700662 incrementCurrentTime(MINUTE_IN_MILLIS);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700663 state = new NetworkState[] { buildWifi() };
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700664 stats = new NetworkStats(getElapsedRealtime(), 1)
665 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700666
667 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700668 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700669 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700670 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
671 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800672 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700673
674 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700675 expectSetInterfaceQuota(TEST_IFACE, 2 * MB_IN_BYTES);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700676
677 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700678 expectAdvisePersistThreshold();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700679 future = expectMeteredIfacesChanged(TEST_IFACE);
680
681 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900682 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700683 future.get();
684 verifyAndReset();
685 }
686
687 // go over warning, which should kick notification
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700688 incrementCurrentTime(MINUTE_IN_MILLIS);
689 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700690 .addIfaceValues(TEST_IFACE, 1536 * KB_IN_BYTES, 15L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700691
692 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700693 expectCurrentTime();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700694 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
695 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800696 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700697
698 expectForceUpdate();
699 expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800700 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700701
702 replay();
703 mNetworkObserver.limitReached(null, TEST_IFACE);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800704 assertNotificationType(TYPE_WARNING, tagFuture.get());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700705 verifyAndReset();
706 }
707
708 // go over limit, which should kick notification and dialog
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700709 incrementCurrentTime(MINUTE_IN_MILLIS);
710 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700711 .addIfaceValues(TEST_IFACE, 5 * MB_IN_BYTES, 512L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700712
713 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700714 expectCurrentTime();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700715 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
716 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800717 expectPolicyDataEnable(TYPE_WIFI, false);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700718
719 expectForceUpdate();
720 expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800721 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700722
723 replay();
724 mNetworkObserver.limitReached(null, TEST_IFACE);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800725 assertNotificationType(TYPE_LIMIT, tagFuture.get());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700726 verifyAndReset();
727 }
728
729 // now snooze policy, which should remove quota
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700730 incrementCurrentTime(MINUTE_IN_MILLIS);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700731
732 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700733 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700734 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700735 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
736 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800737 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700738
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700739 // snoozed interface still has high quota so background data is
740 // still restricted.
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700741 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700742 expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE);
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700743 expectAdvisePersistThreshold();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800744 expectMeteredIfacesChanged(TEST_IFACE);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700745
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800746 future = expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800747 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700748
749 replay();
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800750 mService.snoozeLimit(sTemplateWifi);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800751 assertNotificationType(TYPE_LIMIT_SNOOZED, tagFuture.get());
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800752 future.get();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700753 verifyAndReset();
754 }
755 }
756
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600757 @Suppress
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800758 public void testMeteredNetworkWithoutLimit() throws Exception {
759 NetworkState[] state = null;
760 NetworkStats stats = null;
761 Future<Void> future;
762 Future<String> tagFuture;
763
764 final long TIME_FEB_15 = 1171497600000L;
765 final long TIME_MAR_10 = 1173484800000L;
766 final int CYCLE_DAY = 15;
767
768 setCurrentTimeMillis(TIME_MAR_10);
769
770 // bring up wifi network with metered policy
771 state = new NetworkState[] { buildWifi() };
772 stats = new NetworkStats(getElapsedRealtime(), 1)
773 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
774
775 {
776 expectCurrentTime();
777 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700778 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
779 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800780 expectPolicyDataEnable(TYPE_WIFI, true);
781
782 expectRemoveInterfaceQuota(TEST_IFACE);
783 expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE);
784
785 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700786 expectAdvisePersistThreshold();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800787 future = expectMeteredIfacesChanged(TEST_IFACE);
788
789 replay();
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800790 setNetworkPolicies(new NetworkPolicy(
791 sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED,
792 true));
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800793 future.get();
794 verifyAndReset();
795 }
796 }
797
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700798 private static long parseTime(String time) {
799 final Time result = new Time();
800 result.parse3339(time);
801 return result.toMillis(true);
802 }
803
Jeff Sharkeyaf11d482011-06-13 00:14:31 -0700804 private void setNetworkPolicies(NetworkPolicy... policies) {
805 mService.setNetworkPolicies(policies);
806 }
807
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700808 private static NetworkState buildWifi() {
809 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
810 info.setDetailedState(DetailedState.CONNECTED, null, null);
811 final LinkProperties prop = new LinkProperties();
812 prop.setInterfaceName(TEST_IFACE);
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -0800813 return new NetworkState(info, prop, null, null, null, TEST_SSID);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700814 }
815
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700816 private void expectCurrentTime() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700817 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
818 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700819 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700820 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
821 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
822 }
823
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700824 private void expectForceUpdate() throws Exception {
825 mStatsService.forceUpdate();
826 expectLastCall().atLeastOnce();
827 }
828
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800829 private Future<Void> expectClearNotifications() throws Exception {
830 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700831 mNotifManager.cancelNotificationWithTag(
832 isA(String.class), isA(String.class), anyInt(), anyInt());
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800833 expectLastCall().andAnswer(future).anyTimes();
834 return future;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700835 }
836
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800837 private Future<String> expectEnqueueNotification() throws Exception {
838 final FutureCapture<String> tag = new FutureCapture<String>();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800839 mNotifManager.enqueueNotificationWithTag(isA(String.class), isA(String.class),
840 capture(tag.capture), anyInt(),
Dianne Hackborn41203752012-08-31 14:05:51 -0700841 isA(Notification.class), isA(int[].class), UserHandle.myUserId());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700842 return tag;
843 }
844
845 private void expectSetInterfaceQuota(String iface, long quotaBytes) throws Exception {
846 mNetworkManager.setInterfaceQuota(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700847 expectLastCall().atLeastOnce();
848 }
849
850 private void expectRemoveInterfaceQuota(String iface) throws Exception {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700851 mNetworkManager.removeInterfaceQuota(iface);
852 expectLastCall().atLeastOnce();
853 }
854
855 private void expectSetInterfaceAlert(String iface, long alertBytes) throws Exception {
856 mNetworkManager.setInterfaceAlert(iface, alertBytes);
857 expectLastCall().atLeastOnce();
858 }
859
860 private void expectRemoveInterfaceAlert(String iface) throws Exception {
861 mNetworkManager.removeInterfaceAlert(iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700862 expectLastCall().atLeastOnce();
863 }
864
Felipe Leme65be3022016-03-22 14:53:13 -0700865 private void expectSetUidMeteredNetworkBlacklist(int uid, boolean rejectOnQuotaInterfaces)
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700866 throws Exception {
Felipe Leme65be3022016-03-22 14:53:13 -0700867 mNetworkManager.setUidMeteredNetworkBlacklist(uid, rejectOnQuotaInterfaces);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700868 expectLastCall().atLeastOnce();
869 }
870
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700871 private void expectSetUidForeground(int uid, boolean uidForeground) throws Exception {
872 mStatsService.setUidForeground(uid, uidForeground);
873 expectLastCall().atLeastOnce();
874 }
875
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700876 private Future<Void> expectRulesChanged(int uid, int policy) throws Exception {
877 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700878 mPolicyListener.onUidRulesChanged(eq(uid), eq(policy));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700879 expectLastCall().andAnswer(future);
880 return future;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700881 }
882
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700883 private Future<Void> expectMeteredIfacesChanged(String... ifaces) throws Exception {
884 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700885 mPolicyListener.onMeteredIfacesChanged(aryEq(ifaces));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700886 expectLastCall().andAnswer(future);
887 return future;
888 }
889
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800890 private Future<Void> expectPolicyDataEnable(int type, boolean enabled) throws Exception {
Jeff Sharkey32566012014-12-02 18:30:14 -0800891 // TODO: bring back this test
892 return null;
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700893 }
894
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700895 private void expectAdvisePersistThreshold() throws Exception {
896 mStatsService.advisePersistThreshold(anyLong());
897 expectLastCall().anyTimes();
898 }
899
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800900 private static class TestAbstractFuture<T> extends AbstractFuture<T> {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700901 @Override
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800902 public T get() throws InterruptedException, ExecutionException {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700903 try {
904 return get(5, TimeUnit.SECONDS);
905 } catch (TimeoutException e) {
906 throw new RuntimeException(e);
907 }
908 }
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800909 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700910
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800911 private static class FutureAnswer extends TestAbstractFuture<Void> implements IAnswer<Void> {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700912 @Override
913 public Void answer() {
914 set(null);
915 return null;
916 }
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700917 }
918
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800919 private static class FutureCapture<T> extends TestAbstractFuture<T> {
920 public Capture<T> capture = new Capture<T>() {
921 @Override
922 public void setValue(T value) {
923 super.setValue(value);
924 set(value);
925 }
926 };
927 }
928
Jeff Sharkey163e6442011-10-31 16:37:52 -0700929 private static class IdleFuture extends AbstractFuture<Void> implements IdleHandler {
930 @Override
931 public Void get() throws InterruptedException, ExecutionException {
932 try {
933 return get(5, TimeUnit.SECONDS);
934 } catch (TimeoutException e) {
935 throw new RuntimeException(e);
936 }
937 }
938
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700939 @Override
Jeff Sharkey163e6442011-10-31 16:37:52 -0700940 public boolean queueIdle() {
941 set(null);
942 return false;
943 }
944 }
945
946 /**
947 * Wait until {@link #mService} internal {@link Handler} is idle.
948 */
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800949 private IdleFuture expectIdle() {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700950 final IdleFuture future = new IdleFuture();
951 mService.addIdleHandler(future);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800952 return future;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700953 }
954
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700955 private static void assertTimeEquals(long expected, long actual) {
956 if (expected != actual) {
957 fail("expected " + formatTime(expected) + " but was actually " + formatTime(actual));
958 }
959 }
960
961 private static String formatTime(long millis) {
962 final Time time = new Time(Time.TIMEZONE_UTC);
963 time.set(millis);
964 return time.format3339(false);
965 }
966
967 private static void assertEqualsFuzzy(long expected, long actual, long fuzzy) {
968 final long low = expected - fuzzy;
969 final long high = expected + fuzzy;
970 if (actual < low || actual > high) {
971 fail("value " + actual + " is outside [" + low + "," + high + "]");
972 }
973 }
974
975 private static void assertUnique(LinkedHashSet<Long> seen, Long value) {
976 if (!seen.add(value)) {
977 fail("found duplicate time " + value + " in series " + seen.toString());
978 }
979 }
980
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700981 private static void assertNotificationType(int expected, String actualTag) {
982 assertEquals(
983 Integer.toString(expected), actualTag.substring(actualTag.lastIndexOf(':') + 1));
984 }
985
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700986 private long getElapsedRealtime() {
987 return mElapsedRealtime;
988 }
989
990 private void setCurrentTimeMillis(long currentTimeMillis) {
991 mStartTime = currentTimeMillis;
992 mElapsedRealtime = 0L;
993 }
994
995 private long currentTimeMillis() {
996 return mStartTime + mElapsedRealtime;
997 }
998
999 private void incrementCurrentTime(long duration) {
1000 mElapsedRealtime += duration;
1001 }
1002
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001003 private void replay() {
Felipe Lemef8dd7b42016-08-10 13:00:32 -07001004 EasyMock.replay(mActivityManager, mStatsService, mPolicyListener, mNetworkManager, mTime,
1005 mConnManager, mNotifManager);
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001006 }
1007
1008 private void verifyAndReset() {
Felipe Lemef8dd7b42016-08-10 13:00:32 -07001009 EasyMock.verify(mActivityManager, mStatsService, mPolicyListener, mNetworkManager, mTime,
1010 mConnManager, mNotifManager);
1011 EasyMock.reset(mActivityManager, mStatsService, mPolicyListener, mNetworkManager, mTime,
1012 mConnManager, mNotifManager);
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001013 }
1014}