1 /*
2  * Copyright (C) 2020 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 com.android.systemui.screenshot;
18 
19 
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.anyInt;
25 import static org.mockito.ArgumentMatchers.isNull;
26 import static org.mockito.Mockito.doAnswer;
27 import static org.mockito.Mockito.mock;
28 
29 import android.graphics.Rect;
30 import android.os.RemoteException;
31 import android.testing.AndroidTestingRunner;
32 import android.view.Display;
33 import android.view.IScrollCaptureResponseListener;
34 import android.view.IWindowManager;
35 import android.view.ScrollCaptureResponse;
36 
37 import androidx.test.filters.SmallTest;
38 
39 import com.android.systemui.SysuiTestCase;
40 import com.android.systemui.screenshot.ScrollCaptureClient.CaptureResult;
41 import com.android.systemui.screenshot.ScrollCaptureClient.Session;
42 
43 import com.google.common.util.concurrent.ListenableFuture;
44 
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.MockitoAnnotations;
49 import org.mockito.stubbing.Answer;
50 
51 import java.util.concurrent.CountDownLatch;
52 import java.util.concurrent.ExecutionException;
53 import java.util.concurrent.TimeUnit;
54 import java.util.concurrent.TimeoutException;
55 
56 @SmallTest
57 @RunWith(AndroidTestingRunner.class)
58 public class ScrollCaptureClientTest extends SysuiTestCase {
59     private static final float MAX_PAGES = 3.0f;
60 
61     private IWindowManager mWm;
62 
63     @Before
setUp()64     public void setUp() {
65         MockitoAnnotations.initMocks(this);
66         mWm = mock(IWindowManager.class);
67     }
68 
69     @Test
testDetectAndConnect()70     public void testDetectAndConnect()
71             throws RemoteException, InterruptedException, ExecutionException, TimeoutException {
72         doAnswer((Answer<Void>) invocation -> {
73             IScrollCaptureResponseListener listener = invocation.getArgument(3);
74             listener.onScrollCaptureResponse(new ScrollCaptureResponse.Builder()
75                     .setBoundsInWindow(new Rect(0, 0, 100, 100))
76                     .setWindowBounds(new Rect(0, 0, 100, 100))
77                     .setConnection(new FakeScrollCaptureConnection())
78                     .build());
79             return null;
80         }).when(mWm).requestScrollCapture(/* displayId */ anyInt(), /* token */  isNull(),
81                 /* taskId */ anyInt(), any(IScrollCaptureResponseListener.class));
82 
83         // Create client
84         ScrollCaptureClient client = new ScrollCaptureClient(mWm, Runnable::run, mContext);
85 
86         // Request scroll capture
87         ListenableFuture<ScrollCaptureResponse> requestFuture =
88                 client.request(Display.DEFAULT_DISPLAY);
89         assertNotNull(requestFuture.get(100, TimeUnit.MILLISECONDS));
90 
91         ScrollCaptureResponse response = requestFuture.get();
92         assertTrue(response.isConnected());
93 
94         // Start a session
95         ListenableFuture<Session> startFuture = client.start(response, MAX_PAGES);
96         assertNotNull(startFuture.get(100, TimeUnit.MILLISECONDS));
97 
98         Session session = startFuture.get();
99         Rect request = new Rect(0, 0, session.getPageWidth(), session.getTileHeight());
100 
101         // Request a tile
102         ListenableFuture<CaptureResult> tileFuture = session.requestTile(0);
103         assertNotNull(tileFuture.get(100, TimeUnit.MILLISECONDS));
104 
105         CaptureResult result = tileFuture.get();
106         assertEquals(request, result.requested);
107         assertEquals(result.requested, result.captured);
108         assertNotNull(result.image);
109 
110         // End the session
111         ListenableFuture<Void> endFuture = session.end();
112         CountDownLatch latch = new CountDownLatch(1);
113         endFuture.addListener(latch::countDown, Runnable::run);
114         assertTrue(latch.await(100, TimeUnit.MILLISECONDS));
115     }
116 }
117