Home | History | Annotate | Download | only in app
      1 /*
      2  * Copyright (C) 2007 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.example.android.apis.app;
     18 
     19 import android.app.Activity;
     20 import android.app.Instrumentation;
     21 import android.content.Intent;
     22 import android.content.IntentFilter;
     23 import android.os.IBinder;
     24 import android.view.KeyEvent;
     25 import android.provider.Contacts;
     26 import android.os.Bundle;
     27 import android.util.Log;
     28 
     29 import java.util.Map;
     30 
     31 /**
     32  * This is an example implementation of the {@link android.app.Instrumentation}
     33  * class, allowing you to run tests against application code.  The
     34  * instrumentation implementation here is loaded into the application's
     35  * process, for controlling and monitoring what it does.
     36  */
     37 public class ContactsSelectInstrumentation extends Instrumentation {
     38     @Override
     39     public void onCreate(Bundle arguments) {
     40         super.onCreate(arguments);
     41 
     42         // When this instrumentation is created, we simply want to start
     43         // its test code off in a separate thread, which will call back
     44         // to us in onStart().
     45         start();
     46     }
     47 
     48     @Override
     49     public void onStart() {
     50         super.onStart();
     51         // First start the activity we are instrumenting -- the contacts
     52         // list.
     53         Intent intent = new Intent(Intent.ACTION_MAIN);
     54         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     55         intent.setClassName(getTargetContext(),
     56                 "com.android.phone.Dialer");
     57         Activity activity = startActivitySync(intent);
     58 
     59         // This is the Activity object that was started, to do with as we want.
     60         Log.i("ContactsSelectInstrumentation", "Started: " + activity);
     61 
     62         // Monitor for the expected start activity call.
     63         ActivityMonitor am = addMonitor(IntentFilter.create(
     64             Intent.ACTION_VIEW, Contacts.People.CONTENT_ITEM_TYPE), null, true);
     65 
     66         // We are going to enqueue a couple key events to simulate the user
     67         // selecting an item in the list.
     68         sendKeySync(new KeyEvent(
     69             KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_DOWN));
     70         sendKeySync(new KeyEvent(
     71             KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_DOWN));
     72         sendKeySync(new KeyEvent(
     73             KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_DPAD_CENTER));
     74         sendKeySync(new KeyEvent(
     75             KeyEvent.ACTION_UP, KeyEvent.KEYCODE_DPAD_CENTER));
     76 
     77         // Was the expected activity started?
     78         if (checkMonitorHit(am, 1)) {
     79             Log.i("ContactsSelectInstrumentation", "Activity started!");
     80         } else {
     81             Log.i("ContactsSelectInstrumentation", "*** ACTIVITY NOT STARTED!");
     82         }
     83 
     84         // And we are done!
     85         Log.i("ContactsSelectInstrumentation", "Done!");
     86         finish(Activity.RESULT_OK, null);
     87     }
     88 }
     89 
     90