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

Unified Diff: chrome/android/sync_shell/javatests/src/chromium/chrome/browser/sync/FakeServerHelper.java

Issue 457883002: Use Sync FakeServer in Android tests via custom APK (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 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 side-by-side diff with in-line comments
Download patch
Index: chrome/android/sync_shell/javatests/src/chromium/chrome/browser/sync/FakeServerHelper.java
diff --git a/chrome/android/sync_shell/javatests/src/chromium/chrome/browser/sync/FakeServerHelper.java b/chrome/android/sync_shell/javatests/src/chromium/chrome/browser/sync/FakeServerHelper.java
new file mode 100644
index 0000000000000000000000000000000000000000..09a25ce673c06ba2aed6e30c9c8b9fcad538a497
--- /dev/null
+++ b/chrome/android/sync_shell/javatests/src/chromium/chrome/browser/sync/FakeServerHelper.java
@@ -0,0 +1,123 @@
+// 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.chrome.browser.sync;
+
+import android.content.Context;
+
+import org.chromium.base.ThreadUtils;
+//import org.chromium.chrome.browser.sync.ProfileSyncService;
+
+import java.util.concurrent.Callable;
+
+/**
+ * Assists in Java interaction the native Sync FakeServer.
+ */
+public class FakeServerHelper {
+ // Lazily-instantiated singleton FakeServerHelper.
+ private static FakeServerHelper sFakeServerHelper;
+
+ // Pointer value for the FakeServer. This pointer is not owned by native
+ // code, so it must be stored here for future deletion.
+ private static long sNativeFakeServer = 0L;
+
+ // The pointer to the native object called here.
+ private final long mNativeFakeServerHelperAndroid;
+
+ // Accesses the singleton FakeServerHelper. There is at most one instance created per
+ // application lifetime, so no deletion mechanism is provided for the native object.
+ public static FakeServerHelper get() {
+ ThreadUtils.assertOnUiThread();
+ if (sFakeServerHelper == null) {
+ sFakeServerHelper = new FakeServerHelper();
+ }
+ return sFakeServerHelper;
+ }
+
+ private FakeServerHelper() {
+ mNativeFakeServerHelperAndroid = nativeInit();
+ }
+
+ /**
+ * Creates and configures FakeServer.
+ *
+ * Each call to this method should be accompanied by a later call to deleteFakeServer to avoid
+ * a memory leak.
+ */
+ public static void useFakeServer(final Context context) {
+ if (sNativeFakeServer != 0L) {
+ throw new IllegalStateException(
+ "deleteFakeServer must be called before calling useFakeServer again.");
+ }
+
+ sNativeFakeServer = ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Long>() {
+ @Override
+ public Long call() {
+ FakeServerHelper fakeServerHelper = FakeServerHelper.get();
+ long nativeFakeServer = fakeServerHelper.createFakeServer();
+ long resources = fakeServerHelper.createNetworkResources(nativeFakeServer);
+ ProfileSyncService.get(context).overrideNetworkResourcesForTest(resources);
+
+ return nativeFakeServer;
+ }
+ });
+ }
+
+ /**
+ * Deletes the existing FakeServer.
+ */
+ public static void deleteFakeServer() {
+ if (sNativeFakeServer == 0L) {
+ throw new IllegalStateException(
+ "useFakeServer must be called before calling deleteFakeServer.");
+ }
+
+ ThreadUtils.runOnUiThreadBlockingNoException(new Callable<Void>() {
+ @Override
+ public Void call() {
+ FakeServerHelper.get().deleteFakeServer(sNativeFakeServer);
+ sNativeFakeServer = 0L;
+ return null;
+ }
+ });
+ }
+
+ /**
+ * Creates a native FakeServer object and returns its pointer. This pointer is owned by the
+ * Java caller.
+ *
+ * @return the FakeServer pointer
+ */
+ public long createFakeServer() {
+ return nativeCreateFakeServer(mNativeFakeServerHelperAndroid);
+ }
+
+ /**
+ * Creates a native NetworkResources object. This pointer is owned by the Java caller, but
+ * ownership is transferred as part of ProfileSyncService.overrideNetworkResourcesForTest.
+ *
+ * @param nativeFakeServer pointer to a native FakeServer object.
+ * @return the NetworkResources pointer
+ */
+ public long createNetworkResources(long nativeFakeServer) {
+ return nativeCreateNetworkResources(mNativeFakeServerHelperAndroid, nativeFakeServer);
+ }
+
+ /**
+ * Deletes a native FakeServer.
+ *
+ * @param nativeFakeServer the pointer to be deleted
+ */
+ public void deleteFakeServer(long nativeFakeServer) {
+ nativeDeleteFakeServer(mNativeFakeServerHelperAndroid, nativeFakeServer);
+ }
+
+ // Native methods.
+ private native long nativeInit();
+ private native long nativeCreateFakeServer(long nativeFakeServerHelperAndroid);
+ private native long nativeCreateNetworkResources(
+ long nativeFakeServerHelperAndroid, long nativeFakeServer);
+ private native void nativeDeleteFakeServer(
+ long nativeFakeServerHelperAndroid, long nativeFakeServer);
+}

Powered by Google App Engine
This is Rietveld 408576698