Skip to content

Commit abd31d3

Browse files
committed
Update unit tests
1 parent b0a3bfc commit abd31d3

File tree

2 files changed

+42
-52
lines changed

2 files changed

+42
-52
lines changed

firebase-crashlytics/src/androidTest/java/com/google/firebase/crashlytics/internal/common/SessionReportingCoordinatorTest.java

Lines changed: 38 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@
3434
import com.google.android.gms.tasks.Task;
3535
import com.google.android.gms.tasks.Tasks;
3636
import com.google.firebase.concurrent.TestOnlyExecutors;
37+
import com.google.firebase.crashlytics.internal.CrashlyticsTestCase;
3738
import com.google.firebase.crashlytics.internal.concurrency.CrashlyticsWorkers;
3839
import com.google.firebase.crashlytics.internal.metadata.EventMetadata;
3940
import com.google.firebase.crashlytics.internal.metadata.LogFileManager;
41+
import com.google.firebase.crashlytics.internal.metadata.RolloutAssignment;
4042
import com.google.firebase.crashlytics.internal.metadata.UserMetadata;
4143
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport;
4244
import com.google.firebase.crashlytics.internal.model.CrashlyticsReport.CustomAttribute;
4345
import com.google.firebase.crashlytics.internal.persistence.CrashlyticsReportPersistence;
46+
import com.google.firebase.crashlytics.internal.persistence.FileStore;
4447
import com.google.firebase.crashlytics.internal.send.DataTransportCrashlyticsReportSender;
4548
import java.io.File;
4649
import java.util.ArrayList;
@@ -55,13 +58,14 @@
5558
import org.mockito.Mock;
5659
import org.mockito.MockitoAnnotations;
5760

58-
public class SessionReportingCoordinatorTest {
61+
public class SessionReportingCoordinatorTest extends CrashlyticsTestCase {
62+
63+
private static String TEST_SESSION_ID = "testSession1";
5964

6065
@Mock private CrashlyticsReportDataCapture dataCapture;
6166
@Mock private CrashlyticsReportPersistence reportPersistence;
6267
@Mock private DataTransportCrashlyticsReportSender reportSender;
6368
@Mock private LogFileManager logFileManager;
64-
@Mock private UserMetadata reportMetadata;
6569
@Mock private IdManager idManager;
6670
@Mock private CrashlyticsReport mockReport;
6771
@Mock private CrashlyticsReport.Session.Event mockEvent;
@@ -71,6 +75,7 @@ public class SessionReportingCoordinatorTest {
7175
@Mock private Exception mockException;
7276
@Mock private Thread mockThread;
7377

78+
private UserMetadata reportMetadata;
7479
private SessionReportingCoordinator reportingCoordinator;
7580
private AutoCloseable mocks;
7681

@@ -81,6 +86,9 @@ public class SessionReportingCoordinatorTest {
8186
public void setUp() {
8287
mocks = MockitoAnnotations.openMocks(this);
8388

89+
FileStore testFileStore = new FileStore(getContext());
90+
reportMetadata = new UserMetadata(TEST_SESSION_ID, testFileStore, crashlyticsWorkers);
91+
8492
reportingCoordinator =
8593
new SessionReportingCoordinator(
8694
dataCapture,
@@ -261,8 +269,7 @@ public void testNonFatalEvent_addsSortedKeysToEvent() throws Exception {
261269
expectedCustomAttributes.add(customAttribute1);
262270
expectedCustomAttributes.add(customAttribute2);
263271

264-
when(reportMetadata.getCustomKeys()).thenReturn(attributes);
265-
when(reportMetadata.getInternalKeys()).thenReturn(attributes);
272+
addKeysToUserMetadata(attributes);
266273

267274
reportingCoordinator.onBeginSession(sessionId, timestamp);
268275
reportingCoordinator.persistNonFatalEvent(
@@ -286,10 +293,6 @@ public void testNonFatalEvent_addsNoKeysToEventWhenNoneAvailable() throws Except
286293

287294
final String sessionId = "testSessionId";
288295

289-
final Map<String, String> attributes = Collections.emptyMap();
290-
291-
when(reportMetadata.getCustomKeys()).thenReturn(attributes);
292-
293296
reportingCoordinator.onBeginSession(sessionId, timestamp);
294297
reportingCoordinator.persistNonFatalEvent(
295298
mockException, mockThread, new EventMetadata(sessionId, timestamp));
@@ -311,9 +314,6 @@ public void testNonFatalEvent_addsUserInfoKeysToEventWhenAvailable() throws Exce
311314

312315
final String sessionId = "testSessionId";
313316

314-
final Map<String, String> attributes = Collections.emptyMap();
315-
when(reportMetadata.getCustomKeys()).thenReturn(attributes);
316-
317317
final String testKey1 = "testKey1";
318318
final String testValue1 = "testValue1";
319319

@@ -357,7 +357,7 @@ public void testNonFatalEvent_mergesUserInfoKeysWithCustomKeys() throws Exceptio
357357
attributes.put(testKey1, testValue1);
358358
attributes.put(testKey2, testValue2);
359359

360-
when(reportMetadata.getCustomKeys()).thenReturn(attributes);
360+
addKeysToUserMetadata(attributes);
361361

362362
final String testValue1UserInfo = "testValue1";
363363
final String testKey3 = "testKey3";
@@ -374,10 +374,8 @@ public void testNonFatalEvent_mergesUserInfoKeysWithCustomKeys() throws Exceptio
374374
final CustomAttribute customAttribute3 =
375375
CustomAttribute.builder().setKey(testKey3).setValue(testValue3).build();
376376

377-
final List<CustomAttribute> expectedCustomAttributes = new ArrayList<>();
378-
expectedCustomAttributes.add(customAttribute1);
379-
expectedCustomAttributes.add(customAttribute2);
380-
expectedCustomAttributes.add(customAttribute3);
377+
final List<CustomAttribute> expectedCustomAttributes =
378+
List.of(customAttribute1, customAttribute2, customAttribute3);
381379

382380
reportingCoordinator.onBeginSession(sessionId, timestamp);
383381
reportingCoordinator.persistNonFatalEvent(
@@ -398,10 +396,10 @@ public void testNonFatalEvent_addRolloutsEvent() throws Exception {
398396
String sessionId = "testSessionId";
399397
mockEventInteractions();
400398

401-
List<CrashlyticsReport.Session.Event.RolloutAssignment> rolloutsState =
402-
new ArrayList<CrashlyticsReport.Session.Event.RolloutAssignment>();
403-
rolloutsState.add(mockRolloutAssignment());
404-
when(reportMetadata.getRolloutsState()).thenReturn(rolloutsState);
399+
List<RolloutAssignment> rolloutsState = new ArrayList<>();
400+
rolloutsState.add(fakeRolloutAssignment());
401+
reportMetadata.updateRolloutsState(rolloutsState);
402+
crashlyticsWorkers.diskWrite.await();
405403

406404
reportingCoordinator.onBeginSession(sessionId, timestamp);
407405
reportingCoordinator.persistNonFatalEvent(
@@ -412,14 +410,13 @@ public void testNonFatalEvent_addRolloutsEvent() throws Exception {
412410
verify(mockEventAppBuilder, never()).setCustomAttributes(anyList());
413411
verify(mockEventAppBuilder, never()).build();
414412
verify(mockEventBuilder, never()).setApp(mockEventApp);
415-
verify(reportMetadata).getRolloutsState();
416413
// first build for custom keys
417414
// second build for rollouts
418415
verify(mockEventBuilder, times(2)).build();
419416
}
420417

421418
@Test
422-
public void testFatalEvent_addsSortedCustomKeysToEvent() {
419+
public void testFatalEvent_addsSortedCustomKeysToEvent() throws Exception {
423420
final long timestamp = System.currentTimeMillis();
424421

425422
mockEventInteractions();
@@ -444,7 +441,7 @@ public void testFatalEvent_addsSortedCustomKeysToEvent() {
444441
expectedCustomAttributes.add(customAttribute1);
445442
expectedCustomAttributes.add(customAttribute2);
446443

447-
when(reportMetadata.getCustomKeys()).thenReturn(attributes);
444+
addKeysToUserMetadata(attributes);
448445

449446
reportingCoordinator.onBeginSession(sessionId, timestamp);
450447
reportingCoordinator.persistFatalEvent(mockException, mockThread, sessionId, timestamp);
@@ -457,7 +454,7 @@ public void testFatalEvent_addsSortedCustomKeysToEvent() {
457454
}
458455

459456
@Test
460-
public void testFatalEvent_addsSortedInternalKeysToEvent() {
457+
public void testFatalEvent_addsSortedInternalKeysToEvent() throws Exception {
461458
final long timestamp = System.currentTimeMillis();
462459

463460
mockEventInteractions();
@@ -482,7 +479,7 @@ public void testFatalEvent_addsSortedInternalKeysToEvent() {
482479
expectedCustomAttributes.add(customAttribute1);
483480
expectedCustomAttributes.add(customAttribute2);
484481

485-
when(reportMetadata.getInternalKeys()).thenReturn(attributes);
482+
addKeysToUserMetadata(attributes);
486483

487484
reportingCoordinator.onBeginSession(sessionId, timestamp);
488485
reportingCoordinator.persistFatalEvent(mockException, mockThread, sessionId, timestamp);
@@ -502,10 +499,6 @@ public void testFatalEvent_addsNoKeysToEventWhenNoneAvailable() {
502499

503500
final String sessionId = "testSessionId";
504501

505-
final Map<String, String> attributes = Collections.emptyMap();
506-
507-
when(reportMetadata.getCustomKeys()).thenReturn(attributes);
508-
509502
reportingCoordinator.onBeginSession(sessionId, timestamp);
510503
reportingCoordinator.persistFatalEvent(mockException, mockThread, sessionId, timestamp);
511504

@@ -517,23 +510,23 @@ public void testFatalEvent_addsNoKeysToEventWhenNoneAvailable() {
517510
}
518511

519512
@Test
520-
public void testFatalEvent_addRolloutsToEvent() {
513+
public void testFatalEvent_addRolloutsToEvent() throws Exception {
521514
long timestamp = System.currentTimeMillis();
522515
String sessionId = "testSessionId";
523516
mockEventInteractions();
524517

525-
List<CrashlyticsReport.Session.Event.RolloutAssignment> rolloutsState =
526-
new ArrayList<CrashlyticsReport.Session.Event.RolloutAssignment>();
527-
rolloutsState.add(mockRolloutAssignment());
528-
when(reportMetadata.getRolloutsState()).thenReturn(rolloutsState);
518+
List<RolloutAssignment> rolloutsState = new ArrayList<>();
519+
rolloutsState.add(fakeRolloutAssignment());
520+
521+
reportMetadata.updateRolloutsState(rolloutsState);
522+
crashlyticsWorkers.diskWrite.await();
529523

530524
reportingCoordinator.onBeginSession(sessionId, timestamp);
531525
reportingCoordinator.persistFatalEvent(mockException, mockThread, sessionId, timestamp);
532526

533527
verify(mockEventAppBuilder, never()).setCustomAttributes(anyList());
534528
verify(mockEventAppBuilder, never()).build();
535529
verify(mockEventBuilder, never()).setApp(mockEventApp);
536-
verify(reportMetadata).getRolloutsState();
537530
// first build for custom keys
538531
// second build for rollouts
539532
verify(mockEventBuilder, times(2)).build();
@@ -618,6 +611,14 @@ public void testRemoveAllReports_deletesPersistedReports() {
618611
verify(reportPersistence).deleteAllReports();
619612
}
620613

614+
private void addKeysToUserMetadata(Map<String, String> customKeys) throws Exception {
615+
reportMetadata.setCustomKeys(customKeys);
616+
for (Map.Entry<String, String> entry : customKeys.entrySet()) {
617+
reportMetadata.setInternalKey(entry.getKey(), entry.getValue());
618+
}
619+
crashlyticsWorkers.diskWrite.await();
620+
}
621+
621622
private void mockEventInteractions() {
622623
when(mockEvent.toBuilder()).thenReturn(mockEventBuilder);
623624
when(mockEventBuilder.build()).thenReturn(mockEvent);
@@ -652,16 +653,7 @@ private static CrashlyticsReportWithSessionId mockReportWithSessionId(String ses
652653
mockReport(sessionId), sessionId, new File("fake"));
653654
}
654655

655-
private static CrashlyticsReport.Session.Event.RolloutAssignment mockRolloutAssignment() {
656-
return CrashlyticsReport.Session.Event.RolloutAssignment.builder()
657-
.setTemplateVersion(2)
658-
.setParameterKey("my_feature")
659-
.setParameterValue("false")
660-
.setRolloutVariant(
661-
CrashlyticsReport.Session.Event.RolloutAssignment.RolloutVariant.builder()
662-
.setRolloutId("rollout_1")
663-
.setVariantId("enabled")
664-
.build())
665-
.build();
656+
private static RolloutAssignment fakeRolloutAssignment() {
657+
return RolloutAssignment.create("rollout_1", "my_feature", "false", "enabled", 2);
666658
}
667659
}

firebase-crashlytics/src/main/java/com/google/firebase/crashlytics/internal/metadata/UserMetadata.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,15 +148,13 @@ public Map<String, String> getCustomKeys(Map<String, String> eventKeys) {
148148
Map<String, String> globalKeys = customKeys.getKeys();
149149
HashMap<String, String> result = new HashMap<>(globalKeys);
150150
for (Map.Entry<String, String> entry : eventKeys.entrySet()) {
151-
if (result.size() < MAX_ATTRIBUTES) {
152-
String key = KeysMap.sanitizeString(entry.getKey(), MAX_ATTRIBUTE_SIZE);
153-
String value = KeysMap.sanitizeString(entry.getValue(), MAX_ATTRIBUTE_SIZE);
154-
result.put(key, value);
155-
151+
String sanitizedKey = KeysMap.sanitizeString(entry.getKey(), MAX_ATTRIBUTE_SIZE);
152+
if (result.size() < MAX_ATTRIBUTES || result.containsKey(sanitizedKey)) {
153+
String sanitizedValue = KeysMap.sanitizeString(entry.getValue(), MAX_ATTRIBUTE_SIZE);
154+
result.put(sanitizedKey, sanitizedValue);
156155
continue;
157156
}
158157

159-
// TODO: Explore using a LinkedHashMap to overwrite keys in this case.
160158
long ignoredEventKeysCount = eventKeys.size() - (MAX_ATTRIBUTES - globalKeys.size());
161159
Logger.getLogger()
162160
.w(

0 commit comments

Comments
 (0)