Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(160)

Side by Side Diff: chrome/android/javatests/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java

Issue 2766373004: Convert the rest of chrome_public_test_apk InstrumentationTestCases to JUnit4 (Closed)
Patch Set: nits and rebase Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 android.content.SharedPreferences; 7 import android.content.SharedPreferences;
8 import android.support.test.InstrumentationRegistry;
8 import android.support.test.filters.SmallTest; 9 import android.support.test.filters.SmallTest;
9 import android.test.InstrumentationTestCase; 10
11 import org.junit.Assert;
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.junit.runner.RunWith;
10 15
11 import org.chromium.base.ContextUtils; 16 import org.chromium.base.ContextUtils;
12 import org.chromium.base.test.util.DisabledTest; 17 import org.chromium.base.test.util.DisabledTest;
13 import org.chromium.base.test.util.FlakyTest; 18 import org.chromium.base.test.util.FlakyTest;
14 import org.chromium.base.test.util.RetryOnFailure; 19 import org.chromium.base.test.util.RetryOnFailure;
20 import org.chromium.chrome.test.ChromeJUnit4ClassRunner;
15 import org.chromium.content.browser.test.util.Criteria; 21 import org.chromium.content.browser.test.util.Criteria;
16 import org.chromium.content.browser.test.util.CriteriaHelper; 22 import org.chromium.content.browser.test.util.CriteriaHelper;
17 23
18 import java.util.ArrayList; 24 import java.util.ArrayList;
19 import java.util.HashSet; 25 import java.util.HashSet;
20 import java.util.List; 26 import java.util.List;
21 import java.util.Set; 27 import java.util.Set;
22 28
23 /** 29 /**
24 * Tests for {@link UrlManager}. 30 * Tests for {@link UrlManager}.
25 */ 31 */
26 public class UrlManagerTest extends InstrumentationTestCase { 32 @RunWith(ChromeJUnit4ClassRunner.class)
33 public class UrlManagerTest {
27 private static final String URL1 = "https://example.com/"; 34 private static final String URL1 = "https://example.com/";
28 private static final String TITLE1 = "Example"; 35 private static final String TITLE1 = "Example";
29 private static final String DESC1 = "Example Website"; 36 private static final String DESC1 = "Example Website";
30 private static final String URL2 = "https://google.com/"; 37 private static final String URL2 = "https://google.com/";
31 private static final String TITLE2 = "Google"; 38 private static final String TITLE2 = "Google";
32 private static final String DESC2 = "Search the Web"; 39 private static final String DESC2 = "Search the Web";
33 private static final String URL3 = "https://html5zombo.com/"; 40 private static final String URL3 = "https://html5zombo.com/";
34 private static final String URL4 = "https://hooli.xyz/"; 41 private static final String URL4 = "https://hooli.xyz/";
35 private static final String URL5 = "https://www.gmail.com/mail/help/paper/"; 42 private static final String URL5 = "https://www.gmail.com/mail/help/paper/";
36 private static final String GROUP1 = "group1"; 43 private static final String GROUP1 = "group1";
37 private static final String GROUP2 = "group2"; 44 private static final String GROUP2 = "group2";
38 private static final String GROUP3 = "group3"; 45 private static final String GROUP3 = "group3";
39 private static final String PREF_PHYSICAL_WEB = "physical_web"; 46 private static final String PREF_PHYSICAL_WEB = "physical_web";
40 private static final int PHYSICAL_WEB_OFF = 0; 47 private static final int PHYSICAL_WEB_OFF = 0;
41 private static final int PHYSICAL_WEB_ON = 1; 48 private static final int PHYSICAL_WEB_ON = 1;
42 private static final int PHYSICAL_WEB_ONBOARDING = 2; 49 private static final int PHYSICAL_WEB_ONBOARDING = 2;
43 private UrlManager mUrlManager = null; 50 private UrlManager mUrlManager = null;
44 private MockPwsClient mMockPwsClient = null; 51 private MockPwsClient mMockPwsClient = null;
45 52
46 @Override 53 @Before
47 protected void setUp() throws Exception { 54 public void setUp() throws Exception {
48 super.setUp();
49 ContextUtils.getAppSharedPreferences() 55 ContextUtils.getAppSharedPreferences()
50 .edit() 56 .edit()
51 .putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ON) 57 .putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ON)
52 .apply(); 58 .apply();
53 UrlManager.clearPrefsForTesting(); 59 UrlManager.clearPrefsForTesting();
54 mUrlManager = new UrlManager(); 60 mUrlManager = new UrlManager();
55 mMockPwsClient = new MockPwsClient(); 61 mMockPwsClient = new MockPwsClient();
56 mUrlManager.overridePwsClientForTesting(mMockPwsClient); 62 mUrlManager.overridePwsClientForTesting(mMockPwsClient);
57 } 63 }
58 64
(...skipping 29 matching lines...) Expand all
88 mMockPwsClient.addPwsResults(new ArrayList<PwsResult>()); 94 mMockPwsClient.addPwsResults(new ArrayList<PwsResult>());
89 } 95 }
90 96
91 private void setOnboarding() { 97 private void setOnboarding() {
92 ContextUtils.getAppSharedPreferences() 98 ContextUtils.getAppSharedPreferences()
93 .edit() 99 .edit()
94 .putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ONBOARDING) 100 .putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ONBOARDING)
95 .apply(); 101 .apply();
96 } 102 }
97 103
104 @Test
98 @SmallTest 105 @SmallTest
99 @RetryOnFailure 106 @RetryOnFailure
100 public void testAddUrlAfterClearAllUrlsWorks() { 107 public void testAddUrlAfterClearAllUrlsWorks() {
101 addPwsResult1(); 108 addPwsResult1();
102 addPwsResult2(); 109 addPwsResult2();
103 addPwsResult1(); 110 addPwsResult1();
104 addPwsResult2(); 111 addPwsResult2();
105 addUrlInfo1(); 112 addUrlInfo1();
106 addUrlInfo2(); 113 addUrlInfo2();
107 getInstrumentation().waitForIdleSync(); 114 InstrumentationRegistry.getInstrumentation().waitForIdleSync();
108 mUrlManager.clearAllUrls(); 115 mUrlManager.clearAllUrls();
109 116
110 // Add some more URLs...this should not crash if we cleared correctly. 117 // Add some more URLs...this should not crash if we cleared correctly.
111 addUrlInfo1(); 118 addUrlInfo1();
112 addUrlInfo2(); 119 addUrlInfo2();
113 getInstrumentation().waitForIdleSync(); 120 InstrumentationRegistry.getInstrumentation().waitForIdleSync();
114 List<UrlInfo> urlInfos = mUrlManager.getUrls(); 121 List<UrlInfo> urlInfos = mUrlManager.getUrls();
115 assertEquals(2, urlInfos.size()); 122 Assert.assertEquals(2, urlInfos.size());
116 } 123 }
117 124
125 @Test
118 @SmallTest 126 @SmallTest
119 @RetryOnFailure 127 @RetryOnFailure
120 public void testClearNearbyUrlsWorks() { 128 public void testClearNearbyUrlsWorks() {
121 addPwsResult1(); 129 addPwsResult1();
122 addPwsResult2(); 130 addPwsResult2();
123 addUrlInfo1(); 131 addUrlInfo1();
124 addUrlInfo2(); 132 addUrlInfo2();
125 getInstrumentation().waitForIdleSync(); 133 InstrumentationRegistry.getInstrumentation().waitForIdleSync();
126 134
127 mUrlManager.clearNearbyUrls(); 135 mUrlManager.clearNearbyUrls();
128 136
129 // Test that the URLs are not nearby, but do exist in the cache. 137 // Test that the URLs are not nearby, but do exist in the cache.
130 List<UrlInfo> urlInfos = mUrlManager.getUrls(true); 138 List<UrlInfo> urlInfos = mUrlManager.getUrls(true);
131 assertEquals(0, urlInfos.size()); 139 Assert.assertEquals(0, urlInfos.size());
132 assertTrue(mUrlManager.containsInAnyCache(URL1)); 140 Assert.assertTrue(mUrlManager.containsInAnyCache(URL1));
133 assertTrue(mUrlManager.containsInAnyCache(URL2)); 141 Assert.assertTrue(mUrlManager.containsInAnyCache(URL2));
134 142
135 mUrlManager.clearAllUrls(); 143 mUrlManager.clearAllUrls();
136 144
137 // Test that cache is empty. 145 // Test that cache is empty.
138 assertFalse(mUrlManager.containsInAnyCache(URL1)); 146 Assert.assertFalse(mUrlManager.containsInAnyCache(URL1));
139 assertFalse(mUrlManager.containsInAnyCache(URL2)); 147 Assert.assertFalse(mUrlManager.containsInAnyCache(URL2));
140 } 148 }
141 149
150 @Test
142 @SmallTest 151 @SmallTest
143 @RetryOnFailure 152 @RetryOnFailure
144 public void testAddUrlGarbageCollectsForSize() throws Exception { 153 public void testAddUrlGarbageCollectsForSize() throws Exception {
145 // Add and remove 101 URLs, making sure one is clearly slightly older th an the others. 154 // Add and remove 101 URLs, making sure one is clearly slightly older th an the others.
146 addEmptyPwsResult(); 155 addEmptyPwsResult();
147 UrlInfo urlInfo = new UrlInfo(URL1, -1.0, System.currentTimeMillis() - 1 ); 156 UrlInfo urlInfo = new UrlInfo(URL1, -1.0, System.currentTimeMillis() - 1 );
148 mUrlManager.addUrl(urlInfo); 157 mUrlManager.addUrl(urlInfo);
149 mUrlManager.removeUrl(urlInfo); 158 mUrlManager.removeUrl(urlInfo);
150 for (int i = 1; i <= mUrlManager.getMaxCacheSize(); i++) { 159 for (int i = 1; i <= mUrlManager.getMaxCacheSize(); i++) {
151 mMockPwsClient.addPwsResults(new ArrayList<PwsResult>()); 160 mMockPwsClient.addPwsResults(new ArrayList<PwsResult>());
152 urlInfo = new UrlInfo(URL1 + i, -1.0, System.currentTimeMillis()); 161 urlInfo = new UrlInfo(URL1 + i, -1.0, System.currentTimeMillis());
153 mUrlManager.addUrl(urlInfo); 162 mUrlManager.addUrl(urlInfo);
154 mUrlManager.removeUrl(urlInfo); 163 mUrlManager.removeUrl(urlInfo);
155 } 164 }
156 165
157 // Make our cache is missing the first URL and contains the others. 166 // Make our cache is missing the first URL and contains the others.
158 assertFalse(mUrlManager.containsInAnyCache(URL1)); 167 Assert.assertFalse(mUrlManager.containsInAnyCache(URL1));
159 assertTrue(mUrlManager.containsInAnyCache(URL1 + 1)); 168 Assert.assertTrue(mUrlManager.containsInAnyCache(URL1 + 1));
160 assertTrue(mUrlManager.containsInAnyCache(URL1 + mUrlManager.getMaxCache Size())); 169 Assert.assertTrue(mUrlManager.containsInAnyCache(URL1 + mUrlManager.getM axCacheSize()));
161 } 170 }
162 171
172 @Test
163 @SmallTest 173 @SmallTest
164 public void testAddUrlGarbageCollectsForAge() throws Exception { 174 public void testAddUrlGarbageCollectsForAge() throws Exception {
165 // Add a URL with a phony timestamp. 175 // Add a URL with a phony timestamp.
166 addEmptyPwsResult(); 176 addEmptyPwsResult();
167 addEmptyPwsResult(); 177 addEmptyPwsResult();
168 UrlInfo urlInfo1 = new UrlInfo(URL1, -1.0, 0); 178 UrlInfo urlInfo1 = new UrlInfo(URL1, -1.0, 0);
169 UrlInfo urlInfo2 = new UrlInfo(URL2, -1.0, System.currentTimeMillis()); 179 UrlInfo urlInfo2 = new UrlInfo(URL2, -1.0, System.currentTimeMillis());
170 mUrlManager.addUrl(urlInfo1); 180 mUrlManager.addUrl(urlInfo1);
171 mUrlManager.removeUrl(urlInfo1); 181 mUrlManager.removeUrl(urlInfo1);
172 mUrlManager.addUrl(urlInfo2); 182 mUrlManager.addUrl(urlInfo2);
173 mUrlManager.removeUrl(urlInfo2); 183 mUrlManager.removeUrl(urlInfo2);
174 184
175 // Make sure only URL2 is still in the cache. 185 // Make sure only URL2 is still in the cache.
176 assertFalse(mUrlManager.containsInAnyCache(URL1)); 186 Assert.assertFalse(mUrlManager.containsInAnyCache(URL1));
177 assertTrue(mUrlManager.containsInAnyCache(URL2)); 187 Assert.assertTrue(mUrlManager.containsInAnyCache(URL2));
178 } 188 }
179 189
190 @Test
180 @SmallTest 191 @SmallTest
181 public void testAddUrlUpdatesCache() throws Exception { 192 public void testAddUrlUpdatesCache() throws Exception {
182 addEmptyPwsResult(); 193 addEmptyPwsResult();
183 addEmptyPwsResult(); 194 addEmptyPwsResult();
184 195
185 UrlInfo urlInfo = new UrlInfo(URL1); 196 UrlInfo urlInfo = new UrlInfo(URL1);
186 mUrlManager.addUrl(urlInfo); 197 mUrlManager.addUrl(urlInfo);
187 List<UrlInfo> urls = mUrlManager.getUrls(true); 198 List<UrlInfo> urls = mUrlManager.getUrls(true);
188 assertEquals(1, urls.size()); 199 Assert.assertEquals(1, urls.size());
189 assertEquals(urlInfo.getDistance(), urls.get(0).getDistance()); 200 Assert.assertEquals(urlInfo.getDistance(), urls.get(0).getDistance(), 0) ;
190 assertEquals(urlInfo.getDeviceAddress(), urls.get(0).getDeviceAddress()) ; 201 Assert.assertEquals(urlInfo.getDeviceAddress(), urls.get(0).getDeviceAdd ress());
191 assertEquals(urlInfo.getFirstSeenTimestamp(), urls.get(0).getFirstSeenTi mestamp()); 202 Assert.assertEquals(urlInfo.getFirstSeenTimestamp(), urls.get(0).getFirs tSeenTimestamp());
192 203
193 urlInfo = new UrlInfo(URL1).setDistance(100.0).setDeviceAddress("00:11:2 2:33:AA:BB"); 204 urlInfo = new UrlInfo(URL1).setDistance(100.0).setDeviceAddress("00:11:2 2:33:AA:BB");
194 mUrlManager.addUrl(urlInfo); 205 mUrlManager.addUrl(urlInfo);
195 urls = mUrlManager.getUrls(true); 206 urls = mUrlManager.getUrls(true);
196 assertEquals(1, urls.size()); 207 Assert.assertEquals(1, urls.size());
197 assertEquals(urlInfo.getDistance(), urls.get(0).getDistance()); 208 Assert.assertEquals(urlInfo.getDistance(), urls.get(0).getDistance(), 0) ;
198 assertEquals(urlInfo.getDeviceAddress(), urls.get(0).getDeviceAddress()) ; 209 Assert.assertEquals(urlInfo.getDeviceAddress(), urls.get(0).getDeviceAdd ress());
199 } 210 }
200 211
212 @Test
201 @SmallTest 213 @SmallTest
202 @RetryOnFailure 214 @RetryOnFailure
203 public void testAddUrlTwiceWorks() throws Exception { 215 public void testAddUrlTwiceWorks() throws Exception {
204 // Add and remove an old URL twice and add new URL twice before removing . 216 // 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. 217 // This should cover several issues involved with updating the cache que ue.
206 addEmptyPwsResult(); 218 addEmptyPwsResult();
207 addEmptyPwsResult(); 219 addEmptyPwsResult();
208 addEmptyPwsResult(); 220 addEmptyPwsResult();
209 addEmptyPwsResult(); 221 addEmptyPwsResult();
210 UrlInfo urlInfo1 = new UrlInfo(URL1, -1.0, 0); 222 UrlInfo urlInfo1 = new UrlInfo(URL1, -1.0, 0);
211 UrlInfo urlInfo2 = new UrlInfo(URL2, -1.0, System.currentTimeMillis()); 223 UrlInfo urlInfo2 = new UrlInfo(URL2, -1.0, System.currentTimeMillis());
212 mUrlManager.addUrl(urlInfo1); 224 mUrlManager.addUrl(urlInfo1);
213 mUrlManager.removeUrl(urlInfo1); 225 mUrlManager.removeUrl(urlInfo1);
214 mUrlManager.addUrl(urlInfo1); 226 mUrlManager.addUrl(urlInfo1);
215 mUrlManager.removeUrl(urlInfo1); 227 mUrlManager.removeUrl(urlInfo1);
216 mUrlManager.addUrl(urlInfo2); 228 mUrlManager.addUrl(urlInfo2);
217 mUrlManager.addUrl(urlInfo2); 229 mUrlManager.addUrl(urlInfo2);
218 mUrlManager.removeUrl(urlInfo2); 230 mUrlManager.removeUrl(urlInfo2);
219 231
220 // Make sure only URL2 is still in the cache. 232 // Make sure only URL2 is still in the cache.
221 assertFalse(mUrlManager.containsInAnyCache(URL1)); 233 Assert.assertFalse(mUrlManager.containsInAnyCache(URL1));
222 assertTrue(mUrlManager.containsInAnyCache(URL2)); 234 Assert.assertTrue(mUrlManager.containsInAnyCache(URL2));
223 } 235 }
224 236
237 @Test
225 @SmallTest 238 @SmallTest
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 InstrumentationRegistry.getInstrumentation().waitForIdleSync();
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 Assert.assertEquals(3, urlInfos.size());
244 assertEquals(10.0, urlInfos.get(0).getDistance()); 257 Assert.assertEquals(10.0, urlInfos.get(0).getDistance(), 0);
245 assertEquals(URL3, urlInfos.get(0).getUrl()); 258 Assert.assertEquals(URL3, urlInfos.get(0).getUrl());
246 assertEquals(30.0, urlInfos.get(1).getDistance()); 259 Assert.assertEquals(30.0, urlInfos.get(1).getDistance(), 0);
247 assertEquals(URL1, urlInfos.get(1).getUrl()); 260 Assert.assertEquals(URL1, urlInfos.get(1).getUrl());
248 assertEquals(50.0, urlInfos.get(2).getDistance()); 261 Assert.assertEquals(50.0, urlInfos.get(2).getDistance(), 0);
249 assertEquals(URL5, urlInfos.get(2).getUrl()); 262 Assert.assertEquals(URL5, urlInfos.get(2).getUrl());
250 } 263 }
251 264
252 /* 265 /*
253 * @SmallTest 266 * @SmallTest
254 * Bug=crbug.com/684148 267 * Bug=crbug.com/684148
255 */ 268 */
269 @Test
256 @DisabledTest 270 @DisabledTest
257 public void testSerializationWorksWithPoorlySerializedResult() throws Except ion { 271 public void testSerializationWorksWithPoorlySerializedResult() throws Except ion {
258 addPwsResult1(); 272 addPwsResult1();
259 addPwsResult2(); 273 addPwsResult2();
260 long curTime = System.currentTimeMillis(); 274 long curTime = System.currentTimeMillis();
261 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, curTime + 42)); 275 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, curTime + 42));
262 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, curTime + 43)); 276 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, curTime + 43));
263 getInstrumentation().waitForIdleSync(); 277 InstrumentationRegistry.getInstrumentation().waitForIdleSync();
264 278
265 // Create an invalid serialization. 279 // Create an invalid serialization.
266 Set<String> serializedUrls = new HashSet<>(); 280 Set<String> serializedUrls = new HashSet<>();
267 serializedUrls.add(new UrlInfo(URL1, 99.5, curTime + 42).jsonSerialize() .toString()); 281 serializedUrls.add(new UrlInfo(URL1, 99.5, curTime + 42).jsonSerialize() .toString());
268 serializedUrls.add("{\"not_a_value\": \"This is totally not a serialized UrlInfo.\"}"); 282 serializedUrls.add("{\"not_a_value\": \"This is totally not a serialized UrlInfo.\"}");
269 ContextUtils.getAppSharedPreferences() 283 ContextUtils.getAppSharedPreferences()
270 .edit() 284 .edit()
271 .putStringSet("physicalweb_all_urls", serializedUrls) 285 .putStringSet("physicalweb_all_urls", serializedUrls)
272 .apply(); 286 .apply();
273 287
274 // Make sure only the properly serialized URL is restored. 288 // Make sure only the properly serialized URL is restored.
275 UrlManager urlManager = new UrlManager(); 289 UrlManager urlManager = new UrlManager();
276 List<UrlInfo> urlInfos = urlManager.getUrls(); 290 List<UrlInfo> urlInfos = urlManager.getUrls();
277 assertEquals(0, urlInfos.size()); 291 Assert.assertEquals(0, urlInfos.size());
278 assertTrue(urlManager.containsInAnyCache(URL1)); 292 Assert.assertTrue(urlManager.containsInAnyCache(URL1));
279 assertTrue(urlManager.containsInAnyCache(URL2)); 293 Assert.assertTrue(urlManager.containsInAnyCache(URL2));
280 } 294 }
281 295
296 @Test
282 @FlakyTest(message = "https://crbug.com/685471") 297 @FlakyTest(message = "https://crbug.com/685471")
283 @SmallTest 298 @SmallTest
284 @RetryOnFailure 299 @RetryOnFailure
285 public void testSerializationWorksWithoutGarbageCollection() throws Exceptio n { 300 public void testSerializationWorksWithoutGarbageCollection() throws Exceptio n {
286 addPwsResult1(); 301 addPwsResult1();
287 addPwsResult2(); 302 addPwsResult2();
288 long curTime = System.currentTimeMillis(); 303 long curTime = System.currentTimeMillis();
289 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, curTime + 42)); 304 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, curTime + 42));
290 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, curTime + 43)); 305 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, curTime + 43));
291 getInstrumentation().waitForIdleSync(); 306 InstrumentationRegistry.getInstrumentation().waitForIdleSync();
292 307
293 // Make sure all URLs are restored. 308 // Make sure all URLs are restored.
294 UrlManager urlManager = new UrlManager(); 309 UrlManager urlManager = new UrlManager();
295 List<UrlInfo> urlInfos = urlManager.getUrls(); 310 List<UrlInfo> urlInfos = urlManager.getUrls();
296 assertEquals(0, urlInfos.size()); 311 Assert.assertEquals(0, urlInfos.size());
297 assertTrue(urlManager.containsInAnyCache(URL1)); 312 Assert.assertTrue(urlManager.containsInAnyCache(URL1));
298 assertTrue(urlManager.containsInAnyCache(URL2)); 313 Assert.assertTrue(urlManager.containsInAnyCache(URL2));
299 Set<String> resolvedUrls = urlManager.getResolvedUrls(); 314 Set<String> resolvedUrls = urlManager.getResolvedUrls();
300 assertEquals(2, resolvedUrls.size()); 315 Assert.assertEquals(2, resolvedUrls.size());
301 } 316 }
302 317
318 @Test
303 @SmallTest 319 @SmallTest
304 @RetryOnFailure 320 @RetryOnFailure
305 public void testSerializationWorksWithGarbageCollection() throws Exception { 321 public void testSerializationWorksWithGarbageCollection() throws Exception {
306 addPwsResult1(); 322 addPwsResult1();
307 addPwsResult2(); 323 addPwsResult2();
308 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, 42)); 324 mUrlManager.addUrl(new UrlInfo(URL1, 99.5, 42));
309 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, 43)); 325 mUrlManager.addUrl(new UrlInfo(URL2, 100.5, 43));
310 getInstrumentation().waitForIdleSync(); 326 InstrumentationRegistry.getInstrumentation().waitForIdleSync();
311 327
312 // Make sure all URLs are restored. 328 // Make sure all URLs are restored.
313 UrlManager urlManager = new UrlManager(); 329 UrlManager urlManager = new UrlManager();
314 List<UrlInfo> urlInfos = urlManager.getUrls(); 330 List<UrlInfo> urlInfos = urlManager.getUrls();
315 assertEquals(0, urlInfos.size()); 331 Assert.assertEquals(0, urlInfos.size());
316 Set<String> resolvedUrls = urlManager.getResolvedUrls(); 332 Set<String> resolvedUrls = urlManager.getResolvedUrls();
317 assertEquals(0, resolvedUrls.size()); 333 Assert.assertEquals(0, resolvedUrls.size());
318 } 334 }
319 335
336 @Test
320 @SmallTest 337 @SmallTest
321 public void testUpgradeFromNone() throws Exception { 338 public void testUpgradeFromNone() throws Exception {
322 Set<String> oldResolvedUrls = new HashSet<String>(); 339 Set<String> oldResolvedUrls = new HashSet<String>();
323 oldResolvedUrls.add("old"); 340 oldResolvedUrls.add("old");
324 ContextUtils.getAppSharedPreferences() 341 ContextUtils.getAppSharedPreferences()
325 .edit() 342 .edit()
326 .remove(UrlManager.getVersionKey()) 343 .remove(UrlManager.getVersionKey())
327 .putStringSet("physicalweb_nearby_urls", oldResolvedUrls) 344 .putStringSet("physicalweb_nearby_urls", oldResolvedUrls)
328 .putInt("org.chromium.chrome.browser.physicalweb.VERSION", 1) 345 .putInt("org.chromium.chrome.browser.physicalweb.VERSION", 1)
329 .putInt("org.chromium.chrome.browser.physicalweb.BOTTOM_BAR_DISP LAY_COUNT", 1) 346 .putInt("org.chromium.chrome.browser.physicalweb.BOTTOM_BAR_DISP LAY_COUNT", 1)
330 .apply(); 347 .apply();
331 new UrlManager(); 348 new UrlManager();
332 349
333 // Make sure the new prefs are populated and old prefs are gone. 350 // Make sure the new prefs are populated and old prefs are gone.
334 final SharedPreferences sharedPreferences = ContextUtils.getAppSharedPre ferences(); 351 final SharedPreferences sharedPreferences = ContextUtils.getAppSharedPre ferences();
335 CriteriaHelper.pollInstrumentationThread(new Criteria() { 352 CriteriaHelper.pollInstrumentationThread(new Criteria() {
336 @Override 353 @Override
337 public boolean isSatisfied() { 354 public boolean isSatisfied() {
338 SharedPreferences sharedPreferences = ContextUtils.getAppSharedP references(); 355 SharedPreferences sharedPreferences = ContextUtils.getAppSharedP references();
339 return sharedPreferences.contains(UrlManager.getVersionKey()) 356 return sharedPreferences.contains(UrlManager.getVersionKey())
340 && !sharedPreferences.contains("physicalweb_nearby_urls" ) 357 && !sharedPreferences.contains("physicalweb_nearby_urls" )
341 && !sharedPreferences.contains( 358 && !sharedPreferences.contains(
342 "org.chromium.chrome.browser.physicalweb.VERS ION") 359 "org.chromium.chrome.browser.physicalweb.VERS ION")
343 && !sharedPreferences.contains("org.chromium.chrome.brow ser.physicalweb" 360 && !sharedPreferences.contains("org.chromium.chrome.brow ser.physicalweb"
344 + ".BOTTOM_BAR_DISPLAY_COUNT"); 361 + ".BOTTOM_BAR_DISPLAY_COUNT");
345 } 362 }
346 }, 5000, CriteriaHelper.DEFAULT_POLLING_INTERVAL); 363 }, 5000, CriteriaHelper.DEFAULT_POLLING_INTERVAL);
347 364
348 assertEquals( 365 Assert.assertEquals(
349 UrlManager.getVersion(), sharedPreferences.getInt(UrlManager.get VersionKey(), 0)); 366 UrlManager.getVersion(), sharedPreferences.getInt(UrlManager.get VersionKey(), 0));
350 } 367 }
351 } 368 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698