Commit 22c2cd98 authored by Michael Thiessen's avatar Michael Thiessen Committed by Commit Bot

Move CriteriaHelper to base/

This will be a 3-sided multi-part CL.
In this change I move CriteriaHelper to base/, leaving a copy in
content/
I will then migrate the downstream and upstream content/ imports to
base/ imports.
After downstream has rolled I'll remove the content/ copies.

Bug: 1134178
Change-Id: I07c646a1cc9aebfcd2c2a7cd3e0446f0581f6e82
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2518120Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Commit-Queue: Michael Thiessen <mthiesse@chromium.org>
Cr-Commit-Position: refs/heads/master@{#823669}
parent 7b3ca597
......@@ -3874,6 +3874,8 @@ if (is_android) {
"test/android/javatests/src/org/chromium/base/test/util/CallbackHelper.java",
"test/android/javatests/src/org/chromium/base/test/util/CloseableOnMainThread.java",
"test/android/javatests/src/org/chromium/base/test/util/CommandLineFlags.java",
"test/android/javatests/src/org/chromium/base/test/util/Criteria.java",
"test/android/javatests/src/org/chromium/base/test/util/CriteriaHelper.java",
"test/android/javatests/src/org/chromium/base/test/util/CriteriaNotSatisfiedException.java",
"test/android/javatests/src/org/chromium/base/test/util/DisableIf.java",
"test/android/javatests/src/org/chromium/base/test/util/DisableIfSkipCheck.java",
......@@ -3886,6 +3888,7 @@ if (is_android) {
"test/android/javatests/src/org/chromium/base/test/util/InstrumentationUtils.java",
"test/android/javatests/src/org/chromium/base/test/util/IntegrationTest.java",
"test/android/javatests/src/org/chromium/base/test/util/JniMocker.java",
"test/android/javatests/src/org/chromium/base/test/util/LooperUtils.java",
"test/android/javatests/src/org/chromium/base/test/util/Manual.java",
"test/android/javatests/src/org/chromium/base/test/util/Matchers.java",
"test/android/javatests/src/org/chromium/base/test/util/MetricsUtils.java",
......
// Copyright 2012 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.base.test.util;
import android.text.TextUtils;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
/**
* Provides a means for validating whether some condition/criteria has been met.
* <p>
* See {@link CriteriaHelper} for usage guidelines.
*/
public final class Criteria {
private Criteria() {}
/**
* Validates that a expected condition has been met, and throws an
* {@link CriteriaNotSatisfiedException} if not.
*
* @param <T> The type of value whose being tested.
* @param actual The actual value being tested.
* @param matcher Determines if the current value matches the desired expectation.
*/
public static <T> void checkThat(T actual, Matcher<T> matcher) {
checkThat("", actual, matcher);
}
/**
* Validates that a expected condition has been met, and throws an
* {@link CriteriaNotSatisfiedException} if not.
*
* @param <T> The type of value whose being tested.
* @param reason Additional reason description for the failure.
* @param actual The actual value being tested.
* @param matcher Determines if the current value matches the desired expectation.
*/
public static <T> void checkThat(String reason, T actual, Matcher<T> matcher) {
if (matcher.matches(actual)) return;
Description description = new StringDescription();
if (!TextUtils.isEmpty(reason)) {
description.appendText(reason).appendText(System.lineSeparator());
}
description.appendText("Expected: ")
.appendDescriptionOf(matcher)
.appendText(System.lineSeparator())
.appendText(" but: ");
matcher.describeMismatch(actual, description);
throw new CriteriaNotSatisfiedException(description.toString());
}
}
// Copyright 2020 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.base.test.util;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Test utilities for interacting with the Android Looper.
*/
public class LooperUtils {
private static final Method sNextMethod = getMethod(MessageQueue.class, "next");
private static final Field sMessageTargetField = getField(Message.class, "target");
private static final Field sMessageFlagsField = getField(Message.class, "flags");
private static Field getField(Class<?> clazz, String name) {
Field f = null;
try {
f = clazz.getDeclaredField(name);
f.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
return f;
}
private static Method getMethod(Class<?> clazz, String name) {
Method m = null;
try {
m = clazz.getDeclaredMethod(name);
m.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
return m;
}
/**
* Runs a single nested task on the current Looper.
*/
public static void runSingleNestedLooperTask() throws IllegalArgumentException,
IllegalAccessException, SecurityException,
InvocationTargetException {
MessageQueue queue = Looper.myQueue();
// This call will block if there are no messages in the queue. It will
// also run or more pending C++ tasks as a side effect before returning
// |msg|.
Message msg = (Message) sNextMethod.invoke(queue);
if (msg == null) return;
Handler target = (Handler) sMessageTargetField.get(msg);
if (target != null) target.dispatchMessage(msg);
// Unset in-use flag.
Integer oldFlags = (Integer) sMessageFlagsField.get(msg);
sMessageFlagsField.set(msg, oldFlags & ~(1 << 0 /* FLAG_IN_USE */));
msg.recycle();
}
}
......@@ -8,7 +8,10 @@ generate_jni("test_support_content_jni_headers") {
android_library("android_test_message_pump_support_java") {
testonly = true
deps = [ "//base:base_java" ]
deps = [
"//base:base_java",
"//base:base_java_test_support",
]
sources = [ "javatests/src/org/chromium/content_public/browser/test/NestedSystemMessageHandler.java" ]
}
......
......@@ -5,16 +5,12 @@
package org.chromium.content_public.browser.test;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.test.util.LooperUtils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Handles processing messages in nested run loops.
......@@ -27,58 +23,9 @@ import java.lang.reflect.Method;
public class NestedSystemMessageHandler {
private static final int QUIT_MESSAGE = 10;
private static final Handler sHandler = new Handler();
private static final Method sNextMethod = getMethod(MessageQueue.class, "next");
private static final Field sMessageTargetField = getField(Message.class, "target");
private static final Field sMessageFlagsField = getField(Message.class, "flags");
private static Field getField(Class<?> clazz, String name) {
Field f = null;
try {
f = clazz.getDeclaredField(name);
f.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
return f;
}
private static Method getMethod(Class<?> clazz, String name) {
Method m = null;
try {
m = clazz.getDeclaredMethod(name);
m.setAccessible(true);
} catch (Exception e) {
e.printStackTrace();
}
return m;
}
private NestedSystemMessageHandler() {}
/**
* Runs a single nested task on the provided MessageQueue
*/
public static void runSingleNestedLooperTask(MessageQueue queue)
throws IllegalArgumentException, IllegalAccessException, SecurityException,
InvocationTargetException {
// This call will block if there are no messages in the queue. It will
// also run or more pending C++ tasks as a side effect before returning
// |msg|.
Message msg = (Message) sNextMethod.invoke(queue);
if (msg == null) return;
Handler target = (Handler) sMessageTargetField.get(msg);
if (target != null) {
target.dispatchMessage(msg);
}
// Unset in-use flag.
Integer oldFlags = (Integer) sMessageFlagsField.get(msg);
sMessageFlagsField.set(msg, oldFlags & ~(1 << 0 /* FLAG_IN_USE */));
msg.recycle();
}
/**
* Dispatches the first message from the current MessageQueue, blocking
* until a task becomes available if the queue is empty. Callbacks for
......@@ -91,9 +38,8 @@ public class NestedSystemMessageHandler {
@SuppressWarnings("unused")
@CalledByNative
private static boolean dispatchOneMessage() {
MessageQueue queue = Looper.myQueue();
try {
runSingleNestedLooperTask(queue);
LooperUtils.runSingleNestedLooperTask();
} catch (IllegalArgumentException | IllegalAccessException | SecurityException
| InvocationTargetException e) {
e.printStackTrace();
......
......@@ -4,13 +4,7 @@
package org.chromium.content_public.browser.test.util;
import android.text.TextUtils;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;
import org.chromium.base.test.util.CriteriaNotSatisfiedException;
/**
* Provides a means for validating whether some condition/criteria has been met.
......@@ -29,7 +23,7 @@ public final class Criteria {
* @param matcher Determines if the current value matches the desired expectation.
*/
public static <T> void checkThat(T actual, Matcher<T> matcher) {
checkThat("", actual, matcher);
org.chromium.base.test.util.Criteria.checkThat(actual, matcher);
}
/**
......@@ -42,16 +36,6 @@ public final class Criteria {
* @param matcher Determines if the current value matches the desired expectation.
*/
public static <T> void checkThat(String reason, T actual, Matcher<T> matcher) {
if (matcher.matches(actual)) return;
Description description = new StringDescription();
if (!TextUtils.isEmpty(reason)) {
description.appendText(reason).appendText(System.lineSeparator());
}
description.appendText("Expected: ")
.appendDescriptionOf(matcher)
.appendText(System.lineSeparator())
.appendText(" but: ");
matcher.describeMismatch(actual, description);
throw new CriteriaNotSatisfiedException(description.toString());
org.chromium.base.test.util.Criteria.checkThat(reason, actual, matcher);
}
}
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