|
38 | 38 | import com.google.firebase.crashlytics.internal.settings.model.SettingsData;
|
39 | 39 | import com.google.firebase.crashlytics.internal.unity.UnityVersionProvider;
|
40 | 40 | import com.google.firebase.installations.FirebaseInstallationsApi;
|
| 41 | +import java.util.HashMap; |
| 42 | +import java.util.Map; |
41 | 43 | import java.util.concurrent.TimeUnit;
|
42 | 44 | import org.mockito.Mockito;
|
43 | 45 |
|
@@ -125,6 +127,126 @@ public void testCustomAttributes() throws Exception {
|
125 | 127 | assertEquals(longValue, metadata.getCustomKeys().get(key1));
|
126 | 128 | }
|
127 | 129 |
|
| 130 | + public void testBulkCustomKeys() throws Exception { |
| 131 | + final double DELTA = 1e-15; |
| 132 | + |
| 133 | + UserMetadata metadata = crashlyticsCore.getController().getUserMetadata(); |
| 134 | + |
| 135 | + final String stringKey = "string key"; |
| 136 | + final String stringValue = "value1"; |
| 137 | + final String trimmedKey = "trimmed key"; |
| 138 | + final String trimmedValue = "trimmed value"; |
| 139 | + |
| 140 | + final StringBuffer idBuffer = new StringBuffer("id012345"); |
| 141 | + while (idBuffer.length() < UserMetadata.MAX_ATTRIBUTE_SIZE) { |
| 142 | + idBuffer.append("0"); |
| 143 | + } |
| 144 | + final String longId = idBuffer.toString(); |
| 145 | + final String superLongId = longId + "more chars"; |
| 146 | + final String longStringValue = longId.replaceAll("0", "x"); |
| 147 | + final String superLongValue = longStringValue + "some more chars"; |
| 148 | + |
| 149 | + final String booleanKey = "boolean key"; |
| 150 | + final Boolean booleanValue = true; |
| 151 | + |
| 152 | + final String doubleKey = "double key"; |
| 153 | + final double doubleValue = 1.000000000000001; |
| 154 | + |
| 155 | + final String floatKey = "float key"; |
| 156 | + final float floatValue = 2.000002f; |
| 157 | + |
| 158 | + final String longKey = "long key"; |
| 159 | + final long longValue = 3; |
| 160 | + |
| 161 | + final String intKey = "int key"; |
| 162 | + final int intValue = 4; |
| 163 | + |
| 164 | + Map<String, String> keysAndValues = new HashMap<>(); |
| 165 | + keysAndValues.put(stringKey, stringValue); |
| 166 | + keysAndValues.put(" " + trimmedKey + " ", " " + trimmedValue + " "); |
| 167 | + keysAndValues.put(longId, longStringValue); |
| 168 | + keysAndValues.put(superLongId, superLongValue); |
| 169 | + keysAndValues.put(booleanKey, booleanValue.toString()); |
| 170 | + keysAndValues.put(doubleKey, String.valueOf(doubleValue)); |
| 171 | + keysAndValues.put(floatKey, String.valueOf(floatValue)); |
| 172 | + keysAndValues.put(longKey, String.valueOf(longValue)); |
| 173 | + keysAndValues.put(intKey, String.valueOf(intValue)); |
| 174 | + |
| 175 | + crashlyticsCore.setCustomKeys(keysAndValues); |
| 176 | + |
| 177 | + assertEquals(stringValue, metadata.getCustomKeys().get(stringKey)); |
| 178 | + assertEquals(trimmedValue, metadata.getCustomKeys().get(trimmedKey)); |
| 179 | + assertEquals(longStringValue, metadata.getCustomKeys().get(longId)); |
| 180 | + // Test truncation of custom keys and attributes |
| 181 | + assertNull(metadata.getCustomKeys().get(superLongId)); |
| 182 | + assertTrue(Boolean.parseBoolean(metadata.getCustomKeys().get(booleanKey))); |
| 183 | + assertEquals(doubleValue, Double.parseDouble(metadata.getCustomKeys().get(doubleKey)), DELTA); |
| 184 | + assertEquals(floatValue, Float.parseFloat(metadata.getCustomKeys().get(floatKey)), DELTA); |
| 185 | + assertEquals(longValue, Long.parseLong(metadata.getCustomKeys().get(longKey)), DELTA); |
| 186 | + assertEquals(intValue, Integer.parseInt(metadata.getCustomKeys().get(intKey)), DELTA); |
| 187 | + |
| 188 | + // Add the max number of attributes (already set 8) |
| 189 | + Map<String, String> addlKeysAndValues = new HashMap<>(); |
| 190 | + for (int i = 8; i < UserMetadata.MAX_ATTRIBUTES; ++i) { |
| 191 | + final String key = "key" + i; |
| 192 | + final String value = "value" + i; |
| 193 | + addlKeysAndValues.put(key, value); |
| 194 | + } |
| 195 | + crashlyticsCore.setCustomKeys(addlKeysAndValues); |
| 196 | + |
| 197 | + // Ensure all keys have been set |
| 198 | + assertEquals(UserMetadata.MAX_ATTRIBUTES, metadata.getCustomKeys().size(), DELTA); |
| 199 | + |
| 200 | + // Make sure the first MAX_ATTRIBUTES - 8 keys were set |
| 201 | + for (int i = 8; i < UserMetadata.MAX_ATTRIBUTES + 1; ++i) { |
| 202 | + final String key = "key" + i; |
| 203 | + final String value = "value" + i; |
| 204 | + } |
| 205 | + |
| 206 | + Map<String, String> extraKeysAndValues = new HashMap<>(); |
| 207 | + for (int i = UserMetadata.MAX_ATTRIBUTES; i < UserMetadata.MAX_ATTRIBUTES + 10; ++i) { |
| 208 | + final String key = "key" + i; |
| 209 | + final String value = "value" + i; |
| 210 | + extraKeysAndValues.put(key, value); |
| 211 | + } |
| 212 | + crashlyticsCore.setCustomKeys(extraKeysAndValues); |
| 213 | + |
| 214 | + // Make sure the extra keys were not added |
| 215 | + for (int i = UserMetadata.MAX_ATTRIBUTES; i < UserMetadata.MAX_ATTRIBUTES + 10; ++i) { |
| 216 | + final String key = "key" + i; |
| 217 | + assertFalse(metadata.getCustomKeys().containsKey(key)); |
| 218 | + } |
| 219 | + |
| 220 | + // Check updating existing keys and setting to null |
| 221 | + final String updatedStringValue = "string value 1"; |
| 222 | + final boolean updatedBooleanValue = false; |
| 223 | + final double updatedDoubleValue = -1.000000000000001; |
| 224 | + final float updatedFloatValue = -2.000002f; |
| 225 | + final long updatedLongValue = -3; |
| 226 | + final int updatedIntValue = -4; |
| 227 | + |
| 228 | + Map<String, String> updatedKeysAndValues = new HashMap<>(); |
| 229 | + updatedKeysAndValues.put(stringKey, updatedStringValue); |
| 230 | + updatedKeysAndValues.put(longId, null); |
| 231 | + updatedKeysAndValues.put(booleanKey, String.valueOf(updatedBooleanValue)); |
| 232 | + updatedKeysAndValues.put(doubleKey, String.valueOf(updatedDoubleValue)); |
| 233 | + updatedKeysAndValues.put(floatKey, String.valueOf(updatedFloatValue)); |
| 234 | + updatedKeysAndValues.put(longKey, String.valueOf(updatedLongValue)); |
| 235 | + updatedKeysAndValues.put(intKey, String.valueOf(updatedIntValue)); |
| 236 | + |
| 237 | + crashlyticsCore.setCustomKeys(updatedKeysAndValues); |
| 238 | + |
| 239 | + assertEquals(updatedStringValue, metadata.getCustomKeys().get(stringKey)); |
| 240 | + assertFalse(Boolean.parseBoolean(metadata.getCustomKeys().get(booleanKey))); |
| 241 | + assertEquals( |
| 242 | + updatedDoubleValue, Double.parseDouble(metadata.getCustomKeys().get(doubleKey)), DELTA); |
| 243 | + assertEquals( |
| 244 | + updatedFloatValue, Float.parseFloat(metadata.getCustomKeys().get(floatKey)), DELTA); |
| 245 | + assertEquals(updatedLongValue, Long.parseLong(metadata.getCustomKeys().get(longKey)), DELTA); |
| 246 | + assertEquals(updatedIntValue, Integer.parseInt(metadata.getCustomKeys().get(intKey)), DELTA); |
| 247 | + assertEquals("", metadata.getCustomKeys().get(longId)); |
| 248 | + } |
| 249 | + |
128 | 250 | public void testGetVersion() {
|
129 | 251 | assertFalse(TextUtils.isEmpty(CrashlyticsCore.getVersion()));
|
130 | 252 | assertFalse(CrashlyticsCore.getVersion().equalsIgnoreCase("version"));
|
|
0 commit comments