Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package org.chromium.chrome.browser.omnibox.geo; | |
| 6 | |
| 7 import static org.junit.Assert.assertEquals; | |
| 8 | |
| 9 import android.content.Context; | |
| 10 | |
| 11 import org.junit.Before; | |
| 12 import org.junit.Test; | |
| 13 import org.junit.runner.RunWith; | |
| 14 import org.mockito.Mock; | |
| 15 import org.mockito.MockitoAnnotations; | |
| 16 import org.robolectric.annotation.Config; | |
| 17 import org.robolectric.annotation.Implementation; | |
| 18 import org.robolectric.annotation.Implements; | |
| 19 | |
| 20 import org.chromium.chrome.browser.omnibox.geo.VisibleNetworks.VisibleCell; | |
| 21 import org.chromium.chrome.browser.omnibox.geo.VisibleNetworks.VisibleWifi; | |
| 22 import org.chromium.chrome.browser.omnibox.geo.VisibleNetworksTrackerTest.Shadow PlatformNetworksManager; | |
| 23 import org.chromium.testing.local.CustomShadowAsyncTask; | |
| 24 import org.chromium.testing.local.LocalRobolectricTestRunner; | |
| 25 | |
| 26 import java.util.Arrays; | |
| 27 import java.util.HashSet; | |
| 28 import java.util.LinkedList; | |
| 29 import java.util.List; | |
| 30 | |
| 31 /** | |
| 32 * Robolectric tests for {@link VisibleNetworksTracker}. | |
| 33 */ | |
| 34 @RunWith(LocalRobolectricTestRunner.class) | |
| 35 @Config(manifest = Config.NONE, | |
| 36 shadows = {ShadowPlatformNetworksManager.class, CustomShadowAsyncTask.cl ass}) | |
| 37 public class VisibleNetworksTrackerTest { | |
| 38 private static final VisibleWifi VISIBLE_WIFI_1 = | |
| 39 VisibleWifi.create("ssid1", "11:11:11:11:11:11", 1, 10L); | |
| 40 private static final VisibleWifi VISIBLE_WIFI_2 = | |
| 41 VisibleWifi.create("ssid2", "11:11:11:11:11:12", 2, 20L); | |
| 42 private static final VisibleCell VISIBLE_CELL_1 = | |
| 43 VisibleCell.builder(VisibleCell.GSM_RADIO_TYPE) | |
| 44 .setCellId(30) | |
| 45 .setLocationAreaCode(31) | |
| 46 .setMobileCountryCode(32) | |
| 47 .setMobileNetworkCode(33) | |
| 48 .setTimestamp(30L) | |
| 49 .build(); | |
| 50 private static final VisibleCell VISIBLE_CELL_2 = | |
| 51 VisibleCell.builder(VisibleCell.CDMA_RADIO_TYPE) | |
| 52 .setCellId(40) | |
| 53 .setLocationAreaCode(41) | |
| 54 .setMobileCountryCode(42) | |
| 55 .setMobileNetworkCode(43) | |
| 56 .setTimestamp(40L) | |
| 57 .build(); | |
| 58 | |
| 59 private static final VisibleNetworks FIRST_ALL_VISIBLE_NETWORKS = | |
| 60 VisibleNetworks.create(VISIBLE_WIFI_1, VISIBLE_CELL_1, | |
| 61 new HashSet<VisibleWifi>(Arrays.asList(VISIBLE_WIFI_1, VISIB LE_WIFI_2)), | |
| 62 new HashSet<VisibleCell>(Arrays.asList(VISIBLE_CELL_1, VISIB LE_CELL_2))); | |
| 63 private static final VisibleNetworks SECOND_ALL_VISIBLE_NETWORKS = VisibleNe tworks.create( | |
| 64 VISIBLE_WIFI_2, VISIBLE_CELL_2, new HashSet<VisibleWifi>(Arrays.asLi st(VISIBLE_WIFI_1)), | |
| 65 new HashSet<VisibleCell>(Arrays.asList(VISIBLE_CELL_1))); | |
| 66 private static final VisibleNetworks FIRST_ONLY_CONNECTED_NETWORKS = | |
| 67 VisibleNetworks.create(VISIBLE_WIFI_1, VISIBLE_CELL_1, null, null); | |
| 68 private static final VisibleNetworks SECOND_ONLY_CONNECTED_NETWORKS = | |
| 69 VisibleNetworks.create(VISIBLE_WIFI_2, VISIBLE_CELL_2, null, null); | |
| 70 | |
| 71 private static long sCurrentTime; | |
| 72 private static final long CURRENT_TIME_MS = 90000000L; | |
| 73 private static final int AGE_THRESHOLD = 5 * 60 * 1000; // 5 min | |
|
Ted C
2017/05/18 02:13:33
i would just make the other constant protected
lbargu
2017/05/18 14:15:29
Done.
| |
| 74 private static final long ELAPSED_UNDER_THRESHOLD_TIME_MS = AGE_THRESHOLD - 1000L; | |
| 75 private static final long ELAPSED_OVER_THRESHOLD_TIME_MS = AGE_THRESHOLD + 1 000L; | |
| 76 | |
| 77 @Mock | |
| 78 private static Context sContext; | |
| 79 | |
| 80 @Before | |
| 81 public void setUp() { | |
| 82 MockitoAnnotations.initMocks(this); | |
| 83 sCurrentTime = CURRENT_TIME_MS; | |
| 84 VisibleNetworksTracker.sTimeProvider = new VisibleNetworksTracker.TimePr ovider() { | |
|
Ted C
2017/05/18 02:13:33
instead of introducing the TimeProvider, can you i
lbargu
2017/05/18 14:15:29
Done.
| |
| 85 @Override | |
| 86 public long getCurrentTime() { | |
| 87 return sCurrentTime; | |
| 88 } | |
| 89 }; | |
| 90 ShadowPlatformNetworksManager.sAllVisibleNetworks = new LinkedList<>( | |
| 91 Arrays.asList(FIRST_ALL_VISIBLE_NETWORKS, SECOND_ALL_VISIBLE_NET WORKS)); | |
| 92 ShadowPlatformNetworksManager.sOnlyConnectedNetworks = new LinkedList<>( | |
| 93 Arrays.asList(FIRST_ONLY_CONNECTED_NETWORKS, SECOND_ONLY_CONNECT ED_NETWORKS)); | |
| 94 | |
| 95 // Make sure that the cache is empty before every test. | |
| 96 VisibleNetworksTracker.clearCache(); | |
| 97 } | |
| 98 | |
| 99 @Test | |
| 100 public void testGetLastKnownVisibleNetworks_emptyCache() { | |
| 101 // Cache is empty before the getLastKnownVisibleNetworks call. The visib leNetworks | |
| 102 // are computed only for the connected wifi and cell and then cached. | |
| 103 VisibleNetworks visibleNetworks = | |
| 104 VisibleNetworksTracker.getLastKnownVisibleNetworks(sContext); | |
| 105 | |
| 106 assertEquals(FIRST_ONLY_CONNECTED_NETWORKS, visibleNetworks); | |
| 107 assertEquals( | |
| 108 FIRST_ONLY_CONNECTED_NETWORKS, VisibleNetworksTracker.getCachedV isibleNetworks()); | |
| 109 assertEquals(CURRENT_TIME_MS, VisibleNetworksTracker.getCachedVisibleNet worksTime()); | |
| 110 } | |
| 111 | |
| 112 @Test | |
| 113 public void testGetLastKnownVisibleNetworks_withValidCache() { | |
| 114 VisibleNetworks visibleNetworks = | |
| 115 VisibleNetworksTracker.getLastKnownVisibleNetworks(sContext); | |
| 116 assertEquals(FIRST_ONLY_CONNECTED_NETWORKS, visibleNetworks); | |
| 117 | |
| 118 // Time to consider the first cached networks still as valid. | |
| 119 sCurrentTime = CURRENT_TIME_MS + ELAPSED_UNDER_THRESHOLD_TIME_MS; | |
| 120 visibleNetworks = VisibleNetworksTracker.getLastKnownVisibleNetworks(sCo ntext); | |
| 121 | |
| 122 assertEquals(FIRST_ONLY_CONNECTED_NETWORKS, visibleNetworks); | |
| 123 assertEquals( | |
| 124 FIRST_ONLY_CONNECTED_NETWORKS, VisibleNetworksTracker.getCachedV isibleNetworks()); | |
| 125 assertEquals(CURRENT_TIME_MS, VisibleNetworksTracker.getCachedVisibleNet worksTime()); | |
| 126 } | |
| 127 | |
| 128 @Test | |
| 129 public void testGetLastKnownVisibleNetworks_withOldCache() { | |
| 130 VisibleNetworks visibleNetworks = | |
| 131 VisibleNetworksTracker.getLastKnownVisibleNetworks(sContext); | |
| 132 assertEquals(FIRST_ONLY_CONNECTED_NETWORKS, visibleNetworks); | |
| 133 | |
| 134 // Time to consider the first cached networks as invalid. Should fetch t he second ones. | |
| 135 sCurrentTime = CURRENT_TIME_MS + ELAPSED_OVER_THRESHOLD_TIME_MS; | |
| 136 visibleNetworks = VisibleNetworksTracker.getLastKnownVisibleNetworks(sCo ntext); | |
| 137 | |
| 138 assertEquals(SECOND_ONLY_CONNECTED_NETWORKS, visibleNetworks); | |
| 139 assertEquals( | |
| 140 SECOND_ONLY_CONNECTED_NETWORKS, VisibleNetworksTracker.getCached VisibleNetworks()); | |
| 141 assertEquals(CURRENT_TIME_MS + ELAPSED_OVER_THRESHOLD_TIME_MS, | |
| 142 VisibleNetworksTracker.getCachedVisibleNetworksTime()); | |
| 143 } | |
| 144 | |
| 145 @Test | |
| 146 public void testRefreshVisibleNetworks_emptyCache() { | |
| 147 // Cache is empty before the refreshVisibleNetworks call. The visibleNet works are | |
| 148 // computed including all the visible cells and wifis then cached. | |
| 149 VisibleNetworksTracker.refreshVisibleNetworks(sContext); | |
| 150 | |
| 151 assertEquals(FIRST_ALL_VISIBLE_NETWORKS, VisibleNetworksTracker.getCache dVisibleNetworks()); | |
| 152 assertEquals(CURRENT_TIME_MS, VisibleNetworksTracker.getCachedVisibleNet worksTime()); | |
| 153 } | |
| 154 | |
| 155 @Test | |
| 156 public void testRefreshVisibleNetworks_validCacheOnlyConnected() { | |
| 157 VisibleNetworks visibleNetworks = | |
| 158 VisibleNetworksTracker.getLastKnownVisibleNetworks(sContext); | |
| 159 assertEquals(FIRST_ONLY_CONNECTED_NETWORKS, visibleNetworks); | |
| 160 | |
| 161 // Time to consider the first cached networks still as valid, refresh sh ould be a noop. | |
| 162 sCurrentTime = CURRENT_TIME_MS + ELAPSED_UNDER_THRESHOLD_TIME_MS; | |
| 163 VisibleNetworksTracker.refreshVisibleNetworks(sContext); | |
| 164 | |
| 165 assertEquals( | |
| 166 FIRST_ONLY_CONNECTED_NETWORKS, VisibleNetworksTracker.getCachedV isibleNetworks()); | |
| 167 assertEquals(CURRENT_TIME_MS, VisibleNetworksTracker.getCachedVisibleNet worksTime()); | |
| 168 } | |
| 169 | |
| 170 @Test | |
| 171 public void testRefreshVisibleNetworks_validCacheAllVisible() { | |
| 172 VisibleNetworksTracker.refreshVisibleNetworks(sContext); | |
| 173 assertEquals(FIRST_ALL_VISIBLE_NETWORKS, VisibleNetworksTracker.getCache dVisibleNetworks()); | |
| 174 assertEquals(CURRENT_TIME_MS, VisibleNetworksTracker.getCachedVisibleNet worksTime()); | |
| 175 | |
| 176 // Time to consider the first cached networks still as valid, refresh sh ould be a noop. | |
| 177 sCurrentTime = CURRENT_TIME_MS + ELAPSED_UNDER_THRESHOLD_TIME_MS; | |
| 178 VisibleNetworksTracker.refreshVisibleNetworks(sContext); | |
| 179 | |
| 180 assertEquals(FIRST_ALL_VISIBLE_NETWORKS, VisibleNetworksTracker.getCache dVisibleNetworks()); | |
| 181 assertEquals(CURRENT_TIME_MS, VisibleNetworksTracker.getCachedVisibleNet worksTime()); | |
| 182 } | |
| 183 | |
| 184 @Test | |
| 185 public void testRefreshVisibleNetworks_oldCacheOnlyConnected() { | |
| 186 VisibleNetworks visibleNetworks = | |
| 187 VisibleNetworksTracker.getLastKnownVisibleNetworks(sContext); | |
| 188 assertEquals(FIRST_ONLY_CONNECTED_NETWORKS, visibleNetworks); | |
| 189 | |
| 190 // Time to consider the first cached networks as invalid. Should fetch t he second ones. | |
| 191 sCurrentTime = CURRENT_TIME_MS + ELAPSED_OVER_THRESHOLD_TIME_MS; | |
| 192 VisibleNetworksTracker.refreshVisibleNetworks(sContext); | |
| 193 | |
| 194 assertEquals(FIRST_ALL_VISIBLE_NETWORKS, VisibleNetworksTracker.getCache dVisibleNetworks()); | |
| 195 assertEquals(CURRENT_TIME_MS + ELAPSED_OVER_THRESHOLD_TIME_MS, | |
| 196 VisibleNetworksTracker.getCachedVisibleNetworksTime()); | |
| 197 } | |
| 198 | |
| 199 @Test | |
| 200 public void testRefreshVisibleNetworks_oldCacheAllVisible() { | |
| 201 VisibleNetworksTracker.refreshVisibleNetworks(sContext); | |
| 202 assertEquals(FIRST_ALL_VISIBLE_NETWORKS, VisibleNetworksTracker.getCache dVisibleNetworks()); | |
| 203 assertEquals(CURRENT_TIME_MS, VisibleNetworksTracker.getCachedVisibleNet worksTime()); | |
| 204 | |
| 205 // Time to consider the first cached networks as invalid. Should fetch t he second ones. | |
| 206 sCurrentTime = CURRENT_TIME_MS + ELAPSED_OVER_THRESHOLD_TIME_MS; | |
| 207 VisibleNetworksTracker.refreshVisibleNetworks(sContext); | |
| 208 | |
| 209 assertEquals( | |
| 210 SECOND_ALL_VISIBLE_NETWORKS, VisibleNetworksTracker.getCachedVis ibleNetworks()); | |
| 211 assertEquals(CURRENT_TIME_MS + ELAPSED_OVER_THRESHOLD_TIME_MS, | |
| 212 VisibleNetworksTracker.getCachedVisibleNetworksTime()); | |
| 213 } | |
| 214 | |
| 215 /** | |
| 216 * Shadow PlatformNetworksManager for robolectric tests. | |
| 217 */ | |
| 218 @Implements(PlatformNetworksManager.class) | |
| 219 public static class ShadowPlatformNetworksManager { | |
| 220 private static List<VisibleNetworks> sAllVisibleNetworks; | |
| 221 private static List<VisibleNetworks> sOnlyConnectedNetworks; | |
| 222 | |
| 223 @Implementation | |
| 224 public static VisibleNetworks computeVisibleNetworks( | |
| 225 Context context, boolean includeAllVisibleNotConnectedNetworks) { | |
| 226 return includeAllVisibleNotConnectedNetworks ? sAllVisibleNetworks.r emove(0) | |
| 227 : sOnlyConnectedNetwork s.remove(0); | |
| 228 } | |
| 229 } | |
| 230 } | |
| OLD | NEW |