Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 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 androidx.app.slice; |
| 18 | |
| 19 | import static android.app.slice.Slice.HINT_LARGE; |
| 20 | import static android.app.slice.Slice.HINT_LIST; |
| 21 | import static android.app.slice.Slice.HINT_NO_TINT; |
| 22 | import static android.app.slice.Slice.HINT_TITLE; |
| 23 | import static android.app.slice.SliceItem.FORMAT_ACTION; |
| 24 | import static android.app.slice.SliceItem.FORMAT_IMAGE; |
Jason Monk | 98ae4f8 | 2017-12-18 11:29:07 -0500 | [diff] [blame] | 25 | import static android.app.slice.SliceItem.FORMAT_INT; |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 26 | import static android.app.slice.SliceItem.FORMAT_SLICE; |
| 27 | import static android.app.slice.SliceItem.FORMAT_TEXT; |
| 28 | import static android.app.slice.SliceItem.FORMAT_TIMESTAMP; |
| 29 | import static android.app.slice.SliceProvider.SLICE_TYPE; |
| 30 | |
| 31 | import static org.junit.Assert.assertEquals; |
| 32 | import static org.junit.Assert.assertFalse; |
| 33 | import static org.junit.Assert.assertNotEquals; |
| 34 | import static org.junit.Assert.assertTrue; |
| 35 | |
| 36 | import android.app.PendingIntent.CanceledException; |
| 37 | import android.content.BroadcastReceiver; |
| 38 | import android.content.Context; |
| 39 | import android.content.Intent; |
| 40 | import android.content.IntentFilter; |
| 41 | import android.graphics.drawable.Icon; |
| 42 | import android.net.Uri; |
| 43 | import android.support.test.InstrumentationRegistry; |
| 44 | import android.support.test.filters.SmallTest; |
| 45 | import android.support.test.runner.AndroidJUnit4; |
| 46 | |
| 47 | import org.junit.Test; |
| 48 | import org.junit.runner.RunWith; |
| 49 | |
| 50 | import java.util.Arrays; |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 51 | import java.util.Collections; |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 52 | import java.util.concurrent.CountDownLatch; |
| 53 | import java.util.concurrent.TimeUnit; |
| 54 | |
| 55 | import androidx.app.slice.core.test.R; |
| 56 | |
| 57 | @RunWith(AndroidJUnit4.class) |
| 58 | @SmallTest |
| 59 | public class SliceTest { |
| 60 | |
| 61 | public static boolean sFlag = false; |
| 62 | |
| 63 | private static final Uri BASE_URI = Uri.parse("content://androidx.app.slice.core.test/"); |
| 64 | private final Context mContext = InstrumentationRegistry.getContext(); |
| 65 | |
| 66 | @Test |
| 67 | public void testProcess() { |
| 68 | sFlag = false; |
| 69 | Slice.bindSlice(mContext, |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 70 | BASE_URI.buildUpon().appendPath("set_flag").build(), |
| 71 | Collections.<SliceSpec>emptyList()); |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 72 | assertFalse(sFlag); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void testType() { |
| 77 | assertEquals(SLICE_TYPE, mContext.getContentResolver().getType(BASE_URI)); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testSliceUri() { |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 82 | Slice s = Slice.bindSlice(mContext, BASE_URI, Collections.<SliceSpec>emptyList()); |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 83 | assertEquals(BASE_URI, s.getUri()); |
| 84 | } |
| 85 | |
| 86 | @Test |
| 87 | public void testSubSlice() { |
| 88 | Uri uri = BASE_URI.buildUpon().appendPath("subslice").build(); |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 89 | Slice s = Slice.bindSlice(mContext, uri, Collections.<SliceSpec>emptyList()); |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 90 | assertEquals(uri, s.getUri()); |
| 91 | assertEquals(1, s.getItems().size()); |
| 92 | |
| 93 | SliceItem item = s.getItems().get(0); |
| 94 | assertEquals(FORMAT_SLICE, item.getFormat()); |
| 95 | assertEquals("subslice", item.getSubType()); |
| 96 | // The item should start with the same Uri as the parent, but be different. |
| 97 | assertTrue(item.getSlice().getUri().toString().startsWith(uri.toString())); |
| 98 | assertNotEquals(uri, item.getSlice().getUri()); |
| 99 | } |
| 100 | |
| 101 | @Test |
| 102 | public void testText() { |
| 103 | Uri uri = BASE_URI.buildUpon().appendPath("text").build(); |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 104 | Slice s = Slice.bindSlice(mContext, uri, Collections.<SliceSpec>emptyList()); |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 105 | assertEquals(uri, s.getUri()); |
| 106 | assertEquals(1, s.getItems().size()); |
| 107 | |
| 108 | SliceItem item = s.getItems().get(0); |
| 109 | assertEquals(FORMAT_TEXT, item.getFormat()); |
| 110 | // TODO: Test spannables here. |
| 111 | assertEquals("Expected text", item.getText()); |
| 112 | } |
| 113 | |
| 114 | @Test |
| 115 | public void testIcon() { |
| 116 | Uri uri = BASE_URI.buildUpon().appendPath("icon").build(); |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 117 | Slice s = Slice.bindSlice(mContext, uri, Collections.<SliceSpec>emptyList()); |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 118 | assertEquals(uri, s.getUri()); |
| 119 | assertEquals(1, s.getItems().size()); |
| 120 | |
| 121 | SliceItem item = s.getItems().get(0); |
| 122 | assertEquals(FORMAT_IMAGE, item.getFormat()); |
| 123 | assertEquals(Icon.createWithResource(mContext, R.drawable.size_48x48).toString(), |
| 124 | item.getIcon().toString()); |
| 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void testAction() { |
| 129 | sFlag = false; |
| 130 | final CountDownLatch latch = new CountDownLatch(1); |
| 131 | BroadcastReceiver receiver = new BroadcastReceiver() { |
| 132 | @Override |
| 133 | public void onReceive(Context context, Intent intent) { |
| 134 | sFlag = true; |
| 135 | latch.countDown(); |
| 136 | } |
| 137 | }; |
| 138 | mContext.registerReceiver(receiver, |
| 139 | new IntentFilter(mContext.getPackageName() + ".action")); |
| 140 | Uri uri = BASE_URI.buildUpon().appendPath("action").build(); |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 141 | Slice s = Slice.bindSlice(mContext, uri, Collections.<SliceSpec>emptyList()); |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 142 | assertEquals(uri, s.getUri()); |
| 143 | assertEquals(1, s.getItems().size()); |
| 144 | |
| 145 | SliceItem item = s.getItems().get(0); |
| 146 | assertEquals(FORMAT_ACTION, item.getFormat()); |
| 147 | try { |
| 148 | item.getAction().send(); |
| 149 | } catch (CanceledException e) { |
| 150 | } |
| 151 | |
| 152 | try { |
| 153 | latch.await(100, TimeUnit.MILLISECONDS); |
| 154 | } catch (InterruptedException e) { |
| 155 | e.printStackTrace(); |
| 156 | } |
| 157 | assertTrue(sFlag); |
| 158 | mContext.unregisterReceiver(receiver); |
| 159 | } |
| 160 | |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 161 | @Test |
| 162 | public void testInt() { |
| 163 | Uri uri = BASE_URI.buildUpon().appendPath("int").build(); |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 164 | Slice s = Slice.bindSlice(mContext, uri, Collections.<SliceSpec>emptyList()); |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 165 | assertEquals(uri, s.getUri()); |
| 166 | assertEquals(1, s.getItems().size()); |
| 167 | |
| 168 | SliceItem item = s.getItems().get(0); |
| 169 | assertEquals(FORMAT_INT, item.getFormat()); |
| 170 | assertEquals(0xff121212, item.getInt()); |
| 171 | } |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 172 | |
| 173 | @Test |
| 174 | public void testTimestamp() { |
| 175 | Uri uri = BASE_URI.buildUpon().appendPath("timestamp").build(); |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 176 | Slice s = Slice.bindSlice(mContext, uri, Collections.<SliceSpec>emptyList()); |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 177 | assertEquals(uri, s.getUri()); |
| 178 | assertEquals(1, s.getItems().size()); |
| 179 | |
| 180 | SliceItem item = s.getItems().get(0); |
| 181 | assertEquals(FORMAT_TIMESTAMP, item.getFormat()); |
| 182 | assertEquals(43, item.getTimestamp()); |
| 183 | } |
| 184 | |
| 185 | @Test |
| 186 | public void testHints() { |
| 187 | // Note this tests that hints are propagated through to the client but not that any specific |
| 188 | // hints have any effects. |
| 189 | Uri uri = BASE_URI.buildUpon().appendPath("hints").build(); |
Jason Monk | a09cb67 | 2018-01-08 13:17:36 -0500 | [diff] [blame] | 190 | Slice s = Slice.bindSlice(mContext, uri, Collections.<SliceSpec>emptyList()); |
Jason Monk | 6fa0c9b | 2017-12-12 20:55:35 -0500 | [diff] [blame] | 191 | assertEquals(uri, s.getUri()); |
| 192 | |
| 193 | assertEquals(Arrays.asList(HINT_LIST), s.getHints()); |
| 194 | assertEquals(Arrays.asList(HINT_TITLE), s.getItems().get(0).getHints()); |
| 195 | assertEquals(Arrays.asList(HINT_NO_TINT, HINT_LARGE), |
| 196 | s.getItems().get(1).getHints()); |
| 197 | } |
| 198 | } |