1 /*
2  * Copyright 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 package com.android.systemui.statusbar;
17 
18 import static com.google.common.truth.Truth.assertThat;
19 
20 import android.app.Notification;
21 import android.app.PendingIntent;
22 import android.app.RemoteInput;
23 import android.content.Intent;
24 import android.graphics.drawable.Icon;
25 
26 import androidx.test.filters.SmallTest;
27 
28 import com.android.internal.R;
29 import com.android.systemui.SysuiTestCase;
30 
31 import org.junit.Test;
32 
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 
37 @SmallTest
38 public class NotificationUiAdjustmentTest extends SysuiTestCase {
39 
40     @Test
needReinflate_differentLength()41     public void needReinflate_differentLength() {
42         PendingIntent pendingIntent =
43                 PendingIntent.getActivity(mContext, 0,
44                         new Intent().setPackage(mContext.getPackageName()),
45                         PendingIntent.FLAG_MUTABLE);
46         Notification.Action action =
47                 createActionBuilder("first", R.drawable.ic_corp_icon, pendingIntent).build();
48         assertThat(NotificationUiAdjustment.needReinflate(
49                 createUiAdjustmentFromSmartActions("first", Collections.emptyList()),
50                 createUiAdjustmentFromSmartActions("second", Collections.singletonList(action))))
51                 .isTrue();
52     }
53 
54     @Test
needReinflate_differentLabels()55     public void needReinflate_differentLabels() {
56         PendingIntent pendingIntent =
57                 PendingIntent.getActivity(mContext, 0,
58                         new Intent().setPackage(mContext.getPackageName()),
59                         PendingIntent.FLAG_MUTABLE);
60         Notification.Action firstAction =
61                 createActionBuilder("first", R.drawable.ic_corp_icon, pendingIntent).build();
62         Notification.Action secondAction =
63                 createActionBuilder("second", R.drawable.ic_corp_icon, pendingIntent).build();
64 
65         assertThat(NotificationUiAdjustment.needReinflate(
66                 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
67                 createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction))))
68                 .isTrue();
69     }
70 
71     @Test
needReinflate_differentIcons()72     public void needReinflate_differentIcons() {
73         PendingIntent pendingIntent =
74                 PendingIntent.getActivity(mContext, 0,
75                         new Intent().setPackage(mContext.getPackageName()),
76                         PendingIntent.FLAG_MUTABLE);
77         Notification.Action firstAction =
78                 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent).build();
79         Notification.Action secondAction =
80                 createActionBuilder("same", com.android.settingslib.R.drawable.ic_account_circle,
81                         pendingIntent).build();
82 
83         assertThat(NotificationUiAdjustment.needReinflate(
84                 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
85                 createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction))))
86                 .isTrue();
87     }
88 
89     @Test
needReinflate_differentPendingIntent()90     public void needReinflate_differentPendingIntent() {
91         PendingIntent firstPendingIntent =
92                 PendingIntent.getActivity(mContext, 0,
93                         new Intent(Intent.ACTION_VIEW).setPackage(mContext.getPackageName()),
94                         PendingIntent.FLAG_MUTABLE);
95         PendingIntent secondPendingIntent =
96                 PendingIntent.getActivity(mContext, 0,
97                         new Intent(Intent.ACTION_PROCESS_TEXT)
98                                 .setPackage(mContext.getPackageName()),
99                         PendingIntent.FLAG_MUTABLE);
100         Notification.Action firstAction =
101                 createActionBuilder("same", R.drawable.ic_corp_icon, firstPendingIntent)
102                         .build();
103         Notification.Action secondAction =
104                 createActionBuilder("same", R.drawable.ic_corp_icon, secondPendingIntent)
105                         .build();
106 
107         assertThat(NotificationUiAdjustment.needReinflate(
108                 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
109                 createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction))))
110                 .isTrue();
111     }
112 
113     @Test
needReinflate_differentChoices()114     public void needReinflate_differentChoices() {
115         PendingIntent pendingIntent =
116                 PendingIntent.getActivity(mContext, 0,
117                         new Intent().setPackage(mContext.getPackageName()),
118                         PendingIntent.FLAG_MUTABLE);
119 
120         RemoteInput firstRemoteInput =
121                 createRemoteInput("same", "same", new CharSequence[] {"first"});
122         RemoteInput secondRemoteInput =
123                 createRemoteInput("same", "same", new CharSequence[] {"second"});
124 
125         Notification.Action firstAction =
126                 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
127                         .addRemoteInput(firstRemoteInput)
128                         .build();
129         Notification.Action secondAction =
130                 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
131                         .addRemoteInput(secondRemoteInput)
132                         .build();
133 
134         assertThat(NotificationUiAdjustment.needReinflate(
135                 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
136                 createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction))))
137                 .isTrue();
138     }
139 
140     @Test
needReinflate_differentRemoteInputLabel()141     public void needReinflate_differentRemoteInputLabel() {
142         PendingIntent pendingIntent =
143                 PendingIntent.getActivity(mContext, 0,
144                         new Intent().setPackage(mContext.getPackageName()),
145                         PendingIntent.FLAG_MUTABLE);
146 
147         RemoteInput firstRemoteInput =
148                 createRemoteInput("same", "first", new CharSequence[] {"same"});
149         RemoteInput secondRemoteInput =
150                 createRemoteInput("same", "second", new CharSequence[] {"same"});
151 
152         Notification.Action firstAction =
153                 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
154                         .addRemoteInput(firstRemoteInput)
155                         .build();
156         Notification.Action secondAction =
157                 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
158                         .addRemoteInput(secondRemoteInput)
159                         .build();
160 
161         assertThat(NotificationUiAdjustment.needReinflate(
162                 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
163                 createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction))))
164                 .isTrue();
165     }
166 
167     @Test
needReinflate_negative()168     public void needReinflate_negative() {
169         PendingIntent pendingIntent =
170                 PendingIntent.getActivity(mContext, 0,
171                         new Intent().setPackage(mContext.getPackageName()),
172                         PendingIntent.FLAG_MUTABLE);
173         RemoteInput firstRemoteInput =
174                 createRemoteInput("same", "same", new CharSequence[] {"same"});
175         RemoteInput secondRemoteInput =
176                 createRemoteInput("same", "same", new CharSequence[] {"same"});
177 
178         Notification.Action firstAction =
179                 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
180                         .addRemoteInput(firstRemoteInput).build();
181         Notification.Action secondAction =
182                 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
183                         .addRemoteInput(secondRemoteInput).build();
184 
185         assertThat(NotificationUiAdjustment.needReinflate(
186                 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
187                 createUiAdjustmentFromSmartActions(
188                         "second", Collections.singletonList(secondAction))))
189                 .isFalse();
190     }
191 
192     @Test
needReinflate_differentSmartReplies()193     public void needReinflate_differentSmartReplies() {
194         assertThat(NotificationUiAdjustment.needReinflate(
195                 createUiAdjustmentFromSmartReplies("first", new CharSequence[]{"a", "b"}),
196                 createUiAdjustmentFromSmartReplies("first", new CharSequence[] {"b", "a"})))
197                 .isTrue();
198     }
199 
200     @Test
needReinflate_sameSmartReplies()201     public void needReinflate_sameSmartReplies() {
202         assertThat(NotificationUiAdjustment.needReinflate(
203                 createUiAdjustmentFromSmartReplies("first", new CharSequence[] {"a", "b"}),
204                 createUiAdjustmentFromSmartReplies("first", new CharSequence[] {"a", "b"})))
205                 .isFalse();
206     }
207 
208     @Test
needReinflate_bothConversation()209     public void needReinflate_bothConversation() {
210         assertThat(NotificationUiAdjustment.needReinflate(
211                 createUiAdjustmentForConversation("first", true),
212                 createUiAdjustmentForConversation("first", true)))
213                 .isFalse();
214     }
215 
216     @Test
needReinflate_neitherConversation()217     public void needReinflate_neitherConversation() {
218         assertThat(NotificationUiAdjustment.needReinflate(
219                 createUiAdjustmentForConversation("first", false),
220                 createUiAdjustmentForConversation("first", false)))
221                 .isFalse();
222     }
223 
224     @Test
needReinflate_differentIsConversation()225     public void needReinflate_differentIsConversation() {
226         assertThat(NotificationUiAdjustment.needReinflate(
227                 createUiAdjustmentForConversation("first", false),
228                 createUiAdjustmentForConversation("first", true)))
229                 .isTrue();
230     }
231 
createActionBuilder( String title, int drawableRes, PendingIntent pendingIntent)232     private Notification.Action.Builder createActionBuilder(
233             String title, int drawableRes, PendingIntent pendingIntent) {
234         return new Notification.Action.Builder(
235                 Icon.createWithResource(mContext, drawableRes), title, pendingIntent);
236     }
237 
createRemoteInput(String resultKey, String label, CharSequence[] choices)238     private RemoteInput createRemoteInput(String resultKey, String label, CharSequence[] choices) {
239         return new RemoteInput.Builder(resultKey).setLabel(label).setChoices(choices).build();
240     }
241 
createUiAdjustmentFromSmartActions( String key, List<Notification.Action> actions)242     private NotificationUiAdjustment createUiAdjustmentFromSmartActions(
243             String key, List<Notification.Action> actions) {
244         return new NotificationUiAdjustment(key, actions, null, false);
245     }
246 
createUiAdjustmentFromSmartReplies( String key, CharSequence[] replies)247     private NotificationUiAdjustment createUiAdjustmentFromSmartReplies(
248             String key, CharSequence[] replies) {
249         return new NotificationUiAdjustment(key, null, Arrays.asList(replies), false);
250     }
251 
createUiAdjustmentForConversation( String key, boolean isConversation)252     private NotificationUiAdjustment createUiAdjustmentForConversation(
253             String key, boolean isConversation) {
254         return new NotificationUiAdjustment(key, null, null, isConversation);
255     }
256 }
257