| /* |
| * Copyright (C) 2020 The Android Open Source Project |
| * |
| * Licensed under the Apache License, Version 2.0 (the "License"); |
| * you may not use this file except in compliance with the License. |
| * You may obtain a copy of the License at |
| * |
| * http://www.apache.org/licenses/LICENSE-2.0 |
| * |
| * Unless required by applicable law or agreed to in writing, software |
| * distributed under the License is distributed on an "AS IS" BASIS, |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| * See the License for the specific language governing permissions and |
| * limitations under the License. |
| */ |
| |
| package com.android.systemui.screenrecord; |
| |
| import static junit.framework.Assert.assertFalse; |
| import static junit.framework.Assert.assertTrue; |
| |
| import static org.mockito.Mockito.verify; |
| |
| import android.app.PendingIntent; |
| import android.os.Looper; |
| import android.testing.AndroidTestingRunner; |
| |
| import androidx.test.filters.SmallTest; |
| |
| import com.android.systemui.SysuiTestCase; |
| |
| import org.junit.Before; |
| import org.junit.Test; |
| import org.junit.runner.RunWith; |
| import org.mockito.Mock; |
| import org.mockito.Mockito; |
| import org.mockito.MockitoAnnotations; |
| |
| @SmallTest |
| @RunWith(AndroidTestingRunner.class) |
| /** |
| * Tests for exception handling and bitmap configuration in adding smart actions to Screenshot |
| * Notification. |
| */ |
| public class RecordingControllerTest extends SysuiTestCase { |
| |
| @Mock |
| RecordingController.RecordingStateChangeCallback mCallback; |
| |
| RecordingController mController; |
| |
| @Before |
| public void setUp() { |
| MockitoAnnotations.initMocks(this); |
| mController = new RecordingController(mContext); |
| mController.addCallback(mCallback); |
| } |
| |
| // Test that when a countdown in progress is cancelled, the controller goes from starting to not |
| // starting, and notifies listeners. |
| @Test |
| public void testCancelCountdown() { |
| if (Looper.myLooper() == null) { |
| Looper.prepare(); |
| } |
| |
| mController.startCountdown(100, 10, null, null); |
| |
| assertTrue(mController.isStarting()); |
| assertFalse(mController.isRecording()); |
| |
| mController.cancelCountdown(); |
| |
| assertFalse(mController.isStarting()); |
| assertFalse(mController.isRecording()); |
| |
| verify(mCallback).onCountdownEnd(); |
| } |
| |
| // Test that when recording is started, the start intent is sent and listeners are notified. |
| @Test |
| public void testStartRecording() throws PendingIntent.CanceledException { |
| if (Looper.myLooper() == null) { |
| Looper.prepare(); |
| } |
| |
| PendingIntent startIntent = Mockito.mock(PendingIntent.class); |
| mController.startCountdown(0, 0, startIntent, null); |
| |
| verify(mCallback).onCountdownEnd(); |
| verify(startIntent).send(); |
| } |
| |
| // Test that when recording is stopped, the stop intent is sent and listeners are notified. |
| @Test |
| public void testStopRecording() throws PendingIntent.CanceledException { |
| if (Looper.myLooper() == null) { |
| Looper.prepare(); |
| } |
| |
| PendingIntent startIntent = Mockito.mock(PendingIntent.class); |
| PendingIntent stopIntent = Mockito.mock(PendingIntent.class); |
| |
| mController.startCountdown(0, 0, startIntent, stopIntent); |
| mController.stopRecording(); |
| |
| assertFalse(mController.isStarting()); |
| assertFalse(mController.isRecording()); |
| verify(stopIntent).send(); |
| verify(mCallback).onRecordingEnd(); |
| } |
| |
| // Test that updating the controller state works and notifies listeners. |
| @Test |
| public void testUpdateState() { |
| mController.updateState(true); |
| assertTrue(mController.isRecording()); |
| verify(mCallback).onRecordingStart(); |
| |
| mController.updateState(false); |
| assertFalse(mController.isRecording()); |
| verify(mCallback).onRecordingEnd(); |
| } |
| } |