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.concat;
20import static com.android.quicksearchbox.SuggestionCursorUtil.iterable;
21import static com.android.quicksearchbox.SuggestionCursorUtil.slice;
22
23import android.test.AndroidTestCase;
24import android.test.suitebuilder.annotation.MediumTest;
25
26import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.List;
29
30/**
31 * Tests for {@link WebPromoter}.
32 */
33@MediumTest
34public class WebPromoterTest extends AndroidTestCase {
35
36    private String mQuery;
37
38    private Suggestion mS11;
39    private Suggestion mS12;
40    private Suggestion mS21;
41    private Suggestion mS22;
42    private Suggestion mWeb1;
43    private Suggestion mWeb2;
44
45    private CorpusResult mCorpusResult1;
46    private CorpusResult mCorpusResult2;
47    private CorpusResult mCorpusResultWeb;
48
49    private SuggestionCursor mShortcuts;
50    private List<CorpusResult> mCorpusResults;
51    private Suggestions mSuggestions;
52
53    @Override
54    protected void setUp() throws Exception {
55        mQuery = "foo";
56        mS11 = MockSource.SOURCE_1.createSuggestion(mQuery + "_1_1");
57        mS12 = MockSource.SOURCE_1.createSuggestion(mQuery + "_1_2");
58        mS21 = MockSource.SOURCE_2.createSuggestion(mQuery + "_1_1");
59        mS22 = MockSource.SOURCE_2.createSuggestion(mQuery + "_1_2");
60        mWeb1 = MockSource.WEB_SOURCE.createSuggestion(mQuery + "_web_1");
61        mWeb2 = MockSource.WEB_SOURCE.createSuggestion(mQuery + "_web_2");
62        mShortcuts = cursor(mS11, mS12, mS21, mS22, mWeb1, mWeb2);
63        mCorpusResult1 = MockCorpus.CORPUS_1.getSuggestions(mQuery, 10, false);
64        mCorpusResult2 = MockCorpus.CORPUS_2.getSuggestions(mQuery, 10, false);
65        mCorpusResultWeb = MockCorpus.WEB_CORPUS.getSuggestions(mQuery, 10, false);
66        mCorpusResults = new ArrayList<CorpusResult>();
67        mCorpusResults.add(mCorpusResult1);
68        mCorpusResults.add(mCorpusResult2);
69        mCorpusResults.add(mCorpusResultWeb);
70        mSuggestions = new Suggestions(mQuery,
71                Arrays.asList(MockCorpus.CORPUS_1, MockCorpus.CORPUS_2, MockCorpus.WEB_CORPUS));
72        mSuggestions.setShortcuts(new ShortcutCursor(mShortcuts));
73        mSuggestions.addCorpusResults(mCorpusResults);
74    }
75
76    public void testZeroShortcuts() {
77        SuggestionCursor promoted = promote(mSuggestions, 0, 1);
78        SuggestionCursor expected = slice(mCorpusResultWeb, 0, 1);
79        // Test sanity check: shouldn't expect any non-web suggestions
80        assertOnlyWebSuggestions(expected);
81        assertSameSuggestions(expected, promoted);
82    }
83
84    public void testZeroSuggestions() {
85        SuggestionCursor promoted = promote(mSuggestions, 1, 1);
86        SuggestionCursor expected = cursor(mWeb1);
87        // Test sanity check: shouldn't expect any non-web suggestions
88        assertOnlyWebSuggestions(expected);
89        assertSameSuggestions(expected, promoted);
90    }
91
92    public void testOnlyWebPromoted() {
93        SuggestionCursor promoted = promote(mSuggestions, 1, 2);
94        SuggestionCursor expected = concat(cursor(mWeb1), slice(mCorpusResultWeb, 0, 1));
95        // Test sanity check: shouldn't expect any non-web suggestions
96        assertOnlyWebSuggestions(expected);
97        assertSameSuggestions(expected, promoted);
98    }
99
100    private ListSuggestionCursor promote(Suggestions suggestions, int maxShortcuts,
101            int maxSuggestions) {
102        WebPromoter promoter = new WebPromoter(maxShortcuts);
103        ListSuggestionCursor promoted = new ListSuggestionCursor(mQuery);
104        promoter.pickPromoted(suggestions, maxSuggestions, promoted);
105        return promoted;
106    }
107
108    private SuggestionCursor cursor(Suggestion... suggestions) {
109        return new ListSuggestionCursor(mQuery, suggestions);
110    }
111
112    private void assertOnlyWebSuggestions(SuggestionCursor expected) {
113        for (Suggestion s : iterable(expected)) {
114            assertTrue("Not a web suggestion", s.isWebSearchSuggestion());
115        }
116    }
117
118}
119