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