Home | History | Annotate | Download | only in cts
      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 android.app.cts;
     18 
     19 import android.app.Activity;
     20 import android.app.Instrumentation;
     21 import android.content.BroadcastReceiver;
     22 import android.content.Context;
     23 import android.content.Intent;
     24 import android.os.Bundle;
     25 import android.util.Log;
     26 
     27 public class BaseProcessInstrumentation extends Instrumentation {
     28     final String mMainProc;
     29     final String mReceiverClass;
     30 
     31     public BaseProcessInstrumentation(String mainProc, String receiverClass) {
     32         mMainProc = mainProc;
     33         mReceiverClass = receiverClass;
     34     }
     35 
     36     @Override
     37     public void onCreate(Bundle arguments) {
     38         super.onCreate(arguments);
     39         final String proc = getProcessName();
     40         //Log.i("xxx", "Instrumentation starting in " + proc);
     41         final Bundle result = new Bundle();
     42         result.putBoolean(proc, true);
     43         if (proc.equals(mMainProc)) {
     44             // We are running in the main instr process...  start a service that will launch
     45             // a secondary proc.
     46             Intent intent = new Intent(Intent.ACTION_MAIN);
     47             intent.setClassName(ActivityManagerTest.SIMPLE_PACKAGE_NAME, mReceiverClass);
     48             intent.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
     49             //Log.i("xxx", "Instrumentation sending broadcast: " + intent);
     50             getContext().sendOrderedBroadcast(intent, null, new BroadcastReceiver() {
     51                 @Override public void onReceive(Context context, Intent intent) {
     52                     //Log.i("xxx", "Instrumentation finishing in " + proc);
     53                     finish(Activity.RESULT_OK, result);
     54                 }
     55             }, null, 0, null, null);
     56         } else {
     57             // We are running in a secondary proc, just report it.
     58             //Log.i("xxx", "Instrumentation adding result in " + proc);
     59             addResults(result);
     60         }
     61     }
     62 }
     63