Home | History | Annotate | Download | only in intents
      1 /*
      2  * Copyright (C) 2016 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.cts.verifier.camera.intents;
     18 
     19 import android.annotation.TargetApi;
     20 import android.app.job.JobInfo;
     21 import android.app.job.JobParameters;
     22 import android.app.job.JobScheduler;
     23 import android.app.job.JobService;
     24 import android.content.Context;
     25 import android.os.ConditionVariable;
     26 import android.os.Handler;
     27 import android.util.Log;
     28 
     29 /**
     30  * Handles callback from the framework {@link android.app.job.JobScheduler}. The behaviour of this
     31  * class is configured through the static
     32  * {@link TestEnvironment}.
     33  */
     34 @TargetApi(21)
     35 public class CameraContentJobService extends JobService {
     36     private static final String TAG = "CameraContentJobService";
     37 
     38     /** Wait this long before timing out the test. */
     39     private static final long DEFAULT_TIMEOUT_MILLIS = 60000L; // 60 seconds.
     40 
     41     @Override
     42     public void onCreate() {
     43         super.onCreate();
     44         Log.i(TAG, "Created test service.");
     45     }
     46 
     47     @Override
     48     public boolean onStartJob(JobParameters params) {
     49         Log.i(TAG, "Test job executing: " + params.getJobId());
     50         TestEnvironment.getTestEnvironment().notifyExecution(params);
     51         return false;  // No work to do.
     52     }
     53 
     54     @Override
     55     public boolean onStopJob(JobParameters params) {
     56         return false;
     57     }
     58 
     59     /**
     60      * Configures the expected behaviour for each test. This object is shared across consecutive
     61      * tests, so to clear state each test is responsible for calling
     62      * {@link TestEnvironment#setUp()}.
     63      */
     64     public static final class TestEnvironment {
     65 
     66         private static TestEnvironment kTestEnvironment;
     67 
     68         private ConditionVariable mCondition = new ConditionVariable();
     69         private JobParameters mExecutedJobParameters;
     70         private boolean mCancelled = false;
     71 
     72         public static TestEnvironment getTestEnvironment() {
     73             if (kTestEnvironment == null) {
     74                 kTestEnvironment = new TestEnvironment();
     75             }
     76             return kTestEnvironment;
     77         }
     78 
     79         public JobParameters getLastJobParameters() {
     80             return mExecutedJobParameters;
     81         }
     82 
     83         /**
     84          * Block the test thread, waiting on the JobScheduler to execute some previously scheduled
     85          * job on this service.
     86          */
     87         public boolean awaitExecution() throws InterruptedException {
     88             final boolean executed = mCondition.block(DEFAULT_TIMEOUT_MILLIS);
     89             if (mCancelled) {
     90                 return false;
     91             }
     92             return executed;
     93         }
     94 
     95         private void notifyExecution(JobParameters params) {
     96             Log.d(TAG, "Job executed:" + params.getJobId());
     97             mExecutedJobParameters = params;
     98             mCondition.open();
     99         }
    100 
    101         // Cancel any ongoing wait. Will cause awaitExecution to return false (timeout)
    102         public void cancelWait() {
    103             mCancelled = true;
    104             mCondition.open();
    105         }
    106 
    107         /** Called before starting a new test */
    108         public void setUp() {
    109             mCondition.close();
    110             mExecutedJobParameters = null;
    111             mCancelled = false;
    112         }
    113 
    114     }
    115 }
    116