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