18b2936607176720172aee068abc5631bdf77e843Bjorn Bringert/*
28b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * Copyright (C) 2010 The Android Open Source Project
38b2936607176720172aee068abc5631bdf77e843Bjorn Bringert *
48b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * Licensed under the Apache License, Version 2.0 (the "License");
58b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * you may not use this file except in compliance with the License.
68b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * You may obtain a copy of the License at
78b2936607176720172aee068abc5631bdf77e843Bjorn Bringert *
88b2936607176720172aee068abc5631bdf77e843Bjorn Bringert *      http://www.apache.org/licenses/LICENSE-2.0
98b2936607176720172aee068abc5631bdf77e843Bjorn Bringert *
108b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * Unless required by applicable law or agreed to in writing, software
118b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * distributed under the License is distributed on an "AS IS" BASIS,
128b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * See the License for the specific language governing permissions and
148b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * limitations under the License.
158b2936607176720172aee068abc5631bdf77e843Bjorn Bringert */
168b2936607176720172aee068abc5631bdf77e843Bjorn Bringertpackage com.android.quicksearchbox;
178b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
188b2936607176720172aee068abc5631bdf77e843Bjorn Bringertimport android.test.AndroidTestCase;
198b2936607176720172aee068abc5631bdf77e843Bjorn Bringertimport android.test.suitebuilder.annotation.SmallTest;
208b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
218b2936607176720172aee068abc5631bdf77e843Bjorn Bringertimport java.util.ArrayList;
228b2936607176720172aee068abc5631bdf77e843Bjorn Bringertimport java.util.Arrays;
238b2936607176720172aee068abc5631bdf77e843Bjorn Bringertimport java.util.List;
248b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
258b2936607176720172aee068abc5631bdf77e843Bjorn Bringert/**
268b2936607176720172aee068abc5631bdf77e843Bjorn Bringert * Tests for SingleCorpusPromoter.
278b2936607176720172aee068abc5631bdf77e843Bjorn Bringert */
288b2936607176720172aee068abc5631bdf77e843Bjorn Bringert@SmallTest
298b2936607176720172aee068abc5631bdf77e843Bjorn Bringertpublic class SingleCorpusPromoterTest extends AndroidTestCase {
308b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    public static final String TEST_QUERY = "query";
318b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
328b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    private List<Corpus> mCorpora = Arrays.asList(MockCorpus.CORPUS_1, MockCorpus.CORPUS_2);
338b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    private SingleCorpusPromoter mPromoter;
348b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
358b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    @Override
368b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    public void setUp() {
378b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        mPromoter = new SingleCorpusPromoter(MockCorpus.CORPUS_1, 10);
388b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    }
398b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
408b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    public void testPromotesOnlyGivenCorpus() {
418b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        Suggestions suggestions = makeSuggestions(TEST_QUERY);
428b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
438b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        ListSuggestionCursor promoted = new ListSuggestionCursor(TEST_QUERY);
448b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        mPromoter.pickPromoted(suggestions, 4, promoted);
458b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
468b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        assertEquals(2, promoted.getCount());
478b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
488b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        Source[] expectedSource = { MockSource.SOURCE_1, MockSource.SOURCE_1 };
498b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
508b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        for (int i = 0; i < promoted.getCount(); i++) {
518b2936607176720172aee068abc5631bdf77e843Bjorn Bringert            promoted.moveTo(i);
528b2936607176720172aee068abc5631bdf77e843Bjorn Bringert            assertEquals("Source in position " + i,
538b2936607176720172aee068abc5631bdf77e843Bjorn Bringert                    expectedSource[i], promoted.getSuggestionSource());
548b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        }
558b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    }
568b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
578b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    private Suggestions makeSuggestions(String query) {
588b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        Suggestions suggestions = new Suggestions(query, mCorpora);
598b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        ArrayList<CorpusResult> results = new ArrayList<CorpusResult>();
608b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        for (Corpus corpus : mCorpora) {
618b2936607176720172aee068abc5631bdf77e843Bjorn Bringert            results.add(corpus.getSuggestions(query, 10, false));
628b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        }
638b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        suggestions.addCorpusResults(results);
648b2936607176720172aee068abc5631bdf77e843Bjorn Bringert        return suggestions;
658b2936607176720172aee068abc5631bdf77e843Bjorn Bringert    }
668b2936607176720172aee068abc5631bdf77e843Bjorn Bringert
678b2936607176720172aee068abc5631bdf77e843Bjorn Bringert}
68