1/*
2 * Copyright (C) 2012 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 */
16package com.android.test.runner.listener;
17
18import android.app.Instrumentation;
19import android.os.Bundle;
20
21import org.junit.runner.notification.RunListener;
22
23import java.io.PrintStream;
24
25/**
26 * A {@link RunListener} that has access to a {@link Instrumentation}. This is useful for
27 * test result listeners that want to dump data back to the instrumentation results.
28 */
29public abstract class InstrumentationRunListener extends RunListener {
30
31    private final Instrumentation mInstr;
32
33    public InstrumentationRunListener(Instrumentation instr) {
34        mInstr = instr;
35    }
36
37    public Instrumentation getInstrumentation() {
38        return mInstr;
39    }
40
41    /**
42     * Convenience method for {@link #getInstrumentation()#sendStatus()}
43     */
44    public void sendStatus(int code, Bundle bundle) {
45        getInstrumentation().sendStatus(code, bundle);
46    }
47
48    /**
49     * Optional callback subclasses can implement. Will be called when instrumentation run
50     * completes.
51     *
52     * @param streamResult the {@link PrintStream} to instrumentation out.
53     * @param resultBundle the instrumentation result bundle. Can be used to inject key-value pairs
54     * into the instrumentation output when run in -r/raw mode
55     */
56    public void instrumentationRunFinished(PrintStream streamResult, Bundle resultBundle) {
57    }
58}
59