1/*
2 * Copyright (C) 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 */
16package android.support.text.emoji;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertSame;
20
21import android.support.test.filters.SdkSuppress;
22import android.support.test.filters.SmallTest;
23import android.support.test.runner.AndroidJUnit4;
24import android.support.text.emoji.MetadataRepo.Node;
25
26import org.junit.Before;
27import org.junit.Test;
28import org.junit.runner.RunWith;
29
30@SmallTest
31@RunWith(AndroidJUnit4.class)
32@SdkSuppress(minSdkVersion = 19)
33public class MetadataRepoTest {
34
35    MetadataRepo mMetadataRepo;
36
37    @Before
38    public void clearResourceIndex() {
39        mMetadataRepo = new MetadataRepo();
40    }
41
42    @Test(expected = NullPointerException.class)
43    public void testPut_withNullMetadata() {
44        mMetadataRepo.put(null);
45    }
46
47    @Test(expected = IllegalArgumentException.class)
48    public void testPut_withEmptyKeys() {
49        mMetadataRepo.put(new TestEmojiMetadata(new int[0]));
50    }
51
52    @Test
53    public void testPut_withSingleCodePointMapping() {
54        final int[] codePoint = new int[]{1};
55        final TestEmojiMetadata metadata = new TestEmojiMetadata(codePoint);
56        mMetadataRepo.put(metadata);
57        assertSame(metadata, getNode(codePoint));
58    }
59
60    @Test
61    public void testPut_withMultiCodePointsMapping() {
62        final int[] codePoint = new int[]{1, 2, 3, 4};
63        final TestEmojiMetadata metadata = new TestEmojiMetadata(codePoint);
64        mMetadataRepo.put(metadata);
65        assertSame(metadata, getNode(codePoint));
66
67        assertEquals(null, getNode(new int[]{1}));
68        assertEquals(null, getNode(new int[]{1, 2}));
69        assertEquals(null, getNode(new int[]{1, 2, 3}));
70        assertEquals(null, getNode(new int[]{1, 2, 3, 5}));
71    }
72
73    @Test
74    public void testPut_sequentialCodePoints() {
75        final int[] codePoint1 = new int[]{1, 2, 3, 4};
76        final EmojiMetadata metadata1 = new TestEmojiMetadata(codePoint1);
77
78        final int[] codePoint2 = new int[]{1, 2, 3};
79        final EmojiMetadata metadata2 = new TestEmojiMetadata(codePoint2);
80
81        final int[] codePoint3 = new int[]{1, 2};
82        final EmojiMetadata metadata3 = new TestEmojiMetadata(codePoint3);
83
84        mMetadataRepo.put(metadata1);
85        mMetadataRepo.put(metadata2);
86        mMetadataRepo.put(metadata3);
87
88        assertSame(metadata1, getNode(codePoint1));
89        assertSame(metadata2, getNode(codePoint2));
90        assertSame(metadata3, getNode(codePoint3));
91
92        assertEquals(null, getNode(new int[]{1}));
93        assertEquals(null, getNode(new int[]{1, 2, 3, 4, 5}));
94    }
95
96    final EmojiMetadata getNode(final int[] codepoints) {
97        return getNode(mMetadataRepo.getRootNode(), codepoints, 0);
98    }
99
100    final EmojiMetadata getNode(Node node, final int[] codepoints, int start) {
101        if (codepoints.length < start) return null;
102        if (codepoints.length == start) return node.getData();
103
104        final Node childNode = node.get(codepoints[start]);
105        if (childNode == null) return null;
106        return getNode(childNode, codepoints, start + 1);
107    }
108}
109