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.provider.cts; 18 19 import android.content.ContentResolver; 20 import android.content.ContentValues; 21 import android.content.Context; 22 import android.cts.util.PollingCheck; 23 import android.database.Cursor; 24 import android.net.Uri; 25 import android.provider.SearchRecentSuggestions; 26 import android.test.ProviderTestCase2; 27 28 public class SearchRecentSuggestionsTest extends 29 ProviderTestCase2<TestSearchRecentSuggestionsProvider> { 30 private final static String AUTHORITY_HEAD = "content://" 31 + TestSearchRecentSuggestionsProvider.AUTHORITY; 32 33 private Uri mTestUri; 34 private TestSearchRecentSuggestionsProvider mTestSRSProvider; 35 private Context mProviderContext; 36 37 @Override 38 public void setUp() throws Exception { 39 super.setUp(); 40 mTestUri = Uri.parse(AUTHORITY_HEAD + "/suggestions"); 41 mTestSRSProvider = getProvider(); 42 mProviderContext = mTestSRSProvider.getContext(); 43 } 44 45 public SearchRecentSuggestionsTest() { 46 super(TestSearchRecentSuggestionsProvider.class, 47 TestSearchRecentSuggestionsProvider.AUTHORITY); 48 } 49 50 public void testConstructor() { 51 new SearchRecentSuggestions(mProviderContext, TestSearchRecentSuggestionsProvider.AUTHORITY, 52 TestSearchRecentSuggestionsProvider.MODE); 53 } 54 55 public void testSearchRecentSuggestions() { 56 SearchRecentSuggestions srs = new MySearchRecentSuggestions(mProviderContext, 57 TestSearchRecentSuggestionsProvider.AUTHORITY, 58 TestSearchRecentSuggestionsProvider.MODE); 59 Cursor c = mTestSRSProvider.query(mTestUri, null, null, null, null); 60 61 try { 62 assertNotNull(c); 63 assertEquals(0, c.getCount()); 64 c.close(); 65 66 // insert three rows 67 String query1 = "query1"; 68 String line1 = "line1"; 69 srs.saveRecentQuery(query1, line1); 70 71 waitForCursorCount(mTestUri, SearchRecentSuggestions.QUERIES_PROJECTION_2LINE, 1); 72 73 c = mTestSRSProvider.query(mTestUri, SearchRecentSuggestions.QUERIES_PROJECTION_2LINE, 74 null, null, null); 75 c.moveToFirst(); 76 assertEquals(query1, c 77 .getString(SearchRecentSuggestions.QUERIES_PROJECTION_QUERY_INDEX)); 78 assertEquals(line1, c 79 .getString(SearchRecentSuggestions.QUERIES_PROJECTION_DISPLAY2_INDEX)); 80 c.close(); 81 82 String query2 = "query2"; 83 String line2 = "line2"; 84 srs.saveRecentQuery(query2, line2); 85 waitForCursorCount(mTestUri, null, 2); 86 87 String query3 = "query3"; 88 String line3 = "line3"; 89 srs.saveRecentQuery(query3, line3); 90 waitForCursorCount(mTestUri, null, 3); 91 92 // truncateHistory will delete the oldest one record 93 ContentResolver cr = mProviderContext.getContentResolver(); 94 ((MySearchRecentSuggestions) srs).truncateHistory(cr, 2); 95 96 waitForCursorCount(mTestUri, SearchRecentSuggestions.QUERIES_PROJECTION_2LINE, 2); 97 98 c = mTestSRSProvider.query(mTestUri, SearchRecentSuggestions.QUERIES_PROJECTION_2LINE, 99 null, null, null); 100 101 // and the left two should be: test2 and test3, test1 should be delete 102 c.moveToFirst(); 103 assertEquals(query2, c 104 .getString(SearchRecentSuggestions.QUERIES_PROJECTION_QUERY_INDEX)); 105 assertEquals(line2, c 106 .getString(SearchRecentSuggestions.QUERIES_PROJECTION_DISPLAY2_INDEX)); 107 c.moveToNext(); 108 assertEquals(query3, c 109 .getString(SearchRecentSuggestions.QUERIES_PROJECTION_QUERY_INDEX)); 110 assertEquals(line3, c 111 .getString(SearchRecentSuggestions.QUERIES_PROJECTION_DISPLAY2_INDEX)); 112 c.close(); 113 114 // clear all history 115 srs.clearHistory(); 116 waitForCursorCount(mTestUri, null, 0); 117 } finally { 118 c.close(); 119 } 120 } 121 122 public void testSuggestionsTable() { 123 String insertDisplay1 = "display1_insert"; 124 String insertDisplay2 = "display2_insert"; 125 String insertQuery = "query_insert"; 126 127 String updateDisplay1 = "display1_update"; 128 String updateDisplay2 = "display2_update"; 129 String updateQuery = "query_update"; 130 131 // Test: insert 132 ContentValues value = new ContentValues(); 133 value.put("display1", insertDisplay1); 134 value.put("display2", insertDisplay2); 135 value.put("query", insertQuery); 136 value.put("date", 1); 137 138 mTestSRSProvider.insert(mTestUri, value); 139 140 Cursor cursor = mTestSRSProvider.query(mTestUri, 141 SearchRecentSuggestions.QUERIES_PROJECTION_2LINE, 142 "display1=\"" + insertDisplay1 + "\"", null, null); 143 try { 144 assertNotNull(cursor); 145 assertEquals(1, cursor.getCount()); 146 assertTrue(cursor.moveToFirst()); 147 assertEquals(insertDisplay2, cursor 148 .getString(SearchRecentSuggestions.QUERIES_PROJECTION_DISPLAY2_INDEX)); 149 assertEquals(insertQuery, cursor 150 .getString(SearchRecentSuggestions.QUERIES_PROJECTION_QUERY_INDEX)); 151 assertEquals(1, cursor.getInt(SearchRecentSuggestions.QUERIES_PROJECTION_DATE_INDEX)); 152 cursor.close(); 153 154 // Test: update 155 /** 156 * SearchRecentSuggestionsProvider.update is not implement, always 157 * throw an UnsupportedOperationException. 158 */ 159 value.clear(); 160 value.put("display1", updateDisplay1); 161 value.put("display2", updateDisplay2); 162 value.put("query", updateQuery); 163 value.put("date", 2); 164 165 try { 166 mTestSRSProvider.update(mTestUri, value, "display1=\"" + insertDisplay1 + "\"", 167 null); 168 fail("There should be an UnsupportedOperationException thrown out."); 169 } catch (UnsupportedOperationException e) { 170 // expected, test success. 171 } 172 173 // Test: delete 174 mTestSRSProvider.delete(mTestUri, "display1=\"" + insertDisplay1 + "\"", null); 175 cursor = mTestSRSProvider.query(mTestUri, 176 SearchRecentSuggestions.QUERIES_PROJECTION_2LINE, "display1=\"" 177 + insertDisplay1 + "\"", null, null); 178 assertNotNull(cursor); 179 assertEquals(0, cursor.getCount()); 180 } finally { 181 cursor.close(); 182 } 183 } 184 185 private class MySearchRecentSuggestions extends SearchRecentSuggestions { 186 public MySearchRecentSuggestions(Context context, String authority, int mode) { 187 super(context, authority, mode); 188 } 189 190 @Override 191 protected void truncateHistory(ContentResolver cr, int maxEntries) { 192 super.truncateHistory(cr, maxEntries); 193 } 194 } 195 196 private void waitForCursorCount(final Uri uri, final String[] projection, 197 final int expectedCount) { 198 new PollingCheck() { 199 @Override 200 protected boolean check() { 201 Cursor cursor = null; 202 try { 203 cursor = mTestSRSProvider.query(uri, projection, null, null, null); 204 return cursor != null && cursor.getCount() == expectedCount; 205 } finally { 206 if (cursor != null) { 207 cursor.close(); 208 } 209 } 210 } 211 }.run(); 212 } 213 } 214