BlendingPromoterTest.java revision b83882b9efa37ec0f20a0f1c85cf5ccc93194aee
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 */
16package com.android.quicksearchbox;
17
18import static com.android.quicksearchbox.SuggestionCursorUtil.assertSameSuggestions;
19import static com.android.quicksearchbox.SuggestionCursorUtil.slice;
20
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.MediumTest;
23
24/**
25 * Tests for {@link BlendingPromoter}.
26 */
27@MediumTest
28public 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        ListSuggestionCursor 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 ListSuggestionCursor promote(Config config, SuggestionCursor shortcuts,
134            int maxPromoted) {
135        BlendingPromoter promoter = new BlendingPromoter(config);
136        ListSuggestionCursor promoted = new ListSuggestionCursor(mQuery);
137        promoter.promoteShortcuts(shortcuts, maxPromoted, promoted);
138        return promoted;
139    }
140
141    private Config config() {
142        return new Config(getContext());
143    }
144
145    private Config config(final int maxShortcutsPerWebSource,
146            final int maxShortcutsPerNonWebSource) {
147        return new Config(getContext()) {
148            @Override
149            public int getMaxShortcutsPerWebSource() {
150                return maxShortcutsPerWebSource;
151            }
152            @Override
153            public int getMaxShortcutsPerNonWebSource() {
154                return maxShortcutsPerNonWebSource;
155            }
156        };
157    }
158
159    private SuggestionCursor cursor(Suggestion... suggestions) {
160        return new ListSuggestionCursor(mQuery, suggestions);
161    }
162
163}
164