Home | History | Annotate | Download | only in quicksearchbox
      1 /*
      2  * Copyright (C) 2010 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 
     17 package com.android.quicksearchbox;
     18 
     19 import android.test.AndroidTestCase;
     20 import android.test.suitebuilder.annotation.SmallTest;
     21 
     22 /**
     23  * Tests for {@link ShouldQueryStrategy}.
     24  */
     25 @SmallTest
     26 public class ShouldQueryStrategyTest extends AndroidTestCase {
     27 
     28     private ShouldQueryStrategy mShouldQuery;
     29 
     30     @Override
     31     public void setUp() throws Exception {
     32         super.setUp();
     33         mShouldQuery = new ShouldQueryStrategy(new Config(getContext()){
     34             @Override
     35             public boolean showSuggestionsForZeroQuery() {
     36                 return true;
     37             }
     38             @Override
     39             public boolean showShortcutsForZeroQuery() {
     40                 return true;
     41             }});
     42     }
     43 
     44     public static final Corpus CORPUS_1 = new MockCorpus(MockSource.SOURCE_1) {
     45         @Override
     46         public int getQueryThreshold() {
     47             return 3;
     48         }
     49     };
     50 
     51     public static final Corpus CORPUS_2 = new MockCorpus(MockSource.SOURCE_2) {
     52         @Override
     53         public boolean queryAfterZeroResults() {
     54             return true;
     55         }
     56     };
     57 
     58     public void testRespectsQueryThreshold() {
     59         assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "aa"));
     60         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "aaa"));
     61         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, ""));
     62     }
     63 
     64     public void testQueriesAfterNoResultsWhenQueryAfterZeroIsTrue() {
     65         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, "query"));
     66         mShouldQuery.onZeroResults(CORPUS_2, "query");
     67         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, "query"));
     68         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, "query123"));
     69     }
     70 
     71     public void testDoesntQueryLongerAfterNoResults() {
     72         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query"));
     73         mShouldQuery.onZeroResults(CORPUS_1, "query");
     74         // Now we don't query for queries starting with "query"
     75         assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "queryx"));
     76         assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "queryxy"));
     77     }
     78 
     79     public void testDoesntQuerySameAfterNoResults() {
     80         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query"));
     81         mShouldQuery.onZeroResults(CORPUS_1, "query");
     82         // Now we don't query for "query"
     83         assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query"));
     84     }
     85 
     86     public void testQueriesDifferent() {
     87         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query"));
     88         mShouldQuery.onZeroResults(CORPUS_1, "query");
     89         // Now we do query for "queen"
     90         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "queen"));
     91         mShouldQuery.onZeroResults(CORPUS_1, "queen");
     92         // And then not for "queens"
     93         assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "queens"));
     94     }
     95 
     96     public void testShorterThenDifferent() {
     97         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query"));
     98         mShouldQuery.onZeroResults(CORPUS_1, "query");
     99         // Should query for shorter
    100         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "que"));
    101         mShouldQuery.onZeroResults(CORPUS_1, "que");
    102         // But not for longer after that
    103         assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "queen"));
    104     }
    105 
    106     public void testQueriesForShorterAfterNoResults() {
    107         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query"));
    108         mShouldQuery.onZeroResults(CORPUS_1, "query");
    109         // We do query for something shorter
    110         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "quer"));
    111     }
    112 
    113     public void testOutOfOrder1() {
    114         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "quer"));
    115         // Result for something typed earlier comes in
    116         mShouldQuery.onZeroResults(CORPUS_1, "que");
    117         // Now we don't query for the original
    118         assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "quer"));
    119     }
    120 
    121     public void testOutOfOrder2() {
    122         assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "quer"));
    123         // Result for something typed earlier comes in
    124         mShouldQuery.onZeroResults(CORPUS_1, "que");
    125         // Now we don't query for an extension of the original
    126         assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query"));
    127     }
    128 }
    129