Home | History | Annotate | Download | only in quicksearchbox
      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 
     17 package com.android.quicksearchbox;
     18 
     19 import com.android.quicksearchbox.util.MockDataSetObserver;
     20 
     21 import android.test.AndroidTestCase;
     22 import android.test.suitebuilder.annotation.SmallTest;
     23 
     24 import java.util.Arrays;
     25 import java.util.Collections;
     26 import java.util.List;
     27 import java.util.Set;
     28 
     29 /**
     30  * Tests for {@link Suggestions}.
     31  *
     32  */
     33 @SmallTest
     34 public class SuggestionsTest extends AndroidTestCase {
     35 
     36     private Suggestions mSuggestions;
     37     private MockDataSetObserver mObserver;
     38     private List<Corpus> mExpectedCorpora;
     39 
     40     @Override
     41     protected void setUp() throws Exception {
     42         mExpectedCorpora = Arrays.asList(MockCorpus.CORPUS_1, MockCorpus.WEB_CORPUS);
     43         mSuggestions = new Suggestions("foo", mExpectedCorpora);
     44         mObserver = new MockDataSetObserver();
     45         mSuggestions.registerDataSetObserver(mObserver);
     46     }
     47 
     48     @Override
     49     protected void tearDown() throws Exception {
     50         mSuggestions.release();
     51         mSuggestions = null;
     52     }
     53 
     54     public void testGetExpectedResultCount() {
     55         assertEquals(mExpectedCorpora.size(), mSuggestions.getExpectedResultCount());
     56     }
     57 
     58     public void testGetExpectedCorpora() {
     59         List<Corpus> expectedCorpora = mSuggestions.getExpectedCorpora();
     60         assertEquals(mExpectedCorpora.size(), expectedCorpora.size());
     61         for (int i=0; i<mExpectedCorpora.size(); ++i) {
     62             assertEquals(mExpectedCorpora.get(i), expectedCorpora.get(i));
     63         }
     64     }
     65 
     66     public void testExpectsCorpus() {
     67         for (int i=0; i<mExpectedCorpora.size(); ++i) {
     68             assertTrue(mSuggestions.expectsCorpus(mExpectedCorpora.get(i)));
     69         }
     70         assertFalse(mSuggestions.expectsCorpus(MockCorpus.CORPUS_2));
     71     }
     72 
     73     public void testGetUserQuery() {
     74         assertEquals("foo", mSuggestions.getQuery());
     75     }
     76 
     77     public void testGetIncludedCorpora() {
     78         Corpus corpus = MockCorpus.CORPUS_1;
     79         mSuggestions.addCorpusResults(
     80                 Collections.singletonList(corpus.getSuggestions("foo", 50, true)));
     81         Set<Corpus> includedCorpora = mSuggestions.getIncludedCorpora();
     82         assertEquals(includedCorpora.size(), 1);
     83         assertTrue(includedCorpora.contains(corpus));
     84     }
     85 
     86     public void testObserverNotified() {
     87         Corpus corpus = MockCorpus.CORPUS_1;
     88         mObserver.assertNotChanged();
     89         mObserver.assertNotInvalidated();
     90         mSuggestions.addCorpusResults(
     91                 Collections.singletonList(corpus.getSuggestions("foo", 50, true)));
     92         mObserver.assertChanged();
     93         mObserver.assertNotInvalidated();
     94     }
     95 
     96 }
     97