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