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