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