Index: chrome/android/javatests/src/org/chromium/chrome/browser/GeolocationTest.java |
diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/GeolocationTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/GeolocationTest.java |
index ea5182c6cbcfa9fdb97b09bb3b6537a7170b9d6a..9515b1a44394f77ba60e5c1468ee5d09a88c9488 100644 |
--- a/chrome/android/javatests/src/org/chromium/chrome/browser/GeolocationTest.java |
+++ b/chrome/android/javatests/src/org/chromium/chrome/browser/GeolocationTest.java |
@@ -4,22 +4,20 @@ |
package org.chromium.chrome.browser; |
-import android.location.Location; |
-import android.test.FlakyTest; |
+import android.test.suitebuilder.annotation.MediumTest; |
+import android.test.suitebuilder.annotation.Smoke; |
-import org.chromium.base.ThreadUtils; |
+import org.chromium.base.test.util.Feature; |
import org.chromium.chrome.browser.infobar.InfoBarContainer; |
import org.chromium.chrome.browser.tab.EmptyTabObserver; |
import org.chromium.chrome.browser.tab.Tab; |
-import org.chromium.chrome.browser.tab.TabObserver; |
import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
import org.chromium.chrome.test.util.InfoBarTestAnimationListener; |
import org.chromium.chrome.test.util.InfoBarUtil; |
import org.chromium.chrome.test.util.TestHttpServerClient; |
-import org.chromium.content.browser.LocationProviderAdapter; |
import org.chromium.content.browser.LocationProviderFactory; |
-import org.chromium.content.browser.LocationProviderFactory.LocationProvider; |
import org.chromium.content.browser.test.util.CallbackHelper; |
+import org.chromium.content.browser.test.util.MockLocationProvider; |
/** |
* Test suite for Geo-Location functionality. |
@@ -33,9 +31,35 @@ public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity> |
private static final double LATITUDE = 51.01; |
private static final double LONGITUDE = 0.23; |
private static final float ACCURACY = 10; |
+ private static final String TEST_FILE = "content/test/data/android/geolocation.html"; |
private InfoBarTestAnimationListener mListener; |
+ /** |
+ * Waits till the geolocation JavaScript callback is called the specified number of times. |
+ */ |
+ private class GeolocationUpdateWaiter extends EmptyTabObserver { |
+ private CallbackHelper mCallbackHelper; |
+ private int mExpectedCount; |
+ |
+ public GeolocationUpdateWaiter() { |
+ mCallbackHelper = new CallbackHelper(); |
+ } |
+ |
+ @Override |
+ public void onTitleUpdated(Tab tab) { |
+ String expectedTitle = "Count:" + mExpectedCount; |
+ if (getActivity().getActivityTab().getTitle().equals(expectedTitle)) { |
+ mCallbackHelper.notifyCalled(); |
+ } |
+ } |
+ |
+ public void waitForNumUpdates(int numUpdates) throws Exception { |
+ mExpectedCount = numUpdates; |
+ mCallbackHelper.waitForCallback(0); |
+ } |
+ } |
+ |
public GeolocationTest() { |
super(ChromeActivity.class); |
} |
@@ -49,143 +73,58 @@ public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity> |
mListener = new InfoBarTestAnimationListener(); |
container.setAnimationListener(mListener); |
- LocationProviderFactory.setLocationProviderImpl(new LocationProvider() { |
- private boolean mIsRunning; |
- |
- @Override |
- public boolean isRunning() { |
- return mIsRunning; |
- } |
- |
- @Override |
- public void start(boolean gpsEnabled) { |
- mIsRunning = true; |
- } |
- |
- @Override |
- public void stop() { |
- mIsRunning = false; |
- } |
- }); |
+ LocationProviderFactory.setLocationProviderImpl(new MockLocationProvider()); |
} |
/** |
* Verify Geolocation creates an InfoBar and receives a mock location. |
- * |
- * Fails frequently. |
- * Bug 141518 |
- * @Smoke |
- * @MediumTest |
- * @Feature({"Location", "Main"}) |
- * |
* @throws Exception |
*/ |
- @FlakyTest |
+ @Smoke |
+ @MediumTest |
+ @Feature({"Location", "Main"}) |
public void testGeolocationPlumbing() throws Exception { |
final String url = TestHttpServerClient.getUrl( |
- "chrome/test/data/geolocation/geolocation_on_load.html"); |
+ "content/test/data/android/geolocation.html"); |
Tab tab = getActivity().getActivityTab(); |
- final CallbackHelper loadCallback = new CallbackHelper(); |
- TabObserver observer = new EmptyTabObserver() { |
- @Override |
- public void onLoadStopped(Tab tab, boolean toDifferentDocument) { |
- // If the device has a cached non-mock location, we won't get back our |
- // lat/long, so checking that it has "#pass" is sufficient. |
- if (tab.getUrl().startsWith(url + "#pass|")) { |
- loadCallback.notifyCalled(); |
- } |
- } |
- }; |
- tab.addObserver(observer); |
+ GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter(); |
+ tab.addObserver(updateWaiter); |
+ |
loadUrl(url); |
+ runJavaScriptCodeInCurrentTab("initiate_getCurrentPosition()"); |
assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished()); |
assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(getInfoBars().get(0))); |
+ updateWaiter.waitForNumUpdates(1); |
- sendLocation(createMockLocation(LATITUDE, LONGITUDE, ACCURACY)); |
- loadCallback.waitForCallback(0); |
- tab.removeObserver(observer); |
+ tab.removeObserver(updateWaiter); |
} |
/** |
* Verify Geolocation creates an InfoBar and receives multiple locations. |
- * |
- * Bug 141518 |
- * @MediumTest |
- * @Feature({"Location"}) |
- * |
* @throws Exception |
*/ |
- @FlakyTest |
+ @MediumTest |
+ @Feature({"Location"}) |
public void testGeolocationWatch() throws Exception { |
final String url = TestHttpServerClient.getUrl( |
- "chrome/test/data/geolocation/geolocation_on_load.html"); |
+ "content/test/data/android/geolocation.html"); |
Tab tab = getActivity().getActivityTab(); |
- final CallbackHelper loadCallback0 = new CallbackHelper(); |
- TabObserver observer = new EmptyTabObserver() { |
- @Override |
- public void onLoadStopped(Tab tab, boolean toDifferentDocument) { |
- // If the device has a cached non-mock location, we won't get back our |
- // lat/long, so checking that it has "#pass" is sufficient. |
- if (tab.getUrl().startsWith(url + "#pass|0|")) { |
- loadCallback0.notifyCalled(); |
- } |
- } |
- }; |
- tab.addObserver(observer); |
+ GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter(); |
+ tab.addObserver(updateWaiter); |
+ |
loadUrl(url); |
+ runJavaScriptCodeInCurrentTab("initiate_watchPosition()"); |
assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished()); |
- |
assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(getInfoBars().get(0))); |
- sendLocation(createMockLocation(LATITUDE, LONGITUDE, ACCURACY)); |
- loadCallback0.waitForCallback(0); |
- tab.removeObserver(observer); |
- |
- // Send another location: it'll update the URL again, so increase the count. |
- final CallbackHelper loadCallback1 = new CallbackHelper(); |
- observer = new EmptyTabObserver() { |
- @Override |
- public void onLoadStopped(Tab tab, boolean toDifferentDocument) { |
- // If the device has a cached non-mock location, we won't get back our |
- // lat/long, so checking that it has "#pass" is sufficient. |
- if (tab.getUrl().startsWith(url + "#pass|1|")) { |
- loadCallback1.notifyCalled(); |
- } |
- } |
- }; |
- tab.addObserver(observer); |
- sendLocation(createMockLocation(LATITUDE + 1, LONGITUDE - 1, ACCURACY - 1)); |
- loadCallback1.waitForCallback(0); |
- tab.removeObserver(observer); |
+ updateWaiter.waitForNumUpdates(2); |
+ |
+ tab.removeObserver(updateWaiter); |
} |
@Override |
public void startMainActivity() throws InterruptedException { |
startMainActivityOnBlankPage(); |
} |
- |
- private Location createMockLocation(double latitude, double longitude, float accuracy) { |
- Location mockLocation = new Location(LOCATION_PROVIDER_MOCK); |
- mockLocation.setLatitude(latitude); |
- mockLocation.setLongitude(longitude); |
- mockLocation.setAccuracy(accuracy); |
- mockLocation.setTime(System.currentTimeMillis()); |
- return mockLocation; |
- } |
- |
- private void sendLocation(final Location location) { |
- ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
- @Override |
- public void run() { |
- LocationProviderAdapter.newLocationAvailable( |
- location.getLatitude(), location.getLongitude(), |
- location.getTime() / 1000.0, |
- location.hasAltitude(), location.getAltitude(), |
- location.hasAccuracy(), location.getAccuracy(), |
- location.hasBearing(), location.getBearing(), |
- location.hasSpeed(), location.getSpeed()); |
- } |
- }); |
- } |
} |