Commit 6b4025d7 authored by Ted Choc's avatar Ted Choc Committed by Commit Bot

Add support for a Criteria that uses Hamcrest matchers.

BUG=1064397

Change-Id: I66193f5a558ed44850dea33c2b7d7c6ba48a46dc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2118580Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Commit-Queue: Ted Choc <tedchoc@chromium.org>
Cr-Commit-Position: refs/heads/master@{#753214}
parent f14e31f8
...@@ -20,6 +20,7 @@ android_library("content_java_test_support") { ...@@ -20,6 +20,7 @@ android_library("content_java_test_support") {
"//base:base_java_test_support", "//base:base_java_test_support",
"//content/public/android:content_java", "//content/public/android:content_java",
"//third_party/android_support_test_runner:runner_java", "//third_party/android_support_test_runner:runner_java",
"//third_party/hamcrest:hamcrest_java",
"//third_party/junit:junit", "//third_party/junit:junit",
"//ui/android:ui_java", "//ui/android:ui_java",
"//ui/android:ui_java_test_support", "//ui/android:ui_java_test_support",
...@@ -41,10 +42,10 @@ android_library("content_java_test_support") { ...@@ -41,10 +42,10 @@ android_library("content_java_test_support") {
"javatests/src/org/chromium/content_public/browser/test/util/CriteriaHelper.java", "javatests/src/org/chromium/content_public/browser/test/util/CriteriaHelper.java",
"javatests/src/org/chromium/content_public/browser/test/util/DOMUtils.java", "javatests/src/org/chromium/content_public/browser/test/util/DOMUtils.java",
"javatests/src/org/chromium/content_public/browser/test/util/DomAutomationController.java", "javatests/src/org/chromium/content_public/browser/test/util/DomAutomationController.java",
"javatests/src/org/chromium/content_public/browser/test/util/EqualityCriteria.java",
"javatests/src/org/chromium/content_public/browser/test/util/HistoryUtils.java", "javatests/src/org/chromium/content_public/browser/test/util/HistoryUtils.java",
"javatests/src/org/chromium/content_public/browser/test/util/JavaScriptUtils.java", "javatests/src/org/chromium/content_public/browser/test/util/JavaScriptUtils.java",
"javatests/src/org/chromium/content_public/browser/test/util/KeyUtils.java", "javatests/src/org/chromium/content_public/browser/test/util/KeyUtils.java",
"javatests/src/org/chromium/content_public/browser/test/util/MatcherCriteria.java",
"javatests/src/org/chromium/content_public/browser/test/util/RenderProcessLimit.java", "javatests/src/org/chromium/content_public/browser/test/util/RenderProcessLimit.java",
"javatests/src/org/chromium/content_public/browser/test/util/TestCallbackHelperContainer.java", "javatests/src/org/chromium/content_public/browser/test/util/TestCallbackHelperContainer.java",
"javatests/src/org/chromium/content_public/browser/test/util/TestInputMethodManagerWrapper.java", "javatests/src/org/chromium/content_public/browser/test/util/TestInputMethodManagerWrapper.java",
......
...@@ -4,6 +4,9 @@ ...@@ -4,6 +4,9 @@
package org.chromium.content_public.browser.test.util; package org.chromium.content_public.browser.test.util;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
/** /**
...@@ -78,6 +81,21 @@ public abstract class Criteria { ...@@ -78,6 +81,21 @@ public abstract class Criteria {
* @return A Criteria that will check the equality of the passed in data. * @return A Criteria that will check the equality of the passed in data.
*/ */
public static <T> Criteria equals(T expectedValue, Callable<T> actualValueCallable) { public static <T> Criteria equals(T expectedValue, Callable<T> actualValueCallable) {
return new EqualityCriteria<T>(expectedValue, actualValueCallable); return new MatcherCriteria<>(actualValueCallable, Matchers.equalTo(expectedValue));
}
/**
* Constructs a Criteria that determines if the actual value matches the specified matching
* criteria.
*
* @param <T> The type of value whose equality will be tested.
* @param actualValueCallable A {@link Callable} that provides a way of getting the current
* actual value.
* @param matcher Determines if the current value matches the desired expectation.
* @return A Criteria that will determine if the current value matches the expected criteria.
*/
public static <T> Criteria checkThat(
Callable<T> actualValueCallable, Matcher<? super T> matcher) {
return new MatcherCriteria<>(actualValueCallable, matcher);
} }
} }
// Copyright 2016 The Chromium Authors. All rights reserved. // Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
package org.chromium.content_public.browser.test.util; package org.chromium.content_public.browser.test.util;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
/** /**
* Extension of the Criteria that handles object equality while providing a standard error message. * Criteria that supports the Matcher assertion patterns.
* *
* @param <T> The type of value whose equality will be tested. * @param <T> The type of value being tested.
*/ */
class EqualityCriteria<T> extends Criteria { public class MatcherCriteria<T> extends Criteria {
private final T mExpectedValue; private final Matcher<? super T> mMatcher;
private final Callable<T> mActualValueCallable; private final Callable<T> mActualValueCallable;
/** /**
* Construct the EqualityCriteria with the given expected value. * Construct the MatcherCriteria with a specific matcher and a means of fetching the current
* @param expectedValue The value that is expected to determine the success of the criteria. * actual value.
* @param actualValueCallable Provides access to the current value.
* @param matcher Determines if the current value matches the desired expectation.
*/ */
public EqualityCriteria(T expectedValue, Callable<T> actualValueCallable) { public MatcherCriteria(Callable<T> actualValueCallable, Matcher<? super T> matcher) {
mExpectedValue = expectedValue;
mActualValueCallable = actualValueCallable; mActualValueCallable = actualValueCallable;
mMatcher = matcher;
} }
@Override @Override
...@@ -35,11 +41,15 @@ class EqualityCriteria<T> extends Criteria { ...@@ -35,11 +41,15 @@ class EqualityCriteria<T> extends Criteria {
return false; return false;
} }
updateFailureReason( if (mMatcher.matches(actualValue)) return true;
"Values did not match. Expected: " + mExpectedValue + ", actual: " + actualValue);
if (mExpectedValue == null) { Description description = new StringDescription();
return actualValue == null; description.appendText("Expected: ")
} .appendDescriptionOf(mMatcher)
return mExpectedValue.equals(actualValue); .appendText(System.lineSeparator())
.appendText(" but: ");
mMatcher.describeMismatch(actualValue, description);
updateFailureReason(description.toString());
return false;
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment