James O'Leary | fa5bb7a | 2019-09-05 13:43:29 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.internal.util; |
| 18 | |
| 19 | import static android.view.WindowManager.TAKE_SCREENSHOT_FULLSCREEN; |
| 20 | import static android.view.WindowManager.TAKE_SCREENSHOT_SELECTED_REGION; |
| 21 | |
| 22 | import static junit.framework.Assert.assertFalse; |
| 23 | import static junit.framework.Assert.fail; |
| 24 | |
| 25 | import static org.mockito.ArgumentMatchers.any; |
| 26 | import static org.mockito.ArgumentMatchers.anyInt; |
| 27 | |
| 28 | import android.content.Context; |
| 29 | import android.os.Handler; |
| 30 | import android.os.Looper; |
| 31 | |
| 32 | import androidx.test.runner.AndroidJUnit4; |
| 33 | |
| 34 | import org.junit.Before; |
| 35 | import org.junit.Test; |
| 36 | import org.junit.runner.RunWith; |
| 37 | import org.mockito.Mockito; |
| 38 | |
| 39 | import java.util.concurrent.CountDownLatch; |
| 40 | import java.util.concurrent.TimeUnit; |
| 41 | |
| 42 | @RunWith(AndroidJUnit4.class) |
| 43 | public 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 | } |