Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 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 | import android.support.test.filters.SmallTest; |
| 26 | |
| 27 | import com.android.internal.R; |
| 28 | import com.android.systemui.SysuiTestCase; |
| 29 | |
| 30 | import org.junit.Test; |
| 31 | |
| 32 | import java.util.Collections; |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 33 | import java.util.List; |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 34 | |
| 35 | @SmallTest |
| 36 | public class NotificationUiAdjustmentTest extends SysuiTestCase { |
| 37 | |
| 38 | @Test |
| 39 | public void needReinflate_differentLength() { |
| 40 | PendingIntent pendingIntent = |
| 41 | PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 42 | Notification.Action action = |
| 43 | createActionBuilder("first", R.drawable.ic_corp_icon, pendingIntent).build(); |
| 44 | assertThat(NotificationUiAdjustment.needReinflate( |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 45 | createUiAdjustmentFromSmartActions("first", Collections.emptyList()), |
| 46 | createUiAdjustmentFromSmartActions("second", Collections.singletonList(action)))) |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 47 | .isTrue(); |
| 48 | } |
| 49 | |
| 50 | @Test |
| 51 | public void needReinflate_differentLabels() { |
| 52 | PendingIntent pendingIntent = |
| 53 | PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 54 | Notification.Action firstAction = |
| 55 | createActionBuilder("first", R.drawable.ic_corp_icon, pendingIntent).build(); |
| 56 | Notification.Action secondAction = |
| 57 | createActionBuilder("second", R.drawable.ic_corp_icon, pendingIntent).build(); |
| 58 | |
| 59 | assertThat(NotificationUiAdjustment.needReinflate( |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 60 | createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)), |
| 61 | createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction)))) |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 62 | .isTrue(); |
| 63 | } |
| 64 | |
| 65 | @Test |
| 66 | public void needReinflate_differentIcons() { |
| 67 | PendingIntent pendingIntent = |
| 68 | PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 69 | Notification.Action firstAction = |
| 70 | createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent).build(); |
| 71 | Notification.Action secondAction = |
| 72 | createActionBuilder("same", R.drawable.ic_account_circle, pendingIntent) |
| 73 | .build(); |
| 74 | |
| 75 | assertThat(NotificationUiAdjustment.needReinflate( |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 76 | createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)), |
| 77 | createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction)))) |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 78 | .isTrue(); |
| 79 | } |
| 80 | |
| 81 | @Test |
| 82 | public void needReinflate_differentPendingIntent() { |
| 83 | PendingIntent firstPendingIntent = |
| 84 | PendingIntent.getActivity(mContext, 0, new Intent(Intent.ACTION_VIEW), 0); |
| 85 | PendingIntent secondPendingIntent = |
| 86 | PendingIntent.getActivity(mContext, 0, new Intent(Intent.ACTION_PROCESS_TEXT), 0); |
| 87 | Notification.Action firstAction = |
| 88 | createActionBuilder("same", R.drawable.ic_corp_icon, firstPendingIntent) |
| 89 | .build(); |
| 90 | Notification.Action secondAction = |
| 91 | createActionBuilder("same", R.drawable.ic_corp_icon, secondPendingIntent) |
| 92 | .build(); |
| 93 | |
| 94 | assertThat(NotificationUiAdjustment.needReinflate( |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 95 | createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)), |
| 96 | createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction)))) |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 97 | .isTrue(); |
| 98 | } |
| 99 | |
| 100 | @Test |
| 101 | public void needReinflate_differentChoices() { |
| 102 | PendingIntent pendingIntent = |
| 103 | PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 104 | |
| 105 | RemoteInput firstRemoteInput = |
| 106 | createRemoteInput("same", "same", new CharSequence[] {"first"}); |
| 107 | RemoteInput secondRemoteInput = |
| 108 | createRemoteInput("same", "same", new CharSequence[] {"second"}); |
| 109 | |
| 110 | Notification.Action firstAction = |
| 111 | createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent) |
| 112 | .addRemoteInput(firstRemoteInput) |
| 113 | .build(); |
| 114 | Notification.Action secondAction = |
| 115 | createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent) |
| 116 | .addRemoteInput(secondRemoteInput) |
| 117 | .build(); |
| 118 | |
| 119 | assertThat(NotificationUiAdjustment.needReinflate( |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 120 | createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)), |
| 121 | createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction)))) |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 122 | .isTrue(); |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | public void needReinflate_differentRemoteInputLabel() { |
| 127 | PendingIntent pendingIntent = |
| 128 | PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 129 | |
| 130 | RemoteInput firstRemoteInput = |
| 131 | createRemoteInput("same", "first", new CharSequence[] {"same"}); |
| 132 | RemoteInput secondRemoteInput = |
| 133 | createRemoteInput("same", "second", new CharSequence[] {"same"}); |
| 134 | |
| 135 | Notification.Action firstAction = |
| 136 | createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent) |
| 137 | .addRemoteInput(firstRemoteInput) |
| 138 | .build(); |
| 139 | Notification.Action secondAction = |
| 140 | createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent) |
| 141 | .addRemoteInput(secondRemoteInput) |
| 142 | .build(); |
| 143 | |
| 144 | assertThat(NotificationUiAdjustment.needReinflate( |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 145 | createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)), |
| 146 | createUiAdjustmentFromSmartActions("second", Collections.singletonList(secondAction)))) |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 147 | .isTrue(); |
| 148 | } |
| 149 | |
| 150 | @Test |
| 151 | public void needReinflate_negative() { |
| 152 | PendingIntent pendingIntent = |
| 153 | PendingIntent.getActivity(mContext, 0, new Intent(), 0); |
| 154 | RemoteInput firstRemoteInput = |
| 155 | createRemoteInput("same", "same", new CharSequence[] {"same"}); |
| 156 | RemoteInput secondRemoteInput = |
| 157 | createRemoteInput("same", "same", new CharSequence[] {"same"}); |
| 158 | |
| 159 | Notification.Action firstAction = |
| 160 | createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent) |
| 161 | .addRemoteInput(firstRemoteInput).build(); |
| 162 | Notification.Action secondAction = |
| 163 | createActionBuilder("same", R.drawable.ic_corp_icon, pendingIntent) |
| 164 | .addRemoteInput(secondRemoteInput).build(); |
| 165 | |
| 166 | assertThat(NotificationUiAdjustment.needReinflate( |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 167 | createUiAdjustmentFromSmartActions("first", Collections.singletonList(firstAction)), |
| 168 | createUiAdjustmentFromSmartActions( |
| 169 | "second", Collections.singletonList(secondAction)))) |
| 170 | .isFalse(); |
| 171 | } |
| 172 | |
| 173 | @Test |
| 174 | public void needReinflate_differentSmartReplies() { |
| 175 | assertThat(NotificationUiAdjustment.needReinflate( |
| 176 | createUiAdjustmentFromSmartReplies("first", new CharSequence[]{"a", "b"}), |
| 177 | createUiAdjustmentFromSmartReplies("first", new CharSequence[] {"b", "a"}))) |
| 178 | .isTrue(); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void needReinflate_sameSmartReplies() { |
| 183 | assertThat(NotificationUiAdjustment.needReinflate( |
| 184 | createUiAdjustmentFromSmartReplies("first", new CharSequence[] {"a", "b"}), |
| 185 | createUiAdjustmentFromSmartReplies("first", new CharSequence[] {"a", "b"}))) |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 186 | .isFalse(); |
| 187 | } |
| 188 | |
| 189 | private Notification.Action.Builder createActionBuilder( |
| 190 | String title, int drawableRes, PendingIntent pendingIntent) { |
| 191 | return new Notification.Action.Builder( |
| 192 | Icon.createWithResource(mContext, drawableRes), title, pendingIntent); |
| 193 | } |
| 194 | |
| 195 | private RemoteInput createRemoteInput(String resultKey, String label, CharSequence[] choices) { |
| 196 | return new RemoteInput.Builder(resultKey).setLabel(label).setChoices(choices).build(); |
| 197 | } |
Tony Mak | c9acf67 | 2018-07-20 13:58:24 +0200 | [diff] [blame] | 198 | |
| 199 | private NotificationUiAdjustment createUiAdjustmentFromSmartActions( |
| 200 | String key, List<Notification.Action> actions) { |
| 201 | return new NotificationUiAdjustment(key, actions, new CharSequence[0]); |
| 202 | } |
| 203 | |
| 204 | private NotificationUiAdjustment createUiAdjustmentFromSmartReplies( |
| 205 | String key, CharSequence[] replies) { |
| 206 | return new NotificationUiAdjustment(key, Collections.emptyList(), replies); |
| 207 | } |
Tony Mak | 628cb93 | 2018-06-19 18:30:41 +0100 | [diff] [blame] | 208 | } |