RankAwarePromoterTest.java revision 4572856ac85bb53ea06e65d00beebdf336af9b27
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;
20import android.test.suitebuilder.annotation.SmallTest;
21
22import java.util.ArrayList;
23import java.util.List;
24
25/**
26 * Tests for RankAwarePromoter
27 */
28@SmallTest
29public class RankAwarePromoterTest extends AndroidTestCase {
30    public static final int NUM_SUGGESTIONS_ABOVE_KEYBOARD = 4;
31    public static final int MAX_PROMOTED_CORPORA = 3;
32    public static final int MAX_PROMOTED_SUGGESTIONS = 8;
33    public static final String TEST_QUERY = "query";
34
35    private CorpusRanker mRanker;
36    private RankAwarePromoter mPromoter;
37
38    @Override
39    public void setUp() {
40        Corpora corpora = createMockCorpora(5, MAX_PROMOTED_CORPORA);
41        mRanker = new LexicographicalCorpusRanker(corpora);
42        mPromoter = new RankAwarePromoter(new Config(mContext){
43            @Override
44            public int getNumSuggestionsAboveKeyboard() {
45                return NUM_SUGGESTIONS_ABOVE_KEYBOARD;
46            }
47        });
48    }
49
50    public void testPromotesExpectedSuggestions() {
51        ArrayList<CorpusResult> suggestions = getSuggestions(TEST_QUERY);
52        ListSuggestionCursor promoted = new ListSuggestionCursor(TEST_QUERY);
53        mPromoter.pickPromoted(null, suggestions, MAX_PROMOTED_SUGGESTIONS, promoted);
54
55        assertEquals(MAX_PROMOTED_SUGGESTIONS, promoted.getCount());
56
57        int[] expectedSource = {0, 1, 2, 0, 1, 2, 3, 4};
58        int[] expectedSuggestion = {1, 1, 1, 2, 2, 2, 1, 1};
59
60        for (int i = 0; i < promoted.getCount(); i++) {
61            promoted.moveTo(i);
62            assertEquals("Source in position " + i,
63                    "MockSource Source" + expectedSource[i],
64                    promoted.getSuggestionSource().getLabel());
65            assertEquals("Suggestion in position " + i,
66                    TEST_QUERY + "_" + expectedSuggestion[i],
67                    promoted.getSuggestionText1());
68        }
69    }
70
71    private List<Corpus> getRankedCorpora() {
72        return mRanker.getRankedCorpora();
73    }
74
75    private ArrayList<CorpusResult> getSuggestions(String query) {
76        ArrayList<CorpusResult> suggestions = new ArrayList<CorpusResult>();
77        for (Corpus corpus : getRankedCorpora()) {
78            suggestions.add(corpus.getSuggestions(query, 10, false));
79        }
80        return suggestions;
81    }
82
83    private static MockCorpora createMockCorpora(int count, int defaultCount) {
84        MockCorpora corpora = new MockCorpora();
85        for (int i = 0; i < count; i++) {
86            Source mockSource = new MockSource("Source" + i);
87            Corpus mockCorpus = new MockCorpus(mockSource, i < defaultCount);
88            corpora.addCorpus(mockCorpus);
89        }
90        return corpora;
91    }
92}
93