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