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.view.KeyEvent;
     23 import android.os.Bundle;
     24 import android.util.Log;
     25 
     26 /**
     27  * This is an example implementation of the {@link android.app.Instrumentation}
     28  * class, allowing you to run tests against application code.  The
     29  * instrumentation implementation here is loaded into the application's
     30  * process, for controlling and monitoring what it does.
     31  */
     32 public class ContactsFilterInstrumentation extends Instrumentation {
     33     @Override
     34     public void onCreate(Bundle arguments) {
     35         super.onCreate(arguments);
     36 
     37         // When this instrumentation is created, we simply want to start
     38         // its test code off in a separate thread, which will call back
     39         // to us in onStart().
     40         start();
     41     }
     42 
     43     @Override
     44     public void onStart() {
     45         super.onStart();
     46         // First start the activity we are instrumenting -- the contacts
     47         // list.
     48         Intent intent = new Intent(Intent.ACTION_MAIN);
     49         intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
     50         intent.setClassName(getTargetContext(),
     51                 "com.android.phone.Dialer");
     52         Activity activity = startActivitySync(intent);
     53 
     54         // This is the Activity object that was started, to do with as we want.
     55         Log.i("ContactsFilterInstrumentation", "Started: " + activity);
     56 
     57         // We are going to enqueue a couple key events to simulate the user
     58         // filtering the list.  This is the low-level API so we must send both
     59         // down and up events.
     60         sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_M));
     61         sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_M));
     62         sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_A));
     63         sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_A));
     64 
     65         // Wait for the activity to finish all of its processing.
     66         waitForIdleSync();
     67 
     68         // And we are done!
     69         Log.i("ContactsFilterInstrumentation", "Done!");
     70         finish(Activity.RESULT_OK, null);
     71     }
     72 }
     73 
     74