Index: components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDWithSubtype.java |
diff --git a/components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDWithSubtype.java b/components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDWithSubtype.java |
index 8af5f6e3a3ab82669d5b8bb60d3fb302b45da8d7..bf5ec63a1bdd951a802680eac593d90fc6cb1f50 100644 |
--- a/components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDWithSubtype.java |
+++ b/components/gcm_driver/instance_id/android/java/src/org/chromium/components/gcm_driver/instance_id/InstanceIDWithSubtype.java |
@@ -9,6 +9,8 @@ import android.text.TextUtils; |
import com.google.android.gms.iid.InstanceID; |
+import org.chromium.base.VisibleForTesting; |
+ |
import java.io.IOException; |
import java.util.HashMap; |
import java.util.Map; |
@@ -21,9 +23,14 @@ public class InstanceIDWithSubtype extends InstanceID { |
private final String mSubtype; |
/** Cached instances. May be accessed from multiple threads; synchronize on InstanceID.class. */ |
- private static Map<String, InstanceIDWithSubtype> sSubtypeInstances = new HashMap<>(); |
+ @VisibleForTesting |
+ protected static Map<String, InstanceIDWithSubtype> sSubtypeInstances = new HashMap<>(); |
+ |
+ /** Fake subclasses can set this so getInstance creates instances of them. */ |
+ @VisibleForTesting |
+ protected static FakeFactory sFakeFactoryForTesting = null; |
- private InstanceIDWithSubtype(Context context, String subtype) { |
+ protected InstanceIDWithSubtype(Context context, String subtype) { |
super(context, subtype, null /* options */); |
mSubtype = subtype; |
} |
@@ -41,7 +48,7 @@ public class InstanceIDWithSubtype extends InstanceID { |
// Synchronize on the base class, to match the synchronized statements in |
// InstanceID.getInstance. |
synchronized (InstanceID.class) { |
- if (sSubtypeInstances.isEmpty()) { |
+ if (sSubtypeInstances.isEmpty() && sFakeFactoryForTesting == null) { |
// The static InstanceID.getInstance method performs some one-time initialization |
// logic that is also necessary for users of this sub-class. To work around this, |
// first get (but don't use) the default InstanceID. |
@@ -50,7 +57,11 @@ public class InstanceIDWithSubtype extends InstanceID { |
InstanceIDWithSubtype existing = sSubtypeInstances.get(subtype); |
if (existing == null) { |
- existing = new InstanceIDWithSubtype(context, subtype); |
+ if (sFakeFactoryForTesting != null) { |
+ existing = sFakeFactoryForTesting.create(context, subtype); |
+ } else { |
+ existing = new InstanceIDWithSubtype(context, subtype); |
+ } |
sSubtypeInstances.put(subtype, existing); |
} |
return existing; |
@@ -69,4 +80,10 @@ public class InstanceIDWithSubtype extends InstanceID { |
public String getSubtype() { |
return mSubtype; |
} |
+ |
+ /** Fake subclasses can set {@link #sFakeFactoryForTesting} to an implementation of this. */ |
+ @VisibleForTesting |
+ public interface FakeFactory { |
+ public InstanceIDWithSubtype create(Context context, String subtype); |
+ } |
} |