Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.job; |
| 18 | |
| 19 | import static com.android.servicestests.apps.jobtestapp.TestJobService.ACTION_JOB_STARTED; |
| 20 | import static com.android.servicestests.apps.jobtestapp.TestJobService.ACTION_JOB_STOPPED; |
| 21 | import static com.android.servicestests.apps.jobtestapp.TestJobService.JOB_PARAMS_EXTRA_KEY; |
| 22 | |
| 23 | import static org.junit.Assert.assertFalse; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | |
| 26 | import android.app.ActivityManager; |
| 27 | import android.app.AppOpsManager; |
| 28 | import android.app.IActivityManager; |
| 29 | import android.app.job.JobParameters; |
| 30 | import android.content.BroadcastReceiver; |
| 31 | import android.content.ComponentName; |
| 32 | import android.content.Context; |
| 33 | import android.content.Intent; |
| 34 | import android.content.IntentFilter; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 35 | import android.os.IDeviceIdleController; |
| 36 | import android.os.RemoteException; |
| 37 | import android.os.ServiceManager; |
| 38 | import android.os.SystemClock; |
| 39 | import android.os.UserHandle; |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 40 | import android.support.test.InstrumentationRegistry; |
| 41 | import android.support.test.filters.LargeTest; |
| 42 | import android.support.test.runner.AndroidJUnit4; |
| 43 | import android.util.Log; |
| 44 | |
| 45 | import com.android.servicestests.apps.jobtestapp.TestJobActivity; |
| 46 | |
| 47 | import org.junit.After; |
| 48 | import org.junit.Before; |
| 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
| 51 | |
| 52 | /** |
| 53 | * TODO: Also add a test for temp power whitelist |
| 54 | * Tests that background restrictions on jobs work as expected. |
| 55 | * This test requires test-apps/JobTestApp to be installed on the device. |
| 56 | * To run this test from root of checkout: |
| 57 | * <pre> |
| 58 | * mmm -j32 frameworks/base/services/tests/servicestests/ |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 59 | * adb install -r $OUT/data/app/JobTestApp/JobTestApp.apk |
| 60 | * adb install -r $OUT/data/app/FrameworksServicesTests/FrameworksServicesTests.apk |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 61 | * adb shell am instrument -e class 'com.android.server.job.BackgroundRestrictionsTest' -w \ |
Makoto Onuki | 9be0140 | 2017-11-10 13:22:26 -0800 | [diff] [blame] | 62 | com.android.frameworks.servicestests |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 63 | * </pre> |
| 64 | */ |
| 65 | @RunWith(AndroidJUnit4.class) |
| 66 | @LargeTest |
| 67 | public class BackgroundRestrictionsTest { |
| 68 | private static final String TAG = BackgroundRestrictionsTest.class.getSimpleName(); |
| 69 | private static final String TEST_APP_PACKAGE = "com.android.servicestests.apps.jobtestapp"; |
| 70 | private static final String TEST_APP_ACTIVITY = TEST_APP_PACKAGE + ".TestJobActivity"; |
| 71 | private static final long POLL_INTERVAL = 2000; |
| 72 | private static final long DEFAULT_WAIT_TIMEOUT = 5000; |
| 73 | |
| 74 | private Context mContext; |
| 75 | private AppOpsManager mAppOpsManager; |
| 76 | private IDeviceIdleController mDeviceIdleController; |
| 77 | private IActivityManager mIActivityManager; |
| 78 | private int mTestJobId; |
| 79 | private int mTestPackageUid; |
| 80 | /* accesses must be synchronized on itself */ |
| 81 | private final TestJobStatus mTestJobStatus = new TestJobStatus(); |
| 82 | private final BroadcastReceiver mJobStateChangeReceiver = new BroadcastReceiver() { |
| 83 | @Override |
| 84 | public void onReceive(Context context, Intent intent) { |
| 85 | final JobParameters params = intent.getParcelableExtra(JOB_PARAMS_EXTRA_KEY); |
| 86 | Log.d(TAG, "Received action " + intent.getAction()); |
| 87 | synchronized (mTestJobStatus) { |
| 88 | switch (intent.getAction()) { |
| 89 | case ACTION_JOB_STARTED: |
| 90 | mTestJobStatus.running = true; |
| 91 | mTestJobStatus.jobId = params.getJobId(); |
| 92 | mTestJobStatus.stopReason = JobParameters.REASON_CANCELED; |
| 93 | break; |
| 94 | case ACTION_JOB_STOPPED: |
| 95 | mTestJobStatus.running = false; |
| 96 | mTestJobStatus.jobId = params.getJobId(); |
| 97 | mTestJobStatus.stopReason = params.getStopReason(); |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | }; |
| 103 | |
| 104 | @Before |
| 105 | public void setUp() throws Exception { |
| 106 | mContext = InstrumentationRegistry.getTargetContext(); |
| 107 | mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); |
| 108 | mDeviceIdleController = IDeviceIdleController.Stub.asInterface( |
| 109 | ServiceManager.getService(Context.DEVICE_IDLE_CONTROLLER)); |
| 110 | mIActivityManager = ActivityManager.getService(); |
| 111 | mTestPackageUid = mContext.getPackageManager().getPackageUid(TEST_APP_PACKAGE, 0); |
| 112 | mTestJobId = (int) (SystemClock.uptimeMillis() / 1000); |
| 113 | mTestJobStatus.reset(); |
| 114 | final IntentFilter intentFilter = new IntentFilter(); |
| 115 | intentFilter.addAction(ACTION_JOB_STARTED); |
| 116 | intentFilter.addAction(ACTION_JOB_STOPPED); |
| 117 | mContext.registerReceiver(mJobStateChangeReceiver, intentFilter); |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 118 | setAppOpsModeAllowed(true); |
| 119 | setPowerWhiteListed(false); |
| 120 | } |
| 121 | |
| 122 | private void scheduleAndAssertJobStarted() throws Exception { |
| 123 | final Intent scheduleJobIntent = new Intent(TestJobActivity.ACTION_START_JOB); |
| 124 | scheduleJobIntent.putExtra(TestJobActivity.EXTRA_JOB_ID_KEY, mTestJobId); |
| 125 | scheduleJobIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 126 | scheduleJobIntent.setComponent(new ComponentName(TEST_APP_PACKAGE, TEST_APP_ACTIVITY)); |
| 127 | mContext.startActivity(scheduleJobIntent); |
| 128 | Thread.sleep(TestJobActivity.JOB_MINIMUM_LATENCY); |
| 129 | assertTrue("Job did not start after scheduling", awaitJobStart(DEFAULT_WAIT_TIMEOUT)); |
| 130 | } |
| 131 | |
| 132 | @Test |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 133 | public void testPowerWhiteList() throws Exception { |
| 134 | scheduleAndAssertJobStarted(); |
| 135 | setAppOpsModeAllowed(false); |
| 136 | mIActivityManager.makePackageIdle(TEST_APP_PACKAGE, UserHandle.USER_CURRENT); |
| 137 | assertTrue("Job did not stop after making idle", awaitJobStop(DEFAULT_WAIT_TIMEOUT)); |
| 138 | setPowerWhiteListed(true); |
| 139 | Thread.sleep(TestJobActivity.JOB_INITIAL_BACKOFF); |
| 140 | assertTrue("Job did not start after adding to power whitelist", |
| 141 | awaitJobStart(DEFAULT_WAIT_TIMEOUT)); |
| 142 | setPowerWhiteListed(false); |
| 143 | assertTrue("Job did not stop after removing from power whitelist", |
| 144 | awaitJobStop(DEFAULT_WAIT_TIMEOUT)); |
| 145 | } |
| 146 | |
| 147 | @After |
| 148 | public void tearDown() throws Exception { |
| 149 | Intent cancelJobsIntent = new Intent(TestJobActivity.ACTION_CANCEL_JOBS); |
| 150 | cancelJobsIntent.setComponent(new ComponentName(TEST_APP_PACKAGE, TEST_APP_ACTIVITY)); |
| 151 | cancelJobsIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); |
| 152 | mContext.startActivity(cancelJobsIntent); |
| 153 | mContext.unregisterReceiver(mJobStateChangeReceiver); |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 154 | setAppOpsModeAllowed(true); |
| 155 | setPowerWhiteListed(false); |
| 156 | } |
| 157 | |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 158 | private void setPowerWhiteListed(boolean whitelist) throws RemoteException { |
| 159 | if (whitelist) { |
| 160 | mDeviceIdleController.addPowerSaveWhitelistApp(TEST_APP_PACKAGE); |
| 161 | } else { |
| 162 | mDeviceIdleController.removePowerSaveWhitelistApp(TEST_APP_PACKAGE); |
| 163 | } |
| 164 | } |
| 165 | |
Suprabh Shukla | 14e5e1d | 2017-08-30 16:39:21 -0700 | [diff] [blame] | 166 | private void setAppOpsModeAllowed(boolean allow) { |
Suprabh Shukla | 3ac1daa | 2017-07-14 12:15:27 -0700 | [diff] [blame] | 167 | mAppOpsManager.setMode(AppOpsManager.OP_RUN_ANY_IN_BACKGROUND, mTestPackageUid, |
| 168 | TEST_APP_PACKAGE, allow ? AppOpsManager.MODE_ALLOWED : AppOpsManager.MODE_IGNORED); |
| 169 | } |
| 170 | |
| 171 | private boolean awaitJobStart(long timeout) throws InterruptedException { |
| 172 | return waitUntilTrue(timeout, () -> { |
| 173 | synchronized (mTestJobStatus) { |
| 174 | return (mTestJobStatus.jobId == mTestJobId) && mTestJobStatus.running; |
| 175 | } |
| 176 | }); |
| 177 | } |
| 178 | |
| 179 | private boolean awaitJobStop(long timeout) throws InterruptedException { |
| 180 | return waitUntilTrue(timeout, () -> { |
| 181 | synchronized (mTestJobStatus) { |
| 182 | return (mTestJobStatus.jobId == mTestJobId) && !mTestJobStatus.running && |
| 183 | mTestJobStatus.stopReason == JobParameters.REASON_CONSTRAINTS_NOT_SATISFIED; |
| 184 | } |
| 185 | }); |
| 186 | } |
| 187 | |
| 188 | private boolean waitUntilTrue(long timeout, Condition condition) throws InterruptedException { |
| 189 | final long deadLine = SystemClock.uptimeMillis() + timeout; |
| 190 | do { |
| 191 | Thread.sleep(POLL_INTERVAL); |
| 192 | } while (!condition.isTrue() && SystemClock.uptimeMillis() < deadLine); |
| 193 | return condition.isTrue(); |
| 194 | } |
| 195 | |
| 196 | private static final class TestJobStatus { |
| 197 | int jobId; |
| 198 | int stopReason; |
| 199 | boolean running; |
| 200 | private void reset() { |
| 201 | running = false; |
| 202 | stopReason = jobId = 0; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | private interface Condition { |
| 207 | boolean isTrue(); |
| 208 | } |
| 209 | } |