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