Commit 5228d5af authored by pkotwicz's avatar pkotwicz Committed by Commit bot

Fix org.chromium.chrome.browser.GeolocationTest

The geolocation tests did not work because:
- They referred to incorrect test pages.
  Correct test pages:
    https://chrome-internal-review.googlesource.com/#/c/27084/3/test/data/android/geolocation/geolocation_on_load.html
    https://chrome-internal-review.googlesource.com/#/c/27084/3/test/data/android/geolocation/geolocation_watch_on_load.html
- A race condition was causing LocationProviderAdapter.newLocationAvailable() to
  be often called prior to LocationProviderFactory.LocationProvider.start().

In addition to fixing the above problems, this CL makes
org.chromium.chrome.browser.GeolocationTest more similar to
- org.chromium.android_webview.test.GeolocationTest
- org.chromium.content.browser.ContentViewLocationTest
In particular, all of the tests now:
- Use the same test page
- Use MockLocationProvider

BUG=141518
Test=Less flakiness

Review URL: https://codereview.chromium.org/1599113002

Cr-Commit-Position: refs/heads/master@{#370807}
parent 75527dd8
...@@ -4,22 +4,20 @@ ...@@ -4,22 +4,20 @@
package org.chromium.chrome.browser; package org.chromium.chrome.browser;
import android.location.Location; import android.test.suitebuilder.annotation.MediumTest;
import android.test.FlakyTest; 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.infobar.InfoBarContainer;
import org.chromium.chrome.browser.tab.EmptyTabObserver; import org.chromium.chrome.browser.tab.EmptyTabObserver;
import org.chromium.chrome.browser.tab.Tab; 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.ChromeActivityTestCaseBase;
import org.chromium.chrome.test.util.InfoBarTestAnimationListener; import org.chromium.chrome.test.util.InfoBarTestAnimationListener;
import org.chromium.chrome.test.util.InfoBarUtil; import org.chromium.chrome.test.util.InfoBarUtil;
import org.chromium.chrome.test.util.TestHttpServerClient; 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;
import org.chromium.content.browser.LocationProviderFactory.LocationProvider;
import org.chromium.content.browser.test.util.CallbackHelper; import org.chromium.content.browser.test.util.CallbackHelper;
import org.chromium.content.browser.test.util.MockLocationProvider;
/** /**
* Test suite for Geo-Location functionality. * Test suite for Geo-Location functionality.
...@@ -33,9 +31,35 @@ public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity> ...@@ -33,9 +31,35 @@ public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity>
private static final double LATITUDE = 51.01; private static final double LATITUDE = 51.01;
private static final double LONGITUDE = 0.23; private static final double LONGITUDE = 0.23;
private static final float ACCURACY = 10; private static final float ACCURACY = 10;
private static final String TEST_FILE = "content/test/data/android/geolocation.html";
private InfoBarTestAnimationListener mListener; 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() { public GeolocationTest() {
super(ChromeActivity.class); super(ChromeActivity.class);
} }
...@@ -49,143 +73,58 @@ public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity> ...@@ -49,143 +73,58 @@ public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity>
mListener = new InfoBarTestAnimationListener(); mListener = new InfoBarTestAnimationListener();
container.setAnimationListener(mListener); container.setAnimationListener(mListener);
LocationProviderFactory.setLocationProviderImpl(new LocationProvider() { LocationProviderFactory.setLocationProviderImpl(new MockLocationProvider());
private boolean mIsRunning;
@Override
public boolean isRunning() {
return mIsRunning;
}
@Override
public void start(boolean gpsEnabled) {
mIsRunning = true;
}
@Override
public void stop() {
mIsRunning = false;
}
});
} }
/** /**
* Verify Geolocation creates an InfoBar and receives a mock location. * Verify Geolocation creates an InfoBar and receives a mock location.
*
* Fails frequently.
* Bug 141518
* @Smoke
* @MediumTest
* @Feature({"Location", "Main"})
*
* @throws Exception * @throws Exception
*/ */
@FlakyTest @Smoke
@MediumTest
@Feature({"Location", "Main"})
public void testGeolocationPlumbing() throws Exception { public void testGeolocationPlumbing() throws Exception {
final String url = TestHttpServerClient.getUrl( final String url = TestHttpServerClient.getUrl(
"chrome/test/data/geolocation/geolocation_on_load.html"); "content/test/data/android/geolocation.html");
Tab tab = getActivity().getActivityTab(); Tab tab = getActivity().getActivityTab();
final CallbackHelper loadCallback = new CallbackHelper(); GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
TabObserver observer = new EmptyTabObserver() { tab.addObserver(updateWaiter);
@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);
loadUrl(url); loadUrl(url);
runJavaScriptCodeInCurrentTab("initiate_getCurrentPosition()");
assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished()); assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished());
assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(getInfoBars().get(0))); assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(getInfoBars().get(0)));
updateWaiter.waitForNumUpdates(1);
sendLocation(createMockLocation(LATITUDE, LONGITUDE, ACCURACY)); tab.removeObserver(updateWaiter);
loadCallback.waitForCallback(0);
tab.removeObserver(observer);
} }
/** /**
* Verify Geolocation creates an InfoBar and receives multiple locations. * Verify Geolocation creates an InfoBar and receives multiple locations.
*
* Bug 141518
* @MediumTest
* @Feature({"Location"})
*
* @throws Exception * @throws Exception
*/ */
@FlakyTest @MediumTest
@Feature({"Location"})
public void testGeolocationWatch() throws Exception { public void testGeolocationWatch() throws Exception {
final String url = TestHttpServerClient.getUrl( final String url = TestHttpServerClient.getUrl(
"chrome/test/data/geolocation/geolocation_on_load.html"); "content/test/data/android/geolocation.html");
Tab tab = getActivity().getActivityTab(); Tab tab = getActivity().getActivityTab();
final CallbackHelper loadCallback0 = new CallbackHelper(); GeolocationUpdateWaiter updateWaiter = new GeolocationUpdateWaiter();
TabObserver observer = new EmptyTabObserver() { tab.addObserver(updateWaiter);
@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);
loadUrl(url); loadUrl(url);
runJavaScriptCodeInCurrentTab("initiate_watchPosition()");
assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished()); assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished());
assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(getInfoBars().get(0))); assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(getInfoBars().get(0)));
sendLocation(createMockLocation(LATITUDE, LONGITUDE, ACCURACY)); updateWaiter.waitForNumUpdates(2);
loadCallback0.waitForCallback(0);
tab.removeObserver(observer);
// Send another location: it'll update the URL again, so increase the count. tab.removeObserver(updateWaiter);
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);
} }
@Override @Override
public void startMainActivity() throws InterruptedException { public void startMainActivity() throws InterruptedException {
startMainActivityOnBlankPage(); 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());
}
});
}
} }
...@@ -6,9 +6,19 @@ ...@@ -6,9 +6,19 @@
var positionCount = 0; var positionCount = 0;
function gotPos(position) { function gotPos(position) {
positionCount++; positionCount++;
window.document.title = 'Count:' + positionCount;
}
function errorCallback(error){
window.document.title = 'deny';
console.log('navigator.getCurrentPosition error: ', error);
}
function initiate_getCurrentPosition() {
navigator.geolocation.getCurrentPosition(
gotPos, errorCallback, { });
} }
function initiate_watchPosition() { function initiate_watchPosition() {
navigator.geolocation.watchPosition(gotPos, function() { }, { }); navigator.geolocation.watchPosition(
gotPos, errorCallback, { });
} }
</script> </script>
</head> </head>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment