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 17 package com.android.quicksearchbox; 18 19 import android.test.AndroidTestCase; 20 import android.test.suitebuilder.annotation.SmallTest; 21 22 import java.util.ArrayList; 23 import java.util.Collections; 24 import java.util.Comparator; 25 import java.util.List; 26 27 /** 28 * Tests for RankAwarePromoter 29 */ 30 @SmallTest 31 public 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)); 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); 84 corpora.addCorpus(mockCorpus); 85 if (i < defaultCount) { 86 corpora.addDefaultCorpus(mockCorpus); 87 } 88 } 89 return corpora; 90 } 91 92 // A corpus ranker that orders corpora lexicographically by name. 93 private class LexicographicalCorpusRanker extends AbstractCorpusRanker { 94 95 public LexicographicalCorpusRanker(Corpora corpora) { 96 super(corpora); 97 } 98 99 @Override 100 public List<Corpus> rankCorpora(Corpora corpora) { 101 ArrayList<Corpus> ordered = new ArrayList<Corpus>(corpora.getEnabledCorpora()); 102 Collections.sort(ordered, new Comparator<Corpus>() { 103 public int compare(Corpus c1, Corpus c2) { 104 return c1.getName().compareTo(c2.getName()); 105 } 106 }); 107 return ordered; 108 } 109 110 } 111 } 112