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.TestIdentifier, java.lang.String)
50      */
51     @Override
testFailed(TestIdentifier test, String trace)52     public synchronized void testFailed(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#testIgnored(com.android.ddmlib.testrunner.TestIdentifier)
58      */
59     @Override
testIgnored(TestIdentifier test)60     public synchronized void testIgnored(TestIdentifier test) {
61         // ignore - should be impossible since this is only collecting test information
62     }
63 
64     /* (non-Javadoc)
65      * @see com.android.ddmlib.testrunner.ITestRunListener#testAssumptionFailure(com.android.ddmlib.testrunner.TestIdentifier, java.lang.String)
66      */
67     @Override
testAssumptionFailure(TestIdentifier test, String trace)68     public synchronized void testAssumptionFailure(TestIdentifier test, String trace) {
69         // ignore - should be impossible since this is only collecting test information
70     }
71 
72     /* (non-Javadoc)
73      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunEnded(long, Map<String, String>)
74      */
75     @Override
testRunEnded(long elapsedTime, Map<String, String> runMetrics)76     public synchronized void testRunEnded(long elapsedTime, Map<String, String> runMetrics) {
77         // ignore
78     }
79 
80     /* (non-Javadoc)
81      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunFailed(java.lang.String)
82      */
83     @Override
testRunFailed(String errorMessage)84     public synchronized void testRunFailed(String errorMessage) {
85         mErrorMessage = errorMessage;
86     }
87 
88     /* (non-Javadoc)
89      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunStarted(int)
90      */
91     @Override
testRunStarted(String ignoredRunName, int testCount)92     public synchronized void testRunStarted(String ignoredRunName, int testCount) {
93         mTotalTestCount = testCount;
94     }
95 
96     /* (non-Javadoc)
97      * @see com.android.ddmlib.testrunner.ITestRunListener#testRunStopped(long)
98      */
99     @Override
testRunStopped(long elapsedTime)100     public synchronized void testRunStopped(long elapsedTime) {
101         // ignore
102     }
103 
104     /* (non-Javadoc)
105      * @see com.android.ddmlib.testrunner.ITestRunListener#testStarted(com.android.ddmlib.testrunner.TestIdentifier)
106      */
107     @Override
testStarted(TestIdentifier test)108     public synchronized void testStarted(TestIdentifier test) {
109         TestSuiteReference suiteRef = mDeviceSuiteRef.getTestSuite(test.getClassName());
110         if (suiteRef == null) {
111             suiteRef = new TestSuiteReference(test.getClassName());
112             mDeviceSuiteRef.addTest(suiteRef);
113         }
114 
115         suiteRef.addTest(new TestCaseReference(mDeviceName, test));
116     }
117 
118     /**
119      * Returns the total test count in the test run.
120      */
getTestCaseCount()121     public synchronized int getTestCaseCount() {
122         return mTotalTestCount;
123     }
124 
125     /**
126      * Returns the error message that was reported when collecting test info.
127      * Returns <code>null</code> if no error occurred.
128      */
getErrorMessage()129     public synchronized String getErrorMessage() {
130         return mErrorMessage;
131     }
132 
getDeviceSuite()133     public TestSuiteReference getDeviceSuite() {
134         return mDeviceSuiteRef;
135     }
136 }
137