Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 1 | /* |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 2 | * Copyright (C) 2019 The Android Open Source Project |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 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 |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 14 | * limitations under the License. |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | package com.android.server; |
| 18 | |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 19 | import static org.mockito.ArgumentMatchers.any; |
| 20 | import static org.mockito.ArgumentMatchers.anyLong; |
| 21 | import static org.mockito.Mockito.doAnswer; |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 22 | import static org.mockito.Mockito.doReturn; |
| 23 | import static org.mockito.Mockito.when; |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 24 | |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 25 | import android.content.Context; |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 26 | import android.content.res.Resources; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 27 | import android.location.Country; |
| 28 | import android.location.CountryListener; |
| 29 | import android.location.ICountryListener; |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 30 | import android.os.Handler; |
| 31 | import android.os.Looper; |
| 32 | import android.os.Message; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 33 | import android.os.RemoteException; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 34 | |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 35 | import androidx.test.core.app.ApplicationProvider; |
| 36 | |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 37 | import com.android.internal.R; |
| 38 | import com.android.server.location.ComprehensiveCountryDetector; |
| 39 | import com.android.server.location.CustomCountryDetectorTestClass; |
| 40 | |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 41 | import com.google.common.truth.Expect; |
| 42 | |
| 43 | import org.junit.Before; |
| 44 | import org.junit.BeforeClass; |
| 45 | import org.junit.Rule; |
| 46 | import org.junit.Test; |
| 47 | import org.junit.runner.RunWith; |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 48 | import org.mockito.Mock; |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 49 | import org.mockito.Spy; |
| 50 | import org.mockito.junit.MockitoJUnitRunner; |
| 51 | |
| 52 | @RunWith(MockitoJUnitRunner.class) |
| 53 | public class CountryDetectorServiceTest { |
| 54 | |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 55 | 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 Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 60 | private static class CountryListenerTester extends ICountryListener.Stub { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 61 | private Country mCountry; |
| 62 | |
| 63 | @Override |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 64 | public void onCountryDetected(Country country) { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 65 | mCountry = country; |
| 66 | } |
| 67 | |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 68 | Country getCountry() { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 69 | return mCountry; |
| 70 | } |
| 71 | |
| 72 | public boolean isNotified() { |
| 73 | return mCountry != null; |
| 74 | } |
| 75 | } |
| 76 | |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 77 | private static class CountryDetectorServiceTester extends CountryDetectorService { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 78 | private CountryListener mListener; |
| 79 | |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 80 | CountryDetectorServiceTester(Context context, Handler handler) { |
| 81 | super(context, handler); |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 82 | } |
| 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 Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 94 | boolean isListenerSet() { |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 95 | return mListener != null; |
| 96 | } |
| 97 | } |
| 98 | |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 99 | @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 Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 104 | private CountryDetectorServiceTester mCountryDetectorService; |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 105 | |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 106 | @BeforeClass |
| 107 | public static void oneTimeInitialization() { |
| 108 | if (Looper.myLooper() == null) { |
| 109 | Looper.prepare(); |
Bai Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 110 | } |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 111 | } |
| 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 Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 123 | |
| 124 | doReturn(mResources).when(mContext).getResources(); |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Test |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 128 | public void addCountryListener_validListener_listenerAdded() throws RemoteException { |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 129 | 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 Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 139 | public void removeCountryListener_validListener_listenerRemoved() throws RemoteException { |
Christophe Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 140 | 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 Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 150 | @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 Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 166 | @Test |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 167 | 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 Koessler | 5036168 | 2019-12-17 16:40:47 -0800 | [diff] [blame] | 175 | 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 Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 190 | } |
Christophe Koessler | 3414760 | 2019-12-20 14:30:35 -0800 | [diff] [blame] | 191 | |
| 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 Tao | a58a875 | 2010-07-13 15:32:16 +0800 | [diff] [blame] | 213 | } |