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..9c8da3e408adb89204d6f0b284ffa8b7ce2be56c 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,9 @@ import android.text.TextUtils; |
import com.google.android.gms.iid.InstanceID; |
+import org.chromium.base.VisibleForTesting; |
+import org.chromium.base.annotations.SuppressFBWarnings; |
+ |
import java.io.IOException; |
import java.util.HashMap; |
import java.util.Map; |
@@ -21,9 +24,16 @@ 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 |
+ @SuppressFBWarnings("MS_MUTABLE_COLLECTION_PKGPROTECT") |
+ protected static final Map<String, InstanceIDWithSubtype> sSubtypeInstances = new HashMap<>(); |
+ |
+ /** Fake subclasses can set this so getInstance creates instances of them. */ |
+ @VisibleForTesting |
+ @SuppressFBWarnings("MS_SHOULD_BE_FINAL") |
+ 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 +51,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 +60,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 +83,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); |
+ } |
} |