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