1 /* 2 * Copyright (C) 2008 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 android.app.cts; 18 19 import android.app.SearchManager; 20 import android.app.UiModeManager; 21 import android.app.stubs.CTSActivityTestCaseBase; 22 import android.app.stubs.SearchManagerStubActivity; 23 import android.content.Context; 24 import android.content.Intent; 25 import android.content.res.Configuration; 26 27 public class SearchManagerTest extends CTSActivityTestCaseBase { 28 29 private void setupActivity(String action) { 30 Intent intent = new Intent(); 31 intent.setAction(action); 32 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 33 intent.setClass(getInstrumentation().getTargetContext(), SearchManagerStubActivity.class); 34 getInstrumentation().getTargetContext().startActivity(intent); 35 } 36 37 public void testStopSearch() throws InterruptedException { 38 if (!hasGlobalSearchActivity()) { 39 return; 40 } 41 SearchManagerStubActivity.setCTSResult(this); 42 setupActivity(SearchManagerStubActivity.TEST_STOP_SEARCH); 43 waitForResult(); 44 } 45 46 public void testSetOnDismissListener() throws InterruptedException { 47 if (!hasGlobalSearchActivity()) { 48 return; 49 } 50 SearchManagerStubActivity.setCTSResult(this); 51 setupActivity(SearchManagerStubActivity.TEST_ON_DISMISSLISTENER); 52 waitForResult(); 53 } 54 55 public void testSetOnCancelListener() throws InterruptedException { 56 if (!hasGlobalSearchActivity()) { 57 return; 58 } 59 SearchManagerStubActivity.setCTSResult(this); 60 setupActivity(SearchManagerStubActivity.TEST_ON_CANCELLISTENER); 61 waitForResult(); 62 } 63 64 private boolean hasGlobalSearchActivity() { 65 Context context = getInstrumentation().getTargetContext(); 66 UiModeManager uiModeManager = context.getSystemService(UiModeManager.class); 67 if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_TELEVISION) { 68 return false; 69 } 70 SearchManager searchManager = 71 (SearchManager) context.getSystemService(Context.SEARCH_SERVICE); 72 if (searchManager == null) { 73 return false; 74 } 75 try { 76 return searchManager.getGlobalSearchActivity() != null; 77 } catch (NullPointerException e) { 78 // Means there is no internal search service. 79 return false; 80 } 81 } 82 } 83