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