1/*
2 * Copyright (C) 2009 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 */
16
17package com.android.quicksearchbox;
18
19import android.test.AndroidTestCase;
20import android.test.MoreAsserts;
21import android.test.suitebuilder.annotation.MediumTest;
22
23import java.util.Arrays;
24import java.util.Collection;
25
26/**
27 * Tests for {@link SearchableCorpora}.
28 *
29 */
30@MediumTest
31public class SearchableCorporaTest extends AndroidTestCase {
32
33    protected SearchSettings mSettings;
34
35    protected SearchableCorpora mCorpora;
36
37    private static final MockCorpus CORPUS_1 = new MockCorpus(MockSource.SOURCE_1);
38    private static final MockCorpus CORPUS_DISABLED = new MockCorpus(MockSource.SOURCE_2);
39    private static final MockCorpus CORPUS_NOT_IN_ALL = new MockCorpus(MockSource.SOURCE_3) {
40        @Override
41        public boolean includeInAll() {
42            return false;
43        }
44    };
45    private static final MockCorpus CORPUS_WEB = new MockCorpus(MockSource.WEB_SOURCE);
46
47    @Override
48    protected void setUp() throws Exception {
49        super.setUp();
50
51        mSettings = new MockSearchSettings() {
52            @Override
53            public boolean isCorpusEnabled(Corpus corpus) {
54                return !CORPUS_DISABLED.equals(corpus);
55            }
56        };
57        MockSources sources = new MockSources();
58        sources.addSource(MockSource.SOURCE_1);
59        sources.addSource(MockSource.SOURCE_2);
60        sources.addSource(MockSource.SOURCE_3);
61        sources.addSource(MockSource.WEB_SOURCE);
62        mCorpora = new SearchableCorpora(mContext, mSettings, sources, new MockCorpusFactory());
63        mCorpora.update();
64    }
65
66    public void testGetAllCorpora() {
67        MoreAsserts.assertContentsInAnyOrder(mCorpora.getAllCorpora(),
68                CORPUS_1, CORPUS_DISABLED, CORPUS_NOT_IN_ALL, CORPUS_WEB);
69    }
70
71    public void testEnabledCorpora() {
72        MoreAsserts.assertContentsInAnyOrder(mCorpora.getEnabledCorpora(),
73                CORPUS_1, CORPUS_NOT_IN_ALL, CORPUS_WEB);
74    }
75
76    public void testCorporaIncludedInAll() {
77        MoreAsserts.assertContentsInAnyOrder(mCorpora.getCorporaInAll(),
78                CORPUS_1, CORPUS_WEB);
79    }
80
81    public void testGetWebCorpus() {
82        assertEquals(CORPUS_WEB, mCorpora.getWebCorpus());
83    }
84
85    public void testGetCorpusForSource() {
86        assertEquals(CORPUS_1, mCorpora.getCorpusForSource(MockSource.SOURCE_1));
87        assertNull(mCorpora.getCorpusForSource(new MockSource("foo")));
88    }
89
90    public void testGetCorpus() {
91        assertEquals(CORPUS_WEB, mCorpora.getCorpus(CORPUS_WEB.getName()));
92    }
93
94    /**
95     * Mock implementation of {@link CorpusFactory}.
96     */
97    private static class MockCorpusFactory implements CorpusFactory {
98        public Collection<Corpus> createCorpora(Sources sources) {
99            return Arrays.<Corpus>asList(CORPUS_1, CORPUS_DISABLED, CORPUS_NOT_IN_ALL, CORPUS_WEB);
100        }
101    }
102
103}
104