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.qs.tiles; |
| 18 | |
| 19 | import static junit.framework.Assert.assertEquals; |
| 20 | import static junit.framework.Assert.assertTrue; |
| 21 | |
| 22 | import static org.mockito.Mockito.times; |
| 23 | import static org.mockito.Mockito.verify; |
| 24 | import static org.mockito.Mockito.when; |
| 25 | |
| 26 | import android.service.quicksettings.Tile; |
| 27 | import android.testing.AndroidTestingRunner; |
| 28 | import android.testing.TestableLooper; |
| 29 | |
| 30 | import androidx.test.filters.SmallTest; |
| 31 | |
| 32 | import com.android.systemui.Dependency; |
| 33 | import com.android.systemui.R; |
| 34 | import com.android.systemui.SysuiTestCase; |
| 35 | import com.android.systemui.qs.QSTileHost; |
| 36 | import com.android.systemui.screenrecord.RecordingController; |
Beth Thibodeau | 231ac9b | 2020-06-17 22:34:42 -0400 | [diff] [blame] | 37 | import com.android.systemui.statusbar.phone.KeyguardDismissUtil; |
Beth Thibodeau | 5877278 | 2020-02-18 20:55:38 -0500 | [diff] [blame] | 38 | |
| 39 | import org.junit.Before; |
| 40 | import org.junit.Test; |
| 41 | import org.junit.runner.RunWith; |
| 42 | import org.mockito.Mock; |
| 43 | import org.mockito.MockitoAnnotations; |
| 44 | |
| 45 | @RunWith(AndroidTestingRunner.class) |
Beth Thibodeau | 231ac9b | 2020-06-17 22:34:42 -0400 | [diff] [blame] | 46 | @TestableLooper.RunWithLooper(setAsMainLooper = true) |
Beth Thibodeau | 5877278 | 2020-02-18 20:55:38 -0500 | [diff] [blame] | 47 | @SmallTest |
| 48 | public class ScreenRecordTileTest extends SysuiTestCase { |
| 49 | |
| 50 | @Mock |
| 51 | private RecordingController mController; |
| 52 | @Mock |
| 53 | private QSTileHost mHost; |
Beth Thibodeau | abb338f | 2020-03-03 16:17:24 -0500 | [diff] [blame] | 54 | @Mock |
Beth Thibodeau | 231ac9b | 2020-06-17 22:34:42 -0400 | [diff] [blame] | 55 | private KeyguardDismissUtil mKeyguardDismissUtil; |
Beth Thibodeau | 5877278 | 2020-02-18 20:55:38 -0500 | [diff] [blame] | 56 | |
| 57 | private TestableLooper mTestableLooper; |
| 58 | private ScreenRecordTile mTile; |
| 59 | |
| 60 | @Before |
| 61 | public void setUp() throws Exception { |
| 62 | MockitoAnnotations.initMocks(this); |
| 63 | |
| 64 | mTestableLooper = TestableLooper.get(this); |
| 65 | mDependency.injectTestDependency(Dependency.BG_LOOPER, mTestableLooper.getLooper()); |
| 66 | mController = mDependency.injectMockDependency(RecordingController.class); |
| 67 | |
| 68 | when(mHost.getContext()).thenReturn(mContext); |
| 69 | |
Beth Thibodeau | 231ac9b | 2020-06-17 22:34:42 -0400 | [diff] [blame] | 70 | mTile = new ScreenRecordTile(mHost, mController, mKeyguardDismissUtil); |
Beth Thibodeau | 5877278 | 2020-02-18 20:55:38 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | // Test that the tile is inactive and labeled correctly when the controller is neither starting |
| 74 | // or recording, and that clicking on the tile in this state brings up the record prompt |
| 75 | @Test |
| 76 | public void testNotActive() { |
| 77 | when(mController.isStarting()).thenReturn(false); |
| 78 | when(mController.isRecording()).thenReturn(false); |
| 79 | |
| 80 | mTile.refreshState(); |
| 81 | mTestableLooper.processAllMessages(); |
| 82 | |
| 83 | assertEquals(Tile.STATE_INACTIVE, mTile.getState().state); |
| 84 | assertTrue(mTile.getState().secondaryLabel.toString().equals( |
| 85 | mContext.getString(R.string.quick_settings_screen_record_start))); |
| 86 | |
| 87 | mTile.handleClick(); |
Beth Thibodeau | 231ac9b | 2020-06-17 22:34:42 -0400 | [diff] [blame] | 88 | mTestableLooper.processAllMessages(); |
Beth Thibodeau | f54cace | 2020-04-23 12:46:55 -0400 | [diff] [blame] | 89 | verify(mController, times(1)).getPromptIntent(); |
Beth Thibodeau | 5877278 | 2020-02-18 20:55:38 -0500 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | // Test that the tile is active and labeled correctly when the controller is starting |
| 93 | @Test |
| 94 | public void testIsStarting() { |
| 95 | when(mController.isStarting()).thenReturn(true); |
| 96 | when(mController.isRecording()).thenReturn(false); |
| 97 | |
| 98 | mTile.refreshState(); |
| 99 | mTestableLooper.processAllMessages(); |
| 100 | |
| 101 | assertEquals(Tile.STATE_ACTIVE, mTile.getState().state); |
| 102 | assertTrue(mTile.getState().secondaryLabel.toString().endsWith("...")); |
| 103 | } |
| 104 | |
| 105 | // Test that the tile cancels countdown if it is clicked when the controller is starting |
| 106 | @Test |
| 107 | public void testCancelRecording() { |
| 108 | when(mController.isStarting()).thenReturn(true); |
| 109 | when(mController.isRecording()).thenReturn(false); |
| 110 | |
| 111 | mTile.handleClick(); |
| 112 | |
| 113 | verify(mController, times(1)).cancelCountdown(); |
| 114 | } |
| 115 | |
| 116 | // Test that the tile is active and labeled correctly when the controller is recording |
| 117 | @Test |
| 118 | public void testIsRecording() { |
| 119 | when(mController.isStarting()).thenReturn(false); |
| 120 | when(mController.isRecording()).thenReturn(true); |
| 121 | |
| 122 | mTile.refreshState(); |
| 123 | mTestableLooper.processAllMessages(); |
| 124 | |
| 125 | assertEquals(Tile.STATE_ACTIVE, mTile.getState().state); |
| 126 | assertTrue(mTile.getState().secondaryLabel.toString().equals( |
| 127 | mContext.getString(R.string.quick_settings_screen_record_stop))); |
| 128 | } |
| 129 | |
| 130 | // Test that the tile stops the recording if it is clicked when the controller is recording |
| 131 | @Test |
| 132 | public void testStopRecording() { |
| 133 | when(mController.isStarting()).thenReturn(false); |
| 134 | when(mController.isRecording()).thenReturn(true); |
| 135 | |
| 136 | mTile.handleClick(); |
| 137 | |
| 138 | verify(mController, times(1)).stopRecording(); |
| 139 | } |
Fabian Kozynski | 0daa970 | 2020-04-20 15:03:50 -0400 | [diff] [blame] | 140 | |
| 141 | @Test |
| 142 | public void testContentDescriptionHasTileName() { |
| 143 | mTile.refreshState(); |
| 144 | mTestableLooper.processAllMessages(); |
| 145 | |
| 146 | assertTrue(mTile.getState().contentDescription.toString().contains(mTile.getState().label)); |
| 147 | } |
Beth Thibodeau | 5877278 | 2020-02-18 20:55:38 -0500 | [diff] [blame] | 148 | } |