blob: 72a458bdb96cc8bbdadacd2fc38732b27fd967a9 [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 Sharkeyaf82ea22011-08-04 15:38:48 -070023import static android.net.NetworkPolicy.LIMIT_DISABLED;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070024import static android.net.NetworkPolicy.WARNING_DISABLED;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070025import static android.net.NetworkPolicyManager.POLICY_NONE;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070026import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070027import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070028import static android.net.NetworkPolicyManager.RULE_REJECT_METERED;
Jeff Sharkeycd2ca402011-06-10 15:14:07 -070029import static android.net.NetworkPolicyManager.computeLastCycleBoundary;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070030import static android.net.NetworkPolicyManager.computeNextCycleBoundary;
Jeff Sharkey241dde22012-02-03 14:50:07 -080031import static android.net.TrafficStats.KB_IN_BYTES;
32import static android.net.TrafficStats.MB_IN_BYTES;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070033import static android.text.format.DateUtils.DAY_IN_MILLIS;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070034import static android.text.format.DateUtils.MINUTE_IN_MILLIS;
Jeff Sharkey9bf31502012-03-09 17:07:21 -080035import static android.text.format.Time.TIMEZONE_UTC;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070036import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT;
37import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT_SNOOZED;
38import static com.android.server.net.NetworkPolicyManagerService.TYPE_WARNING;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -070039import static org.easymock.EasyMock.anyInt;
Jeff Sharkey0cf6de02012-05-04 15:03:30 -070040import static org.easymock.EasyMock.anyLong;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -070041import static org.easymock.EasyMock.aryEq;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070042import static org.easymock.EasyMock.capture;
43import static org.easymock.EasyMock.createMock;
44import static org.easymock.EasyMock.eq;
45import static org.easymock.EasyMock.expect;
46import static org.easymock.EasyMock.expectLastCall;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -070047import static org.easymock.EasyMock.isA;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070048
49import android.app.IActivityManager;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -070050import android.app.INotificationManager;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070051import android.app.IProcessObserver;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070052import android.app.Notification;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070053import android.content.Intent;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070054import android.content.pm.PackageInfo;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070055import android.content.pm.PackageManager;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070056import android.content.pm.Signature;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070057import android.net.ConnectivityManager;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070058import android.net.IConnectivityManager;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -070059import android.net.INetworkManagementEventObserver;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070060import android.net.INetworkPolicyListener;
Jeff Sharkey75279902011-05-24 18:39:45 -070061import android.net.INetworkStatsService;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070062import android.net.LinkProperties;
63import android.net.NetworkInfo;
64import android.net.NetworkInfo.DetailedState;
65import android.net.NetworkPolicy;
66import android.net.NetworkState;
67import android.net.NetworkStats;
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -070068import android.net.NetworkTemplate;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070069import android.os.Binder;
Ashish Sharma50fd36d2011-06-15 19:34:53 -070070import android.os.INetworkManagementService;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070071import android.os.IPowerManager;
Jeff Sharkey163e6442011-10-31 16:37:52 -070072import android.os.MessageQueue.IdleHandler;
Dianne Hackbornf02b60a2012-08-16 10:48:27 -070073import android.os.UserHandle;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070074import android.test.AndroidTestCase;
75import android.test.mock.MockPackageManager;
76import android.test.suitebuilder.annotation.LargeTest;
77import android.test.suitebuilder.annotation.Suppress;
Jeff Sharkey21c9c452011-06-07 12:26:43 -070078import android.text.format.Time;
79import android.util.TrustedTime;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070080
81import com.android.server.net.NetworkPolicyManagerService;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070082import com.google.common.util.concurrent.AbstractFuture;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070083
84import org.easymock.Capture;
85import org.easymock.EasyMock;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070086import org.easymock.IAnswer;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070087
Jeff Sharkey21c9c452011-06-07 12:26:43 -070088import java.io.File;
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -070089import java.util.LinkedHashSet;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070090import java.util.concurrent.ExecutionException;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070091import java.util.concurrent.Future;
Jeff Sharkey4414cea2011-06-24 17:05:24 -070092import java.util.concurrent.TimeUnit;
93import java.util.concurrent.TimeoutException;
Jeff Sharkey163e6442011-10-31 16:37:52 -070094import java.util.logging.Handler;
Jeff Sharkey9599cc52011-05-22 14:59:31 -070095
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -070096import libcore.io.IoUtils;
97
Jeff Sharkey9599cc52011-05-22 14:59:31 -070098/**
99 * Tests for {@link NetworkPolicyManagerService}.
100 */
101@LargeTest
102public class NetworkPolicyManagerServiceTest extends AndroidTestCase {
103 private static final String TAG = "NetworkPolicyManagerServiceTest";
104
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700105 private static final long TEST_START = 1194220800000L;
106 private static final String TEST_IFACE = "test0";
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700107 private static final String TEST_SSID = "AndroidAP";
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700108
Jeff Sharkeye8914c32012-05-01 16:26:09 -0700109 private static NetworkTemplate sTemplateWifi = NetworkTemplate.buildTemplateWifi(TEST_SSID);
Jeff Sharkey1b5a2a92011-06-18 18:34:16 -0700110
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700111 private BroadcastInterceptingContext mServiceContext;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700112 private File mPolicyDir;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700113
114 private IActivityManager mActivityManager;
115 private IPowerManager mPowerManager;
Jeff Sharkey75279902011-05-24 18:39:45 -0700116 private INetworkStatsService mStatsService;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700117 private INetworkManagementService mNetworkManager;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700118 private INetworkPolicyListener mPolicyListener;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700119 private TrustedTime mTime;
120 private IConnectivityManager mConnManager;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700121 private INotificationManager mNotifManager;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700122
123 private NetworkPolicyManagerService mService;
124 private IProcessObserver mProcessObserver;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700125 private INetworkManagementEventObserver mNetworkObserver;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700126
127 private Binder mStubBinder = new Binder();
128
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700129 private long mStartTime;
130 private long mElapsedRealtime;
131
Jeff Sharkeycae04a22012-03-22 15:18:52 -0700132 private static final int USER_ID = 0;
Jeff Sharkeycae04a22012-03-22 15:18:52 -0700133
134 private static final int APP_ID_A = android.os.Process.FIRST_APPLICATION_UID + 800;
135 private static final int APP_ID_B = android.os.Process.FIRST_APPLICATION_UID + 801;
136
Dianne Hackbornf02b60a2012-08-16 10:48:27 -0700137 private static final int UID_A = UserHandle.getUid(USER_ID, APP_ID_A);
138 private static final int UID_B = UserHandle.getUid(USER_ID, APP_ID_B);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700139
140 private static final int PID_1 = 400;
141 private static final int PID_2 = 401;
142 private static final int PID_3 = 402;
143
144 @Override
145 public void setUp() throws Exception {
146 super.setUp();
147
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700148 setCurrentTimeMillis(TEST_START);
149
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700150 // intercept various broadcasts, and pretend that uids have packages
151 mServiceContext = new BroadcastInterceptingContext(getContext()) {
152 @Override
153 public PackageManager getPackageManager() {
154 return new MockPackageManager() {
155 @Override
156 public String[] getPackagesForUid(int uid) {
157 return new String[] { "com.example" };
158 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700159
160 @Override
161 public PackageInfo getPackageInfo(String packageName, int flags) {
162 final PackageInfo info = new PackageInfo();
163 final Signature signature;
164 if ("android".equals(packageName)) {
165 signature = new Signature("F00D");
166 } else {
167 signature = new Signature("DEAD");
168 }
169 info.signatures = new Signature[] { signature };
170 return info;
171 }
Jeff Sharkeycae04a22012-03-22 15:18:52 -0700172
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700173 };
174 }
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700175
176 @Override
177 public void startActivity(Intent intent) {
178 // ignored
179 }
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700180 };
181
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700182 mPolicyDir = getContext().getFilesDir();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700183 if (mPolicyDir.exists()) {
184 IoUtils.deleteContents(mPolicyDir);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700185 }
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700186
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700187 mActivityManager = createMock(IActivityManager.class);
188 mPowerManager = createMock(IPowerManager.class);
Jeff Sharkey75279902011-05-24 18:39:45 -0700189 mStatsService = createMock(INetworkStatsService.class);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700190 mNetworkManager = createMock(INetworkManagementService.class);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700191 mPolicyListener = createMock(INetworkPolicyListener.class);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700192 mTime = createMock(TrustedTime.class);
193 mConnManager = createMock(IConnectivityManager.class);
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700194 mNotifManager = createMock(INotificationManager.class);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700195
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700196 mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mPowerManager,
197 mStatsService, mNetworkManager, mTime, mPolicyDir, true);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700198 mService.bindConnectivityManager(mConnManager);
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700199 mService.bindNotificationManager(mNotifManager);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700200
201 // RemoteCallbackList needs a binder to use as key
202 expect(mPolicyListener.asBinder()).andReturn(mStubBinder).atLeastOnce();
203 replay();
204 mService.registerListener(mPolicyListener);
205 verifyAndReset();
206
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700207 // catch IProcessObserver during systemReady()
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700208 final Capture<IProcessObserver> processObserver = new Capture<IProcessObserver>();
209 mActivityManager.registerProcessObserver(capture(processObserver));
210 expectLastCall().atLeastOnce();
211
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700212 // catch INetworkManagementEventObserver during systemReady()
213 final Capture<INetworkManagementEventObserver> networkObserver = new Capture<
214 INetworkManagementEventObserver>();
215 mNetworkManager.registerObserver(capture(networkObserver));
216 expectLastCall().atLeastOnce();
217
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700218 // expect to answer screen status during systemReady()
Narayan Kamath24c089a2014-04-11 11:42:33 +0100219 expect(mPowerManager.isInteractive()).andReturn(true).atLeastOnce();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700220 expect(mNetworkManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700221 expectCurrentTime();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700222
223 replay();
224 mService.systemReady();
225 verifyAndReset();
226
227 mProcessObserver = processObserver.getValue();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700228 mNetworkObserver = networkObserver.getValue();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700229
230 }
231
232 @Override
233 public void tearDown() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700234 for (File file : mPolicyDir.listFiles()) {
235 file.delete();
236 }
237
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700238 mServiceContext = null;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700239 mPolicyDir = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700240
241 mActivityManager = null;
242 mPowerManager = null;
Jeff Sharkey75279902011-05-24 18:39:45 -0700243 mStatsService = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700244 mPolicyListener = null;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700245 mTime = null;
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700246
247 mService = null;
248 mProcessObserver = null;
249
250 super.tearDown();
251 }
252
253 @Suppress
254 public void testPolicyChangeTriggersBroadcast() throws Exception {
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700255 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700256
257 // change background policy and expect broadcast
258 final Future<Intent> backgroundChanged = mServiceContext.nextBroadcastIntent(
259 ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED);
260
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700261 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700262
263 backgroundChanged.get();
264 }
265
266 public void testPidForegroundCombined() throws Exception {
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800267 IdleFuture idle;
268
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700269 // push all uid into background
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800270 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700271 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
272 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
273 mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800274 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700275 assertFalse(mService.isUidForeground(UID_A));
276 assertFalse(mService.isUidForeground(UID_B));
277
278 // push one of the shared pids into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800279 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700280 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800281 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700282 assertTrue(mService.isUidForeground(UID_A));
283 assertFalse(mService.isUidForeground(UID_B));
284
285 // and swap another uid into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800286 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700287 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
288 mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800289 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700290 assertFalse(mService.isUidForeground(UID_A));
291 assertTrue(mService.isUidForeground(UID_B));
292
293 // push both pid into foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800294 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700295 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
296 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800297 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700298 assertTrue(mService.isUidForeground(UID_A));
299
300 // pull one out, should still be foreground
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800301 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700302 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800303 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700304 assertTrue(mService.isUidForeground(UID_A));
305
306 // pull final pid out, should now be background
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800307 idle = expectIdle();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700308 mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800309 idle.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700310 assertFalse(mService.isUidForeground(UID_A));
311 }
312
313 public void testScreenChangesRules() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700314 Future<Void> future;
315
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700316 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700317 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700318 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700319 replay();
320 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700321 future.get();
322 verifyAndReset();
323
324 // push strict policy for foreground uid, verify ALLOW rule
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700325 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700326 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700327 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
328 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700329 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700330 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700331 verifyAndReset();
332
333 // now turn screen off and verify REJECT rule
Narayan Kamath24c089a2014-04-11 11:42:33 +0100334 expect(mPowerManager.isInteractive()).andReturn(false).atLeastOnce();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700335 expectSetUidNetworkRules(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700336 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700337 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700338 replay();
339 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_OFF));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700340 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700341 verifyAndReset();
342
343 // and turn screen back on, verify ALLOW rule restored
Narayan Kamath24c089a2014-04-11 11:42:33 +0100344 expect(mPowerManager.isInteractive()).andReturn(true).atLeastOnce();
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700345 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700346 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700347 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700348 replay();
349 mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_ON));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700350 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700351 verifyAndReset();
352 }
353
354 public void testPolicyNone() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700355 Future<Void> future;
356
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700357 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700358 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700359 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
360 replay();
361 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
362 future.get();
363 verifyAndReset();
364
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700365 // POLICY_NONE should RULE_ALLOW in foreground
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700366 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700367 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700368 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700369 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700370 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700371 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700372 verifyAndReset();
373
374 // POLICY_NONE should RULE_ALLOW in background
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700375 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700376 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700377 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700378 replay();
379 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700380 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700381 verifyAndReset();
382 }
383
384 public void testPolicyReject() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700385 Future<Void> future;
386
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700387 // POLICY_REJECT should RULE_ALLOW in background
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700388 expectSetUidNetworkRules(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700389 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700390 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700391 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700392 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700393 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700394 verifyAndReset();
395
396 // POLICY_REJECT should RULE_ALLOW in foreground
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700397 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700398 expectSetUidForeground(UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700399 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700400 replay();
401 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700402 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700403 verifyAndReset();
404
405 // POLICY_REJECT should RULE_REJECT in background
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700406 expectSetUidNetworkRules(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700407 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700408 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700409 replay();
410 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700411 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700412 verifyAndReset();
413 }
414
415 public void testPolicyRejectAddRemove() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700416 Future<Void> future;
417
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700418 // POLICY_NONE should have RULE_ALLOW in background
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700419 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700420 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700421 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700422 replay();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700423 mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false);
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700424 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700425 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700426 verifyAndReset();
427
428 // adding POLICY_REJECT should cause RULE_REJECT
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700429 expectSetUidNetworkRules(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700430 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700431 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700432 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700433 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700434 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700435 verifyAndReset();
436
437 // removing POLICY_REJECT should return us to RULE_ALLOW
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700438 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700439 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700440 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700441 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700442 mService.setUidPolicy(APP_ID_A, POLICY_NONE);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700443 future.get();
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700444 verifyAndReset();
445 }
446
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700447 public void testLastCycleBoundaryThisMonth() throws Exception {
448 // assume cycle day of "5th", which should be in same month
449 final long currentTime = parseTime("2007-11-14T00:00:00.000Z");
450 final long expectedCycle = parseTime("2007-11-05T00:00:00.000Z");
451
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700452 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800453 sTemplateWifi, 5, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700454 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700455 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700456 }
457
458 public void testLastCycleBoundaryLastMonth() throws Exception {
459 // assume cycle day of "20th", which should be in last month
460 final long currentTime = parseTime("2007-11-14T00:00:00.000Z");
461 final long expectedCycle = parseTime("2007-10-20T00:00:00.000Z");
462
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700463 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800464 sTemplateWifi, 20, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700465 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700466 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700467 }
468
469 public void testLastCycleBoundaryThisMonthFebruary() throws Exception {
470 // assume cycle day of "30th" in february; should go to january
471 final long currentTime = parseTime("2007-02-14T00:00:00.000Z");
472 final long expectedCycle = parseTime("2007-01-30T00:00:00.000Z");
473
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700474 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800475 sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700476 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700477 assertTimeEquals(expectedCycle, actualCycle);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700478 }
479
480 public void testLastCycleBoundaryLastMonthFebruary() throws Exception {
481 // assume cycle day of "30th" in february, which should clamp
482 final long currentTime = parseTime("2007-03-14T00:00:00.000Z");
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700483 final long expectedCycle = parseTime("2007-02-28T23:59:59.000Z");
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700484
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700485 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800486 sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700487 final long actualCycle = computeLastCycleBoundary(currentTime, policy);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700488 assertTimeEquals(expectedCycle, actualCycle);
489 }
490
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800491 public void testCycleBoundaryLeapYear() throws Exception {
492 final NetworkPolicy policy = new NetworkPolicy(
493 sTemplateWifi, 29, TIMEZONE_UTC, 1024L, 1024L, false);
494
495 assertTimeEquals(parseTime("2012-01-29T00:00:00.000Z"),
496 computeNextCycleBoundary(parseTime("2012-01-14T00:00:00.000Z"), policy));
497 assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"),
498 computeNextCycleBoundary(parseTime("2012-02-14T00:00:00.000Z"), policy));
499 assertTimeEquals(parseTime("2012-02-29T00:00:00.000Z"),
500 computeLastCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy));
501 assertTimeEquals(parseTime("2012-03-29T00:00:00.000Z"),
502 computeNextCycleBoundary(parseTime("2012-03-14T00:00:00.000Z"), policy));
503
504 assertTimeEquals(parseTime("2007-01-29T00:00:00.000Z"),
505 computeNextCycleBoundary(parseTime("2007-01-14T00:00:00.000Z"), policy));
506 assertTimeEquals(parseTime("2007-02-28T23:59:59.000Z"),
507 computeNextCycleBoundary(parseTime("2007-02-14T00:00:00.000Z"), policy));
508 assertTimeEquals(parseTime("2007-02-28T23:59:59.000Z"),
509 computeLastCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy));
510 assertTimeEquals(parseTime("2007-03-29T00:00:00.000Z"),
511 computeNextCycleBoundary(parseTime("2007-03-14T00:00:00.000Z"), policy));
512 }
513
514 public void testNextCycleTimezoneAfterUtc() throws Exception {
515 // US/Central is UTC-6
516 final NetworkPolicy policy = new NetworkPolicy(
517 sTemplateWifi, 10, "US/Central", 1024L, 1024L, false);
518 assertTimeEquals(parseTime("2012-01-10T06:00:00.000Z"),
519 computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
520 }
521
522 public void testNextCycleTimezoneBeforeUtc() throws Exception {
523 // Israel is UTC+2
524 final NetworkPolicy policy = new NetworkPolicy(
525 sTemplateWifi, 10, "Israel", 1024L, 1024L, false);
526 assertTimeEquals(parseTime("2012-01-09T22:00:00.000Z"),
527 computeNextCycleBoundary(parseTime("2012-01-05T00:00:00.000Z"), policy));
528 }
529
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700530 public void testNextCycleSane() throws Exception {
531 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800532 sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700533 final LinkedHashSet<Long> seen = new LinkedHashSet<Long>();
534
535 // walk forwards, ensuring that cycle boundaries don't get stuck
536 long currentCycle = computeNextCycleBoundary(parseTime("2011-08-01T00:00:00.000Z"), policy);
537 for (int i = 0; i < 128; i++) {
538 long nextCycle = computeNextCycleBoundary(currentCycle, policy);
539 assertEqualsFuzzy(DAY_IN_MILLIS * 30, nextCycle - currentCycle, DAY_IN_MILLIS * 3);
540 assertUnique(seen, nextCycle);
541 currentCycle = nextCycle;
542 }
543 }
544
545 public void testLastCycleSane() throws Exception {
546 final NetworkPolicy policy = new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800547 sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false);
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700548 final LinkedHashSet<Long> seen = new LinkedHashSet<Long>();
549
550 // walk backwards, ensuring that cycle boundaries look sane
551 long currentCycle = computeLastCycleBoundary(parseTime("2011-08-04T00:00:00.000Z"), policy);
552 for (int i = 0; i < 128; i++) {
553 long lastCycle = computeLastCycleBoundary(currentCycle, policy);
554 assertEqualsFuzzy(DAY_IN_MILLIS * 30, currentCycle - lastCycle, DAY_IN_MILLIS * 3);
555 assertUnique(seen, lastCycle);
556 currentCycle = lastCycle;
557 }
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700558 }
559
Jeff Sharkey15399052013-01-15 14:15:53 -0800560 public void testCycleTodayJanuary() throws Exception {
561 final NetworkPolicy policy = new NetworkPolicy(
562 sTemplateWifi, 14, "US/Pacific", 1024L, 1024L, false);
563
564 assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"),
565 computeNextCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy));
566 assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"),
567 computeNextCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy));
568 assertTimeEquals(parseTime("2013-02-14T00:00:00.000-08:00"),
569 computeNextCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy));
570
571 assertTimeEquals(parseTime("2012-12-14T00:00:00.000-08:00"),
572 computeLastCycleBoundary(parseTime("2013-01-13T23:59:59.000-08:00"), policy));
573 assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"),
574 computeLastCycleBoundary(parseTime("2013-01-14T00:00:01.000-08:00"), policy));
575 assertTimeEquals(parseTime("2013-01-14T00:00:00.000-08:00"),
576 computeLastCycleBoundary(parseTime("2013-01-14T15:11:00.000-08:00"), policy));
577 }
578
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700579 public void testNetworkPolicyAppliedCycleLastMonth() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700580 NetworkState[] state = null;
581 NetworkStats stats = null;
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700582 Future<Void> future;
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700583
584 final long TIME_FEB_15 = 1171497600000L;
585 final long TIME_MAR_10 = 1173484800000L;
586 final int CYCLE_DAY = 15;
587
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700588 setCurrentTimeMillis(TIME_MAR_10);
589
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700590 // first, pretend that wifi network comes online. no policy active,
591 // which means we shouldn't push limit to interface.
592 state = new NetworkState[] { buildWifi() };
593 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700594 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700595 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700596 expectAdvisePersistThreshold();
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700597 future = expectMeteredIfacesChanged();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700598
599 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900600 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700601 future.get();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700602 verifyAndReset();
603
604 // now change cycle to be on 15th, and test in early march, to verify we
605 // pick cycle day in previous month.
606 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700607 expectCurrentTime();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700608
609 // pretend that 512 bytes total have happened
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700610 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700611 .addIfaceValues(TEST_IFACE, 256L, 2L, 256L, 2L);
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700612 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, TIME_MAR_10))
613 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800614 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700615
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700616 // TODO: consider making strongly ordered mock
617 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700618 expectSetInterfaceQuota(TEST_IFACE, (2 * MB_IN_BYTES) - 512);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700619
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700620 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700621 expectAdvisePersistThreshold();
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700622 future = expectMeteredIfacesChanged(TEST_IFACE);
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700623
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700624 replay();
Jeff Sharkey163e6442011-10-31 16:37:52 -0700625 setNetworkPolicies(new NetworkPolicy(
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800626 sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 * MB_IN_BYTES, 2 * MB_IN_BYTES, false));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700627 future.get();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700628 verifyAndReset();
629 }
630
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700631 public void testUidRemovedPolicyCleared() throws Exception {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700632 Future<Void> future;
633
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700634 // POLICY_REJECT should RULE_REJECT in background
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700635 expectSetUidNetworkRules(UID_A, true);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700636 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700637 future = expectRulesChanged(UID_A, RULE_REJECT_METERED);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700638 replay();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700639 mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700640 future.get();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700641 verifyAndReset();
642
643 // uninstall should clear RULE_REJECT
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700644 expectSetUidNetworkRules(UID_A, false);
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700645 expectSetUidForeground(UID_A, false);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700646 future = expectRulesChanged(UID_A, RULE_ALLOW_ALL);
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700647 replay();
648 final Intent intent = new Intent(ACTION_UID_REMOVED);
649 intent.putExtra(EXTRA_UID, UID_A);
650 mServiceContext.sendBroadcast(intent);
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700651 future.get();
Jeff Sharkeyb09540f2011-06-19 01:08:12 -0700652 verifyAndReset();
653 }
654
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700655 public void testOverWarningLimitNotification() throws Exception {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700656 NetworkState[] state = null;
657 NetworkStats stats = null;
658 Future<Void> future;
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800659 Future<String> tagFuture;
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700660
661 final long TIME_FEB_15 = 1171497600000L;
662 final long TIME_MAR_10 = 1173484800000L;
663 final int CYCLE_DAY = 15;
664
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700665 setCurrentTimeMillis(TIME_MAR_10);
666
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700667 // assign wifi policy
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700668 state = new NetworkState[] {};
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700669 stats = new NetworkStats(getElapsedRealtime(), 1)
670 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700671
672 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700673 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700674 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700675 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
676 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800677 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700678
679 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700680 expectAdvisePersistThreshold();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700681 future = expectMeteredIfacesChanged();
682
683 replay();
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800684 setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1
685 * MB_IN_BYTES, 2 * MB_IN_BYTES, false));
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700686 future.get();
687 verifyAndReset();
688 }
689
690 // bring up wifi network
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700691 incrementCurrentTime(MINUTE_IN_MILLIS);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700692 state = new NetworkState[] { buildWifi() };
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700693 stats = new NetworkStats(getElapsedRealtime(), 1)
694 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700695
696 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700697 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700698 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700699 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
700 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800701 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700702
703 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey163e6442011-10-31 16:37:52 -0700704 expectSetInterfaceQuota(TEST_IFACE, 2 * MB_IN_BYTES);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700705
706 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700707 expectAdvisePersistThreshold();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700708 future = expectMeteredIfacesChanged(TEST_IFACE);
709
710 replay();
Erik Klinef851d6d2015-04-20 16:03:48 +0900711 mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION));
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700712 future.get();
713 verifyAndReset();
714 }
715
716 // go over warning, which should kick notification
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700717 incrementCurrentTime(MINUTE_IN_MILLIS);
718 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700719 .addIfaceValues(TEST_IFACE, 1536 * KB_IN_BYTES, 15L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700720
721 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700722 expectCurrentTime();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700723 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
724 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800725 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700726
727 expectForceUpdate();
728 expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800729 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700730
731 replay();
732 mNetworkObserver.limitReached(null, TEST_IFACE);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800733 assertNotificationType(TYPE_WARNING, tagFuture.get());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700734 verifyAndReset();
735 }
736
737 // go over limit, which should kick notification and dialog
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700738 incrementCurrentTime(MINUTE_IN_MILLIS);
739 stats = new NetworkStats(getElapsedRealtime(), 1)
Jeff Sharkey163e6442011-10-31 16:37:52 -0700740 .addIfaceValues(TEST_IFACE, 5 * MB_IN_BYTES, 512L, 0L, 0L);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700741
742 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700743 expectCurrentTime();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700744 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
745 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800746 expectPolicyDataEnable(TYPE_WIFI, false);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700747
748 expectForceUpdate();
749 expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800750 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700751
752 replay();
753 mNetworkObserver.limitReached(null, TEST_IFACE);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800754 assertNotificationType(TYPE_LIMIT, tagFuture.get());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700755 verifyAndReset();
756 }
757
758 // now snooze policy, which should remove quota
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700759 incrementCurrentTime(MINUTE_IN_MILLIS);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700760
761 {
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700762 expectCurrentTime();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700763 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700764 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
765 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800766 expectPolicyDataEnable(TYPE_WIFI, true);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700767
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700768 // snoozed interface still has high quota so background data is
769 // still restricted.
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700770 expectRemoveInterfaceQuota(TEST_IFACE);
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700771 expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE);
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700772 expectAdvisePersistThreshold();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800773 expectMeteredIfacesChanged(TEST_IFACE);
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700774
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800775 future = expectClearNotifications();
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800776 tagFuture = expectEnqueueNotification();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700777
778 replay();
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800779 mService.snoozeLimit(sTemplateWifi);
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800780 assertNotificationType(TYPE_LIMIT_SNOOZED, tagFuture.get());
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800781 future.get();
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700782 verifyAndReset();
783 }
784 }
785
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800786 public void testMeteredNetworkWithoutLimit() throws Exception {
787 NetworkState[] state = null;
788 NetworkStats stats = null;
789 Future<Void> future;
790 Future<String> tagFuture;
791
792 final long TIME_FEB_15 = 1171497600000L;
793 final long TIME_MAR_10 = 1173484800000L;
794 final int CYCLE_DAY = 15;
795
796 setCurrentTimeMillis(TIME_MAR_10);
797
798 // bring up wifi network with metered policy
799 state = new NetworkState[] { buildWifi() };
800 stats = new NetworkStats(getElapsedRealtime(), 1)
801 .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L);
802
803 {
804 expectCurrentTime();
805 expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce();
Jeff Sharkeyb52e3e52012-04-06 11:12:08 -0700806 expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis()))
807 .andReturn(stats.getTotalBytes()).atLeastOnce();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800808 expectPolicyDataEnable(TYPE_WIFI, true);
809
810 expectRemoveInterfaceQuota(TEST_IFACE);
811 expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE);
812
813 expectClearNotifications();
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700814 expectAdvisePersistThreshold();
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800815 future = expectMeteredIfacesChanged(TEST_IFACE);
816
817 replay();
Jeff Sharkey9bf31502012-03-09 17:07:21 -0800818 setNetworkPolicies(new NetworkPolicy(
819 sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED,
820 true));
Jeff Sharkeyf60d0af2011-11-30 15:28:02 -0800821 future.get();
822 verifyAndReset();
823 }
824 }
825
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700826 private static long parseTime(String time) {
827 final Time result = new Time();
828 result.parse3339(time);
829 return result.toMillis(true);
830 }
831
Jeff Sharkeyaf11d482011-06-13 00:14:31 -0700832 private void setNetworkPolicies(NetworkPolicy... policies) {
833 mService.setNetworkPolicies(policies);
834 }
835
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700836 private static NetworkState buildWifi() {
837 final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null);
838 info.setDetailedState(DetailedState.CONNECTED, null, null);
839 final LinkProperties prop = new LinkProperties();
840 prop.setInterfaceName(TEST_IFACE);
Sreeram Ramachandran21b5ee32014-11-12 22:31:52 -0800841 return new NetworkState(info, prop, null, null, null, TEST_SSID);
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700842 }
843
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700844 private void expectCurrentTime() throws Exception {
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700845 expect(mTime.forceRefresh()).andReturn(false).anyTimes();
846 expect(mTime.hasCache()).andReturn(true).anyTimes();
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700847 expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes();
Jeff Sharkey21c9c452011-06-07 12:26:43 -0700848 expect(mTime.getCacheAge()).andReturn(0L).anyTimes();
849 expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes();
850 }
851
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700852 private void expectForceUpdate() throws Exception {
853 mStatsService.forceUpdate();
854 expectLastCall().atLeastOnce();
855 }
856
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800857 private Future<Void> expectClearNotifications() throws Exception {
858 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyd0c6ccb2012-09-14 16:26:37 -0700859 mNotifManager.cancelNotificationWithTag(
860 isA(String.class), isA(String.class), anyInt(), anyInt());
Jeff Sharkeyc506ff62011-11-17 11:59:29 -0800861 expectLastCall().andAnswer(future).anyTimes();
862 return future;
Jeff Sharkey2ef2aeb2011-06-15 11:01:31 -0700863 }
864
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800865 private Future<String> expectEnqueueNotification() throws Exception {
866 final FutureCapture<String> tag = new FutureCapture<String>();
Dianne Hackbornf265ea92013-01-31 15:00:51 -0800867 mNotifManager.enqueueNotificationWithTag(isA(String.class), isA(String.class),
868 capture(tag.capture), anyInt(),
Dianne Hackborn41203752012-08-31 14:05:51 -0700869 isA(Notification.class), isA(int[].class), UserHandle.myUserId());
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700870 return tag;
871 }
872
873 private void expectSetInterfaceQuota(String iface, long quotaBytes) throws Exception {
874 mNetworkManager.setInterfaceQuota(iface, quotaBytes);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700875 expectLastCall().atLeastOnce();
876 }
877
878 private void expectRemoveInterfaceQuota(String iface) throws Exception {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700879 mNetworkManager.removeInterfaceQuota(iface);
880 expectLastCall().atLeastOnce();
881 }
882
883 private void expectSetInterfaceAlert(String iface, long alertBytes) throws Exception {
884 mNetworkManager.setInterfaceAlert(iface, alertBytes);
885 expectLastCall().atLeastOnce();
886 }
887
888 private void expectRemoveInterfaceAlert(String iface) throws Exception {
889 mNetworkManager.removeInterfaceAlert(iface);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700890 expectLastCall().atLeastOnce();
891 }
892
893 private void expectSetUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces)
894 throws Exception {
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -0700895 mNetworkManager.setUidNetworkRules(uid, rejectOnQuotaInterfaces);
Jeff Sharkeyb3f19ca2011-06-29 23:54:13 -0700896 expectLastCall().atLeastOnce();
897 }
898
Jeff Sharkeyb5d55e32011-08-10 17:53:27 -0700899 private void expectSetUidForeground(int uid, boolean uidForeground) throws Exception {
900 mStatsService.setUidForeground(uid, uidForeground);
901 expectLastCall().atLeastOnce();
902 }
903
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700904 private Future<Void> expectRulesChanged(int uid, int policy) throws Exception {
905 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700906 mPolicyListener.onUidRulesChanged(eq(uid), eq(policy));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700907 expectLastCall().andAnswer(future);
908 return future;
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700909 }
910
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700911 private Future<Void> expectMeteredIfacesChanged(String... ifaces) throws Exception {
912 final FutureAnswer future = new FutureAnswer();
Jeff Sharkeyfdfef572011-06-16 15:07:48 -0700913 mPolicyListener.onMeteredIfacesChanged(aryEq(ifaces));
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700914 expectLastCall().andAnswer(future);
915 return future;
916 }
917
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800918 private Future<Void> expectPolicyDataEnable(int type, boolean enabled) throws Exception {
Jeff Sharkey32566012014-12-02 18:30:14 -0800919 // TODO: bring back this test
920 return null;
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700921 }
922
Jeff Sharkey0cf6de02012-05-04 15:03:30 -0700923 private void expectAdvisePersistThreshold() throws Exception {
924 mStatsService.advisePersistThreshold(anyLong());
925 expectLastCall().anyTimes();
926 }
927
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800928 private static class TestAbstractFuture<T> extends AbstractFuture<T> {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700929 @Override
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800930 public T get() throws InterruptedException, ExecutionException {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700931 try {
932 return get(5, TimeUnit.SECONDS);
933 } catch (TimeoutException e) {
934 throw new RuntimeException(e);
935 }
936 }
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800937 }
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700938
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800939 private static class FutureAnswer extends TestAbstractFuture<Void> implements IAnswer<Void> {
Jeff Sharkey4414cea2011-06-24 17:05:24 -0700940 @Override
941 public Void answer() {
942 set(null);
943 return null;
944 }
Jeff Sharkey9599cc52011-05-22 14:59:31 -0700945 }
946
Jeff Sharkey7ee86582011-11-14 18:02:21 -0800947 private static class FutureCapture<T> extends TestAbstractFuture<T> {
948 public Capture<T> capture = new Capture<T>() {
949 @Override
950 public void setValue(T value) {
951 super.setValue(value);
952 set(value);
953 }
954 };
955 }
956
Jeff Sharkey163e6442011-10-31 16:37:52 -0700957 private static class IdleFuture extends AbstractFuture<Void> implements IdleHandler {
958 @Override
959 public Void get() throws InterruptedException, ExecutionException {
960 try {
961 return get(5, TimeUnit.SECONDS);
962 } catch (TimeoutException e) {
963 throw new RuntimeException(e);
964 }
965 }
966
Jeff Sharkeybfdd6802012-04-09 10:49:19 -0700967 @Override
Jeff Sharkey163e6442011-10-31 16:37:52 -0700968 public boolean queueIdle() {
969 set(null);
970 return false;
971 }
972 }
973
974 /**
975 * Wait until {@link #mService} internal {@link Handler} is idle.
976 */
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800977 private IdleFuture expectIdle() {
Jeff Sharkey163e6442011-10-31 16:37:52 -0700978 final IdleFuture future = new IdleFuture();
979 mService.addIdleHandler(future);
Jeff Sharkey0e2e5f82012-02-02 16:02:51 -0800980 return future;
Jeff Sharkey163e6442011-10-31 16:37:52 -0700981 }
982
Jeff Sharkeyaf82ea22011-08-04 15:38:48 -0700983 private static void assertTimeEquals(long expected, long actual) {
984 if (expected != actual) {
985 fail("expected " + formatTime(expected) + " but was actually " + formatTime(actual));
986 }
987 }
988
989 private static String formatTime(long millis) {
990 final Time time = new Time(Time.TIMEZONE_UTC);
991 time.set(millis);
992 return time.format3339(false);
993 }
994
995 private static void assertEqualsFuzzy(long expected, long actual, long fuzzy) {
996 final long low = expected - fuzzy;
997 final long high = expected + fuzzy;
998 if (actual < low || actual > high) {
999 fail("value " + actual + " is outside [" + low + "," + high + "]");
1000 }
1001 }
1002
1003 private static void assertUnique(LinkedHashSet<Long> seen, Long value) {
1004 if (!seen.add(value)) {
1005 fail("found duplicate time " + value + " in series " + seen.toString());
1006 }
1007 }
1008
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001009 private static void assertNotificationType(int expected, String actualTag) {
1010 assertEquals(
1011 Integer.toString(expected), actualTag.substring(actualTag.lastIndexOf(':') + 1));
1012 }
1013
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -07001014 private long getElapsedRealtime() {
1015 return mElapsedRealtime;
1016 }
1017
1018 private void setCurrentTimeMillis(long currentTimeMillis) {
1019 mStartTime = currentTimeMillis;
1020 mElapsedRealtime = 0L;
1021 }
1022
1023 private long currentTimeMillis() {
1024 return mStartTime + mElapsedRealtime;
1025 }
1026
1027 private void incrementCurrentTime(long duration) {
1028 mElapsedRealtime += duration;
1029 }
1030
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001031 private void replay() {
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001032 EasyMock.replay(mActivityManager, mPowerManager, mStatsService, mPolicyListener,
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001033 mNetworkManager, mTime, mConnManager, mNotifManager);
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001034 }
1035
1036 private void verifyAndReset() {
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001037 EasyMock.verify(mActivityManager, mPowerManager, mStatsService, mPolicyListener,
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001038 mNetworkManager, mTime, mConnManager, mNotifManager);
Ashish Sharma50fd36d2011-06-15 19:34:53 -07001039 EasyMock.reset(mActivityManager, mPowerManager, mStatsService, mPolicyListener,
Jeff Sharkey41ff7ec2011-07-25 15:21:22 -07001040 mNetworkManager, mTime, mConnManager, mNotifManager);
Jeff Sharkey9599cc52011-05-22 14:59:31 -07001041 }
1042}