diff options
| author | 2024-01-24 15:59:42 +0000 | |
|---|---|---|
| committer | 2024-01-24 15:59:42 +0000 | |
| commit | fe1fd01b0650d76a566d447c3fcbe785a85fc042 (patch) | |
| tree | c1eddc06d54be087a2cde5cf72700ebc94d2d092 | |
| parent | df89b2585f8957f640e64860d7d006386a2acb22 (diff) | |
| parent | 5bc6a51ad47a87d4f4ac78f1cabda211f02ea8bc (diff) | |
Merge "Set upper limit of process queue in MainContentCaptureSessionV2" into main
| -rw-r--r-- | core/java/android/view/contentcapture/MainContentCaptureSessionV2.java | 7 | ||||
| -rw-r--r-- | core/tests/coretests/src/android/view/contentcapture/MainContentCaptureSessionV2Test.java | 66 |
2 files changed, 71 insertions, 2 deletions
diff --git a/core/java/android/view/contentcapture/MainContentCaptureSessionV2.java b/core/java/android/view/contentcapture/MainContentCaptureSessionV2.java index bf1d31c8496d..fbb66d1485dd 100644 --- a/core/java/android/view/contentcapture/MainContentCaptureSessionV2.java +++ b/core/java/android/view/contentcapture/MainContentCaptureSessionV2.java @@ -149,9 +149,12 @@ public final class MainContentCaptureSessionV2 extends ContentCaptureSession { * * Because it is not guaranteed that the events will be enqueued from a single thread, the * implementation must be thread-safe to prevent unexpected behaviour. + * + * @hide */ + @VisibleForTesting(visibility = VisibleForTesting.Visibility.PRIVATE) @NonNull - private final ConcurrentLinkedQueue<ContentCaptureEvent> mEventProcessQueue; + public final ConcurrentLinkedQueue<ContentCaptureEvent> mEventProcessQueue; /** * List of events held to be sent to the {@link ContentCaptureService} as a batch. @@ -908,7 +911,7 @@ public final class MainContentCaptureSessionV2 extends ContentCaptureSession { * clear the buffer events then starting sending out current event. */ private void enqueueEvent(@NonNull final ContentCaptureEvent event, boolean forceFlush) { - if (forceFlush) { + if (forceFlush || mEventProcessQueue.size() >= mManager.mOptions.maxBufferSize - 1) { // The buffer events are cleared in the same thread first to prevent new events // being added during the time of context switch. This would disrupt the sequence // of events. diff --git a/core/tests/coretests/src/android/view/contentcapture/MainContentCaptureSessionV2Test.java b/core/tests/coretests/src/android/view/contentcapture/MainContentCaptureSessionV2Test.java index f0f3a9683353..00751288ad62 100644 --- a/core/tests/coretests/src/android/view/contentcapture/MainContentCaptureSessionV2Test.java +++ b/core/tests/coretests/src/android/view/contentcapture/MainContentCaptureSessionV2Test.java @@ -433,6 +433,72 @@ public class MainContentCaptureSessionV2Test { assertThat(session.mEvents).isEmpty(); } + @Test + public void notifyViewAppearedBelowMaximumBufferSize() throws RemoteException { + ContentCaptureOptions options = + createOptions( + /* enableContentCaptureReceiver= */ true, + /* enableContentProtectionReceiver= */ true); + MainContentCaptureSessionV2 session = createSession(options); + session.mDirectServiceInterface = mMockContentCaptureDirectManager; + + session.onSessionStarted(0x2, null); + for (int i = 0; i < BUFFER_SIZE - 1; i++) { + View view = prepareView(session); + session.notifyViewAppeared(session.newViewStructure(view)); + } + mTestableLooper.processAllMessages(); + + verify(mMockContentCaptureDirectManager, times(0)) + .sendEvents(any(), anyInt(), any()); + assertThat(session.mEvents).isNull(); + assertThat(session.mEventProcessQueue).hasSize(BUFFER_SIZE - 1); + } + + @Test + public void notifyViewAppearedExactAsMaximumBufferSize() throws RemoteException { + ContentCaptureOptions options = + createOptions( + /* enableContentCaptureReceiver= */ true, + /* enableContentProtectionReceiver= */ true); + MainContentCaptureSessionV2 session = createSession(options); + session.mDirectServiceInterface = mMockContentCaptureDirectManager; + + session.onSessionStarted(0x2, null); + for (int i = 0; i < BUFFER_SIZE; i++) { + View view = prepareView(session); + session.notifyViewAppeared(session.newViewStructure(view)); + } + mTestableLooper.processAllMessages(); + + verify(mMockContentCaptureDirectManager, times(1)) + .sendEvents(any(), anyInt(), any()); + assertThat(session.mEvents).isEmpty(); + assertThat(session.mEventProcessQueue).isEmpty(); + } + + @Test + public void notifyViewAppearedAboveMaximumBufferSize() throws RemoteException { + ContentCaptureOptions options = + createOptions( + /* enableContentCaptureReceiver= */ true, + /* enableContentProtectionReceiver= */ true); + MainContentCaptureSessionV2 session = createSession(options); + session.mDirectServiceInterface = mMockContentCaptureDirectManager; + + session.onSessionStarted(0x2, null); + for (int i = 0; i < BUFFER_SIZE * 2 + 1; i++) { + View view = prepareView(session); + session.notifyViewAppeared(session.newViewStructure(view)); + } + mTestableLooper.processAllMessages(); + + verify(mMockContentCaptureDirectManager, times(2)) + .sendEvents(any(), anyInt(), any()); + assertThat(session.mEvents).isEmpty(); + assertThat(session.mEventProcessQueue).hasSize(1); + } + /** Simulates the regular content capture events sequence. */ private void notifyContentCaptureEvents(final MainContentCaptureSessionV2 session) { final ArrayList<Object> events = new ArrayList<>( |