1/*
2 * Copyright (C) 2006 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
17package android.app;
18
19import android.app.activity.LocalActivity;
20
21import android.app.Activity;
22import android.app.ISearchManager;
23import android.app.SearchManager;
24import android.app.SearchableInfo;
25import android.content.ComponentName;
26import android.content.Context;
27import android.os.ServiceManager;
28import android.test.ActivityInstrumentationTestCase2;
29import android.test.suitebuilder.annotation.LargeTest;
30import android.test.suitebuilder.annotation.MediumTest;
31
32/**
33 * To launch this test from the command line:
34 *
35 * adb shell am instrument -w \
36 *   -e class com.android.unit_tests.SearchManagerTest \
37 *   com.android.unit_tests/android.test.InstrumentationTestRunner
38 */
39public class SearchManagerTest extends ActivityInstrumentationTestCase2<LocalActivity> {
40
41    private ComponentName SEARCHABLE_ACTIVITY =
42            new ComponentName("com.android.frameworks.coretests",
43                    "android.app.activity.SearchableActivity");
44
45    /*
46     * Bug list of test ideas.
47     *
48     * testSearchManagerInterfaceAvailable()
49     *  Exercise the interface obtained
50     *
51     * testSearchManagerAvailable()
52     *  Exercise the interface obtained
53     *
54     * testSearchManagerInvocations()
55     *  FIX - make it work again
56     *  stress test with a very long string
57     *
58     * SearchManager tests
59     *  confirm proper identification of "default" activity based on policy, not hardcoded contacts
60     *
61     * SearchBar tests
62     *  Maybe have to do with framework / unittest runner - need instrumented activity?
63     *  How can we unit test the suggestions content providers?
64     *  Should I write unit tests for any of them?
65     *  Test scenarios:
66     *    type-BACK (cancel)
67     *    type-GO (send)
68     *    type-navigate-click (suggestion)
69     *    type-action
70     *    type-navigate-action (suggestion)
71     */
72
73    /**
74     * Local copy of activity context
75     */
76    Context mContext;
77
78    public SearchManagerTest() {
79        super("com.android.frameworks.coretests", LocalActivity.class);
80    }
81
82    /**
83     * Setup any common data for the upcoming tests.
84     */
85    @Override
86    public void setUp() throws Exception {
87        super.setUp();
88
89        Activity testActivity = getActivity();
90        mContext = testActivity;
91    }
92
93    private ISearchManager getSearchManagerService() {
94        return ISearchManager.Stub.asInterface(
95                ServiceManager.getService(Context.SEARCH_SERVICE));
96    }
97
98    /**
99     * The goal of this test is to confirm that we can obtain
100     * a search manager interface.
101     */
102    @MediumTest
103    public void testSearchManagerInterfaceAvailable() {
104        assertNotNull(getSearchManagerService());
105    }
106
107    /**
108     * The goal of this test is to confirm that we can obtain
109     * a search manager at any time, and that for any given context,
110     * it is a singleton.
111     */
112    @LargeTest
113    public void testSearchManagerAvailable() {
114        SearchManager searchManager1 = (SearchManager)
115                mContext.getSystemService(Context.SEARCH_SERVICE);
116        assertNotNull(searchManager1);
117        SearchManager searchManager2 = (SearchManager)
118                mContext.getSystemService(Context.SEARCH_SERVICE);
119        assertNotNull(searchManager2);
120        assertSame(searchManager1, searchManager2 );
121    }
122
123    /**
124     * Tests that startSearch() can be called multiple times without stopSearch()
125     * in between.
126     */
127    public void testStartSearchIdempotent() throws Exception {
128         SearchManager searchManager = (SearchManager)
129                 mContext.getSystemService(Context.SEARCH_SERVICE);
130         assertNotNull(searchManager);
131
132         searchManager.startSearch(null, false, SEARCHABLE_ACTIVITY, null, false);
133         searchManager.startSearch(null, false, SEARCHABLE_ACTIVITY, null, false);
134         searchManager.stopSearch();
135    }
136
137    /**
138     * Tests that stopSearch() can be called when the search UI is not visible and can be
139     * called multiple times without startSearch() in between.
140     */
141    public void testStopSearchIdempotent() throws Exception {
142         SearchManager searchManager = (SearchManager)
143                 mContext.getSystemService(Context.SEARCH_SERVICE);
144         assertNotNull(searchManager);
145         searchManager.stopSearch();
146
147         searchManager.startSearch(null, false, SEARCHABLE_ACTIVITY, null, false);
148         searchManager.stopSearch();
149         searchManager.stopSearch();
150    }
151
152    /**
153     * The goal of this test is to confirm that we can start and then
154     * stop a simple search.
155     */
156    public void testSearchManagerInvocations() throws Exception {
157        SearchManager searchManager = (SearchManager)
158                mContext.getSystemService(Context.SEARCH_SERVICE);
159        assertNotNull(searchManager);
160
161        // These tests should simply run to completion w/o exceptions
162        searchManager.startSearch(null, false, SEARCHABLE_ACTIVITY, null, false);
163        searchManager.stopSearch();
164
165        searchManager.startSearch("", false, SEARCHABLE_ACTIVITY, null, false);
166        searchManager.stopSearch();
167
168        searchManager.startSearch("test search string", false, SEARCHABLE_ACTIVITY, null, false);
169        searchManager.stopSearch();
170
171        searchManager.startSearch("test search string", true, SEARCHABLE_ACTIVITY, null, false);
172        searchManager.stopSearch();
173    }
174
175}
176