blob: f63389c911c91cc288818b61d28c5cf9019b4564 [file] [log] [blame]
Tony Mak628cb932018-06-19 18:30:41 +01001/*
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 */
16package com.android.systemui.statusbar;
17
18import static com.google.common.truth.Truth.assertThat;
19
20import android.app.Notification;
21import android.app.PendingIntent;
22import android.app.RemoteInput;
23import android.content.Intent;
24import android.graphics.drawable.Icon;
Brett Chabot84151d92019-02-27 15:37:59 -080025
26import androidx.test.filters.SmallTest;
Tony Mak628cb932018-06-19 18:30:41 +010027
28import com.android.internal.R;
29import com.android.systemui.SysuiTestCase;
30
31import org.junit.Test;
32
33import java.util.Collections;
Tony Makc9acf672018-07-20 13:58:24 +020034import java.util.List;
Tony Mak628cb932018-06-19 18:30:41 +010035
36@SmallTest
37public class NotificationUiAdjustmentTest extends SysuiTestCase {
38
39 @Test
40 public void needReinflate_differentLength() {
41 PendingIntent pendingIntent =
42 PendingIntent.getActivity(mContext, 0, new Intent(), 0);
43 Notification.Action action =
44 createActionBuilder("first", R.drawable.ic_corp_icon, pendingIntent).build();
45 assertThat(NotificationUiAdjustment.needReinflate(
Tony Makc9acf672018-07-20 13:58:24 +020046 createUiAdjustmentFromSmartActions("first", Collections.emptyList()),
47 createUiAdjustmentFromSmartActions("second", Collections.singletonList(action))))
Tony Mak628cb932018-06-19 18:30:41 +010048 .isTrue();
49 }
50
51 @Test
52 public void needReinflate_differentLabels() {
53 PendingIntent pendingIntent =
54 PendingIntent.getActivity(mContext, 0, new Intent(), 0);
55 Notification.Action firstAction =
56 createActionBuilder("first", R.drawable.ic_corp_icon, pendingIntent).build();
57 Notification.Action secondAction =
58 createActionBuilder("second", R.drawable.ic_corp_icon, pendingIntent).build();
59
60 assertThat(NotificationUiAdjustment.needReinflate(
Tony Makc9acf672018-07-20 13:58:24 +020061 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
62 createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction))))
Tony Mak628cb932018-06-19 18:30:41 +010063 .isTrue();
64 }
65
66 @Test
67 public void needReinflate_differentIcons() {
68 PendingIntent pendingIntent =
69 PendingIntent.getActivity(mContext, 0, new Intent(), 0);
70 Notification.Action firstAction =
71 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent).build();
72 Notification.Action secondAction =
73 createActionBuilder("same", R.drawable.ic_account_circle, pendingIntent)
74 .build();
75
76 assertThat(NotificationUiAdjustment.needReinflate(
Tony Makc9acf672018-07-20 13:58:24 +020077 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
78 createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction))))
Tony Mak628cb932018-06-19 18:30:41 +010079 .isTrue();
80 }
81
82 @Test
83 public void needReinflate_differentPendingIntent() {
84 PendingIntent firstPendingIntent =
85 PendingIntent.getActivity(mContext, 0, new Intent(Intent.ACTION_VIEW), 0);
86 PendingIntent secondPendingIntent =
87 PendingIntent.getActivity(mContext, 0, new Intent(Intent.ACTION_PROCESS_TEXT), 0);
88 Notification.Action firstAction =
89 createActionBuilder("same", R.drawable.ic_corp_icon, firstPendingIntent)
90 .build();
91 Notification.Action secondAction =
92 createActionBuilder("same", R.drawable.ic_corp_icon, secondPendingIntent)
93 .build();
94
95 assertThat(NotificationUiAdjustment.needReinflate(
Tony Makc9acf672018-07-20 13:58:24 +020096 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
97 createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction))))
Tony Mak628cb932018-06-19 18:30:41 +010098 .isTrue();
99 }
100
101 @Test
102 public void needReinflate_differentChoices() {
103 PendingIntent pendingIntent =
104 PendingIntent.getActivity(mContext, 0, new Intent(), 0);
105
106 RemoteInput firstRemoteInput =
107 createRemoteInput("same", "same", new CharSequence[] {"first"});
108 RemoteInput secondRemoteInput =
109 createRemoteInput("same", "same", new CharSequence[] {"second"});
110
111 Notification.Action firstAction =
112 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
113 .addRemoteInput(firstRemoteInput)
114 .build();
115 Notification.Action secondAction =
116 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
117 .addRemoteInput(secondRemoteInput)
118 .build();
119
120 assertThat(NotificationUiAdjustment.needReinflate(
Tony Makc9acf672018-07-20 13:58:24 +0200121 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
122 createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction))))
Tony Mak628cb932018-06-19 18:30:41 +0100123 .isTrue();
124 }
125
126 @Test
127 public void needReinflate_differentRemoteInputLabel() {
128 PendingIntent pendingIntent =
129 PendingIntent.getActivity(mContext, 0, new Intent(), 0);
130
131 RemoteInput firstRemoteInput =
132 createRemoteInput("same", "first", new CharSequence[] {"same"});
133 RemoteInput secondRemoteInput =
134 createRemoteInput("same", "second", new CharSequence[] {"same"});
135
136 Notification.Action firstAction =
137 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
138 .addRemoteInput(firstRemoteInput)
139 .build();
140 Notification.Action secondAction =
141 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
142 .addRemoteInput(secondRemoteInput)
143 .build();
144
145 assertThat(NotificationUiAdjustment.needReinflate(
Tony Makc9acf672018-07-20 13:58:24 +0200146 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
147 createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction))))
Tony Mak628cb932018-06-19 18:30:41 +0100148 .isTrue();
149 }
150
151 @Test
152 public void needReinflate_negative() {
153 PendingIntent pendingIntent =
154 PendingIntent.getActivity(mContext, 0, new Intent(), 0);
155 RemoteInput firstRemoteInput =
156 createRemoteInput("same", "same", new CharSequence[] {"same"});
157 RemoteInput secondRemoteInput =
158 createRemoteInput("same", "same", new CharSequence[] {"same"});
159
160 Notification.Action firstAction =
161 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
162 .addRemoteInput(firstRemoteInput).build();
163 Notification.Action secondAction =
164 createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent)
165 .addRemoteInput(secondRemoteInput).build();
166
167 assertThat(NotificationUiAdjustment.needReinflate(
Tony Makc9acf672018-07-20 13:58:24 +0200168 createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)),
169 createUiAdjustmentFromSmartActions(
170 "second", Collections.singletonList(secondAction))))
171 .isFalse();
172 }
173
174 @Test
175 public void needReinflate_differentSmartReplies() {
176 assertThat(NotificationUiAdjustment.needReinflate(
177 createUiAdjustmentFromSmartReplies("first", new CharSequence[]{"a", "b"}),
178 createUiAdjustmentFromSmartReplies("first", new CharSequence[] {"b", "a"})))
179 .isTrue();
180 }
181
182 @Test
183 public void needReinflate_sameSmartReplies() {
184 assertThat(NotificationUiAdjustment.needReinflate(
185 createUiAdjustmentFromSmartReplies("first", new CharSequence[] {"a", "b"}),
186 createUiAdjustmentFromSmartReplies("first", new CharSequence[] {"a", "b"})))
Tony Mak628cb932018-06-19 18:30:41 +0100187 .isFalse();
188 }
189
190 private Notification.Action.Builder createActionBuilder(
191 String title, int drawableRes, PendingIntent pendingIntent) {
192 return new Notification.Action.Builder(
193 Icon.createWithResource(mContext, drawableRes), title, pendingIntent);
194 }
195
196 private RemoteInput createRemoteInput(String resultKey, String label, CharSequence[] choices) {
197 return new RemoteInput.Builder(resultKey).setLabel(label).setChoices(choices).build();
198 }
Tony Makc9acf672018-07-20 13:58:24 +0200199
200 private NotificationUiAdjustment createUiAdjustmentFromSmartActions(
201 String key, List<Notification.Action> actions) {
202 return new NotificationUiAdjustment(key, actions, new CharSequence[0]);
203 }
204
205 private NotificationUiAdjustment createUiAdjustmentFromSmartReplies(
206 String key, CharSequence[] replies) {
207 return new NotificationUiAdjustment(key, Collections.emptyList(), replies);
208 }
Tony Mak628cb932018-06-19 18:30:41 +0100209}