1 /*
2  * 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 
17 package android.location.cts.fine;
18 
19 import static org.junit.Assert.assertNotNull;
20 import static org.mockito.ArgumentMatchers.anyString;
21 import static org.mockito.Mockito.never;
22 import static org.mockito.Mockito.verify;
23 
24 import android.content.Context;
25 import android.location.Address;
26 import android.location.provider.ForwardGeocodeRequest;
27 import android.location.provider.GeocodeProviderBase;
28 import android.location.provider.IGeocodeCallback;
29 import android.location.provider.IGeocodeProvider;
30 import android.location.provider.ReverseGeocodeRequest;
31 import android.os.OutcomeReceiver;
32 
33 import androidx.test.core.app.ApplicationProvider;
34 import androidx.test.ext.junit.runners.AndroidJUnit4;
35 
36 import com.android.compatibility.common.util.ApiTest;
37 
38 import org.junit.Before;
39 import org.junit.Rule;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.Mock;
43 import org.mockito.junit.MockitoJUnit;
44 import org.mockito.junit.MockitoRule;
45 
46 import java.util.Collections;
47 import java.util.List;
48 import java.util.Locale;
49 
50 @RunWith(AndroidJUnit4.class)
51 public class GeocodeProviderBaseTest {
52 
53     private static final String TAG = "GeocodeProviderBaseTest";
54 
55     @Rule public final MockitoRule mocks = MockitoJUnit.rule();
56 
57     private Context mContext;
58 
59     @Mock private IGeocodeCallback mMock;
60 
61     private MyProvider mGeocodeProvider;
62 
63     @Before
setUp()64     public void setUp() throws Exception {
65         mContext = ApplicationProvider.getApplicationContext();
66         mGeocodeProvider = new MyProvider(mContext, TAG);
67     }
68 
69     @ApiTest(apis = "android.location.provider.GeocodeProviderBase#onReverseGeocode")
70     @Test
testReverseGeocode()71     public void testReverseGeocode() throws Exception {
72         ReverseGeocodeRequest request =
73                 new ReverseGeocodeRequest.Builder(1, 2, 3, Locale.CANADA, 4, "package")
74                         .setCallingAttributionTag("attribution")
75                         .build();
76         mGeocodeProvider.asProvider().reverseGeocode(request, mMock);
77         verify(mMock).onResults(Collections.emptyList());
78         verify(mMock, never()).onError(anyString());
79     }
80 
81     @ApiTest(apis = "android.location.provider.GeocodeProviderBase#onForwardGeocode")
82     @Test
testGetFromLocationName()83     public void testGetFromLocationName() throws Exception {
84         ForwardGeocodeRequest request =
85                 new ForwardGeocodeRequest.Builder(
86                                 "location", 1, 2, 3, 4, 5, Locale.CANADA, 4, "package")
87                         .setCallingAttributionTag("attribution")
88                         .build();
89         mGeocodeProvider.asProvider().forwardGeocode(request, mMock);
90         verify(mMock).onResults(Collections.emptyList());
91         verify(mMock, never()).onError(anyString());
92     }
93 
94 
95 
96     @ApiTest(apis = "android.location.provider.GeocodeProviderBase#getBinder")
97     @Test
testGetBinder()98     public void testGetBinder() {
99         assertNotNull(mGeocodeProvider.getBinder());
100     }
101 
102     private static class MyProvider extends GeocodeProviderBase {
103 
MyProvider(Context context, String tag)104         MyProvider(Context context, String tag) {
105             super(context, tag);
106         }
107 
asProvider()108         public IGeocodeProvider asProvider() {
109             return IGeocodeProvider.Stub.asInterface(getBinder());
110         }
111 
112         @Override
onReverseGeocode( ReverseGeocodeRequest request, OutcomeReceiver<List<Address>, Throwable> callback)113         public void onReverseGeocode(
114                 ReverseGeocodeRequest request, OutcomeReceiver<List<Address>, Throwable> callback) {
115             callback.onResult(Collections.emptyList());
116         }
117 
118         @Override
onForwardGeocode( ForwardGeocodeRequest request, OutcomeReceiver<List<Address>, Throwable> callback)119         public void onForwardGeocode(
120                 ForwardGeocodeRequest request, OutcomeReceiver<List<Address>, Throwable> callback) {
121             callback.onResult(Collections.emptyList());
122         }
123     }
124 }
125