1 /* 2 * Copyright (C) 2017 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.app.cts; 18 19 import android.app.RemoteInput; 20 import android.content.Intent; 21 import android.net.Uri; 22 import android.os.Bundle; 23 import android.test.AndroidTestCase; 24 25 import java.util.HashMap; 26 import java.util.Map; 27 28 public class RemoteInputTest extends AndroidTestCase { 29 private static final String RESULT_KEY = "result_key"; // value doesn't matter 30 private static final String MIME_TYPE = "mimeType"; // value doesn't matter 31 32 public void testRemoteInputBuilder_setDataOnly() throws Throwable { 33 RemoteInput input = newDataOnlyRemoteInput(); 34 35 assertTrue(input.isDataOnly()); 36 assertFalse(input.getAllowFreeFormInput()); 37 assertTrue(input.getChoices() == null || input.getChoices().length == 0); 38 assertEquals(RemoteInput.EDIT_CHOICES_BEFORE_SENDING_AUTO, 39 input.getEditChoicesBeforeSending()); 40 assertEquals(1, input.getAllowedDataTypes().size()); 41 assertTrue(input.getAllowedDataTypes().contains(MIME_TYPE)); 42 } 43 44 public void testRemoteInputBuilder_setTextOnly() throws Throwable { 45 RemoteInput input = newTextRemoteInput(); 46 47 assertFalse(input.isDataOnly()); 48 assertTrue(input.getAllowFreeFormInput()); 49 assertTrue(input.getChoices() == null || input.getChoices().length == 0); 50 assertEquals(RemoteInput.EDIT_CHOICES_BEFORE_SENDING_AUTO, 51 input.getEditChoicesBeforeSending()); 52 assertTrue(input.getAllowedDataTypes() == null || input.getAllowedDataTypes().isEmpty()); 53 } 54 55 public void testRemoteInputBuilder_setChoicesOnly() throws Throwable { 56 RemoteInput input = newChoicesOnlyRemoteInput(); 57 58 assertFalse(input.isDataOnly()); 59 assertFalse(input.getAllowFreeFormInput()); 60 assertTrue(input.getChoices() != null && input.getChoices().length > 0); 61 assertEquals(RemoteInput.EDIT_CHOICES_BEFORE_SENDING_AUTO, 62 input.getEditChoicesBeforeSending()); 63 assertTrue(input.getAllowedDataTypes() == null || input.getAllowedDataTypes().isEmpty()); 64 } 65 66 public void testRemoteInputBuilder_setDataAndTextAndChoices() throws Throwable { 67 CharSequence[] choices = new CharSequence[2]; 68 choices[0] = "first"; 69 choices[1] = "second"; 70 RemoteInput input = 71 new RemoteInput.Builder(RESULT_KEY) 72 .setChoices(choices) 73 .setAllowDataType(MIME_TYPE, true) 74 .build(); 75 76 assertFalse(input.isDataOnly()); 77 assertTrue(input.getAllowFreeFormInput()); 78 assertTrue(input.getChoices() != null && input.getChoices().length > 0); 79 assertEquals(RemoteInput.EDIT_CHOICES_BEFORE_SENDING_AUTO, 80 input.getEditChoicesBeforeSending()); 81 assertEquals(1, input.getAllowedDataTypes().size()); 82 assertTrue(input.getAllowedDataTypes().contains(MIME_TYPE)); 83 } 84 85 public void testRemoteInputBuilder_setEditChoicesBeforeSending() throws Throwable { 86 RemoteInput input = 87 new RemoteInput.Builder(RESULT_KEY) 88 .setChoices(new CharSequence[]{"first", "second"}) 89 .setEditChoicesBeforeSending( 90 RemoteInput.EDIT_CHOICES_BEFORE_SENDING_ENABLED) 91 .build(); 92 assertEquals(RemoteInput.EDIT_CHOICES_BEFORE_SENDING_ENABLED, 93 input.getEditChoicesBeforeSending()); 94 } 95 96 public void testRemoteInputBuilder_setEditChoicesBeforeSendingRequiresFreeInput() 97 throws Throwable { 98 RemoteInput.Builder builder = 99 new RemoteInput.Builder(RESULT_KEY) 100 .setEditChoicesBeforeSending( 101 RemoteInput.EDIT_CHOICES_BEFORE_SENDING_ENABLED) 102 .setAllowFreeFormInput(false); 103 try { 104 builder.build(); 105 fail(); 106 } catch (IllegalArgumentException e) { 107 // expected. 108 } 109 } 110 111 public void testRemoteInputBuilder_addAndGetDataResultsFromIntent() throws Throwable { 112 Uri uri = Uri.parse("Some Uri"); 113 RemoteInput input = newDataOnlyRemoteInput(); 114 Intent intent = new Intent(); 115 Map<String, Uri> putResults = new HashMap<>(); 116 putResults.put(MIME_TYPE, uri); 117 RemoteInput.addDataResultToIntent(input, intent, putResults); 118 119 verifyIntentHasDataResults(intent, uri); 120 } 121 122 public void testRemoteInputBuilder_addAndGetTextResultsFromIntent() throws Throwable { 123 CharSequence charSequence = "value doesn't matter"; 124 RemoteInput input = newTextRemoteInput(); 125 Intent intent = new Intent(); 126 Bundle putResults = new Bundle(); 127 putResults.putCharSequence(input.getResultKey(), charSequence); 128 RemoteInput[] arr = new RemoteInput[1]; 129 arr[0] = input; 130 RemoteInput.addResultsToIntent(arr, intent, putResults); 131 132 verifyIntentHasTextResults(intent, charSequence); 133 } 134 135 public void testRemoteInputBuilder_addAndGetDataAndTextResultsFromIntentDataFirst() 136 throws Throwable { 137 CharSequence charSequence = "value doesn't matter"; 138 Uri uri = Uri.parse("Some Uri"); 139 RemoteInput input = newTextAndDataRemoteInput(); 140 Intent intent = new Intent(); 141 142 addDataResultsToIntent(input, intent, uri); 143 addTextResultsToIntent(input, intent, charSequence); 144 RemoteInput.setResultsSource(intent, RemoteInput.SOURCE_FREE_FORM_INPUT); 145 146 verifyIntentHasTextResults(intent, charSequence); 147 verifyIntentHasDataResults(intent, uri); 148 } 149 150 public void testRemoteInputBuilder_addAndGetDataAndTextResultsFromIntentTextFirst() 151 throws Throwable { 152 CharSequence charSequence = "value doesn't matter"; 153 Uri uri = Uri.parse("Some Uri"); 154 RemoteInput input = newTextAndDataRemoteInput(); 155 Intent intent = new Intent(); 156 157 addTextResultsToIntent(input, intent, charSequence); 158 addDataResultsToIntent(input, intent, uri); 159 RemoteInput.setResultsSource(intent, RemoteInput.SOURCE_CHOICE); 160 161 verifyIntentHasTextResults(intent, charSequence); 162 verifyIntentHasDataResults(intent, uri); 163 } 164 165 public void testGetResultsSource_emptyIntent() { 166 Intent intent = new Intent(); 167 168 assertEquals(RemoteInput.SOURCE_FREE_FORM_INPUT, RemoteInput.getResultsSource(intent)); 169 } 170 171 public void testGetResultsSource_addDataAndTextResults() { 172 CharSequence charSequence = "value doesn't matter"; 173 Uri uri = Uri.parse("Some Uri"); 174 RemoteInput input = newTextAndDataRemoteInput(); 175 Intent intent = new Intent(); 176 177 addTextResultsToIntent(input, intent, charSequence); 178 addDataResultsToIntent(input, intent, uri); 179 180 assertEquals(RemoteInput.SOURCE_FREE_FORM_INPUT, RemoteInput.getResultsSource(intent)); 181 } 182 183 public void testGetResultsSource_setSource() { 184 Intent intent = new Intent(); 185 186 RemoteInput.setResultsSource(intent, RemoteInput.SOURCE_CHOICE); 187 188 assertEquals(RemoteInput.SOURCE_CHOICE, RemoteInput.getResultsSource(intent)); 189 } 190 191 public void testGetResultsSource_setSourceAndAddDataAndTextResults() { 192 CharSequence charSequence = "value doesn't matter"; 193 Uri uri = Uri.parse("Some Uri"); 194 RemoteInput input = newTextAndDataRemoteInput(); 195 Intent intent = new Intent(); 196 197 RemoteInput.setResultsSource(intent, RemoteInput.SOURCE_CHOICE); 198 addTextResultsToIntent(input, intent, charSequence); 199 addDataResultsToIntent(input, intent, uri); 200 201 assertEquals(RemoteInput.SOURCE_CHOICE, RemoteInput.getResultsSource(intent)); 202 } 203 204 private static void addTextResultsToIntent(RemoteInput input, Intent intent, 205 CharSequence charSequence) { 206 Bundle textResults = new Bundle(); 207 textResults.putCharSequence(input.getResultKey(), charSequence); 208 RemoteInput[] arr = new RemoteInput[1]; 209 arr[0] = input; 210 RemoteInput.addResultsToIntent(arr, intent, textResults); 211 } 212 213 private static void addDataResultsToIntent(RemoteInput input, Intent intent, Uri uri) { 214 Map<String, Uri> dataResults = new HashMap<>(); 215 dataResults.put(MIME_TYPE, uri); 216 RemoteInput.addDataResultToIntent(input, intent, dataResults); 217 } 218 219 private static void verifyIntentHasTextResults(Intent intent, CharSequence expected) { 220 Bundle getResults = RemoteInput.getResultsFromIntent(intent); 221 assertNotNull(getResults); 222 assertTrue(getResults.containsKey(RESULT_KEY)); 223 assertEquals(expected, getResults.getCharSequence(RESULT_KEY, "default")); 224 } 225 226 private static void verifyIntentHasDataResults(Intent intent, Uri expectedUri) { 227 Map<String, Uri> getResults = RemoteInput.getDataResultsFromIntent(intent, RESULT_KEY); 228 assertNotNull(getResults); 229 assertEquals(1, getResults.size()); 230 assertTrue(getResults.containsKey(MIME_TYPE)); 231 assertEquals(expectedUri, getResults.get(MIME_TYPE)); 232 } 233 234 private static RemoteInput newTextRemoteInput() { 235 return new RemoteInput.Builder(RESULT_KEY).build(); // allowFreeForm defaults to true 236 } 237 238 private static RemoteInput newChoicesOnlyRemoteInput() { 239 CharSequence[] choices = new CharSequence[2]; 240 choices[0] = "first"; 241 choices[1] = "second"; 242 return new RemoteInput.Builder(RESULT_KEY) 243 .setAllowFreeFormInput(false) 244 .setChoices(choices) 245 .build(); 246 } 247 248 private static RemoteInput newDataOnlyRemoteInput() { 249 return new RemoteInput.Builder(RESULT_KEY) 250 .setAllowFreeFormInput(false) 251 .setAllowDataType(MIME_TYPE, true) 252 .build(); 253 } 254 255 private static RemoteInput newTextAndDataRemoteInput() { 256 return new RemoteInput.Builder(RESULT_KEY) 257 .setAllowFreeFormInput(true) 258 .setAllowDataType(MIME_TYPE, true) 259 .build(); 260 } 261 } 262 263