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 @@
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);
updateWaiter.waitForNumUpdates(2);
// 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);
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());
}
});
}
}
......@@ -6,9 +6,19 @@
var positionCount = 0;
function gotPos(position) {
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() {
navigator.geolocation.watchPosition(gotPos, function() { }, { });
navigator.geolocation.watchPosition(
gotPos, errorCallback, { });
}
</script>
</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