OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.omnibox.geo; | 5 package org.chromium.chrome.browser.omnibox.geo; |
6 | 6 |
7 import static org.junit.Assert.assertEquals; | 7 import static org.junit.Assert.assertEquals; |
8 import static org.junit.Assert.assertNull; | 8 import static org.junit.Assert.assertNull; |
9 import static org.junit.Assert.assertTrue; | 9 import static org.junit.Assert.assertTrue; |
| 10 import static org.mockito.Matchers.argThat; |
10 import static org.mockito.Mockito.any; | 11 import static org.mockito.Mockito.any; |
11 import static org.mockito.Mockito.eq; | 12 import static org.mockito.Mockito.eq; |
| 13 import static org.mockito.Mockito.verify; |
12 import static org.mockito.Mockito.when; | 14 import static org.mockito.Mockito.when; |
13 | 15 |
14 import android.Manifest; | 16 import android.Manifest; |
15 import android.content.Context; | 17 import android.content.Context; |
| 18 import android.content.Intent; |
| 19 import android.content.IntentFilter; |
16 import android.content.pm.PackageManager; | 20 import android.content.pm.PackageManager; |
17 import android.net.ConnectivityManager; | 21 import android.net.ConnectivityManager; |
18 import android.net.NetworkInfo; | 22 import android.net.NetworkInfo; |
19 import android.net.wifi.ScanResult; | 23 import android.net.wifi.ScanResult; |
20 import android.net.wifi.WifiInfo; | 24 import android.net.wifi.WifiInfo; |
21 import android.net.wifi.WifiManager; | 25 import android.net.wifi.WifiManager; |
22 import android.os.Build; | 26 import android.os.Build; |
23 import android.telephony.CellIdentityCdma; | 27 import android.telephony.CellIdentityCdma; |
24 import android.telephony.CellIdentityGsm; | 28 import android.telephony.CellIdentityGsm; |
25 import android.telephony.CellIdentityLte; | 29 import android.telephony.CellIdentityLte; |
26 import android.telephony.CellIdentityWcdma; | 30 import android.telephony.CellIdentityWcdma; |
27 import android.telephony.CellInfoCdma; | 31 import android.telephony.CellInfoCdma; |
28 import android.telephony.CellInfoGsm; | 32 import android.telephony.CellInfoGsm; |
29 import android.telephony.CellInfoLte; | 33 import android.telephony.CellInfoLte; |
30 import android.telephony.CellInfoWcdma; | 34 import android.telephony.CellInfoWcdma; |
31 import android.telephony.TelephonyManager; | 35 import android.telephony.TelephonyManager; |
32 | 36 |
33 import org.junit.Before; | 37 import org.junit.Before; |
34 import org.junit.Test; | 38 import org.junit.Test; |
35 import org.junit.runner.RunWith; | 39 import org.junit.runner.RunWith; |
| 40 import org.mockito.ArgumentMatcher; |
36 import org.mockito.Mock; | 41 import org.mockito.Mock; |
37 import org.mockito.MockitoAnnotations; | 42 import org.mockito.MockitoAnnotations; |
38 import org.robolectric.annotation.Config; | 43 import org.robolectric.annotation.Config; |
39 import org.robolectric.util.ReflectionHelpers; | 44 import org.robolectric.util.ReflectionHelpers; |
40 | 45 |
41 import org.chromium.chrome.browser.omnibox.geo.VisibleNetworks.VisibleCell; | 46 import org.chromium.chrome.browser.omnibox.geo.VisibleNetworks.VisibleCell; |
42 import org.chromium.chrome.browser.omnibox.geo.VisibleNetworks.VisibleWifi; | 47 import org.chromium.chrome.browser.omnibox.geo.VisibleNetworks.VisibleWifi; |
43 import org.chromium.testing.local.LocalRobolectricTestRunner; | 48 import org.chromium.testing.local.LocalRobolectricTestRunner; |
44 | 49 |
45 import java.util.Arrays; | 50 import java.util.Arrays; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 @Mock | 138 @Mock |
134 private CellInfoCdma mCellInfoCdma; | 139 private CellInfoCdma mCellInfoCdma; |
135 @Mock | 140 @Mock |
136 private CellIdentityLte mCellIdentityLte; | 141 private CellIdentityLte mCellIdentityLte; |
137 @Mock | 142 @Mock |
138 private CellIdentityWcdma mCellIdentityWcdma; | 143 private CellIdentityWcdma mCellIdentityWcdma; |
139 @Mock | 144 @Mock |
140 private CellIdentityGsm mCellIdentityGsm; | 145 private CellIdentityGsm mCellIdentityGsm; |
141 @Mock | 146 @Mock |
142 private CellIdentityCdma mCellIdentityCdma; | 147 private CellIdentityCdma mCellIdentityCdma; |
| 148 @Mock |
| 149 private Intent mNetworkStateChangedIntent; |
143 | 150 |
144 @Before | 151 @Before |
145 public void setUp() { | 152 public void setUp() { |
146 MockitoAnnotations.initMocks(this); | 153 MockitoAnnotations.initMocks(this); |
147 PlatformNetworksManager.sTimeProvider = new PlatformNetworksManager.Time
Provider() { | 154 PlatformNetworksManager.sTimeProvider = new PlatformNetworksManager.Time
Provider() { |
148 @Override | 155 @Override |
149 public long getCurrentTime() { | 156 public long getCurrentTime() { |
150 return CURRENT_TIME_MS; | 157 return CURRENT_TIME_MS; |
151 } | 158 } |
152 @Override | 159 @Override |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
213 when(mCellIdentityCdma.getNetworkId()).thenReturn(CDMA_CELL.locationArea
Code().intValue()); | 220 when(mCellIdentityCdma.getNetworkId()).thenReturn(CDMA_CELL.locationArea
Code().intValue()); |
214 when(mCellIdentityCdma.getSystemId()).thenReturn(CDMA_CELL.mobileNetwork
Code().intValue()); | 221 when(mCellIdentityCdma.getSystemId()).thenReturn(CDMA_CELL.mobileNetwork
Code().intValue()); |
215 when(mCellInfoCdma.getCellIdentity()).thenReturn(mCellIdentityCdma); | 222 when(mCellInfoCdma.getCellIdentity()).thenReturn(mCellIdentityCdma); |
216 when(mCellInfoCdma.isRegistered()).thenReturn(false); | 223 when(mCellInfoCdma.isRegistered()).thenReturn(false); |
217 when(mCellInfoCdma.getTimeStamp()) | 224 when(mCellInfoCdma.getTimeStamp()) |
218 .thenReturn(TimeUnit.MILLISECONDS.toNanos(CURRENT_ELAPSED_TIME_M
S - CDMA_CELL_AGE)); | 225 .thenReturn(TimeUnit.MILLISECONDS.toNanos(CURRENT_ELAPSED_TIME_M
S - CDMA_CELL_AGE)); |
219 when(mTelephonyManager.getAllCellInfo()) | 226 when(mTelephonyManager.getAllCellInfo()) |
220 .thenReturn( | 227 .thenReturn( |
221 Arrays.asList(mCellInfoLte, mCellInfoWcdma, mCellInfoGsm
, mCellInfoCdma)); | 228 Arrays.asList(mCellInfoLte, mCellInfoWcdma, mCellInfoGsm
, mCellInfoCdma)); |
222 allPermissionsGranted(); | 229 allPermissionsGranted(); |
| 230 |
| 231 when(mContext.registerReceiver(eq(null), any(IntentFilter.class))) |
| 232 .thenReturn(mNetworkStateChangedIntent); |
| 233 when(mNetworkStateChangedIntent.getParcelableExtra(eq(WifiManager.EXTRA_
WIFI_INFO))) |
| 234 .thenReturn(mWifiInfo); |
223 } | 235 } |
224 | 236 |
225 @Test | 237 @Test |
226 public void testGetConnectedCell_JBMR1() { | 238 public void testGetConnectedCell_JBMR1() { |
227 ReflectionHelpers.setStaticField( | 239 ReflectionHelpers.setStaticField( |
228 Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN_M
R1); | 240 Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.JELLY_BEAN_M
R1); |
229 VisibleCell visibleCell = | 241 VisibleCell visibleCell = |
230 PlatformNetworksManager.getConnectedCell(mContext, mTelephonyMan
ager); | 242 PlatformNetworksManager.getConnectedCell(mContext, mTelephonyMan
ager); |
231 assertEquals(LTE_CELL, visibleCell); | 243 assertEquals(LTE_CELL, visibleCell); |
232 assertEquals(visibleCell.timestampMs(), Long.valueOf(CURRENT_TIME_MS - L
TE_CELL_AGE)); | 244 assertEquals(visibleCell.timestampMs(), Long.valueOf(CURRENT_TIME_MS - L
TE_CELL_AGE)); |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 public void testGetConnectedWifi_allPermissionsDenied() { | 373 public void testGetConnectedWifi_allPermissionsDenied() { |
362 ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.V
ERSION_CODES.M); | 374 ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.V
ERSION_CODES.M); |
363 allPermissionsDenied(); | 375 allPermissionsDenied(); |
364 VisibleWifi visibleWifi = PlatformNetworksManager.getConnectedWifi(mCont
ext, mWifiManager); | 376 VisibleWifi visibleWifi = PlatformNetworksManager.getConnectedWifi(mCont
ext, mWifiManager); |
365 assertEquals(UNKNOWN_VISIBLE_WIFI, visibleWifi); | 377 assertEquals(UNKNOWN_VISIBLE_WIFI, visibleWifi); |
366 assertNull(visibleWifi.timestampMs()); | 378 assertNull(visibleWifi.timestampMs()); |
367 } | 379 } |
368 | 380 |
369 @Test | 381 @Test |
370 public void testGetConnectedWifi_locationGrantedWifiDenied() { | 382 public void testGetConnectedWifi_locationGrantedWifiDenied() { |
371 ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.V
ERSION_CODES.M); | 383 ReflectionHelpers.setStaticField( |
| 384 Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.LOLLIPOP); |
372 locationGrantedWifiDenied(); | 385 locationGrantedWifiDenied(); |
373 VisibleWifi visibleWifi = PlatformNetworksManager.getConnectedWifi(mCont
ext, mWifiManager); | 386 VisibleWifi visibleWifi = PlatformNetworksManager.getConnectedWifi(mCont
ext, mWifiManager); |
| 387 assertEquals(CONNECTED_WIFI, visibleWifi); |
| 388 assertEquals(Long.valueOf(CURRENT_TIME_MS), visibleWifi.timestampMs()); |
| 389 verifyNetworkStateAction(); |
| 390 } |
| 391 |
| 392 @Test |
| 393 public void testGetConnectedWifi_locationGrantedWifiDenied_noWifiInfo() { |
| 394 ReflectionHelpers.setStaticField( |
| 395 Build.VERSION.class, "SDK_INT", Build.VERSION_CODES.LOLLIPOP); |
| 396 locationGrantedWifiDenied(); |
| 397 when(mNetworkStateChangedIntent.getParcelableExtra(eq(WifiManager.EXTRA_
WIFI_INFO))) |
| 398 .thenReturn(null); |
| 399 VisibleWifi visibleWifi = PlatformNetworksManager.getConnectedWifi(mCont
ext, mWifiManager); |
374 assertEquals(UNKNOWN_VISIBLE_WIFI, visibleWifi); | 400 assertEquals(UNKNOWN_VISIBLE_WIFI, visibleWifi); |
375 assertNull(visibleWifi.timestampMs()); | 401 assertNull(visibleWifi.timestampMs()); |
| 402 verifyNetworkStateAction(); |
376 } | 403 } |
377 | 404 |
378 @Test | 405 @Test |
379 public void testGetConnectedWifi_locationDeniedWifiGranted() { | 406 public void testGetConnectedWifi_locationDeniedWifiGranted() { |
380 ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.V
ERSION_CODES.M); | 407 ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", Build.V
ERSION_CODES.M); |
381 locationDeniedWifiGranted(); | 408 locationDeniedWifiGranted(); |
382 VisibleWifi visibleWifi = PlatformNetworksManager.getConnectedWifi(mCont
ext, mWifiManager); | 409 VisibleWifi visibleWifi = PlatformNetworksManager.getConnectedWifi(mCont
ext, mWifiManager); |
383 assertEquals(UNKNOWN_VISIBLE_WIFI, visibleWifi); | 410 assertEquals(UNKNOWN_VISIBLE_WIFI, visibleWifi); |
384 assertNull(visibleWifi.timestampMs()); | 411 assertNull(visibleWifi.timestampMs()); |
385 } | 412 } |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
481 mContext, true /* includeAllVisibleNotConnectedNetworks */); | 508 mContext, true /* includeAllVisibleNotConnectedNetworks */); |
482 | 509 |
483 assertTrue(visibleNetworks.isEmpty()); | 510 assertTrue(visibleNetworks.isEmpty()); |
484 } | 511 } |
485 | 512 |
486 @Test | 513 @Test |
487 public void testComputeVisibleNetworks_locationGrantedWifiDenied() { | 514 public void testComputeVisibleNetworks_locationGrantedWifiDenied() { |
488 Set<VisibleCell> expectedVisibleCells = | 515 Set<VisibleCell> expectedVisibleCells = |
489 new HashSet<VisibleCell>(Arrays.asList(LTE_CELL, WCDMA_CELL, GSM
_CELL, CDMA_CELL)); | 516 new HashSet<VisibleCell>(Arrays.asList(LTE_CELL, WCDMA_CELL, GSM
_CELL, CDMA_CELL)); |
490 Set<VisibleWifi> expectedVisibleWifis = Collections.emptySet(); | 517 Set<VisibleWifi> expectedVisibleWifis = Collections.emptySet(); |
491 VisibleNetworks expectedVisibleNetworks = | 518 VisibleNetworks expectedVisibleNetworks = VisibleNetworks.create( |
492 VisibleNetworks.create(null, LTE_CELL, expectedVisibleWifis, exp
ectedVisibleCells); | 519 CONNECTED_WIFI, LTE_CELL, expectedVisibleWifis, expectedVisibleC
ells); |
493 locationGrantedWifiDenied(); | 520 locationGrantedWifiDenied(); |
494 | 521 |
495 VisibleNetworks visibleNetworks = PlatformNetworksManager.computeVisible
Networks( | 522 VisibleNetworks visibleNetworks = PlatformNetworksManager.computeVisible
Networks( |
496 mContext, true /* includeAllVisibleNotConnectedNetworks */); | 523 mContext, true /* includeAllVisibleNotConnectedNetworks */); |
497 | 524 |
498 assertEquals(expectedVisibleNetworks, visibleNetworks); | 525 assertEquals(expectedVisibleNetworks, visibleNetworks); |
| 526 verifyNetworkStateAction(); |
499 } | 527 } |
500 | 528 |
501 @Test | 529 @Test |
502 public void testComputeVisibleNetworks_locationDeniedWifiGranted() { | 530 public void testComputeVisibleNetworks_locationDeniedWifiGranted() { |
503 Set<VisibleCell> expectedVisibleCells = Collections.emptySet(); | 531 Set<VisibleCell> expectedVisibleCells = Collections.emptySet(); |
504 Set<VisibleWifi> expectedVisibleWifis = Collections.emptySet(); | 532 Set<VisibleWifi> expectedVisibleWifis = Collections.emptySet(); |
505 locationDeniedWifiGranted(); | 533 locationDeniedWifiGranted(); |
506 VisibleNetworks expectedVisibleNetworks = | 534 VisibleNetworks expectedVisibleNetworks = |
507 VisibleNetworks.create(null, null, expectedVisibleWifis, expecte
dVisibleCells); | 535 VisibleNetworks.create(null, null, expectedVisibleWifis, expecte
dVisibleCells); |
508 | 536 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 when(mContext.checkPermission(eq(Manifest.permission.ACCESS_COARSE_LOCAT
ION), | 568 when(mContext.checkPermission(eq(Manifest.permission.ACCESS_COARSE_LOCAT
ION), |
541 any(Integer.class), any(Integer.class))) | 569 any(Integer.class), any(Integer.class))) |
542 .thenReturn(coarseLocationPermission); | 570 .thenReturn(coarseLocationPermission); |
543 when(mContext.checkPermission(eq(Manifest.permission.ACCESS_FINE_LOCATIO
N), | 571 when(mContext.checkPermission(eq(Manifest.permission.ACCESS_FINE_LOCATIO
N), |
544 any(Integer.class), any(Integer.class))) | 572 any(Integer.class), any(Integer.class))) |
545 .thenReturn(fineLocationPermission); | 573 .thenReturn(fineLocationPermission); |
546 when(mContext.checkPermission(eq(Manifest.permission.ACCESS_WIFI_STATE),
any(Integer.class), | 574 when(mContext.checkPermission(eq(Manifest.permission.ACCESS_WIFI_STATE),
any(Integer.class), |
547 any(Integer.class))) | 575 any(Integer.class))) |
548 .thenReturn(wifiStatePermission); | 576 .thenReturn(wifiStatePermission); |
549 } | 577 } |
| 578 |
| 579 private void verifyNetworkStateAction() { |
| 580 verify(mContext).registerReceiver(eq(null), argThat(new ArgumentMatcher<
IntentFilter>() { |
| 581 @Override |
| 582 public boolean matches(IntentFilter intentFilter) { |
| 583 return intentFilter.hasAction(WifiManager.NETWORK_STATE_CHANGED_
ACTION); |
| 584 } |
| 585 })); |
| 586 } |
550 } | 587 } |
OLD | NEW |