1 package org.robolectric.res; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 import java.util.TreeMap; 8 import java.util.TreeSet; 9 10 public class ResourceTestUtil { time(String message, Runnable runnable)11 void time(String message, Runnable runnable) { 12 long startTime = System.nanoTime(); 13 for (int i = 0; i < 10; i++) { 14 runnable.run(); 15 } 16 long elapsed = System.nanoTime() - startTime; 17 System.out.println("elapsed " + message + ": " + (elapsed / 1000000.0) + "ms"); 18 } 19 stringify(ResourceTable resourceTable)20 static String stringify(ResourceTable resourceTable) { 21 final HashMap<String, List<TypedResource>> map = new HashMap<>(); 22 resourceTable.receive(new ResourceTable.Visitor() { 23 @Override 24 public void visit(ResName key, Iterable<TypedResource> values) { 25 List<TypedResource> v = new ArrayList<>(); 26 for (TypedResource value : values) { 27 v.add(value); 28 } 29 map.put(key.getFullyQualifiedName(), v); 30 } 31 }); 32 StringBuilder buf = new StringBuilder(); 33 TreeSet<String> keys = new TreeSet<>(map.keySet()); 34 for (String key : keys) { 35 buf.append(key).append(":\n"); 36 for (TypedResource typedResource : map.get(key)) { 37 Object data = typedResource.getData(); 38 if (data instanceof List) { 39 ArrayList<String> newList = new ArrayList<>(); 40 for (Object item : ((List) data)) { 41 if (item.getClass().equals(TypedResource.class)) { 42 TypedResource typedResourceItem = (TypedResource) item; 43 newList.add(typedResourceItem.getData().toString() + " (" + typedResourceItem.getResType() + ")"); 44 } else { 45 newList.add(item.toString()); 46 } 47 } 48 data = newList.toString(); 49 } else if (data instanceof StyleData) { 50 StyleData styleData = (StyleData) data; 51 final Map<String, String> attrs = new TreeMap<>(); 52 styleData.visit(new StyleData.Visitor() { 53 @Override 54 public void visit(AttributeResource attributeResource) { 55 attrs.put(attributeResource.resName.getFullyQualifiedName(), attributeResource.value); 56 } 57 }); 58 data = data.toString() + "^" + styleData.getParent() + " " + attrs; 59 } 60 buf.append(" ").append(data).append(" {").append(typedResource.getResType()) 61 .append("/").append(typedResource.getConfig()).append(": ") 62 .append(shortContext(typedResource)).append("}").append("\n"); 63 } 64 } 65 return buf.toString(); 66 } 67 shortContext(TypedResource typedResource)68 static String shortContext(TypedResource typedResource) { 69 return typedResource.getXmlContext().toString().replaceAll("jar:/usr/local/google/home/.*\\.jar\\!", "jar:"); 70 } 71 } 72