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; |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 21 | import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; |
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() |
Narayan Kamath | 24c089a | 2014-04-11 11:42:33 +0100 | [diff] [blame] | 219 | expect(mPowerManager.isInteractive()).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 |
Narayan Kamath | 24c089a | 2014-04-11 11:42:33 +0100 | [diff] [blame] | 334 | expect(mPowerManager.isInteractive()).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 |
Narayan Kamath | 24c089a | 2014-04-11 11:42:33 +0100 | [diff] [blame] | 344 | expect(mPowerManager.isInteractive()).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 | |
Jeff Sharkey | 1539905 | 2013-01-15 14:15:53 -0800 | [diff] [blame] | 560 | 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 Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 579 | public void testNetworkPolicyAppliedCycleLastMonth() throws Exception { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 580 | NetworkState[] state = null; |
| 581 | NetworkStats stats = null; |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 582 | Future<Void> future; |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 583 | |
| 584 | final long TIME_FEB_15 = 1171497600000L; |
| 585 | final long TIME_MAR_10 = 1173484800000L; |
| 586 | final int CYCLE_DAY = 15; |
| 587 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 588 | setCurrentTimeMillis(TIME_MAR_10); |
| 589 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 590 | // 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 Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 594 | expectCurrentTime(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 595 | expectClearNotifications(); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 596 | expectAdvisePersistThreshold(); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 597 | future = expectMeteredIfacesChanged(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 598 | |
| 599 | replay(); |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 600 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 601 | future.get(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 602 | 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 Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 607 | expectCurrentTime(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 608 | |
| 609 | // pretend that 512 bytes total have happened |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 610 | stats = new NetworkStats(getElapsedRealtime(), 1) |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 611 | .addIfaceValues(TEST_IFACE, 256L, 2L, 256L, 2L); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 612 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, TIME_MAR_10)) |
| 613 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 614 | expectPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 615 | |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 616 | // TODO: consider making strongly ordered mock |
| 617 | expectRemoveInterfaceQuota(TEST_IFACE); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 618 | expectSetInterfaceQuota(TEST_IFACE, (2 * MB_IN_BYTES) - 512); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 619 | |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 620 | expectClearNotifications(); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 621 | expectAdvisePersistThreshold(); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 622 | future = expectMeteredIfacesChanged(TEST_IFACE); |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 623 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 624 | replay(); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 625 | setNetworkPolicies(new NetworkPolicy( |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 626 | 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] | 627 | future.get(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 628 | verifyAndReset(); |
| 629 | } |
| 630 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 631 | public void testUidRemovedPolicyCleared() throws Exception { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 632 | Future<Void> future; |
| 633 | |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 634 | // POLICY_REJECT should RULE_REJECT in background |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 635 | expectSetUidNetworkRules(UID_A, true); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 636 | expectSetUidForeground(UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 637 | future = expectRulesChanged(UID_A, RULE_REJECT_METERED); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 638 | replay(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 639 | mService.setUidPolicy(APP_ID_A, POLICY_REJECT_METERED_BACKGROUND); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 640 | future.get(); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 641 | verifyAndReset(); |
| 642 | |
| 643 | // uninstall should clear RULE_REJECT |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 644 | expectSetUidNetworkRules(UID_A, false); |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 645 | expectSetUidForeground(UID_A, false); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 646 | future = expectRulesChanged(UID_A, RULE_ALLOW_ALL); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 647 | replay(); |
| 648 | final Intent intent = new Intent(ACTION_UID_REMOVED); |
| 649 | intent.putExtra(EXTRA_UID, UID_A); |
| 650 | mServiceContext.sendBroadcast(intent); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 651 | future.get(); |
Jeff Sharkey | b09540f | 2011-06-19 01:08:12 -0700 | [diff] [blame] | 652 | verifyAndReset(); |
| 653 | } |
| 654 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 655 | public void testOverWarningLimitNotification() throws Exception { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 656 | NetworkState[] state = null; |
| 657 | NetworkStats stats = null; |
| 658 | Future<Void> future; |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 659 | Future<String> tagFuture; |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 660 | |
| 661 | final long TIME_FEB_15 = 1171497600000L; |
| 662 | final long TIME_MAR_10 = 1173484800000L; |
| 663 | final int CYCLE_DAY = 15; |
| 664 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 665 | setCurrentTimeMillis(TIME_MAR_10); |
| 666 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 667 | // assign wifi policy |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 668 | state = new NetworkState[] {}; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 669 | stats = new NetworkStats(getElapsedRealtime(), 1) |
| 670 | .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 671 | |
| 672 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 673 | expectCurrentTime(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 674 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 675 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 676 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 677 | expectPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 678 | |
| 679 | expectClearNotifications(); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 680 | expectAdvisePersistThreshold(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 681 | future = expectMeteredIfacesChanged(); |
| 682 | |
| 683 | replay(); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 684 | setNetworkPolicies(new NetworkPolicy(sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, 1 |
| 685 | * MB_IN_BYTES, 2 * MB_IN_BYTES, false)); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 686 | future.get(); |
| 687 | verifyAndReset(); |
| 688 | } |
| 689 | |
| 690 | // bring up wifi network |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 691 | incrementCurrentTime(MINUTE_IN_MILLIS); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 692 | state = new NetworkState[] { buildWifi() }; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 693 | stats = new NetworkStats(getElapsedRealtime(), 1) |
| 694 | .addIfaceValues(TEST_IFACE, 0L, 0L, 0L, 0L); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 695 | |
| 696 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 697 | expectCurrentTime(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 698 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 699 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 700 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 701 | expectPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 702 | |
| 703 | expectRemoveInterfaceQuota(TEST_IFACE); |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 704 | expectSetInterfaceQuota(TEST_IFACE, 2 * MB_IN_BYTES); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 705 | |
| 706 | expectClearNotifications(); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 707 | expectAdvisePersistThreshold(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 708 | future = expectMeteredIfacesChanged(TEST_IFACE); |
| 709 | |
| 710 | replay(); |
Erik Kline | f851d6d | 2015-04-20 16:03:48 +0900 | [diff] [blame] | 711 | mServiceContext.sendBroadcast(new Intent(CONNECTIVITY_ACTION)); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 712 | future.get(); |
| 713 | verifyAndReset(); |
| 714 | } |
| 715 | |
| 716 | // go over warning, which should kick notification |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 717 | incrementCurrentTime(MINUTE_IN_MILLIS); |
| 718 | stats = new NetworkStats(getElapsedRealtime(), 1) |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 719 | .addIfaceValues(TEST_IFACE, 1536 * KB_IN_BYTES, 15L, 0L, 0L); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 720 | |
| 721 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 722 | expectCurrentTime(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 723 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 724 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 725 | expectPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 726 | |
| 727 | expectForceUpdate(); |
| 728 | expectClearNotifications(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 729 | tagFuture = expectEnqueueNotification(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 730 | |
| 731 | replay(); |
| 732 | mNetworkObserver.limitReached(null, TEST_IFACE); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 733 | assertNotificationType(TYPE_WARNING, tagFuture.get()); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 734 | verifyAndReset(); |
| 735 | } |
| 736 | |
| 737 | // go over limit, which should kick notification and dialog |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 738 | incrementCurrentTime(MINUTE_IN_MILLIS); |
| 739 | stats = new NetworkStats(getElapsedRealtime(), 1) |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 740 | .addIfaceValues(TEST_IFACE, 5 * MB_IN_BYTES, 512L, 0L, 0L); |
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 | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 744 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 745 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 746 | expectPolicyDataEnable(TYPE_WIFI, false); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 747 | |
| 748 | expectForceUpdate(); |
| 749 | expectClearNotifications(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 750 | tagFuture = expectEnqueueNotification(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 751 | |
| 752 | replay(); |
| 753 | mNetworkObserver.limitReached(null, TEST_IFACE); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 754 | assertNotificationType(TYPE_LIMIT, tagFuture.get()); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 755 | verifyAndReset(); |
| 756 | } |
| 757 | |
| 758 | // now snooze policy, which should remove quota |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 759 | incrementCurrentTime(MINUTE_IN_MILLIS); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 760 | |
| 761 | { |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 762 | expectCurrentTime(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 763 | expect(mConnManager.getAllNetworkState()).andReturn(state).atLeastOnce(); |
Jeff Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 764 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 765 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 766 | expectPolicyDataEnable(TYPE_WIFI, true); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 767 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 768 | // snoozed interface still has high quota so background data is |
| 769 | // still restricted. |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 770 | expectRemoveInterfaceQuota(TEST_IFACE); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 771 | expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 772 | expectAdvisePersistThreshold(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 773 | expectMeteredIfacesChanged(TEST_IFACE); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 774 | |
Jeff Sharkey | c506ff6 | 2011-11-17 11:59:29 -0800 | [diff] [blame] | 775 | future = expectClearNotifications(); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 776 | tagFuture = expectEnqueueNotification(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 777 | |
| 778 | replay(); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 779 | mService.snoozeLimit(sTemplateWifi); |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 780 | assertNotificationType(TYPE_LIMIT_SNOOZED, tagFuture.get()); |
Jeff Sharkey | c506ff6 | 2011-11-17 11:59:29 -0800 | [diff] [blame] | 781 | future.get(); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 782 | verifyAndReset(); |
| 783 | } |
| 784 | } |
| 785 | |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 786 | 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 Sharkey | b52e3e5 | 2012-04-06 11:12:08 -0700 | [diff] [blame] | 806 | expect(mStatsService.getNetworkTotalBytes(sTemplateWifi, TIME_FEB_15, currentTimeMillis())) |
| 807 | .andReturn(stats.getTotalBytes()).atLeastOnce(); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 808 | expectPolicyDataEnable(TYPE_WIFI, true); |
| 809 | |
| 810 | expectRemoveInterfaceQuota(TEST_IFACE); |
| 811 | expectSetInterfaceQuota(TEST_IFACE, Long.MAX_VALUE); |
| 812 | |
| 813 | expectClearNotifications(); |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 814 | expectAdvisePersistThreshold(); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 815 | future = expectMeteredIfacesChanged(TEST_IFACE); |
| 816 | |
| 817 | replay(); |
Jeff Sharkey | 9bf3150 | 2012-03-09 17:07:21 -0800 | [diff] [blame] | 818 | setNetworkPolicies(new NetworkPolicy( |
| 819 | sTemplateWifi, CYCLE_DAY, TIMEZONE_UTC, WARNING_DISABLED, LIMIT_DISABLED, |
| 820 | true)); |
Jeff Sharkey | f60d0af | 2011-11-30 15:28:02 -0800 | [diff] [blame] | 821 | future.get(); |
| 822 | verifyAndReset(); |
| 823 | } |
| 824 | } |
| 825 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 826 | private static long parseTime(String time) { |
| 827 | final Time result = new Time(); |
| 828 | result.parse3339(time); |
| 829 | return result.toMillis(true); |
| 830 | } |
| 831 | |
Jeff Sharkey | af11d48 | 2011-06-13 00:14:31 -0700 | [diff] [blame] | 832 | private void setNetworkPolicies(NetworkPolicy... policies) { |
| 833 | mService.setNetworkPolicies(policies); |
| 834 | } |
| 835 | |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 836 | 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 Ramachandran | 21b5ee3 | 2014-11-12 22:31:52 -0800 | [diff] [blame] | 841 | return new NetworkState(info, prop, null, null, null, TEST_SSID); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 842 | } |
| 843 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 844 | private void expectCurrentTime() throws Exception { |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 845 | expect(mTime.forceRefresh()).andReturn(false).anyTimes(); |
| 846 | expect(mTime.hasCache()).andReturn(true).anyTimes(); |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 847 | expect(mTime.currentTimeMillis()).andReturn(currentTimeMillis()).anyTimes(); |
Jeff Sharkey | 21c9c45 | 2011-06-07 12:26:43 -0700 | [diff] [blame] | 848 | expect(mTime.getCacheAge()).andReturn(0L).anyTimes(); |
| 849 | expect(mTime.getCacheCertainty()).andReturn(0L).anyTimes(); |
| 850 | } |
| 851 | |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 852 | private void expectForceUpdate() throws Exception { |
| 853 | mStatsService.forceUpdate(); |
| 854 | expectLastCall().atLeastOnce(); |
| 855 | } |
| 856 | |
Jeff Sharkey | c506ff6 | 2011-11-17 11:59:29 -0800 | [diff] [blame] | 857 | private Future<Void> expectClearNotifications() throws Exception { |
| 858 | final FutureAnswer future = new FutureAnswer(); |
Jeff Sharkey | d0c6ccb | 2012-09-14 16:26:37 -0700 | [diff] [blame] | 859 | mNotifManager.cancelNotificationWithTag( |
| 860 | isA(String.class), isA(String.class), anyInt(), anyInt()); |
Jeff Sharkey | c506ff6 | 2011-11-17 11:59:29 -0800 | [diff] [blame] | 861 | expectLastCall().andAnswer(future).anyTimes(); |
| 862 | return future; |
Jeff Sharkey | 2ef2aeb | 2011-06-15 11:01:31 -0700 | [diff] [blame] | 863 | } |
| 864 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 865 | private Future<String> expectEnqueueNotification() throws Exception { |
| 866 | final FutureCapture<String> tag = new FutureCapture<String>(); |
Dianne Hackborn | f265ea9 | 2013-01-31 15:00:51 -0800 | [diff] [blame] | 867 | mNotifManager.enqueueNotificationWithTag(isA(String.class), isA(String.class), |
| 868 | capture(tag.capture), anyInt(), |
Dianne Hackborn | 4120375 | 2012-08-31 14:05:51 -0700 | [diff] [blame] | 869 | isA(Notification.class), isA(int[].class), UserHandle.myUserId()); |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 870 | return tag; |
| 871 | } |
| 872 | |
| 873 | private void expectSetInterfaceQuota(String iface, long quotaBytes) throws Exception { |
| 874 | mNetworkManager.setInterfaceQuota(iface, quotaBytes); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 875 | expectLastCall().atLeastOnce(); |
| 876 | } |
| 877 | |
| 878 | private void expectRemoveInterfaceQuota(String iface) throws Exception { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 879 | 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 Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 890 | expectLastCall().atLeastOnce(); |
| 891 | } |
| 892 | |
| 893 | private void expectSetUidNetworkRules(int uid, boolean rejectOnQuotaInterfaces) |
| 894 | throws Exception { |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 895 | mNetworkManager.setUidNetworkRules(uid, rejectOnQuotaInterfaces); |
Jeff Sharkey | b3f19ca | 2011-06-29 23:54:13 -0700 | [diff] [blame] | 896 | expectLastCall().atLeastOnce(); |
| 897 | } |
| 898 | |
Jeff Sharkey | b5d55e3 | 2011-08-10 17:53:27 -0700 | [diff] [blame] | 899 | private void expectSetUidForeground(int uid, boolean uidForeground) throws Exception { |
| 900 | mStatsService.setUidForeground(uid, uidForeground); |
| 901 | expectLastCall().atLeastOnce(); |
| 902 | } |
| 903 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 904 | private Future<Void> expectRulesChanged(int uid, int policy) throws Exception { |
| 905 | final FutureAnswer future = new FutureAnswer(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 906 | mPolicyListener.onUidRulesChanged(eq(uid), eq(policy)); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 907 | expectLastCall().andAnswer(future); |
| 908 | return future; |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 909 | } |
| 910 | |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 911 | private Future<Void> expectMeteredIfacesChanged(String... ifaces) throws Exception { |
| 912 | final FutureAnswer future = new FutureAnswer(); |
Jeff Sharkey | fdfef57 | 2011-06-16 15:07:48 -0700 | [diff] [blame] | 913 | mPolicyListener.onMeteredIfacesChanged(aryEq(ifaces)); |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 914 | expectLastCall().andAnswer(future); |
| 915 | return future; |
| 916 | } |
| 917 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 918 | private Future<Void> expectPolicyDataEnable(int type, boolean enabled) throws Exception { |
Jeff Sharkey | 3256601 | 2014-12-02 18:30:14 -0800 | [diff] [blame] | 919 | // TODO: bring back this test |
| 920 | return null; |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Jeff Sharkey | 0cf6de0 | 2012-05-04 15:03:30 -0700 | [diff] [blame] | 923 | private void expectAdvisePersistThreshold() throws Exception { |
| 924 | mStatsService.advisePersistThreshold(anyLong()); |
| 925 | expectLastCall().anyTimes(); |
| 926 | } |
| 927 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 928 | private static class TestAbstractFuture<T> extends AbstractFuture<T> { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 929 | @Override |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 930 | public T get() throws InterruptedException, ExecutionException { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 931 | try { |
| 932 | return get(5, TimeUnit.SECONDS); |
| 933 | } catch (TimeoutException e) { |
| 934 | throw new RuntimeException(e); |
| 935 | } |
| 936 | } |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 937 | } |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 938 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 939 | private static class FutureAnswer extends TestAbstractFuture<Void> implements IAnswer<Void> { |
Jeff Sharkey | 4414cea | 2011-06-24 17:05:24 -0700 | [diff] [blame] | 940 | @Override |
| 941 | public Void answer() { |
| 942 | set(null); |
| 943 | return null; |
| 944 | } |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 945 | } |
| 946 | |
Jeff Sharkey | 7ee8658 | 2011-11-14 18:02:21 -0800 | [diff] [blame] | 947 | 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 Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 957 | 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 Sharkey | bfdd680 | 2012-04-09 10:49:19 -0700 | [diff] [blame] | 967 | @Override |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 968 | 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 Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 977 | private IdleFuture expectIdle() { |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 978 | final IdleFuture future = new IdleFuture(); |
| 979 | mService.addIdleHandler(future); |
Jeff Sharkey | 0e2e5f8 | 2012-02-02 16:02:51 -0800 | [diff] [blame] | 980 | return future; |
Jeff Sharkey | 163e644 | 2011-10-31 16:37:52 -0700 | [diff] [blame] | 981 | } |
| 982 | |
Jeff Sharkey | af82ea2 | 2011-08-04 15:38:48 -0700 | [diff] [blame] | 983 | 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 Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1009 | private static void assertNotificationType(int expected, String actualTag) { |
| 1010 | assertEquals( |
| 1011 | Integer.toString(expected), actualTag.substring(actualTag.lastIndexOf(':') + 1)); |
| 1012 | } |
| 1013 | |
Jeff Sharkey | 8e28b7d | 2011-08-19 02:24:24 -0700 | [diff] [blame] | 1014 | 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 Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1031 | private void replay() { |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1032 | EasyMock.replay(mActivityManager, mPowerManager, mStatsService, mPolicyListener, |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1033 | mNetworkManager, mTime, mConnManager, mNotifManager); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | private void verifyAndReset() { |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1037 | EasyMock.verify(mActivityManager, mPowerManager, mStatsService, mPolicyListener, |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1038 | mNetworkManager, mTime, mConnManager, mNotifManager); |
Ashish Sharma | 50fd36d | 2011-06-15 19:34:53 -0700 | [diff] [blame] | 1039 | EasyMock.reset(mActivityManager, mPowerManager, mStatsService, mPolicyListener, |
Jeff Sharkey | 41ff7ec | 2011-07-25 15:21:22 -0700 | [diff] [blame] | 1040 | mNetworkManager, mTime, mConnManager, mNotifManager); |
Jeff Sharkey | 9599cc5 | 2011-05-22 14:59:31 -0700 | [diff] [blame] | 1041 | } |
| 1042 | } |