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