Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 19 | import static android.content.Intent.ACTION_UID_REMOVED; |
| 20 | import static android.content.Intent.EXTRA_UID; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION_IMMEDIATE; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 22 | import static android.net.ConnectivityManager.TYPE_WIFI; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 23 | import static android.net.NetworkPolicy.LIMIT_DISABLED; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 24 | import static android.net.NetworkPolicy.WARNING_DISABLED; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 25 | import static android.net.NetworkPolicyManager.POLICY_NONE; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 26 | import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 27 | import static android.net.NetworkPolicyManager.RULE_ALLOW_ALL; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 28 | import static android.net.NetworkPolicyManager.RULE_REJECT_METERED; |
Jeff Sharkey | cd2ca40 | 2011-06-10 15:14:07 -0700 | [diff] [blame] | 29 | import static android.net.NetworkPolicyManager.computeLastCycleBoundary; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 30 | import static android.net.NetworkPolicyManager.computeNextCycleBoundary; |
Jeff Sharkey | 241dde2 | 2012-02-03 14:50:07 -0800 | [diff] [blame] | 31 | import static android.net.TrafficStats.KB_IN_BYTES; |
| 32 | import static android.net.TrafficStats.MB_IN_BYTES; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 33 | import static android.text.format.DateUtils.DAY_IN_MILLIS; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 34 | import static android.text.format.DateUtils.MINUTE_IN_MILLIS; |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 35 | import static android.text.format.Time.TIMEZONE_UTC; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 36 | import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT; |
| 37 | import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT_SNOOZED; |
| 38 | import static com.android.server.net.NetworkPolicyManagerService.TYPE_WARNING; |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 39 | import static org.easymock.EasyMock.anyInt; |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 40 | import static org.easymock.EasyMock.anyLong; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 41 | import static org.easymock.EasyMock.aryEq; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 42 | import static org.easymock.EasyMock.capture; |
| 43 | import static org.easymock.EasyMock.createMock; |
| 44 | import static org.easymock.EasyMock.eq; |
| 45 | import static org.easymock.EasyMock.expect; |
| 46 | import static org.easymock.EasyMock.expectLastCall; |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 47 | import static org.easymock.EasyMock.isA; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 48 | |
| 49 | import android.app.IActivityManager; |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 50 | import android.app.INotificationManager; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 51 | import android.app.IProcessObserver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 52 | import android.app.Notification; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 53 | import android.content.Intent; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 54 | import android.content.pm.PackageInfo; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 55 | import android.content.pm.PackageManager; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 56 | import android.content.pm.Signature; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 57 | import android.net.ConnectivityManager; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 58 | import android.net.IConnectivityManager; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 59 | import android.net.INetworkManagementEventObserver; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 60 | import android.net.INetworkPolicyListener; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 61 | import android.net.INetworkStatsService; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 62 | import android.net.LinkProperties; |
| 63 | import android.net.NetworkInfo; |
| 64 | import android.net.NetworkInfo.DetailedState; |
| 65 | import android.net.NetworkPolicy; |
| 66 | import android.net.NetworkState; |
| 67 | import android.net.NetworkStats; |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 68 | import android.net.NetworkTemplate; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 69 | import android.os.Binder; |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 70 | import android.os.INetworkManagementService; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 71 | import android.os.IPowerManager; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 72 | import android.os.MessageQueue.IdleHandler; |
Dianne Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 73 | import android.os.UserHandle; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 74 | import android.test.AndroidTestCase; |
| 75 | import android.test.mock.MockPackageManager; |
| 76 | import android.test.suitebuilder.annotation.LargeTest; |
| 77 | import android.test.suitebuilder.annotation.Suppress; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 78 | import android.text.format.Time; |
| 79 | import android.util.TrustedTime; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 80 | |
| 81 | import com.android.server.net.NetworkPolicyManagerService; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 82 | import com.google.common.util.concurrent.AbstractFuture; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 83 | |
| 84 | import org.easymock.Capture; |
| 85 | import org.easymock.EasyMock; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 86 | import org.easymock.IAnswer; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 87 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 88 | import java.io.File; |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 89 | import java.util.LinkedHashSet; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 90 | import java.util.concurrent.ExecutionException; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 91 | import java.util.concurrent.Future; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 92 | import java.util.concurrent.TimeUnit; |
| 93 | import java.util.concurrent.TimeoutException; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 94 | import java.util.logging.Handler; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 95 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 96 | import libcore.io.IoUtils; |
| 97 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 98 | /** |
| 99 | * Tests for {@link NetworkPolicyManagerService}. |
| 100 | */ |
| 101 | @LargeTest |
| 102 | public class NetworkPolicyManagerServiceTest extends AndroidTestCase { |
| 103 | private static final String TAG = "NetworkPolicyManagerServiceTest"; |
| 104 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 105 | private static final long TEST_START = 1194220800000L; |
| 106 | private static final String TEST_IFACE = "test0"; |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 107 | private static final String TEST_SSID = "AndroidAP"; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 108 | |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 109 | private static NetworkTemplate sTemplateWifi = NetworkTemplate.buildTemplateWifi(TEST_SSID); |
Jeff Sharkey | 1b5a2a9 | 2011-06-18 18:34:16 -0700 | [diff] [blame] | 110 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 111 | private BroadcastInterceptingContext mServiceContext; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 112 | private File mPolicyDir; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 113 | |
| 114 | private IActivityManager mActivityManager; |
| 115 | private IPowerManager mPowerManager; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 116 | private INetworkStatsService mStatsService; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 117 | private INetworkManagementService mNetworkManager; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 118 | private INetworkPolicyListener mPolicyListener; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 119 | private TrustedTime mTime; |
| 120 | private IConnectivityManager mConnManager; |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 121 | private INotificationManager mNotifManager; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 122 | |
| 123 | private NetworkPolicyManagerService mService; |
| 124 | private IProcessObserver mProcessObserver; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 125 | private INetworkManagementEventObserver mNetworkObserver; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 126 | |
| 127 | private Binder mStubBinder = new Binder(); |
| 128 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 129 | private long mStartTime; |
| 130 | private long mElapsedRealtime; |
| 131 | |
Jeff Sharkey | cae04a2 | 2012-03-22 15:18:52 -0700 | [diff] [blame] | 132 | private static final int USER_ID = 0; |
Jeff Sharkey | cae04a2 | 2012-03-22 15:18:52 -0700 | [diff] [blame] | 133 | |
| 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 Hackborn | f02b60a | 2012-08-16 10:48:27 -0700 | [diff] [blame] | 137 | 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 Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 139 | |
| 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 Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 148 | setCurrentTimeMillis(TEST_START); |
| 149 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 150 | // 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 Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 159 | |
| 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 Sharkey | cae04a2 | 2012-03-22 15:18:52 -0700 | [diff] [blame] | 172 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 173 | }; |
| 174 | } |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 175 | |
| 176 | @Override |
| 177 | public void startActivity(Intent intent) { |
| 178 | // ignored |
| 179 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 180 | }; |
| 181 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 182 | mPolicyDir = getContext().getFilesDir(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 183 | if (mPolicyDir.exists()) { |
| 184 | IoUtils.deleteContents(mPolicyDir); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 185 | } |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 186 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 187 | mActivityManager = createMock(IActivityManager.class); |
| 188 | mPowerManager = createMock(IPowerManager.class); |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 189 | mStatsService = createMock(INetworkStatsService.class); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 190 | mNetworkManager = createMock(INetworkManagementService.class); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 191 | mPolicyListener = createMock(INetworkPolicyListener.class); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 192 | mTime = createMock(TrustedTime.class); |
| 193 | mConnManager = createMock(IConnectivityManager.class); |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 194 | mNotifManager = createMock(INotificationManager.class); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 195 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 196 | mService = new NetworkPolicyManagerService(mServiceContext, mActivityManager, mPowerManager, |
| 197 | mStatsService, mNetworkManager, mTime, mPolicyDir, true); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 198 | mService.bindConnectivityManager(mConnManager); |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 199 | mService.bindNotificationManager(mNotifManager); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 200 | |
| 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 Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 207 | // catch IProcessObserver during systemReady() |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 208 | final Capture<IProcessObserver> processObserver = new Capture<IProcessObserver>(); |
| 209 | mActivityManager.registerProcessObserver(capture(processObserver)); |
| 210 | expectLastCall().atLeastOnce(); |
| 211 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 212 | // catch INetworkManagementEventObserver during systemReady() |
| 213 | final Capture<INetworkManagementEventObserver> networkObserver = new Capture< |
| 214 | INetworkManagementEventObserver>(); |
| 215 | mNetworkManager.registerObserver(capture(networkObserver)); |
| 216 | expectLastCall().atLeastOnce(); |
| 217 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 218 | // expect to answer screen status during systemReady() |
| 219 | expect(mPowerManager.isScreenOn()).andReturn(true).atLeastOnce(); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 220 | expect(mNetworkManager.isBandwidthControlEnabled()).andReturn(true).atLeastOnce(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 221 | expectCurrentTime(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 222 | |
| 223 | replay(); |
| 224 | mService.systemReady(); |
| 225 | verifyAndReset(); |
| 226 | |
| 227 | mProcessObserver = processObserver.getValue(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 228 | mNetworkObserver = networkObserver.getValue(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 229 | |
| 230 | } |
| 231 | |
| 232 | @Override |
| 233 | public void tearDown() throws Exception { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 234 | for (File file : mPolicyDir.listFiles()) { |
| 235 | file.delete(); |
| 236 | } |
| 237 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 238 | mServiceContext = null; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 239 | mPolicyDir = null; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 240 | |
| 241 | mActivityManager = null; |
| 242 | mPowerManager = null; |
Jeff Sharkey | 7527990 | 2011-05-24 18:39:45 -0700 | [diff] [blame] | 243 | mStatsService = null; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 244 | mPolicyListener = null; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 245 | mTime = null; |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 246 | |
| 247 | mService = null; |
| 248 | mProcessObserver = null; |
| 249 | |
| 250 | super.tearDown(); |
| 251 | } |
| 252 | |
| 253 | @Suppress |
| 254 | public void testPolicyChangeTriggersBroadcast() throws Exception { |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame^] | 255 | mService.setUidPolicy(APP_ID_A, POLICY_NONE); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 256 | |
| 257 | // change background policy and expect broadcast |
| 258 | final Future<Intent> backgroundChanged = mServiceContext.nextBroadcastIntent( |
| 259 | ConnectivityManager.ACTION_BACKGROUND_DATA_SETTING_CHANGED); |
| 260 | |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame^] | 261 | mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 262 | |
| 263 | backgroundChanged.get(); |
| 264 | } |
| 265 | |
| 266 | public void testPidForegroundCombined() throws Exception { |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 267 | IdleFuture idle; |
| 268 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 269 | // push all uid into background |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 270 | idle = expectIdle(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 271 | mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false); |
| 272 | mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false); |
| 273 | mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, false); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 274 | idle.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 275 | assertFalse(mService.isUidForeground(UID_A)); |
| 276 | assertFalse(mService.isUidForeground(UID_B)); |
| 277 | |
| 278 | // push one of the shared pids into foreground |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 279 | idle = expectIdle(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 280 | mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 281 | idle.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 282 | assertTrue(mService.isUidForeground(UID_A)); |
| 283 | assertFalse(mService.isUidForeground(UID_B)); |
| 284 | |
| 285 | // and swap another uid into foreground |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 286 | idle = expectIdle(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 287 | mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false); |
| 288 | mProcessObserver.onForegroundActivitiesChanged(PID_3, UID_B, true); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 289 | idle.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 290 | assertFalse(mService.isUidForeground(UID_A)); |
| 291 | assertTrue(mService.isUidForeground(UID_B)); |
| 292 | |
| 293 | // push both pid into foreground |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 294 | idle = expectIdle(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 295 | mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true); |
| 296 | mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, true); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 297 | idle.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 298 | assertTrue(mService.isUidForeground(UID_A)); |
| 299 | |
| 300 | // pull one out, should still be foreground |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 301 | idle = expectIdle(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 302 | mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 303 | idle.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 304 | assertTrue(mService.isUidForeground(UID_A)); |
| 305 | |
| 306 | // pull final pid out, should now be background |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 307 | idle = expectIdle(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 308 | mProcessObserver.onForegroundActivitiesChanged(PID_2, UID_A, false); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 309 | idle.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 310 | assertFalse(mService.isUidForeground(UID_A)); |
| 311 | } |
| 312 | |
| 313 | public void testScreenChangesRules() throws Exception { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 314 | Future<Void> future; |
| 315 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 316 | expectSetUidNetworkRules(UID_A, false); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 317 | expectSetUidForeground(UID_A, true); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 318 | future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 319 | replay(); |
| 320 | mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 321 | future.get(); |
| 322 | verifyAndReset(); |
| 323 | |
| 324 | // push strict policy for foreground uid, verify ALLOW rule |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 325 | expectSetUidNetworkRules(UID_A, false); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 326 | expectSetUidForeground(UID_A, true); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 327 | future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); |
| 328 | replay(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame^] | 329 | mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 330 | future.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 331 | verifyAndReset(); |
| 332 | |
| 333 | // now turn screen off and verify REJECT rule |
| 334 | expect(mPowerManager.isScreenOn()).andReturn(false).atLeastOnce(); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 335 | expectSetUidNetworkRules(UID_A, true); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 336 | expectSetUidForeground(UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 337 | future = expectRulesChanged(UID_A, RULE_REJECT_METERED); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 338 | replay(); |
| 339 | mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_OFF)); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 340 | future.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 341 | verifyAndReset(); |
| 342 | |
| 343 | // and turn screen back on, verify ALLOW rule restored |
| 344 | expect(mPowerManager.isScreenOn()).andReturn(true).atLeastOnce(); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 345 | expectSetUidNetworkRules(UID_A, false); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 346 | expectSetUidForeground(UID_A, true); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 347 | future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 348 | replay(); |
| 349 | mServiceContext.sendBroadcast(new Intent(Intent.ACTION_SCREEN_ON)); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 350 | future.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 351 | verifyAndReset(); |
| 352 | } |
| 353 | |
| 354 | public void testPolicyNone() throws Exception { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 355 | Future<Void> future; |
| 356 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 357 | expectSetUidNetworkRules(UID_A, false); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 358 | expectSetUidForeground(UID_A, true); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 359 | future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); |
| 360 | replay(); |
| 361 | mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true); |
| 362 | future.get(); |
| 363 | verifyAndReset(); |
| 364 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 365 | // POLICY_NONE should RULE_ALLOW in foreground |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 366 | expectSetUidNetworkRules(UID_A, false); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 367 | expectSetUidForeground(UID_A, true); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 368 | future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 369 | replay(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame^] | 370 | mService.setUidPolicy(APP_ID_A, POLICY_NONE); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 371 | future.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 372 | verifyAndReset(); |
| 373 | |
| 374 | // POLICY_NONE should RULE_ALLOW in background |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 375 | expectSetUidNetworkRules(UID_A, false); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 376 | expectSetUidForeground(UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 377 | future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 378 | replay(); |
| 379 | mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 380 | future.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 381 | verifyAndReset(); |
| 382 | } |
| 383 | |
| 384 | public void testPolicyReject() throws Exception { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 385 | Future<Void> future; |
| 386 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 387 | // POLICY_REJECT should RULE_ALLOW in background |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 388 | expectSetUidNetworkRules(UID_A, true); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 389 | expectSetUidForeground(UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 390 | future = expectRulesChanged(UID_A, RULE_REJECT_METERED); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 391 | replay(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame^] | 392 | mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 393 | future.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 394 | verifyAndReset(); |
| 395 | |
| 396 | // POLICY_REJECT should RULE_ALLOW in foreground |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 397 | expectSetUidNetworkRules(UID_A, false); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 398 | expectSetUidForeground(UID_A, true); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 399 | future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 400 | replay(); |
| 401 | mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, true); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 402 | future.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 403 | verifyAndReset(); |
| 404 | |
| 405 | // POLICY_REJECT should RULE_REJECT in background |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 406 | expectSetUidNetworkRules(UID_A, true); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 407 | expectSetUidForeground(UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 408 | future = expectRulesChanged(UID_A, RULE_REJECT_METERED); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 409 | replay(); |
| 410 | mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 411 | future.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 412 | verifyAndReset(); |
| 413 | } |
| 414 | |
| 415 | public void testPolicyRejectAddRemove() throws Exception { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 416 | Future<Void> future; |
| 417 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 418 | // POLICY_NONE should have RULE_ALLOW in background |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 419 | expectSetUidNetworkRules(UID_A, false); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 420 | expectSetUidForeground(UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 421 | future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 422 | replay(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 423 | mProcessObserver.onForegroundActivitiesChanged(PID_1, UID_A, false); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame^] | 424 | mService.setUidPolicy(APP_ID_A, POLICY_NONE); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 425 | future.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 426 | verifyAndReset(); |
| 427 | |
| 428 | // adding POLICY_REJECT should cause RULE_REJECT |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 429 | expectSetUidNetworkRules(UID_A, true); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 430 | expectSetUidForeground(UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 431 | future = expectRulesChanged(UID_A, RULE_REJECT_METERED); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 432 | replay(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame^] | 433 | mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 434 | future.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 435 | verifyAndReset(); |
| 436 | |
| 437 | // removing POLICY_REJECT should return us to RULE_ALLOW |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 438 | expectSetUidNetworkRules(UID_A, false); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 439 | expectSetUidForeground(UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 440 | future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 441 | replay(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame^] | 442 | mService.setUidPolicy(APP_ID_A, POLICY_NONE); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 443 | future.get(); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 444 | verifyAndReset(); |
| 445 | } |
| 446 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 447 | 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 Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 452 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 453 | sTemplateWifi, 5, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 454 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 455 | assertTimeEquals(expectedCycle, actualCycle); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 456 | } |
| 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 Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 463 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 464 | sTemplateWifi, 20, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 465 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 466 | assertTimeEquals(expectedCycle, actualCycle); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 467 | } |
| 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 Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 474 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 475 | sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 476 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 477 | assertTimeEquals(expectedCycle, actualCycle); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 478 | } |
| 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 Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 483 | final long expectedCycle = parseTime("2007-02-28T23:59:59.000Z"); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 484 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 485 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 486 | sTemplateWifi, 30, TIMEZONE_UTC, 1024L, 1024L, false); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 487 | final long actualCycle = computeLastCycleBoundary(currentTime, policy); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 488 | assertTimeEquals(expectedCycle, actualCycle); |
| 489 | } |
| 490 | |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 491 | 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 Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 530 | public void testNextCycleSane() throws Exception { |
| 531 | final NetworkPolicy policy = new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 532 | sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 533 | 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 Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 547 | sTemplateWifi, 31, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, false); |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 548 | 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 Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | public void testNetworkPolicyAppliedCycleLastMonth() throws Exception { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 561 | NetworkState[] state = null; |
| 562 | NetworkStats stats = null; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 563 | Future<Void> future; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 564 | |
| 565 | final long TIME_FEB_15 = 1171497600000L; |
| 566 | final long TIME_MAR_10 = 1173484800000L; |
| 567 | final int CYCLE_DAY = 15; |
| 568 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 569 | setCurrentTimeMillis(TIME_MAR_10); |
| 570 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 571 | // first, pretend that wifi network comes online. no policy active, |
| 572 | // which means we shouldn't push limit to interface. |
| 573 | state = new NetworkState[] { buildWifi() }; |
| 574 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 575 | expectCurrentTime(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 576 | expectClearNotifications(); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 577 | expectAdvisePersistThreshold(); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 578 | future = expectMeteredIfacesChanged(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 579 | |
| 580 | replay(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 581 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 582 | future.get(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 583 | verifyAndReset(); |
| 584 | |
| 585 | // now change cycle to be on 15th, and test in early march, to verify we |
| 586 | // pick cycle day in previous month. |
| 587 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 588 | expectCurrentTime(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 589 | |
| 590 | // pretend that 512 bytes total have happened |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 591 | stats = new NetworkStats(getElapsedRealtime(), 1) |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 592 | .addIfaceValues(TEST_IFACE, 256L, 2L, 256L, 2L); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 593 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, TIME_MAR_10)) |
| 594 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 595 | expectPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 596 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 597 | // TODO: consider making strongly ordered mock |
| 598 | expectRemoveInterfaceQuota(TEST_IFACE); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 599 | expectSetInterfaceQuota(TEST_IFACE, (2 * MB_IN_BYTES) - 512); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 600 | |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 601 | expectClearNotifications(); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 602 | expectAdvisePersistThreshold(); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 603 | future = expectMeteredIfacesChanged(TEST_IFACE); |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 604 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 605 | replay(); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 606 | setNetworkPolicies(new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 607 | sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 * MB_IN_BYTES, 2 * MB_IN_BYTES, false)); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 608 | future.get(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 609 | verifyAndReset(); |
| 610 | } |
| 611 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 612 | public void testUidRemovedPolicyCleared() throws Exception { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 613 | Future<Void> future; |
| 614 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 615 | // POLICY_REJECT should RULE_REJECT in background |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 616 | expectSetUidNetworkRules(UID_A, true); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 617 | expectSetUidForeground(UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 618 | future = expectRulesChanged(UID_A, RULE_REJECT_METERED); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 619 | replay(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame^] | 620 | mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 621 | future.get(); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 622 | verifyAndReset(); |
| 623 | |
| 624 | // uninstall should clear RULE_REJECT |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 625 | expectSetUidNetworkRules(UID_A, false); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 626 | expectSetUidForeground(UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 627 | future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 628 | replay(); |
| 629 | final Intent intent = new Intent(ACTION_UID_REMOVED); |
| 630 | intent.putExtra(EXTRA_UID, UID_A); |
| 631 | mServiceContext.sendBroadcast(intent); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 632 | future.get(); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 633 | verifyAndReset(); |
| 634 | } |
| 635 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 636 | public void testOverWarningLimitNotification() throws Exception { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 637 | NetworkState[] state = null; |
| 638 | NetworkStats stats = null; |
| 639 | Future<Void> future; |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 640 | Future<String> tagFuture; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 641 | |
| 642 | final long TIME_FEB_15 = 1171497600000L; |
| 643 | final long TIME_MAR_10 = 1173484800000L; |
| 644 | final int CYCLE_DAY = 15; |
| 645 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 646 | setCurrentTimeMillis(TIME_MAR_10); |
| 647 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 648 | // assign wifi policy |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 649 | state = new NetworkState[] {}; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 650 | stats = new NetworkStats(getElapsedRealtime(), 1) |
| 651 | .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 652 | |
| 653 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 654 | expectCurrentTime(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 655 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 656 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 657 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 658 | expectPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 659 | |
| 660 | expectClearNotifications(); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 661 | expectAdvisePersistThreshold(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 662 | future = expectMeteredIfacesChanged(); |
| 663 | |
| 664 | replay(); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 665 | setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 |
| 666 | * MB_IN_BYTES, 2 * MB_IN_BYTES, false)); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 667 | future.get(); |
| 668 | verifyAndReset(); |
| 669 | } |
| 670 | |
| 671 | // bring up wifi network |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 672 | incrementCurrentTime(MINUTE_IN_MILLIS); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 673 | state = new NetworkState[] { buildWifi() }; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 674 | stats = new NetworkStats(getElapsedRealtime(), 1) |
| 675 | .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 676 | |
| 677 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 678 | expectCurrentTime(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 679 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 680 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 681 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 682 | expectPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 683 | |
| 684 | expectRemoveInterfaceQuota(TEST_IFACE); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 685 | expectSetInterfaceQuota(TEST_IFACE, 2 * MB_IN_BYTES); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 686 | |
| 687 | expectClearNotifications(); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 688 | expectAdvisePersistThreshold(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 689 | future = expectMeteredIfacesChanged(TEST_IFACE); |
| 690 | |
| 691 | replay(); |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 692 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION_IMMEDIATE)); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 693 | future.get(); |
| 694 | verifyAndReset(); |
| 695 | } |
| 696 | |
| 697 | // go over warning, which should kick notification |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 698 | incrementCurrentTime(MINUTE_IN_MILLIS); |
| 699 | stats = new NetworkStats(getElapsedRealtime(), 1) |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 700 | .addIfaceValues(TEST_IFACE, 1536 * KB_IN_BYTES, 15L, 0L, 0L); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 701 | |
| 702 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 703 | expectCurrentTime(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 704 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 705 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 706 | expectPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 707 | |
| 708 | expectForceUpdate(); |
| 709 | expectClearNotifications(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 710 | tagFuture = expectEnqueueNotification(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 711 | |
| 712 | replay(); |
| 713 | mNetworkObserver.limitReached(null, TEST_IFACE); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 714 | assertNotificationType(TYPE_WARNING, tagFuture.get()); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 715 | verifyAndReset(); |
| 716 | } |
| 717 | |
| 718 | // go over limit, which should kick notification and dialog |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 719 | incrementCurrentTime(MINUTE_IN_MILLIS); |
| 720 | stats = new NetworkStats(getElapsedRealtime(), 1) |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 721 | .addIfaceValues(TEST_IFACE, 5 * MB_IN_BYTES, 512L, 0L, 0L); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 722 | |
| 723 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 724 | expectCurrentTime(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 725 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 726 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 727 | expectPolicyDataEnable(TYPE_WIFI, false); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 728 | |
| 729 | expectForceUpdate(); |
| 730 | expectClearNotifications(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 731 | tagFuture = expectEnqueueNotification(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 732 | |
| 733 | replay(); |
| 734 | mNetworkObserver.limitReached(null, TEST_IFACE); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 735 | assertNotificationType(TYPE_LIMIT, tagFuture.get()); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 736 | verifyAndReset(); |
| 737 | } |
| 738 | |
| 739 | // now snooze policy, which should remove quota |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 740 | incrementCurrentTime(MINUTE_IN_MILLIS); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 741 | |
| 742 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 743 | expectCurrentTime(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 744 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 745 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 746 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 747 | expectPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 748 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 749 | // snoozed interface still has high quota so background data is |
| 750 | // still restricted. |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 751 | expectRemoveInterfaceQuota(TEST_IFACE); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 752 | expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 753 | expectAdvisePersistThreshold(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 754 | expectMeteredIfacesChanged(TEST_IFACE); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 755 | |
Jeff Sharkey | c506ff6 | 2011-11-17 11:59:29 -0800 | [diff] [blame] | 756 | future = expectClearNotifications(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 757 | tagFuture = expectEnqueueNotification(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 758 | |
| 759 | replay(); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 760 | mService.snoozeLimit(sTemplateWifi); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 761 | assertNotificationType(TYPE_LIMIT_SNOOZED, tagFuture.get()); |
Jeff Sharkey | c506ff6 | 2011-11-17 11:59:29 -0800 | [diff] [blame] | 762 | future.get(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 763 | verifyAndReset(); |
| 764 | } |
| 765 | } |
| 766 | |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 767 | public void testMeteredNetworkWithoutLimit() throws Exception { |
| 768 | NetworkState[] state = null; |
| 769 | NetworkStats stats = null; |
| 770 | Future<Void> future; |
| 771 | Future<String> tagFuture; |
| 772 | |
| 773 | final long TIME_FEB_15 = 1171497600000L; |
| 774 | final long TIME_MAR_10 = 1173484800000L; |
| 775 | final int CYCLE_DAY = 15; |
| 776 | |
| 777 | setCurrentTimeMillis(TIME_MAR_10); |
| 778 | |
| 779 | // bring up wifi network with metered policy |
| 780 | state = new NetworkState[] { buildWifi() }; |
| 781 | stats = new NetworkStats(getElapsedRealtime(), 1) |
| 782 | .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L); |
| 783 | |
| 784 | { |
| 785 | expectCurrentTime(); |
| 786 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 787 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 788 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 789 | expectPolicyDataEnable(TYPE_WIFI, true); |
| 790 | |
| 791 | expectRemoveInterfaceQuota(TEST_IFACE); |
| 792 | expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE); |
| 793 | |
| 794 | expectClearNotifications(); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 795 | expectAdvisePersistThreshold(); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 796 | future = expectMeteredIfacesChanged(TEST_IFACE); |
| 797 | |
| 798 | replay(); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 799 | setNetworkPolicies(new NetworkPolicy( |
| 800 | sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, |
| 801 | true)); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 802 | future.get(); |
| 803 | verifyAndReset(); |
| 804 | } |
| 805 | } |
| 806 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 807 | private static long parseTime(String time) { |
| 808 | final Time result = new Time(); |
| 809 | result.parse3339(time); |
| 810 | return result.toMillis(true); |
| 811 | } |
| 812 | |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 813 | private void setNetworkPolicies(NetworkPolicy... policies) { |
| 814 | mService.setNetworkPolicies(policies); |
| 815 | } |
| 816 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 817 | private static NetworkState buildWifi() { |
| 818 | final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null); |
| 819 | info.setDetailedState(DetailedState.CONNECTED, null, null); |
| 820 | final LinkProperties prop = new LinkProperties(); |
| 821 | prop.setInterfaceName(TEST_IFACE); |
Jeff Sharkey | e8914c3 | 2012-05-01 16:26:09 -0700 | [diff] [blame] | 822 | return new NetworkState(info, prop, null, null, TEST_SSID); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 823 | } |
| 824 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 825 | private void expectCurrentTime() throws Exception { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 826 | expect(mTime.forceRefresh()).andReturn(false).anyTimes(); |
| 827 | expect(mTime.hasCache()).andReturn(true).anyTimes(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 828 | expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 829 | expect(mTime.getCacheAge()).andReturn(0L).anyTimes(); |
| 830 | expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes(); |
| 831 | } |
| 832 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 833 | private void expectForceUpdate() throws Exception { |
| 834 | mStatsService.forceUpdate(); |
| 835 | expectLastCall().atLeastOnce(); |
| 836 | } |
| 837 | |
Jeff Sharkey | c506ff6 | 2011-11-17 11:59:29 -0800 | [diff] [blame] | 838 | private Future<Void> expectClearNotifications() throws Exception { |
| 839 | final FutureAnswer future = new FutureAnswer(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame^] | 840 | mNotifManager.cancelNotificationWithTag( |
| 841 | isA(String.class), isA(String.class), anyInt(), anyInt()); |
Jeff Sharkey | c506ff6 | 2011-11-17 11:59:29 -0800 | [diff] [blame] | 842 | expectLastCall().andAnswer(future).anyTimes(); |
| 843 | return future; |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 844 | } |
| 845 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 846 | private Future<String> expectEnqueueNotification() throws Exception { |
| 847 | final FutureCapture<String> tag = new FutureCapture<String>(); |
| 848 | mNotifManager.enqueueNotificationWithTag(isA(String.class), capture(tag.capture), anyInt(), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 849 | isA(Notification.class), isA(int[].class), UserHandle.myUserId()); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 850 | return tag; |
| 851 | } |
| 852 | |
| 853 | private void expectSetInterfaceQuota(String iface, long quotaBytes) throws Exception { |
| 854 | mNetworkManager.setInterfaceQuota(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 855 | expectLastCall().atLeastOnce(); |
| 856 | } |
| 857 | |
| 858 | private void expectRemoveInterfaceQuota(String iface) throws Exception { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 859 | mNetworkManager.removeInterfaceQuota(iface); |
| 860 | expectLastCall().atLeastOnce(); |
| 861 | } |
| 862 | |
| 863 | private void expectSetInterfaceAlert(String iface, long alertBytes) throws Exception { |
| 864 | mNetworkManager.setInterfaceAlert(iface, alertBytes); |
| 865 | expectLastCall().atLeastOnce(); |
| 866 | } |
| 867 | |
| 868 | private void expectRemoveInterfaceAlert(String iface) throws Exception { |
| 869 | mNetworkManager.removeInterfaceAlert(iface); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 870 | expectLastCall().atLeastOnce(); |
| 871 | } |
| 872 | |
| 873 | private void expectSetUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) |
| 874 | throws Exception { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 875 | mNetworkManager.setUidNetworkRules(uid, rejectOnQuotaInterfaces); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 876 | expectLastCall().atLeastOnce(); |
| 877 | } |
| 878 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 879 | private void expectSetUidForeground(int uid, boolean uidForeground) throws Exception { |
| 880 | mStatsService.setUidForeground(uid, uidForeground); |
| 881 | expectLastCall().atLeastOnce(); |
| 882 | } |
| 883 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 884 | private Future<Void> expectRulesChanged(int uid, int policy) throws Exception { |
| 885 | final FutureAnswer future = new FutureAnswer(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 886 | mPolicyListener.onUidRulesChanged(eq(uid), eq(policy)); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 887 | expectLastCall().andAnswer(future); |
| 888 | return future; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 891 | private Future<Void> expectMeteredIfacesChanged(String... ifaces) throws Exception { |
| 892 | final FutureAnswer future = new FutureAnswer(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 893 | mPolicyListener.onMeteredIfacesChanged(aryEq(ifaces)); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 894 | expectLastCall().andAnswer(future); |
| 895 | return future; |
| 896 | } |
| 897 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 898 | private Future<Void> expectPolicyDataEnable(int type, boolean enabled) throws Exception { |
| 899 | final FutureAnswer future = new FutureAnswer(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 900 | mConnManager.setPolicyDataEnable(type, enabled); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 901 | expectLastCall().andAnswer(future); |
| 902 | return future; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 903 | } |
| 904 | |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 905 | private void expectAdvisePersistThreshold() throws Exception { |
| 906 | mStatsService.advisePersistThreshold(anyLong()); |
| 907 | expectLastCall().anyTimes(); |
| 908 | } |
| 909 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 910 | private static class TestAbstractFuture<T> extends AbstractFuture<T> { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 911 | @Override |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 912 | public T get() throws InterruptedException, ExecutionException { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 913 | try { |
| 914 | return get(5, TimeUnit.SECONDS); |
| 915 | } catch (TimeoutException e) { |
| 916 | throw new RuntimeException(e); |
| 917 | } |
| 918 | } |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 919 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 920 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 921 | private static class FutureAnswer extends TestAbstractFuture<Void> implements IAnswer<Void> { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 922 | @Override |
| 923 | public Void answer() { |
| 924 | set(null); |
| 925 | return null; |
| 926 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 929 | private static class FutureCapture<T> extends TestAbstractFuture<T> { |
| 930 | public Capture<T> capture = new Capture<T>() { |
| 931 | @Override |
| 932 | public void setValue(T value) { |
| 933 | super.setValue(value); |
| 934 | set(value); |
| 935 | } |
| 936 | }; |
| 937 | } |
| 938 | |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 939 | private static class IdleFuture extends AbstractFuture<Void> implements IdleHandler { |
| 940 | @Override |
| 941 | public Void get() throws InterruptedException, ExecutionException { |
| 942 | try { |
| 943 | return get(5, TimeUnit.SECONDS); |
| 944 | } catch (TimeoutException e) { |
| 945 | throw new RuntimeException(e); |
| 946 | } |
| 947 | } |
| 948 | |
Jeff Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 949 | @Override |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 950 | public boolean queueIdle() { |
| 951 | set(null); |
| 952 | return false; |
| 953 | } |
| 954 | } |
| 955 | |
| 956 | /** |
| 957 | * Wait until {@link #mService} internal {@link Handler} is idle. |
| 958 | */ |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 959 | private IdleFuture expectIdle() { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 960 | final IdleFuture future = new IdleFuture(); |
| 961 | mService.addIdleHandler(future); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 962 | return future; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 963 | } |
| 964 | |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 965 | private static void assertTimeEquals(long expected, long actual) { |
| 966 | if (expected != actual) { |
| 967 | fail("expected " + formatTime(expected) + " but was actually " + formatTime(actual)); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | private static String formatTime(long millis) { |
| 972 | final Time time = new Time(Time.TIMEZONE_UTC); |
| 973 | time.set(millis); |
| 974 | return time.format3339(false); |
| 975 | } |
| 976 | |
| 977 | private static void assertEqualsFuzzy(long expected, long actual, long fuzzy) { |
| 978 | final long low = expected - fuzzy; |
| 979 | final long high = expected + fuzzy; |
| 980 | if (actual < low || actual > high) { |
| 981 | fail("value " + actual + " is outside [" + low + "," + high + "]"); |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | private static void assertUnique(LinkedHashSet<Long> seen, Long value) { |
| 986 | if (!seen.add(value)) { |
| 987 | fail("found duplicate time " + value + " in series " + seen.toString()); |
| 988 | } |
| 989 | } |
| 990 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 991 | private static void assertNotificationType(int expected, String actualTag) { |
| 992 | assertEquals( |
| 993 | Integer.toString(expected), actualTag.substring(actualTag.lastIndexOf(':') + 1)); |
| 994 | } |
| 995 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 996 | private long getElapsedRealtime() { |
| 997 | return mElapsedRealtime; |
| 998 | } |
| 999 | |
| 1000 | private void setCurrentTimeMillis(long currentTimeMillis) { |
| 1001 | mStartTime = currentTimeMillis; |
| 1002 | mElapsedRealtime = 0L; |
| 1003 | } |
| 1004 | |
| 1005 | private long currentTimeMillis() { |
| 1006 | return mStartTime + mElapsedRealtime; |
| 1007 | } |
| 1008 | |
| 1009 | private void incrementCurrentTime(long duration) { |
| 1010 | mElapsedRealtime += duration; |
| 1011 | } |
| 1012 | |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1013 | private void replay() { |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1014 | EasyMock.replay(mActivityManager, mPowerManager, mStatsService, mPolicyListener, |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1015 | mNetworkManager, mTime, mConnManager, mNotifManager); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | private void verifyAndReset() { |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1019 | EasyMock.verify(mActivityManager, mPowerManager, mStatsService, mPolicyListener, |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1020 | mNetworkManager, mTime, mConnManager, mNotifManager); |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1021 | EasyMock.reset(mActivityManager, mPowerManager, mStatsService, mPolicyListener, |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1022 | mNetworkManager, mTime, mConnManager, mNotifManager); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1023 | } |
| 1024 | } |