Skip to content

Commit 27300b9

Browse files
committed
add test for dangling fragment behavior
1 parent 3148b6c commit 27300b9

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/application/FrameMetricsRecorder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public Optional<PerfFrameMetrics> stop() {
8989
return Optional.absent();
9090
}
9191
if (!fragmentSnapshotMap.isEmpty()) {
92-
// Instrumentation will be stopped and return activity data. Dangling fragments will fail.
92+
// Instrumentation stops and still return activity data, but invalidate all dangling fragments
9393
logger.debug(
9494
"Sub-recordings are still ongoing! Sub-recordings should be stopped first before stopping Activity screen trace.");
9595
fragmentSnapshotMap.clear();

firebase-perf/src/test/java/com/google/firebase/perf/application/FrameMetricsRecorderTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,27 @@ public void startAndStopSubTrace_whenTwoSubTracesOverlap_returnsCorrectResults()
199199
assertThat(subTrace2.get().getFrozenFrames()).isEqualTo(5 - 2);
200200
}
201201

202+
/**
203+
* This case happens when AppStateMonitor calls stop() before all Fragment traces are stopped by
204+
* stopFragment(fragment), leaving some dangling fragment traces that are invalid. Even if
205+
* activity recording starts again later, then stopFragment(fragment) is called, it should not
206+
* return a result which is incorrect.
207+
*/
208+
@Test
209+
public void
210+
startAndStopSubTrace_notContainedWithinActivityRecordingStartAndStop_returnsEmptyResult() {
211+
Fragment fragment = new Fragment();
212+
recorder.start();
213+
stubFrameMetricsAggregatorData(fma, frameTimesDefault);
214+
recorder.startFragment(fragment);
215+
stubFrameMetricsAggregatorData(fma, frameTimes1);
216+
recorder.stop();
217+
recorder.start();
218+
stubFrameMetricsAggregatorData(fma, frameTimes2);
219+
Optional<PerfFrameMetrics> result = recorder.stopFragment(fragment); // invalid dangling trace
220+
assertThat(result.isAvailable()).isFalse();
221+
}
222+
202223
private static Activity createFakeActivity(boolean isHardwareAccelerated) {
203224
ActivityController<Activity> fakeActivityController = Robolectric.buildActivity(Activity.class);
204225

0 commit comments

Comments
 (0)