1 /*
2  * Copyright 2020 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 // @exportToFramework:skipFile()
17 package androidx.appsearch.cts.app;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.junit.Assume.assumeTrue;
22 
23 import android.content.Context;
24 import android.content.pm.ModuleInfo;
25 import android.content.pm.PackageInfo;
26 import android.content.pm.PackageManager;
27 import android.os.Build;
28 
29 import androidx.appsearch.app.AppSearchSession;
30 import androidx.appsearch.app.Features;
31 import androidx.appsearch.platformstorage.PlatformStorage;
32 import androidx.test.core.app.ApplicationProvider;
33 import androidx.test.filters.SdkSuppress;
34 
35 import com.google.common.util.concurrent.ListenableFuture;
36 
37 import org.jspecify.annotations.NonNull;
38 import org.junit.Test;
39 
40 import java.util.List;
41 import java.util.concurrent.ExecutorService;
42 
43 @SdkSuppress(minSdkVersion = Build.VERSION_CODES.S)
44 public class AppSearchSessionPlatformCtsTest extends AppSearchSessionCtsTestBase {
45     static final String APPSEARCH_MAINLINE_MODULE_NAME = "com.google.android.appsearch";
46 
47     @Override
createSearchSessionAsync(@onNull String dbName)48     protected ListenableFuture<AppSearchSession> createSearchSessionAsync(@NonNull String dbName) {
49         Context context = ApplicationProvider.getApplicationContext();
50         return PlatformStorage.createSearchSessionAsync(
51                 new PlatformStorage.SearchContext.Builder(context, dbName).build());
52     }
53 
54     @Override
createSearchSessionAsync( @onNull String dbName, @NonNull ExecutorService executor)55     protected ListenableFuture<AppSearchSession> createSearchSessionAsync(
56             @NonNull String dbName, @NonNull ExecutorService executor) {
57         Context context = ApplicationProvider.getApplicationContext();
58         return PlatformStorage.createSearchSessionAsync(
59                 new PlatformStorage.SearchContext.Builder(context, dbName)
60                         .setWorkerExecutor(executor).build());
61     }
62 
63     @Test
testFeaturesSupported()64     public void testFeaturesSupported() throws Exception {
65         Context context = ApplicationProvider.getApplicationContext();
66         AppSearchSession db2 = PlatformStorage.createSearchSessionAsync(
67                 new PlatformStorage.SearchContext.Builder(context, DB_NAME_2).build()).get();
68         assertThat(db2.getFeatures().isFeatureSupported(
69                 Features.SEARCH_RESULT_MATCH_INFO_SUBMATCH))
70                 .isEqualTo(Build.VERSION.SDK_INT >= 33);
71         assertThat(db2.getFeatures().isFeatureSupported(
72                 Features.GLOBAL_SEARCH_SESSION_REGISTER_OBSERVER_CALLBACK))
73                 .isEqualTo(Build.VERSION.SDK_INT >= 33);
74         assertThat(db2.getFeatures().isFeatureSupported(
75                 Features.GLOBAL_SEARCH_SESSION_GET_SCHEMA))
76                 .isEqualTo(Build.VERSION.SDK_INT >= 33);
77         assertThat(db2.getFeatures().isFeatureSupported(
78                 Features.GLOBAL_SEARCH_SESSION_GET_BY_ID))
79                 .isEqualTo(Build.VERSION.SDK_INT >= 33);
80         assertThat(db2.getFeatures().isFeatureSupported(
81                 Features.ADD_PERMISSIONS_AND_GET_VISIBILITY))
82                 .isEqualTo(Build.VERSION.SDK_INT >= 33);
83     }
84 
85     @Test
86     @Override
testPutDocuments_emptyBytesAndDocuments()87     public void testPutDocuments_emptyBytesAndDocuments() throws Exception {
88         // b/185441119 was fixed in Android T, this test will fail on S_V2 devices and below.
89         assumeTrue(Build.VERSION.SDK_INT >= 33);
90         super.testPutDocuments_emptyBytesAndDocuments();
91     }
92 
93     @Override
94     @Test
testSetSchema_addIndexedNestedDocumentProperty()95     public void testSetSchema_addIndexedNestedDocumentProperty() throws Exception {
96         long appsearchVersionCode = 0;
97         PackageManager pm = ApplicationProvider.getApplicationContext().getPackageManager();
98         List<ModuleInfo> modules = pm.getInstalledModules(0);
99         for (int i = 0; i < modules.size(); ++i) {
100             String packageName = modules.get(i).getPackageName();
101             if (packageName.equals(APPSEARCH_MAINLINE_MODULE_NAME)) {
102                 PackageInfo pInfo = pm.getPackageInfo(packageName, PackageManager.MATCH_APEX);
103                 appsearchVersionCode = pInfo.getLongVersionCode();
104             }
105         }
106         // This is a test for b/291019114. The bug was only fixed in mainline module
107         // 'aml_ase_340913000', so this test will fail on any versions below that.
108         assumeTrue(appsearchVersionCode >= 340913000);
109         super.testSetSchema_addIndexedNestedDocumentProperty();
110     }
111 
112     @Override
113     @Test
testPutLargeDocumentBatch()114     public void testPutLargeDocumentBatch() throws Exception {
115         // b/185441119 was fixed in Android T, this test will fail on S_V2 devices and below.
116         assumeTrue(Build.VERSION.SDK_INT >= 33);
117         super.testPutLargeDocumentBatch();
118     }
119 
120     @Override
121     @Test
testSetSchemaWithIncompatibleNestedSchema()122     public void testSetSchemaWithIncompatibleNestedSchema() throws Exception {
123         // TODO(b/230879098) This bug was fixed in Android T, but will currently fail on S_V2
124         // devices and below. However, we could implement a workaround in platform-storage.
125         // Implement that workaround and enable on S and S_V2.
126         assumeTrue(Build.VERSION.SDK_INT >= 33);
127         super.testSetSchemaWithIncompatibleNestedSchema();
128     }
129 
130     @Override
131     @Test
testEmojiSnippet()132     public void testEmojiSnippet() throws Exception {
133         // b/229770338 was fixed in Android T, this test will fail on S_V2 devices and below.
134         assumeTrue(Build.VERSION.SDK_INT >= 33);
135         super.testEmojiSnippet();
136     }
137 
138     // TODO(b/256022027) Remove this overridden test once the change to setMaxJoinedResultCount
139     // is synced over into framework.
140     @Override
141     @Test
testSimpleJoin()142     public void testSimpleJoin() throws Exception { }
143 
144     // TODO(b/256022027) Remove this overridden test once the change to rename
145     //  `this.childrenScores()` to `this.childrenRankingSignals()` is synced to udc-dev.
146     @Override
147     @Test
testQuery_invalidAdvancedRankingWithChildrenRankingSignals()148     public void testQuery_invalidAdvancedRankingWithChildrenRankingSignals() throws Exception { }
149 
150     // TODO(b/256022027) Remove this overridden test once the change to rename
151     //  `this.childrenScores()` to `this.childrenRankingSignals()` is synced to udc-dev.
152     @Override
153     @Test
testQuery_advancedRankingWithJoin()154     public void testQuery_advancedRankingWithJoin() throws Exception { }
155 }
156