1/*
2 * Copyright (C) 2010 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.android.commands.monkey;
18
19
20import android.app.IActivityManager;
21import android.content.ComponentName;
22import android.os.Bundle;
23import android.os.RemoteException;
24import android.view.IWindowManager;
25
26/**
27 * monkey instrumentation event
28 */
29public class MonkeyInstrumentationEvent extends MonkeyEvent {
30    String mRunnerName;
31    String mTestCaseName;
32
33    public MonkeyInstrumentationEvent(String testCaseName, String runnerName) {
34        super(EVENT_TYPE_ACTIVITY);
35        mTestCaseName = testCaseName;
36        mRunnerName = runnerName;
37    }
38
39    @Override
40    public int injectEvent(IWindowManager iwm, IActivityManager iam, int verbose) {
41        ComponentName cn = ComponentName.unflattenFromString(mRunnerName);
42        if (cn == null || mTestCaseName == null)
43            throw new IllegalArgumentException("Bad component name");
44
45        Bundle args = new Bundle();
46        args.putString("class", mTestCaseName);
47        try {
48            iam.startInstrumentation(cn, null, 0, args, null, null, 0);
49        } catch (RemoteException e) {
50            System.err.println("** Failed talking with activity manager!");
51            return MonkeyEvent.INJECT_ERROR_REMOTE_EXCEPTION;
52        }
53        return MonkeyEvent.INJECT_SUCCESS;
54    }
55}
56