1/*
2 * Copyright (C) 2013 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.uiautomator.testrunner;
18
19import android.test.AndroidTestRunner;
20import android.test.InstrumentationTestRunner;
21
22import com.android.uiautomator.core.Tracer;
23
24import junit.framework.AssertionFailedError;
25import junit.framework.Test;
26import junit.framework.TestListener;
27
28/**
29 * Test runner for {@link UiAutomatorTestCase}s. Such tests are executed
30 * on the device and have access to an applications context.
31 * @deprecated New tests should be written using UI Automator 2.0 which is available as part of the
32 * Android Testing Support Library.
33 */
34@Deprecated
35public class UiAutomatorInstrumentationTestRunner extends InstrumentationTestRunner {
36
37    @Override
38    public void onStart() {
39        // process runner arguments before test starts
40        String traceType = getArguments().getString("traceOutputMode");
41        if(traceType != null) {
42            Tracer.Mode mode = Tracer.Mode.valueOf(Tracer.Mode.class, traceType);
43            if (mode == Tracer.Mode.FILE || mode == Tracer.Mode.ALL) {
44                String filename = getArguments().getString("traceLogFilename");
45                if (filename == null) {
46                    throw new RuntimeException("Name of log file not specified. " +
47                            "Please specify it using traceLogFilename parameter");
48                }
49                Tracer.getInstance().setOutputFilename(filename);
50            }
51            Tracer.getInstance().setOutputMode(mode);
52        }
53        super.onStart();
54    }
55
56    @Override
57    protected AndroidTestRunner getAndroidTestRunner() {
58        AndroidTestRunner testRunner = super.getAndroidTestRunner();
59        testRunner.addTestListener(new TestListener() {
60            @Override
61            public void startTest(Test test) {
62                if (test instanceof UiAutomatorTestCase) {
63                    ((UiAutomatorTestCase)test).initialize(getArguments());
64                }
65            }
66
67            @Override
68            public void endTest(Test test) {
69            }
70
71            @Override
72            public void addFailure(Test test, AssertionFailedError e) {
73            }
74
75            @Override
76            public void addError(Test test, Throwable t) {
77            }
78        });
79        return testRunner;
80    }
81}
82