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