RemoteInputTest.java revision a2326755a16d744175acc58d1497fe2849977001
1/*
2 * Copyright (C) 2016 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
17package android.support.v4.app;
18
19import static org.junit.Assert.assertEquals;
20import static org.junit.Assert.assertFalse;
21import static org.junit.Assert.assertNotNull;
22import static org.junit.Assert.assertTrue;
23
24import android.content.Intent;
25import android.net.Uri;
26import android.os.Bundle;
27import android.support.test.filters.SdkSuppress;
28import android.support.test.filters.SmallTest;
29import android.support.test.runner.AndroidJUnit4;
30import android.support.v4.BaseInstrumentationTestCase;
31
32import org.junit.Test;
33import org.junit.runner.RunWith;
34
35import java.util.HashMap;
36import java.util.Map;
37
38@RunWith(AndroidJUnit4.class)
39@SmallTest
40public class RemoteInputTest extends BaseInstrumentationTestCase<TestSupportActivity> {
41    private static final String RESULT_KEY = "result_key";  // value doesn't matter
42    private static final String MIME_TYPE = "mimeType";  // value doesn't matter
43
44    public RemoteInputTest() {
45        super(TestSupportActivity.class);
46    }
47
48    @Test
49    public void testRemoteInputBuilder_setDataOnly() throws Throwable {
50        RemoteInput input = newDataOnlyRemoteInput();
51
52        assertTrue(input.isDataOnly());
53        assertFalse(input.getAllowFreeFormInput());
54        assertTrue(input.getChoices() == null || input.getChoices().length == 0);
55        assertEquals(1, input.getAllowedDataTypes().size());
56        assertTrue(input.getAllowedDataTypes().contains(MIME_TYPE));
57    }
58
59    @Test
60    public void testRemoteInputBuilder_setTextOnly() throws Throwable {
61        RemoteInput input = newTextRemoteInput();
62
63        assertFalse(input.isDataOnly());
64        assertTrue(input.getAllowFreeFormInput());
65        assertTrue(input.getChoices() == null || input.getChoices().length == 0);
66        assertTrue(input.getAllowedDataTypes() == null || input.getAllowedDataTypes().isEmpty());
67    }
68
69    @Test
70    public void testRemoteInputBuilder_setChoicesOnly() throws Throwable {
71        RemoteInput input = newChoicesOnlyRemoteInput();
72
73        assertFalse(input.isDataOnly());
74        assertFalse(input.getAllowFreeFormInput());
75        assertTrue(input.getChoices() != null && input.getChoices().length > 0);
76        assertTrue(input.getAllowedDataTypes() == null || input.getAllowedDataTypes().isEmpty());
77    }
78
79    @Test
80    public void testRemoteInputBuilder_setDataAndTextAndChoices() throws Throwable {
81        CharSequence[] choices = new CharSequence[2];
82        choices[0] = "first";
83        choices[1] = "second";
84        RemoteInput input =
85                new RemoteInput.Builder(RESULT_KEY)
86                .setChoices(choices)
87                .setAllowDataType(MIME_TYPE, true)
88                .build();
89
90        assertFalse(input.isDataOnly());
91        assertTrue(input.getAllowFreeFormInput());
92        assertTrue(input.getChoices() != null && input.getChoices().length > 0);
93        assertEquals(1, input.getAllowedDataTypes().size());
94        assertTrue(input.getAllowedDataTypes().contains(MIME_TYPE));
95    }
96
97    @Test
98    public void testRemoteInputBuilder_addAndGetDataResultsFromIntent() throws Throwable {
99        Uri uri = Uri.parse("Some Uri");
100        RemoteInput input = newDataOnlyRemoteInput();
101        Intent intent = new Intent();
102        Map<String, Uri> putResults = new HashMap<>();
103        putResults.put(MIME_TYPE, uri);
104        RemoteInput.addDataResultToIntent(input, intent, putResults);
105
106        verifyIntentHasDataResults(intent, uri);
107    }
108
109    @Test
110    public void testRemoteInputBuilder_addAndGetTextResultsFromIntent() throws Throwable {
111        CharSequence charSequence = "value doesn't matter";
112        RemoteInput input = newTextRemoteInput();
113        Intent intent = new Intent();
114        Bundle putResults = new Bundle();
115        putResults.putCharSequence(input.getResultKey(), charSequence);
116        RemoteInput[] arr = new RemoteInput[1];
117        arr[0] = input;
118        RemoteInput.addResultsToIntent(arr, intent, putResults);
119
120        verifyIntentHasTextResults(intent, charSequence);
121    }
122
123    @SdkSuppress(minSdkVersion = 17)
124    @Test
125    public void testRemoteInputBuilder_addAndGetDataAndTextResultsFromIntentDataFirst()
126            throws Throwable {
127        CharSequence charSequence = "value doesn't matter";
128        Uri uri = Uri.parse("Some Uri");
129        RemoteInput input =
130                new RemoteInput.Builder(RESULT_KEY)
131                .setAllowDataType(MIME_TYPE, true)
132                .build();
133        Intent intent = new Intent();
134
135        Map<String, Uri> dataResults = new HashMap<>();
136        dataResults.put(MIME_TYPE, uri);
137        RemoteInput.addDataResultToIntent(input, intent, dataResults);
138
139        Bundle textResults = new Bundle();
140        textResults.putCharSequence(input.getResultKey(), charSequence);
141        RemoteInput[] arr = new RemoteInput[1];
142        arr[0] = input;
143        RemoteInput.addResultsToIntent(arr, intent, textResults);
144
145        verifyIntentHasTextResults(intent, charSequence);
146        verifyIntentHasDataResults(intent, uri);
147    }
148
149    @SdkSuppress(minSdkVersion = 17)
150    @Test
151    public void testRemoteInputBuilder_addAndGetDataAndTextResultsFromIntentTextFirst()
152            throws Throwable {
153        CharSequence charSequence = "value doesn't matter";
154        Uri uri = Uri.parse("Some Uri");
155        RemoteInput input =
156                new RemoteInput.Builder(RESULT_KEY)
157                .setAllowDataType(MIME_TYPE, true)
158                .build();
159        Intent intent = new Intent();
160
161        Bundle textResults = new Bundle();
162        textResults.putCharSequence(input.getResultKey(), charSequence);
163        RemoteInput[] arr = new RemoteInput[1];
164        arr[0] = input;
165        RemoteInput.addResultsToIntent(arr, intent, textResults);
166
167        Map<String, Uri> dataResults = new HashMap<>();
168        dataResults.put(MIME_TYPE, uri);
169        RemoteInput.addDataResultToIntent(input, intent, dataResults);
170
171        verifyIntentHasTextResults(intent, charSequence);
172        verifyIntentHasDataResults(intent, uri);
173    }
174
175    private static void verifyIntentHasTextResults(Intent intent, CharSequence expected) {
176        Bundle getResults = RemoteInput.getResultsFromIntent(intent);
177        assertNotNull(getResults);
178        assertTrue(getResults.containsKey(RESULT_KEY));
179        assertEquals(expected, getResults.getCharSequence(RESULT_KEY, "default"));
180    }
181
182    private static void verifyIntentHasDataResults(Intent intent, Uri expectedUri) {
183        Map<String, Uri> getResults = RemoteInput.getDataResultsFromIntent(intent, RESULT_KEY);
184        assertNotNull(getResults);
185        assertEquals(1, getResults.size());
186        assertTrue(getResults.containsKey(MIME_TYPE));
187        assertEquals(expectedUri, getResults.get(MIME_TYPE));
188    }
189
190    private static RemoteInput newTextRemoteInput() {
191        return new RemoteInput.Builder(RESULT_KEY).build();  // allowFreeForm defaults to true
192    }
193
194    private static RemoteInput newChoicesOnlyRemoteInput() {
195        CharSequence[] choices = new CharSequence[2];
196        choices[0] = "first";
197        choices[1] = "second";
198        return new RemoteInput.Builder(RESULT_KEY)
199            .setAllowFreeFormInput(false)
200            .setChoices(choices)
201            .build();
202    }
203
204    private static RemoteInput newDataOnlyRemoteInput() {
205        return new RemoteInput.Builder(RESULT_KEY)
206            .setAllowFreeFormInput(false)
207            .setAllowDataType(MIME_TYPE, true)
208            .build();
209    }
210}
211