1 package autotest.common; 2 3 4 public abstract class AbstractStatusSummary { 5 public static final String BLANK_COLOR = "status_blank"; 6 private static final ColorMapping[] CELL_COLOR_MAP = { 7 // must be in descending order of percentage 8 new ColorMapping(95, "status_95"), 9 new ColorMapping(90, "status_90"), 10 new ColorMapping(85, "status_85"), 11 new ColorMapping(75, "status_75"), 12 new ColorMapping(1, "status_bad"), 13 new ColorMapping(0, "status_none"), 14 }; 15 16 /** 17 * Stores a CSS class for pass rates and the minimum passing percentage required 18 * to have that class. 19 */ 20 private static class ColorMapping { 21 // store percentage as int so we can reprint it consistently 22 public int minPercent; 23 public String cssClass; 24 ColorMapping(int minPercent, String cssClass)25 public ColorMapping(int minPercent, String cssClass) { 26 this.minPercent = minPercent; 27 this.cssClass = cssClass; 28 } 29 matches(double ratio)30 public boolean matches(double ratio) { 31 return ratio * 100 >= minPercent; 32 } 33 } 34 formatStatusCounts()35 public String formatStatusCounts() { 36 String text = getPassed() + " / " + getComplete(); 37 if (getIncomplete() > 0) { 38 text += " (" + getIncomplete() + " incomplete)"; 39 } 40 return text; 41 } 42 getCssClass()43 public String getCssClass() { 44 if (getComplete() == 0) { 45 return BLANK_COLOR; 46 } 47 double ratio = (double) getPassed() / getComplete(); 48 for (ColorMapping mapping : CELL_COLOR_MAP) { 49 if (mapping.matches(ratio)) 50 return mapping.cssClass; 51 } 52 throw new RuntimeException("No color map match for ratio " + ratio); 53 } 54 getPassed()55 protected abstract int getPassed(); getComplete()56 protected abstract int getComplete(); getIncomplete()57 protected abstract int getIncomplete(); 58 } 59