• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
17 package android.app.appsearch.testutil;
18 
19 import android.annotation.NonNull;
20 import android.app.appsearch.AppSearchResult;
21 import android.app.appsearch.SearchResult;
22 import android.app.appsearch.SearchResults;
23 import android.app.appsearch.SearchResultsShim;
24 
25 import com.google.common.util.concurrent.Futures;
26 import com.google.common.util.concurrent.ListenableFuture;
27 import com.google.common.util.concurrent.SettableFuture;
28 
29 import java.util.List;
30 import java.util.Objects;
31 import java.util.concurrent.Executor;
32 
33 /**
34  * This test class adapts the AppSearch Framework API to ListenableFuture, so it can be tested via
35  * a consistent interface.
36  * @hide
37  */
38 public class SearchResultsShimImpl implements SearchResultsShim {
39     private final Executor mExecutor;
40     private final SearchResults mSearchResults;
41 
SearchResultsShimImpl(@onNull SearchResults searchResults, @NonNull Executor executor)42     SearchResultsShimImpl(@NonNull SearchResults searchResults, @NonNull Executor executor) {
43         mExecutor = Objects.requireNonNull(executor);
44         mSearchResults = Objects.requireNonNull(searchResults);
45     }
46 
47     @NonNull
getNextPageAsync()48     public ListenableFuture<List<SearchResult>> getNextPageAsync() {
49         SettableFuture<AppSearchResult<List<SearchResult>>> future = SettableFuture.create();
50         mSearchResults.getNextPage(mExecutor, future::set);
51         return Futures.transform(future, AppSearchResult::getResultValue, mExecutor);
52     }
53 
54     @Override
close()55     public void close() {
56         mSearchResults.close();
57     }
58 }
59