1 /* 2 * Copyright (C) 2010 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 package com.android.tradefed.result; 17 18 import com.android.tradefed.invoker.IInvocationContext; 19 import com.android.tradefed.log.LogUtil.CLog; 20 21 import java.util.ArrayList; 22 import java.util.List; 23 24 /** 25 * Helper class for gathering and reporting {@link TestSummary} for set of listeners 26 */ 27 public class InvocationSummaryHelper { 28 InvocationSummaryHelper()29 private InvocationSummaryHelper() { 30 } 31 reportInvocationStarted( List<ITestInvocationListener> listeners, IInvocationContext context)32 public static void reportInvocationStarted( 33 List<ITestInvocationListener> listeners, IInvocationContext context) { 34 List<TestSummary> summaries = new ArrayList<TestSummary>(listeners.size()); 35 for (ITestInvocationListener listener : listeners) { 36 String log = "putEarlySummary"; 37 try { 38 if (listener instanceof ITestSummaryListener) { 39 ((ITestSummaryListener) listener).putEarlySummary(summaries); 40 } 41 log = "invocationStarted"; 42 listener.invocationStarted(context); 43 log = "getSummary"; 44 TestSummary summary = listener.getSummary(); 45 if (summary != null) { 46 summary.setSource(listener.getClass().getName()); 47 summaries.add(summary); 48 } 49 } catch (RuntimeException e) { 50 CLog.e( 51 "RuntimeException while invoking %s on %s", 52 log, listener.getClass().getName()); 53 CLog.e(e); 54 } 55 } 56 } 57 reportInvocationEnded(List<ITestInvocationListener> listeners, long elapsedTime)58 public static void reportInvocationEnded(List<ITestInvocationListener> listeners, 59 long elapsedTime) { 60 List<TestSummary> summaries = new ArrayList<TestSummary>(listeners.size()); 61 for (ITestInvocationListener listener : listeners) { 62 /* 63 * For InvocationListeners (as opposed to SummaryListeners), we call 64 * invocationEnded() followed by getSummary(). If getSummary returns a non-null 65 * value, we gather it to pass to the SummaryListeners below. 66 */ 67 if (!(listener instanceof ITestSummaryListener)) { 68 try { 69 listener.invocationEnded(elapsedTime); 70 TestSummary summary = listener.getSummary(); 71 if (summary != null) { 72 summary.setSource(listener.getClass().getName()); 73 summaries.add(summary); 74 } 75 } catch (RuntimeException e) { 76 CLog.e( 77 "RuntimeException while invoking invocationEnded on %s", 78 listener.getClass().getName()); 79 CLog.e(e); 80 } 81 } 82 } 83 84 /* 85 * For SummaryListeners (as opposed to InvocationListeners), we now call putSummary() 86 * followed by invocationEnded(). This means that the SummaryListeners will have 87 * access to the summaries (if any) when invocationEnded is called. 88 */ 89 for (ITestInvocationListener listener : listeners) { 90 if (listener instanceof ITestSummaryListener) { 91 try { 92 ((ITestSummaryListener) listener).putSummary(summaries); 93 listener.invocationEnded(elapsedTime); 94 } catch (RuntimeException e) { 95 CLog.e( 96 "RuntimeException while invoking invocationEnded on %s", 97 listener.getClass().getName()); 98 CLog.e(e); 99 } 100 } 101 } 102 } 103 } 104