Fixes onOpenedWithContent() being called with a time to open being equal to the time since the epoch while restoring.

This means that we track time to restore the same as we track time to open (in the same metrics).

PiperOrigin-RevId: 257862255
Change-Id: Ic94152774ed71365de63d62406effe479954f14e
diff --git a/src/main/java/com/google/android/libraries/feed/basicstream/BasicStream.java b/src/main/java/com/google/android/libraries/feed/basicstream/BasicStream.java
index 32bc464..0379362 100644
--- a/src/main/java/com/google/android/libraries/feed/basicstream/BasicStream.java
+++ b/src/main/java/com/google/android/libraries/feed/basicstream/BasicStream.java
@@ -668,11 +668,11 @@
 
   private void logContent() {
     if (loggingState == LoggingState.STARTING) {
-      if (clock.currentTimeMillis() - sessionStartTimestamp > immediateContentThreshold) {
+      long timeToPopulateMs = clock.currentTimeMillis() - sessionStartTimestamp;
+      if (timeToPopulateMs > immediateContentThreshold) {
         basicLoggingApi.onOpenedWithNoImmediateContent();
       }
 
-      long timeToPopulateMs = clock.currentTimeMillis() - sessionStartTimestamp;
       if (checkNotNull(streamDriver).hasContent()) {
         basicLoggingApi.onOpenedWithContent(
             (int) timeToPopulateMs, checkNotNull(streamDriver).getLeafFeatureDrivers().size());
@@ -741,7 +741,6 @@
 
   private void createModelProviderAndStreamDriver() {
     if (modelProvider == null) {
-
       // For nullness checker
       ModelProvider localModelProvider = null;
       String localSavedSessionId = savedSessionId;
@@ -757,9 +756,9 @@
         Logger.d(TAG, "Creating new session for showing.");
         localModelProvider =
             modelProviderFactory.createNew(deepestContentTracker, UiContext.getDefaultInstance());
-        sessionStartTimestamp = clock.currentTimeMillis();
       }
 
+      sessionStartTimestamp = clock.currentTimeMillis();
       modelProvider = localModelProvider;
 
       registerObserversOnModelProvider(modelProvider);
diff --git a/src/test/java/com/google/android/libraries/feed/basicstream/BasicStreamTest.java b/src/test/java/com/google/android/libraries/feed/basicstream/BasicStreamTest.java
index d75057b..703d439 100644
--- a/src/test/java/com/google/android/libraries/feed/basicstream/BasicStreamTest.java
+++ b/src/test/java/com/google/android/libraries/feed/basicstream/BasicStreamTest.java
@@ -237,6 +237,32 @@
   }
 
   @Test
+  public void testOnSessionStart_logsOnOpenedWithStreamContentAfterOnShow_whenRestoring() {
+    basicStream.onShow();
+
+    String savedInstanceState = basicStream.getSavedInstanceStateString();
+
+    basicStream.onHide();
+    basicStream.onDestroy();
+
+    when(modelProviderFactory.create(SESSION_ID, UiContext.getDefaultInstance()))
+        .thenReturn(restoredModelProvider);
+
+    basicStream = createBasicStream(new LinearLayoutManager(context));
+
+    basicStream.onCreate(savedInstanceState);
+
+    clock.set(15L);
+    basicStream.onShow();
+
+    when(streamDriver.hasContent()).thenReturn(true);
+    clock.advance(5L);
+    basicStream.onSessionStart(UiContext.getDefaultInstance());
+
+    verify(basicLoggingApi).onOpenedWithContent(5, 0);
+  }
+
+  @Test
   public void testOnSessionStart_doesNotLogOnOpenedWithStreamContentAfterInitialOnShow() {
     basicStream.onShow();
     basicStream.onSessionStart(UiContext.getDefaultInstance());