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
17package vogar;
18
19import com.google.common.collect.Lists;
20import com.google.common.collect.Ordering;
21import java.util.ArrayList;
22import java.util.List;
23import java.util.SortedMap;
24import java.util.TreeMap;
25
26/**
27 * Contains an outcome for a test, along with some metadata pertaining to the history of this test,
28 * including a list of previous outcomes, an outcome corresponding to the tag Vogar is being run
29 * with, if applicable, and the expectation for this test, so that result value information is
30 * available.
31 */
32public final class AnnotatedOutcome {
33    public static Ordering<AnnotatedOutcome> ORDER_BY_NAME = new Ordering<AnnotatedOutcome>() {
34        @Override public int compare(AnnotatedOutcome a, AnnotatedOutcome b) {
35            return a.getName().compareTo(b.getName());
36       }
37    };
38
39    private final Expectation expectation;
40    private final Outcome outcome;
41    /** a list of previous outcomes for the same action, sorted in chronological order */
42    private final SortedMap<Long, Outcome> previousOutcomes = new TreeMap<Long, Outcome>();
43
44    AnnotatedOutcome(Outcome outcome, Expectation expectation) {
45        this.expectation = expectation;
46        this.outcome = outcome;
47    }
48
49    public void add(long date, Outcome outcome) {
50        previousOutcomes.put(date, outcome);
51    }
52
53    public Outcome getOutcome() {
54        return outcome;
55    }
56
57    public String getName() {
58        return outcome.getName();
59    }
60
61    public ResultValue getResultValue() {
62        return outcome.getResultValue(expectation);
63    }
64
65    public List<ResultValue> getPreviousResultValues() {
66        List<ResultValue> previousResultValues = new ArrayList<ResultValue>();
67        for (Outcome previousOutcome : previousOutcomes.values()) {
68            previousResultValues.add(previousOutcome.getResultValue(expectation));
69        }
70        return previousResultValues;
71    }
72
73    /**
74     * Returns the most recent result value of a run of this test (before the current run).
75     */
76    public ResultValue getMostRecentResultValue(ResultValue defaultValue) {
77        List<ResultValue> previousResultValues = getPreviousResultValues();
78        return previousResultValues.isEmpty() ?
79                defaultValue :
80                previousResultValues.get(previousResultValues.size() - 1);
81    }
82
83    /**
84     * Returns true if the outcome is noteworthy given the result value and previous history.
85     */
86    public boolean isNoteworthy() {
87        return getResultValue() != ResultValue.OK || recentlyChanged();
88    }
89
90    /**
91     * Returns true if the outcome recently changed in result value.
92     */
93    private boolean recentlyChanged() {
94        List<ResultValue> previousResultValues = getPreviousResultValues();
95        if (previousResultValues.isEmpty()) {
96            return false;
97        }
98        return previousResultValues.get(previousResultValues.size() - 1) != getResultValue();
99    }
100
101    /**
102     * Returns a Long representing the time the outcome was last run. Returns {@code defaultValue}
103     * if the outcome is not known to have run before.
104     */
105    public Long lastRun(Long defaultValue) {
106        List<Long> runTimes = Lists.newArrayList(previousOutcomes.keySet());
107        return runTimes.isEmpty() ? defaultValue : runTimes.get(runTimes.size() - 1);
108    }
109}
110