diff options
author | mvanouwerkerk <mvanouwerkerk@chromium.org> | 2015-12-01 02:45:57 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-12-01 10:47:28 +0000 |
commit | 37acca793d11829f8006e17da779e706f9667fcc (patch) | |
tree | 91e1a7f38117ba2d1926e1260558ac558a6db84a /components | |
parent | 20307b827528c927cb508f8057838f92b3913899 (diff) | |
download | chromium_src-37acca793d11829f8006e17da779e706f9667fcc.zip chromium_src-37acca793d11829f8006e17da779e706f9667fcc.tar.gz chromium_src-37acca793d11829f8006e17da779e706f9667fcc.tar.bz2 |
Instrumentation integration test for the Push API on Android.
Checks the happy path of subscribing to push, receiving a push message, and showing a notification. More scenarios to be added later.
BUG=558402
Review URL: https://codereview.chromium.org/1471193006
Cr-Commit-Position: refs/heads/master@{#362371}
Diffstat (limited to 'components')
5 files changed, 118 insertions, 8 deletions
diff --git a/components/gcm_driver/android/BUILD.gn b/components/gcm_driver/android/BUILD.gn index 70e99a9..d3adb02 100644 --- a/components/gcm_driver/android/BUILD.gn +++ b/components/gcm_driver/android/BUILD.gn @@ -20,6 +20,7 @@ android_library("gcm_driver_java") { "//sync/android:sync_java", "//third_party/android_tools:android_gcm_java", "//third_party/cacheinvalidation:cacheinvalidation_javalib", + "//third_party/jsr-305:jsr_305_javalib", ] DEPRECATED_java_in_dir = "java/src" diff --git a/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/FakeGoogleCloudMessagingSubscriber.java b/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/FakeGoogleCloudMessagingSubscriber.java new file mode 100644 index 0000000..8085b4b --- /dev/null +++ b/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/FakeGoogleCloudMessagingSubscriber.java @@ -0,0 +1,56 @@ +// Copyright 2015 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.components.gcm_driver; + +import android.os.Bundle; + +import org.chromium.base.VisibleForTesting; + +import java.io.IOException; + +import javax.annotation.Nullable; + +/** + * A fake subscriber for test purposes. + */ +public class FakeGoogleCloudMessagingSubscriber implements GoogleCloudMessagingSubscriber { + private static final String FAKE_REGISTRATION_ID = "FAKE_REGISTRATION_ID"; + private String mLastSubscribeSource; + private String mLastSubscribeSubtype; + + @Override + public String subscribe(String source, String subtype, @Nullable Bundle data) + throws IOException { + mLastSubscribeSource = source; + mLastSubscribeSubtype = subtype; + return FAKE_REGISTRATION_ID; + } + + @Override + public void unsubscribe(String source, String subtype, @Nullable Bundle data) + throws IOException { + // No need to do anything here yet. + } + + /** + * The source (sender id) passed to #subscribe the last time it was called, or null if it was + * never called. + */ + @Nullable + @VisibleForTesting + public String getLastSubscribeSource() { + return mLastSubscribeSource; + } + + /** + * The subtype (app id) passed to #subscribe the last time it was called, or null if it was + * never called. + */ + @Nullable + @VisibleForTesting + public String getLastSubscribeSubtype() { + return mLastSubscribeSubtype; + } +} diff --git a/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GCMDriver.java b/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GCMDriver.java index 7cb0d6d..13ecf83 100644 --- a/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GCMDriver.java +++ b/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GCMDriver.java @@ -10,6 +10,7 @@ import android.os.Bundle; import android.util.Log; import org.chromium.base.ThreadUtils; +import org.chromium.base.VisibleForTesting; import org.chromium.base.annotations.CalledByNative; import org.chromium.base.annotations.JNINamespace; import org.chromium.base.library_loader.LibraryProcessType; @@ -37,12 +38,12 @@ public class GCMDriver { private long mNativeGCMDriverAndroid; private final Context mContext; - private final GoogleCloudMessagingV2 mGcm; + private GoogleCloudMessagingSubscriber mSubscriber; private GCMDriver(long nativeGCMDriverAndroid, Context context) { mNativeGCMDriverAndroid = nativeGCMDriverAndroid; mContext = context; - mGcm = new GoogleCloudMessagingV2(context); + mSubscriber = new GoogleCloudMessagingV2(context); } /** @@ -80,7 +81,7 @@ public class GCMDriver { protected String doInBackground(Void... voids) { try { String subtype = appId; - String registrationId = mGcm.subscribe(senderId, subtype, null); + String registrationId = mSubscriber.subscribe(senderId, subtype, null); return registrationId; } catch (IOException ex) { Log.w(TAG, "GCM subscription failed for " + appId + ", " + senderId, ex); @@ -102,7 +103,7 @@ public class GCMDriver { protected Boolean doInBackground(Void... voids) { try { String subtype = appId; - mGcm.unsubscribe(senderId, subtype, null); + mSubscriber.unsubscribe(senderId, subtype, null); return true; } catch (IOException ex) { Log.w(TAG, "GCM unsubscription failed for " + appId + ", " + senderId, ex); @@ -160,6 +161,13 @@ public class GCMDriver { }); } + @VisibleForTesting + public static void overrideSubscriberForTesting(GoogleCloudMessagingSubscriber subscriber) { + assert sInstance != null; + assert subscriber != null; + sInstance.mSubscriber = subscriber; + } + private native void nativeOnRegisterFinished(long nativeGCMDriverAndroid, String appId, String registrationId, boolean success); private native void nativeOnUnregisterFinished(long nativeGCMDriverAndroid, String appId, diff --git a/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GoogleCloudMessagingSubscriber.java b/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GoogleCloudMessagingSubscriber.java new file mode 100644 index 0000000..9ea0844 --- /dev/null +++ b/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GoogleCloudMessagingSubscriber.java @@ -0,0 +1,41 @@ +// Copyright 2015 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.components.gcm_driver; + +import android.os.Bundle; + +import java.io.IOException; + +import javax.annotation.Nullable; + +/** + * Helper to subscribe to and unsubscribe from Google Cloud Messaging. + */ +public interface GoogleCloudMessagingSubscriber { + /** + * Subscribes to a source to start receiving messages from it. + * <p> + * This method may perform blocking I/O and should not be called on the main thread. + * + * @param source The source of the notifications to subscribe to. + * @param subtype The sub-source of the notifications. + * @param data Additional information. + * @return The registration id. + * @throws IOException if the request fails. + */ + String subscribe(String source, String subtype, @Nullable Bundle data) throws IOException; + + /** + * Unsubscribes from a source to stop receiving messages from it. + * <p> + * This method may perform blocking I/O and should not be called on the main thread. + * + * @param source The source to unsubscribe from. + * @param subtype The sub-source of the notifications. + * @param data Additional information. + * @throws IOException if the request fails. + */ + void unsubscribe(String source, String subtype, @Nullable Bundle data) throws IOException; +} diff --git a/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GoogleCloudMessagingV2.java b/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GoogleCloudMessagingV2.java index 30d3213..2b21403 100644 --- a/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GoogleCloudMessagingV2.java +++ b/components/gcm_driver/android/java/src/org/chromium/components/gcm_driver/GoogleCloudMessagingV2.java @@ -20,14 +20,14 @@ import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; +import javax.annotation.Nullable; /** * Temporary code for sending subtypes when (un)subscribing with GCM. * Subtypes are experimental and may change without notice! * TODO(johnme): Remove this file, once we switch to the GMS client library. */ -public class GoogleCloudMessagingV2 { - +public class GoogleCloudMessagingV2 implements GoogleCloudMessagingSubscriber { private static final String GOOGLE_PLAY_SERVICES_PACKAGE = "com.google.android.gms"; private static final long REGISTER_TIMEOUT = 5000; private static final String ACTION_C2DM_REGISTER = "com.google.android.c2dm.intent.REGISTER"; @@ -50,7 +50,9 @@ public class GoogleCloudMessagingV2 { mContext = context; } - public String subscribe(String source, String subtype, Bundle data) throws IOException { + @Override + public String subscribe(String source, String subtype, @Nullable Bundle data) + throws IOException { if (data == null) { data = new Bundle(); } @@ -59,7 +61,9 @@ public class GoogleCloudMessagingV2 { return result.getString(EXTRA_REGISTRATION_ID); } - public void unsubscribe(String source, String subtype, Bundle data) throws IOException { + @Override + public void unsubscribe(String source, String subtype, @Nullable Bundle data) + throws IOException { if (data == null) { data = new Bundle(); } |