1 /* 2 * Copyright (C) 2014 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 android.support.v4.app; 18 19 import android.app.Notification; 20 import android.app.PendingIntent; 21 import android.content.Intent; 22 import android.os.Bundle; 23 import android.support.tests.R; 24 import android.test.AndroidTestCase; 25 import android.view.Gravity; 26 27 import java.util.Arrays; 28 import java.util.List; 29 30 /** 31 * Tests for {@link android.support.v4.app.NotificationCompat.WearableExtender}. 32 */ 33 public class NotificationCompatWearableExtenderTest extends AndroidTestCase { 34 public static final int CUSTOM_CONTENT_HEIGHT_DP = 256; 35 36 private PendingIntent mPendingIntent; 37 private int mCustomContentHeightPx; 38 39 @Override 40 protected void setUp() throws Exception { 41 super.setUp(); 42 43 mPendingIntent = PendingIntent.getActivity(getContext(), 0, new Intent(), 0); 44 45 mCustomContentHeightPx = Math.round(getContext().getResources().getDisplayMetrics().density 46 * CUSTOM_CONTENT_HEIGHT_DP); 47 } 48 49 public void testEmptyEquals() throws Exception { 50 assertExtendersEqual(new Notification.WearableExtender(), 51 new NotificationCompat.WearableExtender()); 52 } 53 54 public void testRealReadCompatEmptyValue() throws Exception { 55 NotificationCompat.WearableExtender compatExtender = 56 new NotificationCompat.WearableExtender(); 57 Notification notif = new NotificationCompat.Builder(getContext()) 58 .extend(compatExtender) 59 .build(); 60 61 assertExtendersEqual(new Notification.WearableExtender(notif), compatExtender); 62 assertExtendersEqual(new Notification.WearableExtender(notif), 63 new NotificationCompat.WearableExtender(notif)); 64 } 65 66 public void testCompatReadRealEmptyValue() throws Exception { 67 Notification.WearableExtender realExtender = 68 new Notification.WearableExtender(); 69 Notification notif = new Notification.Builder(getContext()) 70 .extend(realExtender) 71 .build(); 72 73 assertExtendersEqual(realExtender, new NotificationCompat.WearableExtender(notif)); 74 assertExtendersEqual(new Notification.WearableExtender(notif), 75 new NotificationCompat.WearableExtender(notif)); 76 } 77 78 public void testRealReadCompatValue() throws Exception { 79 RemoteInput.Builder remoteInput = new RemoteInput.Builder("result_key1") 80 .setLabel("label") 81 .setChoices(new CharSequence[] {"choice 1", "choice 2"}); 82 remoteInput.getExtras().putString("remoteinput_string", "test"); 83 NotificationCompat.Action.Builder action2 = new NotificationCompat.Action.Builder( 84 R.drawable.action_icon, "Test title", mPendingIntent) 85 .addRemoteInput(remoteInput.build()) 86 .extend(new NotificationCompat.Action.WearableExtender() 87 .setAvailableOffline(false) 88 .setInProgressLabel("In Progress Label") 89 .setConfirmLabel("Confirmation Label") 90 .setCancelLabel("Cancelation Label")); 91 // Add an arbitrary key/value. 92 action2.getExtras().putFloat("action_float", 10.5f); 93 94 Notification page2 = new Notification.Builder(getContext()) 95 .setContentTitle("page2 title") 96 .extend(new Notification.WearableExtender() 97 .setContentIcon(R.drawable.content_icon)) 98 .build(); 99 100 NotificationCompat.WearableExtender compatExtender = 101 new NotificationCompat.WearableExtender() 102 .addAction(new NotificationCompat.Action(R.drawable.action_icon2, "Action1", 103 mPendingIntent)) 104 .addAction(action2.build()) 105 .setContentIntentAvailableOffline(false) 106 .setHintHideIcon(true) 107 .setHintShowBackgroundOnly(true) 108 .setStartScrollBottom(true) 109 .setDisplayIntent(mPendingIntent) 110 .addPage(page2) 111 .setContentIcon(R.drawable.content_icon2) 112 .setContentIconGravity(Gravity.START) 113 .setContentAction(5 /* arbitrary content action index */) 114 .setCustomSizePreset(NotificationCompat.WearableExtender.SIZE_MEDIUM) 115 .setCustomContentHeight(mCustomContentHeightPx) 116 .setGravity(Gravity.TOP); 117 118 Notification notif = new NotificationCompat.Builder(getContext()) 119 .extend(compatExtender).build(); 120 assertExtendersEqual(new Notification.WearableExtender(notif), compatExtender); 121 assertExtendersEqual(new Notification.WearableExtender(notif), 122 new NotificationCompat.WearableExtender(notif)); 123 } 124 125 public void testCompatReadRealValue() throws Exception { 126 android.app.RemoteInput.Builder remoteInput = new android.app.RemoteInput.Builder( 127 "result_key1") 128 .setLabel("label") 129 .setChoices(new CharSequence[] {"choice 1", "choice 2"}); 130 remoteInput.getExtras().putString("remoteinput_string", "test"); 131 Notification.Action.Builder action2 = new Notification.Action.Builder( 132 R.drawable.action_icon, "Test title", mPendingIntent) 133 .addRemoteInput(remoteInput.build()) 134 .extend(new Notification.Action.WearableExtender() 135 .setAvailableOffline(false) 136 .setInProgressLabel("In Progress Label") 137 .setConfirmLabel("Confirmation Label") 138 .setCancelLabel("Cancelation Label")); 139 // Add an arbitrary key/value. 140 action2.getExtras().putFloat("action_float", 10.5f); 141 142 Notification page2 = new Notification.Builder(getContext()) 143 .setContentTitle("page2 title") 144 .extend(new Notification.WearableExtender() 145 .setContentIcon(R.drawable.content_icon)) 146 .build(); 147 148 Notification.WearableExtender realExtender = 149 new Notification.WearableExtender() 150 .addAction(new Notification.Action(R.drawable.action_icon2, "Action1", 151 mPendingIntent)) 152 .addAction(action2.build()) 153 .setContentIntentAvailableOffline(false) 154 .setHintHideIcon(true) 155 .setHintShowBackgroundOnly(true) 156 .setStartScrollBottom(true) 157 .setDisplayIntent(mPendingIntent) 158 .addPage(page2) 159 .setContentIcon(R.drawable.content_icon2) 160 .setContentIconGravity(Gravity.START) 161 .setContentAction(5 /* arbitrary content action index */) 162 .setCustomSizePreset(NotificationCompat.WearableExtender.SIZE_MEDIUM) 163 .setCustomContentHeight(mCustomContentHeightPx) 164 .setGravity(Gravity.TOP); 165 166 Notification notif = new Notification.Builder(getContext()) 167 .extend(realExtender).build(); 168 assertExtendersEqual(realExtender, new NotificationCompat.WearableExtender(notif)); 169 assertExtendersEqual(new Notification.WearableExtender(notif), 170 new NotificationCompat.WearableExtender(notif)); 171 } 172 173 private void assertExtendersEqual(Notification.WearableExtender real, 174 NotificationCompat.WearableExtender compat) { 175 assertActionsEquals(real.getActions(), compat.getActions()); 176 assertEquals(real.getContentIntentAvailableOffline(), 177 compat.getContentIntentAvailableOffline()); 178 assertEquals(real.getHintHideIcon(), compat.getHintHideIcon()); 179 assertEquals(real.getHintShowBackgroundOnly(), compat.getHintShowBackgroundOnly()); 180 assertEquals(real.getStartScrollBottom(), compat.getStartScrollBottom()); 181 assertEquals(real.getDisplayIntent(), compat.getDisplayIntent()); 182 assertPagesEquals(real.getPages(), compat.getPages()); 183 assertEquals(real.getBackground(), compat.getBackground()); 184 assertEquals(real.getContentIcon(), compat.getContentIcon()); 185 assertEquals(real.getContentIconGravity(), compat.getContentIconGravity()); 186 assertEquals(real.getContentAction(), compat.getContentAction()); 187 assertEquals(real.getCustomSizePreset(), compat.getCustomSizePreset()); 188 assertEquals(real.getCustomContentHeight(), compat.getCustomContentHeight()); 189 assertEquals(real.getGravity(), compat.getGravity()); 190 } 191 192 private void assertPagesEquals(List<Notification> pages1, List<Notification> pages2) { 193 assertEquals(pages1.size(), pages2.size()); 194 for (int i = 0; i < pages1.size(); i++) { 195 assertNotificationsEqual(pages1.get(i), pages2.get(i)); 196 } 197 } 198 199 private void assertNotificationsEqual(Notification n1, Notification n2) { 200 assertEquals(n1.icon, n2.icon); 201 assertBundlesEqual(n1.extras, n2.extras); 202 assertExtendersEqual(new Notification.WearableExtender(n1), 203 new NotificationCompat.WearableExtender(n2)); 204 } 205 206 private void assertActionsEquals(List<Notification.Action> realArray, 207 List<NotificationCompat.Action> compatArray) { 208 assertEquals(realArray.size(), compatArray.size()); 209 for (int i = 0; i < realArray.size(); i++) { 210 assertActionsEqual(realArray.get(i), compatArray.get(i)); 211 } 212 } 213 214 private void assertActionsEqual(Notification.Action real, NotificationCompat.Action compat) { 215 assertEquals(real.icon, compat.icon); 216 assertEquals(real.title, compat.title); 217 assertEquals(real.actionIntent, compat.actionIntent); 218 assertRemoteInputsEquals(real.getRemoteInputs(), compat.getRemoteInputs()); 219 assertBundlesEqual(real.getExtras(), compat.getExtras()); 220 } 221 222 private void assertRemoteInputsEquals(android.app.RemoteInput[] realArray, 223 RemoteInput[] compatArray) { 224 assertEquals(realArray == null, compatArray == null); 225 if (realArray != null) { 226 assertEquals(realArray.length, compatArray.length); 227 for (int i = 0; i < realArray.length; i++) { 228 assertRemoteInputsEqual(realArray[i], compatArray[i]); 229 } 230 } 231 } 232 233 private void assertRemoteInputsEqual(android.app.RemoteInput real, 234 RemoteInput compat) { 235 assertEquals(real.getResultKey(), compat.getResultKey()); 236 assertEquals(real.getLabel(), compat.getLabel()); 237 assertCharSequencesEquals(real.getChoices(), compat.getChoices()); 238 assertEquals(real.getAllowFreeFormInput(), compat.getAllowFreeFormInput()); 239 assertBundlesEqual(real.getExtras(), compat.getExtras()); 240 } 241 242 private void assertCharSequencesEquals(CharSequence[] array1, CharSequence[] array2) { 243 if (!Arrays.deepEquals(array1, array2)) { 244 fail("Arrays not equal: " + Arrays.toString(array1) + " != " + Arrays.toString(array2)); 245 } 246 } 247 248 private void assertBundlesEqual(Bundle bundle1, Bundle bundle2) { 249 assertEquals(bundle1.size(), bundle2.size()); 250 for (String key : bundle1.keySet()) { 251 Object value1 = bundle1.get(key); 252 Object value2 = bundle2.get(key); 253 if (value1 instanceof Bundle && value2 instanceof Bundle) { 254 assertBundlesEqual((Bundle) value1, (Bundle) value2); 255 } else { 256 assertEquals(value1, value2); 257 } 258 } 259 } 260 } 261