Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 com.android.server; |
| 18 | |
David Su | bc81782 | 2020-01-13 17:12:13 -0800 | [diff] [blame] | 19 | import static android.net.NetworkScoreManager.SCORE_FILTER_NONE; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 20 | |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 21 | import static junit.framework.Assert.assertEquals; |
| 22 | import static junit.framework.Assert.assertFalse; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 23 | import static junit.framework.Assert.assertTrue; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 24 | import static junit.framework.Assert.fail; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 25 | |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 26 | import static org.mockito.Matchers.any; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 27 | import static org.mockito.Matchers.anyListOf; |
| 28 | import static org.mockito.Matchers.anyString; |
| 29 | import static org.mockito.Matchers.eq; |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 30 | import static org.mockito.Mockito.atLeastOnce; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 31 | import static org.mockito.Mockito.doThrow; |
David Su | 543edf2 | 2020-01-07 16:07:02 -0800 | [diff] [blame] | 32 | import static org.mockito.Mockito.mock; |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 33 | import static org.mockito.Mockito.never; |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 34 | import static org.mockito.Mockito.timeout; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 35 | import static org.mockito.Mockito.times; |
| 36 | import static org.mockito.Mockito.verify; |
| 37 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 38 | import static org.mockito.Mockito.verifyZeroInteractions; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 39 | import static org.mockito.Mockito.when; |
| 40 | |
| 41 | import android.Manifest.permission; |
| 42 | import android.content.ComponentName; |
| 43 | import android.content.ContentResolver; |
| 44 | import android.content.Context; |
| 45 | import android.content.Intent; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 46 | import android.content.pm.PackageManager; |
| 47 | import android.content.res.Resources; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 48 | import android.net.INetworkRecommendationProvider; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 49 | import android.net.INetworkScoreCache; |
| 50 | import android.net.NetworkKey; |
Jeremy Joslin | 586d36e | 2016-12-29 15:00:23 -0800 | [diff] [blame] | 51 | import android.net.NetworkScoreManager; |
Jeremy Joslin | f621bc9 | 2017-02-16 11:11:57 -0800 | [diff] [blame] | 52 | import android.net.NetworkScorerAppData; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 53 | import android.net.ScoredNetwork; |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 54 | import android.net.Uri; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 55 | import android.net.WifiKey; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 56 | import android.net.wifi.ScanResult; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 57 | import android.net.wifi.WifiConfiguration; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 58 | import android.net.wifi.WifiInfo; |
David Su | 1cdbf62 | 2019-11-13 15:06:41 -0800 | [diff] [blame] | 59 | import android.net.wifi.WifiManager; |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 60 | import android.os.Binder; |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 61 | import android.os.Handler; |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 62 | import android.os.HandlerThread; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 63 | import android.os.IBinder; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 64 | import android.os.Looper; |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 65 | import android.os.Message; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 66 | import android.os.RemoteException; |
| 67 | import android.os.UserHandle; |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 68 | import android.provider.Settings; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 69 | |
| 70 | import androidx.test.InstrumentationRegistry; |
| 71 | import androidx.test.filters.MediumTest; |
| 72 | import androidx.test.runner.AndroidJUnit4; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 73 | |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 74 | import com.android.server.devicepolicy.MockUtils; |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 75 | import com.android.server.pm.permission.PermissionManagerServiceInternal; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 76 | |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 77 | import com.google.android.collect.Lists; |
| 78 | |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 79 | import org.junit.After; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 80 | import org.junit.Before; |
| 81 | import org.junit.Test; |
| 82 | import org.junit.runner.RunWith; |
| 83 | import org.mockito.ArgumentCaptor; |
| 84 | import org.mockito.Captor; |
| 85 | import org.mockito.Mock; |
| 86 | import org.mockito.MockitoAnnotations; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 87 | |
| 88 | import java.io.FileDescriptor; |
| 89 | import java.io.PrintWriter; |
| 90 | import java.io.StringWriter; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 91 | import java.util.ArrayList; |
| 92 | import java.util.Collections; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 93 | import java.util.List; |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 94 | import java.util.concurrent.CountDownLatch; |
| 95 | import java.util.concurrent.TimeUnit; |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 96 | import java.util.function.UnaryOperator; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 97 | |
| 98 | /** |
| 99 | * Tests for {@link NetworkScoreService}. |
| 100 | */ |
| 101 | @RunWith(AndroidJUnit4.class) |
| 102 | @MediumTest |
| 103 | public class NetworkScoreServiceTest { |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 104 | private static final String SSID = "ssid"; |
| 105 | private static final String SSID_2 = "ssid_2"; |
| 106 | private static final String SSID_3 = "ssid_3"; |
Stephen Chen | fde900d | 2017-02-14 16:40:21 -0800 | [diff] [blame] | 107 | private static final String INVALID_BSSID = "invalid_bssid"; |
Jeremy Joslin | 37e877b | 2017-02-02 11:06:14 -0800 | [diff] [blame] | 108 | private static final ComponentName RECOMMENDATION_SERVICE_COMP = |
| 109 | new ComponentName("newPackageName", "newScoringServiceClass"); |
Stephen Chen | 8b1339a | 2017-02-28 18:11:34 -0800 | [diff] [blame] | 110 | private static final String RECOMMENDATION_SERVICE_LABEL = "Test Recommendation Service"; |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 111 | private static final ComponentName USE_WIFI_ENABLE_ACTIVITY_COMP = |
| 112 | new ComponentName("useWifiPackageName", "enableUseWifiActivityClass"); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 113 | private static final ScoredNetwork SCORED_NETWORK = |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 114 | new ScoredNetwork(new NetworkKey(new WifiKey(quote(SSID), "00:00:00:00:00:00")), |
| 115 | null /* rssiCurve*/); |
| 116 | private static final ScoredNetwork SCORED_NETWORK_2 = |
| 117 | new ScoredNetwork(new NetworkKey(new WifiKey(quote(SSID_2), "00:00:00:00:00:00")), |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 118 | null /* rssiCurve*/); |
Amin Shaikh | d601360 | 2017-03-24 15:52:32 -0700 | [diff] [blame] | 119 | private static final String NETWORK_AVAILABLE_NOTIFICATION_CHANNEL_ID = |
| 120 | "networkAvailableNotificationChannelId"; |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 121 | private static final NetworkScorerAppData NEW_SCORER = new NetworkScorerAppData( |
Stephen Chen | 8b1339a | 2017-02-28 18:11:34 -0800 | [diff] [blame] | 122 | 1, RECOMMENDATION_SERVICE_COMP, RECOMMENDATION_SERVICE_LABEL, |
Amin Shaikh | d601360 | 2017-03-24 15:52:32 -0700 | [diff] [blame] | 123 | USE_WIFI_ENABLE_ACTIVITY_COMP, NETWORK_AVAILABLE_NOTIFICATION_CHANNEL_ID); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 124 | |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 125 | @Mock private NetworkScorerAppManager mNetworkScorerAppManager; |
| 126 | @Mock private Context mContext; |
| 127 | @Mock private Resources mResources; |
| 128 | @Mock private INetworkScoreCache.Stub mNetworkScoreCache, mNetworkScoreCache2; |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 129 | @Mock private IBinder mScoreCacheIBinder, mScoreCacheIBinder2; |
| 130 | @Mock private IBinder mServiceConnectionIBinder; |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 131 | @Mock private INetworkRecommendationProvider mRecommendationProvider; |
Jeremy Joslin | e71fe2b | 2017-01-25 11:40:08 -0800 | [diff] [blame] | 132 | @Mock private UnaryOperator<List<ScoredNetwork>> mCurrentNetworkFilter; |
| 133 | @Mock private UnaryOperator<List<ScoredNetwork>> mScanResultsFilter; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 134 | @Mock private WifiInfo mWifiInfo; |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 135 | @Mock private NetworkScoreService.ScoringServiceConnection mServiceConnection; |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 136 | @Mock private PermissionManagerServiceInternal mPermissionManagerInternal; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 137 | @Captor private ArgumentCaptor<List<ScoredNetwork>> mScoredNetworkCaptor; |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 138 | @Captor private |
| 139 | ArgumentCaptor<PermissionManagerServiceInternal.PackagesProvider> mPackagesProviderCaptor; |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 140 | |
| 141 | private ContentResolver mContentResolver; |
| 142 | private NetworkScoreService mNetworkScoreService; |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 143 | private HandlerThread mHandlerThread; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 144 | private List<ScanResult> mScanResults; |
| 145 | |
| 146 | private static String quote(String str) { |
| 147 | return String.format("\"%s\"", str); |
| 148 | } |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 149 | |
| 150 | @Before |
| 151 | public void setUp() throws Exception { |
| 152 | MockitoAnnotations.initMocks(this); |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 153 | when(mNetworkScoreCache.asBinder()).thenReturn(mScoreCacheIBinder); |
| 154 | when(mNetworkScoreCache2.asBinder()).thenReturn(mScoreCacheIBinder2); |
| 155 | when(mServiceConnectionIBinder.queryLocalInterface(anyString())) |
| 156 | .thenReturn(mRecommendationProvider); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 157 | mContentResolver = InstrumentationRegistry.getContext().getContentResolver(); |
| 158 | when(mContext.getContentResolver()).thenReturn(mContentResolver); |
| 159 | when(mContext.getResources()).thenReturn(mResources); |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 160 | when(mWifiInfo.getSSID()).thenReturn(SCORED_NETWORK.networkKey.wifiKey.ssid); |
| 161 | when(mWifiInfo.getBSSID()).thenReturn(SCORED_NETWORK.networkKey.wifiKey.bssid); |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 162 | when(mServiceConnection.getAppData()).thenReturn(NEW_SCORER); |
| 163 | when(mServiceConnection.getRecommendationProvider()).thenReturn(mRecommendationProvider); |
| 164 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(NEW_SCORER); |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 165 | mHandlerThread = new HandlerThread("NetworkScoreServiceTest"); |
| 166 | mHandlerThread.start(); |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 167 | LocalServices.addService( |
| 168 | PermissionManagerServiceInternal.class, mPermissionManagerInternal); |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 169 | mNetworkScoreService = new NetworkScoreService(mContext, mNetworkScorerAppManager, |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 170 | networkScorerAppData -> mServiceConnection, mHandlerThread.getLooper()); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 171 | WifiConfiguration configuration = new WifiConfiguration(); |
Jeremy Joslin | 8f5521a | 2016-12-20 14:36:20 -0800 | [diff] [blame] | 172 | configuration.SSID = "NetworkScoreServiceTest_SSID"; |
| 173 | configuration.BSSID = "NetworkScoreServiceTest_BSSID"; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 174 | populateScanResults(); |
| 175 | } |
| 176 | |
| 177 | private void populateScanResults() { |
| 178 | mScanResults = new ArrayList<>(); |
| 179 | mScanResults.add(createScanResult(SSID, SCORED_NETWORK.networkKey.wifiKey.bssid)); |
| 180 | mScanResults.add(createScanResult(SSID_2, SCORED_NETWORK_2.networkKey.wifiKey.bssid)); |
| 181 | mScanResults.add(createScanResult(SSID_3, "10:10:00:00:10:10")); |
| 182 | } |
| 183 | |
| 184 | private ScanResult createScanResult(String ssid, String bssid) { |
David Su | 543edf2 | 2020-01-07 16:07:02 -0800 | [diff] [blame] | 185 | ScanResult result = mock(ScanResult.class); |
| 186 | result.SSID = ssid; |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 187 | result.BSSID = bssid; |
| 188 | return result; |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | @After |
| 192 | public void tearDown() throws Exception { |
| 193 | mHandlerThread.quitSafely(); |
Philip P. Moltmann | 128b703 | 2019-09-27 08:44:12 -0700 | [diff] [blame] | 194 | LocalServices.removeServiceForTest(PermissionManagerServiceInternal.class); |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Test |
| 198 | public void testConstructor_setsUseOpenWifiPackagesProvider() { |
| 199 | Settings.Global.putString(mContentResolver, |
| 200 | Settings.Global.USE_OPEN_WIFI_PACKAGE, "com.some.app"); |
| 201 | |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 202 | verify(mPermissionManagerInternal) |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 203 | .setUseOpenWifiAppPackagesProvider(mPackagesProviderCaptor.capture()); |
| 204 | |
| 205 | String[] packages = mPackagesProviderCaptor.getValue().getPackages(0); |
| 206 | assertEquals(1, packages.length); |
| 207 | assertEquals("com.some.app", packages[0]); |
| 208 | } |
| 209 | |
| 210 | @Test |
| 211 | public void testConstructor_registersUseOpenWifiPackageContentObserver() { |
| 212 | Settings.Global.putString(mContentResolver, |
| 213 | Settings.Global.USE_OPEN_WIFI_PACKAGE, "com.some.other.app"); |
| 214 | |
Todd Kennedy | 583378d | 2019-07-12 06:50:30 -0700 | [diff] [blame] | 215 | verify(mPermissionManagerInternal, timeout(500)) |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 216 | .grantDefaultPermissionsToDefaultUseOpenWifiApp("com.some.other.app", 0); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | @Test |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 220 | public void testOnUserUnlocked() { |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 221 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(NEW_SCORER); |
| 222 | |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 223 | mNetworkScoreService.onUserUnlocked(0); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 224 | |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 225 | verify(mNetworkScorerAppManager).updateState(); |
| 226 | verify(mNetworkScorerAppManager).migrateNetworkScorerAppSettingIfNeeded(); |
| 227 | verify(mServiceConnection).bind(mContext); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | @Test |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 231 | public void testRequestScores_noPermission() throws Exception { |
| 232 | doThrow(new SecurityException()).when(mContext) |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 233 | .enforceCallingOrSelfPermission(eq(permission.REQUEST_NETWORK_SCORES), |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 234 | anyString()); |
| 235 | try { |
| 236 | mNetworkScoreService.requestScores(null); |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 237 | fail("REQUEST_NETWORK_SCORES not enforced."); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 238 | } catch (SecurityException e) { |
| 239 | // expected |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | @Test |
| 244 | public void testRequestScores_providerNotConnected() throws Exception { |
| 245 | assertFalse(mNetworkScoreService.requestScores(new NetworkKey[0])); |
| 246 | verifyZeroInteractions(mRecommendationProvider); |
| 247 | } |
| 248 | |
| 249 | @Test |
| 250 | public void testRequestScores_providerThrowsRemoteException() throws Exception { |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 251 | doThrow(new RemoteException()).when(mRecommendationProvider) |
| 252 | .requestScores(any(NetworkKey[].class)); |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 253 | mNetworkScoreService.onUserUnlocked(0); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 254 | |
| 255 | assertFalse(mNetworkScoreService.requestScores(new NetworkKey[0])); |
| 256 | } |
| 257 | |
| 258 | @Test |
| 259 | public void testRequestScores_providerAvailable() throws Exception { |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 260 | mNetworkScoreService.onUserUnlocked(0); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 261 | |
| 262 | final NetworkKey[] networks = new NetworkKey[0]; |
| 263 | assertTrue(mNetworkScoreService.requestScores(networks)); |
| 264 | verify(mRecommendationProvider).requestScores(networks); |
| 265 | } |
| 266 | |
| 267 | @Test |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 268 | public void dispatchingContentObserver_nullUri() throws Exception { |
| 269 | NetworkScoreService.DispatchingContentObserver observer = |
| 270 | new NetworkScoreService.DispatchingContentObserver(mContext, null /*handler*/); |
| 271 | |
| 272 | observer.onChange(false, null); |
| 273 | // nothing to assert or verify but since we passed in a null handler we'd see a NPE |
| 274 | // if it were interacted with. |
| 275 | } |
| 276 | |
| 277 | @Test |
| 278 | public void dispatchingContentObserver_dispatchUri() throws Exception { |
| 279 | final CountDownHandler handler = new CountDownHandler(mHandlerThread.getLooper()); |
| 280 | NetworkScoreService.DispatchingContentObserver observer = |
| 281 | new NetworkScoreService.DispatchingContentObserver(mContext, handler); |
| 282 | Uri uri = Uri.parse("content://settings/global/network_score_service_test"); |
| 283 | int expectedWhat = 24; |
| 284 | observer.observe(uri, expectedWhat); |
| 285 | |
| 286 | observer.onChange(false, uri); |
| 287 | final boolean msgHandled = handler.latch.await(3, TimeUnit.SECONDS); |
| 288 | assertTrue(msgHandled); |
| 289 | assertEquals(expectedWhat, handler.receivedWhat); |
| 290 | } |
| 291 | |
| 292 | @Test |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 293 | public void testUpdateScores_notActiveScorer() { |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 294 | bindToScorer(false /*callerIsScorer*/); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 295 | |
| 296 | try { |
| 297 | mNetworkScoreService.updateScores(new ScoredNetwork[0]); |
| 298 | fail("SecurityException expected"); |
| 299 | } catch (SecurityException e) { |
| 300 | // expected |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | @Test |
| 305 | public void testUpdateScores_oneRegisteredCache() throws RemoteException { |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 306 | bindToScorer(true /*callerIsScorer*/); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 307 | |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 308 | mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, |
David Su | bc81782 | 2020-01-13 17:12:13 -0800 | [diff] [blame] | 309 | mNetworkScoreCache, SCORE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 310 | |
| 311 | mNetworkScoreService.updateScores(new ScoredNetwork[]{SCORED_NETWORK}); |
| 312 | |
| 313 | verify(mNetworkScoreCache).updateScores(mScoredNetworkCaptor.capture()); |
| 314 | |
| 315 | assertEquals(1, mScoredNetworkCaptor.getValue().size()); |
| 316 | assertEquals(SCORED_NETWORK, mScoredNetworkCaptor.getValue().get(0)); |
| 317 | } |
| 318 | |
| 319 | @Test |
| 320 | public void testUpdateScores_twoRegisteredCaches() throws RemoteException { |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 321 | bindToScorer(true /*callerIsScorer*/); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 322 | |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 323 | mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, |
David Su | bc81782 | 2020-01-13 17:12:13 -0800 | [diff] [blame] | 324 | mNetworkScoreCache, SCORE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 325 | mNetworkScoreService.registerNetworkScoreCache( |
David Su | bc81782 | 2020-01-13 17:12:13 -0800 | [diff] [blame] | 326 | NetworkKey.TYPE_WIFI, mNetworkScoreCache2, SCORE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 327 | |
| 328 | // updateScores should update both caches |
| 329 | mNetworkScoreService.updateScores(new ScoredNetwork[]{SCORED_NETWORK}); |
| 330 | |
| 331 | verify(mNetworkScoreCache).updateScores(anyListOf(ScoredNetwork.class)); |
| 332 | verify(mNetworkScoreCache2).updateScores(anyListOf(ScoredNetwork.class)); |
| 333 | |
| 334 | mNetworkScoreService.unregisterNetworkScoreCache( |
| 335 | NetworkKey.TYPE_WIFI, mNetworkScoreCache2); |
| 336 | |
| 337 | // updateScores should only update the first cache since the 2nd has been unregistered |
| 338 | mNetworkScoreService.updateScores(new ScoredNetwork[]{SCORED_NETWORK}); |
| 339 | |
| 340 | verify(mNetworkScoreCache, times(2)).updateScores(anyListOf(ScoredNetwork.class)); |
| 341 | |
| 342 | mNetworkScoreService.unregisterNetworkScoreCache( |
| 343 | NetworkKey.TYPE_WIFI, mNetworkScoreCache); |
| 344 | |
| 345 | // updateScores should not update any caches since they are both unregistered |
| 346 | mNetworkScoreService.updateScores(new ScoredNetwork[]{SCORED_NETWORK}); |
| 347 | |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 348 | // The register and unregister calls grab the binder from the score cache. |
| 349 | verify(mNetworkScoreCache, atLeastOnce()).asBinder(); |
| 350 | verify(mNetworkScoreCache2, atLeastOnce()).asBinder(); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 351 | verifyNoMoreInteractions(mNetworkScoreCache, mNetworkScoreCache2); |
| 352 | } |
| 353 | |
| 354 | @Test |
Jeremy Joslin | 06d1fd7 | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 355 | public void testClearScores_notActiveScorer_noRequestNetworkScoresPermission() { |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 356 | bindToScorer(false /*callerIsScorer*/); |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 357 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 358 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 359 | try { |
| 360 | mNetworkScoreService.clearScores(); |
| 361 | fail("SecurityException expected"); |
| 362 | } catch (SecurityException e) { |
| 363 | // expected |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | @Test |
Jeremy Joslin | 06d1fd7 | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 368 | public void testClearScores_activeScorer_noRequestNetworkScoresPermission() { |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 369 | bindToScorer(true /*callerIsScorer*/); |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 370 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 371 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 372 | |
| 373 | mNetworkScoreService.clearScores(); |
| 374 | } |
| 375 | |
| 376 | @Test |
| 377 | public void testClearScores_activeScorer() throws RemoteException { |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 378 | bindToScorer(true /*callerIsScorer*/); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 379 | |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 380 | mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache, |
David Su | bc81782 | 2020-01-13 17:12:13 -0800 | [diff] [blame] | 381 | SCORE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 382 | mNetworkScoreService.clearScores(); |
| 383 | |
| 384 | verify(mNetworkScoreCache).clearScores(); |
| 385 | } |
| 386 | |
| 387 | @Test |
Jeremy Joslin | 06d1fd7 | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 388 | public void testClearScores_notActiveScorer_hasRequestNetworkScoresPermission() |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 389 | throws RemoteException { |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 390 | bindToScorer(false /*callerIsScorer*/); |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 391 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 392 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 393 | |
Jeremy Joslin | c5ac587 | 2016-11-30 15:05:40 -0800 | [diff] [blame] | 394 | mNetworkScoreService.registerNetworkScoreCache(NetworkKey.TYPE_WIFI, mNetworkScoreCache, |
David Su | bc81782 | 2020-01-13 17:12:13 -0800 | [diff] [blame] | 395 | SCORE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 396 | mNetworkScoreService.clearScores(); |
| 397 | |
| 398 | verify(mNetworkScoreCache).clearScores(); |
| 399 | } |
| 400 | |
| 401 | @Test |
Jeremy Joslin | a9f933e | 2017-04-28 13:08:17 -0700 | [diff] [blame] | 402 | public void testSetActiveScorer_noScoreNetworksPermission() { |
Jeremy Joslin | 1588b3b | 2017-08-11 15:35:12 -0700 | [diff] [blame] | 403 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
| 404 | .thenReturn(PackageManager.PERMISSION_DENIED); |
Jeremy Joslin | a9f933e | 2017-04-28 13:08:17 -0700 | [diff] [blame] | 405 | when(mContext.checkCallingOrSelfPermission(permission.SCORE_NETWORKS)) |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 406 | .thenReturn(PackageManager.PERMISSION_DENIED); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 407 | |
| 408 | try { |
| 409 | mNetworkScoreService.setActiveScorer(null); |
| 410 | fail("SecurityException expected"); |
| 411 | } catch (SecurityException e) { |
| 412 | // expected |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | @Test |
Jeremy Joslin | 3bddadd | 2017-03-21 16:16:46 -0700 | [diff] [blame] | 417 | public void testSetActiveScorer_requestNetworkScoresPermission() { |
| 418 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
| 419 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 420 | |
| 421 | mNetworkScoreService.setActiveScorer(null); |
| 422 | } |
| 423 | |
| 424 | @Test |
Jeremy Joslin | 06d1fd7 | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 425 | public void testDisableScoring_notActiveScorer_noRequestNetworkScoresPermission() { |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 426 | bindToScorer(false /*callerIsScorer*/); |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 427 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 428 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 429 | |
| 430 | try { |
| 431 | mNetworkScoreService.disableScoring(); |
| 432 | fail("SecurityException expected"); |
| 433 | } catch (SecurityException e) { |
| 434 | // expected |
| 435 | } |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | @Test |
Jeremy Joslin | 3bddadd | 2017-03-21 16:16:46 -0700 | [diff] [blame] | 439 | public void testDisableScoring_activeScorer_noRequestNetworkScoresPermission() { |
| 440 | bindToScorer(true /*callerIsScorer*/); |
| 441 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
| 442 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 443 | |
| 444 | mNetworkScoreService.disableScoring(); |
| 445 | } |
| 446 | |
| 447 | @Test |
| 448 | public void testGetAllValidScorer_noRequestNetworkScoresPermission() { |
Jeremy Joslin | 1588b3b | 2017-08-11 15:35:12 -0700 | [diff] [blame] | 449 | doThrow(new SecurityException()).when(mContext) |
| 450 | .enforceCallingOrSelfPermission(eq(permission.REQUEST_NETWORK_SCORES), anyString()); |
Jeremy Joslin | 3bddadd | 2017-03-21 16:16:46 -0700 | [diff] [blame] | 451 | |
| 452 | try { |
| 453 | mNetworkScoreService.getAllValidScorers(); |
| 454 | fail("SecurityException expected"); |
| 455 | } catch (SecurityException e) { |
| 456 | // expected |
| 457 | } |
| 458 | } |
| 459 | |
| 460 | @Test |
| 461 | public void testGetAllValidScorer_requestNetworkScoresPermission() { |
| 462 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
| 463 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 464 | |
| 465 | mNetworkScoreService.getAllValidScorers(); |
| 466 | } |
| 467 | |
| 468 | @Test |
Jeremy Joslin | 06d1fd7 | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 469 | public void testRegisterNetworkScoreCache_noRequestNetworkScoresPermission() { |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 470 | doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 471 | eq(permission.REQUEST_NETWORK_SCORES), anyString()); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 472 | |
| 473 | try { |
| 474 | mNetworkScoreService.registerNetworkScoreCache( |
David Su | bc81782 | 2020-01-13 17:12:13 -0800 | [diff] [blame] | 475 | NetworkKey.TYPE_WIFI, mNetworkScoreCache, SCORE_FILTER_NONE); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 476 | fail("SecurityException expected"); |
| 477 | } catch (SecurityException e) { |
| 478 | // expected |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | @Test |
Jeremy Joslin | 06d1fd7 | 2017-01-10 08:53:02 -0800 | [diff] [blame] | 483 | public void testUnregisterNetworkScoreCache_noRequestNetworkScoresPermission() { |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 484 | doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( |
Jeremy Joslin | 5519d7c | 2017-01-06 14:36:54 -0800 | [diff] [blame] | 485 | eq(permission.REQUEST_NETWORK_SCORES), anyString()); |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 486 | |
| 487 | try { |
| 488 | mNetworkScoreService.unregisterNetworkScoreCache( |
| 489 | NetworkKey.TYPE_WIFI, mNetworkScoreCache); |
| 490 | fail("SecurityException expected"); |
| 491 | } catch (SecurityException e) { |
| 492 | // expected |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | @Test |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 497 | public void testDump_doesNotCrash() { |
| 498 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(NEW_SCORER); |
| 499 | StringWriter stringWriter = new StringWriter(); |
| 500 | |
| 501 | mNetworkScoreService.dump( |
| 502 | new FileDescriptor(), new PrintWriter(stringWriter), new String[0]); |
| 503 | |
| 504 | assertFalse(stringWriter.toString().isEmpty()); |
| 505 | } |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 506 | |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 507 | @Test |
| 508 | public void testIsCallerActiveScorer_noBoundService() throws Exception { |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 509 | // No active scorer. |
| 510 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(null); |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 511 | mNetworkScoreService.onUserUnlocked(0); |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 512 | |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 513 | mNetworkScoreService.isCallerActiveScorer(Binder.getCallingUid()); |
| 514 | |
| 515 | verify(mServiceConnection, never()).getAppData(); |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | @Test |
| 519 | public void testIsCallerActiveScorer_boundServiceIsNotCaller() throws Exception { |
| 520 | bindToScorer(false /*callerIsScorer*/); |
| 521 | |
| 522 | assertFalse(mNetworkScoreService.isCallerActiveScorer(Binder.getCallingUid())); |
| 523 | } |
| 524 | |
| 525 | @Test |
| 526 | public void testIsCallerActiveScorer_boundServiceIsCaller() throws Exception { |
| 527 | bindToScorer(true /*callerIsScorer*/); |
| 528 | |
| 529 | assertTrue(mNetworkScoreService.isCallerActiveScorer(Binder.getCallingUid())); |
| 530 | } |
| 531 | |
Jeremy Joslin | 6c1ca28 | 2017-01-10 13:08:32 -0800 | [diff] [blame] | 532 | @Test |
| 533 | public void testGetActiveScorerPackage_notActive() throws Exception { |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 534 | // No active scorer. |
| 535 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(null); |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 536 | mNetworkScoreService.onUserUnlocked(0); |
Jeremy Joslin | 6c1ca28 | 2017-01-10 13:08:32 -0800 | [diff] [blame] | 537 | |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 538 | mNetworkScoreService.getActiveScorerPackage(); |
| 539 | |
| 540 | verify(mServiceConnection, never()).getPackageName(); |
Jeremy Joslin | 6c1ca28 | 2017-01-10 13:08:32 -0800 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | @Test |
| 544 | public void testGetActiveScorerPackage_active() throws Exception { |
| 545 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(NEW_SCORER); |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 546 | mNetworkScoreService.onUserUnlocked(0); |
Jeremy Joslin | 6c1ca28 | 2017-01-10 13:08:32 -0800 | [diff] [blame] | 547 | |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 548 | mNetworkScoreService.getActiveScorerPackage(); |
| 549 | |
| 550 | verify(mServiceConnection).getPackageName(); |
Jeremy Joslin | 6c1ca28 | 2017-01-10 13:08:32 -0800 | [diff] [blame] | 551 | } |
| 552 | |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 553 | @Test |
Jeremy Joslin | 2d55b18 | 2017-07-11 15:54:14 -0700 | [diff] [blame] | 554 | public void testGetActiveScorerPackage_missingRequiredPermissions() throws Exception { |
| 555 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
| 556 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 557 | when(mContext.checkCallingOrSelfPermission(permission.SCORE_NETWORKS)) |
| 558 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 559 | |
| 560 | try { |
| 561 | mNetworkScoreService.getActiveScorerPackage(); |
| 562 | fail("SecurityException expected"); |
| 563 | } catch (SecurityException e) { |
| 564 | // expected |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | @Test |
| 569 | public void testGetActiveScorerPackage_noRequestScoresPermission() throws Exception { |
| 570 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
| 571 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 572 | when(mContext.checkCallingOrSelfPermission(permission.SCORE_NETWORKS)) |
| 573 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 574 | |
| 575 | try { |
| 576 | mNetworkScoreService.getActiveScorerPackage(); |
| 577 | } catch (SecurityException e) { |
| 578 | fail("Unexpected SecurityException"); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | @Test |
| 583 | public void testGetActiveScorerPackage_noScoreNetworksPermission() throws Exception { |
| 584 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
| 585 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
| 586 | when(mContext.checkCallingOrSelfPermission(permission.SCORE_NETWORKS)) |
| 587 | .thenReturn(PackageManager.PERMISSION_DENIED); |
| 588 | |
| 589 | try { |
| 590 | mNetworkScoreService.getActiveScorerPackage(); |
| 591 | } catch (SecurityException e) { |
| 592 | fail("Unexpected SecurityException"); |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | @Test |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 597 | public void testCacheUpdatingConsumer_nullFilter() throws Exception { |
| 598 | List<ScoredNetwork> scoredNetworkList = Lists.newArrayList(SCORED_NETWORK); |
| 599 | NetworkScoreService.FilteringCacheUpdatingConsumer consumer = |
| 600 | new NetworkScoreService.FilteringCacheUpdatingConsumer(mContext, |
| 601 | new ArrayList<>(scoredNetworkList), NetworkKey.TYPE_WIFI, |
| 602 | mCurrentNetworkFilter, mScanResultsFilter); |
| 603 | |
| 604 | consumer.accept(mNetworkScoreCache, null /*cookie*/); |
| 605 | |
| 606 | verify(mNetworkScoreCache).updateScores(scoredNetworkList); |
| 607 | verifyZeroInteractions(mCurrentNetworkFilter, mScanResultsFilter); |
| 608 | } |
| 609 | |
| 610 | @Test |
| 611 | public void testCacheUpdatingConsumer_noneFilter() throws Exception { |
| 612 | List<ScoredNetwork> scoredNetworkList = Lists.newArrayList(SCORED_NETWORK); |
| 613 | NetworkScoreService.FilteringCacheUpdatingConsumer |
| 614 | consumer = new NetworkScoreService.FilteringCacheUpdatingConsumer(mContext, |
| 615 | new ArrayList<>(scoredNetworkList), |
| 616 | NetworkKey.TYPE_WIFI, mCurrentNetworkFilter, mScanResultsFilter); |
| 617 | |
David Su | bc81782 | 2020-01-13 17:12:13 -0800 | [diff] [blame] | 618 | consumer.accept(mNetworkScoreCache, NetworkScoreManager.SCORE_FILTER_NONE); |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 619 | |
| 620 | verify(mNetworkScoreCache).updateScores(scoredNetworkList); |
| 621 | verifyZeroInteractions(mCurrentNetworkFilter, mScanResultsFilter); |
| 622 | } |
| 623 | |
| 624 | @Test |
| 625 | public void testCacheUpdatingConsumer_unknownFilter() throws Exception { |
| 626 | List<ScoredNetwork> scoredNetworkList = Lists.newArrayList(SCORED_NETWORK); |
| 627 | NetworkScoreService.FilteringCacheUpdatingConsumer |
| 628 | consumer = new NetworkScoreService.FilteringCacheUpdatingConsumer(mContext, |
| 629 | new ArrayList<>(scoredNetworkList), |
| 630 | NetworkKey.TYPE_WIFI, mCurrentNetworkFilter, mScanResultsFilter); |
| 631 | |
| 632 | consumer.accept(mNetworkScoreCache, -1 /*cookie*/); |
| 633 | |
| 634 | verify(mNetworkScoreCache).updateScores(scoredNetworkList); |
| 635 | verifyZeroInteractions(mCurrentNetworkFilter, mScanResultsFilter); |
| 636 | } |
| 637 | |
| 638 | @Test |
| 639 | public void testCacheUpdatingConsumer_nonIntFilter() throws Exception { |
| 640 | List<ScoredNetwork> scoredNetworkList = Lists.newArrayList(SCORED_NETWORK); |
| 641 | NetworkScoreService.FilteringCacheUpdatingConsumer |
| 642 | consumer = new NetworkScoreService.FilteringCacheUpdatingConsumer(mContext, |
| 643 | new ArrayList<>(scoredNetworkList), |
| 644 | NetworkKey.TYPE_WIFI, mCurrentNetworkFilter, mScanResultsFilter); |
| 645 | |
| 646 | consumer.accept(mNetworkScoreCache, "not an int" /*cookie*/); |
| 647 | |
| 648 | verify(mNetworkScoreCache).updateScores(scoredNetworkList); |
| 649 | verifyZeroInteractions(mCurrentNetworkFilter, mScanResultsFilter); |
| 650 | } |
| 651 | |
| 652 | @Test |
| 653 | public void testCacheUpdatingConsumer_emptyScoreList() throws Exception { |
| 654 | NetworkScoreService.FilteringCacheUpdatingConsumer |
| 655 | consumer = new NetworkScoreService.FilteringCacheUpdatingConsumer(mContext, |
| 656 | Collections.emptyList(), |
| 657 | NetworkKey.TYPE_WIFI, mCurrentNetworkFilter, mScanResultsFilter); |
| 658 | |
David Su | bc81782 | 2020-01-13 17:12:13 -0800 | [diff] [blame] | 659 | consumer.accept(mNetworkScoreCache, NetworkScoreManager.SCORE_FILTER_NONE); |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 660 | |
| 661 | verifyZeroInteractions(mNetworkScoreCache, mCurrentNetworkFilter, mScanResultsFilter); |
| 662 | } |
| 663 | |
| 664 | @Test |
| 665 | public void testCacheUpdatingConsumer_currentNetworkFilter() throws Exception { |
| 666 | List<ScoredNetwork> scoredNetworkList = |
| 667 | Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2); |
| 668 | NetworkScoreService.FilteringCacheUpdatingConsumer |
| 669 | consumer = new NetworkScoreService.FilteringCacheUpdatingConsumer(mContext, |
| 670 | new ArrayList<>(scoredNetworkList), |
| 671 | NetworkKey.TYPE_WIFI, mCurrentNetworkFilter, mScanResultsFilter); |
| 672 | |
| 673 | List<ScoredNetwork> filteredList = new ArrayList<>(scoredNetworkList); |
| 674 | filteredList.remove(SCORED_NETWORK); |
| 675 | when(mCurrentNetworkFilter.apply(scoredNetworkList)).thenReturn(filteredList); |
David Su | bc81782 | 2020-01-13 17:12:13 -0800 | [diff] [blame] | 676 | consumer.accept(mNetworkScoreCache, NetworkScoreManager.SCORE_FILTER_CURRENT_NETWORK); |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 677 | |
| 678 | verify(mNetworkScoreCache).updateScores(filteredList); |
| 679 | verifyZeroInteractions(mScanResultsFilter); |
| 680 | } |
| 681 | |
| 682 | @Test |
| 683 | public void testCacheUpdatingConsumer_scanResultsFilter() throws Exception { |
| 684 | List<ScoredNetwork> scoredNetworkList = |
| 685 | Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2); |
| 686 | NetworkScoreService.FilteringCacheUpdatingConsumer |
| 687 | consumer = new NetworkScoreService.FilteringCacheUpdatingConsumer(mContext, |
| 688 | new ArrayList<>(scoredNetworkList), |
| 689 | NetworkKey.TYPE_WIFI, mCurrentNetworkFilter, mScanResultsFilter); |
| 690 | |
| 691 | List<ScoredNetwork> filteredList = new ArrayList<>(scoredNetworkList); |
| 692 | filteredList.remove(SCORED_NETWORK); |
| 693 | when(mScanResultsFilter.apply(scoredNetworkList)).thenReturn(filteredList); |
David Su | bc81782 | 2020-01-13 17:12:13 -0800 | [diff] [blame] | 694 | consumer.accept(mNetworkScoreCache, NetworkScoreManager.SCORE_FILTER_SCAN_RESULTS); |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 695 | |
| 696 | verify(mNetworkScoreCache).updateScores(filteredList); |
| 697 | verifyZeroInteractions(mCurrentNetworkFilter); |
| 698 | } |
| 699 | |
| 700 | @Test |
| 701 | public void testCurrentNetworkScoreCacheFilter_nullWifiInfo() throws Exception { |
| 702 | NetworkScoreService.CurrentNetworkScoreCacheFilter cacheFilter = |
| 703 | new NetworkScoreService.CurrentNetworkScoreCacheFilter(() -> null /*WifiInfo*/); |
| 704 | |
| 705 | List<ScoredNetwork> actualList = |
| 706 | cacheFilter.apply(Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2)); |
| 707 | |
| 708 | assertTrue(actualList.isEmpty()); |
| 709 | } |
| 710 | |
| 711 | @Test |
Stephen Chen | fde900d | 2017-02-14 16:40:21 -0800 | [diff] [blame] | 712 | public void testCurrentNetworkScoreCacheFilter_invalidWifiInfo_nullSsid() throws Exception { |
| 713 | when(mWifiInfo.getSSID()).thenReturn(null); |
| 714 | NetworkScoreService.CurrentNetworkScoreCacheFilter cacheFilter = |
| 715 | new NetworkScoreService.CurrentNetworkScoreCacheFilter(() -> mWifiInfo); |
| 716 | |
| 717 | List<ScoredNetwork> actualList = |
| 718 | cacheFilter.apply(Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2)); |
| 719 | |
| 720 | assertTrue(actualList.isEmpty()); |
| 721 | } |
| 722 | |
| 723 | @Test |
| 724 | public void testCurrentNetworkScoreCacheFilter_invalidWifiInfo_noneSsid() throws Exception { |
David Su | 1cdbf62 | 2019-11-13 15:06:41 -0800 | [diff] [blame] | 725 | when(mWifiInfo.getSSID()).thenReturn(WifiManager.UNKNOWN_SSID); |
Stephen Chen | fde900d | 2017-02-14 16:40:21 -0800 | [diff] [blame] | 726 | NetworkScoreService.CurrentNetworkScoreCacheFilter cacheFilter = |
| 727 | new NetworkScoreService.CurrentNetworkScoreCacheFilter(() -> mWifiInfo); |
| 728 | |
| 729 | List<ScoredNetwork> actualList = |
| 730 | cacheFilter.apply(Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2)); |
| 731 | |
| 732 | assertTrue(actualList.isEmpty()); |
| 733 | } |
| 734 | |
| 735 | @Test |
| 736 | public void testCurrentNetworkScoreCacheFilter_invalidWifiInfo_emptySsid() throws Exception { |
| 737 | when(mWifiInfo.getSSID()).thenReturn(""); |
| 738 | NetworkScoreService.CurrentNetworkScoreCacheFilter cacheFilter = |
| 739 | new NetworkScoreService.CurrentNetworkScoreCacheFilter(() -> mWifiInfo); |
| 740 | |
| 741 | List<ScoredNetwork> actualList = |
| 742 | cacheFilter.apply(Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2)); |
| 743 | |
| 744 | assertTrue(actualList.isEmpty()); |
| 745 | } |
| 746 | |
| 747 | @Test |
| 748 | public void testCurrentNetworkScoreCacheFilter_invalidWifiInfo_invalidBssid() throws Exception { |
| 749 | when(mWifiInfo.getBSSID()).thenReturn(INVALID_BSSID); |
| 750 | NetworkScoreService.CurrentNetworkScoreCacheFilter cacheFilter = |
| 751 | new NetworkScoreService.CurrentNetworkScoreCacheFilter(() -> mWifiInfo); |
| 752 | |
| 753 | List<ScoredNetwork> actualList = |
| 754 | cacheFilter.apply(Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2)); |
| 755 | |
| 756 | assertTrue(actualList.isEmpty()); |
| 757 | } |
| 758 | |
| 759 | @Test |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 760 | public void testCurrentNetworkScoreCacheFilter_scoreFiltered() throws Exception { |
| 761 | NetworkScoreService.CurrentNetworkScoreCacheFilter cacheFilter = |
| 762 | new NetworkScoreService.CurrentNetworkScoreCacheFilter(() -> mWifiInfo); |
| 763 | |
| 764 | List<ScoredNetwork> actualList = |
| 765 | cacheFilter.apply(Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2)); |
| 766 | |
| 767 | List<ScoredNetwork> expectedList = Collections.singletonList(SCORED_NETWORK); |
| 768 | assertEquals(expectedList, actualList); |
| 769 | } |
| 770 | |
| 771 | @Test |
| 772 | public void testCurrentNetworkScoreCacheFilter_currentNetworkNotInList() throws Exception { |
| 773 | when(mWifiInfo.getSSID()).thenReturn("\"notInList\""); |
| 774 | NetworkScoreService.CurrentNetworkScoreCacheFilter cacheFilter = |
| 775 | new NetworkScoreService.CurrentNetworkScoreCacheFilter(() -> mWifiInfo); |
| 776 | |
| 777 | List<ScoredNetwork> actualList = |
| 778 | cacheFilter.apply(Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2)); |
| 779 | |
| 780 | assertTrue(actualList.isEmpty()); |
| 781 | } |
| 782 | |
| 783 | @Test |
| 784 | public void testScanResultsScoreCacheFilter_emptyScanResults() throws Exception { |
| 785 | NetworkScoreService.ScanResultsScoreCacheFilter cacheFilter = |
| 786 | new NetworkScoreService.ScanResultsScoreCacheFilter(Collections::emptyList); |
| 787 | |
| 788 | List<ScoredNetwork> actualList = |
| 789 | cacheFilter.apply(Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2)); |
| 790 | |
| 791 | assertTrue(actualList.isEmpty()); |
| 792 | } |
| 793 | |
| 794 | @Test |
Stephen Chen | fde900d | 2017-02-14 16:40:21 -0800 | [diff] [blame] | 795 | public void testScanResultsScoreCacheFilter_invalidScanResults() throws Exception { |
| 796 | List<ScanResult> invalidScanResults = Lists.newArrayList( |
David Su | 543edf2 | 2020-01-07 16:07:02 -0800 | [diff] [blame] | 797 | mock(ScanResult.class), |
Stephen Chen | fde900d | 2017-02-14 16:40:21 -0800 | [diff] [blame] | 798 | createScanResult("", SCORED_NETWORK.networkKey.wifiKey.bssid), |
David Su | 1cdbf62 | 2019-11-13 15:06:41 -0800 | [diff] [blame] | 799 | createScanResult(WifiManager.UNKNOWN_SSID, SCORED_NETWORK.networkKey.wifiKey.bssid), |
Stephen Chen | fde900d | 2017-02-14 16:40:21 -0800 | [diff] [blame] | 800 | createScanResult(SSID, null), |
| 801 | createScanResult(SSID, INVALID_BSSID) |
| 802 | ); |
| 803 | NetworkScoreService.ScanResultsScoreCacheFilter cacheFilter = |
| 804 | new NetworkScoreService.ScanResultsScoreCacheFilter(() -> invalidScanResults); |
| 805 | |
| 806 | List<ScoredNetwork> actualList = |
| 807 | cacheFilter.apply(Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2)); |
| 808 | |
| 809 | assertTrue(actualList.isEmpty()); |
| 810 | } |
| 811 | |
| 812 | @Test |
Jeremy Joslin | ba24273 | 2017-01-24 17:16:42 -0800 | [diff] [blame] | 813 | public void testScanResultsScoreCacheFilter_scoresFiltered() throws Exception { |
| 814 | NetworkScoreService.ScanResultsScoreCacheFilter cacheFilter = |
| 815 | new NetworkScoreService.ScanResultsScoreCacheFilter(() -> mScanResults); |
| 816 | |
| 817 | ScoredNetwork unmatchedScore = |
| 818 | new ScoredNetwork(new NetworkKey(new WifiKey(quote("newSsid"), |
| 819 | "00:00:00:00:00:00")), null /* rssiCurve*/); |
| 820 | |
| 821 | List<ScoredNetwork> actualList = |
| 822 | cacheFilter.apply(Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2, |
| 823 | unmatchedScore)); |
| 824 | |
| 825 | List<ScoredNetwork> expectedList = Lists.newArrayList(SCORED_NETWORK, SCORED_NETWORK_2); |
| 826 | assertEquals(expectedList, actualList); |
| 827 | } |
| 828 | |
Jeremy Joslin | a5172f6 | 2017-02-02 14:27:05 -0800 | [diff] [blame] | 829 | @Test |
| 830 | public void testGetActiveScorer_notConnected_canRequestScores() throws Exception { |
| 831 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
| 832 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 833 | when(mNetworkScorerAppManager.getActiveScorer()).thenReturn(null); |
| 834 | mNetworkScoreService.onUserUnlocked(0); |
| 835 | |
| 836 | mNetworkScoreService.getActiveScorer(); |
| 837 | |
| 838 | verify(mServiceConnection, never()).getAppData(); |
Jeremy Joslin | a5172f6 | 2017-02-02 14:27:05 -0800 | [diff] [blame] | 839 | } |
| 840 | |
| 841 | @Test |
| 842 | public void testGetActiveScorer_notConnected_canNotRequestScores() throws Exception { |
Jeremy Joslin | 1588b3b | 2017-08-11 15:35:12 -0700 | [diff] [blame] | 843 | doThrow(new SecurityException()).when(mContext) |
| 844 | .enforceCallingOrSelfPermission(eq(permission.REQUEST_NETWORK_SCORES), anyString()); |
Jeremy Joslin | a5172f6 | 2017-02-02 14:27:05 -0800 | [diff] [blame] | 845 | try { |
| 846 | mNetworkScoreService.getActiveScorer(); |
| 847 | fail("SecurityException expected."); |
| 848 | } catch (SecurityException e) { |
| 849 | // expected |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | @Test |
| 854 | public void testGetActiveScorer_connected_canRequestScores() |
| 855 | throws Exception { |
| 856 | when(mContext.checkCallingOrSelfPermission(permission.REQUEST_NETWORK_SCORES)) |
| 857 | .thenReturn(PackageManager.PERMISSION_GRANTED); |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 858 | mNetworkScoreService.onUserUnlocked(0); |
| 859 | |
| 860 | mNetworkScoreService.getActiveScorer(); |
| 861 | |
| 862 | verify(mServiceConnection).getAppData(); |
Jeremy Joslin | a5172f6 | 2017-02-02 14:27:05 -0800 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | @Test |
| 866 | public void testGetActiveScorer_connected_canNotRequestScores() |
| 867 | throws Exception { |
Jeremy Joslin | 1588b3b | 2017-08-11 15:35:12 -0700 | [diff] [blame] | 868 | doThrow(new SecurityException()).when(mContext) |
| 869 | .enforceCallingOrSelfPermission(eq(permission.REQUEST_NETWORK_SCORES), anyString()); |
Jeremy Joslin | a5172f6 | 2017-02-02 14:27:05 -0800 | [diff] [blame] | 870 | bindToScorer(false); |
| 871 | try { |
| 872 | mNetworkScoreService.getActiveScorer(); |
| 873 | fail("SecurityException expected."); |
| 874 | } catch (SecurityException e) { |
| 875 | // expected |
| 876 | } |
| 877 | } |
| 878 | |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 879 | @Test |
| 880 | public void testServiceConnection_bind_notBound() throws Exception { |
| 881 | NetworkScoreService.ScoringServiceConnection connection = new NetworkScoreService |
| 882 | .ScoringServiceConnection(NEW_SCORER); |
| 883 | connection.bind(mContext); |
| 884 | |
| 885 | verify(mContext).bindServiceAsUser(MockUtils.checkIntent( |
| 886 | new Intent(NetworkScoreManager.ACTION_RECOMMEND_NETWORKS) |
| 887 | .setComponent(RECOMMENDATION_SERVICE_COMP)), |
| 888 | eq(connection), |
| 889 | eq(Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE), |
| 890 | eq(UserHandle.SYSTEM)); |
| 891 | } |
| 892 | |
| 893 | @Test |
| 894 | public void testServiceConnection_bind_alreadyBound() throws Exception { |
| 895 | NetworkScoreService.ScoringServiceConnection connection = new NetworkScoreService |
| 896 | .ScoringServiceConnection(NEW_SCORER); |
| 897 | |
| 898 | when(mContext.bindServiceAsUser(MockUtils.checkIntent( |
| 899 | new Intent(NetworkScoreManager.ACTION_RECOMMEND_NETWORKS) |
| 900 | .setComponent(RECOMMENDATION_SERVICE_COMP)), |
| 901 | eq(connection), |
| 902 | eq(Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE), |
| 903 | eq(UserHandle.SYSTEM))).thenReturn(true /*bound*/); |
| 904 | |
| 905 | // Calling bind more than once should only result in 1 bindService call. |
| 906 | connection.bind(mContext); |
| 907 | connection.bind(mContext); |
| 908 | |
| 909 | verify(mContext, times(1)).bindServiceAsUser(MockUtils.checkIntent( |
| 910 | new Intent(NetworkScoreManager.ACTION_RECOMMEND_NETWORKS) |
| 911 | .setComponent(RECOMMENDATION_SERVICE_COMP)), |
| 912 | eq(connection), |
| 913 | eq(Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE), |
| 914 | eq(UserHandle.SYSTEM)); |
| 915 | } |
| 916 | |
| 917 | @Test |
| 918 | public void testServiceConnection_bindFails() throws Exception { |
| 919 | NetworkScoreService.ScoringServiceConnection connection = new NetworkScoreService |
| 920 | .ScoringServiceConnection(NEW_SCORER); |
| 921 | |
| 922 | when(mContext.bindServiceAsUser(MockUtils.checkIntent( |
| 923 | new Intent(NetworkScoreManager.ACTION_RECOMMEND_NETWORKS) |
| 924 | .setComponent(RECOMMENDATION_SERVICE_COMP)), |
| 925 | eq(connection), |
| 926 | eq(Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE), |
| 927 | eq(UserHandle.SYSTEM))).thenReturn(false /*bound*/); |
| 928 | |
| 929 | connection.bind(mContext); |
| 930 | |
| 931 | verify(mContext).unbindService(connection); |
| 932 | } |
| 933 | |
| 934 | @Test |
| 935 | public void testServiceConnection_unbind_notBound() throws Exception { |
| 936 | NetworkScoreService.ScoringServiceConnection connection = new NetworkScoreService |
| 937 | .ScoringServiceConnection(NEW_SCORER); |
| 938 | |
| 939 | connection.unbind(mContext); |
| 940 | |
| 941 | verify(mContext, never()).unbindService(connection); |
| 942 | } |
| 943 | |
| 944 | @Test |
| 945 | public void testServiceConnection_unbind_alreadyBound() throws Exception { |
| 946 | NetworkScoreService.ScoringServiceConnection connection = new NetworkScoreService |
| 947 | .ScoringServiceConnection(NEW_SCORER); |
| 948 | |
| 949 | when(mContext.bindServiceAsUser(MockUtils.checkIntent( |
| 950 | new Intent(NetworkScoreManager.ACTION_RECOMMEND_NETWORKS) |
| 951 | .setComponent(RECOMMENDATION_SERVICE_COMP)), |
| 952 | eq(connection), |
| 953 | eq(Context.BIND_AUTO_CREATE | Context.BIND_FOREGROUND_SERVICE), |
| 954 | eq(UserHandle.SYSTEM))).thenReturn(true /*bound*/); |
| 955 | |
| 956 | connection.bind(mContext); |
| 957 | connection.unbind(mContext); |
| 958 | |
| 959 | verify(mContext).unbindService(connection); |
| 960 | } |
| 961 | |
| 962 | @Test |
| 963 | public void testServiceConnection_dump_doesNotCrash() throws Exception { |
| 964 | NetworkScoreService.ScoringServiceConnection connection = new NetworkScoreService |
| 965 | .ScoringServiceConnection(NEW_SCORER); |
| 966 | final StringWriter stringWriter = new StringWriter(); |
| 967 | |
| 968 | connection.dump(new FileDescriptor(), new PrintWriter(stringWriter), new String[0]); |
| 969 | |
| 970 | assertFalse(stringWriter.toString().isEmpty()); |
Jeremy Joslin | 145c343 | 2016-12-09 13:11:51 -0800 | [diff] [blame] | 971 | } |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 972 | |
| 973 | private void bindToScorer(boolean callerIsScorer) { |
Jeremy Joslin | a5172f6 | 2017-02-02 14:27:05 -0800 | [diff] [blame] | 974 | final int callingUid = callerIsScorer ? Binder.getCallingUid() : Binder.getCallingUid() + 1; |
Amin Shaikh | bc9a8e6 | 2017-02-02 15:39:12 -0800 | [diff] [blame] | 975 | NetworkScorerAppData appData = new NetworkScorerAppData(callingUid, |
Stephen Chen | 8b1339a | 2017-02-28 18:11:34 -0800 | [diff] [blame] | 976 | RECOMMENDATION_SERVICE_COMP, RECOMMENDATION_SERVICE_LABEL, |
Amin Shaikh | d601360 | 2017-03-24 15:52:32 -0700 | [diff] [blame] | 977 | USE_WIFI_ENABLE_ACTIVITY_COMP, NETWORK_AVAILABLE_NOTIFICATION_CHANNEL_ID); |
Jeremy Joslin | 1e2595d | 2017-04-05 14:50:32 -0700 | [diff] [blame] | 978 | when(mServiceConnection.getAppData()).thenReturn(appData); |
Jeremy Joslin | ee3fb5c | 2017-02-13 13:44:11 -0800 | [diff] [blame] | 979 | mNetworkScoreService.onUserUnlocked(0); |
Jeremy Joslin | 134c9d3 | 2017-01-09 16:22:20 -0800 | [diff] [blame] | 980 | } |
Jeremy Joslin | ce73c6f | 2016-12-29 14:49:38 -0800 | [diff] [blame] | 981 | |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 982 | private static class CountDownHandler extends Handler { |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 983 | final CountDownLatch latch = new CountDownLatch(1); |
| 984 | volatile int receivedWhat; |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 985 | |
| 986 | CountDownHandler(Looper looper) { |
| 987 | super(looper); |
| 988 | } |
| 989 | |
| 990 | @Override |
| 991 | public void handleMessage(Message msg) { |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 992 | receivedWhat = msg.what; |
Eric Enslen | 1e423b9 | 2017-12-18 11:30:21 -0800 | [diff] [blame] | 993 | latch.countDown(); |
Jeremy Joslin | cb594f3 | 2017-01-03 17:31:23 -0800 | [diff] [blame] | 994 | } |
| 995 | } |
Amin Shaikh | 972e236 | 2016-12-07 14:08:09 -0800 | [diff] [blame] | 996 | } |