Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 03e5696f authored by mpodolian's avatar mpodolian Committed by Mykola Podolian
Browse files

Altered log formatting of BubbleEventHistoryLogger.

Bug: 436320481
Flag: EXEMPT not hooked up
Test: BubbleEventHistoryLoggerTest
Change-Id: I6f7389b081d03c164688731b304b1b682809090e
parent 193afd3d
Loading
Loading
Loading
Loading
+2 −1
Original line number Original line Diff line number Diff line
@@ -76,13 +76,14 @@ class BubbleEventHistoryLogger : DebugLogger {
            if (!event.eventData.isNullOrBlank()) {
            if (!event.eventData.isNullOrBlank()) {
                eventData = " | ${event.eventData}"
                eventData = " | ${event.eventData}"
            }
            }
            pw.println("$prefix  $eventFormattedTime ${event.title} $eventData)")
            pw.println("$prefix  $eventFormattedTime ${event.title}$eventData")
        }
        }
    }
    }


    companion object {
    companion object {
        const val DATE_FORMAT = "MM-dd HH:mm:ss.SSS"
        const val DATE_FORMAT = "MM-dd HH:mm:ss.SSS"
        const val MAX_EVENTS: Int = 25
        const val MAX_EVENTS: Int = 25
        @VisibleForTesting
        val DATE_FORMATTER = SimpleDateFormat(DATE_FORMAT, Locale.US)
        val DATE_FORMATTER = SimpleDateFormat(DATE_FORMAT, Locale.US)
    }
    }
}
}
+29 −21
Original line number Original line Diff line number Diff line
@@ -18,17 +18,18 @@ package com.android.wm.shell.shared.bubbles.logging


import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import androidx.test.filters.SmallTest
import com.android.wm.shell.shared.bubbles.logging.BubbleEventHistoryLogger.Companion.DATE_FORMAT
import com.android.wm.shell.shared.bubbles.logging.BubbleEventHistoryLogger.Companion.DATE_FORMATTER
import com.android.wm.shell.shared.bubbles.logging.BubbleEventHistoryLogger.Companion.DATE_FORMATTER
import com.android.wm.shell.shared.bubbles.logging.BubbleEventHistoryLogger.Companion.MAX_EVENTS
import com.android.wm.shell.shared.bubbles.logging.BubbleEventHistoryLogger.Companion.MAX_EVENTS
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertThat
import java.io.PrintWriter
import java.io.PrintWriter
import java.io.StringWriter
import java.io.StringWriter
import kotlin.text.split
import org.junit.Test
import org.junit.runner.RunWith
import java.util.concurrent.CountDownLatch
import java.util.concurrent.CountDownLatch
import java.util.concurrent.Executors
import java.util.concurrent.Executors
import java.util.concurrent.TimeUnit
import java.util.concurrent.TimeUnit
import kotlin.text.split
import org.junit.Test
import org.junit.runner.RunWith


/** Unit tests for [BubbleEventHistoryLogger]. */
/** Unit tests for [BubbleEventHistoryLogger]. */
@SmallTest
@SmallTest
@@ -45,7 +46,21 @@ class BubbleEventHistoryLoggerTest {
    }
    }


    @Test
    @Test
    fun dump_RespectsMAX_EVENTS() {
    fun dump_printsHeaderWithEvents() {
        val timestamp = System.currentTimeMillis()
        val formattedTimeStamp = DATE_FORMATTER.format(timestamp)
        logger.logEvent("e: test", "eventData", timestamp)
        logger.logEvent("d: hey", timestamp = timestamp)
        val expectedOutput = """
            Bubbles events history:
              $formattedTimeStamp d: hey
              $formattedTimeStamp e: test | eventData
            """.trimIndent() + "\n"
        assertThat(getDumpOutput()).isEqualTo(expectedOutput)
    }

    @Test
    fun dump_respectsMAX_EVENTS() {
        repeat(MAX_EVENTS + 10) { logger.d(message = "title") }
        repeat(MAX_EVENTS + 10) { logger.d(message = "title") }
        val linesCount = getTrimmedLogLines().size
        val linesCount = getTrimmedLogLines().size


@@ -53,7 +68,7 @@ class BubbleEventHistoryLoggerTest {
    }
    }


    @Test
    @Test
    fun dump_PrintsEventsInReverseChronologicalOrderStartingFromTheMostRecentEvent() {
    fun dump_printsEventsInReverseChronologicalOrderStartingFromTheMostRecentEvent() {
        val repetitions = MAX_EVENTS * 2
        val repetitions = MAX_EVENTS * 2
        repeat(repetitions) { repetition ->
        repeat(repetitions) { repetition ->
            logger.logEvent(title = "", timestamp = repetition.toLong())
            logger.logEvent(title = "", timestamp = repetition.toLong())
@@ -77,11 +92,11 @@ class BubbleEventHistoryLoggerTest {


        val logLines = getTrimmedLogLines()
        val logLines = getTrimmedLogLines()


        assertThat(checkLogFormat(logLines[4], 'd', "test true", "eventData")).isTrue()
        assertLogFormat(logLines[4], "d: test true | eventData")
        assertThat(checkLogFormat(logLines[3], 'v', "test 0", "eventData")).isTrue()
        assertLogFormat(logLines[3], "v: test 0 | eventData")
        assertThat(checkLogFormat(logLines[2], 'i', "test stringArgument", "eventData")).isTrue()
        assertLogFormat(logLines[2], "i: test stringArgument | eventData")
        assertThat(checkLogFormat(logLines[1], 'w', "test")).isTrue()
        assertLogFormat(logLines[1], "w: test")
        assertThat(checkLogFormat(logLines[0], 'e', "test", "eventData")).isTrue()
        assertLogFormat(logLines[0], "e: test | eventData")
    }
    }


    @Test
    @Test
@@ -113,17 +128,10 @@ class BubbleEventHistoryLoggerTest {
        assertThat(logLinesCount).isEqualTo(MAX_EVENTS)
        assertThat(logLinesCount).isEqualTo(MAX_EVENTS)
    }
    }


    private fun checkLogFormat(
    private fun assertLogFormat(logEntry: String, expectedLogWithoutDate: String) {
        logEntry: String,
        assertThat(logEntry.matches(logPattern)).isTrue()
        logLevel: Char,
        val trimmedDateTime = logEntry.substring(DATE_FORMAT.length + 1)
        title: String,
        assertThat(trimmedDateTime).isEqualTo(expectedLogWithoutDate)
        eventData: String? = null,
    ): Boolean {
        val matchesEventData = eventData.isNullOrBlank() || logEntry.contains("| $eventData")
        return logEntry.matches(logPattern) &&
            logEntry.contains(logLevel) &&
            logEntry.contains(title) &&
            matchesEventData
    }
    }


    private fun getTrimmedLogLines(): List<String> {
    private fun getTrimmedLogLines(): List<String> {