| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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; |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import java.util.Set; |
| 25 | |
| 26 | /** |
| 27 | * Prints the test progress to stdout. Android includes a default |
| 28 | * implementation and calls these methods to print out test progress; you |
| 29 | * probably will not need to create or extend this class or call its methods manually. |
| 30 | * See the full {@link android.test} package description for information about |
| 31 | * getting test results. |
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 32 | * |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | * {@hide} Not needed for 1.0 SDK. |
| 34 | */ |
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 35 | @Deprecated |
| Paul Duffin | b3f83ae | 2017-06-22 14:17:29 +0100 | [diff] [blame] | 36 | class TestPrinter implements TestListener { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 37 | |
| 38 | private String mTag; |
| 39 | private boolean mOnlyFailures; |
| 40 | private Set<String> mFailedTests = new HashSet<String>(); |
| 41 | |
| 42 | |
| Paul Duffin | b3f83ae | 2017-06-22 14:17:29 +0100 | [diff] [blame] | 43 | TestPrinter(String tag, boolean onlyFailures) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | mTag = tag; |
| 45 | mOnlyFailures = onlyFailures; |
| 46 | } |
| 47 | |
| Paul Duffin | b3f83ae | 2017-06-22 14:17:29 +0100 | [diff] [blame] | 48 | private void started(String className) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | if (!mOnlyFailures) { |
| 50 | Log.i(mTag, "started: " + className); |
| 51 | } |
| 52 | } |
| 53 | |
| Paul Duffin | b3f83ae | 2017-06-22 14:17:29 +0100 | [diff] [blame] | 54 | private void finished(String className) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | if (!mOnlyFailures) { |
| 56 | Log.i(mTag, "finished: " + className); |
| 57 | } |
| 58 | } |
| 59 | |
| Paul Duffin | b3f83ae | 2017-06-22 14:17:29 +0100 | [diff] [blame] | 60 | private void passed(String className) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 61 | if (!mOnlyFailures) { |
| 62 | Log.i(mTag, "passed: " + className); |
| 63 | } |
| 64 | } |
| 65 | |
| Paul Duffin | b3f83ae | 2017-06-22 14:17:29 +0100 | [diff] [blame] | 66 | private void failed(String className, Throwable exception) { |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | Log.i(mTag, "failed: " + className); |
| 68 | Log.i(mTag, "----- begin exception -----"); |
| 69 | Log.i(mTag, "", exception); |
| 70 | Log.i(mTag, "----- end exception -----"); |
| 71 | } |
| 72 | |
| 73 | private void failed(Test test, Throwable t) { |
| 74 | mFailedTests.add(test.toString()); |
| 75 | failed(test.toString(), t); |
| 76 | } |
| Stephan Linzner | b51617f | 2016-01-27 18:09:50 -0800 | [diff] [blame] | 77 | |
| The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 78 | public void addError(Test test, Throwable t) { |
| 79 | failed(test, t); |
| 80 | } |
| 81 | |
| 82 | public void addFailure(Test test, junit.framework.AssertionFailedError t) { |
| 83 | failed(test, t); |
| 84 | } |
| 85 | |
| 86 | public void endTest(Test test) { |
| 87 | finished(test.toString()); |
| 88 | if (!mFailedTests.contains(test.toString())) { |
| 89 | passed(test.toString()); |
| 90 | } |
| 91 | mFailedTests.remove(test.toString()); |
| 92 | } |
| 93 | |
| 94 | public void startTest(Test test) { |
| 95 | started(test.toString()); |
| 96 | } |
| 97 | } |