Home | History | Annotate | Download | only in inputmethod
      1 /*
      2  * Copyright (C) 2017 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 com.android.settings.inputmethod;
     18 
     19 import static com.google.common.truth.Truth.assertThat;
     20 
     21 import android.content.ContentProvider;
     22 import android.content.ContentValues;
     23 import android.database.Cursor;
     24 import android.database.MatrixCursor;
     25 import android.net.Uri;
     26 import android.provider.UserDictionary;
     27 
     28 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     29 
     30 import org.junit.Before;
     31 import org.junit.Test;
     32 import org.junit.runner.RunWith;
     33 import org.mockito.MockitoAnnotations;
     34 import org.robolectric.RuntimeEnvironment;
     35 import org.robolectric.shadows.ShadowContentResolver;
     36 
     37 @RunWith(SettingsRobolectricTestRunner.class)
     38 public class UserDictionaryCursorLoaderTest {
     39 
     40     private ContentProvider mContentProvider;
     41     private UserDictionaryCursorLoader mLoader;
     42 
     43     @Before
     44     public void setUp() {
     45         MockitoAnnotations.initMocks(this);
     46         mContentProvider = new FakeProvider();
     47         mLoader = new UserDictionaryCursorLoader(RuntimeEnvironment.application, "" /* locale */);
     48         ShadowContentResolver.registerProviderInternal(UserDictionary.AUTHORITY, mContentProvider);
     49     }
     50 
     51     @Test
     52     public void testLoad_shouldRemoveDuplicate() {
     53         final Cursor cursor = mLoader.loadInBackground();
     54 
     55         assertThat(cursor.getCount()).isEqualTo(4);
     56     }
     57 
     58     public static class FakeProvider extends ContentProvider {
     59 
     60         @Override
     61         public boolean onCreate() {
     62             return false;
     63         }
     64 
     65         @Override
     66         public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs,
     67                 String sortOrder) {
     68             final MatrixCursor cursor = new MatrixCursor(
     69                     UserDictionaryCursorLoader.QUERY_PROJECTION);
     70             cursor.addRow(new Object[]{1, "word1", "shortcut1"});
     71             cursor.addRow(new Object[]{2, "word2", "shortcut2"});
     72             cursor.addRow(new Object[]{3, "word3", "shortcut3"});
     73             cursor.addRow(new Object[]{4, "word3", "shortcut3"});   // dupe of 3
     74             cursor.addRow(new Object[]{5, "word5", null});          // no shortcut
     75             return cursor;
     76         }
     77 
     78         @Override
     79         public String getType(Uri uri) {
     80             return null;
     81         }
     82 
     83         @Override
     84         public Uri insert(Uri uri, ContentValues values) {
     85             return null;
     86         }
     87 
     88         @Override
     89         public int delete(Uri uri, String selection, String[] selectionArgs) {
     90             return 0;
     91         }
     92 
     93         @Override
     94         public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
     95             return 0;
     96         }
     97     }
     98 }
     99