1 /*
2  * Copyright (C) 2016 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 package com.android.launcher3.search;
17 
18 import static com.android.launcher3.search.StringMatcherUtility.getListOfBreakpoints;
19 import static com.android.launcher3.search.StringMatcherUtility.matches;
20 
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24 
25 import androidx.test.filters.SmallTest;
26 import androidx.test.runner.AndroidJUnit4;
27 
28 import com.android.launcher3.search.StringMatcherUtility.StringMatcher;
29 import com.android.launcher3.search.StringMatcherUtility.StringMatcherSpace;
30 import com.android.launcher3.util.IntArray;
31 
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 /**
36  * Unit tests for {@link StringMatcherUtility}
37  */
38 @SmallTest
39 @RunWith(AndroidJUnit4.class)
40 public class StringMatcherUtilityTest {
41     private static final StringMatcher MATCHER = StringMatcher.getInstance();
42     private static final StringMatcherSpace MATCHER_SPACE = StringMatcherSpace.getInstance();
43 
44     @Test
testMatches()45     public void testMatches() {
46         assertTrue(matches("white", "white cow", MATCHER));
47         assertTrue(matches("white ", "white cow", MATCHER));
48         assertTrue(matches("white c", "white cow", MATCHER));
49         assertTrue(matches("cow", "white cow", MATCHER));
50         assertTrue(matches("cow", "whiteCow", MATCHER));
51         assertTrue(matches("cow", "whiteCOW", MATCHER));
52         assertTrue(matches("cow", "whitecowCOW", MATCHER));
53         assertTrue(matches("cow", "white2cow", MATCHER));
54 
55         assertFalse(matches("cow", "whitecow", MATCHER));
56         assertFalse(matches("cow", "whitEcow", MATCHER));
57 
58         assertTrue(matches("cow", "whitecowCow", MATCHER));
59         assertTrue(matches("cow", "whitecow cow", MATCHER));
60         assertFalse(matches("cow", "whitecowcow", MATCHER));
61         assertFalse(matches("cow", "whit ecowcow", MATCHER));
62 
63         assertTrue(matches("dog", "cats&dogs", MATCHER));
64         assertTrue(matches("dog", "cats&Dogs", MATCHER));
65         assertTrue(matches("&", "cats&Dogs", MATCHER));
66 
67         assertTrue(matches("43", "2+43", MATCHER));
68         assertFalse(matches("3", "2+43", MATCHER));
69 
70         assertTrue(matches("q", "Q", MATCHER));
71         assertTrue(matches("q", "  Q", MATCHER));
72 
73         // match lower case words
74         assertTrue(matches("e", "elephant", MATCHER));
75         assertTrue(matches("eL", "Elephant", MATCHER));
76 
77         assertTrue(matches("电", "电子邮件", MATCHER));
78         assertTrue(matches("电子", "电子邮件", MATCHER));
79         assertTrue(matches("子", "电子邮件", MATCHER));
80         assertTrue(matches("邮件", "电子邮件", MATCHER));
81 
82         assertFalse(matches("ba", "Bot", MATCHER));
83         assertFalse(matches("ba", "bot", MATCHER));
84         assertFalse(matches("phant", "elephant", MATCHER));
85         assertFalse(matches("elephants", "elephant", MATCHER));
86     }
87 
88     @Test
testMatchesVN()89     public void testMatchesVN() {
90         assertTrue(matches("다", "다운로드", MATCHER));
91         assertTrue(matches("드", "드라이브", MATCHER));
92         assertTrue(matches("ㄷ", "다운로드 드라이브", MATCHER));
93         assertTrue(matches("ㄷ", "운로 드라이브", MATCHER));
94         assertTrue(matches("åbç", "abc", MATCHER));
95         assertTrue(matches("ål", "Alpha", MATCHER));
96 
97         assertFalse(matches("ㄷㄷ", "다운로드 드라이브", MATCHER));
98         assertFalse(matches("ㄷ", "로드라이브", MATCHER));
99         assertFalse(matches("åç", "abc", MATCHER));
100     }
101 
102     @Test
testMatchesWithSpaceBreakOnly()103     public void testMatchesWithSpaceBreakOnly() {
104         assertTrue(matches("white", "white cow", MATCHER_SPACE));
105         assertTrue(matches("white ", "white cow", MATCHER_SPACE));
106         assertTrue(matches("white c", "white cow", MATCHER_SPACE));
107         assertTrue(matches("cow", "white cow", MATCHER_SPACE));
108         assertTrue(matches("cow", "whitecow cow", MATCHER_SPACE));
109 
110         assertFalse(matches("cow", "whiteCow", MATCHER_SPACE));
111         assertFalse(matches("cow", "whiteCOW", MATCHER_SPACE));
112         assertFalse(matches("cow", "whitecowCOW", MATCHER_SPACE));
113         assertFalse(matches("cow", "white2cow", MATCHER_SPACE));
114         assertFalse(matches("cow", "whitecow", MATCHER_SPACE));
115         assertFalse(matches("cow", "whitEcow", MATCHER_SPACE));
116         assertFalse(matches("cow", "whitecowCow", MATCHER_SPACE));
117         assertFalse(matches("cow", "whitecowcow", MATCHER_SPACE));
118         assertFalse(matches("cow", "whit ecowcow", MATCHER_SPACE));
119 
120         assertFalse(matches("dog", "cats&dogs", MATCHER_SPACE));
121         assertFalse(matches("dog", "cats&Dogs", MATCHER_SPACE));
122         assertFalse(matches("&", "cats&Dogs", MATCHER_SPACE));
123 
124         assertFalse(matches("43", "2+43", MATCHER_SPACE));
125         assertFalse(matches("3", "2+43", MATCHER_SPACE));
126 
127         assertTrue(matches("q", "Q", MATCHER_SPACE));
128         assertTrue(matches("q", "  Q", MATCHER_SPACE));
129 
130         // match lower case words
131         assertTrue(matches("e", "elephant", MATCHER_SPACE));
132         assertTrue(matches("eL", "Elephant", MATCHER_SPACE));
133 
134         assertTrue(matches("电", "电子邮件", MATCHER_SPACE));
135         assertTrue(matches("电子", "电子邮件", MATCHER_SPACE));
136         assertTrue(matches("子", "电子邮件", MATCHER_SPACE));
137         assertTrue(matches("邮件", "电子邮件", MATCHER_SPACE));
138 
139         assertFalse(matches("ba", "Bot", MATCHER_SPACE));
140         assertFalse(matches("ba", "bot", MATCHER_SPACE));
141         assertFalse(matches("phant", "elephant", MATCHER_SPACE));
142         assertFalse(matches("elephants", "elephant", MATCHER_SPACE));
143     }
144 
145     @Test
testStringWithProperBreaks()146     public void testStringWithProperBreaks() {
147         // empty string
148         assertEquals(IntArray.wrap(), getListOfBreakpoints("", MATCHER));
149 
150         // should be "D Dz" that's why breakpoint is at 0
151         assertEquals(IntArray.wrap(0), getListOfBreakpoints("DDz", MATCHER));
152 
153         // test all caps and all lower-case
154         assertEquals(IntArray.wrap(), getListOfBreakpoints("SNKRS", MATCHER));
155         assertEquals(IntArray.wrap(), getListOfBreakpoints("flutterappflorafy", MATCHER));
156         assertEquals(IntArray.wrap(), getListOfBreakpoints("LEGO®", MATCHER));
157 
158         // test camel case
159         // breakpoint at 9 to be "flutterapp Florafy"
160         assertEquals(IntArray.wrap(9), getListOfBreakpoints("flutterappFlorafy", MATCHER));
161         // breakpoint at 4 to be "Metro Zone"
162         assertEquals(IntArray.wrap(4), getListOfBreakpoints("MetroZone", MATCHER));
163         // breakpoint at 4,5 to be "metro X Zone"
164         assertEquals(IntArray.wrap(4,5), getListOfBreakpoints("metroXZone", MATCHER));
165         // breakpoint at 0 to be "G Pay"
166         assertEquals(IntArray.wrap(0), getListOfBreakpoints("GPay", MATCHER));
167         // breakpoint at 4 to be "Whats App"
168         assertEquals(IntArray.wrap(4), getListOfBreakpoints("WhatsApp", MATCHER));
169         // breakpoint at 2 to be "aaa A"
170         assertEquals(IntArray.wrap(2), getListOfBreakpoints("aaaA", MATCHER));
171         // breakpoint at 4,12,16 to be "Other Launcher Test App"
172         assertEquals(IntArray.wrap(4,12,16),
173                 getListOfBreakpoints("OtherLauncherTestApp", MATCHER));
174 
175         // test with TITLECASE_LETTER
176         // should be "DDz" that's why there are no break points
177         assertEquals(IntArray.wrap(), getListOfBreakpoints("DDz", MATCHER));
178         // breakpoint at 0 to be "D DDž"
179         assertEquals(IntArray.wrap(0), getListOfBreakpoints("DDDž", MATCHER));
180         // breakpoint at 0 because there is a space to be "Dž DD"
181         assertEquals(IntArray.wrap(0), getListOfBreakpoints("Dž DD", MATCHER));
182         // breakpoint at 1 to be "Dw Dz"
183         assertEquals(IntArray.wrap(1), getListOfBreakpoints("DwDz", MATCHER));
184         // breakpoint at 0,2 to be "Dw Dz"
185         assertEquals(IntArray.wrap(0,2), getListOfBreakpoints("wDwDz", MATCHER));
186         // breakpoint at 1,3 to be "ᾋw Dw Dz"
187         assertEquals(IntArray.wrap(1,3), getListOfBreakpoints("ᾋwDwDz", MATCHER));
188         // breakpoint at 0,2,4 to be "ᾋ ᾋw Dw Dz"
189         assertEquals(IntArray.wrap(0,2,4), getListOfBreakpoints("ᾋᾋwDwDz", MATCHER));
190         // breakpoint at 0,2,4 to be "ᾋ ᾋw Dw Dz®"
191         assertEquals(IntArray.wrap(0,2,4), getListOfBreakpoints("ᾋᾋwDwDz®", MATCHER));
192 
193         // test with numbers and symbols
194         // breakpoint at 3,11 to be "Test Activity 13"
195         assertEquals(IntArray.wrap(3,11), getListOfBreakpoints("TestActivity13", MATCHER));
196         // breakpoint at 3, 4, 12, 13 as the breakpoints are at the dashes
197         assertEquals(IntArray.wrap(3,4,12,13),
198                 getListOfBreakpoints("Test-Activity-12", MATCHER));
199         // breakpoint at 1 to be "AA 2"
200         assertEquals(IntArray.wrap(1), getListOfBreakpoints("AA2", MATCHER));
201         // breakpoint at 1 to be "AAA 2"
202         assertEquals(IntArray.wrap(2), getListOfBreakpoints("AAA2", MATCHER));
203         // breakpoint at 1 to be "ab 2"
204         assertEquals(IntArray.wrap(1), getListOfBreakpoints("ab2", MATCHER));
205         // breakpoint at 1,2 to be "el 3 suhwee"
206         assertEquals(IntArray.wrap(1,2), getListOfBreakpoints("el3suhwee", MATCHER));
207         // breakpoint at 0,1 as the breakpoints are at '-'
208         assertEquals(IntArray.wrap(0,1), getListOfBreakpoints("t-mobile", MATCHER));
209         assertEquals(IntArray.wrap(0,1), getListOfBreakpoints("t-Mobile", MATCHER));
210         // breakpoint at 0,1,2 as the breakpoints are at '-'
211         assertEquals(IntArray.wrap(0,1,2), getListOfBreakpoints("t--Mobile", MATCHER));
212         // breakpoint at 1,2,3 as the breakpoints are at '-'
213         assertEquals(IntArray.wrap(1,2,3), getListOfBreakpoints("tr--Mobile", MATCHER));
214         // breakpoint at 3,4 as the breakpoints are at '.'
215         assertEquals(IntArray.wrap(3,4), getListOfBreakpoints("Agar.io", MATCHER));
216         assertEquals(IntArray.wrap(3,4), getListOfBreakpoints("Hole.Io", MATCHER));
217 
218         // breakpoint at 0 to be "µ Torrent®"
219         assertEquals(IntArray.wrap(0), getListOfBreakpoints("µTorrent®", MATCHER));
220         // breakpoint at 4 to be "LEGO® Builder"
221         assertEquals(IntArray.wrap(4), getListOfBreakpoints("LEGO®Builder", MATCHER));
222         // breakpoint at 4 to be "LEGO® builder"
223         assertEquals(IntArray.wrap(4), getListOfBreakpoints("LEGO®builder", MATCHER));
224         // breakpoint at 4 to be "lego® builder"
225         assertEquals(IntArray.wrap(4), getListOfBreakpoints("lego®builder", MATCHER));
226 
227         // test string with spaces - where the breakpoints are right before where the spaces are at
228         assertEquals(IntArray.wrap(3,8), getListOfBreakpoints("HEAD BALL 2", MATCHER));
229         assertEquals(IntArray.wrap(2,8),
230                 getListOfBreakpoints("OFL Agent Application", MATCHER));
231         assertEquals(IntArray.wrap(0,2), getListOfBreakpoints("D D z", MATCHER));
232         assertEquals(IntArray.wrap(6), getListOfBreakpoints("Battery Stats", MATCHER));
233         assertEquals(IntArray.wrap(5,9,15),
234                 getListOfBreakpoints("System UWB Field Test", MATCHER));
235     }
236 }
237