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 com.android.quicksearchbox.util.MockNamedTaskExecutor;
     20 
     21 import android.test.AndroidTestCase;
     22 import android.test.suitebuilder.annotation.MediumTest;
     23 
     24 /**
     25  * Tests for {@link SourceShortcutRefresher}.
     26  */
     27 @MediumTest
     28 public class SourceShortcutRefresherTest extends AndroidTestCase {
     29 
     30     private final String mQuery = "foo";
     31 
     32     private MockNamedTaskExecutor mExecutor;
     33 
     34     private SourceShortcutRefresher mRefresher;
     35 
     36     private RefreshListener mListener;
     37 
     38     private Source mSource1;
     39 
     40     private Source mRefreshedSource;
     41     private String mRefreshedShortcutId;
     42     private SuggestionCursor mRefreshedCursor;
     43 
     44     @Override
     45     protected void setUp() throws Exception {
     46         mExecutor = new MockNamedTaskExecutor();
     47         mRefresher = new SourceShortcutRefresher(mExecutor);
     48         mListener = new RefreshListener();
     49         mSource1 = new MockRefreshSource("source1");
     50         mRefreshedSource = null;
     51         mRefreshedShortcutId = null;
     52         mRefreshedCursor = null;
     53     }
     54 
     55     public void testShouldRefreshTrue() {
     56         assertTrue(mRefresher.shouldRefresh(mSource1, "refresh_me"));
     57     }
     58 
     59     public void testShouldRefreshFalse() {
     60         assertFalse(mRefresher.shouldRefresh(null, "foo"));
     61         assertFalse(mRefresher.shouldRefresh(mSource1, null));
     62     }
     63 
     64     public void testMarkShortcutRefreshed() {
     65         mRefresher.markShortcutRefreshed(mSource1, "refreshed");
     66         assertFalse(mRefresher.shouldRefresh(mSource1, "refreshed"));
     67         assertTrue(mRefresher.shouldRefresh(mSource1, "not_refreshed"));
     68     }
     69 
     70     public void testRefreshNull() {
     71         SuggestionData shortcut1 = new SuggestionData(mSource1)
     72                 .setShortcutId("null_refresh");
     73         ListSuggestionCursor shortcuts = new ListSuggestionCursor(mQuery, shortcut1);
     74         mRefresher.refresh(shortcuts, mListener);
     75         assertTrue(mExecutor.runNext());
     76         assertEquals(mSource1, mRefreshedSource);
     77         assertEquals("null_refresh", mRefreshedShortcutId);
     78         assertEquals(null, mRefreshedCursor);
     79     }
     80 
     81     public void testRefreshEmpty() {
     82         SuggestionData shortcut1 = new SuggestionData(mSource1)
     83                 .setShortcutId("empty_refresh");
     84         ListSuggestionCursor shortcuts = new ListSuggestionCursor(mQuery, shortcut1);
     85         mRefresher.refresh(shortcuts, mListener);
     86         assertTrue(mExecutor.runNext());
     87         assertEquals(mSource1, mRefreshedSource);
     88         assertEquals("empty_refresh", mRefreshedShortcutId);
     89         assertEquals(null, mRefreshedCursor);
     90     }
     91 
     92     public void testRefreshSuccess() {
     93         SuggestionData shortcut1 = new SuggestionData(mSource1)
     94                 .setShortcutId("success");
     95         ListSuggestionCursor shortcuts = new ListSuggestionCursor(mQuery, shortcut1);
     96         mRefresher.refresh(shortcuts, mListener);
     97         assertTrue(mExecutor.runNext());
     98         assertEquals(mSource1, mRefreshedSource);
     99         assertEquals("success", mRefreshedShortcutId);
    100         SuggestionCursor expected =
    101                 SuggestionCursorUtil.slice(mSource1.getSuggestions(mQuery, 1, true), 0, 1);
    102         SuggestionCursorUtil.assertSameSuggestions(expected, mRefreshedCursor);
    103     }
    104 
    105     private class RefreshListener implements ShortcutRefresher.Listener {
    106         public void onShortcutRefreshed(Source source, String shortcutId,
    107                 SuggestionCursor refreshed) {
    108             mRefreshedSource = source;
    109             mRefreshedShortcutId = shortcutId;
    110             mRefreshedCursor = refreshed;
    111         }
    112     }
    113 
    114     private class MockRefreshSource extends MockSource {
    115         public MockRefreshSource(String name) {
    116             super(name);
    117         }
    118 
    119         @Override
    120         public SuggestionCursor refreshShortcut(String shortcutId, String extraData) {
    121             if ("null_refresh".equals(shortcutId)) {
    122                 return null;
    123             } else if ("empty_refresh".equals(shortcutId)) {
    124                 return new ListSuggestionCursor(mQuery);
    125             } else {
    126                  SuggestionCursor suggestions = getSuggestions(mQuery, 1, true);
    127                  return SuggestionCursorUtil.slice(suggestions, 0, 1);
    128             }
    129         }
    130     }
    131 
    132 }
    133