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.matches;
19 
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertTrue;
22 
23 import androidx.test.filters.SmallTest;
24 import androidx.test.runner.AndroidJUnit4;
25 
26 import com.android.launcher3.search.StringMatcherUtility.StringMatcher;
27 
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 
31 /**
32  * Unit tests for {@link StringMatcherUtility}
33  */
34 @SmallTest
35 @RunWith(AndroidJUnit4.class)
36 public class StringMatcherUtilityTest {
37     private static final StringMatcher MATCHER =
38             StringMatcher.getInstance();
39 
40     @Test
testMatches()41     public void testMatches() {
42         assertTrue(matches("white ", "white cow", MATCHER));
43         assertTrue(matches("white c", "white cow", MATCHER));
44         assertTrue(matches("cow", "white cow", MATCHER));
45         assertTrue(matches("cow", "whiteCow", MATCHER));
46         assertTrue(matches("cow", "whiteCOW", MATCHER));
47         assertTrue(matches("cow", "whitecowCOW", MATCHER));
48         assertTrue(matches("cow", "white2cow", MATCHER));
49 
50         assertFalse(matches("cow", "whitecow", MATCHER));
51         assertFalse(matches("cow", "whitEcow", MATCHER));
52 
53         assertTrue(matches("cow", "whitecowCow", MATCHER));
54         assertTrue(matches("cow", "whitecow cow", MATCHER));
55         assertFalse(matches("cow", "whitecowcow", MATCHER));
56         assertFalse(matches("cow", "whit ecowcow", MATCHER));
57 
58         assertTrue(matches("dog", "cats&dogs", MATCHER));
59         assertTrue(matches("dog", "cats&Dogs", MATCHER));
60         assertTrue(matches("&", "cats&Dogs", MATCHER));
61 
62         assertTrue(matches("43", "2+43", MATCHER));
63         assertFalse(matches("3", "2+43", MATCHER));
64 
65         assertTrue(matches("q", "Q", MATCHER));
66         assertTrue(matches("q", "  Q", MATCHER));
67 
68         // match lower case words
69         assertTrue(matches("e", "elephant", MATCHER));
70         assertTrue(matches("eL", "Elephant", MATCHER));
71 
72         assertTrue(matches("电", "电子邮件", MATCHER));
73         assertTrue(matches("电子", "电子邮件", MATCHER));
74         assertTrue(matches("子", "电子邮件", MATCHER));
75         assertTrue(matches("邮件", "电子邮件", MATCHER));
76 
77         assertFalse(matches("ba", "Bot", MATCHER));
78         assertFalse(matches("ba", "bot", MATCHER));
79         assertFalse(matches("phant", "elephant", MATCHER));
80         assertFalse(matches("elephants", "elephant", MATCHER));
81     }
82 
83     @Test
testMatchesVN()84     public void testMatchesVN() {
85         assertTrue(matches("다", "다운로드", MATCHER));
86         assertTrue(matches("드", "드라이브", MATCHER));
87         assertTrue(matches("ㄷ", "다운로드 드라이브", MATCHER));
88         assertTrue(matches("ㄷ", "운로 드라이브", MATCHER));
89         assertTrue(matches("åbç", "abc", MATCHER));
90         assertTrue(matches("ål", "Alpha", MATCHER));
91 
92         assertFalse(matches("ㄷㄷ", "다운로드 드라이브", MATCHER));
93         assertFalse(matches("ㄷ", "로드라이브", MATCHER));
94         assertFalse(matches("åç", "abc", MATCHER));
95     }
96 }
97