0

Android: Convert more legacy JNI calls to Proxy Natives

Hopefully this is the last batch

Bug: 1407278
Change-Id: Ic82a056d8bb6631ffdd39426d63ad92bbe05abeb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4174393
Reviewed-by: Sam Maier <smaier@chromium.org>
Commit-Queue: Andrew Grieve <agrieve@chromium.org>
Owners-Override: Andrew Grieve <agrieve@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1093528}
This commit is contained in:
Andrew Grieve
2023-01-17 21:13:31 +00:00
committed by Chromium LUCI CQ
parent 896f391d74
commit ec09b3a7e8
39 changed files with 361 additions and 321 deletions
chrome
android
javatests
browser
test
BUILD.gn
android
test_support
src
org
chromium
components/heap_profiling/multi_process
BUILD.gnheap_profiling_test_shim.ccheap_profiling_test_shim.h
javatests
src
org
chromium
components
heap_profiling
content/public/test/android
device
BUILD.gn
bluetooth
test
android
java
src
org
chromium
device
bluetooth
mojo/public/java/system
net
testing/android/native_test
url

@ -10,6 +10,7 @@ import org.junit.Assert;
import org.chromium.base.Callback;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.task.PostTask;
import org.chromium.base.test.util.CallbackHelper;
import org.chromium.base.test.util.CriteriaHelper;
@ -31,7 +32,8 @@ import java.util.concurrent.atomic.AtomicReference;
public class OfflineTestUtil {
// Forces request coordinator to process the requests in the queue.
public static void startRequestCoordinatorProcessing() {
TestThreadUtils.runOnUiThreadBlocking(() -> nativeStartRequestCoordinatorProcessing());
TestThreadUtils.runOnUiThreadBlocking(
() -> OfflineTestUtilJni.get().startRequestCoordinatorProcessing());
}
// Gets all the URLs in the request queue.
@ -39,7 +41,7 @@ public class OfflineTestUtil {
final AtomicReference<SavePageRequest[]> result = new AtomicReference<>();
final CallbackHelper callbackHelper = new CallbackHelper();
TestThreadUtils.runOnUiThreadBlocking(() -> {
nativeGetRequestsInQueue((SavePageRequest[] requests) -> {
OfflineTestUtilJni.get().getRequestsInQueue((SavePageRequest[] requests) -> {
result.set(requests);
callbackHelper.notifyCalled();
});
@ -54,10 +56,11 @@ public class OfflineTestUtil {
new AtomicReference<List<OfflinePageItem>>();
final CallbackHelper callbackHelper = new CallbackHelper();
TestThreadUtils.runOnUiThreadBlocking(() -> {
nativeGetAllPages(new ArrayList<OfflinePageItem>(), (List<OfflinePageItem> items) -> {
result.set(items);
callbackHelper.notifyCalled();
});
OfflineTestUtilJni.get().getAllPages(
new ArrayList<OfflinePageItem>(), (List<OfflinePageItem> items) -> {
result.set(items);
callbackHelper.notifyCalled();
});
});
callbackHelper.waitForCallback(0);
return result.get();
@ -69,7 +72,7 @@ public class OfflineTestUtil {
final CallbackHelper callbackHelper = new CallbackHelper();
final AtomicReference<String> result = new AtomicReference<String>();
TestThreadUtils.runOnUiThreadBlocking(() -> {
nativeDumpRequestCoordinatorState((String dump) -> {
OfflineTestUtilJni.get().dumpRequestCoordinatorState((String dump) -> {
result.set(dump);
callbackHelper.notifyCalled();
});
@ -108,7 +111,7 @@ public class OfflineTestUtil {
final AtomicReference<byte[]> result = new AtomicReference<>();
final CallbackHelper callbackHelper = new CallbackHelper();
TestThreadUtils.runOnUiThreadBlocking(() -> {
nativeGetRawThumbnail(offlineId, (byte[] rawThumbnail) -> {
OfflineTestUtilJni.get().getRawThumbnail(offlineId, (byte[] rawThumbnail) -> {
result.set(rawThumbnail);
callbackHelper.notifyCalled();
});
@ -147,21 +150,24 @@ public class OfflineTestUtil {
public static void interceptWithOfflineError(String url) throws TimeoutException {
final CallbackHelper callbackHelper = new CallbackHelper();
TestThreadUtils.runOnUiThreadBlocking(() -> {
nativeInterceptWithOfflineError(url, () -> callbackHelper.notifyCalled());
OfflineTestUtilJni.get().interceptWithOfflineError(
url, () -> callbackHelper.notifyCalled());
});
callbackHelper.waitForCallback(0);
}
// Clears all previous intercepts installed by interceptWithOfflineError.
public static void clearIntercepts() {
TestThreadUtils.runOnUiThreadBlocking(() -> nativeClearIntercepts());
TestThreadUtils.runOnUiThreadBlocking(() -> OfflineTestUtilJni.get().clearIntercepts());
}
// Waits for the connectivity state to change in the native network change notifier.
public static void waitForConnectivityState(boolean connected) {
AtomicBoolean done = new AtomicBoolean();
TestThreadUtils.runOnUiThreadBlocking(
() -> nativeWaitForConnectivityState(connected, () -> done.set(true)));
()
-> OfflineTestUtilJni.get().waitForConnectivityState(
connected, () -> done.set(true)));
CriteriaHelper.pollInstrumentationThread(() -> done.get());
}
@ -169,22 +175,26 @@ public class OfflineTestUtil {
// also ensures that the server-enabled check is due.
public static void setPrefetchingEnabledByServer(boolean enabled) {
TestThreadUtils.runOnUiThreadBlocking(
() -> { nativeSetPrefetchingEnabledByServer(enabled); });
() -> { OfflineTestUtilJni.get().setPrefetchingEnabledByServer(enabled); });
}
public static void setGCMTokenForTesting(String gcmToken) {
TestThreadUtils.runOnUiThreadBlocking(() -> { nativeSetGCMTokenForTesting(gcmToken); });
TestThreadUtils.runOnUiThreadBlocking(
() -> { OfflineTestUtilJni.get().setGCMTokenForTesting(gcmToken); });
}
private static native void nativeGetRequestsInQueue(Callback<SavePageRequest[]> callback);
private static native void nativeGetAllPages(
List<OfflinePageItem> offlinePages, final Callback<List<OfflinePageItem>> callback);
private static native void nativeGetRawThumbnail(long offlineId, Callback<byte[]> callback);
private static native void nativeStartRequestCoordinatorProcessing();
private static native void nativeInterceptWithOfflineError(String url, Runnable readyRunnable);
private static native void nativeClearIntercepts();
private static native void nativeDumpRequestCoordinatorState(Callback<String> callback);
private static native void nativeWaitForConnectivityState(boolean connected, Runnable callback);
private static native void nativeSetPrefetchingEnabledByServer(boolean enabled);
private static native void nativeSetGCMTokenForTesting(String gcmToken);
@NativeMethods
interface Natives {
void getRequestsInQueue(Callback<SavePageRequest[]> callback);
void getAllPages(
List<OfflinePageItem> offlinePages, final Callback<List<OfflinePageItem>> callback);
void getRawThumbnail(long offlineId, Callback<byte[]> callback);
void startRequestCoordinatorProcessing();
void interceptWithOfflineError(String url, Runnable readyRunnable);
void clearIntercepts();
void dumpRequestCoordinatorState(Callback<String> callback);
void waitForConnectivityState(boolean connected, Runnable callback);
void setPrefetchingEnabledByServer(boolean enabled);
void setGCMTokenForTesting(String gcmToken);
}
}

@ -5,27 +5,32 @@
package org.chromium.chrome.browser.offlinepages.prefetch;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
/** Prefetch test Java to native bridge. */
@JNINamespace("offline_pages::prefetch")
public class PrefetchTestBridge {
public static void enableLimitlessPrefetching(boolean enabled) {
nativeEnableLimitlessPrefetching(enabled);
PrefetchTestBridgeJni.get().enableLimitlessPrefetching(enabled);
}
public static boolean isLimitlessPrefetchingEnabled() {
return nativeIsLimitlessPrefetchingEnabled();
return PrefetchTestBridgeJni.get().isLimitlessPrefetchingEnabled();
}
public static void insertIntoCachedImageFetcher(String url, byte[] imageData) {
nativeInsertIntoCachedImageFetcher(url, imageData);
PrefetchTestBridgeJni.get().insertIntoCachedImageFetcher(url, imageData);
}
public static void addCandidatePrefetchURL(String url, String title, String thumbnailUrl,
String faviconUrl, String snippet, String attribution) {
nativeAddCandidatePrefetchURL(url, title, thumbnailUrl, faviconUrl, snippet, attribution);
PrefetchTestBridgeJni.get().addCandidatePrefetchURL(
url, title, thumbnailUrl, faviconUrl, snippet, attribution);
}
static native void nativeEnableLimitlessPrefetching(boolean enabled);
static native boolean nativeIsLimitlessPrefetchingEnabled();
static native void nativeInsertIntoCachedImageFetcher(String url, byte[] imageData);
static native void nativeAddCandidatePrefetchURL(String url, String title, String thumbnailUrl,
String faviconUrl, String snippet, String attribution);
@NativeMethods
interface Natives {
void enableLimitlessPrefetching(boolean enabled);
boolean isLimitlessPrefetchingEnabled();
void insertIntoCachedImageFetcher(String url, byte[] imageData);
void addCandidatePrefetchURL(String url, String title, String thumbnailUrl,
String faviconUrl, String snippet, String attribution);
}
}

@ -5,6 +5,7 @@
package org.chromium.chrome.browser.query_tiles;
import org.chromium.base.Callback;
import org.chromium.base.annotations.NativeMethods;
/**
* Spins up a test server and starts serving query tiles in native.
@ -19,9 +20,13 @@ public class QueryTileFakeServer {
*/
public static void setupFakeServer(
int levels, int tilesPerLevel, Callback<Boolean> onFetchCompletedCallback) {
nativeSetupFakeServer(onFetchCompletedCallback, levels, tilesPerLevel);
QueryTileFakeServerJni.get().setupFakeServer(
onFetchCompletedCallback, levels, tilesPerLevel);
}
static native void nativeSetupFakeServer(
Callback<Boolean> onFetchCompletedCallback, int levels, int tilesPerLevel);
@NativeMethods
interface Natives {
void setupFakeServer(
Callback<Boolean> onFetchCompletedCallback, int levels, int tilesPerLevel);
}
}

@ -5,6 +5,7 @@
package org.chromium.chrome.browser.subresource_filter;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.NativeMethods;
/**
* Class which aids in publishing test rulesets for SubresourceFilter instrumentation tests.
@ -14,7 +15,8 @@ public final class TestRulesetPublisher {
private boolean mPublished;
public void createAndPublishRulesetDisallowingSuffixForTesting(String suffix) {
nativeCreateAndPublishRulesetDisallowingSuffixForTesting(suffix);
TestRulesetPublisherJni.get().createAndPublishRulesetDisallowingSuffixForTesting(
this, suffix);
}
public boolean isPublished() {
@ -26,5 +28,9 @@ public final class TestRulesetPublisher {
mPublished = true;
}
private native void nativeCreateAndPublishRulesetDisallowingSuffixForTesting(String suffix);
@NativeMethods
interface Natives {
void createAndPublishRulesetDisallowingSuffixForTesting(
TestRulesetPublisher obj, String suffix);
}
}

@ -25,29 +25,28 @@ public class MockCertVerifierRuleAndroid extends ExternalResource {
protected void before() {
NativeLibraryTestUtils.loadNativeLibraryNoBrowserProcess();
mNativePtr = MockCertVerifierRuleAndroidJni.get().init();
nativeSetResult(mNativePtr, mResult);
nativeSetUp(mNativePtr);
MockCertVerifierRuleAndroidJni.get().setResult(mNativePtr, mResult);
MockCertVerifierRuleAndroidJni.get().setUp(mNativePtr);
}
public void setResult(int result) {
mResult = result;
if (mNativePtr != 0) {
nativeSetResult(mNativePtr, result);
MockCertVerifierRuleAndroidJni.get().setResult(mNativePtr, result);
}
}
@Override
protected void after() {
nativeTearDown(mNativePtr);
MockCertVerifierRuleAndroidJni.get().tearDown(mNativePtr);
mNativePtr = 0;
}
private native void nativeSetUp(long nativeMockCertVerifierRuleAndroid);
private native void nativeSetResult(long nativeMockCertVerifierRuleAndroid, int result);
private native void nativeTearDown(long nativeMockCertVerifierRuleAndroid);
@NativeMethods
interface Natives {
long init();
void setUp(long nativeMockCertVerifierRuleAndroid);
void setResult(long nativeMockCertVerifierRuleAndroid, int result);
void tearDown(long nativeMockCertVerifierRuleAndroid);
}
}

@ -13,22 +13,15 @@ jlong JNI_MockCertVerifierRuleAndroid_Init(JNIEnv* env) {
MockCertVerifierRuleAndroid::MockCertVerifierRuleAndroid() = default;
void MockCertVerifierRuleAndroid::SetResult(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
int result) {
void MockCertVerifierRuleAndroid::SetResult(JNIEnv* env, int result) {
mock_cert_verifier_.mock_cert_verifier()->set_default_result(result);
}
void MockCertVerifierRuleAndroid::SetUp(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj) {
void MockCertVerifierRuleAndroid::SetUp(JNIEnv* env) {
mock_cert_verifier_.SetUpCommandLine(base::CommandLine::ForCurrentProcess());
mock_cert_verifier_.SetUpInProcessBrowserTestFixture();
}
void MockCertVerifierRuleAndroid::TearDown(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj) {
void MockCertVerifierRuleAndroid::TearDown(JNIEnv* env) {
mock_cert_verifier_.TearDownInProcessBrowserTestFixture();
}

@ -18,12 +18,9 @@ class MockCertVerifierRuleAndroid {
delete;
// Sets the certificate verification result to force.
void SetResult(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
int result);
void SetUp(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj);
void TearDown(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj);
void SetResult(JNIEnv* env, int result);
void SetUp(JNIEnv* env);
void TearDown(JNIEnv* env);
private:
content::ContentMockCertVerifier mock_cert_verifier_;

@ -13,6 +13,7 @@ if (is_android) {
android_library("subresource_filter_java_test_support") {
testonly = true
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
sources = [ "../../android/javatests/src/org/chromium/chrome/browser/subresource_filter/TestRulesetPublisher.java" ]
deps = [ "//base:jni_java" ]
}

@ -89,6 +89,7 @@ if (is_android) {
"android/test_support/src/org/chromium/chrome/test_support/PaymentRequestTestBridge.java",
"android/test_support/src/org/chromium/chrome/test_support/ToolbarManagerTestHelper.java",
]
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
}
generate_jni("test_support_jni_headers") {

@ -10,6 +10,7 @@ import androidx.annotation.Nullable;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.chrome.browser.payments.ChromePaymentRequestFactory;
import org.chromium.chrome.browser.payments.ChromePaymentRequestService;
import org.chromium.components.autofill.EditableOption;
@ -96,8 +97,8 @@ public class PaymentRequestTestBridge {
/**
* Implements NativeObserverForTest by holding pointers to C++ callbacks, and invoking
* them through nativeResolvePaymentRequestObserverCallback() when the observer's
* methods are called.
* them through PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback() when
* the observer's methods are called.
*/
private static class PaymentRequestNativeObserverBridgeToNativeForTest
implements NativeObserverForTest {
@ -152,19 +153,23 @@ public class PaymentRequestTestBridge {
@Override
public void onCanMakePaymentCalled() {
nativeResolvePaymentRequestObserverCallback(mOnCanMakePaymentCalledPtr);
PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback(
mOnCanMakePaymentCalledPtr);
}
@Override
public void onCanMakePaymentReturned() {
nativeResolvePaymentRequestObserverCallback(mOnCanMakePaymentReturnedPtr);
PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback(
mOnCanMakePaymentReturnedPtr);
}
@Override
public void onHasEnrolledInstrumentCalled() {
nativeResolvePaymentRequestObserverCallback(mOnHasEnrolledInstrumentCalledPtr);
PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback(
mOnHasEnrolledInstrumentCalledPtr);
}
@Override
public void onHasEnrolledInstrumentReturned() {
nativeResolvePaymentRequestObserverCallback(mOnHasEnrolledInstrumentReturnedPtr);
PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback(
mOnHasEnrolledInstrumentReturnedPtr);
}
@Override
@ -184,23 +189,28 @@ public class PaymentRequestTestBridge {
}
}
nativeSetAppDescriptions(mSetAppDescriptionsPtr, appLabels, appSublabels, appTotals);
nativeResolvePaymentRequestObserverCallback(mOnAppListReadyPtr);
PaymentRequestTestBridgeJni.get().setAppDescriptions(
mSetAppDescriptionsPtr, appLabels, appSublabels, appTotals);
PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback(
mOnAppListReadyPtr);
}
@Override
public void onShippingSectionVisibilityChange(boolean isShippingSectionVisible) {
nativeInvokeBooleanCallback(mSetShippingSectionVisiblePtr, isShippingSectionVisible);
PaymentRequestTestBridgeJni.get().invokeBooleanCallback(
mSetShippingSectionVisiblePtr, isShippingSectionVisible);
}
@Override
public void onContactSectionVisibilityChange(boolean isContactSectionVisible) {
nativeInvokeBooleanCallback(mSetContactSectionVisiblePtr, isContactSectionVisible);
PaymentRequestTestBridgeJni.get().invokeBooleanCallback(
mSetContactSectionVisiblePtr, isContactSectionVisible);
}
@Override
public void onErrorDisplayed() {
nativeResolvePaymentRequestObserverCallback(mOnErrorDisplayedPtr);
PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback(
mOnErrorDisplayedPtr);
}
private static String ensureNotNull(@Nullable String value) {
@ -209,23 +219,27 @@ public class PaymentRequestTestBridge {
@Override
public void onNotSupportedError() {
nativeResolvePaymentRequestObserverCallback(mOnNotSupportedErrorPtr);
PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback(
mOnNotSupportedErrorPtr);
}
@Override
public void onConnectionTerminated() {
nativeResolvePaymentRequestObserverCallback(mOnConnectionTerminatedPtr);
PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback(
mOnConnectionTerminatedPtr);
}
@Override
public void onAbortCalled() {
nativeResolvePaymentRequestObserverCallback(mOnAbortCalledPtr);
PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback(
mOnAbortCalledPtr);
}
@Override
public void onCompleteHandled() {
nativeResolvePaymentRequestObserverCallback(mOnCompleteHandledPtr);
PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback(
mOnCompleteHandledPtr);
}
@Override
public void onUiDisplayed() {
nativeResolvePaymentRequestObserverCallback(mOnUiDisplayed);
PaymentRequestTestBridgeJni.get().resolvePaymentRequestObserverCallback(mOnUiDisplayed);
}
}
@ -297,14 +311,16 @@ public class PaymentRequestTestBridge {
return false;
}
/**
* The native method responsible to executing RepeatingClosure pointers.
*/
private static native void nativeResolvePaymentRequestObserverCallback(long callbackPtr);
@NativeMethods
interface Natives {
/**
* The native method responsible to executing RepeatingClosure pointers.
*/
void resolvePaymentRequestObserverCallback(long callbackPtr);
private static native void nativeSetAppDescriptions(
long callbackPtr, String[] appLabels, String[] appSublabels, String[] appTotals);
/** The native method responsible for executing RepatingCallback<void(bool)> pointers. */
private static native void nativeInvokeBooleanCallback(long callbackPtr, boolean value);
void setAppDescriptions(
long callbackPtr, String[] appLabels, String[] appSublabels, String[] appTotals);
/** The native method responsible for executing RepatingCallback<void(bool)> pointers. */
void invokeBooleanCallback(long callbackPtr, boolean value);
}
}

@ -33,8 +33,12 @@ if (is_android) {
# shim to function correctly.
android_library("heap_profiling_java_test_support") {
testonly = true
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
sources = [ "javatests/src/org/chromium/components/heap_profiling/multi_process/HeapProfilingTestShim.java" ]
deps = [ "//build/android:build_java" ]
deps = [
"//base:jni_java",
"//build/android:build_java",
]
}
}

@ -21,14 +21,12 @@ static jlong JNI_HeapProfilingTestShim_Init(JNIEnv* env,
HeapProfilingTestShim::HeapProfilingTestShim(JNIEnv* env, jobject obj) {}
HeapProfilingTestShim::~HeapProfilingTestShim() = default;
void HeapProfilingTestShim::Destroy(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
void HeapProfilingTestShim::Destroy(JNIEnv* env) {
delete this;
}
jboolean HeapProfilingTestShim::RunTestForMode(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& mode,
jboolean dynamically_start_profiling,
const base::android::JavaParamRef<jstring>& stack_mode,

@ -14,14 +14,13 @@
class HeapProfilingTestShim {
public:
HeapProfilingTestShim(JNIEnv* env, jobject obj);
void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj);
void Destroy(JNIEnv* env);
HeapProfilingTestShim(const HeapProfilingTestShim&) = delete;
HeapProfilingTestShim& operator=(const HeapProfilingTestShim&) = delete;
jboolean RunTestForMode(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& mode,
jboolean dynamically_start_profiling,
const base::android::JavaParamRef<jstring>& stack_mode,

@ -4,6 +4,7 @@
package org.chromium.components.heap_profiling.multi_process;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.build.annotations.MainDex;
/**
@ -13,7 +14,7 @@ import org.chromium.build.annotations.MainDex;
@MainDex
public class HeapProfilingTestShim {
public HeapProfilingTestShim() {
mNativeHeapProfilingTestShim = nativeInit();
mNativeHeapProfilingTestShim = HeapProfilingTestShimJni.get().init(this);
}
/**
@ -24,8 +25,8 @@ public class HeapProfilingTestShim {
*/
public boolean runTestForMode(String mode, boolean dynamicallyStartProfiling, String stackMode,
boolean shouldSample, boolean sampleEverything) {
return nativeRunTestForMode(mNativeHeapProfilingTestShim, mode, dynamicallyStartProfiling,
stackMode, shouldSample, sampleEverything);
return HeapProfilingTestShimJni.get().runTestForMode(mNativeHeapProfilingTestShim, mode,
dynamicallyStartProfiling, stackMode, shouldSample, sampleEverything);
}
/**
@ -34,15 +35,19 @@ public class HeapProfilingTestShim {
*/
public void destroy() {
if (mNativeHeapProfilingTestShim != 0) {
nativeDestroy(mNativeHeapProfilingTestShim);
HeapProfilingTestShimJni.get().destroy(mNativeHeapProfilingTestShim);
mNativeHeapProfilingTestShim = 0;
}
}
private long mNativeHeapProfilingTestShim;
private native long nativeInit();
private native void nativeDestroy(long nativeHeapProfilingTestShim);
private native boolean nativeRunTestForMode(long nativeHeapProfilingTestShim, String mode,
boolean dynamicallyStartProfiling, String stackMode, boolean shouldSample,
boolean sampleEverything);
@NativeMethods
interface Natives {
long init(HeapProfilingTestShim obj);
void destroy(long nativeHeapProfilingTestShim);
boolean runTestForMode(long nativeHeapProfilingTestShim, String mode,
boolean dynamicallyStartProfiling, String stackMode, boolean shouldSample,
boolean sampleEverything);
}
}

@ -17,6 +17,7 @@ android_library("android_test_message_pump_support_java") {
android_library("content_java_test_support") {
testonly = true
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
deps = [
":android_test_message_pump_support_java",
"//base:base_java",
@ -39,7 +40,6 @@ android_library("content_java_test_support") {
"//url:gurl_java",
"//url:origin_java",
]
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
sources = [
"javatests/src/org/chromium/content_public/browser/test/ChildProcessAllocatorSettings.java",
"javatests/src/org/chromium/content_public/browser/test/ChildProcessAllocatorSettingsHook.java",

@ -6,6 +6,7 @@ package org.chromium.content_public.browser.test;
import org.chromium.base.Callback;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.content.browser.framehost.RenderFrameHostImpl;
import org.chromium.content_public.browser.RenderFrameHost;
@ -17,7 +18,8 @@ public class RenderFrameHostTestExt {
private final long mNativeRenderFrameHostTestExt;
public RenderFrameHostTestExt(RenderFrameHost host) {
mNativeRenderFrameHostTestExt = nativeInit(((RenderFrameHostImpl) host).getNativePtr());
mNativeRenderFrameHostTestExt =
RenderFrameHostTestExtJni.get().init(((RenderFrameHostImpl) host).getNativePtr());
}
/**
@ -28,26 +30,31 @@ public class RenderFrameHostTestExt {
* serialized to a String using JSONStringValueSerializer.
*/
public void executeJavaScript(String script, Callback<String> callback) {
nativeExecuteJavaScript(mNativeRenderFrameHostTestExt, script, callback, false);
RenderFrameHostTestExtJni.get().executeJavaScript(
mNativeRenderFrameHostTestExt, script, callback, false);
}
public void executeJavaScriptWithUserGesture(String script) {
nativeExecuteJavaScript(mNativeRenderFrameHostTestExt, script, (String r) -> {}, true);
RenderFrameHostTestExtJni.get().executeJavaScript(
mNativeRenderFrameHostTestExt, script, (String r) -> {}, true);
}
public void updateVisualState(Callback<Boolean> callback) {
nativeUpdateVisualState(mNativeRenderFrameHostTestExt, callback);
RenderFrameHostTestExtJni.get().updateVisualState(mNativeRenderFrameHostTestExt, callback);
}
public void notifyVirtualKeyboardOverlayRect(int x, int y, int width, int height) {
nativeNotifyVirtualKeyboardOverlayRect(mNativeRenderFrameHostTestExt, x, y, width, height);
RenderFrameHostTestExtJni.get().notifyVirtualKeyboardOverlayRect(
mNativeRenderFrameHostTestExt, x, y, width, height);
}
private native long nativeInit(long renderFrameHostAndroidPtr);
private native void nativeExecuteJavaScript(long nativeRenderFrameHostTestExt, String script,
Callback<String> callback, boolean withUserGesture);
private native void nativeUpdateVisualState(
long nativeRenderFrameHostTestExt, Callback<Boolean> callback);
private native void nativeNotifyVirtualKeyboardOverlayRect(
long nativeRenderFrameHostTestExt, int x, int y, int width, int height);
@NativeMethods
interface Natives {
long init(long renderFrameHostAndroidPtr);
void executeJavaScript(long nativeRenderFrameHostTestExt, String script,
Callback<String> callback, boolean withUserGesture);
void updateVisualState(long nativeRenderFrameHostTestExt, Callback<Boolean> callback);
void notifyVirtualKeyboardOverlayRect(
long nativeRenderFrameHostTestExt, int x, int y, int width, int height);
}
}

@ -5,6 +5,7 @@
package org.chromium.content_public.browser.test.util;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
/**
* Collection of test-only WebContents utilities.
@ -15,8 +16,11 @@ public class RenderProcessHostUtils {
public static int getCurrentRenderProcessCount() {
return TestThreadUtils.runOnUiThreadBlockingNoException(
() -> { return nativeGetCurrentRenderProcessCount(); });
() -> { return RenderProcessHostUtilsJni.get().getCurrentRenderProcessCount(); });
}
private static native int nativeGetCurrentRenderProcessCount();
@NativeMethods
interface Natives {
int getCurrentRenderProcessCount();
}
}

@ -35,7 +35,6 @@ void OnExecuteJavaScriptResult(const base::android::JavaRef<jobject>& jcallback,
} // namespace
jlong JNI_RenderFrameHostTestExt_Init(JNIEnv* env,
const JavaParamRef<jobject>& obj,
jlong render_frame_host_android_ptr) {
RenderFrameHostAndroid* rfha =
reinterpret_cast<RenderFrameHostAndroid*>(render_frame_host_android_ptr);
@ -52,7 +51,6 @@ RenderFrameHostTestExt::RenderFrameHostTestExt(RenderFrameHostImpl* rfhi)
void RenderFrameHostTestExt::ExecuteJavaScript(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jstring>& jscript,
const JavaParamRef<jobject>& jcallback,
jboolean with_user_gesture) {
@ -70,7 +68,6 @@ void RenderFrameHostTestExt::ExecuteJavaScript(
void RenderFrameHostTestExt::UpdateVisualState(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jobject>& jcallback) {
auto result_callback = base::BindOnce(
&base::android::RunBooleanCallbackAndroid,
@ -78,13 +75,11 @@ void RenderFrameHostTestExt::UpdateVisualState(
render_frame_host_->InsertVisualStateCallback(std::move(result_callback));
}
void RenderFrameHostTestExt::NotifyVirtualKeyboardOverlayRect(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
jint x,
jint y,
jint width,
jint height) {
void RenderFrameHostTestExt::NotifyVirtualKeyboardOverlayRect(JNIEnv* env,
jint x,
jint y,
jint width,
jint height) {
gfx::Size size(width, height);
gfx::Point origin(x, y);
render_frame_host_->GetPage().NotifyVirtualKeyboardOverlayRect(

@ -20,23 +20,19 @@ class RenderFrameHostTestExt : public base::SupportsUserData::Data {
explicit RenderFrameHostTestExt(RenderFrameHostImpl* rfhi);
void ExecuteJavaScript(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& jscript,
const base::android::JavaParamRef<jobject>& jcallback,
jboolean with_user_gesture);
// This calls InsertVisualStateCallback(). See it for details on the return
// value.
void UpdateVisualState(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jobject>& jcallback);
void NotifyVirtualKeyboardOverlayRect(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
jint x,
jint y,
jint width,
jint height);
void NotifyVirtualKeyboardOverlayRect(JNIEnv* env,
jint x,
jint y,
jint width,
jint height);
private:
const raw_ptr<RenderFrameHostImpl> render_frame_host_;

@ -454,6 +454,7 @@ if (is_android) {
android_library("bluetooth_test_java") {
testonly = true
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
sources = bluetooth_java_sources_needing_jni
deps = [
"//base:base_java",

@ -21,6 +21,7 @@ import android.util.SparseArray;
import org.chromium.base.Log;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.components.location.LocationUtils;
import org.chromium.device.bluetooth.test.TestRSSI;
import org.chromium.device.bluetooth.test.TestTxPower;
@ -71,7 +72,7 @@ class Fakes {
* Sets the factory for ThreadUtilsWrapper to always post a task to the UI thread
* rather than running the task immediately. This simulates events arriving on a separate
* thread on Android.
* runOnUiThread uses nativePostTaskFromJava. This allows java to post tasks to the
* runOnUiThread uses FakesJni.get().postTaskFromJava. This allows java to post tasks to the
* message loop that the test is using rather than to the Java message loop which
* is not running during tests.
*/
@ -83,7 +84,7 @@ class Fakes {
return new Wrappers.ThreadUtilsWrapper() {
@Override
public void runOnUiThread(Runnable r) {
nativePostTaskFromJava(nativeBluetoothTestAndroid, r);
FakesJni.get().postTaskFromJava(nativeBluetoothTestAndroid, r);
}
};
}
@ -245,11 +246,11 @@ class Fakes {
public boolean disable() {
// android.bluetooth.BluetoothAdapter::disable() is an async call, so we simulate this
// by posting a task to the UI thread.
nativePostTaskFromJava(mNativeBluetoothTestAndroid, new Runnable() {
FakesJni.get().postTaskFromJava(mNativeBluetoothTestAndroid, new Runnable() {
@Override
public void run() {
mPowered = false;
nativeOnFakeAdapterStateChanged(mNativeBluetoothTestAndroid, false);
FakesJni.get().onFakeAdapterStateChanged(mNativeBluetoothTestAndroid, false);
}
});
return true;
@ -259,11 +260,11 @@ class Fakes {
public boolean enable() {
// android.bluetooth.BluetoothAdapter::enable() is an async call, so we simulate this by
// posting a task to the UI thread.
nativePostTaskFromJava(mNativeBluetoothTestAndroid, new Runnable() {
FakesJni.get().postTaskFromJava(mNativeBluetoothTestAndroid, new Runnable() {
@Override
public void run() {
mPowered = true;
nativeOnFakeAdapterStateChanged(mNativeBluetoothTestAndroid, true);
FakesJni.get().onFakeAdapterStateChanged(mNativeBluetoothTestAndroid, true);
}
});
return true;
@ -530,7 +531,8 @@ class Fakes {
"BluetoothGattWrapper doesn't support calls to connectGatt() with "
+ "multiple distinct callbacks.");
}
nativeOnFakeBluetoothDeviceConnectGattCalled(mAdapter.mNativeBluetoothTestAndroid);
FakesJni.get().onFakeBluetoothDeviceConnectGattCalled(
mAdapter.mNativeBluetoothTestAndroid);
mGattCallback = callback;
return mGatt;
}
@ -576,12 +578,13 @@ class Fakes {
@Override
public void disconnect() {
nativeOnFakeBluetoothGattDisconnect(mDevice.mAdapter.mNativeBluetoothTestAndroid);
FakesJni.get().onFakeBluetoothGattDisconnect(
mDevice.mAdapter.mNativeBluetoothTestAndroid);
}
@Override
public void close() {
nativeOnFakeBluetoothGattClose(mDevice.mAdapter.mNativeBluetoothTestAndroid);
FakesJni.get().onFakeBluetoothGattClose(mDevice.mAdapter.mNativeBluetoothTestAndroid);
}
@Override
@ -591,7 +594,8 @@ class Fakes {
@Override
public void discoverServices() {
nativeOnFakeBluetoothGattDiscoverServices(mDevice.mAdapter.mNativeBluetoothTestAndroid);
FakesJni.get().onFakeBluetoothGattDiscoverServices(
mDevice.mAdapter.mNativeBluetoothTestAndroid);
}
@Override
@ -605,7 +609,7 @@ class Fakes {
mReadCharacteristicWillFailSynchronouslyOnce = false;
return false;
}
nativeOnFakeBluetoothGattReadCharacteristic(
FakesJni.get().onFakeBluetoothGattReadCharacteristic(
mDevice.mAdapter.mNativeBluetoothTestAndroid);
return true;
}
@ -617,7 +621,7 @@ class Fakes {
mSetCharacteristicNotificationWillFailSynchronouslyOnce = false;
return false;
}
nativeOnFakeBluetoothGattSetCharacteristicNotification(
FakesJni.get().onFakeBluetoothGattSetCharacteristicNotification(
mDevice.mAdapter.mNativeBluetoothTestAndroid);
return true;
}
@ -628,7 +632,7 @@ class Fakes {
mWriteCharacteristicWillFailSynchronouslyOnce = false;
return false;
}
nativeOnFakeBluetoothGattWriteCharacteristic(
FakesJni.get().onFakeBluetoothGattWriteCharacteristic(
mDevice.mAdapter.mNativeBluetoothTestAndroid, characteristic.getValue());
return true;
}
@ -639,7 +643,8 @@ class Fakes {
mReadDescriptorWillFailSynchronouslyOnce = false;
return false;
}
nativeOnFakeBluetoothGattReadDescriptor(mDevice.mAdapter.mNativeBluetoothTestAndroid);
FakesJni.get().onFakeBluetoothGattReadDescriptor(
mDevice.mAdapter.mNativeBluetoothTestAndroid);
return true;
}
@ -649,7 +654,7 @@ class Fakes {
mWriteDescriptorWillFailSynchronouslyOnce = false;
return false;
}
nativeOnFakeBluetoothGattWriteDescriptor(
FakesJni.get().onFakeBluetoothGattWriteDescriptor(
mDevice.mAdapter.mNativeBluetoothTestAndroid, descriptor.getValue());
return true;
}
@ -987,45 +992,40 @@ class Fakes {
// ---------------------------------------------------------------------------------------------
// BluetoothTestAndroid C++ methods declared for access from java:
@NativeMethods
interface Natives {
// Bind to BluetoothTestAndroid::PostTaskFromJava.
private static native void nativePostTaskFromJava(long nativeBluetoothTestAndroid, Runnable r);
// Bind to BluetoothTestAndroid::PostTaskFromJava.
void postTaskFromJava(long nativeBluetoothTestAndroid, Runnable r);
// Binds to BluetoothTestAndroid::OnFakeAdapterStateChanged.
private static native void nativeOnFakeAdapterStateChanged(
long nativeBluetoothTestAndroid, boolean powered);
// Binds to BluetoothTestAndroid::OnFakeAdapterStateChanged.
void onFakeAdapterStateChanged(long nativeBluetoothTestAndroid, boolean powered);
// Binds to BluetoothTestAndroid::OnFakeBluetoothDeviceConnectGattCalled.
private static native void nativeOnFakeBluetoothDeviceConnectGattCalled(
long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothDeviceConnectGattCalled.
void onFakeBluetoothDeviceConnectGattCalled(long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattDisconnect.
private static native void nativeOnFakeBluetoothGattDisconnect(long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattDisconnect.
void onFakeBluetoothGattDisconnect(long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattClose.
private static native void nativeOnFakeBluetoothGattClose(long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattClose.
void onFakeBluetoothGattClose(long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattDiscoverServices.
private static native void nativeOnFakeBluetoothGattDiscoverServices(
long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattDiscoverServices.
void onFakeBluetoothGattDiscoverServices(long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattSetCharacteristicNotification.
private static native void nativeOnFakeBluetoothGattSetCharacteristicNotification(
long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattSetCharacteristicNotification.
void onFakeBluetoothGattSetCharacteristicNotification(long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattReadCharacteristic.
private static native void nativeOnFakeBluetoothGattReadCharacteristic(
long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattReadCharacteristic.
void onFakeBluetoothGattReadCharacteristic(long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteCharacteristic.
private static native void nativeOnFakeBluetoothGattWriteCharacteristic(
long nativeBluetoothTestAndroid, byte[] value);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteCharacteristic.
void onFakeBluetoothGattWriteCharacteristic(long nativeBluetoothTestAndroid, byte[] value);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattReadDescriptor.
private static native void nativeOnFakeBluetoothGattReadDescriptor(
long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattReadDescriptor.
void onFakeBluetoothGattReadDescriptor(long nativeBluetoothTestAndroid);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteDescriptor.
private static native void nativeOnFakeBluetoothGattWriteDescriptor(
long nativeBluetoothTestAndroid, byte[] value);
// Binds to BluetoothTestAndroid::OnFakeBluetoothGattWriteDescriptor.
void onFakeBluetoothGattWriteDescriptor(long nativeBluetoothTestAndroid, byte[] value);
}
}

@ -70,8 +70,8 @@ android_library("system_impl_java") {
# Targets should also depend on :test_support for the native side.
android_library("test_support_java") {
testonly = true
sources = [ "javatests/src/org/chromium/mojo/MojoTestRule.java" ]
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
sources = [ "javatests/src/org/chromium/mojo/MojoTestRule.java" ]
deps = [
"//base:base_java",
"//base:jni_java",

@ -34,27 +34,20 @@ static void JNI_MojoTestRule_InitCore(JNIEnv* env) {
mojo::core::Init();
}
static void JNI_MojoTestRule_Init(JNIEnv* env,
const JavaParamRef<jobject>& jcaller) {
static void JNI_MojoTestRule_Init(JNIEnv* env) {
base::InitAndroidTestMessageLoop();
}
static jlong JNI_MojoTestRule_SetupTestEnvironment(
JNIEnv* env,
const JavaParamRef<jobject>& jcaller) {
static jlong JNI_MojoTestRule_SetupTestEnvironment(JNIEnv* env) {
return reinterpret_cast<intptr_t>(new TestEnvironment());
}
static void JNI_MojoTestRule_TearDownTestEnvironment(
JNIEnv* env,
const JavaParamRef<jobject>& jcaller,
jlong test_environment) {
static void JNI_MojoTestRule_TearDownTestEnvironment(JNIEnv* env,
jlong test_environment) {
delete reinterpret_cast<TestEnvironment*>(test_environment);
}
static void JNI_MojoTestRule_RunLoop(JNIEnv* env,
const JavaParamRef<jobject>& jcaller,
jlong timeout_ms) {
static void JNI_MojoTestRule_RunLoop(JNIEnv* env, jlong timeout_ms) {
base::RunLoop run_loop;
if (timeout_ms) {
base::SingleThreadTaskRunner::GetCurrentDefault()->PostDelayedTask(

@ -48,39 +48,35 @@ public class MojoTestRule extends ExternalResource {
MojoTestRuleJni.get().initCore();
sIsCoreInitialized = true;
}
nativeInit();
mTestEnvironmentPointer = nativeSetupTestEnvironment();
MojoTestRuleJni.get().init();
mTestEnvironmentPointer = MojoTestRuleJni.get().setupTestEnvironment();
}
@Override
protected void after() {
nativeTearDownTestEnvironment(mTestEnvironmentPointer);
MojoTestRuleJni.get().tearDownTestEnvironment(mTestEnvironmentPointer);
}
/**
* Runs the run loop for the given time.
*/
public void runLoop(long timeoutMS) {
nativeRunLoop(timeoutMS);
MojoTestRuleJni.get().runLoop(timeoutMS);
}
/**
* Runs the run loop until no handle or task are immediately available.
*/
public void runLoopUntilIdle() {
nativeRunLoop(0);
MojoTestRuleJni.get().runLoop(0);
}
private native void nativeInit();
private native long nativeSetupTestEnvironment();
private native void nativeTearDownTestEnvironment(long testEnvironment);
private native void nativeRunLoop(long timeoutMS);
@NativeMethods
interface Natives {
void init();
long setupTestEnvironment();
void tearDownTestEnvironment(long testEnvironment);
void runLoop(long timeoutMS);
void initCore();
}
}

@ -92,6 +92,7 @@ android_library("net_java_test_support") {
android_library("net_java_test_support_provider") {
testonly = true
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
sources = [
"../test/android/javatests/src/org/chromium/net/test/DummySpnegoAuthenticator.java",
"../test/android/javatests/src/org/chromium/net/test/DummySpnegoAuthenticatorService.java",

@ -137,20 +137,15 @@ DummySpnegoAuthenticator::SecurityContextQuery::~SecurityContextQuery() =
default;
base::android::ScopedJavaLocalRef<jstring>
DummySpnegoAuthenticator::SecurityContextQuery::GetTokenToReturn(
JNIEnv* env,
const JavaParamRef<jobject>& /*obj*/) {
DummySpnegoAuthenticator::SecurityContextQuery::GetTokenToReturn(JNIEnv* env) {
return base::android::ConvertUTF8ToJavaString(env, output_token.c_str());
}
int DummySpnegoAuthenticator::SecurityContextQuery::GetResult(
JNIEnv* /*env*/,
const JavaParamRef<jobject>& /*obj*/) {
int DummySpnegoAuthenticator::SecurityContextQuery::GetResult(JNIEnv* /*env*/) {
return response_code;
}
void DummySpnegoAuthenticator::SecurityContextQuery::CheckGetTokenArguments(
JNIEnv* env,
const JavaParamRef<jobject>& /*obj*/,
const JavaParamRef<jstring>& j_incoming_token) {
std::string incoming_token =
base::android::ConvertJavaStringToUTF8(env, j_incoming_token);
@ -187,9 +182,7 @@ void DummySpnegoAuthenticator::ExpectSecurityContext(
base::android::AttachCurrentThread(), reinterpret_cast<intptr_t>(this));
}
long DummySpnegoAuthenticator::GetNextQuery(
JNIEnv* /*env*/,
const JavaParamRef<jobject>& /* obj */) {
long DummySpnegoAuthenticator::GetNextQuery(JNIEnv* /*env*/) {
CheckQueueNotEmpty();
current_query_ = expected_security_queries_.front();
expected_security_queries_.pop_front();

@ -93,17 +93,14 @@ class DummySpnegoAuthenticator {
std::string output_token;
// Java callable members
base::android::ScopedJavaLocalRef<jstring> GetTokenToReturn(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj);
int GetResult(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj);
base::android::ScopedJavaLocalRef<jstring> GetTokenToReturn(JNIEnv* env);
int GetResult(JNIEnv* env);
// Called from Java to check the arguments passed to the GetToken. Has to
// be in C++ since these tests are driven by googletest, and can only report
// failures through the googletest C++ API.
void CheckGetTokenArguments(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jstring>& incoming_token);
};
@ -121,8 +118,7 @@ class DummySpnegoAuthenticator {
static void EnsureTestAccountExists();
static void RemoveTestAccounts();
long GetNextQuery(JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj);
long GetNextQuery(JNIEnv* env);
private:
// Abandon the test if the query queue is empty. Has to be a void function to

@ -18,6 +18,7 @@ import org.chromium.base.ApplicationStatus;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeClassQualifiedName;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.net.HttpNegotiateConstants;
import java.io.IOException;
@ -69,20 +70,22 @@ public class DummySpnegoAuthenticator extends AbstractAccountAuthenticator {
@Override
public Bundle getAuthToken(AccountAuthenticatorResponse response, Account account,
String authTokenType, Bundle options) {
long nativeQuery = nativeGetNextQuery(sNativeDummySpnegoAuthenticator);
long nativeQuery =
DummySpnegoAuthenticatorJni.get().getNextQuery(sNativeDummySpnegoAuthenticator);
String incomingToken = options.getString(HttpNegotiateConstants.KEY_INCOMING_AUTH_TOKEN);
nativeCheckGetTokenArguments(nativeQuery, incomingToken);
DummySpnegoAuthenticatorJni.get().checkGetTokenArguments(nativeQuery, incomingToken);
Bundle result = new Bundle();
result.putString(AccountManager.KEY_ACCOUNT_NAME, account.name);
result.putString(AccountManager.KEY_ACCOUNT_TYPE, account.type);
result.putString(AccountManager.KEY_AUTHTOKEN, nativeGetTokenToReturn(nativeQuery));
result.putString(AccountManager.KEY_AUTHTOKEN,
DummySpnegoAuthenticatorJni.get().getTokenToReturn(nativeQuery));
result.putInt(HttpNegotiateConstants.KEY_SPNEGO_RESULT,
decodeResult(nativeGetResult(nativeQuery)));
decodeResult(DummySpnegoAuthenticatorJni.get().getResult(nativeQuery)));
return result;
}
/**
* @param nativeGetResult
* @param DummySpnegoAuthenticatorJni.get().getResult
* @return
*/
private int decodeResult(int gssApiResult) {
@ -162,22 +165,25 @@ public class DummySpnegoAuthenticator extends AbstractAccountAuthenticator {
sNativeDummySpnegoAuthenticator = nativeDummySpnegoAuthenticator;
}
/**
* Send the relevant decoded arguments of getAuthToken to C++ for checking by googletest checks
* If the checks fail then the C++ unit test using this authenticator will fail.
*
* @param authTokenType
* @param spn
* @param incomingToken
*/
@NativeClassQualifiedName("DummySpnegoAuthenticator::SecurityContextQuery")
private native void nativeCheckGetTokenArguments(long nativeQuery, String incomingToken);
@NativeMethods
interface Natives {
/**
* Send the relevant decoded arguments of getAuthToken to C++ for checking by googletest
* checks If the checks fail then the C++ unit test using this authenticator will fail.
*
* @param authTokenType
* @param spn
* @param incomingToken
*/
@NativeClassQualifiedName("DummySpnegoAuthenticator::SecurityContextQuery")
void checkGetTokenArguments(long nativeQuery, String incomingToken);
@NativeClassQualifiedName("DummySpnegoAuthenticator::SecurityContextQuery")
private native String nativeGetTokenToReturn(long nativeQuery);
@NativeClassQualifiedName("DummySpnegoAuthenticator::SecurityContextQuery")
String getTokenToReturn(long nativeQuery);
@NativeClassQualifiedName("DummySpnegoAuthenticator::SecurityContextQuery")
private native int nativeGetResult(long nativeQuery);
@NativeClassQualifiedName("DummySpnegoAuthenticator::SecurityContextQuery")
int getResult(long nativeQuery);
private native long nativeGetNextQuery(long nativeDummySpnegoAuthenticator);
long getNextQuery(long nativeDummySpnegoAuthenticator);
}
}

@ -14,6 +14,7 @@ import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.library_loader.LibraryLoader;
import org.chromium.base.library_loader.LibraryProcessType;
import org.chromium.base.test.util.UrlUtils;
@ -79,7 +80,8 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
@Override
public Void call() {
if (mNativeEmbeddedTestServer == 0) {
nativeInit(UrlUtils.getIsolatedTestRoot(), https);
EmbeddedTestServerImplJni.get().init(
EmbeddedTestServerImpl.this, UrlUtils.getIsolatedTestRoot(), https);
}
assert mNativeEmbeddedTestServer != 0;
return null;
@ -102,7 +104,7 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
return runOnHandlerThread(new Callable<Boolean>() {
@Override
public Boolean call() {
return nativeStart(mNativeEmbeddedTestServer, port);
return EmbeddedTestServerImplJni.get().start(mNativeEmbeddedTestServer, port);
}
});
}
@ -116,7 +118,8 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
return runOnHandlerThread(new Callable<String>() {
@Override
public String call() {
return nativeGetRootCertPemPath(mNativeEmbeddedTestServer);
return EmbeddedTestServerImplJni.get().getRootCertPemPath(
mNativeEmbeddedTestServer);
}
});
}
@ -132,7 +135,8 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
runOnHandlerThread(new Callable<Void>() {
@Override
public Void call() {
nativeAddDefaultHandlers(mNativeEmbeddedTestServer, directoryPath);
EmbeddedTestServerImplJni.get().addDefaultHandlers(
mNativeEmbeddedTestServer, directoryPath);
return null;
}
});
@ -147,7 +151,8 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
runOnHandlerThread(new Callable<Void>() {
@Override
public Void call() {
nativeSetSSLConfig(mNativeEmbeddedTestServer, serverCertificate);
EmbeddedTestServerImplJni.get().setSSLConfig(
mNativeEmbeddedTestServer, serverCertificate);
return null;
}
});
@ -162,7 +167,8 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
runOnHandlerThread(new Callable<Void>() {
@Override
public Void call() {
nativeRegisterRequestHandler(mNativeEmbeddedTestServer, handler);
EmbeddedTestServerImplJni.get().registerRequestHandler(
mNativeEmbeddedTestServer, handler);
return null;
}
});
@ -177,7 +183,8 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
runOnHandlerThread(new Callable<Void>() {
@Override
public Void call() {
nativeServeFilesFromDirectory(mNativeEmbeddedTestServer, directoryPath);
EmbeddedTestServerImplJni.get().serveFilesFromDirectory(
mNativeEmbeddedTestServer, directoryPath);
return null;
}
});
@ -210,7 +217,8 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
return runOnHandlerThread(new Callable<String>() {
@Override
public String call() {
return nativeGetURL(mNativeEmbeddedTestServer, relativeUrl);
return EmbeddedTestServerImplJni.get().getURL(
mNativeEmbeddedTestServer, relativeUrl);
}
});
}
@ -227,7 +235,8 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
return runOnHandlerThread(new Callable<String>() {
@Override
public String call() {
return nativeGetURLWithHostName(mNativeEmbeddedTestServer, hostName, relativeUrl);
return EmbeddedTestServerImplJni.get().getURLWithHostName(
mNativeEmbeddedTestServer, hostName, relativeUrl);
}
});
}
@ -241,7 +250,8 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
return runOnHandlerThread(new Callable<Boolean>() {
@Override
public Boolean call() {
return nativeShutdownAndWaitUntilComplete(mNativeEmbeddedTestServer);
return EmbeddedTestServerImplJni.get().shutdownAndWaitUntilComplete(
mNativeEmbeddedTestServer);
}
});
}
@ -253,7 +263,7 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
@Override
public Void call() {
assert mNativeEmbeddedTestServer != 0;
nativeDestroy(mNativeEmbeddedTestServer);
EmbeddedTestServerImplJni.get().destroy(mNativeEmbeddedTestServer);
assert mNativeEmbeddedTestServer == 0;
return null;
}
@ -309,20 +319,19 @@ public class EmbeddedTestServerImpl extends IEmbeddedTestServerImpl.Stub {
mNativeEmbeddedTestServer = 0;
}
private native void nativeInit(String testDataDir, boolean https);
private native void nativeDestroy(long nativeEmbeddedTestServerAndroid);
private native boolean nativeStart(long nativeEmbeddedTestServerAndroid, int port);
private native String nativeGetRootCertPemPath(long nativeEmbeddedTestServerAndroid);
private native boolean nativeShutdownAndWaitUntilComplete(long nativeEmbeddedTestServerAndroid);
private native void nativeAddDefaultHandlers(
long nativeEmbeddedTestServerAndroid, String directoryPath);
private native void nativeSetSSLConfig(
long nativeEmbeddedTestServerAndroid, int serverCertificate);
private native void nativeRegisterRequestHandler(
long nativeEmbeddedTestServerAndroid, long handler);
private native String nativeGetURL(long nativeEmbeddedTestServerAndroid, String relativeUrl);
private native String nativeGetURLWithHostName(
long nativeEmbeddedTestServerAndroid, String hostName, String relativeUrl);
private native void nativeServeFilesFromDirectory(
long nativeEmbeddedTestServerAndroid, String directoryPath);
@NativeMethods
interface Natives {
void init(EmbeddedTestServerImpl obj, String testDataDir, boolean https);
void destroy(long nativeEmbeddedTestServerAndroid);
boolean start(long nativeEmbeddedTestServerAndroid, int port);
String getRootCertPemPath(long nativeEmbeddedTestServerAndroid);
boolean shutdownAndWaitUntilComplete(long nativeEmbeddedTestServerAndroid);
void addDefaultHandlers(long nativeEmbeddedTestServerAndroid, String directoryPath);
void setSSLConfig(long nativeEmbeddedTestServerAndroid, int serverCertificate);
void registerRequestHandler(long nativeEmbeddedTestServerAndroid, long handler);
String getURL(long nativeEmbeddedTestServerAndroid, String relativeUrl);
String getURLWithHostName(
long nativeEmbeddedTestServerAndroid, String hostName, String relativeUrl);
void serveFilesFromDirectory(long nativeEmbeddedTestServerAndroid, String directoryPath);
}
}

@ -59,28 +59,22 @@ EmbeddedTestServerAndroid::~EmbeddedTestServerAndroid() {
Java_EmbeddedTestServerImpl_clearNativePtr(env, weak_java_server_.get(env));
}
jboolean EmbeddedTestServerAndroid::Start(JNIEnv* env,
const JavaParamRef<jobject>& jobj,
jint port) {
jboolean EmbeddedTestServerAndroid::Start(JNIEnv* env, jint port) {
return test_server_.Start(static_cast<int>(port));
}
ScopedJavaLocalRef<jstring> EmbeddedTestServerAndroid::GetRootCertPemPath(
JNIEnv* env,
const JavaParamRef<jobject>& jobj) const {
JNIEnv* env) const {
return base::android::ConvertUTF8ToJavaString(
env, test_server_.GetRootCertPemPath().value());
}
jboolean EmbeddedTestServerAndroid::ShutdownAndWaitUntilComplete(
JNIEnv* env,
const JavaParamRef<jobject>& jobj) {
jboolean EmbeddedTestServerAndroid::ShutdownAndWaitUntilComplete(JNIEnv* env) {
return test_server_.ShutdownAndWaitUntilComplete();
}
ScopedJavaLocalRef<jstring> EmbeddedTestServerAndroid::GetURL(
JNIEnv* env,
const JavaParamRef<jobject>& jobj,
const JavaParamRef<jstring>& jrelative_url) const {
const GURL gurl(test_server_.GetURL(
base::android::ConvertJavaStringToUTF8(env, jrelative_url)));
@ -89,7 +83,6 @@ ScopedJavaLocalRef<jstring> EmbeddedTestServerAndroid::GetURL(
ScopedJavaLocalRef<jstring> EmbeddedTestServerAndroid::GetURLWithHostName(
JNIEnv* env,
const JavaParamRef<jobject>& jobj,
const JavaParamRef<jstring>& jhostname,
const JavaParamRef<jstring>& jrelative_url) const {
const GURL gurl(test_server_.GetURL(
@ -100,7 +93,6 @@ ScopedJavaLocalRef<jstring> EmbeddedTestServerAndroid::GetURLWithHostName(
void EmbeddedTestServerAndroid::AddDefaultHandlers(
JNIEnv* env,
const JavaParamRef<jobject>& jobj,
const JavaParamRef<jstring>& jdirectory_path) {
const base::FilePath directory(
base::android::ConvertJavaStringToUTF8(env, jdirectory_path));
@ -108,7 +100,6 @@ void EmbeddedTestServerAndroid::AddDefaultHandlers(
}
void EmbeddedTestServerAndroid::SetSSLConfig(JNIEnv* jenv,
const JavaParamRef<jobject>& jobj,
jint jserver_certificate) {
test_server_.SetSSLConfig(
static_cast<EmbeddedTestServer::ServerCertificate>(jserver_certificate));
@ -117,17 +108,14 @@ void EmbeddedTestServerAndroid::SetSSLConfig(JNIEnv* jenv,
typedef std::unique_ptr<HttpResponse> (*HandleRequestPtr)(
const HttpRequest& request);
void EmbeddedTestServerAndroid::RegisterRequestHandler(
JNIEnv* env,
const JavaParamRef<jobject>& jobj,
jlong handler) {
void EmbeddedTestServerAndroid::RegisterRequestHandler(JNIEnv* env,
jlong handler) {
HandleRequestPtr handler_ptr = reinterpret_cast<HandleRequestPtr>(handler);
test_server_.RegisterRequestHandler(base::BindRepeating(handler_ptr));
}
void EmbeddedTestServerAndroid::ServeFilesFromDirectory(
JNIEnv* env,
const JavaParamRef<jobject>& jobj,
const JavaParamRef<jstring>& jdirectory_path) {
const base::FilePath directory(
base::android::ConvertJavaStringToUTF8(env, jdirectory_path));
@ -146,8 +134,7 @@ void EmbeddedTestServerAndroid::ReadFromSocket(const void* socket_id) {
env, weak_java_server_.get(env), reinterpret_cast<intptr_t>(socket_id));
}
void EmbeddedTestServerAndroid::Destroy(JNIEnv* env,
const JavaParamRef<jobject>& jobj) {
void EmbeddedTestServerAndroid::Destroy(JNIEnv* env) {
delete this;
}

@ -30,47 +30,34 @@ class EmbeddedTestServerAndroid {
~EmbeddedTestServerAndroid();
void Destroy(JNIEnv* env, const base::android::JavaParamRef<jobject>& obj);
void Destroy(JNIEnv* env);
jboolean Start(JNIEnv* env,
const base::android::JavaParamRef<jobject>& jobj,
jint port);
jboolean Start(JNIEnv* env, jint port);
base::android::ScopedJavaLocalRef<jstring> GetRootCertPemPath(
JNIEnv* jenv,
const base::android::JavaParamRef<jobject>& jobj) const;
JNIEnv* jenv) const;
jboolean ShutdownAndWaitUntilComplete(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jobj);
jboolean ShutdownAndWaitUntilComplete(JNIEnv* env);
base::android::ScopedJavaLocalRef<jstring> GetURL(
JNIEnv* jenv,
const base::android::JavaParamRef<jobject>& jobj,
const base::android::JavaParamRef<jstring>& jrelative_url) const;
base::android::ScopedJavaLocalRef<jstring> GetURLWithHostName(
JNIEnv* jenv,
const base::android::JavaParamRef<jobject>& jobj,
const base::android::JavaParamRef<jstring>& jhostname,
const base::android::JavaParamRef<jstring>& jrelative_url) const;
void AddDefaultHandlers(
JNIEnv* jenv,
const base::android::JavaParamRef<jobject>& jobj,
const base::android::JavaParamRef<jstring>& jdirectory_path);
void SetSSLConfig(JNIEnv* jenv,
const base::android::JavaParamRef<jobject>& jobj,
jint jserver_certificate);
void SetSSLConfig(JNIEnv* jenv, jint jserver_certificate);
void RegisterRequestHandler(JNIEnv* jenv,
const base::android::JavaParamRef<jobject>& jobj,
jlong handler);
void RegisterRequestHandler(JNIEnv* jenv, jlong handler);
void ServeFilesFromDirectory(
JNIEnv* env,
const base::android::JavaParamRef<jobject>& jobj,
const base::android::JavaParamRef<jstring>& jdirectory_path);
private:

@ -45,12 +45,14 @@ source_set("native_test_native_code") {
android_library("native_main_runner_java") {
testonly = true
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
sources = [ "java/src/org/chromium/native_test/MainRunner.java" ]
deps = [ "//base:jni_java" ]
}
android_library("native_test_java") {
testonly = true
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
deps = [
":native_main_runner_java",
"//base:base_java",
@ -61,7 +63,6 @@ android_library("native_test_java") {
"//testing/android/reporter:reporter_java",
"//third_party/androidx:androidx_fragment_fragment_java",
]
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
sources = [
"java/src/org/chromium/native_test/NativeBrowserTest.java",
"java/src/org/chromium/native_test/NativeBrowserTestActivity.java",

@ -5,6 +5,7 @@
package org.chromium.native_test;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
/**
* This class provides a way to run the native main method.
@ -20,7 +21,11 @@ public final class MainRunner {
// Maps the file descriptors and executes the main method with the passed in command line.
public static int runMain(String[] commandLine) {
return nativeRunMain(commandLine);
return MainRunnerJni.get().runMain(commandLine);
}
@NativeMethods
interface Natives {
int runMain(String[] commandLine);
}
private static native int nativeRunMain(String[] commandLine);
}

@ -16,6 +16,7 @@ import android.system.Os;
import org.chromium.base.Log;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
import org.chromium.base.test.util.UrlUtils;
import org.chromium.build.gtest_apk.NativeTestIntent;
import org.chromium.test.reporter.TestStatusReporter;
@ -173,8 +174,8 @@ public class NativeTest {
}
private void runTests(Activity activity) {
nativeRunTests(mCommandLineFlags.toString(), mCommandLineFilePath, mStdoutFilePath,
activity.getApplicationContext(), UrlUtils.getIsolatedTestRoot());
NativeTestJni.get().runTests(mCommandLineFlags.toString(), mCommandLineFilePath,
mStdoutFilePath, activity.getApplicationContext(), UrlUtils.getIsolatedTestRoot());
activity.finish();
mReporter.testRunFinished(Process.myPid());
}
@ -186,6 +187,9 @@ public class NativeTest {
Log.e(TAG, "[ RUNNER_FAILED ] could not load native library");
}
private native void nativeRunTests(String commandLineFlags, String commandLineFilePath,
String stdoutFilePath, Context appContext, String testDataDir);
@NativeMethods
interface Natives {
void runTests(String commandLineFlags, String commandLineFilePath, String stdoutFilePath,
Context appContext, String testDataDir);
}
}

@ -77,7 +77,6 @@ void AndroidLog(int priority, const char* format, ...) {
static void JNI_NativeTest_RunTests(
JNIEnv* env,
const JavaParamRef<jobject>& obj,
const JavaParamRef<jstring>& jcommand_line_flags,
const JavaParamRef<jstring>& jcommand_line_file_path,
const JavaParamRef<jstring>& jstdout_file_path,

@ -303,6 +303,7 @@ if (is_android) {
android_library("android_test_helper_java") {
testonly = true
annotation_processor_deps = [ "//base/android/jni_generator:jni_processor" ]
sources = [
"android/javatests/src/org/chromium/url/GURLJavaTestHelper.java",
"android/javatests/src/org/chromium/url/OriginJavaTestHelper.java",

@ -6,6 +6,7 @@ package org.chromium.url;
import org.chromium.base.annotations.CalledByNative;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
/**
* Helpers for GURLJavaTest that need to call into native code.
@ -17,6 +18,17 @@ public class GURLJavaTestHelper {
return new GURL(uri);
}
public static native void nativeInitializeICU();
public static native void nativeTestGURLEquivalence();
public static void nativeInitializeICU() {
GURLJavaTestHelperJni.get().initializeICU();
}
public static void nativeTestGURLEquivalence() {
GURLJavaTestHelperJni.get().testGURLEquivalence();
}
@NativeMethods
interface Natives {
void initializeICU();
void testGURLEquivalence();
}
}

@ -32,7 +32,7 @@ public class OriginJavaTest {
@SmallTest
@Test
public void testOriginEquivalence() {
OriginJavaTestHelper.nativeTestOriginEquivalence();
OriginJavaTestHelper.testOriginEquivalence();
}
@SmallTest

@ -5,11 +5,19 @@
package org.chromium.url;
import org.chromium.base.annotations.JNINamespace;
import org.chromium.base.annotations.NativeMethods;
/**
* Helpers for OriginJavaTest that need to call into native code.
*/
@JNINamespace("url")
public class OriginJavaTestHelper {
public static native void nativeTestOriginEquivalence();
public static void testOriginEquivalence() {
OriginJavaTestHelperJni.get().testOriginEquivalence();
}
@NativeMethods
interface Natives {
void testOriginEquivalence();
}
}