RankAwarePromoterTest.java revision a65408ff321345557f93effa41395174640870eb
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 com.android.quicksearchbox.util.Util; 20 21import android.test.AndroidTestCase; 22import android.test.suitebuilder.annotation.SmallTest; 23 24import java.util.ArrayList; 25import java.util.Collections; 26import java.util.Comparator; 27import java.util.List; 28import java.util.Set; 29 30/** 31 * Tests for RankAwarePromoter 32 */ 33@SmallTest 34public class RankAwarePromoterTest extends AndroidTestCase { 35 public static final int MAX_PROMOTED_CORPORA = 3; 36 public static final int MAX_PROMOTED_SUGGESTIONS = 8; 37 public static final String TEST_QUERY = "query"; 38 39 private CorpusRanker mRanker; 40 private RankAwarePromoter mPromoter; 41 42 @Override 43 public void setUp() { 44 Corpora corpora = createMockCorpora(5); 45 mRanker = new LexicographicalCorpusRanker(corpora); 46 mPromoter = new RankAwarePromoter(); 47 } 48 49 public void testPromotesExpectedSuggestions() { 50 ArrayList<CorpusResult> suggestions = getSuggestions(TEST_QUERY); 51 ListSuggestionCursor promoted = new ListSuggestionCursor(TEST_QUERY); 52 List<Corpus> rankedCorpora = getRankedCorpora(); 53 Set<Corpus> promotedCorpora = Util.setOfFirstN(rankedCorpora, MAX_PROMOTED_CORPORA); 54 mPromoter.pickPromoted(null, suggestions, MAX_PROMOTED_SUGGESTIONS, promoted, 55 promotedCorpora); 56 57 assertEquals(MAX_PROMOTED_SUGGESTIONS, promoted.getCount()); 58 59 int[] expectedSource = {0, 0, 1, 1, 2, 2, 3, 4}; 60 int[] expectedSuggestion = {1, 2, 1, 2, 1, 2, 1, 1}; 61 62 for (int i = 0; i < promoted.getCount(); i++) { 63 promoted.moveTo(i); 64 assertEquals("Source in position " + i, 65 "MockSource Source" + expectedSource[i], 66 promoted.getSuggestionSource().getLabel()); 67 assertEquals("Suggestion in position " + i, 68 TEST_QUERY + "_" + expectedSuggestion[i], 69 promoted.getSuggestionText1()); 70 } 71 } 72 73 private List<Corpus> getRankedCorpora() { 74 return mRanker.getRankedCorpora(); 75 } 76 77 private ArrayList<CorpusResult> getSuggestions(String query) { 78 ArrayList<CorpusResult> suggestions = new ArrayList<CorpusResult>(); 79 for (Corpus corpus : getRankedCorpora()) { 80 suggestions.add(corpus.getSuggestions(query, 10)); 81 } 82 return suggestions; 83 } 84 85 private static MockCorpora createMockCorpora(int count) { 86 MockCorpora corpora = new MockCorpora(); 87 for (int i = 0; i < count; i++) { 88 Source mockSource = new MockSource("Source" + i); 89 corpora.addCorpus(new MockCorpus(mockSource), mockSource); 90 } 91 return corpora; 92 } 93 94 // A corpus ranker that orders corpora lexicographically by name. 95 private class LexicographicalCorpusRanker extends AbstractCorpusRanker { 96 97 public LexicographicalCorpusRanker(Corpora corpora) { 98 super(corpora); 99 } 100 101 @Override 102 public List<Corpus> rankCorpora(Corpora corpora) { 103 ArrayList<Corpus> ordered = new ArrayList<Corpus>(corpora.getEnabledCorpora()); 104 Collections.sort(ordered, new Comparator<Corpus>() { 105 public int compare(Corpus c1, Corpus c2) { 106 return c1.getName().compareTo(c2.getName()); 107 } 108 }); 109 return ordered; 110 } 111 112 } 113} 114