Fixing onOpenedWithNoImmediateContent behavior.

onOpenedWithNoImmediateContent was not being correctly called if the Stream had content loaded with a delay over the immediate content threshold.

PiperOrigin-RevId: 248219753
Change-Id: I7748466545ef4164a80b13f78f86c6e2b2c2465e
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 84211c5..997bde9 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
@@ -618,6 +618,8 @@
     if (streamDriver.hasContent()) {
       isInitialLoad = false;
     }
+
+    logContent();
   }
 
   @Override
@@ -638,7 +640,6 @@
       // If MINIMUM_SPINNER_SHOW_TIME has elapsed, the new content can be shown immediately.
       if (spinnerDisplayTime >= MINIMUM_SPINNER_SHOW_TIME) {
         updateAdapterAfterSessionStart(localModelProvider);
-        logContent();
       } else {
         // If MINIMUM_SPINNER_SHOW_TIME has not elapsed, the new content should only be shown once
         // the remaining time has been fulfilled.
@@ -649,29 +650,31 @@
               // indicates that the session finished before the spinner show time elapsed.
               if (modelProvider == localModelProvider) {
                 updateAdapterAfterSessionStart(localModelProvider);
-                logContent();
               }
             },
             MINIMUM_SPINNER_SHOW_TIME - spinnerDisplayTime);
       }
     } else {
       updateAdapterAfterSessionStart(localModelProvider);
-      logContent();
     }
   }
 
   private void logContent() {
     if (loggingState == LoggingState.STARTING) {
-      if (!checkNotNull(streamDriver).hasContent()
-          && clock.currentTimeMillis() - sessionStartTimestamp > immediateContentThreshold) {
+      if (clock.currentTimeMillis() - sessionStartTimestamp > immediateContentThreshold) {
         basicLoggingApi.onOpenedWithNoImmediateContent();
       }
 
       long timeToPopulateMs = clock.currentTimeMillis() - sessionStartTimestamp;
-      basicLoggingApi.onOpenedWithContent(
-          (int) timeToPopulateMs, checkNotNull(streamDriver).getLeafFeatureDrivers().size());
-      // onOpenedWithContent should only be logged the first time the Stream is opened up.
-      loggingState = LoggingState.LOGGED_CONTENT_SHOWN;
+      if (checkNotNull(streamDriver).hasContent()) {
+        basicLoggingApi.onOpenedWithContent(
+            (int) timeToPopulateMs, checkNotNull(streamDriver).getLeafFeatureDrivers().size());
+        // onOpenedWithContent should only be logged the first time the Stream is opened up.
+        loggingState = LoggingState.LOGGED_CONTENT_SHOWN;
+      } else {
+        basicLoggingApi.onOpenedWithNoContent();
+        loggingState = LoggingState.LOGGED_NO_CONTENT;
+      }
     }
   }
 
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 fad7c57..9a896c4 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
@@ -227,6 +227,7 @@
     clock = clock.set(10);
     basicStream.onShow();
 
+    when(streamDriver.hasContent()).thenReturn(true);
     clock = clock.set(40);
     basicStream.onSessionStart(UiContext.getDefaultInstance());
 
@@ -256,6 +257,15 @@
   }
 
   @Test
+  public void testOnSessionStart_logsOnOpenedWithNoContent_ifStreamDriverDoesNotHaveContent() {
+    basicStream.onShow();
+
+    basicStream.onSessionStart(UiContext.getDefaultInstance());
+
+    verify(basicLoggingApi).onOpenedWithNoContent();
+  }
+
+  @Test
   public void
       testOnSessionStart_doesNotUseNewStreamDriver_ifBothStreamDriversAreShowingZeroState() {
     StreamDriver newStreamDriver = mock(StreamDriver.class);
@@ -328,8 +338,6 @@
   @Test
   public void testOnSessionStart_logsOnOpenedWithNoImmediateContent() {
     basicStream.onShow();
-    reset(streamDriver);
-    when(streamDriver.hasContent()).thenReturn(false);
 
     // Advance so that the spinner starts showing
     clock.advance(MINIMUM_TIME_BEFORE_SHOWING_SPINNER);
@@ -343,6 +351,17 @@
   }
 
   @Test
+  public void testOnSessionStart_doesNotLogOnOpenedWithNoImmediateContent_ifNotWithinThreshold() {
+    basicStream.onShow();
+
+    clock.advance(LOGGING_IMMEDIATE_CONTENT_THRESHOLD_MS - 1);
+
+    basicStream.onSessionStart(UiContext.getDefaultInstance());
+
+    verify(basicLoggingApi, never()).onOpenedWithNoImmediateContent();
+  }
+
+  @Test
   public void testOnSessionStart_logsOnOpenedWithNoContent() {
     when(initialModelProvider.getCurrentState()).thenReturn(State.READY);
     when(initialModelProvider.getRootFeature()).thenReturn(null);
@@ -354,23 +373,21 @@
   }
 
   @Test
-  public void testOnSessionStart_doesNotLogOnOpenedWithNoContent_ifModelProviderNotReady() {
+  public void testOnShow_doesNotLogOnOpenedWithNoContent_ifModelProviderNotReady() {
     when(initialModelProvider.getCurrentState()).thenReturn(State.INITIALIZING);
     when(initialModelProvider.getRootFeature()).thenReturn(null);
-    basicStream.onShow();
 
-    basicStream.onSessionStart(UiContext.getDefaultInstance());
+    basicStream.onShow();
 
     verify(basicLoggingApi, never()).onOpenedWithNoContent();
   }
 
   @Test
-  public void testOnSessionStart_doesNotLogOnOpenedWithNoContent_ifRootFeatureNotNull() {
+  public void testOnShow_doesNotLogOnOpenedWithNoContent_ifRootFeatureNotNull() {
     when(initialModelProvider.getCurrentState()).thenReturn(State.READY);
     when(initialModelProvider.getRootFeature()).thenReturn(modelFeature);
-    basicStream.onShow();
 
-    basicStream.onSessionStart(UiContext.getDefaultInstance());
+    basicStream.onShow();
 
     verify(basicLoggingApi, never()).onOpenedWithNoContent();
   }