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