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 17package androidx.slice; 18 19 20import static android.app.slice.SliceItem.FORMAT_ACTION; 21import static android.app.slice.SliceItem.FORMAT_SLICE; 22import static android.app.slice.SliceItem.FORMAT_TEXT; 23 24import static junit.framework.Assert.assertEquals; 25import static junit.framework.Assert.assertTrue; 26 27import static org.mockito.ArgumentMatchers.eq; 28import static org.mockito.Mockito.mock; 29import static org.mockito.Mockito.verify; 30 31import android.app.PendingIntent; 32import android.content.Context; 33import android.graphics.Bitmap; 34import android.graphics.Canvas; 35import android.net.Uri; 36import android.support.test.InstrumentationRegistry; 37import android.support.test.filters.SmallTest; 38import android.support.test.runner.AndroidJUnit4; 39 40import androidx.core.graphics.drawable.IconCompat; 41import androidx.slice.core.SliceQuery; 42import androidx.slice.view.R; 43 44import org.junit.Test; 45import org.junit.runner.RunWith; 46 47import java.io.ByteArrayInputStream; 48import java.io.ByteArrayOutputStream; 49import java.io.IOException; 50 51@RunWith(AndroidJUnit4.class) 52@SmallTest 53public class SliceXmlTest { 54 55 private final Context mContext = InstrumentationRegistry.getContext(); 56 57 @Test(expected = IllegalArgumentException.class) 58 public void testThrowForAction() throws IOException { 59 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 60 Slice s = new Slice.Builder(Uri.parse("content://pkg/slice")) 61 .addAction((PendingIntent) null, null, null) 62 .build(); 63 SliceUtils.serializeSlice(s, mContext, outputStream, "UTF-8", new SliceUtils 64 .SerializeOptions()); 65 } 66 67 @Test(expected = IllegalArgumentException.class) 68 public void testThrowForRemoteInput() throws IOException { 69 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 70 Slice s = new Slice.Builder(Uri.parse("content://pkg/slice")) 71 .addRemoteInput(null, null) 72 .build(); 73 SliceUtils.serializeSlice(s, mContext, outputStream, "UTF-8", new SliceUtils 74 .SerializeOptions()); 75 } 76 77 @Test(expected = IllegalArgumentException.class) 78 public void testThrowForImage() throws IOException { 79 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 80 Slice s = new Slice.Builder(Uri.parse("content://pkg/slice")) 81 .addIcon(null, null) 82 .build(); 83 SliceUtils.serializeSlice(s, mContext, outputStream, "UTF-8", new SliceUtils 84 .SerializeOptions()); 85 } 86 87 @Test 88 public void testNoThrowForAction() throws IOException { 89 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 90 Slice s = new Slice.Builder(Uri.parse("content://pkg/slice")) 91 .addAction((PendingIntent) null, null, null) 92 .build(); 93 SliceUtils.serializeSlice(s, mContext, outputStream, "UTF-8", new SliceUtils 94 .SerializeOptions().setActionMode(SliceUtils.SerializeOptions.MODE_REMOVE)); 95 } 96 97 @Test 98 public void testNoThrowForRemoteInput() throws IOException { 99 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 100 Slice s = new Slice.Builder(Uri.parse("content://pkg/slice")) 101 .addRemoteInput(null, null) 102 .build(); 103 SliceUtils.serializeSlice(s, mContext, outputStream, "UTF-8", new SliceUtils 104 .SerializeOptions().setActionMode(SliceUtils.SerializeOptions.MODE_REMOVE)); 105 } 106 107 @Test 108 public void testNoThrowForImage() throws IOException { 109 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 110 Slice s = new Slice.Builder(Uri.parse("content://pkg/slice")) 111 .addIcon(null, null) 112 .build(); 113 SliceUtils.serializeSlice(s, mContext, outputStream, "UTF-8", new SliceUtils 114 .SerializeOptions().setImageMode(SliceUtils.SerializeOptions.MODE_REMOVE)); 115 } 116 117 @Test 118 public void testSerialization() throws Exception { 119 Bitmap b = Bitmap.createBitmap(50, 25, Bitmap.Config.ARGB_8888); 120 new Canvas(b).drawColor(0xffff0000); 121 // Create a slice containing all the types in a hierarchy. 122 Slice before = new Slice.Builder(Uri.parse("content://pkg/slice")) 123 .addSubSlice(new Slice.Builder(Uri.parse("content://pkg/slice/sub")) 124 .addTimestamp(System.currentTimeMillis(), null, "Hint") 125 .build()) 126 .addIcon(IconCompat.createWithBitmap(b), null) 127 .addText("Some text", null) 128 .addAction((PendingIntent) null, 129 new Slice.Builder(Uri.parse("content://pkg/slice/action")) 130 .addText("Action text", null) 131 .build(), null) 132 .addInt(0xff00ff00, "subtype") 133 .addIcon(IconCompat.createWithResource(mContext, R.drawable.abc_slice_see_more_bg), 134 null) 135 .addHints("Hint 1", "Hint 2") 136 .build(); 137 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 138 139 SliceUtils.serializeSlice(before, mContext, outputStream, "UTF-8", 140 new SliceUtils.SerializeOptions() 141 .setImageMode(SliceUtils.SerializeOptions.MODE_CONVERT) 142 .setActionMode(SliceUtils.SerializeOptions.MODE_CONVERT)); 143 144 byte[] bytes = outputStream.toByteArray(); 145 ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); 146 SliceUtils.SliceActionListener listener = mock(SliceUtils.SliceActionListener.class); 147 Slice after = SliceUtils.parseSlice(mContext, inputStream, "UTF-8", listener); 148 149 assertEquivalent(before, after); 150 151 SliceItem action = SliceQuery.find(after, FORMAT_ACTION); 152 action.fireAction(null, null); 153 verify(listener).onSliceAction(eq(Uri.parse("content://pkg/slice/action"))); 154 } 155 156 private void assertEquivalent(Slice desired, Slice actual) { 157 assertEquals(desired.getUri(), actual.getUri()); 158 assertEquals(desired.getHints(), actual.getHints()); 159 assertEquals(desired.getItems().size(), actual.getItems().size()); 160 161 for (int i = 0; i < desired.getItems().size(); i++) { 162 assertEquivalent(desired.getItems().get(i), actual.getItems().get(i)); 163 } 164 } 165 166 private void assertEquivalent(SliceItem desired, SliceItem actual) { 167 boolean isSliceType = FORMAT_SLICE.equals(desired.getFormat()) 168 || FORMAT_ACTION.equals(desired.getFormat()); 169 if (isSliceType) { 170 assertTrue(FORMAT_SLICE.equals(actual.getFormat()) 171 || FORMAT_ACTION.equals(actual.getFormat())); 172 } else { 173 assertEquals(desired.getFormat(), actual.getFormat()); 174 if (FORMAT_TEXT.equals(desired.getFormat())) { 175 assertEquals(String.valueOf(desired.getText()), String.valueOf(actual.getText())); 176 } 177 } 178 } 179} 180