1 /*
2  * Copyright (C) 2018 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.statusbar.notification.footer.ui.view;
18 
19 import static com.android.systemui.log.LogAssertKt.assertLogsWtf;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static junit.framework.Assert.assertFalse;
24 import static junit.framework.Assert.assertNotNull;
25 import static junit.framework.Assert.assertTrue;
26 
27 import static org.mockito.ArgumentMatchers.anyInt;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.clearInvocations;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.never;
32 import static org.mockito.Mockito.spy;
33 import static org.mockito.Mockito.verify;
34 
35 import android.content.Context;
36 import android.platform.test.annotations.DisableFlags;
37 import android.platform.test.annotations.EnableFlags;
38 import android.platform.test.flag.junit.FlagsParameterization;
39 import android.view.LayoutInflater;
40 import android.view.View;
41 import android.widget.TextView;
42 
43 import androidx.test.filters.SmallTest;
44 
45 import com.android.systemui.SysuiTestCase;
46 import com.android.systemui.res.R;
47 import com.android.systemui.statusbar.notification.footer.shared.FooterViewRefactor;
48 
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.junit.runner.RunWith;
52 
53 import platform.test.runner.parameterized.ParameterizedAndroidJunit4;
54 import platform.test.runner.parameterized.Parameters;
55 
56 import java.util.List;
57 
58 @SmallTest
59 @RunWith(ParameterizedAndroidJunit4.class)
60 public class FooterViewTest extends SysuiTestCase {
61 
62     @Parameters(name = "{0}")
getFlags()63     public static List<FlagsParameterization> getFlags() {
64         return FlagsParameterization.allCombinationsOf(FooterViewRefactor.FLAG_NAME);
65     }
66 
FooterViewTest(FlagsParameterization flags)67     public FooterViewTest(FlagsParameterization flags) {
68         mSetFlagsRule.setFlagsParameterization(flags);
69     }
70 
71     FooterView mView;
72 
73     Context mSpyContext = spy(mContext);
74 
75     @Before
setUp()76     public void setUp() {
77         mView = (FooterView) LayoutInflater.from(mSpyContext).inflate(
78                 R.layout.status_bar_notification_footer, null, false);
79         mView.setAnimationDuration(0);
80     }
81 
82     @Test
testViewsNotNull()83     public void testViewsNotNull() {
84         assertNotNull(mView.findContentView());
85         assertNotNull(mView.findSecondaryView());
86     }
87 
88     @Test
setDismissOnClick()89     public void setDismissOnClick() {
90         mView.setClearAllButtonClickListener(mock(View.OnClickListener.class));
91         assertTrue(mView.findSecondaryView().hasOnClickListeners());
92     }
93 
94     @Test
setManageOnClick()95     public void setManageOnClick() {
96         mView.setManageButtonClickListener(mock(View.OnClickListener.class));
97         assertTrue(mView.findViewById(R.id.manage_text).hasOnClickListeners());
98     }
99 
100     @Test
101     @DisableFlags(FooterViewRefactor.FLAG_NAME)
setHistoryShown()102     public void setHistoryShown() {
103         mView.showHistory(true);
104         assertTrue(mView.isHistoryShown());
105         assertTrue(((TextView) mView.findViewById(R.id.manage_text))
106                 .getText().toString().contains("History"));
107     }
108 
109     @Test
110     @DisableFlags(FooterViewRefactor.FLAG_NAME)
setHistoryNotShown()111     public void setHistoryNotShown() {
112         mView.showHistory(false);
113         assertFalse(mView.isHistoryShown());
114         assertTrue(((TextView) mView.findViewById(R.id.manage_text))
115                 .getText().toString().contains("Manage"));
116     }
117 
118     @Test
testPerformVisibilityAnimation()119     public void testPerformVisibilityAnimation() {
120         mView.setVisible(false /* visible */, false /* animate */);
121         assertFalse(mView.isVisible());
122 
123         mView.setVisible(true /* visible */, true /* animate */);
124     }
125 
126     @Test
testPerformSecondaryVisibilityAnimation()127     public void testPerformSecondaryVisibilityAnimation() {
128         mView.setClearAllButtonVisible(false /* visible */, false /* animate */);
129         assertFalse(mView.isClearAllButtonVisible());
130 
131         mView.setClearAllButtonVisible(true /* visible */, true /* animate */);
132     }
133 
134     @Test
135     @EnableFlags(FooterViewRefactor.FLAG_NAME)
testSetManageOrHistoryButtonText_resourceOnlyFetchedOnce()136     public void testSetManageOrHistoryButtonText_resourceOnlyFetchedOnce() {
137         int resId = R.string.manage_notifications_history_text;
138         mView.setManageOrHistoryButtonText(resId);
139         verify(mSpyContext).getString(eq(resId));
140 
141         clearInvocations(mSpyContext);
142 
143         assertThat(((TextView) mView.findViewById(R.id.manage_text))
144                 .getText().toString()).contains("History");
145 
146         // Set it a few more times, it shouldn't lead to the resource being fetched again
147         mView.setManageOrHistoryButtonText(resId);
148         mView.setManageOrHistoryButtonText(resId);
149 
150         verify(mSpyContext, never()).getString(anyInt());
151     }
152 
153     @Test
154     @DisableFlags(FooterViewRefactor.FLAG_NAME)
testSetManageOrHistoryButtonText_expectsFlagEnabled()155     public void testSetManageOrHistoryButtonText_expectsFlagEnabled() {
156         clearInvocations(mSpyContext);
157         int resId = R.string.manage_notifications_history_text;
158         assertLogsWtf(() -> mView.setManageOrHistoryButtonText(resId));
159         verify(mSpyContext, never()).getString(anyInt());
160     }
161 
162     @Test
163     @EnableFlags(FooterViewRefactor.FLAG_NAME)
testSetManageOrHistoryButtonDescription_resourceOnlyFetchedOnce()164     public void testSetManageOrHistoryButtonDescription_resourceOnlyFetchedOnce() {
165         int resId = R.string.manage_notifications_history_text;
166         mView.setManageOrHistoryButtonDescription(resId);
167         verify(mSpyContext).getString(eq(resId));
168 
169         clearInvocations(mSpyContext);
170 
171         assertThat(((TextView) mView.findViewById(R.id.manage_text))
172                 .getContentDescription().toString()).contains("History");
173 
174         // Set it a few more times, it shouldn't lead to the resource being fetched again
175         mView.setManageOrHistoryButtonDescription(resId);
176         mView.setManageOrHistoryButtonDescription(resId);
177 
178         verify(mSpyContext, never()).getString(anyInt());
179     }
180 
181     @Test
182     @DisableFlags(FooterViewRefactor.FLAG_NAME)
testSetManageOrHistoryButtonDescription_expectsFlagEnabled()183     public void testSetManageOrHistoryButtonDescription_expectsFlagEnabled() {
184         clearInvocations(mSpyContext);
185         int resId = R.string.accessibility_clear_all;
186         assertLogsWtf(() -> mView.setManageOrHistoryButtonDescription(resId));
187         verify(mSpyContext, never()).getString(anyInt());
188     }
189 
190     @Test
191     @EnableFlags(FooterViewRefactor.FLAG_NAME)
testSetClearAllButtonText_resourceOnlyFetchedOnce()192     public void testSetClearAllButtonText_resourceOnlyFetchedOnce() {
193         int resId = R.string.clear_all_notifications_text;
194         mView.setClearAllButtonText(resId);
195         verify(mSpyContext).getString(eq(resId));
196 
197         clearInvocations(mSpyContext);
198 
199         assertThat(((TextView) mView.findViewById(R.id.dismiss_text))
200                 .getText().toString()).contains("Clear all");
201 
202         // Set it a few more times, it shouldn't lead to the resource being fetched again
203         mView.setClearAllButtonText(resId);
204         mView.setClearAllButtonText(resId);
205 
206         verify(mSpyContext, never()).getString(anyInt());
207     }
208 
209     @Test
210     @DisableFlags(FooterViewRefactor.FLAG_NAME)
testSetClearAllButtonText_expectsFlagEnabled()211     public void testSetClearAllButtonText_expectsFlagEnabled() {
212         clearInvocations(mSpyContext);
213         int resId = R.string.clear_all_notifications_text;
214         assertLogsWtf(() -> mView.setClearAllButtonText(resId));
215         verify(mSpyContext, never()).getString(anyInt());
216     }
217 
218     @Test
219     @EnableFlags(FooterViewRefactor.FLAG_NAME)
testSetClearAllButtonDescription_resourceOnlyFetchedOnce()220     public void testSetClearAllButtonDescription_resourceOnlyFetchedOnce() {
221         int resId = R.string.accessibility_clear_all;
222         mView.setClearAllButtonDescription(resId);
223         verify(mSpyContext).getString(eq(resId));
224 
225         clearInvocations(mSpyContext);
226 
227         assertThat(((TextView) mView.findViewById(R.id.dismiss_text))
228                 .getContentDescription().toString()).contains("Clear all notifications");
229 
230         // Set it a few more times, it shouldn't lead to the resource being fetched again
231         mView.setClearAllButtonDescription(resId);
232         mView.setClearAllButtonDescription(resId);
233 
234         verify(mSpyContext, never()).getString(anyInt());
235     }
236 
237     @Test
238     @DisableFlags(FooterViewRefactor.FLAG_NAME)
testSetClearAllButtonDescription_expectsFlagEnabled()239     public void testSetClearAllButtonDescription_expectsFlagEnabled() {
240         clearInvocations(mSpyContext);
241         int resId = R.string.accessibility_clear_all;
242         assertLogsWtf(() -> mView.setClearAllButtonDescription(resId));
243         verify(mSpyContext, never()).getString(anyInt());
244     }
245 
246     @Test
247     @EnableFlags(FooterViewRefactor.FLAG_NAME)
testSetMessageString_resourceOnlyFetchedOnce()248     public void testSetMessageString_resourceOnlyFetchedOnce() {
249         int resId = R.string.unlock_to_see_notif_text;
250         mView.setMessageString(resId);
251         verify(mSpyContext).getString(eq(resId));
252 
253         clearInvocations(mSpyContext);
254 
255         assertThat(((TextView) mView.findViewById(R.id.unlock_prompt_footer))
256                 .getText().toString()).contains("Unlock");
257 
258         // Set it a few more times, it shouldn't lead to the resource being fetched again
259         mView.setMessageString(resId);
260         mView.setMessageString(resId);
261 
262         verify(mSpyContext, never()).getString(anyInt());
263     }
264 
265     @Test
266     @DisableFlags(FooterViewRefactor.FLAG_NAME)
testSetMessageString_expectsFlagEnabled()267     public void testSetMessageString_expectsFlagEnabled() {
268         clearInvocations(mSpyContext);
269         int resId = R.string.unlock_to_see_notif_text;
270         assertLogsWtf(() -> mView.setMessageString(resId));
271         verify(mSpyContext, never()).getString(anyInt());
272     }
273 
274     @Test
275     @EnableFlags(FooterViewRefactor.FLAG_NAME)
testSetMessageIcon_resourceOnlyFetchedOnce()276     public void testSetMessageIcon_resourceOnlyFetchedOnce() {
277         int resId = R.drawable.ic_friction_lock_closed;
278         mView.setMessageIcon(resId);
279         verify(mSpyContext).getDrawable(eq(resId));
280 
281         clearInvocations(mSpyContext);
282 
283         // Set it a few more times, it shouldn't lead to the resource being fetched again
284         mView.setMessageIcon(resId);
285         mView.setMessageIcon(resId);
286 
287         verify(mSpyContext, never()).getDrawable(anyInt());
288     }
289 
290     @Test
291     @DisableFlags(FooterViewRefactor.FLAG_NAME)
testSetMessageIcon_expectsFlagEnabled()292     public void testSetMessageIcon_expectsFlagEnabled() {
293         clearInvocations(mSpyContext);
294         int resId = R.drawable.ic_friction_lock_closed;
295         assertLogsWtf(() -> mView.setMessageIcon(resId));
296         verify(mSpyContext, never()).getDrawable(anyInt());
297     }
298 
299     @Test
testSetFooterLabelVisible()300     public void testSetFooterLabelVisible() {
301         mView.setFooterLabelVisible(true);
302         assertThat(mView.findViewById(R.id.unlock_prompt_footer).getVisibility())
303                 .isEqualTo(View.VISIBLE);
304     }
305 
306     @Test
testSetFooterLabelInvisible()307     public void testSetFooterLabelInvisible() {
308         mView.setFooterLabelVisible(false);
309         assertThat(mView.findViewById(R.id.unlock_prompt_footer).getVisibility())
310                 .isEqualTo(View.GONE);
311     }
312 }
313 
314