1 /*
2  * Copyright (C) 2021 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.b;
18 
19 import static com.android.server.appsearch.testing.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 
27 import androidx.test.ext.junit.runners.AndroidJUnit4;
28 
29 import com.android.server.appsearch.testing.GlobalSearchSessionShimImpl;
30 
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 
35 import java.util.List;
36 
37 @RunWith(AndroidJUnit4.class)
38 public class AppSearchDeviceTest {
39 
40     private static final String NAMESPACE = "namespace";
41     private static final String ID = "id";
42     private static final GenericDocument DOCUMENT =
43             new GenericDocument.Builder<>(NAMESPACE, ID, "testSchema")
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.createGlobalSearchSession().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                                 .build()));
61         assertThat(outDocuments).containsExactly(DOCUMENT);
62     }
63 
64     @Test
testGlobalGetDocuments_nonexist()65     public void testGlobalGetDocuments_nonexist() throws Exception {
66         List<GenericDocument> outDocuments = convertSearchResultsToDocuments(
67                 mGlobalSearch.search(/*queryExpression=*/"",
68                         new SearchSpec.Builder().setTermMatch(SearchSpec.TERM_MATCH_PREFIX)
69                                 .build()));
70         assertThat(outDocuments).isEmpty();
71     }
72 }
73