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.cts; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertNotNull; 21 22 import android.graphics.Rect; 23 import android.os.CancellationSignal; 24 import android.view.ScrollCaptureCallback; 25 import android.view.ScrollCaptureSession; 26 27 import androidx.annotation.NonNull; 28 29 import java.util.function.Consumer; 30 31 public class MockScrollCaptureCallback implements ScrollCaptureCallback { 32 private Consumer<Rect> mSearchConsumer; 33 private Runnable mStartOnReady; 34 private Consumer<Rect> mImageOnComplete; 35 private Runnable mOnEndReady; 36 private int mModCount; 37 38 @Override onScrollCaptureSearch(@onNull CancellationSignal signal, @NonNull Consumer<Rect> onReady)39 public void onScrollCaptureSearch(@NonNull CancellationSignal signal, 40 @NonNull Consumer<Rect> onReady) { 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 mStartOnReady = onReady; 49 mModCount++; 50 } 51 52 @Override onScrollCaptureImageRequest(@onNull ScrollCaptureSession session, @NonNull CancellationSignal signal, @NonNull Rect captureArea, @NonNull Consumer<Rect> onComplete)53 public void onScrollCaptureImageRequest(@NonNull ScrollCaptureSession session, 54 @NonNull CancellationSignal signal, @NonNull Rect captureArea, 55 @NonNull Consumer<Rect> onComplete) { 56 mImageOnComplete = onComplete; 57 mModCount++; 58 } 59 60 @Override onScrollCaptureEnd(@onNull Runnable onReady)61 public void onScrollCaptureEnd(@NonNull Runnable onReady) { 62 mOnEndReady = onReady; 63 } 64 completeSearchRequest(Rect scrollBounds)65 void completeSearchRequest(Rect scrollBounds) { 66 assertNotNull("Did not receive search request", mSearchConsumer); 67 mSearchConsumer.accept(scrollBounds); 68 mModCount++; 69 } 70 verifyZeroInteractions()71 void verifyZeroInteractions() { 72 assertEquals("Expected zero interactions", 0, mModCount); 73 } 74 completeStartRequest()75 void completeStartRequest() { 76 assertNotNull("Did not receive start request", mStartOnReady); 77 mStartOnReady.run(); 78 } 79 completeImageRequest(Rect captured)80 void completeImageRequest(Rect captured) { 81 assertNotNull("Did not receive image request", mImageOnComplete); 82 mImageOnComplete.accept(captured); 83 } 84 completeEndRequest()85 void completeEndRequest() { 86 assertNotNull("Did not receive end request", mOnEndReady); 87 mOnEndReady.run(); 88 } 89 } 90