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(); 34 } 35 36 public static final Corpus CORPUS_1 = new MockCorpus(MockSource.SOURCE_1) { 37 @Override 38 public int getQueryThreshold() { 39 return 3; 40 } 41 }; 42 43 public static final Corpus CORPUS_2 = new MockCorpus(MockSource.SOURCE_2) { 44 @Override 45 public boolean queryAfterZeroResults() { 46 return true; 47 } 48 }; 49 50 public void testRespectsQueryThreshold() { 51 assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "aa")); 52 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "aaa")); 53 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, "")); 54 } 55 56 public void testQueriesAfterNoResultsWhenQueryAfterZeroIsTrue() { 57 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, "query")); 58 mShouldQuery.onZeroResults(CORPUS_2, "query"); 59 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, "query")); 60 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, "query123")); 61 } 62 63 public void testDoesntQueryLongerAfterNoResults() { 64 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query")); 65 mShouldQuery.onZeroResults(CORPUS_1, "query"); 66 // Now we don't query for queries starting with "query" 67 assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "queryx")); 68 assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "queryxy")); 69 } 70 71 public void testDoesntQuerySameAfterNoResults() { 72 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query")); 73 mShouldQuery.onZeroResults(CORPUS_1, "query"); 74 // Now we don't query for "query" 75 assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query")); 76 } 77 78 public void testQueriesDifferent() { 79 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query")); 80 mShouldQuery.onZeroResults(CORPUS_1, "query"); 81 // Now we do query for "queen" 82 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "queen")); 83 mShouldQuery.onZeroResults(CORPUS_1, "queen"); 84 // And then not for "queens" 85 assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "queens")); 86 } 87 88 public void testShorterThenDifferent() { 89 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query")); 90 mShouldQuery.onZeroResults(CORPUS_1, "query"); 91 // Should query for shorter 92 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "que")); 93 mShouldQuery.onZeroResults(CORPUS_1, "que"); 94 // But not for longer after that 95 assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "queen")); 96 } 97 98 public void testQueriesForShorterAfterNoResults() { 99 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query")); 100 mShouldQuery.onZeroResults(CORPUS_1, "query"); 101 // We do query for something shorter 102 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "quer")); 103 } 104 105 public void testOutOfOrder1() { 106 assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "quer")); 107 // Result for something typed earlier comes in 108 mShouldQuery.onZeroResults(CORPUS_1, "que"); 109 // Now we don't query for the original 110 assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "quer")); 111 } 112 113 public void testOutOfOrder2() { 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 an extension of the original 118 assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query")); 119 } 120 } 121