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 18package com.android.settings.search; 19 20 21import static com.google.common.truth.Truth.assertThat; 22 23import android.content.Context; 24 25import com.android.settings.TestConfig; 26import com.android.settings.testutils.DatabaseTestUtils; 27import com.android.settings.testutils.SettingsRobolectricTestRunner; 28 29import org.junit.After; 30import org.junit.Before; 31import org.junit.Test; 32import org.junit.runner.RunWith; 33import org.robolectric.RuntimeEnvironment; 34import org.robolectric.annotation.Config; 35 36import java.util.List; 37 38@RunWith(SettingsRobolectricTestRunner.class) 39@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 40public class SavedQueryRecorderAndRemoverTest { 41 42 private Context mContext; 43 private SavedQueryRecorder mRecorder; 44 private SavedQueryRemover mRemover; 45 46 @Before 47 public void setUp() { 48 mContext = RuntimeEnvironment.application; 49 } 50 51 @After 52 public void cleanUp() { 53 DatabaseTestUtils.clearDb(mContext); 54 } 55 56 @Test 57 public void canSaveAndRemoveQuery() { 58 final String query = "test"; 59 mRecorder = new SavedQueryRecorder(mContext, query); 60 mRemover = new SavedQueryRemover(mContext); 61 62 // Record a new query and load all queries from DB 63 mRecorder.loadInBackground(); 64 final SavedQueryLoader loader = new SavedQueryLoader(mContext); 65 List<? extends SearchResult> results = loader.loadInBackground(); 66 67 // Should contain the newly recorded query 68 assertThat(results.size()).isEqualTo(1); 69 assertThat(results.get(0).title).isEqualTo(query); 70 71 // Remove the query and load all queries from DB 72 mRemover.loadInBackground(); 73 results = loader.loadInBackground(); 74 75 // Saved query list should be empty because it's removed. 76 assertThat(results).isEmpty(); 77 } 78 79 @Test 80 public void canRemoveAllQueriesAtOnce() { 81 mRemover = new SavedQueryRemover(mContext);; 82 83 // Record a new query and load all queries from DB 84 new SavedQueryRecorder(mContext, "Test1").loadInBackground(); 85 new SavedQueryRecorder(mContext, "Test2").loadInBackground(); 86 final SavedQueryLoader loader = new SavedQueryLoader(mContext); 87 List<? extends SearchResult> results = loader.loadInBackground(); 88 assertThat(results.size()).isEqualTo(2); 89 90 mRemover.loadInBackground(); 91 results = loader.loadInBackground(); 92 93 // Saved query list should be empty because it's removed. 94 assertThat(results).isEmpty(); 95 } 96} 97