SearchManagerTest.java revision 052e88bb60d43d82a94985d655d7dfaa6244fc69
1/* 2 * Copyright (C) 2006 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 17package android.app; 18 19import android.app.activity.LocalActivity; 20 21import android.app.Activity; 22import android.app.ISearchManager; 23import android.app.SearchManager; 24import android.app.SearchableInfo; 25import android.content.ComponentName; 26import android.content.Context; 27import android.os.ServiceManager; 28import android.test.ActivityInstrumentationTestCase2; 29import android.test.suitebuilder.annotation.LargeTest; 30import android.test.suitebuilder.annotation.MediumTest; 31 32/** 33 * To launch this test from the command line: 34 * 35 * adb shell am instrument -w \ 36 * -e class com.android.unit_tests.SearchManagerTest \ 37 * com.android.unit_tests/android.test.InstrumentationTestRunner 38 */ 39public class SearchManagerTest extends ActivityInstrumentationTestCase2<LocalActivity> { 40 41 private ComponentName SEARCHABLE_ACTIVITY = 42 new ComponentName("com.android.frameworks.coretests", 43 "android.app.activity.SearchableActivity"); 44 45 /* 46 * Bug list of test ideas. 47 * 48 * testSearchManagerInterfaceAvailable() 49 * Exercise the interface obtained 50 * 51 * testSearchManagerAvailable() 52 * Exercise the interface obtained 53 * 54 * testSearchManagerInvocations() 55 * FIX - make it work again 56 * stress test with a very long string 57 * 58 * SearchManager tests 59 * confirm proper identification of "default" activity based on policy, not hardcoded contacts 60 * 61 * SearchBar tests 62 * Maybe have to do with framework / unittest runner - need instrumented activity? 63 * How can we unit test the suggestions content providers? 64 * Should I write unit tests for any of them? 65 * Test scenarios: 66 * type-BACK (cancel) 67 * type-GO (send) 68 * type-navigate-click (suggestion) 69 * type-action 70 * type-navigate-action (suggestion) 71 */ 72 73 /** 74 * Local copy of activity context 75 */ 76 Context mContext; 77 78 public SearchManagerTest() { 79 super("com.android.frameworks.coretests", LocalActivity.class); 80 } 81 82 /** 83 * Setup any common data for the upcoming tests. 84 */ 85 @Override 86 public void setUp() throws Exception { 87 super.setUp(); 88 89 Activity testActivity = getActivity(); 90 mContext = testActivity; 91 } 92 93 private ISearchManager getSearchManagerService() { 94 return ISearchManager.Stub.asInterface( 95 ServiceManager.getService(Context.SEARCH_SERVICE)); 96 } 97 98 /** 99 * The goal of this test is to confirm that we can obtain 100 * a search manager interface. 101 */ 102 @MediumTest 103 public void testSearchManagerInterfaceAvailable() { 104 assertNotNull(getSearchManagerService()); 105 } 106 107 /** 108 * The goal of this test is to confirm that we can obtain 109 * a search manager at any time, and that for any given context, 110 * it is a singleton. 111 */ 112 @LargeTest 113 public void testSearchManagerAvailable() { 114 SearchManager searchManager1 = (SearchManager) 115 mContext.getSystemService(Context.SEARCH_SERVICE); 116 assertNotNull(searchManager1); 117 SearchManager searchManager2 = (SearchManager) 118 mContext.getSystemService(Context.SEARCH_SERVICE); 119 assertNotNull(searchManager2); 120 assertSame(searchManager1, searchManager2 ); 121 } 122 123 @MediumTest 124 public void testSearchables() { 125 SearchManager searchManager = (SearchManager) 126 mContext.getSystemService(Context.SEARCH_SERVICE); 127 SearchableInfo si; 128 129 si = searchManager.getSearchableInfo(SEARCHABLE_ACTIVITY, false); 130 assertNotNull(si); 131 assertFalse(searchManager.isDefaultSearchable(si)); 132 si = searchManager.getSearchableInfo(SEARCHABLE_ACTIVITY, true); 133 assertNotNull(si); 134 assertTrue(searchManager.isDefaultSearchable(si)); 135 si = searchManager.getSearchableInfo(null, true); 136 assertNotNull(si); 137 assertTrue(searchManager.isDefaultSearchable(si)); 138 } 139 140 /** 141 * Tests that startSearch() can be called multiple times without stopSearch() 142 * in between. 143 */ 144 public void testStartSearchIdempotent() throws Exception { 145 SearchManager searchManager = (SearchManager) 146 mContext.getSystemService(Context.SEARCH_SERVICE); 147 assertNotNull(searchManager); 148 149 searchManager.startSearch(null, false, SEARCHABLE_ACTIVITY, null, false); 150 searchManager.startSearch(null, false, SEARCHABLE_ACTIVITY, null, false); 151 searchManager.stopSearch(); 152 } 153 154 /** 155 * Tests that stopSearch() can be called when the search UI is not visible and can be 156 * called multiple times without startSearch() in between. 157 */ 158 public void testStopSearchIdempotent() throws Exception { 159 SearchManager searchManager = (SearchManager) 160 mContext.getSystemService(Context.SEARCH_SERVICE); 161 assertNotNull(searchManager); 162 searchManager.stopSearch(); 163 164 searchManager.startSearch(null, false, SEARCHABLE_ACTIVITY, null, false); 165 searchManager.stopSearch(); 166 searchManager.stopSearch(); 167 } 168 169 /** 170 * The goal of this test is to confirm that we can start and then 171 * stop a simple search. 172 */ 173 public void testSearchManagerInvocations() throws Exception { 174 SearchManager searchManager = (SearchManager) 175 mContext.getSystemService(Context.SEARCH_SERVICE); 176 assertNotNull(searchManager); 177 178 // These tests should simply run to completion w/o exceptions 179 searchManager.startSearch(null, false, SEARCHABLE_ACTIVITY, null, false); 180 searchManager.stopSearch(); 181 182 searchManager.startSearch("", false, SEARCHABLE_ACTIVITY, null, false); 183 searchManager.stopSearch(); 184 185 searchManager.startSearch("test search string", false, SEARCHABLE_ACTIVITY, null, false); 186 searchManager.stopSearch(); 187 188 searchManager.startSearch("test search string", true, SEARCHABLE_ACTIVITY, null, false); 189 searchManager.stopSearch(); 190 } 191 192} 193