blob: 848364584ef3e5b4fbdf14d90ea5abacb7f114af [file] [log] [blame]
James O'Learyfa5bb7a2019-09-05 13:43:29 -04001/*
2 * Copyright (C) 2019 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
17package com.android.internal.util;
18
19import static android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN;
20import static android.view.WindowManager.TAKE_SCREENSHOT_SELECTED_REGION;
21
22import static junit.framework.Assert.assertFalse;
23import static junit.framework.Assert.fail;
24
25import static org.mockito.ArgumentMatchers.any;
26import static org.mockito.ArgumentMatchers.anyInt;
27
28import android.content.Context;
29import android.os.Handler;
30import android.os.Looper;
31
32import androidx.test.runner.AndroidJUnit4;
33
34import org.junit.Before;
35import org.junit.Test;
36import org.junit.runner.RunWith;
37import org.mockito.Mockito;
38
39import java.util.concurrent.CountDownLatch;
40import java.util.concurrent.TimeUnit;
41
42@RunWith(AndroidJUnit4.class)
43public final class ScreenshotHelperTest {
44 private Context mContext;
45 private ScreenshotHelper mScreenshotHelper;
46 private Handler mHandler;
47
48
49 @Before
50 public void setUp() {
51 // `ScreenshotHelper.notifyScreenshotError()` calls `Context.sendBroadcastAsUser()` and
52 // `Context.bindServiceAsUser`.
53 //
54 // This raises a `SecurityException` if the device is locked. Calling either `Context`
55 // method results in a broadcast of `android.intent.action. USER_PRESENT`. Only the system
56 // process is allowed to broadcast that `Intent`.
57 mContext = Mockito.spy(Context.class);
58 Mockito.doNothing().when(mContext).sendBroadcastAsUser(any(), any());
59 Mockito.doReturn(true).when(mContext).bindServiceAsUser(any(), any(), anyInt(), any());
60
61 mHandler = new Handler(Looper.getMainLooper());
62 mScreenshotHelper = new ScreenshotHelper(mContext);
63 }
64
65 @Test
66 public void testFullscreenScreenshot() {
67 mScreenshotHelper.takeScreenshot(TAKE_SCREENSHOT_FULLSCREEN, false, false, mHandler, null);
68 }
69
70 @Test
71 public void testSelectedRegionScreenshot() {
72 mScreenshotHelper.takeScreenshot(TAKE_SCREENSHOT_SELECTED_REGION, false, false, mHandler,
73 null);
74 }
75
76 @Test
77 public void testScreenshotTimesOut() {
78 long timeoutMs = 10;
79
80 CountDownLatch lock = new CountDownLatch(1);
81 mScreenshotHelper.takeScreenshot(TAKE_SCREENSHOT_FULLSCREEN, false, false, timeoutMs,
82 mHandler,
83 worked -> {
84 assertFalse(worked);
85 lock.countDown();
86 });
87
88 try {
89 // Add tolerance for delay to prevent flakes.
90 long awaitDurationMs = timeoutMs + 100;
91 if (!lock.await(awaitDurationMs, TimeUnit.MILLISECONDS)) {
92 fail("lock never freed");
93 }
94 } catch (InterruptedException e) {
95 fail("lock interrupted");
96 }
97 }
98}