FileDocCategorySizeDatePackage
SearchManagerTest.javaAPI DocAndroid 1.5 API21006Wed May 06 22:42:02 BST 2009com.android.unit_tests

SearchManagerTest

public class SearchManagerTest extends android.test.ActivityInstrumentationTestCase
To launch this test from the command line: adb shell am instrument -w \ -e class com.android.unit_tests.SearchManagerTest \ com.android.unit_tests/android.test.InstrumentationTestRunner

Fields Summary
private static final int
TEST_SEARCH_START
android.content.Context
mContext
Local copy of activity context
Constructors Summary
public SearchManagerTest()


      
        super("com.android.unit_tests", LocalActivity.class);
    
Methods Summary
private voidassertNotEmpty(java.lang.String s)
Combo assert for "string not null and not empty"

        assertNotNull(s);
        MoreAsserts.assertNotEqual(s, "");
    
private voidassertNullOrNotEmpty(java.lang.String s)
Combo assert for "string null or (not null and not empty)"

        if (s != null) {
            MoreAsserts.assertNotEqual(s, "");
        }
    
private voidcheckSearchables(java.util.ArrayList searchablesList)
Generic health checker for an array of searchables. This is designed to pass for any semi-legal searchable, without knowing much about the format of the underlying data. It's fairly easy for a non-compliant application to provide meta-data that will pass here (e.g. a non-existent suggestions authority).

param
searchables The list of searchables to examine.

        assertNotNull(searchablesList);
        int count = searchablesList.size();
        for (int ii = 0; ii < count; ii++) {
            SearchableInfo si = searchablesList.get(ii);
            assertNotNull(si);
            assertTrue(si.mSearchable);
            assertTrue(si.getLabelId() != 0);        // This must be a useable string
            assertNotEmpty(si.mSearchActivity.getClassName());
            assertNotEmpty(si.mSearchActivity.getPackageName());
            if (si.getSuggestAuthority() != null) {
                // The suggestion fields are largely optional, so we'll just confirm basic health
                assertNotEmpty(si.getSuggestAuthority());
                assertNullOrNotEmpty(si.getSuggestPath());
                assertNullOrNotEmpty(si.getSuggestSelection());
                assertNullOrNotEmpty(si.getSuggestIntentAction());
                assertNullOrNotEmpty(si.getSuggestIntentData());
            }
            /* Add a way to get the entire action key list, then explicitly test its elements */
            /* For now, test the most common action key (CALL) */
            ActionKeyInfo ai = si.findActionKey(KeyEvent.KEYCODE_CALL);
            if (ai != null) {
                assertEquals(ai.mKeyCode, KeyEvent.KEYCODE_CALL);
                // one of these three fields must be non-null & non-empty
                boolean m1 = (ai.mQueryActionMsg != null) && (ai.mQueryActionMsg.length() > 0);
                boolean m2 = (ai.mSuggestActionMsg != null) && (ai.mSuggestActionMsg.length() > 0);
                boolean m3 = (ai.mSuggestActionMsgColumn != null) && 
                                (ai.mSuggestActionMsgColumn.length() > 0);
                assertTrue(m1 || m2 || m3);
            }
            
            /* 
             * Find ways to test these:
             * 
             * private int mSearchMode
             * private Drawable mIcon
             */
            
            /*
             * Explicitly not tested here:
             * 
             * Can be null, so not much to see:
             * public String mSearchHint
             * private String mZeroQueryBanner
             * 
             * To be deprecated/removed, so don't bother:
             * public boolean mFilterMode
             * public boolean mQuickStart
             * private boolean mIconResized
             * private int mIconResizeWidth
             * private int mIconResizeHeight
             * 
             * All of these are "internal" working variables, not part of any contract
             * private ActivityInfo mActivityInfo
             * private Rect mTempRect
             * private String mSuggestProviderPackage
             * private String mCacheActivityContext
             */
        }
    
public voidsetUp()
Setup any common data for the upcoming tests.

        super.setUp();
        
        Activity testActivity = getActivity();
        mContext = (Context)testActivity;
    
public voidtestNonSearchable()
Test that non-searchable activities return no searchable info (this would typically trigger the use of the default searchable e.g. contacts)

        // test basic array & hashmap
        SearchableInfo.buildSearchableList(mContext);

        // confirm that we return null for non-searchy activities
        ComponentName nonActivity = new ComponentName(
                            "com.android.unit_tests",
                            "com.android.unit_tests.NO_SEARCH_ACTIVITY");
        SearchableInfo si = SearchableInfo.getSearchableInfo(mContext, nonActivity);
        assertNull(si);
    
public voidtestSearchManagerAvailable()
The goal of this test is to confirm that we can obtain a search manager at any time, and that for any given context, it is a singleton.

        SearchManager searchManager1 = (SearchManager)
                mContext.getSystemService(Context.SEARCH_SERVICE);
        assertNotNull(searchManager1);
        SearchManager searchManager2 = (SearchManager)
                mContext.getSystemService(Context.SEARCH_SERVICE);
        assertNotNull(searchManager2);
        assertSame( searchManager1, searchManager2 );
    
public voidtestSearchManagerContextRestrictions()
The goal of this test is to confirm that we can *only* obtain a search manager interface from an Activity context.

        SearchManager searchManager1 = (SearchManager)
                mContext.getSystemService(Context.SEARCH_SERVICE);
        assertNotNull(searchManager1);
        
        Context applicationContext = mContext.getApplicationContext();
        // this should fail, because you can't get a SearchManager from a non-Activity context
        try {
            applicationContext.getSystemService(Context.SEARCH_SERVICE);
            assertFalse("Shouldn't retrieve SearchManager from a non-Activity context", true);
        } catch (AndroidRuntimeException e) {
            // happy here - we should catch this.
        }
    
public voidtestSearchManagerInterfaceAvailable()
The goal of this test is to confirm that we can obtain a search manager interface.

        ISearchManager searchManager1 = ISearchManager.Stub.asInterface(
                ServiceManager.getService(Context.SEARCH_SERVICE));
        assertNotNull(searchManager1);
    
public voidtestSearchManagerInvocations()
The goal of this test is to confirm that we can start and then stop a simple search.

        SearchManager searchManager = (SearchManager)
                mContext.getSystemService(Context.SEARCH_SERVICE);
        assertNotNull(searchManager);
        
            // TODO: make a real component name, or remove this need
        final ComponentName cn = new ComponentName("", "");

        if (TEST_SEARCH_START != 0) {
            // These tests should simply run to completion w/o exceptions
            searchManager.startSearch(null, false, cn, null, false);
            searchManager.stopSearch();
            
            searchManager.startSearch("", false, cn, null, false);
            searchManager.stopSearch();
            
            searchManager.startSearch("test search string", false, cn, null, false);
            searchManager.stopSearch();
            
            searchManager.startSearch("test search string", true, cn, null, false);
            searchManager.stopSearch();
        }
     
public voidtestSearchableGoogleSearch()
The goal of this test is to confirm proper operation of the SearchableInfo helper class. TODO: The metadata source needs to be mocked out because adding searchability metadata via this test is causing it to leak into the real system. So for now I'm just going to test for existence of the GoogleSearch app (which is searchable).

        // test basic array & hashmap
        SearchableInfo.buildSearchableList(mContext);

        // test linkage from another activity
        // TODO inject this via mocking into the package manager.
        // TODO for now, just check for searchable GoogleSearch app (this isn't really a unit test)
        ComponentName thisActivity = new ComponentName(
                "com.android.googlesearch", 
                "com.android.googlesearch.GoogleSearch");

        SearchableInfo si = SearchableInfo.getSearchableInfo(mContext, thisActivity);
        assertNotNull(si);
        assertTrue(si.mSearchable);
        assertEquals(thisActivity, si.mSearchActivity);
        
        Context appContext = si.getActivityContext(mContext);
        assertNotNull(appContext);
        MoreAsserts.assertNotEqual(appContext, mContext);
        assertEquals("Google Search", appContext.getString(si.getHintId()));
        assertEquals("Google", appContext.getString(si.getLabelId()));
    
public voidtestSearchableMocked()
This is an attempt to run the searchable info list with a mocked context. Here are some things I'd like to test. Confirm OK with "zero" searchables Confirm "good" metadata read properly Confirm "bad" metadata skipped properly Confirm ordering of searchables Confirm "good" actionkeys confirm "bad" actionkeys are rejected confirm XML ordering enforced (will fail today - bug in SearchableInfo) findActionKey works getIcon works

        MyMockPackageManager mockPM = new MyMockPackageManager(mContext.getPackageManager());
        MyMockContext mockContext = new MyMockContext(mContext, mockPM);
        ArrayList<SearchableInfo> searchables;
        int count;

        // build item list with real-world source data
        mockPM.setSearchablesMode(MyMockPackageManager.SEARCHABLES_PASSTHROUGH);
        SearchableInfo.buildSearchableList(mockContext);
        // tests with "real" searchables (deprecate, this should be a unit test)
        searchables = SearchableInfo.getSearchablesList();
        count = searchables.size();
        assertTrue(count >= 1);         // this isn't really a unit test
        checkSearchables(searchables);

        // build item list with mocked search data
        // this round of tests confirms good operations with "zero" searchables found
        // This should return either a null pointer or an empty list
        mockPM.setSearchablesMode(MyMockPackageManager.SEARCHABLES_MOCK_ZERO);
        SearchableInfo.buildSearchableList(mockContext);
        searchables = SearchableInfo.getSearchablesList();
        if (searchables != null) {
            count = searchables.size();
            assertTrue(count == 0);
        }