Beth Thibodeau | 5877278 | 2020-02-18 20:55:38 -0500 | [diff] [blame] | 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.screenrecord; |
| 18 | |
| 19 | import static junit.framework.Assert.assertFalse; |
| 20 | import static junit.framework.Assert.assertTrue; |
| 21 | |
| 22 | import static org.mockito.Mockito.verify; |
| 23 | |
| 24 | import android.app.PendingIntent; |
| 25 | import android.os.Looper; |
| 26 | import android.testing.AndroidTestingRunner; |
| 27 | |
| 28 | import androidx.test.filters.SmallTest; |
| 29 | |
| 30 | import com.android.systemui.SysuiTestCase; |
| 31 | |
| 32 | import org.junit.Before; |
| 33 | import org.junit.Test; |
| 34 | import org.junit.runner.RunWith; |
| 35 | import org.mockito.Mock; |
| 36 | import org.mockito.Mockito; |
| 37 | import org.mockito.MockitoAnnotations; |
| 38 | |
| 39 | @SmallTest |
| 40 | @RunWith(AndroidTestingRunner.class) |
| 41 | /** |
| 42 | * Tests for exception handling and bitmap configuration in adding smart actions to Screenshot |
| 43 | * Notification. |
| 44 | */ |
| 45 | public class RecordingControllerTest extends SysuiTestCase { |
| 46 | |
| 47 | @Mock |
| 48 | RecordingController.RecordingStateChangeCallback mCallback; |
| 49 | |
| 50 | RecordingController mController; |
| 51 | |
| 52 | @Before |
| 53 | public void setUp() { |
| 54 | MockitoAnnotations.initMocks(this); |
| 55 | mController = new RecordingController(mContext); |
| 56 | mController.addCallback(mCallback); |
| 57 | } |
| 58 | |
| 59 | // Test that when a countdown in progress is cancelled, the controller goes from starting to not |
| 60 | // starting, and notifies listeners. |
| 61 | @Test |
| 62 | public void testCancelCountdown() { |
| 63 | if (Looper.myLooper() == null) { |
| 64 | Looper.prepare(); |
| 65 | } |
| 66 | |
| 67 | mController.startCountdown(100, 10, null, null); |
| 68 | |
| 69 | assertTrue(mController.isStarting()); |
| 70 | assertFalse(mController.isRecording()); |
| 71 | |
| 72 | mController.cancelCountdown(); |
| 73 | |
| 74 | assertFalse(mController.isStarting()); |
| 75 | assertFalse(mController.isRecording()); |
| 76 | |
| 77 | verify(mCallback).onCountdownEnd(); |
| 78 | } |
| 79 | |
| 80 | // Test that when recording is started, the start intent is sent and listeners are notified. |
| 81 | @Test |
| 82 | public void testStartRecording() throws PendingIntent.CanceledException { |
| 83 | if (Looper.myLooper() == null) { |
| 84 | Looper.prepare(); |
| 85 | } |
| 86 | |
| 87 | PendingIntent startIntent = Mockito.mock(PendingIntent.class); |
| 88 | mController.startCountdown(0, 0, startIntent, null); |
| 89 | |
| 90 | verify(mCallback).onCountdownEnd(); |
| 91 | verify(startIntent).send(); |
| 92 | } |
| 93 | |
| 94 | // Test that when recording is stopped, the stop intent is sent and listeners are notified. |
| 95 | @Test |
| 96 | public void testStopRecording() throws PendingIntent.CanceledException { |
| 97 | if (Looper.myLooper() == null) { |
| 98 | Looper.prepare(); |
| 99 | } |
| 100 | |
| 101 | PendingIntent startIntent = Mockito.mock(PendingIntent.class); |
| 102 | PendingIntent stopIntent = Mockito.mock(PendingIntent.class); |
| 103 | |
| 104 | mController.startCountdown(0, 0, startIntent, stopIntent); |
| 105 | mController.stopRecording(); |
| 106 | |
| 107 | assertFalse(mController.isStarting()); |
| 108 | assertFalse(mController.isRecording()); |
| 109 | verify(stopIntent).send(); |
| 110 | verify(mCallback).onRecordingEnd(); |
| 111 | } |
| 112 | |
| 113 | // Test that updating the controller state works and notifies listeners. |
| 114 | @Test |
| 115 | public void testUpdateState() { |
| 116 | mController.updateState(true); |
| 117 | assertTrue(mController.isRecording()); |
| 118 | verify(mCallback).onRecordingStart(); |
| 119 | |
| 120 | mController.updateState(false); |
| 121 | assertFalse(mController.isRecording()); |
| 122 | verify(mCallback).onRecordingEnd(); |
| 123 | } |
| 124 | } |