ShouldQueryStrategyTest.java revision 04a180b52fb4100a2f3747e795fb5d26e3207a4a
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
17package com.android.quicksearchbox;
18
19import android.test.AndroidTestCase;
20
21/**
22 * Tests for {@link ShouldQueryStrategy}.
23 */
24public class ShouldQueryStrategyTest extends AndroidTestCase {
25
26    private ShouldQueryStrategy mShouldQuery;
27
28    @Override
29    public void setUp() throws Exception {
30        super.setUp();
31        mShouldQuery = new ShouldQueryStrategy();
32    }
33
34    public static final Corpus CORPUS_1 = new MockCorpus(MockSource.SOURCE_1) {
35        @Override
36        public int getQueryThreshold() {
37            return 3;
38        }
39    };
40
41    public static final Corpus CORPUS_2 = new MockCorpus(MockSource.SOURCE_2) {
42        @Override
43        public boolean queryAfterZeroResults() {
44            return true;
45        }
46    };
47
48    public void testRespectsQueryThreshold() {
49        assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "aa"));
50        assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "aaa"));
51        assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, ""));
52    }
53
54    public void testQueriesAfterNoResultsWhenQueryAfterZeroIsTrue() {
55        assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, "query"));
56        mShouldQuery.onZeroResults(CORPUS_2, "query");
57        assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, "query"));
58        assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_2, "query123"));
59    }
60
61    public void testDoesntQueryAfterNoResultsWhenQueryAfterZeroIsFalse() {
62        assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query"));
63        mShouldQuery.onZeroResults(CORPUS_1, "query");
64        // Now we don't query for queries starting with "query"
65        assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query"));
66        assertFalse(mShouldQuery.shouldQueryCorpus(CORPUS_1, "query123"));
67        // But we do query for something shorter
68        assertTrue(mShouldQuery.shouldQueryCorpus(CORPUS_1, "quer"));
69    }
70}
71