• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
<lambda>null2  * Copyright (C) 2023 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 package com.android.server.appfunctions
17 
18 import android.app.appfunctions.AppFunctionRuntimeMetadata
19 import android.app.appfunctions.AppFunctionRuntimeMetadata.createAppFunctionRuntimeSchema
20 import android.app.appfunctions.AppFunctionRuntimeMetadata.createParentAppFunctionRuntimeSchema
21 import android.app.appsearch.AppSearchManager
22 import android.app.appsearch.AppSearchManager.SearchContext
23 import android.app.appsearch.PutDocumentsRequest
24 import android.app.appsearch.SetSchemaRequest
25 import android.app.appsearch.observer.DocumentChangeInfo
26 import android.app.appsearch.observer.ObserverCallback
27 import android.app.appsearch.observer.ObserverSpec
28 import android.app.appsearch.observer.SchemaChangeInfo
29 import androidx.test.platform.app.InstrumentationRegistry
30 import com.android.internal.infra.AndroidFuture
31 import com.google.common.truth.Truth.assertThat
32 import com.google.common.util.concurrent.MoreExecutors
33 import org.junit.After
34 import org.junit.Before
35 import org.junit.Test
36 import org.junit.runner.RunWith
37 import org.junit.runners.JUnit4
38 
39 @RunWith(JUnit4::class)
40 class FutureGlobalSearchSessionTest {
41     private val context = InstrumentationRegistry.getInstrumentation().targetContext
42     private val appSearchManager = context.getSystemService(AppSearchManager::class.java)
43     private val testExecutor = MoreExecutors.directExecutor()
44 
45     @Before
46     @After
47     fun clearData() {
48         val searchContext = SearchContext.Builder(TEST_DB).build()
49         FutureAppSearchSessionImpl(appSearchManager, testExecutor, searchContext).use {
50             val setSchemaRequest = SetSchemaRequest.Builder().setForceOverride(true).build()
51             it.setSchema(setSchemaRequest).get()
52         }
53     }
54 
55     @Test
56     fun registerDocumentChangeObserverCallback() {
57         val packageObserverSpec: ObserverSpec =
58             ObserverSpec.Builder()
59                 .addFilterSchemas(
60                     AppFunctionRuntimeMetadata.getRuntimeSchemaNameForPackage(TEST_TARGET_PKG_NAME)
61                 )
62                 .build()
63         val settableDocumentChangeInfo: AndroidFuture<DocumentChangeInfo> = AndroidFuture()
64         val observer: ObserverCallback =
65             object : ObserverCallback {
66                 override fun onSchemaChanged(changeInfo: SchemaChangeInfo) {}
67 
68                 override fun onDocumentChanged(changeInfo: DocumentChangeInfo) {
69                     settableDocumentChangeInfo.complete(changeInfo)
70                 }
71             }
72         val futureGlobalSearchSession = FutureGlobalSearchSession(appSearchManager, testExecutor)
73 
74         val registerPackageObserver: Void? =
75             futureGlobalSearchSession
76                 .registerObserverCallbackAsync(
77                     TEST_TARGET_PKG_NAME,
78                     packageObserverSpec,
79                     testExecutor,
80                     observer,
81                 )
82                 .get()
83         assertThat(registerPackageObserver).isNull()
84         // Trigger document change
85         val searchContext = SearchContext.Builder(TEST_DB).build()
86         FutureAppSearchSessionImpl(appSearchManager, testExecutor, searchContext).use { session ->
87             val setSchemaRequest =
88                 SetSchemaRequest.Builder()
89                     .addSchemas(
90                         createParentAppFunctionRuntimeSchema(),
91                         createAppFunctionRuntimeSchema(TEST_TARGET_PKG_NAME),
92                     )
93                     .build()
94             val schema = session.setSchema(setSchemaRequest)
95             assertThat(schema.get()).isNotNull()
96             val appFunctionRuntimeMetadata =
97                 AppFunctionRuntimeMetadata.Builder(TEST_TARGET_PKG_NAME, TEST_FUNCTION_ID).build()
98             val putDocumentsRequest: PutDocumentsRequest =
99                 PutDocumentsRequest.Builder()
100                     .addGenericDocuments(appFunctionRuntimeMetadata)
101                     .build()
102             val putResult = session.put(putDocumentsRequest).get()
103             assertThat(putResult.isSuccess).isTrue()
104         }
105         assertThat(
106                 settableDocumentChangeInfo
107                     .get()
108                     .changedDocumentIds
109                     .contains(
110                         AppFunctionRuntimeMetadata.getDocumentIdForAppFunction(
111                             TEST_TARGET_PKG_NAME,
112                             TEST_FUNCTION_ID,
113                         )
114                     )
115             )
116             .isTrue()
117     }
118 
119     private companion object {
120         const val TEST_DB: String = "test_db"
121         const val TEST_TARGET_PKG_NAME = "com.android.frameworks.appfunctionstests"
122         const val TEST_FUNCTION_ID: String = "print"
123     }
124 }
125