diff --git a/app/src/main/java/org/mozilla/fenix/perf/StartupActivityLog.kt b/app/src/main/java/org/mozilla/fenix/perf/StartupActivityLog.kt index e841bf8a6..fe73a6db7 100644 --- a/app/src/main/java/org/mozilla/fenix/perf/StartupActivityLog.kt +++ b/app/src/main/java/org/mozilla/fenix/perf/StartupActivityLog.kt @@ -23,6 +23,10 @@ private val logger = Logger("StartupActivityLog") * A record of the [Activity] created, started, and stopped events as well as [Application] * foreground and background events. See [log] for the log. This class is expected to be * registered in [Application.onCreate] by calling [registerInAppOnCreate]. + * + * To prevent this list from growing infinitely, we clear the list when the application is stopped. + * This is acceptable from the current requirements: we never need to inspect more than the current + * start up. */ class StartupActivityLog { @@ -66,8 +70,9 @@ class StartupActivityLog { } override fun onStop(owner: LifecycleOwner) { - _log.add(LogEntry.AppStopped) logEntries() + _log.clear() // Optimization: see class kdoc for details. + _log.add(LogEntry.AppStopped) } } diff --git a/app/src/test/java/org/mozilla/fenix/perf/StartupActivityLogTest.kt b/app/src/test/java/org/mozilla/fenix/perf/StartupActivityLogTest.kt index 09c2d54c1..17005c138 100644 --- a/app/src/test/java/org/mozilla/fenix/perf/StartupActivityLogTest.kt +++ b/app/src/test/java/org/mozilla/fenix/perf/StartupActivityLogTest.kt @@ -50,15 +50,20 @@ class StartupActivityLogTest { @Test // we test start and stop individually due to the clear-on-stop behavior. fun `WHEN app observer start is called THEN it is added directly to the log`() { assertTrue(log.log.isEmpty()) - val expected = mutableListOf() appObserver.onStart(mockk()) - expected.add(LogEntry.AppStarted) - assertEquals(expected, log.log) + assertEquals(listOf(LogEntry.AppStarted), log.log) + + appObserver.onStart(mockk()) + assertEquals(listOf(LogEntry.AppStarted, LogEntry.AppStarted), log.log) + } + + @Test // we test start and stop individually due to the clear-on-stop behavior. + fun `WHEN app observer stop is called THEN it is added directly to the log`() { + assertTrue(log.log.isEmpty()) appObserver.onStop(mockk()) - expected.add(LogEntry.AppStopped) - assertEquals(expected, log.log) + assertEquals(listOf(LogEntry.AppStopped), log.log) } @Test @@ -81,6 +86,19 @@ class StartupActivityLogTest { assertEquals(expected, log.log) } + @Test + fun `WHEN app STOPPED is called THEN the log is emptied expect for the stop event`() { + assertTrue(log.log.isEmpty()) + + activityCallbacks.onActivityCreated(mockk(), null) + activityCallbacks.onActivityStarted(mockk()) + appObserver.onStart(mockk()) + assertEquals(3, log.log.size) + + appObserver.onStop(mockk()) + assertEquals(listOf(LogEntry.AppStopped), log.log) + } + @Test fun `GIVEN debug log level WHEN logEntries is called THEN there is no logcat call`() { log.logEntries(logger, Priority.DEBUG)