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.assertSameSuggestionsNoOrder;
20b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport static com.android.quicksearchbox.SuggestionCursorUtil.concat;
21b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
22b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport android.test.AndroidTestCase;
23b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport android.test.suitebuilder.annotation.MediumTest;
24b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
25b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport java.util.ArrayList;
26b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertimport java.util.List;
27b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
28b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert/**
29b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert * Tests for {@link ResultPromoter}.
30b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert */
31b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert@MediumTest
32b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringertpublic class ResultPromoterTest extends AndroidTestCase {
33b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
34b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private static final int MAX_PROMOTED_SUGGESTIONS = 10;
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
52b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    @Override
53b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    protected void setUp() throws Exception {
54b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mQuery = "foo";
55b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mS11 = MockSource.SOURCE_1.createSuggestion(mQuery + "_1_1");
56b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mS12 = MockSource.SOURCE_1.createSuggestion(mQuery + "_1_2");
57b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mS21 = MockSource.SOURCE_2.createSuggestion(mQuery + "_1_1");
58b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mS22 = MockSource.SOURCE_2.createSuggestion(mQuery + "_1_2");
5953aab8c4459f45664d04ec882d67094c52b78695Bjorn Bringert        mWeb1 = MockSource.WEB_SOURCE.createSuggestion(mQuery + "_web_1");
6053aab8c4459f45664d04ec882d67094c52b78695Bjorn Bringert        mWeb2 = MockSource.WEB_SOURCE.createSuggestion(mQuery + "_web_2");
61b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mShortcuts = cursor(mS11, mS12, mS21, mS22, mWeb1, mWeb2);
62b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResult1 = MockCorpus.CORPUS_1.getSuggestions(mQuery, 10, false);
63b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResult2 = MockCorpus.CORPUS_2.getSuggestions(mQuery, 10, false);
64b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResultWeb = MockCorpus.WEB_CORPUS.getSuggestions(mQuery, 10, false);
65b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResults = new ArrayList<CorpusResult>();
66b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResults.add(mCorpusResult1);
67b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResults.add(mCorpusResult2);
68b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        mCorpusResults.add(mCorpusResultWeb);
69b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
70b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
71b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    public void testOnlyResultShortcutsPromoted() {
72b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        SuggestionCursor promoted = promoteShortcuts(mShortcuts);
73b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        SuggestionCursor expected = cursor(mS11, mS12, mS21, mS22);
74b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        assertSameSuggestions(expected, promoted);
75b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
76b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
77b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    public void testOnlyResultSuggestionsPromoted() {
78b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        SuggestionCursor promoted = promoteSuggestions(mCorpusResults);
79b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        SuggestionCursor expected = concat(mCorpusResult1, mCorpusResult2);
80b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        assertSameSuggestionsNoOrder(expected, promoted);
81b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
82b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
83af1ca2cc65a2c2fdf6f396126e235d64e4da0936Mathew Inwood    private ShortcutPromoter createResultPromoter() {
84af1ca2cc65a2c2fdf6f396126e235d64e4da0936Mathew Inwood        ResultFilter results = new ResultFilter();
8527691bfcdcf3d2918b45bfadd57b08547c317ce5Mathew Inwood        return new ShortcutPromoter(config(),
8627691bfcdcf3d2918b45bfadd57b08547c317ce5Mathew Inwood                new RankAwarePromoter(config(), results, null), results);
87af1ca2cc65a2c2fdf6f396126e235d64e4da0936Mathew Inwood    }
88af1ca2cc65a2c2fdf6f396126e235d64e4da0936Mathew Inwood
89b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private ListSuggestionCursor promoteShortcuts(SuggestionCursor shortcuts) {
90af1ca2cc65a2c2fdf6f396126e235d64e4da0936Mathew Inwood        ShortcutPromoter promoter = createResultPromoter();
91b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        ListSuggestionCursor promoted = new ListSuggestionCursor(mQuery);
92b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        promoter.promoteShortcuts(shortcuts, MAX_PROMOTED_SUGGESTIONS, promoted);
93b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        return promoted;
94b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
95b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
96b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private ListSuggestionCursor promoteSuggestions(List<CorpusResult> suggestions) {
97af1ca2cc65a2c2fdf6f396126e235d64e4da0936Mathew Inwood        ResultFilter results = new ResultFilter();
9827691bfcdcf3d2918b45bfadd57b08547c317ce5Mathew Inwood        RankAwarePromoter promoter = new RankAwarePromoter(config(), results, null);
99b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        ListSuggestionCursor promoted = new ListSuggestionCursor(mQuery);
100b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        promoter.promoteSuggestions(suggestions, MAX_PROMOTED_SUGGESTIONS, promoted);
101b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        return promoted;
102b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
103b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
104b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private Config config() {
105b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        return new Config(getContext());
106b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
107b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
108b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    private SuggestionCursor cursor(Suggestion... suggestions) {
109b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert        return new ListSuggestionCursor(mQuery, suggestions);
110b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert    }
111b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert
112b83882b9efa37ec0f20a0f1c85cf5ccc93194aeeBjorn Bringert}
113