1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Eclipse Public License, Version 1.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.eclipse.org/org/documents/epl-v10.php 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 com.android.ide.eclipse.adt.internal.launch.junit.runtime; 18 19 import com.android.ddmlib.testrunner.ITestRunListener; 20 import com.android.ddmlib.testrunner.TestIdentifier; 21 22 import java.util.Map; 23 24 /** 25 * Collects info about tests to be executed by listening to the results of an Android test run. 26 */ 27 class TestCollector implements ITestRunListener { 28 private final String mDeviceName; 29 private final TestSuiteReference mDeviceSuiteRef; 30 31 private int mTotalTestCount; 32 /** test name to test suite reference map. */ 33 34 private String mErrorMessage = null; 35 TestCollector(String deviceName)36 TestCollector(String deviceName) { 37 mDeviceName = deviceName; 38 mDeviceSuiteRef = new TestSuiteReference(deviceName); 39 40 mTotalTestCount = 0; 41 } 42 43 @Override testEnded(TestIdentifier test, Map<String, String> testMetrics)44 public synchronized void testEnded(TestIdentifier test, Map<String, String> testMetrics) { 45 // ignore 46 } 47 48 /* (non-Javadoc) 49 * @see com.android.ddmlib.testrunner.ITestRunListener#testFailed(com.android.ddmlib.testrunner.ITestRunListener.TestFailure, com.android.ddmlib.testrunner.TestIdentifier, java.lang.String) 50 */ 51 @Override testFailed(TestFailure status, TestIdentifier test, String trace)52 public synchronized void testFailed(TestFailure status, TestIdentifier test, String trace) { 53 // ignore - should be impossible since this is only collecting test information 54 } 55 56 /* (non-Javadoc) 57 * @see com.android.ddmlib.testrunner.ITestRunListener#testRunEnded(long, Map<String, String>) 58 */ 59 @Override testRunEnded(long elapsedTime, Map<String, String> runMetrics)60 public synchronized void testRunEnded(long elapsedTime, Map<String, String> runMetrics) { 61 // ignore 62 } 63 64 /* (non-Javadoc) 65 * @see com.android.ddmlib.testrunner.ITestRunListener#testRunFailed(java.lang.String) 66 */ 67 @Override testRunFailed(String errorMessage)68 public synchronized void testRunFailed(String errorMessage) { 69 mErrorMessage = errorMessage; 70 } 71 72 /* (non-Javadoc) 73 * @see com.android.ddmlib.testrunner.ITestRunListener#testRunStarted(int) 74 */ 75 @Override testRunStarted(String ignoredRunName, int testCount)76 public synchronized void testRunStarted(String ignoredRunName, int testCount) { 77 mTotalTestCount = testCount; 78 } 79 80 /* (non-Javadoc) 81 * @see com.android.ddmlib.testrunner.ITestRunListener#testRunStopped(long) 82 */ 83 @Override testRunStopped(long elapsedTime)84 public synchronized void testRunStopped(long elapsedTime) { 85 // ignore 86 } 87 88 /* (non-Javadoc) 89 * @see com.android.ddmlib.testrunner.ITestRunListener#testStarted(com.android.ddmlib.testrunner.TestIdentifier) 90 */ 91 @Override testStarted(TestIdentifier test)92 public synchronized void testStarted(TestIdentifier test) { 93 TestSuiteReference suiteRef = mDeviceSuiteRef.getTestSuite(test.getClassName()); 94 if (suiteRef == null) { 95 suiteRef = new TestSuiteReference(test.getClassName()); 96 mDeviceSuiteRef.addTest(suiteRef); 97 } 98 99 suiteRef.addTest(new TestCaseReference(mDeviceName, test)); 100 } 101 102 /** 103 * Returns the total test count in the test run. 104 */ getTestCaseCount()105 public synchronized int getTestCaseCount() { 106 return mTotalTestCount; 107 } 108 109 /** 110 * Returns the error message that was reported when collecting test info. 111 * Returns <code>null</code> if no error occurred. 112 */ getErrorMessage()113 public synchronized String getErrorMessage() { 114 return mErrorMessage; 115 } 116 getDeviceSuite()117 public TestSuiteReference getDeviceSuite() { 118 return mDeviceSuiteRef; 119 } 120 } 121