Home | History | Annotate | Download | only in shareduidtests
      1 /*
      2  * Copyright (C) 2018 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 package android.jobscheduler.cts.shareduidtests;
     17 
     18 import android.annotation.TargetApi;
     19 import android.app.job.JobInfo;
     20 import android.app.job.JobScheduler;
     21 import android.content.BroadcastReceiver;
     22 import android.content.ComponentName;
     23 import android.content.Context;
     24 import android.content.Intent;
     25 import android.content.IntentFilter;
     26 import android.content.pm.PackageManager;
     27 import android.jobscheduler.MockJobService;
     28 import android.test.AndroidTestCase;
     29 
     30 import java.util.concurrent.CountDownLatch;
     31 import java.util.concurrent.TimeUnit;
     32 
     33 @TargetApi(27)
     34 public class SharedUidTest extends AndroidTestCase {
     35     private static final String TAG = "SharedUidTest";
     36 
     37     private static final String OTHER_PACKAGE = "android.jobscheduler.cts.shareduid";
     38     private static final long BROADCAST_TIMEOUT_SECONDS = 2 * 60;
     39 
     40     /**
     41      * Test to make sure disabling a package wouldn't cancel the jobs from other packages that use
     42      * the same shared UID.
     43      */
     44     public void testCancelDisabledPackageJob() throws Exception {
     45         final int JOBID = 1122331;
     46 
     47         final JobScheduler js = getContext().getSystemService(JobScheduler.class);
     48 
     49         try {
     50             JobInfo ji = new JobInfo.Builder(JOBID,
     51                     new ComponentName(getContext(), MockJobService.class))
     52                     .setMinimumLatency(1000 * 60 * 60 * 24 /* 1 day */)
     53                     .build();
     54 
     55             js.cancel(JOBID);
     56 
     57             assertEquals(JobScheduler.RESULT_SUCCESS, js.schedule(ji));
     58 
     59             // The job should be scheduled.
     60             assertNotNull("Job should be registered", js.getPendingJob(JOBID));
     61 
     62             // Create a filter with the loweset priority to wait until the jobscheduelr receives the
     63             // intent.
     64             final CountDownLatch latch = new CountDownLatch(1);
     65 
     66             final IntentFilter filter = new IntentFilter();
     67             filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
     68             filter.addDataScheme("package");
     69             filter.setPriority(IntentFilter.SYSTEM_LOW_PRIORITY);
     70 
     71             getContext().registerReceiver(new BroadcastReceiver() {
     72                 @Override
     73                 public void onReceive(Context context, Intent intent) {
     74                     if (OTHER_PACKAGE.equals(intent.getData().getSchemeSpecificPart())) {
     75                         latch.countDown();
     76                     }
     77                 }
     78             }, filter);
     79 
     80 
     81             // Disable the other package with the same UID.
     82             final PackageManager pm = getContext().getPackageManager();
     83             pm.setApplicationEnabledSetting(
     84                     OTHER_PACKAGE,
     85                     PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
     86                     PackageManager.DONT_KILL_APP);
     87 
     88             assertEquals(PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
     89                     pm.getApplicationEnabledSetting(OTHER_PACKAGE));
     90 
     91             // Wait until our receiver gets the broadcast.
     92             assertTrue(latch.await(BROADCAST_TIMEOUT_SECONDS, TimeUnit.SECONDS));
     93 
     94             // Make sure the job hasn't been canceled.
     95             assertNotNull("Job shouldn't be canceled", js.getPendingJob(JOBID));
     96         } finally {
     97             js.cancel(JOBID);
     98         }
     99     }
    100 }
    101