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.Activity; 20 import android.app.SearchManager; 21 import android.content.ComponentName; 22 import android.content.Context; 23 import android.os.Bundle; 24 import android.os.Handler; 25 import android.os.Message; 26 import android.util.Log; 27 28 public class SearchManagerStubActivity extends Activity { 29 30 private static final String TAG = "SearchManagerStubActivity"; 31 32 public static final String TEST_STOP_SEARCH = "stopSearch"; 33 public static final String TEST_ON_DISMISSLISTENER = "setOnDismissListener"; 34 public static final String TEST_ON_CANCELLISTENER = "setOnCancelListener"; 35 36 private SearchManager mSearchManager; 37 private ComponentName mComponentName; 38 39 private static CTSResult sCTSResult; 40 private boolean mDismissCalled; 41 private boolean mCancelCalled; 42 43 public static void setCTSResult(CTSResult result) { 44 sCTSResult = result; 45 } 46 47 @Override 48 public void onCreate(Bundle icicle) { 49 super.onCreate(icicle); 50 mSearchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 51 mComponentName = getComponentName(); 52 String action = getIntent().getAction(); 53 if (action.equals(TEST_STOP_SEARCH)) { 54 testStopSearch(); 55 } else if (action.equals(TEST_ON_DISMISSLISTENER)) { 56 testOnDismissListener(); 57 } else if (action.equals(TEST_ON_CANCELLISTENER)) { 58 testOnCancelListener(); 59 } 60 } 61 62 private void testOnCancelListener() { 63 mCancelCalled = false; 64 mSearchManager.setOnCancelListener(new SearchManager.OnCancelListener() { 65 @Override 66 public void onCancel() { 67 mCancelCalled = true; 68 } 69 }); 70 71 new TestStepHandler() { 72 @Override 73 public boolean doStep(int step) throws FailException { 74 switch (step) { 75 case 1: 76 startSearch("test", false, mComponentName, null, false); 77 return false; 78 case 2: 79 assertFalse("cancel called", mCancelCalled); 80 stopSearch(); 81 return false; 82 case 3: 83 assertTrue("cancel not called", mCancelCalled); 84 pass(); 85 return true; 86 default: 87 throw new IllegalArgumentException("Bad step " + step); 88 } 89 } 90 }.start(); 91 } 92 93 private void testOnDismissListener() { 94 mDismissCalled = false; 95 96 mSearchManager.setOnDismissListener(new SearchManager.OnDismissListener() { 97 public void onDismiss() { 98 mDismissCalled = true; 99 } 100 }); 101 102 new TestStepHandler() { 103 @Override 104 public boolean doStep(int step) throws FailException { 105 switch (step) { 106 case 1: 107 startSearch("test", false, mComponentName, null, false); 108 return false; 109 case 2: 110 if (mDismissCalled) { 111 throw new FailException("dismiss called"); 112 } else { 113 stopSearch(); 114 } 115 return false; 116 case 3: 117 if (mDismissCalled) { 118 pass(); 119 } else { 120 throw new FailException("dismiss not called"); 121 } 122 return true; 123 default: 124 throw new IllegalArgumentException("Bad step " + step); 125 } 126 } 127 }.start(); 128 } 129 130 private void testStopSearch() { 131 new TestStepHandler() { 132 @Override 133 public boolean doStep(int step) throws FailException { 134 switch (step) { 135 case 1: 136 startSearch("test", false, mComponentName, null, false); 137 return false; 138 case 2: 139 assertVisible(); 140 stopSearch(); 141 return false; 142 case 3: 143 assertInVisible(); 144 pass(); 145 return true; 146 default: 147 throw new IllegalArgumentException("Bad step " + step); 148 } 149 } 150 }.start(); 151 } 152 153 private void fail(Exception ex) { 154 Log.e(TAG, "test failed", ex); 155 sCTSResult.setResult(CTSResult.RESULT_FAIL); 156 finish(); 157 } 158 159 private void pass() { 160 sCTSResult.setResult(CTSResult.RESULT_OK); 161 finish(); 162 } 163 164 private void assertInVisible() throws FailException { 165 if (isVisible()) { 166 throw new FailException(); 167 } 168 } 169 170 private void assertVisible() throws FailException { 171 if (!isVisible()) { 172 throw new FailException(); 173 } 174 } 175 176 private void assertFalse(String message, boolean value) throws FailException { 177 assertTrue(message, !value); 178 } 179 180 private void assertTrue(String message, boolean value) throws FailException { 181 if (!value) { 182 throw new FailException(message); 183 } 184 } 185 186 private void startSearch(String initialQuery, boolean selectInitialQuery, 187 ComponentName launchActivity, Bundle appSearchData, boolean globalSearch) { 188 mSearchManager.startSearch(initialQuery, selectInitialQuery, launchActivity, appSearchData, 189 globalSearch); 190 } 191 192 private void stopSearch() { 193 mSearchManager.stopSearch(); 194 } 195 196 private boolean isVisible() { 197 return mSearchManager.isVisible(); 198 } 199 200 private abstract class TestStepHandler extends Handler { 201 202 public void start() { 203 sendEmptyMessage(1); 204 } 205 206 @Override 207 public void handleMessage(Message msg) { 208 try { 209 if (!doStep(msg.what)) { 210 sendEmptyMessage(msg.what + 1); 211 } 212 } catch (FailException ex) { 213 fail(ex); 214 } 215 } 216 217 /** 218 * Performs one step of the test. 219 * 220 * @param step The 1-based number of the step to perform. 221 * @return {@code true} if this was the last step. 222 * @throws FailException If the test failed. 223 */ 224 protected abstract boolean doStep(int step) throws FailException; 225 } 226 227 private static class FailException extends Exception { 228 private static final long serialVersionUID = 1L; 229 230 public FailException() { 231 super(); 232 } 233 234 public FailException(String detailMessage) { 235 super(detailMessage); 236 } 237 } 238 } 239