blob: 5a6823879942397d0adb9532c5b51c9a6877432d [file] [log] [blame]
Beth Thibodeau58772782020-02-18 20:55:38 -05001/*
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
17package com.android.systemui.qs.tiles;
18
19import static junit.framework.Assert.assertEquals;
20import static junit.framework.Assert.assertTrue;
21
22import static org.mockito.Mockito.times;
23import static org.mockito.Mockito.verify;
24import static org.mockito.Mockito.when;
25
26import android.service.quicksettings.Tile;
27import android.testing.AndroidTestingRunner;
28import android.testing.TestableLooper;
29
30import androidx.test.filters.SmallTest;
31
32import com.android.systemui.Dependency;
33import com.android.systemui.R;
34import com.android.systemui.SysuiTestCase;
35import com.android.systemui.qs.QSTileHost;
36import com.android.systemui.screenrecord.RecordingController;
Beth Thibodeau231ac9b2020-06-17 22:34:42 -040037import com.android.systemui.statusbar.phone.KeyguardDismissUtil;
Beth Thibodeau58772782020-02-18 20:55:38 -050038
39import org.junit.Before;
40import org.junit.Test;
41import org.junit.runner.RunWith;
42import org.mockito.Mock;
43import org.mockito.MockitoAnnotations;
44
45@RunWith(AndroidTestingRunner.class)
Beth Thibodeau231ac9b2020-06-17 22:34:42 -040046@TestableLooper.RunWithLooper(setAsMainLooper = true)
Beth Thibodeau58772782020-02-18 20:55:38 -050047@SmallTest
48public class ScreenRecordTileTest extends SysuiTestCase {
49
50 @Mock
51 private RecordingController mController;
52 @Mock
53 private QSTileHost mHost;
Beth Thibodeauabb338f2020-03-03 16:17:24 -050054 @Mock
Beth Thibodeau231ac9b2020-06-17 22:34:42 -040055 private KeyguardDismissUtil mKeyguardDismissUtil;
Beth Thibodeau58772782020-02-18 20:55:38 -050056
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 Thibodeau231ac9b2020-06-17 22:34:42 -040070 mTile = new ScreenRecordTile(mHost, mController, mKeyguardDismissUtil);
Beth Thibodeau58772782020-02-18 20:55:38 -050071 }
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 Thibodeau231ac9b2020-06-17 22:34:42 -040088 mTestableLooper.processAllMessages();
Beth Thibodeauf54cace2020-04-23 12:46:55 -040089 verify(mController, times(1)).getPromptIntent();
Beth Thibodeau58772782020-02-18 20:55:38 -050090 }
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 Kozynski0daa9702020-04-20 15:03:50 -0400140
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 Thibodeau58772782020-02-18 20:55:38 -0500148}