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 package com.android.quicksearchbox;
     17 
     18 import static com.android.quicksearchbox.SuggestionCursorUtil.assertSameSuggestion;
     19 import static com.android.quicksearchbox.SuggestionCursorUtil.assertSameSuggestions;
     20 import static com.android.quicksearchbox.SuggestionCursorUtil.slice;
     21 
     22 import android.test.AndroidTestCase;
     23 import android.test.suitebuilder.annotation.MediumTest;
     24 
     25 import java.util.ArrayList;
     26 import java.util.Arrays;
     27 import java.util.List;
     28 
     29 /**
     30  * Tests for {@link ShortcutPromoter}.
     31  */
     32 @MediumTest
     33 public class ShortcutPromoterTest extends AndroidTestCase {
     34 
     35     private String mQuery;
     36 
     37     private SuggestionCursor mShortcuts;
     38 
     39     private ArrayList<CorpusResult> mSuggestions;
     40 
     41     private int mSuggestionCount;
     42 
     43     @Override
     44     protected void setUp() throws Exception {
     45         mQuery = "foo";
     46         List<Corpus> corpora = Arrays.asList(MockCorpus.CORPUS_1, MockCorpus.CORPUS_2);
     47         mShortcuts = new MockShortcutRepository().getShortcutsForQuery(mQuery, null);
     48         mSuggestions = new ArrayList<CorpusResult>();
     49         for (Corpus corpus : corpora) {
     50             mSuggestions.add(corpus.getSuggestions(mQuery, 10, false));
     51         }
     52         mSuggestionCount = countSuggestions(mSuggestions);
     53     }
     54 
     55     @Override
     56     protected void tearDown() throws Exception {
     57         mQuery = null;
     58         mShortcuts.close();
     59         for (SuggestionCursor c : mSuggestions) {
     60             c.close();
     61         }
     62         mSuggestions = null;
     63     }
     64 
     65     public void testPickPromotedNoNext() {
     66         maxPromotedTestNoNext(0);
     67         maxPromotedTestNoNext(1);
     68         maxPromotedTestNoNext(2);
     69         maxPromotedTestNoNext(5);
     70     }
     71 
     72     public void testPickPromotedConcatNext() {
     73         maxPromotedTestConcatNext(0);
     74         maxPromotedTestConcatNext(1);
     75         maxPromotedTestConcatNext(2);
     76         maxPromotedTestConcatNext(6);
     77         maxPromotedTestConcatNext(7);
     78     }
     79 
     80     private void maxPromotedTestNoNext(int maxPromoted) {
     81         Promoter promoter = new ShortcutPromoter(null);
     82         int expectedCount = Math.min(maxPromoted, mShortcuts.getCount());
     83         ListSuggestionCursor promoted = new ListSuggestionCursor(mQuery);
     84         promoter.pickPromoted(mShortcuts, mSuggestions, maxPromoted, promoted);
     85         assertEquals(expectedCount, promoted.getCount());
     86         int count = Math.min(maxPromoted, mShortcuts.getCount());
     87         assertSameSuggestions(slice(promoted, 0, count), slice(mShortcuts, 0, count));
     88     }
     89 
     90     private void maxPromotedTestConcatNext(int maxPromoted) {
     91         Promoter promoter = new ShortcutPromoter(new ConcatPromoter());
     92         int expectedCount = Math.min(maxPromoted, mShortcuts.getCount() + mSuggestionCount);
     93         ListSuggestionCursor promoted = new ListSuggestionCursor(mQuery);
     94         promoter.pickPromoted(mShortcuts, mSuggestions, maxPromoted, promoted);
     95         assertEquals(expectedCount, promoted.getCount());
     96         int count = Math.min(maxPromoted, mShortcuts.getCount());
     97         assertSameSuggestions(slice(promoted, 0, count), slice(mShortcuts, 0, count));
     98         if (mShortcuts.getCount() < expectedCount) {
     99             assertSameSuggestion("wrong suggestion after shortcuts",
    100                     promoted, mShortcuts.getCount(), mSuggestions.get(0), 0);
    101         }
    102     }
    103 
    104     private static int countSuggestions(ArrayList<CorpusResult> suggestions) {
    105         int count = 0;
    106         for (SuggestionCursor c : suggestions) {
    107             count += c.getCount();
    108         }
    109         return count;
    110     }
    111 }
    112