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 com.android.quicksearchbox.util.MockNamedTaskExecutor; 20 21 import android.os.Handler; 22 import android.test.AndroidTestCase; 23 import android.test.suitebuilder.annotation.MediumTest; 24 25 import java.util.ArrayList; 26 import java.util.Arrays; 27 import java.util.Collections; 28 import java.util.List; 29 30 /** 31 * Tests for {@link SuggestionsProviderImpl}. 32 */ 33 @MediumTest 34 public class SuggestionsProviderImplTest extends AndroidTestCase { 35 36 private List<Corpus> mCorpora; 37 private MockNamedTaskExecutor mTaskExecutor; 38 private SuggestionsProviderImpl mProvider; 39 private MockShortcutRepository mShortcutRepo; 40 41 @Override setUp()42 protected void setUp() throws Exception { 43 Config config = new Config(getContext()); 44 mTaskExecutor = new MockNamedTaskExecutor(); 45 Handler publishThread = new MockHandler(); 46 mShortcutRepo = new MockShortcutRepository(); 47 mCorpora = new ArrayList<Corpus>(); 48 mCorpora.add(MockCorpus.CORPUS_1); 49 mCorpora.add(MockCorpus.CORPUS_2); 50 Logger logger = new MockLogger(); 51 mProvider = new SuggestionsProviderImpl(config, 52 mTaskExecutor, 53 publishThread, 54 logger); 55 } 56 testSingleCorpus()57 public void testSingleCorpus() { 58 Suggestions suggestions = mProvider.getSuggestions("foo", 59 Collections.singletonList(MockCorpus.CORPUS_1)); 60 suggestions.setShortcuts(mShortcutRepo.getShortcutsForQuery( 61 "foo", mCorpora)); 62 try { 63 assertEquals(1, suggestions.getExpectedResultCount()); 64 assertEquals(0, suggestions.getResultCount()); 65 assertEquals(0, promote(suggestions).getCount()); 66 assertTrue(mTaskExecutor.runNext()); 67 assertEquals(1, suggestions.getExpectedResultCount()); 68 assertEquals(1, suggestions.getResultCount()); 69 assertEquals(MockCorpus.CORPUS_1.getSuggestions("foo", 3, true).getCount(), 70 promote(suggestions).getCount()); 71 mTaskExecutor.assertDone(); 72 } finally { 73 if (suggestions != null) suggestions.release(); 74 } 75 } 76 testMultipleCorpora()77 public void testMultipleCorpora() { 78 Suggestions suggestions = mProvider.getSuggestions("foo", 79 Arrays.asList(MockCorpus.CORPUS_1, MockCorpus.CORPUS_2)); 80 suggestions.setShortcuts(mShortcutRepo.getShortcutsForQuery( 81 "foo", mCorpora)); 82 try { 83 int corpus1Count = MockCorpus.CORPUS_1.getSuggestions("foo", 3, true).getCount(); 84 int corpus2Count = MockCorpus.CORPUS_2.getSuggestions("foo", 3, true).getCount(); 85 assertEquals(mCorpora.size(), suggestions.getExpectedResultCount()); 86 assertEquals(0, suggestions.getResultCount()); 87 assertEquals(0, promote(suggestions).getCount()); 88 assertTrue(mTaskExecutor.runNext()); 89 assertEquals(1, suggestions.getResultCount()); 90 assertEquals("Incorrect promoted: " + promote(suggestions), 91 corpus1Count, promote(suggestions).getCount()); 92 assertTrue(mTaskExecutor.runNext()); 93 assertEquals(2, suggestions.getResultCount()); 94 assertEquals("Incorrect promoted: " + promote(suggestions), 95 corpus1Count + corpus2Count, promote(suggestions).getCount()); 96 mTaskExecutor.assertDone(); 97 } finally { 98 if (suggestions != null) suggestions.release(); 99 } 100 } 101 promote(Suggestions suggestions)102 private SuggestionCursor promote(Suggestions suggestions) { 103 return suggestions.getPromoted(new ConcatPromoter(), 10); 104 } 105 106 private static class ConcatPromoter implements Promoter { pickPromoted(Suggestions suggestions, int maxPromoted, ListSuggestionCursor promoted)107 public void pickPromoted(Suggestions suggestions, int maxPromoted, 108 ListSuggestionCursor promoted) { 109 // Add suggestions 110 for (SuggestionCursor c : suggestions.getCorpusResults()) { 111 for (int i = 0; i < c.getCount(); i++) { 112 if (promoted.getCount() >= maxPromoted) { 113 return; 114 } 115 promoted.add(new SuggestionPosition(c, i)); 116 } 117 } 118 } 119 } 120 121 } 122