• 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.view.KeyEvent;
23 import android.os.Bundle;
24 import android.util.Log;
25 
26 import java.util.Map;
27 
28 /**
29  * This is an example implementation of the {@link android.app.Instrumentation}
30  * class demonstrating instrumentation against one of this application's sample
31  * activities.
32  */
33 public class LocalSampleInstrumentation extends Instrumentation {
34     public abstract static class ActivityRunnable implements Runnable {
35         public final Activity activity;
ActivityRunnable(Activity _activity)36         public ActivityRunnable(Activity _activity) {
37             activity = _activity;
38         }
39     }
40 
41     @Override
onCreate(Bundle arguments)42     public void onCreate(Bundle arguments) {
43         super.onCreate(arguments);
44 
45         // When this instrumentation is created, we simply want to start
46         // its test code off in a separate thread, which will call back
47         // to us in onStart().
48         start();
49     }
50 
51     @Override
onStart()52     public void onStart() {
53         super.onStart();
54         // First start the activity we are instrumenting -- the save/restore
55         // state sample, which has a nice edit text into which we can write
56         // text.
57         Intent intent = new Intent(Intent.ACTION_MAIN);
58         intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
59         intent.setClass(getTargetContext(), SaveRestoreState.class);
60         SaveRestoreState activity = (SaveRestoreState)startActivitySync(intent);
61 
62         // This is the Activity object that was started, to do with as we want.
63         Log.i("LocalSampleInstrumentation",
64               "Initial text: " + activity.getSavedText());
65 
66         // Clear the text so we start fresh.
67         runOnMainSync(new ActivityRunnable(activity) {
68             public void run() {
69                 ((SaveRestoreState)activity).setSavedText("");
70             }
71         });
72 
73         // Act like the user is typing some text.
74         sendKeySync(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_SHIFT_LEFT));
75         sendCharacterSync(KeyEvent.KEYCODE_H);
76         sendKeySync(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_SHIFT_LEFT));
77         sendCharacterSync(KeyEvent.KEYCODE_E);
78         sendCharacterSync(KeyEvent.KEYCODE_L);
79         sendCharacterSync(KeyEvent.KEYCODE_L);
80         sendCharacterSync(KeyEvent.KEYCODE_O);
81 
82         // Wait for the activity to finish all of its processing.
83         waitForIdleSync();
84 
85         // Retrieve the text we should have written...
86         Log.i("LocalSampleInstrumentation",
87               "Final text: " + activity.getSavedText());
88 
89         // And we are done!
90         Log.i("ContactsFilterInstrumentation", "Done!");
91         finish(Activity.RESULT_OK, null);
92     }
93 }
94 
95