MultiSourceCorpusTest.java revision 9a3f0f73437fad172a510e85064fc300523476ec
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 */
16
17package com.android.quicksearchbox;
18
19import android.content.Context;
20import android.content.Intent;
21import android.graphics.drawable.Drawable;
22import android.net.Uri;
23import android.os.Bundle;
24import android.test.AndroidTestCase;
25import android.test.MoreAsserts;
26import android.test.suitebuilder.annotation.LargeTest;
27
28import java.util.Collection;
29import java.util.concurrent.Executor;
30import java.util.concurrent.Executors;
31
32/**
33 * Tests for {@link MultiSourceCorpus}.
34 */
35@LargeTest
36public class MultiSourceCorpusTest extends AndroidTestCase {
37
38    protected MultiSourceCorpus mCorpus;
39
40    @Override
41    protected void setUp() throws Exception {
42        super.setUp();
43
44        // Using a single thread to make the test deterministic
45        Executor executor = Executors.newSingleThreadExecutor();
46        mCorpus = new SkeletonMultiSourceCorpus(getContext(), executor,
47                MockSource.SOURCE_1, MockSource.SOURCE_2);
48    }
49
50    public void testGetSources() {
51        MoreAsserts.assertContentsInOrder(mCorpus.getSources(),
52                MockSource.SOURCE_1, MockSource.SOURCE_2);
53    }
54
55    public void testGetSuggestions() {
56        ListSuggestionCursor expected = concatSuggestionCursors("foo",
57                MockSource.SOURCE_1.getSuggestions("foo", 50),
58                MockSource.SOURCE_2.getSuggestions("foo", 50));
59        CorpusResult observed = mCorpus.getSuggestions("foo", 50);
60        SuggestionCursorUtil.assertSameSuggestions(expected, observed);
61    }
62
63    private static ListSuggestionCursor concatSuggestionCursors(String query,
64            SuggestionCursor... cursors) {
65        ListSuggestionCursor out = new ListSuggestionCursor("foo");
66        for (SuggestionCursor cursor : cursors) {
67            int count = cursor.getCount();
68            for (int i = 0; i < count; i++) {
69                out.add(new SuggestionPosition(cursor, i));
70            }
71        }
72        return out;
73    }
74
75    private static class SkeletonMultiSourceCorpus extends MultiSourceCorpus {
76
77        public SkeletonMultiSourceCorpus(Context context, Executor executor, Source... sources) {
78            super(context, executor, sources);
79        }
80
81        public Intent createSearchIntent(String query, Bundle appData) {
82            return null;
83        }
84
85        public SuggestionData createSearchShortcut(String query) {
86            return null;
87        }
88
89        public Intent createVoiceSearchIntent(Bundle appData) {
90            return null;
91        }
92
93        public Drawable getCorpusIcon() {
94            return null;
95        }
96
97        public Uri getCorpusIconUri() {
98            return null;
99        }
100
101        public CharSequence getHint() {
102            return null;
103        }
104
105        public CharSequence getLabel() {
106            return null;
107        }
108
109        public int getQueryThreshold() {
110            return 0;
111        }
112
113        public CharSequence getSettingsDescription() {
114            return null;
115        }
116
117        public boolean isWebCorpus() {
118            return false;
119        }
120
121        public boolean queryAfterZeroResults() {
122            return false;
123        }
124
125        public boolean voiceSearchEnabled() {
126            return false;
127        }
128
129        public String getName() {
130            return "SkeletonMultiSourceCorpus";
131        }
132
133    }
134}
135