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.content.Context; 24 import android.database.Cursor; 25 import android.database.MatrixCursor; 26 import android.net.Uri; 27 import android.provider.UserDictionary; 28 29 import com.android.settings.testutils.SettingsRobolectricTestRunner; 30 31 import org.junit.Before; 32 import org.junit.Test; 33 import org.junit.runner.RunWith; 34 import org.mockito.MockitoAnnotations; 35 import org.robolectric.RuntimeEnvironment; 36 import org.robolectric.shadows.ShadowContentResolver; 37 38 @RunWith(SettingsRobolectricTestRunner.class) 39 public class UserDictionaryListTest { 40 41 private FakeProvider mContentProvider; 42 43 @Before 44 public void setUp() { 45 MockitoAnnotations.initMocks(this); 46 mContentProvider = new FakeProvider(); 47 ShadowContentResolver.registerProviderInternal(UserDictionary.AUTHORITY, mContentProvider); 48 } 49 50 @Test 51 public void getUserDictionaryLocalesSet_noLocale_shouldReturnEmptySet() { 52 mContentProvider.hasDictionary = false; 53 54 final Context context = RuntimeEnvironment.application; 55 assertThat(UserDictionaryList.getUserDictionaryLocalesSet(context)).isEmpty(); 56 } 57 58 public static class FakeProvider extends ContentProvider { 59 60 private boolean hasDictionary = true; 61 62 @Override 63 public boolean onCreate() { 64 return false; 65 } 66 67 @Override 68 public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, 69 String sortOrder) { 70 if (hasDictionary) { 71 final MatrixCursor cursor = new MatrixCursor( 72 new String[]{UserDictionary.Words.LOCALE}); 73 cursor.addRow(new Object[]{"en"}); 74 cursor.addRow(new Object[]{"es"}); 75 return cursor; 76 } else { 77 return null; 78 } 79 } 80 81 @Override 82 public String getType(Uri uri) { 83 return null; 84 } 85 86 @Override 87 public Uri insert(Uri uri, ContentValues values) { 88 return null; 89 } 90 91 @Override 92 public int delete(Uri uri, String selection, String[] selectionArgs) { 93 return 0; 94 } 95 96 @Override 97 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) { 98 return 0; 99 } 100 } 101 } 102