Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(135)

Side by Side Diff: base/test/android/javatests/src/org/chromium/base/test/TestListInstrumentationRunListener.java

Issue 2933623002: Create AwJUnit4ClassRunner AwActivityTestRule and convert AwContentsTest (Closed)
Patch Set: address bo's comments Created 3 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package org.chromium.base.test; 5 package org.chromium.base.test;
6 6
7 import android.support.test.internal.runner.listener.InstrumentationRunListener; 7 import android.support.test.internal.runner.listener.InstrumentationRunListener;
8 8
9 import org.json.JSONArray; 9 import org.json.JSONArray;
10 import org.json.JSONObject; 10 import org.json.JSONObject;
11 import org.junit.runner.Description; 11 import org.junit.runner.Description;
12 import org.junit.runner.Result;
13 12
14 import org.chromium.base.Log; 13 import org.chromium.base.Log;
15 14
16 import java.io.File; 15 import java.io.File;
17 import java.io.FileOutputStream; 16 import java.io.FileOutputStream;
18 import java.io.IOException; 17 import java.io.IOException;
19 import java.io.OutputStreamWriter; 18 import java.io.OutputStreamWriter;
20 import java.io.Writer; 19 import java.io.Writer;
21 import java.lang.annotation.Annotation; 20 import java.lang.annotation.Annotation;
22 import java.lang.reflect.Method; 21 import java.lang.reflect.Method;
23 import java.util.Arrays; 22 import java.util.Arrays;
24 import java.util.Collection; 23 import java.util.Collection;
25 import java.util.HashMap; 24 import java.util.HashMap;
26 import java.util.HashSet; 25 import java.util.HashSet;
27 import java.util.Map; 26 import java.util.Map;
28 import java.util.Set; 27 import java.util.Set;
29 28
30 /** 29 /**
31 * A RunListener that list out all the test information into a json file. 30 * A RunListener that list out all the test information into a json file.
32 */ 31 */
33 public class TestListInstrumentationRunListener extends InstrumentationRunListen er { 32 public class TestListInstrumentationRunListener extends InstrumentationRunListen er {
34 private static final String TAG = "TestListRunListener"; 33 private static final String TAG = "TestListRunListener";
35 private static final Set<String> SKIP_METHODS = new HashSet<>( 34 private static final Set<String> SKIP_METHODS = new HashSet<>(
36 Arrays.asList(new String[] {"toString", "hashCode", "annotationType" , "equals"})); 35 Arrays.asList(new String[] {"toString", "hashCode", "annotationType" , "equals"}));
37 36
38 private final Map<Class<?>, JSONObject> mTestClassJsonMap = new HashMap<>(); 37 private final Map<Class<?>, JSONObject> mTestClassJsonMap = new HashMap<>();
39 private final String mOutputPath;
40
41 public TestListInstrumentationRunListener(String outputPath) {
42 super();
43 mOutputPath = outputPath;
44 }
45 38
46 /** 39 /**
47 * Store the test method description to a Map at the beginning of a test run . 40 * Store the test method description to a Map at the beginning of a test run .
48 */ 41 */
49 @Override 42 @Override
50 public void testStarted(Description desc) throws Exception { 43 public void testStarted(Description desc) throws Exception {
51 if (mTestClassJsonMap.containsKey(desc.getTestClass())) { 44 if (mTestClassJsonMap.containsKey(desc.getTestClass())) {
52 ((JSONArray) mTestClassJsonMap.get(desc.getTestClass()).get("methods ")) 45 ((JSONArray) mTestClassJsonMap.get(desc.getTestClass()).get("methods "))
53 .put(getTestMethodJSON(desc)); 46 .put(getTestMethodJSON(desc));
54 } else { 47 } else {
55 Class<?> testClass = desc.getTestClass(); 48 Class<?> testClass = desc.getTestClass();
56 mTestClassJsonMap.put(desc.getTestClass(), new JSONObject() 49 mTestClassJsonMap.put(desc.getTestClass(), new JSONObject()
57 .put("class", testClass.getName()) 50 .put("class", testClass.getName())
58 .put("superclass", testClass.getSuperclass().getName()) 51 .put("superclass", testClass.getSuperclass().getName())
59 .put("annotations", 52 .put("annotations",
60 getAnnotationJSON(Arrays.asList(testClass.getAnnotat ions()))) 53 getAnnotationJSON(Arrays.asList(testClass.getAnnotat ions())))
61 .put("methods", new JSONArray().put(getTestMethodJSON(desc)) )); 54 .put("methods", new JSONArray().put(getTestMethodJSON(desc)) ));
62 } 55 }
63 } 56 }
64 57
65 /** 58 /**
66 * Create a JSONArray with all the test class JSONObjects and save it to lis ted output path. 59 * Create a JSONArray with all the test class JSONObjects and save it to lis ted output path.
67 */ 60 */
68 @Override 61 public void saveTestsToJson(String outputPath) throws IOException {
69 public void testRunFinished(Result result) throws IOException {
70 Writer writer = null; 62 Writer writer = null;
71 File file = new File(mOutputPath); 63 File file = new File(outputPath);
72 try { 64 try {
73 writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8") ; 65 writer = new OutputStreamWriter(new FileOutputStream(file), "UTF-8") ;
74 JSONArray allTestClassesJSON = new JSONArray(mTestClassJsonMap.value s()); 66 JSONArray allTestClassesJSON = new JSONArray(mTestClassJsonMap.value s());
75 writer.write(allTestClassesJSON.toString()); 67 writer.write(allTestClassesJSON.toString());
76 } catch (IOException e) { 68 } catch (IOException e) {
77 Log.e(TAG, "failed to write json to file", e); 69 Log.e(TAG, "failed to write json to file", e);
78 throw e; 70 throw e;
79 } finally { 71 } finally {
80 if (writer != null) { 72 if (writer != null) {
81 try { 73 try {
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
141 : value.toString()); 133 : value.toString());
142 } 134 }
143 } catch (IllegalArgumentException e) { 135 } catch (IllegalArgumentException e) {
144 } 136 }
145 } 137 }
146 annotationsJsons.put(a.annotationType().getSimpleName(), elementJson Object); 138 annotationsJsons.put(a.annotationType().getSimpleName(), elementJson Object);
147 } 139 }
148 return annotationsJsons; 140 return annotationsJsons;
149 } 141 }
150 } 142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698