Chromium Code Reviews| Index: components/gcm_driver/instance_id/android/javatests/src/org/chromium/components/gcm_driver/instance_id/FakeInstanceIDWithSubtype.java |
| diff --git a/components/gcm_driver/instance_id/android/javatests/src/org/chromium/components/gcm_driver/instance_id/FakeInstanceIDWithSubtype.java b/components/gcm_driver/instance_id/android/javatests/src/org/chromium/components/gcm_driver/instance_id/FakeInstanceIDWithSubtype.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ed87213307fa60cac5512b39859cc51f2213bb6a |
| --- /dev/null |
| +++ b/components/gcm_driver/instance_id/android/javatests/src/org/chromium/components/gcm_driver/instance_id/FakeInstanceIDWithSubtype.java |
| @@ -0,0 +1,121 @@ |
| +// Copyright 2016 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.instance_id; |
| + |
| +import android.content.Context; |
| +import android.os.Bundle; |
| +import android.os.Looper; |
| + |
| +import com.google.android.gms.iid.InstanceID; |
| + |
| +import org.chromium.base.annotations.CalledByNative; |
| +import org.chromium.base.annotations.JNINamespace; |
| + |
| +import java.io.IOException; |
| + |
| +import java.util.HashMap; |
| +import java.util.Map; |
| +import java.util.Random; |
| + |
| +/** |
| + * Fake for InstanceIDWithSubtype. |
|
Peter Beverloo
2016/04/15 00:00:17
Please document how it's fake. Even "avoids hittin
johnme
2016/04/15 16:00:43
Done.
|
| + */ |
| +@JNINamespace("instance_id") |
| +public class FakeInstanceIDWithSubtype extends InstanceIDWithSubtype { |
| + private String mId = null; |
| + private long mCreationTime = 0; |
| + private Map<String, String> mTokens = new HashMap<>(); |
| + |
| + /** Enable this in all InstanceID tests to use this fake instead of hitting the network/disk. */ |
| + @CalledByNative |
| + public static void clearDataAndSetEnabled(boolean enable) { |
| + synchronized (InstanceID.class) { |
| + getInstanceIDsForTesting().clear(); |
| + } |
| + if (enable) { |
| + sFakeFactoryForTesting = new FakeFactory() { |
| + @Override |
| + public InstanceIDWithSubtype create(Context context, String subtype) { |
| + return new FakeInstanceIDWithSubtype(context, subtype); |
| + } |
| + }; |
| + } else { |
| + sFakeFactoryForTesting = null; |
| + } |
| + } |
| + |
| + public FakeInstanceIDWithSubtype(Context context, String subtype) { |
|
Peter Beverloo
2016/04/15 00:00:17
private?
johnme
2016/04/15 16:00:43
Done.
|
| + super(context, subtype); |
| + } |
| + |
| + @Override |
| + public String getId() { |
| + if (mId == null) { |
| + mCreationTime = System.currentTimeMillis(); |
| + mId = randomBase64(11 /* length */); |
| + } |
| + return mId; |
| + } |
| + |
| + @Override |
| + public long getCreationTime() { |
| + return mCreationTime; |
| + } |
| + |
| + @Override |
| + public String getToken(String authorizedEntity, String scope) throws IOException { |
| + return getToken(authorizedEntity, scope, null); |
| + } |
| + |
| + @Override |
| + public String getToken(String authorizedEntity, String scope, Bundle extras) |
| + throws IOException { |
| + if (Looper.getMainLooper() == Looper.myLooper()) |
| + throw new IOException(InstanceID.ERROR_MAIN_THREAD); |
|
Peter Beverloo
2016/04/15 00:00:17
nit: even one-line if-statements must have curly b
johnme
2016/04/15 16:00:43
Done.
|
| + String key = getSubtype() + ',' + authorizedEntity + ',' + scope; |
| + String token = mTokens.get(key); |
| + if (token == null && !"true".equals(extras.getString("fakeOffline"))) { |
|
Peter Beverloo
2016/04/15 00:00:17
Let's please introduce more detailed functionality
johnme
2016/04/15 16:00:43
Removed.
|
| + getId(); |
| + token = mId + ':' + randomBase64(140 /* length */); |
| + mTokens.put(key, token); |
| + } |
| + return token; |
| + } |
| + |
| + @Override |
| + public void deleteToken(String authorizedEntity, String scope) throws IOException { |
| + if (Looper.getMainLooper() == Looper.myLooper()) |
| + throw new IOException(InstanceID.ERROR_MAIN_THREAD); |
| + String key = getSubtype() + ',' + authorizedEntity + ',' + scope; |
| + mTokens.remove(key); |
| + // Calling deleteToken causes ID to be generated; can be observed though getCreationTime. |
| + getId(); |
| + } |
| + |
| + @Override |
| + public void deleteInstanceID() throws IOException { |
| + synchronized (InstanceID.class) { |
| + getInstanceIDsForTesting().remove(getSubtype()); |
| + if (Looper.getMainLooper() == Looper.myLooper()) |
| + throw new IOException(InstanceID.ERROR_MAIN_THREAD); |
| + mTokens.clear(); |
| + mCreationTime = 0; |
| + mId = null; |
| + } |
| + } |
| + |
| + private static final String BASE64URL_ALPHABET = |
| + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_"; |
|
Peter Beverloo
2016/04/15 00:00:17
nit: statics go at the top of the class
johnme
2016/04/15 16:00:43
The style guide says "Define fields either at the
|
| + /** Returns a random (not necessarily valid) base64url encoded string. */ |
|
Peter Beverloo
2016/04/15 00:00:17
Please give this a proper doc block.
Also note th
johnme
2016/04/15 16:00:43
Ah good - removed "not necessarily valid". This is
|
| + private static String randomBase64(int length) { |
| + Random random = new Random(); |
| + StringBuilder sb = new StringBuilder(length); |
| + for (int i = 0; i < length; i++) { |
| + int index = random.nextInt(BASE64URL_ALPHABET.length()); |
| + sb.append(BASE64URL_ALPHABET.charAt(index)); |
| + } |
| + return sb.toString(); |
| + } |
| +} |