1 /* 2 * Copyright (C) 2022 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.appsearch.app.helper_b; 18 19 import static android.app.appsearch.testutil.AppSearchTestUtils.convertSearchResultsToDocuments; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import android.app.appsearch.GenericDocument; 24 import android.app.appsearch.GlobalSearchSessionShim; 25 import android.app.appsearch.SearchSpec; 26 import android.app.appsearch.testutil.GlobalSearchSessionShimImpl; 27 28 import androidx.test.ext.junit.runners.AndroidJUnit4; 29 30 import org.junit.Before; 31 import org.junit.Test; 32 import org.junit.runner.RunWith; 33 34 import java.util.List; 35 36 @RunWith(AndroidJUnit4.class) 37 public class AppSearchDeviceTest { 38 39 private static final String NAMESPACE = "namespace"; 40 private static final String ID = "id"; 41 private static final String SCHEMA = "testSchema"; 42 private static final GenericDocument DOCUMENT = 43 new GenericDocument.Builder<>(NAMESPACE, ID, SCHEMA) 44 .setPropertyString("subject", "testPut example1") 45 .setCreationTimestampMillis(12345L) 46 .build(); 47 48 private GlobalSearchSessionShim mGlobalSearch; 49 50 @Before setUp()51 public void setUp() throws Exception { 52 mGlobalSearch = GlobalSearchSessionShimImpl.createGlobalSearchSessionAsync().get(); 53 } 54 55 @Test testGlobalGetDocuments_exist()56 public void testGlobalGetDocuments_exist() throws Exception { 57 List<GenericDocument> outDocuments = convertSearchResultsToDocuments( 58 mGlobalSearch.search(/*queryExpression=*/"", 59 new SearchSpec.Builder().setTermMatch(SearchSpec.TERM_MATCH_PREFIX) 60 .addFilterSchemas(SCHEMA) 61 .build())); 62 assertThat(outDocuments).containsExactly(DOCUMENT); 63 } 64 65 @Test testGlobalGetDocuments_nonexist()66 public void testGlobalGetDocuments_nonexist() throws Exception { 67 List<GenericDocument> outDocuments = convertSearchResultsToDocuments( 68 mGlobalSearch.search(/*queryExpression=*/"", 69 new SearchSpec.Builder().setTermMatch(SearchSpec.TERM_MATCH_PREFIX) 70 .addFilterSchemas(SCHEMA) 71 .build())); 72 assertThat(outDocuments).isEmpty(); 73 } 74 } 75