1b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert/*
2b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert * Copyright (C) 2010 The Android Open Source Project
3b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert *
4b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
5b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert * you may not use this file except in compliance with the License.
6b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert * You may obtain a copy of the License at
7b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert *
8b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert *      http://www.apache.org/licenses/LICENSE-2.0
9b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert *
10b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert * Unless required by applicable law or agreed to in writing, software
11b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
12b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert * See the License for the specific language governing permissions and
14b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert * limitations under the License.
15b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert */
16b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertpackage com.android.quicksearchbox;
17b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
18b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport static com.android.quicksearchbox.SuggestionCursorUtil.assertSameSuggestions;
19b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport static com.android.quicksearchbox.SuggestionCursorUtil.concat;
20b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport static com.android.quicksearchbox.SuggestionCursorUtil.iterable;
21b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport static com.android.quicksearchbox.SuggestionCursorUtil.slice;
22b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
23b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport android.test.AndroidTestCase;
24b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport android.test.suitebuilder.annotation.MediumTest;
25b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
26b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport java.util.ArrayList;
27b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport java.util.Arrays;
28b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport java.util.List;
29b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
30b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert/**
31b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert * Tests for {@link WebPromoter}.
32b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert */
33b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert@MediumTest
34b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertpublic class WebPromoterTest extends AndroidTestCase {
35b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
36b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private String mQuery;
37b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
38b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private Suggestion mS11;
39b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private Suggestion mS12;
40b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private Suggestion mS21;
41b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private Suggestion mS22;
42b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private Suggestion mWeb1;
43b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private Suggestion mWeb2;
44b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
45b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private CorpusResult mCorpusResult1;
46b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private CorpusResult mCorpusResult2;
47b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private CorpusResult mCorpusResultWeb;
48b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
49b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private SuggestionCursor mShortcuts;
50b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private List<CorpusResult> mCorpusResults;
51b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private Suggestions mSuggestions;
52b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
53b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    @Override
54b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    protected void setUp() throws Exception {
55b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mQuery = "foo";
56b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mS11 = MockSource.SOURCE_1.createSuggestion(mQuery + "_1_1");
57b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mS12 = MockSource.SOURCE_1.createSuggestion(mQuery + "_1_2");
58b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mS21 = MockSource.SOURCE_2.createSuggestion(mQuery + "_1_1");
59b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mS22 = MockSource.SOURCE_2.createSuggestion(mQuery + "_1_2");
6053aab8c4459f45664d04ec882d67094c52b78695Bjorn Bringert        mWeb1 = MockSource.WEB_SOURCE.createSuggestion(mQuery + "_web_1");
6153aab8c4459f45664d04ec882d67094c52b78695Bjorn Bringert        mWeb2 = MockSource.WEB_SOURCE.createSuggestion(mQuery + "_web_2");
62b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mShortcuts = cursor(mS11, mS12, mS21, mS22, mWeb1, mWeb2);
63b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResult1 = MockCorpus.CORPUS_1.getSuggestions(mQuery, 10, false);
64b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResult2 = MockCorpus.CORPUS_2.getSuggestions(mQuery, 10, false);
65b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResultWeb = MockCorpus.WEB_CORPUS.getSuggestions(mQuery, 10, false);
66b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResults = new ArrayList<CorpusResult>();
67b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResults.add(mCorpusResult1);
68b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResults.add(mCorpusResult2);
69b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResults.add(mCorpusResultWeb);
70b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mSuggestions = new Suggestions(mQuery,
71b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert                Arrays.asList(MockCorpus.CORPUS_1, MockCorpus.CORPUS_2, MockCorpus.WEB_CORPUS));
72b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mSuggestions.setShortcuts(new ShortcutCursor(mShortcuts));
73b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mSuggestions.addCorpusResults(mCorpusResults);
74b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
75b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
76b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    public void testZeroShortcuts() {
77b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        SuggestionCursor promoted = promote(mSuggestions, 0, 1);
78b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        SuggestionCursor expected = slice(mCorpusResultWeb, 0, 1);
79b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        // Test sanity check: shouldn't expect any non-web suggestions
80b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        assertOnlyWebSuggestions(expected);
81b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        assertSameSuggestions(expected, promoted);
82b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
83b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
84b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    public void testZeroSuggestions() {
85b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        SuggestionCursor promoted = promote(mSuggestions, 1, 1);
86b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        SuggestionCursor expected = cursor(mWeb1);
87b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        // Test sanity check: shouldn't expect any non-web suggestions
88b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        assertOnlyWebSuggestions(expected);
89b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        assertSameSuggestions(expected, promoted);
90b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
91b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
92b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    public void testOnlyWebPromoted() {
93b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        SuggestionCursor promoted = promote(mSuggestions, 1, 2);
94b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        SuggestionCursor expected = concat(cursor(mWeb1), slice(mCorpusResultWeb, 0, 1));
95b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        // Test sanity check: shouldn't expect any non-web suggestions
96b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        assertOnlyWebSuggestions(expected);
97b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        assertSameSuggestions(expected, promoted);
98b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
99b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
100b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private ListSuggestionCursor promote(Suggestions suggestions, int maxShortcuts,
101b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert            int maxSuggestions) {
102b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        WebPromoter promoter = new WebPromoter(maxShortcuts);
103b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        ListSuggestionCursor promoted = new ListSuggestionCursor(mQuery);
104b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        promoter.pickPromoted(suggestions, maxSuggestions, promoted);
105b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        return promoted;
106b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
107b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
108b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private SuggestionCursor cursor(Suggestion... suggestions) {
109b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        return new ListSuggestionCursor(mQuery, suggestions);
110b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
111b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
112b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private void assertOnlyWebSuggestions(SuggestionCursor expected) {
113b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        for (Suggestion s : iterable(expected)) {
114b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert            assertTrue("Not a web suggestion", s.isWebSearchSuggestion());
115b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        }
116b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
117b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
118b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert}
119