1 /*
2  * Copyright (C) 2006 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 android.test;
18 
19 import android.util.Log;
20 import junit.framework.Test;
21 import junit.framework.TestListener;
22 
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Set;
26 
27 /**
28  * Prints the test progress to stdout. Android includes a default
29  * implementation and calls these methods to print out test progress; you
30  * probably will not need to create or extend this class or call its methods manually.
31  * See the full {@link android.test} package description for information about
32  * getting test results.
33  *
34  * {@hide} Not needed for 1.0 SDK.
35  */
36 @Deprecated
37 public class TestPrinter implements TestRunner.Listener, TestListener {
38 
39     private String mTag;
40     private boolean mOnlyFailures;
41     private Set<String> mFailedTests = new HashSet<String>();
42 
43 
TestPrinter(String tag, boolean onlyFailures)44     public TestPrinter(String tag, boolean onlyFailures) {
45         mTag = tag;
46         mOnlyFailures = onlyFailures;
47     }
48 
started(String className)49     public void started(String className) {
50         if (!mOnlyFailures) {
51             Log.i(mTag, "started: " + className);
52         }
53     }
54 
finished(String className)55     public void finished(String className) {
56         if (!mOnlyFailures) {
57             Log.i(mTag, "finished: " + className);
58         }
59     }
60 
performance(String className, long itemTimeNS, int iterations, List<TestRunner.IntermediateTime> intermediates)61     public void performance(String className,
62             long itemTimeNS, int iterations,
63             List<TestRunner.IntermediateTime> intermediates) {
64         Log.i(mTag, "perf: " + className + " = " + itemTimeNS + "ns/op (done "
65                 + iterations + " times)");
66         if (intermediates != null && intermediates.size() > 0) {
67             int N = intermediates.size();
68             for (int i = 0; i < N; i++) {
69                 TestRunner.IntermediateTime time = intermediates.get(i);
70                 Log.i(mTag, "  intermediate: " + time.name + " = "
71                         + time.timeInNS + "ns");
72             }
73         }
74     }
75 
passed(String className)76     public void passed(String className) {
77         if (!mOnlyFailures) {
78             Log.i(mTag, "passed: " + className);
79         }
80     }
81 
failed(String className, Throwable exception)82     public void failed(String className, Throwable exception) {
83         Log.i(mTag, "failed: " + className);
84         Log.i(mTag, "----- begin exception -----");
85         Log.i(mTag, "", exception);
86         Log.i(mTag, "----- end exception -----");
87     }
88 
failed(Test test, Throwable t)89     private void failed(Test test, Throwable t) {
90         mFailedTests.add(test.toString());
91         failed(test.toString(), t);
92     }
93 
addError(Test test, Throwable t)94     public void addError(Test test, Throwable t) {
95         failed(test, t);
96     }
97 
addFailure(Test test, junit.framework.AssertionFailedError t)98     public void addFailure(Test test, junit.framework.AssertionFailedError t) {
99         failed(test, t);
100     }
101 
endTest(Test test)102     public void endTest(Test test) {
103         finished(test.toString());
104         if (!mFailedTests.contains(test.toString())) {
105             passed(test.toString());
106         }
107         mFailedTests.remove(test.toString());
108     }
109 
startTest(Test test)110     public void startTest(Test test) {
111         started(test.toString());
112     }
113 }
114