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