Skip to content

Commit

Permalink
[CT] Pause Engagement Signals on pages with text fragments
Browse files Browse the repository at this point in the history
(cherry picked from commit 47cba7b)

Bug: 1434438
Change-Id: Ie0eacbbca3053dc7aa0e70e75976084425f3ca5d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4595859
Code-Coverage: Findit <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Kevin Grosu <kgrosu@google.com>
Commit-Queue: Sinan Sahin <sinansahin@google.com>
Cr-Original-Commit-Position: refs/heads/main@{#1154036}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4611968
Cr-Commit-Position: refs/branch-heads/5790@{#759}
Cr-Branched-From: 1d71a33-refs/heads/main@{#1148114}
  • Loading branch information
fsinan authored and Chromium LUCI CQ committed Jun 14, 2023
1 parent fff73dc commit 551a809
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import org.chromium.chrome.browser.dependency_injection.ActivityScope;
import org.chromium.chrome.browser.flags.ChromeFeatureList;
import org.chromium.chrome.browser.privacy.settings.PrivacyPreferencesManagerImpl;
import org.chromium.chrome.browser.share.link_to_text.LinkToTextHelper;
import org.chromium.chrome.browser.tab.Tab;
import org.chromium.chrome.browser.tab.TabHidingType;
import org.chromium.content_public.browser.GestureListenerManager;
import org.chromium.content_public.browser.GestureStateListener;
import org.chromium.content_public.browser.LoadCommittedDetails;
import org.chromium.content_public.browser.NavigationHandle;
import org.chromium.content_public.browser.RenderCoordinates;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.WebContentsObserver;
Expand Down Expand Up @@ -86,6 +88,8 @@ class RealtimeEngagementSignalObserver extends CustomTabTabObserver {
private int mAfterScrollEndThresholdMs;
// Tracks the user interaction state across multiple tabs and WebContents.
private boolean mDidGetUserInteraction;
// Prevents sending Engagement Signals temporarily.
private boolean mSignalsPaused;

/**
* A tab observer that will send real time scrolling signals to CustomTabsConnection, if a
Expand Down Expand Up @@ -141,7 +145,7 @@ protected void onObservingDifferentTab(@NonNull Tab tab) {

@Override
protected void onAllTabsClosed() {
mConnection.notifyDidGetUserInteraction(mSession, mDidGetUserInteraction);
notifySessionEnded(mDidGetUserInteraction);
mDidGetUserInteraction = false;
mConnection.setEngagementSignalsAvailableSupplier(mSession, null);
removeWebContentsDependencies(mWebContents);
Expand Down Expand Up @@ -215,8 +219,7 @@ public void onScrollStarted(
int scrollOffsetY, int scrollExtentY, boolean isDirectionUp) {
mScrollState.onScrollStarted(isDirectionUp);
// If we shouldn't send the real values, always send false.
mConnection.notifyVerticalScrollEvent(
mSession, mShouldSendRealValues && isDirectionUp);
notifyVerticalScrollEvent(mShouldSendRealValues && isDirectionUp);
}

@Override
Expand Down Expand Up @@ -255,8 +258,7 @@ public void onScrollOffsetOrExtentChanged(int scrollOffsetY, int scrollExtentY)
public void onVerticalScrollDirectionChanged(
boolean directionUp, float currentScrollRatio) {
if (mScrollState.onScrollDirectionChanged(directionUp)) {
mConnection.notifyVerticalScrollEvent(
mSession, mShouldSendRealValues && directionUp);
notifyVerticalScrollEvent(mShouldSendRealValues && directionUp);
}
}

Expand All @@ -273,8 +275,8 @@ public void onScrollEnded(int scrollOffsetY, int scrollExtentY) {
private void onScrollEndedInternal(boolean allowUpdateAfter) {
int resultPercentage = mScrollState.onScrollEnded(allowUpdateAfter);
if (resultPercentage != SCROLL_STATE_MAX_PERCENTAGE_NOT_INCREASING) {
mConnection.notifyGreatestScrollPercentageIncreased(
mSession, mShouldSendRealValues ? resultPercentage : STUB_PERCENT);
notifyGreatestScrollPercentageIncreased(
mShouldSendRealValues ? resultPercentage : STUB_PERCENT);
}
}
};
Expand All @@ -286,6 +288,11 @@ public void navigationEntryCommitted(LoadCommittedDetails details) {
mScrollState.resetMaxScrollPercentage();
}
}

@Override
public void didStartNavigationInPrimaryMainFrame(NavigationHandle navigationHandle) {
mSignalsPaused = LinkToTextHelper.hasTextFragment(navigationHandle.getUrl());
}
};

GestureListenerManager gestureListenerManager =
Expand Down Expand Up @@ -329,6 +336,29 @@ private boolean shouldSendEngagementSignal(Tab tab) {
&& PrivacyPreferencesManagerImpl.getInstance().isUsageAndCrashReportingPermitted();
}

/**
* @param isDirectionUp Whether the scroll direction is up.
*/
private void notifyVerticalScrollEvent(boolean isDirectionUp) {
if (mSignalsPaused) return;
mConnection.notifyVerticalScrollEvent(mSession, isDirectionUp);
}

/**
* @param scrollPercentage The new scroll percentage.
*/
private void notifyGreatestScrollPercentageIncreased(int scrollPercentage) {
if (mSignalsPaused) return;
mConnection.notifyGreatestScrollPercentageIncreased(mSession, scrollPercentage);
}

/**
* @param didGetUserInteraction Whether user had any interaction in the current CCT session.
*/
private void notifySessionEnded(boolean didGetUserInteraction) {
mConnection.notifyDidGetUserInteraction(mSession, didGetUserInteraction);
}

/**
* Parameter tracking the entire scrolling journey for the associated tab.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import static org.chromium.cc.mojom.RootScrollOffsetUpdateFrequency.ON_SCROLL_END;
import static org.chromium.chrome.browser.customtabs.content.RealtimeEngagementSignalObserver.REAL_VALUES;
import static org.chromium.chrome.browser.customtabs.content.RealtimeEngagementSignalObserver.TIME_CAN_UPDATE_AFTER_END;
import static org.chromium.url.JUnitTestGURLs.HTTP_URL;
import static org.chromium.url.JUnitTestGURLs.TEXT_FRAGMENT_URL;
import static org.chromium.url.JUnitTestGURLs.URL_1;

import android.graphics.Point;
Expand Down Expand Up @@ -59,15 +61,17 @@
import org.chromium.content.browser.RenderCoordinatesImpl;
import org.chromium.content_public.browser.GestureStateListener;
import org.chromium.content_public.browser.LoadCommittedDetails;
import org.chromium.content_public.browser.NavigationHandle;
import org.chromium.content_public.browser.WebContents;
import org.chromium.content_public.browser.WebContentsObserver;
import org.chromium.url.JUnitTestGURLs;
import org.chromium.url.ShadowGURL;

import java.util.List;

/** Unit test for {@link RealtimeEngagementSignalObserver}. */
@RunWith(BaseRobolectricTestRunner.class)
@Config(shadows = {ShadowSystemClock.class})
@Config(shadows = {ShadowSystemClock.class, ShadowGURL.class})
@Features.EnableFeatures({ChromeFeatureList.CCT_REAL_TIME_ENGAGEMENT_SIGNALS})
@Features.DisableFeatures({ChromeFeatureList.CCT_REAL_TIME_ENGAGEMENT_SIGNALS_ALTERNATIVE_IMPL})
public class RealtimeEngagementSignalObserverUnitTest {
Expand Down Expand Up @@ -721,6 +725,43 @@ public void sendOnSessionEnded_HadNoInteraction() {
verify(env.connection, times(1)).notifyDidGetUserInteraction(env.session, false);
}

@Test
@Features.EnableFeatures({ChromeFeatureList.CCT_REAL_TIME_ENGAGEMENT_SIGNALS_ALTERNATIVE_IMPL})
public void pauseAndUnpauseSignalsOnPageWithTextFragment() {
initializeTabForTest();
GestureStateListener listener = captureGestureStateListener(ON_SCROLL_END);
WebContentsObserver webContentsObserver = captureWebContentsObserver();

// Navigate to a URL with text fragment.
var navigationHandle = NavigationHandle.createForTesting(
JUnitTestGURLs.getGURL(TEXT_FRAGMENT_URL), false, 0, false);
webContentsObserver.didStartNavigationInPrimaryMainFrame(navigationHandle);

// Do a scroll.
listener.onScrollStarted(0, SCROLL_EXTENT, false);
when(mRenderCoordinatesImpl.getScrollYPixInt()).thenReturn(24);
listener.onScrollOffsetOrExtentChanged(24, SCROLL_EXTENT);
listener.onScrollEnded(24, SCROLL_EXTENT);
// We shouldn't get scroll signals.
verify(env.connection, never()).notifyVerticalScrollEvent(eq(env.session), anyBoolean());
verify(env.connection, never())
.notifyGreatestScrollPercentageIncreased(eq(env.session), anyInt());

// Navigate back to a URL with no text fragment.
var navigationHandle2 = NavigationHandle.createForTesting(
JUnitTestGURLs.getGURL(HTTP_URL), false, 0, false);
webContentsObserver.didStartNavigationInPrimaryMainFrame(navigationHandle2);

// Do a scroll.
listener.onScrollStarted(24, SCROLL_EXTENT, false);
when(mRenderCoordinatesImpl.getScrollYPixInt()).thenReturn(50);
listener.onScrollOffsetOrExtentChanged(50, SCROLL_EXTENT);
listener.onScrollEnded(50, SCROLL_EXTENT);
// We should normally get signals.
verify(env.connection).notifyVerticalScrollEvent(eq(env.session), eq(false));
verify(env.connection).notifyGreatestScrollPercentageIncreased(eq(env.session), eq(50));
}

private void advanceTime(long millis) {
SystemClock.setCurrentTimeMillis(CURRENT_TIME_MS + millis);
}
Expand Down

0 comments on commit 551a809

Please sign in to comment.