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 18 package com.android.settings.search; 19 20 21 import android.content.ContentValues; 22 import android.content.Context; 23 import android.database.sqlite.SQLiteDatabase; 24 25 import com.android.settings.testutils.SettingsRobolectricTestRunner; 26 import com.android.settings.TestConfig; 27 import com.android.settings.testutils.DatabaseTestUtils; 28 29 import org.junit.After; 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 import org.robolectric.RuntimeEnvironment; 34 import org.robolectric.annotation.Config; 35 36 import java.util.List; 37 38 import static com.google.common.truth.Truth.assertThat; 39 40 @RunWith(SettingsRobolectricTestRunner.class) 41 @Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 42 public class SavedQueryLoaderTest { 43 44 private Context mContext; 45 private SQLiteDatabase mDb; 46 private SavedQueryLoader mLoader; 47 48 @Before 49 public void setUp() { 50 mContext = RuntimeEnvironment.application; 51 mDb = IndexDatabaseHelper.getInstance(mContext).getWritableDatabase(); 52 mLoader = new SavedQueryLoader(mContext); 53 setUpDb(); 54 } 55 56 @After 57 public void cleanUp() { 58 DatabaseTestUtils.clearDb(mContext); 59 } 60 61 @Test 62 public void loadInBackground_shouldReturnSavedQueries() { 63 final List<? extends SearchResult> results = mLoader.loadInBackground(); 64 assertThat(results.size()).isEqualTo(SavedQueryLoader.MAX_PROPOSED_SUGGESTIONS); 65 for (SearchResult result : results) { 66 assertThat(result.viewType).isEqualTo(ResultPayload.PayloadType.SAVED_QUERY); 67 } 68 } 69 70 private void setUpDb() { 71 final long now = System.currentTimeMillis(); 72 for (int i = 0; i < SavedQueryLoader.MAX_PROPOSED_SUGGESTIONS + 2; i++) { 73 ContentValues values = new ContentValues(); 74 values.put(IndexDatabaseHelper.SavedQueriesColumns.QUERY, String.valueOf(i)); 75 values.put(IndexDatabaseHelper.SavedQueriesColumns.TIME_STAMP, now); 76 mDb.replaceOrThrow(IndexDatabaseHelper.Tables.TABLE_SAVED_QUERIES, null, values); 77 } 78 } 79 } 80