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 package com.android.quicksearchbox; 17 18 import static com.android.quicksearchbox.SuggestionCursorUtil.assertSameSuggestions; 19 import static com.android.quicksearchbox.SuggestionCursorUtil.slice; 20 21 import android.test.AndroidTestCase; 22 import android.test.suitebuilder.annotation.MediumTest; 23 24 /** 25 * Tests for {@link ShortcutPromoter}. 26 */ 27 @MediumTest 28 public class BlendingPromoterTest extends AndroidTestCase { 29 30 private String mQuery; 31 32 private Suggestion mS11; 33 private Suggestion mS12; 34 private Suggestion mS21; 35 private Suggestion mS22; 36 private Suggestion mWeb1; 37 private Suggestion mWeb2; 38 39 @Override 40 protected void setUp() throws Exception { 41 mQuery = "foo"; 42 mS11 = MockSource.SOURCE_1.createSuggestion(mQuery + "_1_1"); 43 mS12 = MockSource.SOURCE_1.createSuggestion(mQuery + "_1_2"); 44 mS21 = MockSource.SOURCE_2.createSuggestion(mQuery + "_1_1"); 45 mS22 = MockSource.SOURCE_2.createSuggestion(mQuery + "_1_2"); 46 mWeb1 = MockSource.WEB_SOURCE.createSuggestion(mQuery + "_web_1"); 47 mWeb2 = MockSource.WEB_SOURCE.createSuggestion(mQuery + "_web_2"); 48 } 49 50 public void testMaxPromoted() { 51 maxPromotedTest(0); 52 maxPromotedTest(1); 53 maxPromotedTest(2); 54 maxPromotedTest(5); 55 } 56 57 public void testLimitZeroShortcutsPerSource() { 58 SuggestionCursor promoted = limit(cursor(mS11, mS12, mS21, mS22), 0, 0); 59 SuggestionCursor expected = cursor(); 60 assertSameSuggestions(expected, promoted); 61 } 62 63 public void testLimitOneShortcutPerSource() { 64 SuggestionCursor promoted = limit(cursor(mS11, mS12, mS21, mS22), 1, 1); 65 SuggestionCursor expected = cursor(mS11, mS21); 66 assertSameSuggestions(expected, promoted); 67 } 68 69 public void testLimitTwoShortcutsPerSource() { 70 SuggestionCursor promoted = limit(cursor(mS11, mS12, mS21, mS22), 2, 2); 71 SuggestionCursor expected = cursor(mS11, mS12, mS21, mS22); 72 assertSameSuggestions(expected, promoted); 73 } 74 75 public void testLimitThreeShortcutsPerSource() { 76 SuggestionCursor promoted = limit(cursor(mS11, mS12, mS21, mS22), 3, 3); 77 SuggestionCursor expected = cursor(mS11, mS12, mS21, mS22); 78 assertSameSuggestions(expected, promoted); 79 } 80 81 public void testLimitOneSourceZeroPromoted() { 82 SuggestionCursor promoted = limit(cursor(mS11, mS12), 0, 0); 83 SuggestionCursor expected = cursor(); 84 assertSameSuggestions(expected, promoted); 85 } 86 87 public void testLimitOneSourceOnePromoted() { 88 SuggestionCursor promoted = limit(cursor(mS11, mS12), 1, 1); 89 SuggestionCursor expected = cursor(mS11); 90 assertSameSuggestions(expected, promoted); 91 } 92 93 public void testLimitOneSourceTwoPromoted() { 94 SuggestionCursor promoted = limit(cursor(mS11, mS12), 2, 2); 95 SuggestionCursor expected = cursor(mS11, mS12); 96 assertSameSuggestions(expected, promoted); 97 } 98 99 public void testLimitNoShortcuts() { 100 SuggestionCursor promoted = limit(cursor(), 2, 2); 101 SuggestionCursor expected = cursor(); 102 assertSameSuggestions(expected, promoted); 103 } 104 105 public void testLimitZeroWebShortcuts() { 106 SuggestionCursor promoted = limit(cursor(mS11, mS12, mWeb1, mWeb2), 0, 2); 107 SuggestionCursor expected = cursor(mS11, mS12); 108 assertSameSuggestions(expected, promoted); 109 } 110 111 public void testLimitTwoWebShortcuts() { 112 SuggestionCursor promoted = limit(cursor(mS11, mS12, mWeb1, mWeb2), 2, 2); 113 SuggestionCursor expected = cursor(mS11, mS12, mWeb1, mWeb2); 114 assertSameSuggestions(expected, promoted); 115 } 116 117 private void maxPromotedTest(int maxPromoted) { 118 SuggestionCursor shortcuts = cursor(mS11, mS12, mS21, mS22, mWeb1, mWeb2); 119 ListSuggestionCursorNoDuplicates promoted = promote(config(), shortcuts, maxPromoted); 120 int expectedCount = Math.min(maxPromoted, shortcuts.getCount()); 121 assertEquals(expectedCount, promoted.getCount()); 122 int count = Math.min(maxPromoted, shortcuts.getCount()); 123 assertSameSuggestions(slice(promoted, 0, count), slice(shortcuts, 0, count)); 124 } 125 126 private SuggestionCursor limit(SuggestionCursor shortcuts, int maxShortcutsPerWebSource, 127 int maxShortcutsPerNonWebSource) { 128 Config config = config(maxShortcutsPerWebSource, maxShortcutsPerNonWebSource); 129 int maxPromoted = 10; 130 return promote(config, shortcuts, maxPromoted); 131 } 132 133 private ListSuggestionCursorNoDuplicates promote(Config config, SuggestionCursor shortcuts, 134 int maxPromoted) { 135 ShortcutPromoter promoter = new ShortcutPromoter(config, 136 new RankAwarePromoter(config, null, null), null); 137 ListSuggestionCursorNoDuplicates promoted = new ListSuggestionCursorNoDuplicates(mQuery); 138 promoter.promoteShortcuts(shortcuts, maxPromoted, promoted); 139 return promoted; 140 } 141 142 private Config config() { 143 return new Config(getContext()); 144 } 145 146 private Config config(final int maxShortcutsPerWebSource, 147 final int maxShortcutsPerNonWebSource) { 148 return new Config(getContext()) { 149 @Override 150 public int getMaxShortcutsPerWebSource() { 151 return maxShortcutsPerWebSource; 152 } 153 @Override 154 public int getMaxShortcutsPerNonWebSource() { 155 return maxShortcutsPerNonWebSource; 156 } 157 }; 158 } 159 160 private SuggestionCursor cursor(Suggestion... suggestions) { 161 return new ListSuggestionCursor(mQuery, suggestions); 162 } 163 164 } 165