Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2009 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 android.app.cts;
     18 
     19 import android.app.IntentService;
     20 import android.content.Intent;
     21 import android.cts.util.PollingCheck;
     22 import android.os.Binder;
     23 import android.os.IBinder;
     24 
     25 public class IntentServiceStub extends IntentService {
     26     public IntentServiceStub() {
     27         super("IntentServiceStub");
     28     }
     29 
     30     public static final String ISS_ADD = "add";
     31     public static final String ISS_VALUE = "value";
     32 
     33     private static int onHandleIntentCalled;
     34     private static boolean onBindCalled;
     35     private static boolean onCreateCalled;
     36     private static boolean onDestroyCalled;
     37     private static boolean onStartCalled;
     38     private static int accumulator;
     39 
     40     private static Throwable throwable;
     41 
     42     public synchronized static void reset() {
     43         onHandleIntentCalled = 0;
     44         onBindCalled = false;
     45         onCreateCalled = false;
     46         onDestroyCalled = false;
     47         onStartCalled = false;
     48         accumulator = 0;
     49         throwable = null;
     50     }
     51 
     52     public static void waitToFinish(long timeout) throws Throwable {
     53         new PollingCheck(timeout) {
     54             @Override
     55             protected boolean check() {
     56                 synchronized (IntentServiceStub.class) {
     57                     return IntentServiceStub.onDestroyCalled;
     58                 }
     59             }
     60         }.run();
     61         if (throwable != null) {
     62             throw throwable;
     63         }
     64     }
     65 
     66     @Override
     67     protected void onHandleIntent(Intent intent) {
     68         synchronized (IntentServiceStub.class) {
     69             onHandleIntentCalled += 1;
     70             try {
     71                 String action = intent.getAction();
     72                 if (action != null && action.equals(ISS_ADD)) {
     73                     accumulator += intent.getIntExtra(ISS_VALUE, 0);
     74                 }
     75             } catch (Throwable t) {
     76                 throwable = t;
     77             }
     78         }
     79     }
     80 
     81     @Override
     82     public IBinder onBind(Intent intent) {
     83         synchronized (IntentServiceStub.class) {
     84             onBindCalled = true;
     85         }
     86         return new Binder();
     87     }
     88 
     89     @Override
     90     public void onCreate() {
     91         synchronized (IntentServiceStub.class) {
     92             onCreateCalled = true;
     93         }
     94         super.onCreate();
     95     }
     96 
     97     @Override
     98     public void onDestroy() {
     99         synchronized (IntentServiceStub.class) {
    100             onDestroyCalled = true;
    101         }
    102         super.onDestroy();
    103     }
    104 
    105     @Override
    106     public void onStart(Intent intent, int startId) {
    107         synchronized (IntentService.class) {
    108             onStartCalled = true;
    109         }
    110         super.onStart(intent, startId);
    111     }
    112 
    113     public synchronized static int getOnHandleIntentCalledCount() {
    114         return onHandleIntentCalled;
    115     }
    116 
    117     public synchronized static boolean isOnBindCalled() {
    118         return onBindCalled;
    119     }
    120 
    121     public synchronized static boolean isOnCreateCalled() {
    122         return onCreateCalled;
    123     }
    124 
    125     public synchronized static boolean isOnDestroyCalled() {
    126         return onDestroyCalled;
    127     }
    128 
    129     public synchronized static boolean isOnStartCalled() {
    130         return onStartCalled;
    131     }
    132 
    133     public synchronized static int getAccumulator() {
    134         return accumulator;
    135     }
    136 }
    137