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
18
19import android.test.AndroidTestCase;
20import android.test.MoreAsserts;
21import android.test.suitebuilder.annotation.MediumTest;
22
23import java.util.HashMap;
24import java.util.List;
25import java.util.Map;
26
27/**
28 * Tests for {@link DefaultCorpusRankerTest}.
29 */
30@MediumTest
31public class DefaultCorpusRankerTest extends AndroidTestCase {
32
33    protected MockCorpora mCorpora;
34    protected MockShortcutRepository mRepo;
35    protected DefaultCorpusRanker mRanker;
36
37    @Override
38    protected void setUp() throws Exception {
39        super.setUp();
40
41        mCorpora = new MockCorpora();
42        mCorpora.addCorpus(MockCorpus.CORPUS_1);
43        mCorpora.addCorpus(MockCorpus.CORPUS_2);
44        mCorpora.addCorpus(MockCorpus.WEB_CORPUS);
45        mRepo = new MockShortcutRepository();
46        mRanker = new DefaultCorpusRanker(mCorpora, mRepo);
47    }
48
49    public void testRankNoScores() {
50        mRanker.clear();
51        MoreAsserts.assertContentsInOrder(getRankedCorpora(),
52                MockCorpus.WEB_CORPUS, MockCorpus.CORPUS_1, MockCorpus.CORPUS_2);
53    }
54
55    public void testRankWebLowScore() {
56        setCorpusScores(1, 2, -100);
57        MoreAsserts.assertContentsInOrder(getRankedCorpora(),
58                MockCorpus.WEB_CORPUS, MockCorpus.CORPUS_2, MockCorpus.CORPUS_1);
59    }
60
61    public void testRankSomeScores() {
62        setCorpusScores(null, 1, null);
63        MoreAsserts.assertContentsInOrder(getRankedCorpora(),
64                MockCorpus.WEB_CORPUS, MockCorpus.CORPUS_2, MockCorpus.CORPUS_1);
65    }
66
67    private void setCorpusScores(Integer corpus1, Integer corpus2, Integer webCorpus) {
68        Map<String,Integer> scores = new HashMap<String,Integer>();
69        if (corpus1 != null) scores.put(MockCorpus.CORPUS_1.getName(), corpus1);
70        if (corpus2 != null) scores.put(MockCorpus.CORPUS_2.getName(), corpus2);
71        if (webCorpus != null) scores.put(MockCorpus.WEB_CORPUS.getName(), webCorpus);
72        mRepo.setCorpusScores(scores);
73        mRanker.clear();
74    }
75
76    private List<Corpus> getRankedCorpora() {
77        ConsumerTrap<List<Corpus>> consumer = new ConsumerTrap<List<Corpus>>();
78        mRanker.getCorporaInAll(consumer);
79        return consumer.getValue();
80    }
81
82}
83