From 705bb6525c03a744b4ac120660e46dad3ca39ef9 Mon Sep 17 00:00:00 2001 From: Nino Jagar Date: Thu, 18 May 2023 17:32:31 +0000 Subject: [PATCH] Add new flush reason for content protection Bug: 275732576 Test: Added new tests No-Typo-Check: This typeo has been merged since Jan 2019 Change-Id: I0f507a8f57bfab50df9a9b3234a3679a06b75423 --- .../contentcapture/ContentCaptureSession.java | 35 +++++++++++-------- .../ContentCaptureSessionTest.java | 33 +++++++++++++++++ 2 files changed, 54 insertions(+), 14 deletions(-) diff --git a/core/java/android/view/contentcapture/ContentCaptureSession.java b/core/java/android/view/contentcapture/ContentCaptureSession.java index 62044aa78213d..b22910648e713 100644 --- a/core/java/android/view/contentcapture/ContentCaptureSession.java +++ b/core/java/android/view/contentcapture/ContentCaptureSession.java @@ -181,6 +181,8 @@ public abstract class ContentCaptureSession implements AutoCloseable { public static final int FLUSH_REASON_VIEW_TREE_APPEARING = 9; /** @hide */ public static final int FLUSH_REASON_VIEW_TREE_APPEARED = 10; + /** @hide */ + public static final int FLUSH_REASON_LOGIN_DETECTED = 11; /** * After {@link UPSIDE_DOWN_CAKE}, {@link #notifyViewsDisappeared(AutofillId, long[])} wraps @@ -191,20 +193,23 @@ public abstract class ContentCaptureSession implements AutoCloseable { static final long NOTIFY_NODES_DISAPPEAR_NOW_SENDS_TREE_EVENTS = 258825825L; /** @hide */ - @IntDef(prefix = { "FLUSH_REASON_" }, value = { - FLUSH_REASON_FULL, - FLUSH_REASON_VIEW_ROOT_ENTERED, - FLUSH_REASON_SESSION_STARTED, - FLUSH_REASON_SESSION_FINISHED, - FLUSH_REASON_IDLE_TIMEOUT, - FLUSH_REASON_TEXT_CHANGE_TIMEOUT, - FLUSH_REASON_SESSION_CONNECTED, - FLUSH_REASON_FORCE_FLUSH, - FLUSH_REASON_VIEW_TREE_APPEARING, - FLUSH_REASON_VIEW_TREE_APPEARED - }) + @IntDef( + prefix = {"FLUSH_REASON_"}, + value = { + FLUSH_REASON_FULL, + FLUSH_REASON_VIEW_ROOT_ENTERED, + FLUSH_REASON_SESSION_STARTED, + FLUSH_REASON_SESSION_FINISHED, + FLUSH_REASON_IDLE_TIMEOUT, + FLUSH_REASON_TEXT_CHANGE_TIMEOUT, + FLUSH_REASON_SESSION_CONNECTED, + FLUSH_REASON_FORCE_FLUSH, + FLUSH_REASON_VIEW_TREE_APPEARING, + FLUSH_REASON_VIEW_TREE_APPEARED, + FLUSH_REASON_LOGIN_DETECTED + }) @Retention(RetentionPolicy.SOURCE) - public @interface FlushReason{} + public @interface FlushReason {} private final Object mLock = new Object(); @@ -685,8 +690,10 @@ public abstract class ContentCaptureSession implements AutoCloseable { return "VIEW_TREE_APPEARING"; case FLUSH_REASON_VIEW_TREE_APPEARED: return "VIEW_TREE_APPEARED"; + case FLUSH_REASON_LOGIN_DETECTED: + return "LOGIN_DETECTED"; default: - return "UNKOWN-" + reason; + return "UNKNOWN-" + reason; } } diff --git a/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java b/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java index 27d58b8cdcb7d..f8ebd09899a8b 100644 --- a/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java +++ b/core/tests/coretests/src/android/view/contentcapture/ContentCaptureSessionTest.java @@ -27,6 +27,8 @@ import android.view.ViewStructure; import android.view.autofill.AutofillId; import android.view.contentcapture.ViewNode.ViewStructureImpl; +import com.google.common.collect.ImmutableMap; + import libcore.junit.util.compat.CoreCompatChangeRule.DisableCompatChanges; import libcore.junit.util.compat.CoreCompatChangeRule.EnableCompatChanges; @@ -37,6 +39,8 @@ import org.junit.runner.RunWith; import org.mockito.Mock; import org.mockito.junit.MockitoJUnitRunner; +import java.util.Map; + /** * Unit tests for {@link ContentCaptureSession}. * @@ -145,6 +149,35 @@ public class ContentCaptureSessionTest { assertThat(session.mInternalNotifyViewTreeEventFinishedCount).isEqualTo(1); } + @Test + public void testGetFlushReasonAsString() { + int invalidFlushReason = ContentCaptureSession.FLUSH_REASON_LOGIN_DETECTED + 1; + Map expectedMap = + new ImmutableMap.Builder() + .put(ContentCaptureSession.FLUSH_REASON_FULL, "FULL") + .put(ContentCaptureSession.FLUSH_REASON_VIEW_ROOT_ENTERED, "VIEW_ROOT") + .put(ContentCaptureSession.FLUSH_REASON_SESSION_STARTED, "STARTED") + .put(ContentCaptureSession.FLUSH_REASON_SESSION_FINISHED, "FINISHED") + .put(ContentCaptureSession.FLUSH_REASON_IDLE_TIMEOUT, "IDLE") + .put(ContentCaptureSession.FLUSH_REASON_TEXT_CHANGE_TIMEOUT, "TEXT_CHANGE") + .put(ContentCaptureSession.FLUSH_REASON_SESSION_CONNECTED, "CONNECTED") + .put(ContentCaptureSession.FLUSH_REASON_FORCE_FLUSH, "FORCE_FLUSH") + .put( + ContentCaptureSession.FLUSH_REASON_VIEW_TREE_APPEARING, + "VIEW_TREE_APPEARING") + .put( + ContentCaptureSession.FLUSH_REASON_VIEW_TREE_APPEARED, + "VIEW_TREE_APPEARED") + .put(ContentCaptureSession.FLUSH_REASON_LOGIN_DETECTED, "LOGIN_DETECTED") + .put(invalidFlushReason, "UNKOWN-" + invalidFlushReason) + .build(); + + expectedMap.forEach( + (reason, expected) -> + assertThat(ContentCaptureSession.getFlushReasonAsString(reason)) + .isEqualTo(expected)); + } + // Cannot use @Spy because we need to pass the session id on constructor private class MyContentCaptureSession extends ContentCaptureSession { int mInternalNotifyViewTreeEventStartedCount = 0;