blob: d5483ffa5445009357e844e9f7b63d3785e5e8ad [file] [log] [blame]
Bai Taoa58a8752010-07-13 15:32:16 +08001/*
Christophe Koessler50361682019-12-17 16:40:47 -08002 * Copyright (C) 2019 The Android Open Source Project
Bai Taoa58a8752010-07-13 15:32:16 +08003 *
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
Christophe Koessler50361682019-12-17 16:40:47 -080014 * limitations under the License.
Bai Taoa58a8752010-07-13 15:32:16 +080015 */
16
17package com.android.server;
18
Christophe Koessler50361682019-12-17 16:40:47 -080019import static org.mockito.ArgumentMatchers.any;
20import static org.mockito.ArgumentMatchers.anyLong;
21import static org.mockito.Mockito.doAnswer;
Christophe Koessler34147602019-12-20 14:30:35 -080022import static org.mockito.Mockito.doReturn;
23import static org.mockito.Mockito.when;
Christophe Koessler50361682019-12-17 16:40:47 -080024
Bai Taoa58a8752010-07-13 15:32:16 +080025import android.content.Context;
Christophe Koessler34147602019-12-20 14:30:35 -080026import android.content.res.Resources;
Bai Taoa58a8752010-07-13 15:32:16 +080027import android.location.Country;
28import android.location.CountryListener;
29import android.location.ICountryListener;
Christophe Koessler50361682019-12-17 16:40:47 -080030import android.os.Handler;
31import android.os.Looper;
32import android.os.Message;
Bai Taoa58a8752010-07-13 15:32:16 +080033import android.os.RemoteException;
Bai Taoa58a8752010-07-13 15:32:16 +080034
Christophe Koessler50361682019-12-17 16:40:47 -080035import androidx.test.core.app.ApplicationProvider;
36
Christophe Koessler34147602019-12-20 14:30:35 -080037import com.android.internal.R;
38import com.android.server.location.ComprehensiveCountryDetector;
39import com.android.server.location.CustomCountryDetectorTestClass;
40
Christophe Koessler50361682019-12-17 16:40:47 -080041import com.google.common.truth.Expect;
42
43import org.junit.Before;
44import org.junit.BeforeClass;
45import org.junit.Rule;
46import org.junit.Test;
47import org.junit.runner.RunWith;
Christophe Koessler34147602019-12-20 14:30:35 -080048import org.mockito.Mock;
Christophe Koessler50361682019-12-17 16:40:47 -080049import org.mockito.Spy;
50import org.mockito.junit.MockitoJUnitRunner;
51
52@RunWith(MockitoJUnitRunner.class)
53public class CountryDetectorServiceTest {
54
Christophe Koessler34147602019-12-20 14:30:35 -080055 private static final String VALID_CUSTOM_TEST_CLASS =
56 "com.android.server.location.CustomCountryDetectorTestClass";
57 private static final String INVALID_CUSTOM_TEST_CLASS =
58 "com.android.server.location.MissingCountryDetectorTestClass";
59
Christophe Koessler50361682019-12-17 16:40:47 -080060 private static class CountryListenerTester extends ICountryListener.Stub {
Bai Taoa58a8752010-07-13 15:32:16 +080061 private Country mCountry;
62
63 @Override
Christophe Koessler50361682019-12-17 16:40:47 -080064 public void onCountryDetected(Country country) {
Bai Taoa58a8752010-07-13 15:32:16 +080065 mCountry = country;
66 }
67
Christophe Koessler50361682019-12-17 16:40:47 -080068 Country getCountry() {
Bai Taoa58a8752010-07-13 15:32:16 +080069 return mCountry;
70 }
71
72 public boolean isNotified() {
73 return mCountry != null;
74 }
75 }
76
Christophe Koessler50361682019-12-17 16:40:47 -080077 private static class CountryDetectorServiceTester extends CountryDetectorService {
Bai Taoa58a8752010-07-13 15:32:16 +080078 private CountryListener mListener;
79
Christophe Koessler50361682019-12-17 16:40:47 -080080 CountryDetectorServiceTester(Context context, Handler handler) {
81 super(context, handler);
Bai Taoa58a8752010-07-13 15:32:16 +080082 }
83
84 @Override
85 public void notifyReceivers(Country country) {
86 super.notifyReceivers(country);
87 }
88
89 @Override
90 protected void setCountryListener(final CountryListener listener) {
91 mListener = listener;
92 }
93
Christophe Koessler50361682019-12-17 16:40:47 -080094 boolean isListenerSet() {
Bai Taoa58a8752010-07-13 15:32:16 +080095 return mListener != null;
96 }
97 }
98
Christophe Koessler34147602019-12-20 14:30:35 -080099 @Rule public final Expect expect = Expect.create();
100 @Spy private Context mContext = ApplicationProvider.getApplicationContext();
101 @Spy private Handler mHandler = new Handler(Looper.myLooper());
102 @Mock private Resources mResources;
103
Christophe Koessler50361682019-12-17 16:40:47 -0800104 private CountryDetectorServiceTester mCountryDetectorService;
Bai Taoa58a8752010-07-13 15:32:16 +0800105
Christophe Koessler50361682019-12-17 16:40:47 -0800106 @BeforeClass
107 public static void oneTimeInitialization() {
108 if (Looper.myLooper() == null) {
109 Looper.prepare();
Bai Taoa58a8752010-07-13 15:32:16 +0800110 }
Christophe Koessler50361682019-12-17 16:40:47 -0800111 }
112
113 @Before
114 public void setUp() {
115 mCountryDetectorService = new CountryDetectorServiceTester(mContext, mHandler);
116
117 // Immediately invoke run on the Runnable posted to the handler
118 doAnswer(invocation -> {
119 Message message = invocation.getArgument(0);
120 message.getCallback().run();
121 return true;
122 }).when(mHandler).sendMessageAtTime(any(Message.class), anyLong());
Christophe Koessler34147602019-12-20 14:30:35 -0800123
124 doReturn(mResources).when(mContext).getResources();
Christophe Koessler50361682019-12-17 16:40:47 -0800125 }
126
127 @Test
Christophe Koessler34147602019-12-20 14:30:35 -0800128 public void addCountryListener_validListener_listenerAdded() throws RemoteException {
Christophe Koessler50361682019-12-17 16:40:47 -0800129 CountryListenerTester countryListener = new CountryListenerTester();
130
131 mCountryDetectorService.systemRunning();
132 expect.that(mCountryDetectorService.isListenerSet()).isFalse();
133 mCountryDetectorService.addCountryListener(countryListener);
134
135 expect.that(mCountryDetectorService.isListenerSet()).isTrue();
136 }
137
138 @Test
Christophe Koessler34147602019-12-20 14:30:35 -0800139 public void removeCountryListener_validListener_listenerRemoved() throws RemoteException {
Christophe Koessler50361682019-12-17 16:40:47 -0800140 CountryListenerTester countryListener = new CountryListenerTester();
141
142 mCountryDetectorService.systemRunning();
143 mCountryDetectorService.addCountryListener(countryListener);
144 expect.that(mCountryDetectorService.isListenerSet()).isTrue();
145 mCountryDetectorService.removeCountryListener(countryListener);
146
147 expect.that(mCountryDetectorService.isListenerSet()).isFalse();
148 }
149
Christophe Koessler34147602019-12-20 14:30:35 -0800150 @Test(expected = RemoteException.class)
151 public void addCountryListener_serviceNotReady_throwsException() throws RemoteException {
152 CountryListenerTester countryListener = new CountryListenerTester();
153
154 expect.that(mCountryDetectorService.isSystemReady()).isFalse();
155 mCountryDetectorService.addCountryListener(countryListener);
156 }
157
158 @Test(expected = RemoteException.class)
159 public void removeCountryListener_serviceNotReady_throwsException() throws RemoteException {
160 CountryListenerTester countryListener = new CountryListenerTester();
161
162 expect.that(mCountryDetectorService.isSystemReady()).isFalse();
163 mCountryDetectorService.removeCountryListener(countryListener);
164 }
165
Christophe Koessler50361682019-12-17 16:40:47 -0800166 @Test
Christophe Koessler34147602019-12-20 14:30:35 -0800167 public void detectCountry_serviceNotReady_returnNull() {
168 expect.that(mCountryDetectorService.isSystemReady()).isFalse();
169
170 expect.that(mCountryDetectorService.detectCountry()).isNull();
171 }
172
173 @Test
174 public void notifyReceivers_twoListenersRegistered_bothNotified() throws RemoteException {
Christophe Koessler50361682019-12-17 16:40:47 -0800175 CountryListenerTester countryListenerA = new CountryListenerTester();
176 CountryListenerTester countryListenerB = new CountryListenerTester();
177 Country country = new Country("US", Country.COUNTRY_SOURCE_NETWORK);
178
179 mCountryDetectorService.systemRunning();
180 mCountryDetectorService.addCountryListener(countryListenerA);
181 mCountryDetectorService.addCountryListener(countryListenerB);
182 expect.that(countryListenerA.isNotified()).isFalse();
183 expect.that(countryListenerB.isNotified()).isFalse();
184 mCountryDetectorService.notifyReceivers(country);
185
186 expect.that(countryListenerA.isNotified()).isTrue();
187 expect.that(countryListenerB.isNotified()).isTrue();
188 expect.that(countryListenerA.getCountry().equalsIgnoreSource(country)).isTrue();
189 expect.that(countryListenerB.getCountry().equalsIgnoreSource(country)).isTrue();
Bai Taoa58a8752010-07-13 15:32:16 +0800190 }
Christophe Koessler34147602019-12-20 14:30:35 -0800191
192 @Test
193 public void initialize_deviceWithCustomDetector_useCustomDetectorClass() {
194 when(mResources.getString(R.string.config_customCountryDetector))
195 .thenReturn(VALID_CUSTOM_TEST_CLASS);
196
197 mCountryDetectorService.initialize();
198
199 expect.that(mCountryDetectorService.getCountryDetector())
200 .isInstanceOf(CustomCountryDetectorTestClass.class);
201 }
202
203 @Test
204 public void initialize_deviceWithInvalidCustomDetector_useDefaultDetector() {
205 when(mResources.getString(R.string.config_customCountryDetector))
206 .thenReturn(INVALID_CUSTOM_TEST_CLASS);
207
208 mCountryDetectorService.initialize();
209
210 expect.that(mCountryDetectorService.getCountryDetector())
211 .isInstanceOf(ComprehensiveCountryDetector.class);
212 }
Bai Taoa58a8752010-07-13 15:32:16 +0800213}