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