Use Timestamp objects rather than Longs

Bug: 267482586
Test: atest FlickerLibTest
Change-Id: Ic29f8bbce5172bb5baacaaf5b5fd26fa3b86aea8
diff --git a/libraries/flicker/src/com/android/server/wm/flicker/io/ResultReader.kt b/libraries/flicker/src/com/android/server/wm/flicker/io/ResultReader.kt
index c7d113b..041a933 100644
--- a/libraries/flicker/src/com/android/server/wm/flicker/io/ResultReader.kt
+++ b/libraries/flicker/src/com/android/server/wm/flicker/io/ResultReader.kt
@@ -21,6 +21,7 @@
 import com.android.server.wm.flicker.AssertionTag
 import com.android.server.wm.flicker.TraceConfig
 import com.android.server.wm.flicker.TraceConfigs
+import com.android.server.wm.traces.common.Timestamp
 import com.android.server.wm.traces.common.events.CujTrace
 import com.android.server.wm.traces.common.events.EventLog
 import com.android.server.wm.traces.common.layers.LayersTrace
@@ -140,8 +141,8 @@
                 WindowManagerTraceParser()
                     .parse(
                         it,
-                        from = transitionTimeRange.start.elapsedNanos,
-                        to = transitionTimeRange.end.elapsedNanos,
+                        from = transitionTimeRange.start,
+                        to = transitionTimeRange.end,
                         addInitialEntry = true,
                         clearCache = true
                     )
@@ -149,8 +150,8 @@
             require(trace.entries.size >= minimumEntries) {
                 "WM trace contained ${trace.entries.size} entries, " +
                     "expected at least $minimumEntries... :: " +
-                    "transition starts at ${transitionTimeRange.start.elapsedNanos} and " +
-                    "ends at ${transitionTimeRange.end.elapsedNanos}."
+                    "transition starts at ${transitionTimeRange.start} and " +
+                    "ends at ${transitionTimeRange.end}."
             }
             trace
         }
@@ -168,8 +169,8 @@
                 LayersTraceParser()
                     .parse(
                         it,
-                        transitionTimeRange.start.systemUptimeNanos,
-                        transitionTimeRange.end.systemUptimeNanos,
+                        transitionTimeRange.start,
+                        transitionTimeRange.end,
                         addInitialEntry = true,
                         clearCache = true
                     )
@@ -177,8 +178,8 @@
             require(trace.entries.size >= minimumEntries) {
                 "Layers trace contained ${trace.entries.size} entries, " +
                     "expected at least $minimumEntries... :: " +
-                    "transition starts at ${transitionTimeRange.start.systemUptimeNanos} and " +
-                    "ends at ${transitionTimeRange.end.systemUptimeNanos}."
+                    "transition starts at ${transitionTimeRange.start} and " +
+                    "ends at ${transitionTimeRange.end}."
             }
             trace
         }
@@ -201,12 +202,9 @@
      */
     @Throws(IOException::class)
     override fun readTransactionsTrace(): TransactionsTrace? =
-        doReadTransactionsTrace(
-            from = transitionTimeRange.start.systemUptimeNanos,
-            to = transitionTimeRange.end.systemUptimeNanos
-        )
+        doReadTransactionsTrace(from = transitionTimeRange.start, to = transitionTimeRange.end)
 
-    private fun doReadTransactionsTrace(from: Long, to: Long): TransactionsTrace? {
+    private fun doReadTransactionsTrace(from: Timestamp, to: Timestamp): TransactionsTrace? {
         val traceData = readFromZip(ResultArtifactDescriptor(TraceType.TRANSACTION))
         return traceData?.let {
             val trace = TransactionsTraceParser().parse(it, from, to, addInitialEntry = true)
@@ -221,18 +219,14 @@
      */
     @Throws(IOException::class)
     override fun readTransitionsTrace(): TransitionsTrace? {
-        val transactionsTrace = doReadTransactionsTrace(Long.MIN_VALUE, Long.MAX_VALUE)
+        val transactionsTrace = doReadTransactionsTrace(Timestamp.MIN, Timestamp.MAX)
         val traceData = readFromZip(ResultArtifactDescriptor(TraceType.TRANSITION))
         if (transactionsTrace == null || traceData == null) {
             return null
         }
 
         val fullTrace = TransitionsTraceParser(transactionsTrace).parse(traceData)
-        val trace =
-            fullTrace.sliceElapsed(
-                transitionTimeRange.start.elapsedNanos,
-                transitionTimeRange.end.elapsedNanos
-            )
+        val trace = fullTrace.slice(transitionTimeRange.start, transitionTimeRange.end)
         if (!traceConfig.transitionsTrace.allowNoChange) {
             require(trace.entries.isNotEmpty()) { "Transitions trace cannot be empty" }
         }
@@ -252,11 +246,7 @@
         val descriptor = ResultArtifactDescriptor(TraceType.EVENT_LOG)
         return readFromZip(descriptor)?.let {
             EventLogParser()
-                .parse(
-                    it,
-                    from = transitionTimeRange.start.unixNanos,
-                    to = transitionTimeRange.end.unixNanos
-                )
+                .parse(it, from = transitionTimeRange.start, to = transitionTimeRange.end)
         }
     }
 
@@ -267,6 +257,18 @@
     @Throws(IOException::class)
     override fun readCujTrace(): CujTrace? = readEventLogTrace()?.cujTrace
 
+    /** @return an [IReader] for the subsection of the trace we are reading in this reader */
+    override fun slice(startTimestamp: Timestamp, endTimestamp: Timestamp): ResultReader {
+        val slicedResult =
+            ResultData(
+                result.artifactPath,
+                TransitionTimeRange(startTimestamp, endTimestamp),
+                result.executionError,
+                result.runStatus
+            )
+        return ResultReader(slicedResult, traceConfig)
+    }
+
     override fun toString(): String = "$result"
 
     /** @return the number of files in the artifact */
diff --git a/libraries/flicker/src/com/android/server/wm/traces/common/Timestamp.kt b/libraries/flicker/src/com/android/server/wm/traces/common/Timestamp.kt
index 3635b41..245ab10 100644
--- a/libraries/flicker/src/com/android/server/wm/traces/common/Timestamp.kt
+++ b/libraries/flicker/src/com/android/server/wm/traces/common/Timestamp.kt
@@ -29,6 +29,10 @@
     val unixNanos: Long = 0
 ) : Comparable<Timestamp> {
 
+    val hasUnixTimestamp: Boolean = unixNanos != NULL_TIMESTAMP
+
+    // TODO: Might also make sense to try and compare with elapsed and system first and fallback on
+    // unix
     override fun compareTo(other: Timestamp): Int {
         // Comparing UNIX timestamp is more accurate
         if (this.unixNanos != NULL_TIMESTAMP && other.unixNanos != NULL_TIMESTAMP) {
@@ -69,7 +73,7 @@
 
         const val NULL_TIMESTAMP = 0L
         val EMPTY = Timestamp(NULL_TIMESTAMP, NULL_TIMESTAMP, NULL_TIMESTAMP)
-        val MIN = EMPTY
+        val MIN = Timestamp(1, 1, 1)
         val MAX = Timestamp(Long.MAX_VALUE, Long.MAX_VALUE, Long.MAX_VALUE)
     }
 }
diff --git a/libraries/flicker/src/com/android/server/wm/traces/common/Utils.kt b/libraries/flicker/src/com/android/server/wm/traces/common/Utils.kt
index b0467bb..c70678c 100644
--- a/libraries/flicker/src/com/android/server/wm/traces/common/Utils.kt
+++ b/libraries/flicker/src/com/android/server/wm/traces/common/Utils.kt
@@ -18,11 +18,11 @@
 
 object Utils {
     fun getTimestampsInRange(
-        entries: List<Long>,
-        from: Long,
-        to: Long,
+        entries: List<Timestamp>,
+        from: Timestamp,
+        to: Timestamp,
         addInitialEntry: Boolean
-    ): Set<Long> {
+    ): Set<Timestamp> {
         require(from <= to) { "`from` must be smaller or equal to `to` but was $from and $to" }
 
         return when {
diff --git a/libraries/flicker/src/com/android/server/wm/traces/common/transition/Transition.kt b/libraries/flicker/src/com/android/server/wm/traces/common/transition/Transition.kt
index 223487e..8954fe7 100644
--- a/libraries/flicker/src/com/android/server/wm/traces/common/transition/Transition.kt
+++ b/libraries/flicker/src/com/android/server/wm/traces/common/transition/Transition.kt
@@ -25,17 +25,16 @@
 
 class Transition(
     @JsName("type") val type: Type,
-    @JsName("start") val start: Long,
-    @JsName("end") val end: Long,
-    @JsName("collectingStart") val collectingStart: Long,
+    @JsName("start") val start: Timestamp,
+    @JsName("end") val end: Timestamp,
+    @JsName("collectingStart") val collectingStart: Timestamp,
     @JsName("startTransaction") val startTransaction: Transaction?,
     @JsName("finishTransaction") val finishTransaction: Transaction?,
     @JsName("changes") val changes: List<TransitionChange>,
     @JsName("played") val played: Boolean,
     @JsName("aborted") val aborted: Boolean
 ) : ITraceEntry {
-    // TODO: Dump other timestamps for this trace
-    override val timestamp = Timestamp(elapsedNanos = start)
+    override val timestamp = start
 
     @JsName("isIncomplete")
     val isIncomplete: Boolean
@@ -43,9 +42,9 @@
 
     override fun toString(): String =
         "Transition#${hashCode()}" +
-            "($type, aborted=$aborted, start=$start, end=$end," +
-            "startTransaction=$startTransaction, finishTransaction=$finishTransaction, " +
-            "changes=[${changes.joinToString()}])"
+            "(\n$type,\naborted=$aborted,\nstart=$start,\nend=$end,\n" +
+            "startTransaction=$startTransaction,\nfinishTransaction=$finishTransaction,\n" +
+            "changes=[\n${changes.joinToString(",\n").prependIndent()}\n])"
 
     companion object {
         enum class Type(val value: Int) {
@@ -66,15 +65,15 @@
             FIRST_CUSTOM(12); // TODO: Add custom types we know about
 
             companion object {
-                fun fromInt(value: Int) = values().first { it.value == value }
+                @JsName("fromInt") fun fromInt(value: Int) = values().first { it.value == value }
             }
         }
 
         class Builder(val id: Int) {
             @JsName("type") var type: Type = Type.UNDEFINED
-            @JsName("start") var start: Long = -1
-            @JsName("end") var end: Long = -1
-            @JsName("collectingStart") var collectingStart: Long = -1
+            @JsName("start") var start: Timestamp = Timestamp.EMPTY
+            @JsName("end") var end: Timestamp = Timestamp.EMPTY
+            @JsName("collectingStart") var collectingStart: Timestamp = Timestamp.EMPTY
             @JsName("changes") var changes: List<TransitionChange> = emptyList()
             @JsName("played") var played = false
             @JsName("aborted") var aborted = false
@@ -153,14 +152,16 @@
             }
 
             @JsName("build")
-            fun build(): Transition {
-                val startTransaction = startTransaction
-                require(startTransaction != null || !played) {
-                    "Can't build played transition without matched start transaction"
-                }
-                val finishTransaction = finishTransaction
-                require(finishTransaction != null || aborted) {
-                    "Can't build non-aborted transition without matched finish transaction"
+            fun build(withoutTransactionLinking: Boolean = false): Transition {
+                if (!withoutTransactionLinking) {
+                    val startTransaction = startTransaction
+                    require(startTransaction != null || !played) {
+                        "Can't build played transition without matched start transaction"
+                    }
+                    val finishTransaction = finishTransaction
+                    require(finishTransaction != null || aborted) {
+                        "Can't build non-aborted transition without matched finish transaction"
+                    }
                 }
                 return Transition(
                     type,
diff --git a/libraries/flicker/src/com/android/server/wm/traces/common/transition/TransitionState.kt b/libraries/flicker/src/com/android/server/wm/traces/common/transition/TransitionState.kt
index 40f0c00..f1db925 100644
--- a/libraries/flicker/src/com/android/server/wm/traces/common/transition/TransitionState.kt
+++ b/libraries/flicker/src/com/android/server/wm/traces/common/transition/TransitionState.kt
@@ -16,12 +16,13 @@
 
 package com.android.server.wm.traces.common.transition
 
+import com.android.server.wm.traces.common.Timestamp
 import kotlin.js.JsName
 
 data class TransitionState(
     @JsName("id") val id: Int,
     @JsName("type") val type: Transition.Companion.Type,
-    @JsName("timestamp") val timestamp: Long,
+    @JsName("timestamp") val timestamp: Timestamp,
     @JsName("state") val state: State,
     @JsName("flags") val flags: Int,
     @JsName("changes") val changes: List<TransitionChange>,
diff --git a/libraries/flicker/src/com/android/server/wm/traces/parser/AbstractTraceParser.kt b/libraries/flicker/src/com/android/server/wm/traces/parser/AbstractTraceParser.kt
index 4302131..cfdc45d 100644
--- a/libraries/flicker/src/com/android/server/wm/traces/parser/AbstractTraceParser.kt
+++ b/libraries/flicker/src/com/android/server/wm/traces/parser/AbstractTraceParser.kt
@@ -17,6 +17,7 @@
 package com.android.server.wm.traces.parser
 
 import com.android.server.wm.traces.common.Cache
+import com.android.server.wm.traces.common.Timestamp
 import com.android.server.wm.traces.common.Utils
 import com.android.server.wm.traces.common.parser.AbstractParser
 
@@ -26,7 +27,7 @@
     AbstractParser<InputTypeTrace, OutputTypeTrace>() {
     protected abstract fun onBeforeParse(input: InputTypeTrace)
     protected abstract fun getEntries(input: InputTypeTrace): List<InputTypeEntry>
-    protected abstract fun getTimestamp(entry: InputTypeEntry): Long
+    protected abstract fun getTimestamp(entry: InputTypeEntry): Timestamp
     protected abstract fun doParseEntry(entry: InputTypeEntry): OutputTypeEntry
     protected abstract fun createTrace(entries: List<OutputTypeEntry>): OutputTypeTrace
 
@@ -35,8 +36,8 @@
     override fun parse(bytes: ByteArray, clearCache: Boolean): OutputTypeTrace {
         return parse(
             bytes,
-            from = Long.MIN_VALUE,
-            to = Long.MAX_VALUE,
+            from = Timestamp.MIN,
+            to = Timestamp.MAX,
             addInitialEntry = true,
             clearCache = clearCache
         )
@@ -45,15 +46,15 @@
     override fun parse(input: InputTypeTrace, clearCache: Boolean): OutputTypeTrace {
         return parse(
             input,
-            from = Long.MIN_VALUE,
-            to = Long.MAX_VALUE,
+            from = Timestamp.MIN,
+            to = Timestamp.MAX,
             addInitialEntry = true,
             clearCache = clearCache
         )
     }
 
     override fun doParse(input: InputTypeTrace): OutputTypeTrace {
-        return doParse(input, from = Long.MIN_VALUE, to = Long.MAX_VALUE, addInitialEntry = true)
+        return doParse(input, from = Timestamp.MIN, to = Timestamp.MAX, addInitialEntry = true)
     }
 
     /**
@@ -66,16 +67,16 @@
      */
     private fun doParse(
         input: InputTypeTrace,
-        from: Long,
-        to: Long,
+        from: Timestamp,
+        to: Timestamp,
         addInitialEntry: Boolean
     ): OutputTypeTrace {
+        onBeforeParse(input)
         val parsedEntries = mutableListOf<OutputTypeEntry>()
         val rawEntries = getEntries(input)
         val allInputTimestamps = rawEntries.map { getTimestamp(it) }
         val selectedInputTimestamps =
             Utils.getTimestampsInRange(allInputTimestamps, from, to, addInitialEntry)
-        onBeforeParse(input)
         for (rawEntry in rawEntries) {
             val currTimestamp = getTimestamp(rawEntry)
             if (!selectedInputTimestamps.contains(currTimestamp) || !shouldParseEntry(rawEntry)) {
@@ -98,8 +99,8 @@
      */
     fun parse(
         input: InputTypeTrace,
-        from: Long,
-        to: Long,
+        from: Timestamp,
+        to: Timestamp,
         addInitialEntry: Boolean = true,
         clearCache: Boolean = true
     ): OutputTypeTrace {
@@ -125,8 +126,8 @@
      */
     fun parse(
         bytes: ByteArray,
-        from: Long,
-        to: Long,
+        from: Timestamp,
+        to: Timestamp,
         addInitialEntry: Boolean = true,
         clearCache: Boolean = true
     ): OutputTypeTrace {
diff --git a/libraries/flicker/src/com/android/server/wm/traces/parser/layers/LayersTraceParser.kt b/libraries/flicker/src/com/android/server/wm/traces/parser/layers/LayersTraceParser.kt
index 2d9c905..45001f0 100644
--- a/libraries/flicker/src/com/android/server/wm/traces/parser/layers/LayersTraceParser.kt
+++ b/libraries/flicker/src/com/android/server/wm/traces/parser/layers/LayersTraceParser.kt
@@ -25,6 +25,7 @@
 import com.android.server.wm.traces.common.Rect
 import com.android.server.wm.traces.common.RectF
 import com.android.server.wm.traces.common.Size
+import com.android.server.wm.traces.common.Timestamp
 import com.android.server.wm.traces.common.layers.HwcCompositionType
 import com.android.server.wm.traces.common.layers.Layer
 import com.android.server.wm.traces.common.layers.LayerTraceEntry
@@ -42,7 +43,7 @@
     AbstractTraceParser<
         Layerstrace.LayersTraceFileProto, Layerstrace.LayersTraceProto, LayerTraceEntry, LayersTrace
     >() {
-    private var realToElapsedTimeOffsetNanos = 0L
+    private var realToElapsedTimeOffsetNanos = Timestamp.NULL_TIMESTAMP
 
     override val traceName: String = "Layers Trace"
 
@@ -56,8 +57,13 @@
         input: Layerstrace.LayersTraceFileProto
     ): List<Layerstrace.LayersTraceProto> = input.entryList
 
-    override fun getTimestamp(entry: Layerstrace.LayersTraceProto): Long =
-        entry.elapsedRealtimeNanos
+    override fun getTimestamp(entry: Layerstrace.LayersTraceProto): Timestamp {
+        require(realToElapsedTimeOffsetNanos != Timestamp.NULL_TIMESTAMP)
+        return Timestamp(
+            elapsedNanos = entry.elapsedRealtimeNanos,
+            unixNanos = entry.elapsedRealtimeNanos + realToElapsedTimeOffsetNanos
+        )
+    }
 
     override fun onBeforeParse(input: Layerstrace.LayersTraceFileProto) {
         realToElapsedTimeOffsetNanos = input.realToElapsedTimeOffsetNanos
diff --git a/libraries/flicker/src/com/android/server/wm/traces/parser/transaction/TransactionsTraceParser.kt b/libraries/flicker/src/com/android/server/wm/traces/parser/transaction/TransactionsTraceParser.kt
index fd7de92..c72c534 100644
--- a/libraries/flicker/src/com/android/server/wm/traces/parser/transaction/TransactionsTraceParser.kt
+++ b/libraries/flicker/src/com/android/server/wm/traces/parser/transaction/TransactionsTraceParser.kt
@@ -34,15 +34,20 @@
         TransactionsTraceEntry,
         TransactionsTrace
     >() {
-    private var timestampOffset = 0L
+    private var timestampOffset = Timestamp.NULL_TIMESTAMP
     override val traceName: String = "Transactions trace"
 
     override fun onBeforeParse(input: TransactionTraceFile) {
         timestampOffset = input.realToElapsedTimeOffsetNanos
     }
 
-    override fun getTimestamp(entry: Transactions.TransactionTraceEntry): Long =
-        entry.elapsedRealtimeNanos
+    override fun getTimestamp(entry: Transactions.TransactionTraceEntry): Timestamp {
+        require(timestampOffset != Timestamp.NULL_TIMESTAMP)
+        return Timestamp(
+            elapsedNanos = entry.elapsedRealtimeNanos,
+            unixNanos = entry.elapsedRealtimeNanos + timestampOffset
+        )
+    }
 
     override fun createTrace(entries: List<TransactionsTraceEntry>): TransactionsTrace =
         TransactionsTrace(entries.toTypedArray())
diff --git a/libraries/flicker/src/com/android/server/wm/traces/parser/transition/TransitionsTraceParser.kt b/libraries/flicker/src/com/android/server/wm/traces/parser/transition/TransitionsTraceParser.kt
index 3548ec8..92c3940 100644
--- a/libraries/flicker/src/com/android/server/wm/traces/parser/transition/TransitionsTraceParser.kt
+++ b/libraries/flicker/src/com/android/server/wm/traces/parser/transition/TransitionsTraceParser.kt
@@ -19,6 +19,7 @@
 import android.util.Log
 import com.android.server.wm.shell.nano.ChangeInfo
 import com.android.server.wm.shell.nano.TransitionTraceProto
+import com.android.server.wm.traces.common.Timestamp
 import com.android.server.wm.traces.common.transactions.TransactionsTrace
 import com.android.server.wm.traces.common.transition.Transition
 import com.android.server.wm.traces.common.transition.Transition.Companion.Type
@@ -58,8 +59,9 @@
         input: TransitionTraceProto
     ): List<com.android.server.wm.shell.nano.Transition> = input.transition.toList()
 
-    override fun getTimestamp(entry: com.android.server.wm.shell.nano.Transition): Long =
-        entry.timestamp
+    override fun getTimestamp(entry: com.android.server.wm.shell.nano.Transition): Timestamp {
+        return Timestamp(elapsedNanos = entry.elapsedRealtimeNanos, unixNanos = entry.unixNanos)
+    }
 
     override fun onBeforeParse(input: TransitionTraceProto) {}
 
@@ -68,7 +70,7 @@
         return TransitionState(
             entry.id,
             Type.fromInt(entry.transitionType),
-            entry.timestamp,
+            Timestamp(elapsedNanos = entry.elapsedRealtimeNanos, unixNanos = entry.unixNanos),
             State.fromInt(entry.state),
             entry.flags,
             changes,
@@ -100,6 +102,10 @@
         val windowName = proto.windowIdentifier.title
         val windowId = proto.windowIdentifier.hashCode.toString(16)
 
-        return TransitionChange(windowName, Type.fromInt(proto.transitMode))
+        return TransitionChange(
+            windowName,
+            Type.fromInt(proto.transitMode),
+            TransitionChange.Companion.WindowingMode.fromInt(proto.windowingMode)
+        )
     }
 }
diff --git a/libraries/flicker/src/com/android/server/wm/traces/parser/windowmanager/WindowManagerTraceParser.kt b/libraries/flicker/src/com/android/server/wm/traces/parser/windowmanager/WindowManagerTraceParser.kt
index 626416d..e0f4c79 100644
--- a/libraries/flicker/src/com/android/server/wm/traces/parser/windowmanager/WindowManagerTraceParser.kt
+++ b/libraries/flicker/src/com/android/server/wm/traces/parser/windowmanager/WindowManagerTraceParser.kt
@@ -18,6 +18,7 @@
 
 import com.android.server.wm.nano.WindowManagerTraceFileProto
 import com.android.server.wm.nano.WindowManagerTraceProto
+import com.android.server.wm.traces.common.Timestamp
 import com.android.server.wm.traces.common.windowmanager.WindowManagerState
 import com.android.server.wm.traces.common.windowmanager.WindowManagerTrace
 import com.android.server.wm.traces.parser.AbstractTraceParser
@@ -27,7 +28,7 @@
     AbstractTraceParser<
         WindowManagerTraceFileProto, WindowManagerTraceProto, WindowManagerState, WindowManagerTrace
     >() {
-    private var realToElapsedTimeOffsetNanos = 0L
+    private var realToElapsedTimeOffsetNanos = Timestamp.NULL_TIMESTAMP
 
     override val traceName: String = "WM Trace"
 
@@ -40,7 +41,13 @@
     override fun getEntries(input: WindowManagerTraceFileProto): List<WindowManagerTraceProto> =
         input.entry.toList()
 
-    override fun getTimestamp(entry: WindowManagerTraceProto): Long = entry.elapsedRealtimeNanos
+    override fun getTimestamp(entry: WindowManagerTraceProto): Timestamp {
+        require(realToElapsedTimeOffsetNanos != Timestamp.NULL_TIMESTAMP)
+        return Timestamp(
+            elapsedNanos = entry.elapsedRealtimeNanos,
+            unixNanos = entry.elapsedRealtimeNanos + realToElapsedTimeOffsetNanos
+        )
+    }
 
     override fun onBeforeParse(input: WindowManagerTraceFileProto) {
         realToElapsedTimeOffsetNanos = input.realToElapsedTimeOffsetNanos
diff --git a/libraries/flicker/test/src/com/android/server/wm/flicker/Utils.kt b/libraries/flicker/test/src/com/android/server/wm/flicker/Utils.kt
index e185202..1b08d29 100644
--- a/libraries/flicker/test/src/com/android/server/wm/flicker/Utils.kt
+++ b/libraries/flicker/test/src/com/android/server/wm/flicker/Utils.kt
@@ -31,6 +31,8 @@
 import com.android.server.wm.flicker.monitor.TransitionsTraceMonitor
 import com.android.server.wm.flicker.monitor.WindowManagerTraceMonitor
 import com.android.server.wm.flicker.traces.FlickerSubjectException
+import com.android.server.wm.traces.common.IScenario
+import com.android.server.wm.traces.common.Timestamp
 import com.android.server.wm.traces.common.layers.LayersTrace
 import com.android.server.wm.traces.common.transactions.TransactionsTrace
 import com.android.server.wm.traces.common.transition.TransitionsTrace
@@ -81,7 +83,13 @@
 ): WindowManagerTrace {
     return try {
         WindowManagerTraceParser()
-            .parse(readAsset(relativePath), from, to, addInitialEntry, clearCache = false)
+            .parse(
+                readAsset(relativePath),
+                Timestamp(elapsedNanos = from),
+                Timestamp(elapsedNanos = to),
+                addInitialEntry,
+                clearCache = false
+            )
     } catch (e: Exception) {
         throw RuntimeException(e)
     }
@@ -290,11 +298,14 @@
     return mockedFlicker
 }
 
-fun captureTrace(actions: () -> Unit): ResultReader {
+fun captureTrace(scenario: IScenario, actions: () -> Unit): ResultReader {
+    if (scenario == null) {
+        ScenarioBuilder().forClass("UNNAMED_CAPTURE").build()
+    }
     val instrumentation: Instrumentation = InstrumentationRegistry.getInstrumentation()
     val writer =
         ResultWriter()
-            .forScenario(TEST_SCENARIO)
+            .forScenario(scenario)
             .withOutputDir(getDefaultFlickerOutputDir())
             .setRunComplete()
     val monitors =
diff --git a/libraries/flicker/test/src/com/android/server/wm/parser/MockTraceParser.kt b/libraries/flicker/test/src/com/android/server/wm/parser/MockTraceParser.kt
index fdc71bc..7bc81c6 100644
--- a/libraries/flicker/test/src/com/android/server/wm/parser/MockTraceParser.kt
+++ b/libraries/flicker/test/src/com/android/server/wm/parser/MockTraceParser.kt
@@ -16,6 +16,7 @@
 
 package com.android.server.wm.parser
 
+import com.android.server.wm.traces.common.Timestamp
 import com.android.server.wm.traces.common.windowmanager.WindowManagerState
 import com.android.server.wm.traces.common.windowmanager.WindowManagerTrace
 import com.android.server.wm.traces.parser.AbstractTraceParser
@@ -32,6 +33,7 @@
     override fun doParseEntry(entry: WindowManagerState): WindowManagerState = entry
     override fun getEntries(input: WindowManagerTrace): List<WindowManagerState> =
         input.entries.toList()
-    override fun getTimestamp(entry: WindowManagerState): Long = entry.elapsedTimestamp
+    override fun getTimestamp(entry: WindowManagerState): Timestamp =
+        Timestamp(elapsedNanos = entry.elapsedTimestamp)
     override fun onBeforeParse(input: WindowManagerTrace) {}
 }
diff --git a/libraries/flicker/test/src/com/android/server/wm/parser/TraceParserTest.kt b/libraries/flicker/test/src/com/android/server/wm/parser/TraceParserTest.kt
index ca3b54e..438491c 100644
--- a/libraries/flicker/test/src/com/android/server/wm/parser/TraceParserTest.kt
+++ b/libraries/flicker/test/src/com/android/server/wm/parser/TraceParserTest.kt
@@ -40,12 +40,22 @@
         val to = mockTraceForSliceTests.last().elapsedTimestamp + 20
         val splitLayersTraceWithoutInitialEntry =
             MockTraceParser(mockTraceForSliceTests)
-                .parse(mockTraceForSliceTests, from, to, addInitialEntry = false)
+                .parse(
+                    mockTraceForSliceTests,
+                    Timestamp(elapsedNanos = from),
+                    Timestamp(elapsedNanos = to),
+                    addInitialEntry = false
+                )
         Truth.assertThat(splitLayersTraceWithoutInitialEntry).isEmpty()
 
         val splitLayersTraceWithInitialEntry =
             MockTraceParser(mockTraceForSliceTests)
-                .parse(mockTraceForSliceTests, from, to, addInitialEntry = true)
+                .parse(
+                    mockTraceForSliceTests,
+                    Timestamp(elapsedNanos = from),
+                    Timestamp(elapsedNanos = to),
+                    addInitialEntry = true
+                )
         Truth.assertThat(splitLayersTraceWithInitialEntry).hasSize(1)
         Truth.assertThat(splitLayersTraceWithInitialEntry.first().timestamp)
             .isEqualTo(mockTraceForSliceTests.last().timestamp)
@@ -228,14 +238,24 @@
     private fun testSliceWithOutInitialEntry(from: Long, to: Long, expected: List<Long>) {
         val splitLayersTrace =
             MockTraceParser(mockTraceForSliceTests)
-                .parse(mockTraceForSliceTests, from, to, addInitialEntry = false)
+                .parse(
+                    mockTraceForSliceTests,
+                    Timestamp(elapsedNanos = from),
+                    Timestamp(elapsedNanos = to),
+                    addInitialEntry = false
+                )
         Truth.assertThat(splitLayersTrace.map { it.timestamp.elapsedNanos }).isEqualTo(expected)
     }
 
     private fun testSliceWithInitialEntry(from: Long, to: Long, expected: List<Long>) {
         val splitLayersTraceWithStartEntry =
             MockTraceParser(mockTraceForSliceTests)
-                .parse(mockTraceForSliceTests, from, to, addInitialEntry = true)
+                .parse(
+                    mockTraceForSliceTests,
+                    Timestamp(elapsedNanos = from),
+                    Timestamp(elapsedNanos = to),
+                    addInitialEntry = true
+                )
         Truth.assertThat(splitLayersTraceWithStartEntry.map { it.timestamp.elapsedNanos })
             .isEqualTo(expected)
     }