RankAwarePromoterTest.java revision 6859aead3af0680b2c9dc326244aa89835c2c852
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 List<Corpus> mCorpora = createMockCorpora(5, MAX_PROMOTED_CORPORA);
36    private RankAwarePromoter mPromoter;
37
38    @Override
39    public void setUp() {
40        mPromoter = new RankAwarePromoter(new Config(mContext){
41            @Override
42            public int getNumSuggestionsAboveKeyboard() {
43                return NUM_SUGGESTIONS_ABOVE_KEYBOARD;
44            }
45        });
46    }
47
48    public void testPromotesExpectedSuggestions() {
49        List<CorpusResult> suggestions = getSuggestions(TEST_QUERY);
50        ListSuggestionCursor promoted = new ListSuggestionCursor(TEST_QUERY);
51        mPromoter.promoteSuggestions(suggestions, MAX_PROMOTED_SUGGESTIONS, promoted);
52
53        assertEquals(MAX_PROMOTED_SUGGESTIONS, promoted.getCount());
54
55        int[] expectedSource = {0, 1, 2, 0, 1, 2, 3, 4};
56        int[] expectedSuggestion = {1, 1, 1, 2, 2, 2, 1, 1};
57
58        for (int i = 0; i < promoted.getCount(); i++) {
59            promoted.moveTo(i);
60            assertEquals("Source in position " + i,
61                    "MockSource Source" + expectedSource[i],
62                    promoted.getSuggestionSource().getLabel());
63            assertEquals("Suggestion in position " + i,
64                    TEST_QUERY + "_" + expectedSuggestion[i],
65                    promoted.getSuggestionText1());
66        }
67    }
68
69    private List<CorpusResult> getSuggestions(String query) {
70        ArrayList<CorpusResult> results = new ArrayList<CorpusResult>();
71        for (Corpus corpus : mCorpora) {
72            results.add(corpus.getSuggestions(query, 10, false));
73        }
74        return results;
75    }
76
77    private static List<Corpus> createMockCorpora(int count, int defaultCount) {
78        ArrayList<Corpus> corpora = new ArrayList<Corpus>();
79        for (int i = 0; i < count; i++) {
80            Source mockSource = new MockSource("Source" + i);
81            Corpus mockCorpus = new MockCorpus(mockSource, i < defaultCount);
82            corpora.add(mockCorpus);
83        }
84        return corpora;
85    }
86}
87