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

Unified Diff: chrome/android/junit/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java

Issue 2723783003: Move UrlManagerTest to junit (Closed)
Patch Set: Add @Test to disabled test Created 3 years, 9 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « chrome/android/junit/src/org/chromium/chrome/browser/physicalweb/MockPwsClient.java ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/android/junit/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java b/chrome/android/junit/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java
similarity index 76%
rename from chrome/android/javatests/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java
rename to chrome/android/junit/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java
index 8762260bd2aeb9aacc894a1219396709d2d7e6e4..d6668b280ca5afd4a4f675b5fc10f77dec85d4b6 100644
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java
+++ b/chrome/android/junit/src/org/chromium/chrome/browser/physicalweb/UrlManagerTest.java
@@ -4,26 +4,41 @@
package org.chromium.chrome.browser.physicalweb;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
import android.content.SharedPreferences;
-import android.support.test.filters.SmallTest;
-import android.test.InstrumentationTestCase;
+
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import org.robolectric.RuntimeEnvironment;
+import org.robolectric.annotation.Config;
+import org.robolectric.shadows.ShadowLooper;
import org.chromium.base.ContextUtils;
import org.chromium.base.test.util.DisabledTest;
import org.chromium.base.test.util.FlakyTest;
import org.chromium.base.test.util.RetryOnFailure;
-import org.chromium.content.browser.test.util.Criteria;
-import org.chromium.content.browser.test.util.CriteriaHelper;
+import org.chromium.testing.local.LocalRobolectricTestRunner;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
/**
* Tests for {@link UrlManager}.
*/
-public class UrlManagerTest extends InstrumentationTestCase {
+@RunWith(LocalRobolectricTestRunner.class)
+@Config(manifest = Config.NONE)
+public class UrlManagerTest {
private static final String URL1 = "https://example.com/";
private static final String TITLE1 = "Example";
private static final String DESC1 = "Example Website";
@@ -40,13 +55,15 @@ public class UrlManagerTest extends InstrumentationTestCase {
private static final int PHYSICAL_WEB_OFF = 0;
private static final int PHYSICAL_WEB_ON = 1;
private static final int PHYSICAL_WEB_ONBOARDING = 2;
+ private static final double EPSILON = .001;
private UrlManager mUrlManager = null;
private MockPwsClient mMockPwsClient = null;
- @Override
- protected void setUp() throws Exception {
- super.setUp();
- ContextUtils.getAppSharedPreferences().edit()
+ @Before
+ public void setUp() {
+ ContextUtils.initApplicationContextForTests(RuntimeEnvironment.application);
+ ContextUtils.getAppSharedPreferences()
+ .edit()
.putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ON)
.apply();
UrlManager.clearPrefsForTesting();
@@ -88,13 +105,13 @@ public class UrlManagerTest extends InstrumentationTestCase {
}
private void setOnboarding() {
- ContextUtils.getAppSharedPreferences().edit()
+ ContextUtils.getAppSharedPreferences()
+ .edit()
.putInt(PREF_PHYSICAL_WEB, PHYSICAL_WEB_ONBOARDING)
.apply();
}
- @SmallTest
- @RetryOnFailure
+ @Test
public void testAddUrlAfterClearAllUrlsWorks() {
addPwsResult1();
addPwsResult2();
@@ -102,25 +119,24 @@ public class UrlManagerTest extends InstrumentationTestCase {
addPwsResult2();
addUrlInfo1();
addUrlInfo2();
- getInstrumentation().waitForIdleSync();
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
mUrlManager.clearAllUrls();
// Add some more URLs...this should not crash if we cleared correctly.
addUrlInfo1();
addUrlInfo2();
- getInstrumentation().waitForIdleSync();
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
List<UrlInfo> urlInfos = mUrlManager.getUrls();
assertEquals(2, urlInfos.size());
}
- @SmallTest
- @RetryOnFailure
+ @Test
public void testClearNearbyUrlsWorks() {
addPwsResult1();
addPwsResult2();
addUrlInfo1();
addUrlInfo2();
- getInstrumentation().waitForIdleSync();
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
mUrlManager.clearNearbyUrls();
@@ -137,7 +153,7 @@ public class UrlManagerTest extends InstrumentationTestCase {
assertFalse(mUrlManager.containsInAnyCache(URL2));
}
- @SmallTest
+ @Test
@RetryOnFailure
public void testAddUrlGarbageCollectsForSize() throws Exception {
// Add and remove 101 URLs, making sure one is clearly slightly older than the others.
@@ -158,7 +174,7 @@ public class UrlManagerTest extends InstrumentationTestCase {
assertTrue(mUrlManager.containsInAnyCache(URL1 + mUrlManager.getMaxCacheSize()));
}
- @SmallTest
+ @Test
public void testAddUrlGarbageCollectsForAge() throws Exception {
// Add a URL with a phony timestamp.
addEmptyPwsResult();
@@ -175,7 +191,7 @@ public class UrlManagerTest extends InstrumentationTestCase {
assertTrue(mUrlManager.containsInAnyCache(URL2));
}
- @SmallTest
+ @Test
public void testAddUrlUpdatesCache() throws Exception {
addEmptyPwsResult();
addEmptyPwsResult();
@@ -184,22 +200,19 @@ public class UrlManagerTest extends InstrumentationTestCase {
mUrlManager.addUrl(urlInfo);
List<UrlInfo> urls = mUrlManager.getUrls(true);
assertEquals(1, urls.size());
- assertEquals(urlInfo.getDistance(), urls.get(0).getDistance());
+ assertEquals(urlInfo.getDistance(), urls.get(0).getDistance(), EPSILON);
assertEquals(urlInfo.getDeviceAddress(), urls.get(0).getDeviceAddress());
assertEquals(urlInfo.getFirstSeenTimestamp(), urls.get(0).getFirstSeenTimestamp());
- urlInfo = new UrlInfo(URL1)
- .setDistance(100.0)
- .setDeviceAddress("00:11:22:33:AA:BB");
+ urlInfo = new UrlInfo(URL1).setDistance(100.0).setDeviceAddress("00:11:22:33:AA:BB");
mUrlManager.addUrl(urlInfo);
urls = mUrlManager.getUrls(true);
assertEquals(1, urls.size());
- assertEquals(urlInfo.getDistance(), urls.get(0).getDistance());
+ assertEquals(urlInfo.getDistance(), urls.get(0).getDistance(), EPSILON);
assertEquals(urlInfo.getDeviceAddress(), urls.get(0).getDeviceAddress());
}
- @SmallTest
- @RetryOnFailure
+ @Test
public void testAddUrlTwiceWorks() throws Exception {
// Add and remove an old URL twice and add new URL twice before removing.
// This should cover several issues involved with updating the cache queue.
@@ -222,7 +235,7 @@ public class UrlManagerTest extends InstrumentationTestCase {
assertTrue(mUrlManager.containsInAnyCache(URL2));
}
- @SmallTest
+ @Test
public void testGetUrlsSortsAndDedups() throws Exception {
// Construct results with matching group IDs and check that getUrls returns only the closest
// URL in each group. The list should be sorted by distance, closest first.
@@ -236,37 +249,38 @@ public class UrlManagerTest extends InstrumentationTestCase {
mUrlManager.addUrl(new UrlInfo(URL3, 10.0, System.currentTimeMillis()));
mUrlManager.addUrl(new UrlInfo(URL4, 40.0, System.currentTimeMillis()));
mUrlManager.addUrl(new UrlInfo(URL5, 50.0, System.currentTimeMillis()));
- getInstrumentation().waitForIdleSync();
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
// Make sure URLs are in order and duplicates are omitted.
List<UrlInfo> urlInfos = mUrlManager.getUrls();
assertEquals(3, urlInfos.size());
- assertEquals(10.0, urlInfos.get(0).getDistance());
+ assertEquals(10.0, urlInfos.get(0).getDistance(), EPSILON);
assertEquals(URL3, urlInfos.get(0).getUrl());
- assertEquals(30.0, urlInfos.get(1).getDistance());
+ assertEquals(30.0, urlInfos.get(1).getDistance(), EPSILON);
assertEquals(URL1, urlInfos.get(1).getUrl());
- assertEquals(50.0, urlInfos.get(2).getDistance());
+ assertEquals(50.0, urlInfos.get(2).getDistance(), EPSILON);
assertEquals(URL5, urlInfos.get(2).getUrl());
}
/*
- * @SmallTest
* Bug=crbug.com/684148
*/
@DisabledTest
+ @Test
public void testSerializationWorksWithPoorlySerializedResult() throws Exception {
addPwsResult1();
addPwsResult2();
long curTime = System.currentTimeMillis();
mUrlManager.addUrl(new UrlInfo(URL1, 99.5, curTime + 42));
mUrlManager.addUrl(new UrlInfo(URL2, 100.5, curTime + 43));
- getInstrumentation().waitForIdleSync();
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
// Create an invalid serialization.
Set<String> serializedUrls = new HashSet<>();
serializedUrls.add(new UrlInfo(URL1, 99.5, curTime + 42).jsonSerialize().toString());
serializedUrls.add("{\"not_a_value\": \"This is totally not a serialized UrlInfo.\"}");
- ContextUtils.getAppSharedPreferences().edit()
+ ContextUtils.getAppSharedPreferences()
+ .edit()
.putStringSet("physicalweb_all_urls", serializedUrls)
.apply();
@@ -279,15 +293,14 @@ public class UrlManagerTest extends InstrumentationTestCase {
}
@FlakyTest(message = "https://crbug.com/685471")
- @SmallTest
- @RetryOnFailure
+ @Test
public void testSerializationWorksWithoutGarbageCollection() throws Exception {
addPwsResult1();
addPwsResult2();
long curTime = System.currentTimeMillis();
mUrlManager.addUrl(new UrlInfo(URL1, 99.5, curTime + 42));
mUrlManager.addUrl(new UrlInfo(URL2, 100.5, curTime + 43));
- getInstrumentation().waitForIdleSync();
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
// Make sure all URLs are restored.
UrlManager urlManager = new UrlManager();
@@ -299,14 +312,14 @@ public class UrlManagerTest extends InstrumentationTestCase {
assertEquals(2, resolvedUrls.size());
}
- @SmallTest
@RetryOnFailure
+ @Test
public void testSerializationWorksWithGarbageCollection() throws Exception {
addPwsResult1();
addPwsResult2();
mUrlManager.addUrl(new UrlInfo(URL1, 99.5, 42));
mUrlManager.addUrl(new UrlInfo(URL2, 100.5, 43));
- getInstrumentation().waitForIdleSync();
+ ShadowLooper.runUiThreadTasksIncludingDelayedTasks();
// Make sure all URLs are restored.
UrlManager urlManager = new UrlManager();
@@ -316,34 +329,49 @@ public class UrlManagerTest extends InstrumentationTestCase {
assertEquals(0, resolvedUrls.size());
}
- @SmallTest
+ @Test
public void testUpgradeFromNone() throws Exception {
Set<String> oldResolvedUrls = new HashSet<String>();
oldResolvedUrls.add("old");
- ContextUtils.getAppSharedPreferences().edit()
+ SharedPreferences prefs = ContextUtils.getAppSharedPreferences();
+ prefs.edit()
.remove(UrlManager.getVersionKey())
.putStringSet("physicalweb_nearby_urls", oldResolvedUrls)
.putInt("org.chromium.chrome.browser.physicalweb.VERSION", 1)
.putInt("org.chromium.chrome.browser.physicalweb.BOTTOM_BAR_DISPLAY_COUNT", 1)
+ .putBoolean("physical_web_ignore_other_clients", true)
.apply();
+ final Lock lock = new ReentrantLock();
+ final Condition condition = lock.newCondition();
+ prefs.registerOnSharedPreferenceChangeListener(
+ new SharedPreferences.OnSharedPreferenceChangeListener() {
+ public void onSharedPreferenceChanged(
+ SharedPreferences sharedPreferences, String key) {
+ lock.lock();
+ try {
+ condition.signal();
+ } finally {
+ lock.unlock();
+ }
+ }
+ });
new UrlManager();
+ lock.lock();
+ try {
+ assertTrue(condition.await(2, TimeUnit.SECONDS));
+ } finally {
+ lock.unlock();
+ }
// Make sure the new prefs are populated and old prefs are gone.
- final SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferences();
- CriteriaHelper.pollInstrumentationThread(new Criteria() {
- @Override
- public boolean isSatisfied() {
- SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferences();
- return sharedPreferences.contains(UrlManager.getVersionKey())
- && !sharedPreferences.contains("physicalweb_nearby_urls")
- && !sharedPreferences.contains(
- "org.chromium.chrome.browser.physicalweb.VERSION")
- && !sharedPreferences.contains("org.chromium.chrome.browser.physicalweb"
- + ".BOTTOM_BAR_DISPLAY_COUNT");
- }
- }, 5000, CriteriaHelper.DEFAULT_POLLING_INTERVAL);
-
- assertEquals(UrlManager.getVersion(),
- sharedPreferences.getInt(UrlManager.getVersionKey(), 0));
+ SharedPreferences sharedPreferences = ContextUtils.getAppSharedPreferences();
+ assertTrue(sharedPreferences.contains(UrlManager.getVersionKey()));
+ assertEquals(
+ UrlManager.getVersion(), sharedPreferences.getInt(UrlManager.getVersionKey(), 0));
+ assertFalse(sharedPreferences.contains("physicalweb_nearby_urls"));
+ assertFalse(sharedPreferences.contains("org.chromium.chrome.browser.physicalweb.VERSION"));
+ assertFalse(sharedPreferences.contains(
+ "org.chromium.chrome.browser.physicalweb.BOTTOM_BAR_DISPLAY_COUNT"));
+ assertFalse(sharedPreferences.contains("physical_web_ignore_other_clients"));
}
}
« no previous file with comments | « chrome/android/junit/src/org/chromium/chrome/browser/physicalweb/MockPwsClient.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698