| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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.physicalweb; | 5 package org.chromium.chrome.browser.physicalweb; |
| 6 | 6 |
| 7 import static org.junit.Assert.assertEquals; |
| 8 import static org.junit.Assert.assertFalse; |
| 9 import static org.junit.Assert.assertTrue; |
| 10 |
| 7 import android.content.SharedPreferences; | 11 import android.content.SharedPreferences; |
| 8 import android.support.test.filters.SmallTest; | 12 |
| 9 import android.test.InstrumentationTestCase; | 13 import org.junit.Before; |
| 14 import org.junit.Test; |
| 15 import org.junit.runner.RunWith; |
| 16 |
| 17 import org.robolectric.RuntimeEnvironment; |
| 18 import org.robolectric.annotation.Config; |
| 19 import org.robolectric.shadows.ShadowLooper; |
| 10 | 20 |
| 11 import org.chromium.base.ContextUtils; | 21 import org.chromium.base.ContextUtils; |
| 12 import org.chromium.base.test.util.DisabledTest; | 22 import org.chromium.base.test.util.DisabledTest; |
| 13 import org.chromium.base.test.util.FlakyTest; | 23 import org.chromium.base.test.util.FlakyTest; |
| 14 import org.chromium.base.test.util.RetryOnFailure; | 24 import org.chromium.base.test.util.RetryOnFailure; |
| 15 import org.chromium.content.browser.test.util.Criteria; | 25 import org.chromium.testing.local.LocalRobolectricTestRunner; |
| 16 import org.chromium.content.browser.test.util.CriteriaHelper; | |
| 17 | 26 |
| 18 import java.util.ArrayList; | 27 import java.util.ArrayList; |
| 19 import java.util.HashSet; | 28 import java.util.HashSet; |
| 20 import java.util.List; | 29 import java.util.List; |
| 21 import java.util.Set; | 30 import java.util.Set; |
| 31 import java.util.concurrent.TimeUnit; |
| 32 import java.util.concurrent.locks.Condition; |
| 33 import java.util.concurrent.locks.Lock; |
| 34 import java.util.concurrent.locks.ReentrantLock; |
| 22 | 35 |
| 23 /** | 36 /** |
| 24 * Tests for {@link UrlManager}. | 37 * Tests for {@link UrlManager}. |
| 25 */ | 38 */ |
| 26 public class UrlManagerTest extends InstrumentationTestCase { | 39 @RunWith(LocalRobolectricTestRunner.class) |
| 40 @Config(manifest = Config.NONE) |
| 41 public class UrlManagerTest { |
| 27 private static final String URL1 = "https://example.com/"; | 42 private static final String URL1 = "https://example.com/"; |
| 28 private static final String TITLE1 = "Example"; | 43 private static final String TITLE1 = "Example"; |
| 29 private static final String DESC1 = "Example Website"; | 44 private static final String DESC1 = "Example Website"; |
| 30 private static final String URL2 = "https://google.com/"; | 45 private static final String URL2 = "https://google.com/"; |
| 31 private static final String TITLE2 = "Google"; | 46 private static final String TITLE2 = "Google"; |
| 32 private static final String DESC2 = "Search the Web"; | 47 private static final String DESC2 = "Search the Web"; |
| 33 private static final String URL3 = "https://html5zombo.com/"; | 48 private static final String URL3 = "https://html5zombo.com/"; |
| 34 private static final String URL4 = "https://hooli.xyz/"; | 49 private static final String URL4 = "https://hooli.xyz/"; |
| 35 private static final String URL5 = "https://www.gmail.com/mail/help/paper/"; | 50 private static final String URL5 = "https://www.gmail.com/mail/help/paper/"; |
| 36 private static final String GROUP1 = "group1"; | 51 private static final String GROUP1 = "group1"; |
| 37 private static final String GROUP2 = "group2"; | 52 private static final String GROUP2 = "group2"; |
| 38 private static final String GROUP3 = "group3"; | 53 private static final String GROUP3 = "group3"; |
| 39 private static final String PREF_PHYSICAL_WEB = "physical_web"; | 54 private static final String PREF_PHYSICAL_WEB = "physical_web"; |
| 40 private static final int PHYSICAL_WEB_OFF = 0; | 55 private static final int PHYSICAL_WEB_OFF = 0; |
| 41 private static final int PHYSICAL_WEB_ON = 1; | 56 private static final int PHYSICAL_WEB_ON = 1; |
| 42 private static final int PHYSICAL_WEB_ONBOARDING = 2; | 57 private static final int PHYSICAL_WEB_ONBOARDING = 2; |
| 58 private static final double EPSILON = .001; |
| 43 private UrlManager mUrlManager = null; | 59 private UrlManager mUrlManager = null; |
| 44 private MockPwsClient mMockPwsClient = null; | 60 private MockPwsClient mMockPwsClient = null; |
| 45 | 61 |
| 46 @Override | 62 @Before |
| 47 protected void setUp() throws Exception { | 63 public void setUp() { |
| 48 super.setUp(); | 64 ContextUtils.initApplicationContextForTests(RuntimeEnvironment.applicati
on); |
| 49 ContextUtils.getAppSharedPreferences().edit() | 65 ContextUtils.getAppSharedPreferences() |
| 66 .edit() |
| 50 .putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ON) | 67 .putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ON) |
| 51 .apply(); | 68 .apply(); |
| 52 UrlManager.clearPrefsForTesting(); | 69 UrlManager.clearPrefsForTesting(); |
| 53 mUrlManager = new UrlManager(); | 70 mUrlManager = new UrlManager(); |
| 54 mMockPwsClient = new MockPwsClient(); | 71 mMockPwsClient = new MockPwsClient(); |
| 55 mUrlManager.overridePwsClientForTesting(mMockPwsClient); | 72 mUrlManager.overridePwsClientForTesting(mMockPwsClient); |
| 56 } | 73 } |
| 57 | 74 |
| 58 private void addPwsResult1() { | 75 private void addPwsResult1() { |
| 59 ArrayList<PwsResult> results = new ArrayList<>(); | 76 ArrayList<PwsResult> results = new ArrayList<>(); |
| (...skipping 21 matching lines...) Expand all Loading... |
| 81 | 98 |
| 82 private void removeUrlInfo2() { | 99 private void removeUrlInfo2() { |
| 83 mUrlManager.removeUrl(new UrlInfo(URL2)); | 100 mUrlManager.removeUrl(new UrlInfo(URL2)); |
| 84 } | 101 } |
| 85 | 102 |
| 86 private void addEmptyPwsResult() { | 103 private void addEmptyPwsResult() { |
| 87 mMockPwsClient.addPwsResults(new ArrayList<PwsResult>()); | 104 mMockPwsClient.addPwsResults(new ArrayList<PwsResult>()); |
| 88 } | 105 } |
| 89 | 106 |
| 90 private void setOnboarding() { | 107 private void setOnboarding() { |
| 91 ContextUtils.getAppSharedPreferences().edit() | 108 ContextUtils.getAppSharedPreferences() |
| 109 .edit() |
| 92 .putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ONBOARDING) | 110 .putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ONBOARDING) |
| 93 .apply(); | 111 .apply(); |
| 94 } | 112 } |
| 95 | 113 |
| 96 @SmallTest | 114 @Test |
| 97 @RetryOnFailure | |
| 98 public void testAddUrlAfterClearAllUrlsWorks() { | 115 public void testAddUrlAfterClearAllUrlsWorks() { |
| 99 addPwsResult1(); | 116 addPwsResult1(); |
| 100 addPwsResult2(); | 117 addPwsResult2(); |
| 101 addPwsResult1(); | 118 addPwsResult1(); |
| 102 addPwsResult2(); | 119 addPwsResult2(); |
| 103 addUrlInfo1(); | 120 addUrlInfo1(); |
| 104 addUrlInfo2(); | 121 addUrlInfo2(); |
| 105 getInstrumentation().waitForIdleSync(); | 122 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 106 mUrlManager.clearAllUrls(); | 123 mUrlManager.clearAllUrls(); |
| 107 | 124 |
| 108 // Add some more URLs...this should not crash if we cleared correctly. | 125 // Add some more URLs...this should not crash if we cleared correctly. |
| 109 addUrlInfo1(); | 126 addUrlInfo1(); |
| 110 addUrlInfo2(); | 127 addUrlInfo2(); |
| 111 getInstrumentation().waitForIdleSync(); | 128 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 112 List<UrlInfo> urlInfos = mUrlManager.getUrls(); | 129 List<UrlInfo> urlInfos = mUrlManager.getUrls(); |
| 113 assertEquals(2, urlInfos.size()); | 130 assertEquals(2, urlInfos.size()); |
| 114 } | 131 } |
| 115 | 132 |
| 116 @SmallTest | 133 @Test |
| 117 @RetryOnFailure | |
| 118 public void testClearNearbyUrlsWorks() { | 134 public void testClearNearbyUrlsWorks() { |
| 119 addPwsResult1(); | 135 addPwsResult1(); |
| 120 addPwsResult2(); | 136 addPwsResult2(); |
| 121 addUrlInfo1(); | 137 addUrlInfo1(); |
| 122 addUrlInfo2(); | 138 addUrlInfo2(); |
| 123 getInstrumentation().waitForIdleSync(); | 139 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 124 | 140 |
| 125 mUrlManager.clearNearbyUrls(); | 141 mUrlManager.clearNearbyUrls(); |
| 126 | 142 |
| 127 // Test that the URLs are not nearby, but do exist in the cache. | 143 // Test that the URLs are not nearby, but do exist in the cache. |
| 128 List<UrlInfo> urlInfos = mUrlManager.getUrls(true); | 144 List<UrlInfo> urlInfos = mUrlManager.getUrls(true); |
| 129 assertEquals(0, urlInfos.size()); | 145 assertEquals(0, urlInfos.size()); |
| 130 assertTrue(mUrlManager.containsInAnyCache(URL1)); | 146 assertTrue(mUrlManager.containsInAnyCache(URL1)); |
| 131 assertTrue(mUrlManager.containsInAnyCache(URL2)); | 147 assertTrue(mUrlManager.containsInAnyCache(URL2)); |
| 132 | 148 |
| 133 mUrlManager.clearAllUrls(); | 149 mUrlManager.clearAllUrls(); |
| 134 | 150 |
| 135 // Test that cache is empty. | 151 // Test that cache is empty. |
| 136 assertFalse(mUrlManager.containsInAnyCache(URL1)); | 152 assertFalse(mUrlManager.containsInAnyCache(URL1)); |
| 137 assertFalse(mUrlManager.containsInAnyCache(URL2)); | 153 assertFalse(mUrlManager.containsInAnyCache(URL2)); |
| 138 } | 154 } |
| 139 | 155 |
| 140 @SmallTest | 156 @Test |
| 141 @RetryOnFailure | 157 @RetryOnFailure |
| 142 public void testAddUrlGarbageCollectsForSize() throws Exception { | 158 public void testAddUrlGarbageCollectsForSize() throws Exception { |
| 143 // Add and remove 101 URLs, making sure one is clearly slightly older th
an the others. | 159 // Add and remove 101 URLs, making sure one is clearly slightly older th
an the others. |
| 144 addEmptyPwsResult(); | 160 addEmptyPwsResult(); |
| 145 UrlInfo urlInfo = new UrlInfo(URL1, -1.0, System.currentTimeMillis() - 1
); | 161 UrlInfo urlInfo = new UrlInfo(URL1, -1.0, System.currentTimeMillis() - 1
); |
| 146 mUrlManager.addUrl(urlInfo); | 162 mUrlManager.addUrl(urlInfo); |
| 147 mUrlManager.removeUrl(urlInfo); | 163 mUrlManager.removeUrl(urlInfo); |
| 148 for (int i = 1; i <= mUrlManager.getMaxCacheSize(); i++) { | 164 for (int i = 1; i <= mUrlManager.getMaxCacheSize(); i++) { |
| 149 mMockPwsClient.addPwsResults(new ArrayList<PwsResult>()); | 165 mMockPwsClient.addPwsResults(new ArrayList<PwsResult>()); |
| 150 urlInfo = new UrlInfo(URL1 + i, -1.0, System.currentTimeMillis()); | 166 urlInfo = new UrlInfo(URL1 + i, -1.0, System.currentTimeMillis()); |
| 151 mUrlManager.addUrl(urlInfo); | 167 mUrlManager.addUrl(urlInfo); |
| 152 mUrlManager.removeUrl(urlInfo); | 168 mUrlManager.removeUrl(urlInfo); |
| 153 } | 169 } |
| 154 | 170 |
| 155 // Make our cache is missing the first URL and contains the others. | 171 // Make our cache is missing the first URL and contains the others. |
| 156 assertFalse(mUrlManager.containsInAnyCache(URL1)); | 172 assertFalse(mUrlManager.containsInAnyCache(URL1)); |
| 157 assertTrue(mUrlManager.containsInAnyCache(URL1 + 1)); | 173 assertTrue(mUrlManager.containsInAnyCache(URL1 + 1)); |
| 158 assertTrue(mUrlManager.containsInAnyCache(URL1 + mUrlManager.getMaxCache
Size())); | 174 assertTrue(mUrlManager.containsInAnyCache(URL1 + mUrlManager.getMaxCache
Size())); |
| 159 } | 175 } |
| 160 | 176 |
| 161 @SmallTest | 177 @Test |
| 162 public void testAddUrlGarbageCollectsForAge() throws Exception { | 178 public void testAddUrlGarbageCollectsForAge() throws Exception { |
| 163 // Add a URL with a phony timestamp. | 179 // Add a URL with a phony timestamp. |
| 164 addEmptyPwsResult(); | 180 addEmptyPwsResult(); |
| 165 addEmptyPwsResult(); | 181 addEmptyPwsResult(); |
| 166 UrlInfo urlInfo1 = new UrlInfo(URL1, -1.0, 0); | 182 UrlInfo urlInfo1 = new UrlInfo(URL1, -1.0, 0); |
| 167 UrlInfo urlInfo2 = new UrlInfo(URL2, -1.0, System.currentTimeMillis()); | 183 UrlInfo urlInfo2 = new UrlInfo(URL2, -1.0, System.currentTimeMillis()); |
| 168 mUrlManager.addUrl(urlInfo1); | 184 mUrlManager.addUrl(urlInfo1); |
| 169 mUrlManager.removeUrl(urlInfo1); | 185 mUrlManager.removeUrl(urlInfo1); |
| 170 mUrlManager.addUrl(urlInfo2); | 186 mUrlManager.addUrl(urlInfo2); |
| 171 mUrlManager.removeUrl(urlInfo2); | 187 mUrlManager.removeUrl(urlInfo2); |
| 172 | 188 |
| 173 // Make sure only URL2 is still in the cache. | 189 // Make sure only URL2 is still in the cache. |
| 174 assertFalse(mUrlManager.containsInAnyCache(URL1)); | 190 assertFalse(mUrlManager.containsInAnyCache(URL1)); |
| 175 assertTrue(mUrlManager.containsInAnyCache(URL2)); | 191 assertTrue(mUrlManager.containsInAnyCache(URL2)); |
| 176 } | 192 } |
| 177 | 193 |
| 178 @SmallTest | 194 @Test |
| 179 public void testAddUrlUpdatesCache() throws Exception { | 195 public void testAddUrlUpdatesCache() throws Exception { |
| 180 addEmptyPwsResult(); | 196 addEmptyPwsResult(); |
| 181 addEmptyPwsResult(); | 197 addEmptyPwsResult(); |
| 182 | 198 |
| 183 UrlInfo urlInfo = new UrlInfo(URL1); | 199 UrlInfo urlInfo = new UrlInfo(URL1); |
| 184 mUrlManager.addUrl(urlInfo); | 200 mUrlManager.addUrl(urlInfo); |
| 185 List<UrlInfo> urls = mUrlManager.getUrls(true); | 201 List<UrlInfo> urls = mUrlManager.getUrls(true); |
| 186 assertEquals(1, urls.size()); | 202 assertEquals(1, urls.size()); |
| 187 assertEquals(urlInfo.getDistance(), urls.get(0).getDistance()); | 203 assertEquals(urlInfo.getDistance(), urls.get(0).getDistance(), EPSILON); |
| 188 assertEquals(urlInfo.getDeviceAddress(), urls.get(0).getDeviceAddress())
; | 204 assertEquals(urlInfo.getDeviceAddress(), urls.get(0).getDeviceAddress())
; |
| 189 assertEquals(urlInfo.getFirstSeenTimestamp(), urls.get(0).getFirstSeenTi
mestamp()); | 205 assertEquals(urlInfo.getFirstSeenTimestamp(), urls.get(0).getFirstSeenTi
mestamp()); |
| 190 | 206 |
| 191 urlInfo = new UrlInfo(URL1) | 207 urlInfo = new UrlInfo(URL1).setDistance(100.0).setDeviceAddress("00:11:2
2:33:AA:BB"); |
| 192 .setDistance(100.0) | |
| 193 .setDeviceAddress("00:11:22:33:AA:BB"); | |
| 194 mUrlManager.addUrl(urlInfo); | 208 mUrlManager.addUrl(urlInfo); |
| 195 urls = mUrlManager.getUrls(true); | 209 urls = mUrlManager.getUrls(true); |
| 196 assertEquals(1, urls.size()); | 210 assertEquals(1, urls.size()); |
| 197 assertEquals(urlInfo.getDistance(), urls.get(0).getDistance()); | 211 assertEquals(urlInfo.getDistance(), urls.get(0).getDistance(), EPSILON); |
| 198 assertEquals(urlInfo.getDeviceAddress(), urls.get(0).getDeviceAddress())
; | 212 assertEquals(urlInfo.getDeviceAddress(), urls.get(0).getDeviceAddress())
; |
| 199 } | 213 } |
| 200 | 214 |
| 201 @SmallTest | 215 @Test |
| 202 @RetryOnFailure | |
| 203 public void testAddUrlTwiceWorks() throws Exception { | 216 public void testAddUrlTwiceWorks() throws Exception { |
| 204 // Add and remove an old URL twice and add new URL twice before removing
. | 217 // Add and remove an old URL twice and add new URL twice before removing
. |
| 205 // This should cover several issues involved with updating the cache que
ue. | 218 // This should cover several issues involved with updating the cache que
ue. |
| 206 addEmptyPwsResult(); | 219 addEmptyPwsResult(); |
| 207 addEmptyPwsResult(); | 220 addEmptyPwsResult(); |
| 208 addEmptyPwsResult(); | 221 addEmptyPwsResult(); |
| 209 addEmptyPwsResult(); | 222 addEmptyPwsResult(); |
| 210 UrlInfo urlInfo1 = new UrlInfo(URL1, -1.0, 0); | 223 UrlInfo urlInfo1 = new UrlInfo(URL1, -1.0, 0); |
| 211 UrlInfo urlInfo2 = new UrlInfo(URL2, -1.0, System.currentTimeMillis()); | 224 UrlInfo urlInfo2 = new UrlInfo(URL2, -1.0, System.currentTimeMillis()); |
| 212 mUrlManager.addUrl(urlInfo1); | 225 mUrlManager.addUrl(urlInfo1); |
| 213 mUrlManager.removeUrl(urlInfo1); | 226 mUrlManager.removeUrl(urlInfo1); |
| 214 mUrlManager.addUrl(urlInfo1); | 227 mUrlManager.addUrl(urlInfo1); |
| 215 mUrlManager.removeUrl(urlInfo1); | 228 mUrlManager.removeUrl(urlInfo1); |
| 216 mUrlManager.addUrl(urlInfo2); | 229 mUrlManager.addUrl(urlInfo2); |
| 217 mUrlManager.addUrl(urlInfo2); | 230 mUrlManager.addUrl(urlInfo2); |
| 218 mUrlManager.removeUrl(urlInfo2); | 231 mUrlManager.removeUrl(urlInfo2); |
| 219 | 232 |
| 220 // Make sure only URL2 is still in the cache. | 233 // Make sure only URL2 is still in the cache. |
| 221 assertFalse(mUrlManager.containsInAnyCache(URL1)); | 234 assertFalse(mUrlManager.containsInAnyCache(URL1)); |
| 222 assertTrue(mUrlManager.containsInAnyCache(URL2)); | 235 assertTrue(mUrlManager.containsInAnyCache(URL2)); |
| 223 } | 236 } |
| 224 | 237 |
| 225 @SmallTest | 238 @Test |
| 226 public void testGetUrlsSortsAndDedups() throws Exception { | 239 public void testGetUrlsSortsAndDedups() throws Exception { |
| 227 // Construct results with matching group IDs and check that getUrls retu
rns only the closest | 240 // Construct results with matching group IDs and check that getUrls retu
rns only the closest |
| 228 // URL in each group. The list should be sorted by distance, closest fir
st. | 241 // URL in each group. The list should be sorted by distance, closest fir
st. |
| 229 addPwsResult1(); // GROUP1 | 242 addPwsResult1(); // GROUP1 |
| 230 addPwsResult2(); // GROUP2 | 243 addPwsResult2(); // GROUP2 |
| 231 mMockPwsClient.addPwsResult(new PwsResult(URL3, URL2 + "#a", null, TITLE
2, DESC2, GROUP2)); | 244 mMockPwsClient.addPwsResult(new PwsResult(URL3, URL2 + "#a", null, TITLE
2, DESC2, GROUP2)); |
| 232 mMockPwsClient.addPwsResult(new PwsResult(URL4, URL1, null, TITLE1, DESC
1, GROUP1)); | 245 mMockPwsClient.addPwsResult(new PwsResult(URL4, URL1, null, TITLE1, DESC
1, GROUP1)); |
| 233 mMockPwsClient.addPwsResult(new PwsResult(URL5, URL5, null, TITLE1, DESC
1, GROUP3)); | 246 mMockPwsClient.addPwsResult(new PwsResult(URL5, URL5, null, TITLE1, DESC
1, GROUP3)); |
| 234 mUrlManager.addUrl(new UrlInfo(URL1, 30.0, System.currentTimeMillis())); | 247 mUrlManager.addUrl(new UrlInfo(URL1, 30.0, System.currentTimeMillis())); |
| 235 mUrlManager.addUrl(new UrlInfo(URL2, 20.0, System.currentTimeMillis())); | 248 mUrlManager.addUrl(new UrlInfo(URL2, 20.0, System.currentTimeMillis())); |
| 236 mUrlManager.addUrl(new UrlInfo(URL3, 10.0, System.currentTimeMillis())); | 249 mUrlManager.addUrl(new UrlInfo(URL3, 10.0, System.currentTimeMillis())); |
| 237 mUrlManager.addUrl(new UrlInfo(URL4, 40.0, System.currentTimeMillis())); | 250 mUrlManager.addUrl(new UrlInfo(URL4, 40.0, System.currentTimeMillis())); |
| 238 mUrlManager.addUrl(new UrlInfo(URL5, 50.0, System.currentTimeMillis())); | 251 mUrlManager.addUrl(new UrlInfo(URL5, 50.0, System.currentTimeMillis())); |
| 239 getInstrumentation().waitForIdleSync(); | 252 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 240 | 253 |
| 241 // Make sure URLs are in order and duplicates are omitted. | 254 // Make sure URLs are in order and duplicates are omitted. |
| 242 List<UrlInfo> urlInfos = mUrlManager.getUrls(); | 255 List<UrlInfo> urlInfos = mUrlManager.getUrls(); |
| 243 assertEquals(3, urlInfos.size()); | 256 assertEquals(3, urlInfos.size()); |
| 244 assertEquals(10.0, urlInfos.get(0).getDistance()); | 257 assertEquals(10.0, urlInfos.get(0).getDistance(), EPSILON); |
| 245 assertEquals(URL3, urlInfos.get(0).getUrl()); | 258 assertEquals(URL3, urlInfos.get(0).getUrl()); |
| 246 assertEquals(30.0, urlInfos.get(1).getDistance()); | 259 assertEquals(30.0, urlInfos.get(1).getDistance(), EPSILON); |
| 247 assertEquals(URL1, urlInfos.get(1).getUrl()); | 260 assertEquals(URL1, urlInfos.get(1).getUrl()); |
| 248 assertEquals(50.0, urlInfos.get(2).getDistance()); | 261 assertEquals(50.0, urlInfos.get(2).getDistance(), EPSILON); |
| 249 assertEquals(URL5, urlInfos.get(2).getUrl()); | 262 assertEquals(URL5, urlInfos.get(2).getUrl()); |
| 250 } | 263 } |
| 251 | 264 |
| 252 /* | 265 /* |
| 253 * @SmallTest | |
| 254 * Bug=crbug.com/684148 | 266 * Bug=crbug.com/684148 |
| 255 */ | 267 */ |
| 256 @DisabledTest | 268 @DisabledTest |
| 269 @Test |
| 257 public void testSerializationWorksWithPoorlySerializedResult() throws Except
ion { | 270 public void testSerializationWorksWithPoorlySerializedResult() throws Except
ion { |
| 258 addPwsResult1(); | 271 addPwsResult1(); |
| 259 addPwsResult2(); | 272 addPwsResult2(); |
| 260 long curTime = System.currentTimeMillis(); | 273 long curTime = System.currentTimeMillis(); |
| 261 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, curTime + 42)); | 274 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, curTime + 42)); |
| 262 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, curTime + 43)); | 275 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, curTime + 43)); |
| 263 getInstrumentation().waitForIdleSync(); | 276 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 264 | 277 |
| 265 // Create an invalid serialization. | 278 // Create an invalid serialization. |
| 266 Set<String> serializedUrls = new HashSet<>(); | 279 Set<String> serializedUrls = new HashSet<>(); |
| 267 serializedUrls.add(new UrlInfo(URL1, 99.5, curTime + 42).jsonSerialize()
.toString()); | 280 serializedUrls.add(new UrlInfo(URL1, 99.5, curTime + 42).jsonSerialize()
.toString()); |
| 268 serializedUrls.add("{\"not_a_value\": \"This is totally not a serialized
UrlInfo.\"}"); | 281 serializedUrls.add("{\"not_a_value\": \"This is totally not a serialized
UrlInfo.\"}"); |
| 269 ContextUtils.getAppSharedPreferences().edit() | 282 ContextUtils.getAppSharedPreferences() |
| 283 .edit() |
| 270 .putStringSet("physicalweb_all_urls", serializedUrls) | 284 .putStringSet("physicalweb_all_urls", serializedUrls) |
| 271 .apply(); | 285 .apply(); |
| 272 | 286 |
| 273 // Make sure only the properly serialized URL is restored. | 287 // Make sure only the properly serialized URL is restored. |
| 274 UrlManager urlManager = new UrlManager(); | 288 UrlManager urlManager = new UrlManager(); |
| 275 List<UrlInfo> urlInfos = urlManager.getUrls(); | 289 List<UrlInfo> urlInfos = urlManager.getUrls(); |
| 276 assertEquals(0, urlInfos.size()); | 290 assertEquals(0, urlInfos.size()); |
| 277 assertTrue(urlManager.containsInAnyCache(URL1)); | 291 assertTrue(urlManager.containsInAnyCache(URL1)); |
| 278 assertTrue(urlManager.containsInAnyCache(URL2)); | 292 assertTrue(urlManager.containsInAnyCache(URL2)); |
| 279 } | 293 } |
| 280 | 294 |
| 281 @FlakyTest(message = "https://crbug.com/685471") | 295 @FlakyTest(message = "https://crbug.com/685471") |
| 282 @SmallTest | 296 @Test |
| 283 @RetryOnFailure | |
| 284 public void testSerializationWorksWithoutGarbageCollection() throws Exceptio
n { | 297 public void testSerializationWorksWithoutGarbageCollection() throws Exceptio
n { |
| 285 addPwsResult1(); | 298 addPwsResult1(); |
| 286 addPwsResult2(); | 299 addPwsResult2(); |
| 287 long curTime = System.currentTimeMillis(); | 300 long curTime = System.currentTimeMillis(); |
| 288 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, curTime + 42)); | 301 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, curTime + 42)); |
| 289 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, curTime + 43)); | 302 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, curTime + 43)); |
| 290 getInstrumentation().waitForIdleSync(); | 303 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 291 | 304 |
| 292 // Make sure all URLs are restored. | 305 // Make sure all URLs are restored. |
| 293 UrlManager urlManager = new UrlManager(); | 306 UrlManager urlManager = new UrlManager(); |
| 294 List<UrlInfo> urlInfos = urlManager.getUrls(); | 307 List<UrlInfo> urlInfos = urlManager.getUrls(); |
| 295 assertEquals(0, urlInfos.size()); | 308 assertEquals(0, urlInfos.size()); |
| 296 assertTrue(urlManager.containsInAnyCache(URL1)); | 309 assertTrue(urlManager.containsInAnyCache(URL1)); |
| 297 assertTrue(urlManager.containsInAnyCache(URL2)); | 310 assertTrue(urlManager.containsInAnyCache(URL2)); |
| 298 Set<String> resolvedUrls = urlManager.getResolvedUrls(); | 311 Set<String> resolvedUrls = urlManager.getResolvedUrls(); |
| 299 assertEquals(2, resolvedUrls.size()); | 312 assertEquals(2, resolvedUrls.size()); |
| 300 } | 313 } |
| 301 | 314 |
| 302 @SmallTest | |
| 303 @RetryOnFailure | 315 @RetryOnFailure |
| 316 @Test |
| 304 public void testSerializationWorksWithGarbageCollection() throws Exception { | 317 public void testSerializationWorksWithGarbageCollection() throws Exception { |
| 305 addPwsResult1(); | 318 addPwsResult1(); |
| 306 addPwsResult2(); | 319 addPwsResult2(); |
| 307 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, 42)); | 320 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, 42)); |
| 308 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, 43)); | 321 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, 43)); |
| 309 getInstrumentation().waitForIdleSync(); | 322 ShadowLooper.runUiThreadTasksIncludingDelayedTasks(); |
| 310 | 323 |
| 311 // Make sure all URLs are restored. | 324 // Make sure all URLs are restored. |
| 312 UrlManager urlManager = new UrlManager(); | 325 UrlManager urlManager = new UrlManager(); |
| 313 List<UrlInfo> urlInfos = urlManager.getUrls(); | 326 List<UrlInfo> urlInfos = urlManager.getUrls(); |
| 314 assertEquals(0, urlInfos.size()); | 327 assertEquals(0, urlInfos.size()); |
| 315 Set<String> resolvedUrls = urlManager.getResolvedUrls(); | 328 Set<String> resolvedUrls = urlManager.getResolvedUrls(); |
| 316 assertEquals(0, resolvedUrls.size()); | 329 assertEquals(0, resolvedUrls.size()); |
| 317 } | 330 } |
| 318 | 331 |
| 319 @SmallTest | 332 @Test |
| 320 public void testUpgradeFromNone() throws Exception { | 333 public void testUpgradeFromNone() throws Exception { |
| 321 Set<String> oldResolvedUrls = new HashSet<String>(); | 334 Set<String> oldResolvedUrls = new HashSet<String>(); |
| 322 oldResolvedUrls.add("old"); | 335 oldResolvedUrls.add("old"); |
| 323 ContextUtils.getAppSharedPreferences().edit() | 336 SharedPreferences prefs = ContextUtils.getAppSharedPreferences(); |
| 337 prefs.edit() |
| 324 .remove(UrlManager.getVersionKey()) | 338 .remove(UrlManager.getVersionKey()) |
| 325 .putStringSet("physicalweb_nearby_urls", oldResolvedUrls) | 339 .putStringSet("physicalweb_nearby_urls", oldResolvedUrls) |
| 326 .putInt("org.chromium.chrome.browser.physicalweb.VERSION", 1) | 340 .putInt("org.chromium.chrome.browser.physicalweb.VERSION", 1) |
| 327 .putInt("org.chromium.chrome.browser.physicalweb.BOTTOM_BAR_DISP
LAY_COUNT", 1) | 341 .putInt("org.chromium.chrome.browser.physicalweb.BOTTOM_BAR_DISP
LAY_COUNT", 1) |
| 342 .putBoolean("physical_web_ignore_other_clients", true) |
| 328 .apply(); | 343 .apply(); |
| 344 final Lock lock = new ReentrantLock(); |
| 345 final Condition condition = lock.newCondition(); |
| 346 prefs.registerOnSharedPreferenceChangeListener( |
| 347 new SharedPreferences.OnSharedPreferenceChangeListener() { |
| 348 public void onSharedPreferenceChanged( |
| 349 SharedPreferences sharedPreferences, String key) { |
| 350 lock.lock(); |
| 351 try { |
| 352 condition.signal(); |
| 353 } finally { |
| 354 lock.unlock(); |
| 355 } |
| 356 } |
| 357 }); |
| 329 new UrlManager(); | 358 new UrlManager(); |
| 359 lock.lock(); |
| 360 try { |
| 361 assertTrue(condition.await(2, TimeUnit.SECONDS)); |
| 362 } finally { |
| 363 lock.unlock(); |
| 364 } |
| 330 | 365 |
| 331 // Make sure the new prefs are populated and old prefs are gone. | 366 // Make sure the new prefs are populated and old prefs are gone. |
| 332 final SharedPreferences sharedPreferences = ContextUtils.getAppSharedPre
ferences(); | 367 SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferenc
es(); |
| 333 CriteriaHelper.pollInstrumentationThread(new Criteria() { | 368 assertTrue(sharedPreferences.contains(UrlManager.getVersionKey())); |
| 334 @Override | 369 assertEquals( |
| 335 public boolean isSatisfied() { | 370 UrlManager.getVersion(), sharedPreferences.getInt(UrlManager.get
VersionKey(), 0)); |
| 336 SharedPreferences sharedPreferences = ContextUtils.getAppSharedP
references(); | 371 assertFalse(sharedPreferences.contains("physicalweb_nearby_urls")); |
| 337 return sharedPreferences.contains(UrlManager.getVersionKey()) | 372 assertFalse(sharedPreferences.contains("org.chromium.chrome.browser.phys
icalweb.VERSION")); |
| 338 && !sharedPreferences.contains("physicalweb_nearby_urls"
) | 373 assertFalse(sharedPreferences.contains( |
| 339 && !sharedPreferences.contains( | 374 "org.chromium.chrome.browser.physicalweb.BOTTOM_BAR_DISPLAY_COUN
T")); |
| 340 "org.chromium.chrome.browser.physicalweb.VERSION
") | 375 assertFalse(sharedPreferences.contains("physical_web_ignore_other_client
s")); |
| 341 && !sharedPreferences.contains("org.chromium.chrome.brow
ser.physicalweb" | |
| 342 + ".BOTTOM_BAR_DISPLAY_COUNT"); | |
| 343 } | |
| 344 }, 5000, CriteriaHelper.DEFAULT_POLLING_INTERVAL); | |
| 345 | |
| 346 assertEquals(UrlManager.getVersion(), | |
| 347 sharedPreferences.getInt(UrlManager.getVersionKey(), 0)); | |
| 348 } | 376 } |
| 349 } | 377 } |
| OLD | NEW |