1 /* 2 * Copyright (C) 2021 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.view; 18 19 import static org.junit.Assert.*; 20 21 import android.graphics.Rect; 22 import android.os.CancellationSignal; 23 24 import androidx.annotation.NonNull; 25 26 import java.util.function.Consumer; 27 28 class TestScrollCaptureCallback implements ScrollCaptureCallback { 29 private Consumer<Rect> mSearchConsumer; 30 private Runnable mStartOnReady; 31 private Consumer<Rect> mImageOnComplete; 32 private Runnable mOnEndReady; 33 private volatile int mModCount; 34 private boolean mOnScrollCaptureEndCalled; 35 private CancellationSignal mLastCancellationSignal; 36 37 @Override onScrollCaptureSearch(@onNull CancellationSignal signal, @NonNull Consumer<Rect> onReady)38 public void onScrollCaptureSearch(@NonNull CancellationSignal signal, 39 @NonNull Consumer<Rect> onReady) { 40 mLastCancellationSignal = signal; 41 mSearchConsumer = onReady; 42 mModCount++; 43 } 44 45 @Override onScrollCaptureStart(@onNull ScrollCaptureSession session, @NonNull CancellationSignal signal, @NonNull Runnable onReady)46 public void onScrollCaptureStart(@NonNull ScrollCaptureSession session, 47 @NonNull CancellationSignal signal, @NonNull Runnable onReady) { 48 mLastCancellationSignal = signal; 49 mStartOnReady = onReady; 50 mModCount++; 51 } 52 53 @Override onScrollCaptureImageRequest(@onNull ScrollCaptureSession session, @NonNull CancellationSignal signal, @NonNull Rect captureArea, @NonNull Consumer<Rect> onComplete)54 public void onScrollCaptureImageRequest(@NonNull ScrollCaptureSession session, 55 @NonNull CancellationSignal signal, @NonNull Rect captureArea, 56 @NonNull Consumer<Rect> onComplete) { 57 mLastCancellationSignal = signal; 58 mImageOnComplete = onComplete; 59 mModCount++; 60 } 61 62 @Override onScrollCaptureEnd(@onNull Runnable onReady)63 public void onScrollCaptureEnd(@NonNull Runnable onReady) { 64 mOnEndReady = onReady; 65 mOnScrollCaptureEndCalled = true; 66 } 67 onScrollCaptureEndCalled()68 public boolean onScrollCaptureEndCalled() { 69 return mOnScrollCaptureEndCalled; 70 } completeSearchRequest(Rect scrollBounds)71 void completeSearchRequest(Rect scrollBounds) { 72 assertNotNull("Did not receive search request", mSearchConsumer); 73 mSearchConsumer.accept(scrollBounds); 74 mModCount++; 75 } 76 verifyZeroInteractions()77 void verifyZeroInteractions() { 78 assertEquals("Expected zero interactions", 0, mModCount); 79 } 80 completeStartRequest()81 void completeStartRequest() { 82 assertNotNull("Did not receive start request", mStartOnReady); 83 if (mLastCancellationSignal != null && !mLastCancellationSignal.isCanceled()) { 84 mStartOnReady.run(); 85 } 86 } 87 completeImageRequest(Rect captured)88 void completeImageRequest(Rect captured) { 89 assertNotNull("Did not receive image request", mImageOnComplete); 90 if (mLastCancellationSignal != null && !mLastCancellationSignal.isCanceled()) { 91 mImageOnComplete.accept(captured); 92 } 93 } 94 completeEndRequest()95 void completeEndRequest() { 96 assertNotNull("Did not receive end request", mOnEndReady); 97 if (mLastCancellationSignal != null && !mLastCancellationSignal.isCanceled()) { 98 mOnEndReady.run(); 99 } 100 } 101 getLastCancellationSignal()102 public CancellationSignal getLastCancellationSignal() { 103 return mLastCancellationSignal; 104 } 105 } 106