Index: testing/android/java/src/org/chromium/testing/local/GtestFilter.java |
diff --git a/testing/android/java/src/org/chromium/testing/local/GtestFilter.java b/testing/android/java/src/org/chromium/testing/local/GtestFilter.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2fac9cfcd23ca322d58e028d38c59ab7cc30b878 |
--- /dev/null |
+++ b/testing/android/java/src/org/chromium/testing/local/GtestFilter.java |
@@ -0,0 +1,95 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+package org.chromium.testing.local; |
+ |
+import org.junit.runner.Description; |
+import org.junit.runner.manipulation.Filter; |
+ |
+import java.util.HashSet; |
+import java.util.Set; |
+import java.util.regex.Pattern; |
+ |
+/** |
+ * Filters tests based on a googletest-style filter string. |
+ */ |
+class GtestFilter extends Filter { |
+ |
+ private final String mFilterString; |
+ |
+ private Set<String> mPositiveRegexes; |
+ private Set<String> mNegativeRegexes; |
+ |
+ /** |
+ * Creates the filter and converts the provided googletest-style filter |
+ * string into positive and negative regexes. |
+ */ |
+ public GtestFilter(String filterString) { |
+ mFilterString = filterString; |
+ mPositiveRegexes = new HashSet<String>(); |
+ mNegativeRegexes = new HashSet<String>(); |
+ |
+ String[] filterStrings = filterString.split(":"); |
+ for (String f : filterStrings) { |
+ String sanitized = f.replaceAll("\\.", "\\\\."); |
+ sanitized = sanitized.replaceAll("\\*", ".*"); |
+ int negIndex = sanitized.indexOf('-'); |
+ if (negIndex == 0) { |
+ mNegativeRegexes.add(sanitized.substring(1)); |
+ } else if (negIndex != -1) { |
+ String[] c = sanitized.split("-", 1); |
+ mPositiveRegexes.add(c[0]); |
+ mNegativeRegexes.add(c[1]); |
+ } else { |
+ mPositiveRegexes.add(sanitized); |
+ } |
+ } |
+ } |
+ |
+ /** |
+ * Determines whether or not a test with the provided description should |
+ * run based on the configured positive and negative regexes. |
+ * |
+ * A test should run if: |
+ * - it's just a class, OR |
+ * - it doesn't match any of the negative regexes, AND |
+ * - either: |
+ * - there are no configured positive regexes, OR |
+ * - it matches at least one of the positive regexes. |
+ */ |
+ public boolean shouldRun(Description description) { |
+ if (description.getMethodName() == null) { |
+ return true; |
+ } |
+ |
+ String gtestName = |
+ description.getClassName() + "." + description.getMethodName(); |
+ for (String p : mNegativeRegexes) { |
+ if (Pattern.matches(p, gtestName)) { |
+ return false; |
+ } |
+ } |
+ |
+ if (mPositiveRegexes.isEmpty()) { |
+ return true; |
+ } |
+ |
+ for (String p : mPositiveRegexes) { |
+ if (Pattern.matches(p, gtestName)) { |
+ return true; |
+ } |
+ } |
+ |
+ return false; |
+ } |
+ |
+ /** |
+ * Returns a description of this filter. |
+ */ |
+ public String describe() { |
+ return new String("gtest-filter: " + mFilterString); |
+ } |
+ |
+} |
+ |