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