blob: aeeca79a8473ecb56d7073575c83121cf434c7da [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
Lorenzo Colitti073e5e92016-10-28 12:56:03 +090081import com.android.internal.util.test.BroadcastInterceptingContext;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070082import com.android.server.net.NetworkPolicyManagerService;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070083import com.google.common.util.concurrent.AbstractFuture;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070084
85import org.easymock.Capture;
86import org.easymock.EasyMock;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070087import org.easymock.IAnswer;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070088
Jeff Sharkey21c9c452011-06-07 12:26:43 -070089import java.io.File;
Jeff Sharkeyf2bead52016-07-06 17:11:48 -060090import java.util.Calendar;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070091import java.util.LinkedHashSet;
Jeff Sharkeyf2bead52016-07-06 17:11:48 -060092import java.util.TimeZone;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070093import java.util.concurrent.ExecutionException;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070094import java.util.concurrent.Future;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070095import java.util.concurrent.TimeUnit;
96import java.util.concurrent.TimeoutException;
Jeff Sharkey163e6442011-10-31 16:37:52 -070097import java.util.logging.Handler;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070098
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -070099import libcore.io.IoUtils;
100
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700101/**
102 * Tests for {@link NetworkPolicyManagerService}.
103 */
104@LargeTest
105public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
106 private static final String TAG = "NetworkPolicyManagerServiceTest";
107
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700108 private static final long TEST_START = 1194220800000L;
109 private static final String TEST_IFACE = "test0";
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700110 private static final String TEST_SSID = "AndroidAP";
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700111
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700112 private static NetworkTemplate sTemplateWifi = NetworkTemplate.buildTemplateWifi(TEST_SSID);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700113
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700114 private BroadcastInterceptingContext mServiceContext;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700115 private File mPolicyDir;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700116
117 private IActivityManager mActivityManager;
Jeff Sharkey75279902011-05-24 18:39:45 -0700118 private INetworkStatsService mStatsService;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700119 private INetworkManagementService mNetworkManager;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700120 private INetworkPolicyListener mPolicyListener;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700121 private TrustedTime mTime;
122 private IConnectivityManager mConnManager;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700123 private INotificationManager mNotifManager;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700124
125 private NetworkPolicyManagerService mService;
126 private IProcessObserver mProcessObserver;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700127 private INetworkManagementEventObserver mNetworkObserver;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700128
129 private Binder mStubBinder = new Binder();
130
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700131 private long mStartTime;
132 private long mElapsedRealtime;
133
Jeff Sharkeycae04a22012-03-22 15:18:52 -0700134 private static final int USER_ID = 0;
Jeff Sharkeycae04a22012-03-22 15:18:52 -0700135
136 private static final int APP_ID_A = android.os.Process.FIRST_APPLICATION_UID + 800;
137 private static final int APP_ID_B = android.os.Process.FIRST_APPLICATION_UID + 801;
138
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700139 private static final int UID_A = UserHandle.getUid(USER_ID, APP_ID_A);
140 private static final int UID_B = UserHandle.getUid(USER_ID, APP_ID_B);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700141
142 private static final int PID_1 = 400;
143 private static final int PID_2 = 401;
144 private static final int PID_3 = 402;
145
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600146 public void _setUp() throws Exception {
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700147 super.setUp();
148
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700149 setCurrentTimeMillis(TEST_START);
150
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700151 // intercept various broadcasts, and pretend that uids have packages
152 mServiceContext = new BroadcastInterceptingContext(getContext()) {
153 @Override
154 public PackageManager getPackageManager() {
155 return new MockPackageManager() {
156 @Override
157 public String[] getPackagesForUid(int uid) {
158 return new String[] { "com.example" };
159 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700160
161 @Override
162 public PackageInfo getPackageInfo(String packageName, int flags) {
163 final PackageInfo info = new PackageInfo();
164 final Signature signature;
165 if ("android".equals(packageName)) {
166 signature = new Signature("F00D");
167 } else {
168 signature = new Signature("DEAD");
169 }
170 info.signatures = new Signature[] { signature };
171 return info;
172 }
Jeff Sharkeycae04a22012-03-22 15:18:52 -0700173
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700174 };
175 }
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700176
177 @Override
178 public void startActivity(Intent intent) {
179 // ignored
180 }
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700181 };
182
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700183 mPolicyDir = getContext().getFilesDir();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700184 if (mPolicyDir.exists()) {
185 IoUtils.deleteContents(mPolicyDir);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700186 }
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700187
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700188 mActivityManager = createMock(IActivityManager.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
Felipe Lemef8dd7b42016-08-10 13:00:32 -0700196 mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager,
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700197 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 Sharkey0cf6de02012-05-04 15:03:30 -0700218 expect(mNetworkManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700219 expectCurrentTime();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700220
221 replay();
222 mService.systemReady();
223 verifyAndReset();
224
225 mProcessObserver = processObserver.getValue();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700226 mNetworkObserver = networkObserver.getValue();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700227
228 }
229
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600230 public void _tearDown() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700231 for (File file : mPolicyDir.listFiles()) {
232 file.delete();
233 }
234
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700235 mServiceContext = null;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700236 mPolicyDir = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700237
238 mActivityManager = null;
Jeff Sharkey75279902011-05-24 18:39:45 -0700239 mStatsService = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700240 mPolicyListener = null;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700241 mTime = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700242
243 mService = null;
244 mProcessObserver = null;
245
246 super.tearDown();
247 }
248
249 @Suppress
250 public void testPolicyChangeTriggersBroadcast() throws Exception {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700251 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700252
253 // change background policy and expect broadcast
254 final Future<Intent> backgroundChanged = mServiceContext.nextBroadcastIntent(
255 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
256
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700257 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700258
259 backgroundChanged.get();
260 }
261
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600262 @Suppress
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700263 public void testPidForegroundCombined() throws Exception {
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800264 IdleFuture idle;
265
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700266 // push all uid into background
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800267 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700268 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
269 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
270 mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800271 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700272 assertFalse(mService.isUidForeground(UID_A));
273 assertFalse(mService.isUidForeground(UID_B));
274
275 // push one of the shared pids into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800276 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700277 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800278 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700279 assertTrue(mService.isUidForeground(UID_A));
280 assertFalse(mService.isUidForeground(UID_B));
281
282 // and swap another uid into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800283 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700284 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
285 mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800286 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700287 assertFalse(mService.isUidForeground(UID_A));
288 assertTrue(mService.isUidForeground(UID_B));
289
290 // push both pid into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800291 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700292 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
293 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800294 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700295 assertTrue(mService.isUidForeground(UID_A));
296
297 // pull one out, should still be foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800298 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700299 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800300 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700301 assertTrue(mService.isUidForeground(UID_A));
302
303 // pull final pid out, should now be background
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800304 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700305 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800306 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700307 assertFalse(mService.isUidForeground(UID_A));
308 }
309
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600310 @Suppress
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700311 public void testPolicyNone() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700312 Future<Void> future;
313
Felipe Leme65be3022016-03-22 14:53:13 -0700314 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700315 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700316 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
317 replay();
318 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
319 future.get();
320 verifyAndReset();
321
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700322 // POLICY_NONE should RULE_ALLOW in foreground
Felipe Leme65be3022016-03-22 14:53:13 -0700323 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700324 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700325 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700326 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700327 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700328 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700329 verifyAndReset();
330
331 // POLICY_NONE should RULE_ALLOW in background
Felipe Leme65be3022016-03-22 14:53:13 -0700332 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700333 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700334 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700335 replay();
336 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700337 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700338 verifyAndReset();
339 }
340
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600341 @Suppress
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700342 public void testPolicyReject() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700343 Future<Void> future;
344
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700345 // POLICY_REJECT should RULE_ALLOW in background
Felipe Leme65be3022016-03-22 14:53:13 -0700346 expectSetUidMeteredNetworkBlacklist(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700347 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700348 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700349 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700350 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700351 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700352 verifyAndReset();
353
354 // POLICY_REJECT should RULE_ALLOW in foreground
Felipe Leme65be3022016-03-22 14:53:13 -0700355 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700356 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700357 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700358 replay();
359 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700360 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700361 verifyAndReset();
362
363 // POLICY_REJECT should RULE_REJECT in background
Felipe Leme65be3022016-03-22 14:53:13 -0700364 expectSetUidMeteredNetworkBlacklist(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700365 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700366 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700367 replay();
368 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700369 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700370 verifyAndReset();
371 }
372
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600373 @Suppress
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700374 public void testPolicyRejectAddRemove() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700375 Future<Void> future;
376
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700377 // POLICY_NONE should have RULE_ALLOW in background
Felipe Leme65be3022016-03-22 14:53:13 -0700378 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700379 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700380 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700381 replay();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700382 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700383 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700384 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700385 verifyAndReset();
386
387 // adding POLICY_REJECT should cause RULE_REJECT
Felipe Leme65be3022016-03-22 14:53:13 -0700388 expectSetUidMeteredNetworkBlacklist(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 // removing POLICY_REJECT should return us to RULE_ALLOW
Felipe Leme65be3022016-03-22 14:53:13 -0700397 expectSetUidMeteredNetworkBlacklist(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700398 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700399 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700400 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700401 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700402 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700403 verifyAndReset();
404 }
405
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700406 public void testLastCycleBoundaryThisMonth() throws Exception {
407 // assume cycle day of "5th", which should be in same month
408 final long currentTime = parseTime("2007-11-14T00:00:00.000Z");
409 final long expectedCycle = parseTime("2007-11-05T00:00:00.000Z");
410
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700411 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800412 sTemplateWifi, 5, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700413 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700414 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700415 }
416
417 public void testLastCycleBoundaryLastMonth() throws Exception {
418 // assume cycle day of "20th", which should be in last month
419 final long currentTime = parseTime("2007-11-14T00:00:00.000Z");
420 final long expectedCycle = parseTime("2007-10-20T00:00:00.000Z");
421
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700422 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800423 sTemplateWifi, 20, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700424 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700425 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700426 }
427
428 public void testLastCycleBoundaryThisMonthFebruary() throws Exception {
429 // assume cycle day of "30th" in february; should go to january
430 final long currentTime = parseTime("2007-02-14T00:00:00.000Z");
431 final long expectedCycle = parseTime("2007-01-30T00:00:00.000Z");
432
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700433 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800434 sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700435 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700436 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700437 }
438
439 public void testLastCycleBoundaryLastMonthFebruary() throws Exception {
440 // assume cycle day of "30th" in february, which should clamp
441 final long currentTime = parseTime("2007-03-14T00:00:00.000Z");
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700442 final long expectedCycle = parseTime("2007-02-28T23:59:59.000Z");
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700443
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700444 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800445 sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700446 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700447 assertTimeEquals(expectedCycle, actualCycle);
448 }
449
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800450 public void testCycleBoundaryLeapYear() throws Exception {
451 final NetworkPolicy policy = new NetworkPolicy(
452 sTemplateWifi, 29, TIMEZONE_UTC, 1024L, 1024L, false);
453
454 assertTimeEquals(parseTime("2012-01-29T00:00:00.000Z"),
455 computeNextCycleBoundary(parseTime("2012-01-14T00:00:00.000Z"), policy));
456 assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"),
457 computeNextCycleBoundary(parseTime("2012-02-14T00:00:00.000Z"), policy));
458 assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"),
459 computeLastCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy));
460 assertTimeEquals(parseTime("2012-03-29T00:00:00.000Z"),
461 computeNextCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy));
462
463 assertTimeEquals(parseTime("2007-01-29T00:00:00.000Z"),
464 computeNextCycleBoundary(parseTime("2007-01-14T00:00:00.000Z"), policy));
465 assertTimeEquals(parseTime("2007-02-28T23:59:59.000Z"),
466 computeNextCycleBoundary(parseTime("2007-02-14T00:00:00.000Z"), policy));
467 assertTimeEquals(parseTime("2007-02-28T23:59:59.000Z"),
468 computeLastCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy));
469 assertTimeEquals(parseTime("2007-03-29T00:00:00.000Z"),
470 computeNextCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy));
471 }
472
473 public void testNextCycleTimezoneAfterUtc() throws Exception {
474 // US/Central is UTC-6
475 final NetworkPolicy policy = new NetworkPolicy(
476 sTemplateWifi, 10, "US/Central", 1024L, 1024L, false);
477 assertTimeEquals(parseTime("2012-01-10T06:00:00.000Z"),
478 computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
479 }
480
481 public void testNextCycleTimezoneBeforeUtc() throws Exception {
482 // Israel is UTC+2
483 final NetworkPolicy policy = new NetworkPolicy(
484 sTemplateWifi, 10, "Israel", 1024L, 1024L, false);
485 assertTimeEquals(parseTime("2012-01-09T22:00:00.000Z"),
486 computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
487 }
488
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700489 public void testNextCycleSane() throws Exception {
490 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800491 sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700492 final LinkedHashSet<Long> seen = new LinkedHashSet<Long>();
493
494 // walk forwards, ensuring that cycle boundaries don't get stuck
495 long currentCycle = computeNextCycleBoundary(parseTime("2011-08-01T00:00:00.000Z"), policy);
496 for (int i = 0; i < 128; i++) {
497 long nextCycle = computeNextCycleBoundary(currentCycle, policy);
498 assertEqualsFuzzy(DAY_IN_MILLIS * 30, nextCycle - currentCycle, DAY_IN_MILLIS * 3);
499 assertUnique(seen, nextCycle);
500 currentCycle = nextCycle;
501 }
502 }
503
504 public void testLastCycleSane() throws Exception {
505 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800506 sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700507 final LinkedHashSet<Long> seen = new LinkedHashSet<Long>();
508
509 // walk backwards, ensuring that cycle boundaries look sane
510 long currentCycle = computeLastCycleBoundary(parseTime("2011-08-04T00:00:00.000Z"), policy);
511 for (int i = 0; i < 128; i++) {
512 long lastCycle = computeLastCycleBoundary(currentCycle, policy);
513 assertEqualsFuzzy(DAY_IN_MILLIS * 30, currentCycle - lastCycle, DAY_IN_MILLIS * 3);
514 assertUnique(seen, lastCycle);
515 currentCycle = lastCycle;
516 }
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700517 }
518
Jeff Sharkey15399052013-01-15 14:15:53 -0800519 public void testCycleTodayJanuary() throws Exception {
520 final NetworkPolicy policy = new NetworkPolicy(
521 sTemplateWifi, 14, "US/Pacific", 1024L, 1024L, false);
522
523 assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"),
524 computeNextCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy));
525 assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"),
526 computeNextCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy));
527 assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"),
528 computeNextCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy));
529
530 assertTimeEquals(parseTime("2012-12-14T00:00:00.000-08:00"),
531 computeLastCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy));
532 assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"),
533 computeLastCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy));
534 assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"),
535 computeLastCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy));
536 }
537
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600538 public void testLastCycleBoundaryDST() throws Exception {
539 final long currentTime = parseTime("1989-01-02T07:30:00.000");
540 final long expectedCycle = parseTime("1988-12-03T02:00:00.000Z");
541
542 final NetworkPolicy policy = new NetworkPolicy(
543 sTemplateWifi, 3, "America/Argentina/Buenos_Aires", 1024L, 1024L, false);
544 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
545 assertTimeEquals(expectedCycle, actualCycle);
546 }
547
Shunta Satod18ffd82016-10-25 17:42:58 +0900548 public void testLastCycleBoundaryJanuaryDST() throws Exception {
549 final long currentTime = parseTime("1989-01-26T21:00:00.000Z");
550 final long expectedCycle = parseTime("1989-01-01T01:59:59.000Z");
551
552 final NetworkPolicy policy = new NetworkPolicy(
553 sTemplateWifi, 32, "America/Argentina/Buenos_Aires", 1024L, 1024L, false);
554 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
555 assertTimeEquals(expectedCycle, actualCycle);
556 }
557
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600558 @Suppress
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700559 public void testNetworkPolicyAppliedCycleLastMonth() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700560 NetworkState[] state = null;
561 NetworkStats stats = null;
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700562 Future<Void> future;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700563
564 final long TIME_FEB_15 = 1171497600000L;
565 final long TIME_MAR_10 = 1173484800000L;
566 final int CYCLE_DAY = 15;
567
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700568 setCurrentTimeMillis(TIME_MAR_10);
569
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700570 // first, pretend that wifi network comes online. no policy active,
571 // which means we shouldn't push limit to interface.
572 state = new NetworkState[] { buildWifi() };
573 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700574 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700575 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700576 expectAdvisePersistThreshold();
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700577 future = expectMeteredIfacesChanged();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700578
579 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900580 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700581 future.get();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700582 verifyAndReset();
583
584 // now change cycle to be on 15th, and test in early march, to verify we
585 // pick cycle day in previous month.
586 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700587 expectCurrentTime();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700588
589 // pretend that 512 bytes total have happened
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700590 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700591 .addIfaceValues(TEST_IFACE, 256L, 2L, 256L, 2L);
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700592 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, TIME_MAR_10))
593 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800594 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700595
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700596 // TODO: consider making strongly ordered mock
597 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700598 expectSetInterfaceQuota(TEST_IFACE, (2 * MB_IN_BYTES) - 512);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700599
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700600 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700601 expectAdvisePersistThreshold();
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700602 future = expectMeteredIfacesChanged(TEST_IFACE);
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700603
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700604 replay();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700605 setNetworkPolicies(new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800606 sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 * MB_IN_BYTES, 2 * MB_IN_BYTES, false));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700607 future.get();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700608 verifyAndReset();
609 }
610
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600611 @Suppress
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
Felipe Leme65be3022016-03-22 14:53:13 -0700616 expectSetUidMeteredNetworkBlacklist(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
Felipe Leme65be3022016-03-22 14:53:13 -0700625 expectSetUidMeteredNetworkBlacklist(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 Sharkeyf2bead52016-07-06 17:11:48 -0600636 @Suppress
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700637 public void testOverWarningLimitNotification() throws Exception {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700638 NetworkState[] state = null;
639 NetworkStats stats = null;
640 Future<Void> future;
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800641 Future<String> tagFuture;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700642
643 final long TIME_FEB_15 = 1171497600000L;
644 final long TIME_MAR_10 = 1173484800000L;
645 final int CYCLE_DAY = 15;
646
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700647 setCurrentTimeMillis(TIME_MAR_10);
648
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700649 // assign wifi policy
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700650 state = new NetworkState[] {};
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700651 stats = new NetworkStats(getElapsedRealtime(), 1)
652 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700653
654 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700655 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700656 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700657 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
658 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800659 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700660
661 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700662 expectAdvisePersistThreshold();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700663 future = expectMeteredIfacesChanged();
664
665 replay();
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800666 setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1
667 * MB_IN_BYTES, 2 * MB_IN_BYTES, false));
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700668 future.get();
669 verifyAndReset();
670 }
671
672 // bring up wifi network
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700673 incrementCurrentTime(MINUTE_IN_MILLIS);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700674 state = new NetworkState[] { buildWifi() };
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700675 stats = new NetworkStats(getElapsedRealtime(), 1)
676 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700677
678 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700679 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700680 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700681 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
682 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800683 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700684
685 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700686 expectSetInterfaceQuota(TEST_IFACE, 2 * MB_IN_BYTES);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700687
688 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700689 expectAdvisePersistThreshold();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700690 future = expectMeteredIfacesChanged(TEST_IFACE);
691
692 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900693 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700694 future.get();
695 verifyAndReset();
696 }
697
698 // go over warning, which should kick notification
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700699 incrementCurrentTime(MINUTE_IN_MILLIS);
700 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700701 .addIfaceValues(TEST_IFACE, 1536 * KB_IN_BYTES, 15L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700702
703 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700704 expectCurrentTime();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700705 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
706 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800707 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700708
709 expectForceUpdate();
710 expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800711 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700712
713 replay();
714 mNetworkObserver.limitReached(null, TEST_IFACE);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800715 assertNotificationType(TYPE_WARNING, tagFuture.get());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700716 verifyAndReset();
717 }
718
719 // go over limit, which should kick notification and dialog
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700720 incrementCurrentTime(MINUTE_IN_MILLIS);
721 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700722 .addIfaceValues(TEST_IFACE, 5 * MB_IN_BYTES, 512L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700723
724 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700725 expectCurrentTime();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700726 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
727 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800728 expectPolicyDataEnable(TYPE_WIFI, false);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700729
730 expectForceUpdate();
731 expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800732 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700733
734 replay();
735 mNetworkObserver.limitReached(null, TEST_IFACE);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800736 assertNotificationType(TYPE_LIMIT, tagFuture.get());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700737 verifyAndReset();
738 }
739
740 // now snooze policy, which should remove quota
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700741 incrementCurrentTime(MINUTE_IN_MILLIS);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700742
743 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700744 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700745 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700746 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
747 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800748 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700749
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700750 // snoozed interface still has high quota so background data is
751 // still restricted.
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700752 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700753 expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE);
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700754 expectAdvisePersistThreshold();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800755 expectMeteredIfacesChanged(TEST_IFACE);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700756
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800757 future = expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800758 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700759
760 replay();
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800761 mService.snoozeLimit(sTemplateWifi);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800762 assertNotificationType(TYPE_LIMIT_SNOOZED, tagFuture.get());
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800763 future.get();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700764 verifyAndReset();
765 }
766 }
767
Jeff Sharkeyf2bead52016-07-06 17:11:48 -0600768 @Suppress
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800769 public void testMeteredNetworkWithoutLimit() throws Exception {
770 NetworkState[] state = null;
771 NetworkStats stats = null;
772 Future<Void> future;
773 Future<String> tagFuture;
774
775 final long TIME_FEB_15 = 1171497600000L;
776 final long TIME_MAR_10 = 1173484800000L;
777 final int CYCLE_DAY = 15;
778
779 setCurrentTimeMillis(TIME_MAR_10);
780
781 // bring up wifi network with metered policy
782 state = new NetworkState[] { buildWifi() };
783 stats = new NetworkStats(getElapsedRealtime(), 1)
784 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
785
786 {
787 expectCurrentTime();
788 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700789 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
790 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800791 expectPolicyDataEnable(TYPE_WIFI, true);
792
793 expectRemoveInterfaceQuota(TEST_IFACE);
794 expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE);
795
796 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700797 expectAdvisePersistThreshold();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800798 future = expectMeteredIfacesChanged(TEST_IFACE);
799
800 replay();
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800801 setNetworkPolicies(new NetworkPolicy(
802 sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED,
803 true));
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800804 future.get();
805 verifyAndReset();
806 }
807 }
808
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700809 private static long parseTime(String time) {
810 final Time result = new Time();
811 result.parse3339(time);
812 return result.toMillis(true);
813 }
814
Jeff Sharkeyaf11d482011-06-13 00:14:31 -0700815 private void setNetworkPolicies(NetworkPolicy... policies) {
816 mService.setNetworkPolicies(policies);
817 }
818
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700819 private static NetworkState buildWifi() {
820 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
821 info.setDetailedState(DetailedState.CONNECTED, null, null);
822 final LinkProperties prop = new LinkProperties();
823 prop.setInterfaceName(TEST_IFACE);
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -0800824 return new NetworkState(info, prop, null, null, null, TEST_SSID);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700825 }
826
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700827 private void expectCurrentTime() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700828 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
829 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700830 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700831 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
832 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
833 }
834
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700835 private void expectForceUpdate() throws Exception {
836 mStatsService.forceUpdate();
837 expectLastCall().atLeastOnce();
838 }
839
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800840 private Future<Void> expectClearNotifications() throws Exception {
841 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700842 mNotifManager.cancelNotificationWithTag(
843 isA(String.class), isA(String.class), anyInt(), anyInt());
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800844 expectLastCall().andAnswer(future).anyTimes();
845 return future;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700846 }
847
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800848 private Future<String> expectEnqueueNotification() throws Exception {
849 final FutureCapture<String> tag = new FutureCapture<String>();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800850 mNotifManager.enqueueNotificationWithTag(isA(String.class), isA(String.class),
851 capture(tag.capture), anyInt(),
Dianne Hackborn41203752012-08-31 14:05:51 -0700852 isA(Notification.class), isA(int[].class), UserHandle.myUserId());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700853 return tag;
854 }
855
856 private void expectSetInterfaceQuota(String iface, long quotaBytes) throws Exception {
857 mNetworkManager.setInterfaceQuota(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700858 expectLastCall().atLeastOnce();
859 }
860
861 private void expectRemoveInterfaceQuota(String iface) throws Exception {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700862 mNetworkManager.removeInterfaceQuota(iface);
863 expectLastCall().atLeastOnce();
864 }
865
866 private void expectSetInterfaceAlert(String iface, long alertBytes) throws Exception {
867 mNetworkManager.setInterfaceAlert(iface, alertBytes);
868 expectLastCall().atLeastOnce();
869 }
870
871 private void expectRemoveInterfaceAlert(String iface) throws Exception {
872 mNetworkManager.removeInterfaceAlert(iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700873 expectLastCall().atLeastOnce();
874 }
875
Felipe Leme65be3022016-03-22 14:53:13 -0700876 private void expectSetUidMeteredNetworkBlacklist(int uid, boolean rejectOnQuotaInterfaces)
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700877 throws Exception {
Felipe Leme65be3022016-03-22 14:53:13 -0700878 mNetworkManager.setUidMeteredNetworkBlacklist(uid, rejectOnQuotaInterfaces);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700879 expectLastCall().atLeastOnce();
880 }
881
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700882 private void expectSetUidForeground(int uid, boolean uidForeground) throws Exception {
883 mStatsService.setUidForeground(uid, uidForeground);
884 expectLastCall().atLeastOnce();
885 }
886
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700887 private Future<Void> expectRulesChanged(int uid, int policy) throws Exception {
888 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700889 mPolicyListener.onUidRulesChanged(eq(uid), eq(policy));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700890 expectLastCall().andAnswer(future);
891 return future;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700892 }
893
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700894 private Future<Void> expectMeteredIfacesChanged(String... ifaces) throws Exception {
895 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700896 mPolicyListener.onMeteredIfacesChanged(aryEq(ifaces));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700897 expectLastCall().andAnswer(future);
898 return future;
899 }
900
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800901 private Future<Void> expectPolicyDataEnable(int type, boolean enabled) throws Exception {
Jeff Sharkey32566012014-12-02 18:30:14 -0800902 // TODO: bring back this test
903 return null;
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700904 }
905
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700906 private void expectAdvisePersistThreshold() throws Exception {
907 mStatsService.advisePersistThreshold(anyLong());
908 expectLastCall().anyTimes();
909 }
910
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800911 private static class TestAbstractFuture<T> extends AbstractFuture<T> {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700912 @Override
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800913 public T get() throws InterruptedException, ExecutionException {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700914 try {
915 return get(5, TimeUnit.SECONDS);
916 } catch (TimeoutException e) {
917 throw new RuntimeException(e);
918 }
919 }
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800920 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700921
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800922 private static class FutureAnswer extends TestAbstractFuture<Void> implements IAnswer<Void> {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700923 @Override
924 public Void answer() {
925 set(null);
926 return null;
927 }
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700928 }
929
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800930 private static class FutureCapture<T> extends TestAbstractFuture<T> {
931 public Capture<T> capture = new Capture<T>() {
932 @Override
933 public void setValue(T value) {
934 super.setValue(value);
935 set(value);
936 }
937 };
938 }
939
Jeff Sharkey163e6442011-10-31 16:37:52 -0700940 private static class IdleFuture extends AbstractFuture<Void> implements IdleHandler {
941 @Override
942 public Void get() throws InterruptedException, ExecutionException {
943 try {
944 return get(5, TimeUnit.SECONDS);
945 } catch (TimeoutException e) {
946 throw new RuntimeException(e);
947 }
948 }
949
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700950 @Override
Jeff Sharkey163e6442011-10-31 16:37:52 -0700951 public boolean queueIdle() {
952 set(null);
953 return false;
954 }
955 }
956
957 /**
958 * Wait until {@link #mService} internal {@link Handler} is idle.
959 */
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800960 private IdleFuture expectIdle() {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700961 final IdleFuture future = new IdleFuture();
962 mService.addIdleHandler(future);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800963 return future;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700964 }
965
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700966 private static void assertTimeEquals(long expected, long actual) {
967 if (expected != actual) {
968 fail("expected " + formatTime(expected) + " but was actually " + formatTime(actual));
969 }
970 }
971
972 private static String formatTime(long millis) {
973 final Time time = new Time(Time.TIMEZONE_UTC);
974 time.set(millis);
975 return time.format3339(false);
976 }
977
978 private static void assertEqualsFuzzy(long expected, long actual, long fuzzy) {
979 final long low = expected - fuzzy;
980 final long high = expected + fuzzy;
981 if (actual < low || actual > high) {
982 fail("value " + actual + " is outside [" + low + "," + high + "]");
983 }
984 }
985
986 private static void assertUnique(LinkedHashSet<Long> seen, Long value) {
987 if (!seen.add(value)) {
988 fail("found duplicate time " + value + " in series " + seen.toString());
989 }
990 }
991
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700992 private static void assertNotificationType(int expected, String actualTag) {
993 assertEquals(
994 Integer.toString(expected), actualTag.substring(actualTag.lastIndexOf(':') + 1));
995 }
996
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700997 private long getElapsedRealtime() {
998 return mElapsedRealtime;
999 }
1000
1001 private void setCurrentTimeMillis(long currentTimeMillis) {
1002 mStartTime = currentTimeMillis;
1003 mElapsedRealtime = 0L;
1004 }
1005
1006 private long currentTimeMillis() {
1007 return mStartTime + mElapsedRealtime;
1008 }
1009
1010 private void incrementCurrentTime(long duration) {
1011 mElapsedRealtime += duration;
1012 }
1013
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001014 private void replay() {
Felipe Lemef8dd7b42016-08-10 13:00:32 -07001015 EasyMock.replay(mActivityManager, mStatsService, mPolicyListener, mNetworkManager, mTime,
1016 mConnManager, mNotifManager);
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001017 }
1018
1019 private void verifyAndReset() {
Felipe Lemef8dd7b42016-08-10 13:00:32 -07001020 EasyMock.verify(mActivityManager, mStatsService, mPolicyListener, mNetworkManager, mTime,
1021 mConnManager, mNotifManager);
1022 EasyMock.reset(mActivityManager, mStatsService, mPolicyListener, mNetworkManager, mTime,
1023 mConnManager, mNotifManager);
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001024 }
1025}