1e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal/*
2e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal * Copyright (C) 2016 The Android Open Source Project
3e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal *
4e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal * Licensed under the Apache License, Version 2.0 (the "License");
5e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal * you may not use this file except in compliance with the License.
6e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal * You may obtain a copy of the License at
7e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal *
8e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal *      http://www.apache.org/licenses/LICENSE-2.0
9e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal *
10e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal * Unless required by applicable law or agreed to in writing, software
11e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal * distributed under the License is distributed on an "AS IS" BASIS,
12e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal * See the License for the specific language governing permissions and
14e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal * limitations under the License.
15e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal */
16161f96bc77805ed87f831b68e51fad61a23153bcSunny Goyalpackage com.android.launcher3.allapps.search;
17e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal
18e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyalimport android.content.ComponentName;
191d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport android.support.test.runner.AndroidJUnit4;
20e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal
21e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyalimport com.android.launcher3.AppInfo;
2205d2df1678bfba02c2fbc54ce03229f45b0308cdSunny Goyalimport com.android.launcher3.Utilities;
23e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal
241d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport org.junit.Test;
251d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport org.junit.runner.RunWith;
261d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal
271d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport static org.junit.Assert.assertFalse;
281d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalimport static org.junit.Assert.assertTrue;
29e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal
30e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal/**
31e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal * Unit tests for {@link DefaultAppSearchAlgorithm}
32e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal */
331d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal@RunWith(AndroidJUnit4.class)
341d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyalpublic class DefaultAppSearchAlgorithmTest {
351d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal    private static final DefaultAppSearchAlgorithm.StringMatcher MATCHER =
361d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal            DefaultAppSearchAlgorithm.StringMatcher.getInstance();
37e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal
381d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal    @Test
39e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal    public void testMatches() {
401d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("white cow"), "cow", MATCHER));
411d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("whiteCow"), "cow", MATCHER));
421d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("whiteCOW"), "cow", MATCHER));
431d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("whitecowCOW"), "cow", MATCHER));
441d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("white2cow"), "cow", MATCHER));
45e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal
461d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("whitecow"), "cow", MATCHER));
471d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("whitEcow"), "cow", MATCHER));
48e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal
491d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("whitecowCow"), "cow", MATCHER));
501d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("whitecow cow"), "cow", MATCHER));
511d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("whitecowcow"), "cow", MATCHER));
521d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("whit ecowcow"), "cow", MATCHER));
53e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal
541d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("cats&dogs"), "dog", MATCHER));
551d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("cats&Dogs"), "dog", MATCHER));
561d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("cats&Dogs"), "&", MATCHER));
57e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal
581d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("2+43"), "43", MATCHER));
591d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("2+43"), "3", MATCHER));
60e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal
611d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("Q"), "q", MATCHER));
621d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("  Q"), "q", MATCHER));
6328a64381b67d72fcc8b994343507ed9c5821df53Sunny Goyal
6428a64381b67d72fcc8b994343507ed9c5821df53Sunny Goyal        // match lower case words
651d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("elephant"), "e", MATCHER));
6628a64381b67d72fcc8b994343507ed9c5821df53Sunny Goyal
671d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("电子邮件"), "电", MATCHER));
681d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("电子邮件"), "电子", MATCHER));
691d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("电子邮件"), "子", MATCHER));
701d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("电子邮件"), "邮件", MATCHER));
7105d2df1678bfba02c2fbc54ce03229f45b0308cdSunny Goyal
721d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("Bot"), "ba", MATCHER));
731d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("bot"), "ba", MATCHER));
7405d2df1678bfba02c2fbc54ce03229f45b0308cdSunny Goyal    }
7505d2df1678bfba02c2fbc54ce03229f45b0308cdSunny Goyal
761d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal    @Test
7705d2df1678bfba02c2fbc54ce03229f45b0308cdSunny Goyal    public void testMatchesVN() {
7805d2df1678bfba02c2fbc54ce03229f45b0308cdSunny Goyal        if (!Utilities.ATLEAST_NOUGAT) {
7905d2df1678bfba02c2fbc54ce03229f45b0308cdSunny Goyal            return;
8005d2df1678bfba02c2fbc54ce03229f45b0308cdSunny Goyal        }
811d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("다운로드"), "다", MATCHER));
821d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("드라이브"), "드", MATCHER));
831d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("다운로드 드라이브"), "ㄷ", MATCHER));
841d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("운로 드라이브"), "ㄷ", MATCHER));
851d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("abc"), "åbç", MATCHER));
861d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("Alpha"), "ål", MATCHER));
871d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal
881d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("다운로드 드라이브"), "ㄷㄷ", MATCHER));
891d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("로드라이브"), "ㄷ", MATCHER));
901d7f45d8f8fafc46f23963e3b43c95a2c2120079Sunny Goyal        assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("abc"), "åç", MATCHER));
91e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal    }
92e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal
93e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal    private AppInfo getInfo(String title) {
94e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal        AppInfo info = new AppInfo();
95e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal        info.title = title;
96e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal        info.componentName = new ComponentName("Test", title);
97e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal        return info;
98e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal    }
99e4a3e0cfaf5b72a54e99bcebdc03eda8aef53091Sunny Goyal}
100