Home | History | Annotate | Download | only in search
      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.allapps.search;
     17 
     18 import android.content.ComponentName;
     19 import android.support.test.runner.AndroidJUnit4;
     20 
     21 import com.android.launcher3.AppInfo;
     22 import com.android.launcher3.Utilities;
     23 
     24 import org.junit.Test;
     25 import org.junit.runner.RunWith;
     26 
     27 import static org.junit.Assert.assertFalse;
     28 import static org.junit.Assert.assertTrue;
     29 
     30 /**
     31  * Unit tests for {@link DefaultAppSearchAlgorithm}
     32  */
     33 @RunWith(AndroidJUnit4.class)
     34 public class DefaultAppSearchAlgorithmTest {
     35     private static final DefaultAppSearchAlgorithm.StringMatcher MATCHER =
     36             DefaultAppSearchAlgorithm.StringMatcher.getInstance();
     37 
     38     @Test
     39     public void testMatches() {
     40         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("white cow"), "cow", MATCHER));
     41         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("whiteCow"), "cow", MATCHER));
     42         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("whiteCOW"), "cow", MATCHER));
     43         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("whitecowCOW"), "cow", MATCHER));
     44         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("white2cow"), "cow", MATCHER));
     45 
     46         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("whitecow"), "cow", MATCHER));
     47         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("whitEcow"), "cow", MATCHER));
     48 
     49         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("whitecowCow"), "cow", MATCHER));
     50         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("whitecow cow"), "cow", MATCHER));
     51         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("whitecowcow"), "cow", MATCHER));
     52         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("whit ecowcow"), "cow", MATCHER));
     53 
     54         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("cats&dogs"), "dog", MATCHER));
     55         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("cats&Dogs"), "dog", MATCHER));
     56         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("cats&Dogs"), "&", MATCHER));
     57 
     58         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("2+43"), "43", MATCHER));
     59         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("2+43"), "3", MATCHER));
     60 
     61         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("Q"), "q", MATCHER));
     62         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("  Q"), "q", MATCHER));
     63 
     64         // match lower case words
     65         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("elephant"), "e", MATCHER));
     66 
     67         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo(""), "", MATCHER));
     68         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo(""), "", MATCHER));
     69         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo(""), "", MATCHER));
     70         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo(""), "", MATCHER));
     71 
     72         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("Bot"), "ba", MATCHER));
     73         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("bot"), "ba", MATCHER));
     74     }
     75 
     76     @Test
     77     public void testMatchesVN() {
     78         if (!Utilities.ATLEAST_NOUGAT) {
     79             return;
     80         }
     81         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo(""), "", MATCHER));
     82         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo(""), "", MATCHER));
     83         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo(" "), "", MATCHER));
     84         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo(" "), "", MATCHER));
     85         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("abc"), "b", MATCHER));
     86         assertTrue(DefaultAppSearchAlgorithm.matches(getInfo("Alpha"), "l", MATCHER));
     87 
     88         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo(" "), "", MATCHER));
     89         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo(""), "", MATCHER));
     90         assertFalse(DefaultAppSearchAlgorithm.matches(getInfo("abc"), "", MATCHER));
     91     }
     92 
     93     private AppInfo getInfo(String title) {
     94         AppInfo info = new AppInfo();
     95         info.title = title;
     96         info.componentName = new ComponentName("Test", title);
     97         return info;
     98     }
     99 }
    100