Chromium Code Reviews| Index: android_webview/javatests/src/org/chromium/android_webview/test/GeolocationTest.java |
| diff --git a/android_webview/javatests/src/org/chromium/android_webview/test/GeolocationTest.java b/android_webview/javatests/src/org/chromium/android_webview/test/GeolocationTest.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b75502c5ec8f8d90c4e8b3e2df88559ceb7bb664 |
| --- /dev/null |
| +++ b/android_webview/javatests/src/org/chromium/android_webview/test/GeolocationTest.java |
| @@ -0,0 +1,279 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +package org.chromium.android_webview.test; |
| + |
| +import android.content.Context; |
| +import android.location.Location; |
| +import android.location.LocationManager; |
| +import android.location.LocationProvider; |
| +import android.test.suitebuilder.annotation.SmallTest; |
| +import android.os.SystemClock; |
| +import android.webkit.GeolocationPermissions; |
| + |
| +import org.chromium.android_webview.AwContents; |
| +import org.chromium.base.test.util.Feature; |
| +import org.chromium.content.browser.test.util.Criteria; |
| +import org.chromium.content.browser.test.util.CriteriaHelper; |
| + |
| +import java.util.List; |
| + |
| +public class GeolocationTest extends AwTestBase { |
| + |
| + private static final long TEST_TIMEOUT = 20000L; |
| + private static final int CHECK_INTERVAL = 100; |
| + |
| + private TestAwContentsClient mContentsClient; |
| + private AwContents mAwContents; |
| + |
| + private static final String RAW_HTML = |
|
mkosiba (inactive)
2013/12/02 10:49:31
nit, minimally shorter:
CommonResources.makeHtml
|
| + "<!DOCTYPE html>\n" + |
| + "<html>\n" + |
| + " <head>\n" + |
| + " <title>Geolocation</title>\n" + |
| + " <script>\n" + |
| + " var positionCount = 0;\n" + |
| + " function gotPos(position) {\n" + |
| + " positionCount++;\n" + |
| + " }\n" + |
| + " function initiate_getCurrentPosition() {\n" + |
| + " navigator.geolocation.getCurrentPosition(\n" + |
| + " gotPos, function() { }, { });\n" + |
| + " }\n" + |
| + " function initiate_watchPosition() {\n" + |
| + " navigator.geolocation.watchPosition(\n" + |
| + " gotPos, function() { }, { });\n" + |
| + " }\n" + |
| + " </script>\n" + |
| + " </head>\n" + |
| + " <body>\n" + |
| + " </body>\n" + |
| + "</html>"; |
| + |
| + private LocationManager mLocationManager; |
| + private List<String> mProviders; |
| + |
| + @Override |
| + public void setUp() throws Exception { |
| + super.setUp(); |
| + mContentsClient = new TestAwContentsClient() { |
| + @Override |
| + public void onGeolocationPermissionsShowPrompt(String origin, |
| + GeolocationPermissions.Callback callback) { |
| + callback.invoke(origin, true, true); |
| + } |
| + }; |
| + mAwContents = createAwTestContainerViewOnMainSync(mContentsClient).getAwContents(); |
| + enableJavaScriptOnUiThread(mAwContents); |
| + setupGeolocation(); |
| + } |
| + |
| + @Override |
| + public void tearDown() throws Exception { |
| + for (String provider : mProviders) { |
| + try { |
| + mLocationManager.clearTestProviderEnabled(provider); |
| + mLocationManager.removeTestProvider(provider); |
| + } catch (IllegalArgumentException e) {} |
| + } |
| + GeolocationPermissions.getInstance().clearAll(); |
| + super.tearDown(); |
| + } |
| + |
| + private void setupGeolocation() { |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + mAwContents.getSettings().setGeolocationEnabled(true); |
| + } |
| + }); |
| + mLocationManager = (LocationManager)getActivity().getApplicationContext() |
| + .getSystemService(Context.LOCATION_SERVICE); |
| + mProviders = mLocationManager.getAllProviders(); |
| + for (String provider : mProviders) { |
| + // Can't mock passive provider. |
| + if (provider.equals(LocationManager.PASSIVE_PROVIDER)) { |
| + mProviders.remove(provider); |
| + break; |
| + } |
| + } |
| + for (String providerName : mProviders) { |
| + LocationProvider provider = mLocationManager.getProvider(providerName); |
| + mLocationManager.addTestProvider(provider.getName(), |
| + provider.requiresNetwork(), //requiresNetwork, |
| + provider.requiresSatellite(), // requiresSatellite, |
| + provider.requiresCell(), // requiresCell, |
| + provider.hasMonetaryCost(), // hasMonetaryCost, |
| + provider.supportsAltitude(), // supportsAltitude, |
| + provider.supportsSpeed(), // supportsSpeed, |
| + provider.supportsBearing(), // supportsBearing, |
| + provider.getPowerRequirement(), // powerRequirement |
| + provider.getAccuracy()); // accuracy |
| + mLocationManager.setTestProviderEnabled(provider.getName(), true); |
| + } |
| + } |
| + |
| + private void pushMockLocation() { |
| + for (int i = 0; i < mProviders.size(); i++) { |
| + Location location = new Location(mProviders.get(i)); |
| + location.setLatitude(40); |
| + location.setLongitude(40); |
| + location.setAccuracy(1.0f); |
| + location.setTime(java.lang.System.currentTimeMillis()); |
| + location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos()); |
| + mLocationManager.setTestProviderLocation(mProviders.get(i), location); |
| + } |
| + } |
| + |
| + private int getPositionCountFromJS() { |
| + int result = -1; |
| + try { |
| + result = Integer.parseInt(executeJavaScriptAndWaitForResult( |
| + mAwContents, mContentsClient, "positionCount")); |
| + } catch (Exception e) { |
| + fail("Unable to get positionCount"); |
| + } |
| + return result; |
| + } |
| + |
| + /** |
| + * Ensure that a call to navigator.getCurrentPosition works in WebView. |
| + */ |
| + @SmallTest |
| + @Feature({"AndroidWebView"}) |
| + public void testGetPosition() throws Throwable { |
| + loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), |
| + RAW_HTML, "text/html", false); |
| + |
| + mAwContents.evaluateJavaScript("initiate_getCurrentPosition();", null); |
| + pushMockLocation(); |
| + |
| + assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + return getPositionCountFromJS() == 1; |
| + } |
| + }, TEST_TIMEOUT, CHECK_INTERVAL)); |
| + |
| + mAwContents.evaluateJavaScript("initiate_getCurrentPosition();", null); |
| + pushMockLocation(); |
| + assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + return getPositionCountFromJS() == 2; |
| + } |
| + }, TEST_TIMEOUT, CHECK_INTERVAL)); |
| + } |
| + |
| + /** |
| + * Ensure that a call to navigator.watchPosition works in WebView. |
| + */ |
| + @SmallTest |
| + @Feature({"AndroidWebView"}) |
| + public void testWatchPosition() throws Throwable { |
| + loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), |
| + RAW_HTML, "text/html", false); |
| + |
| + mAwContents.evaluateJavaScript("initiate_watchPosition();", null); |
| + |
| + assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + pushMockLocation(); |
| + return getPositionCountFromJS() > 3; |
| + } |
| + }, TEST_TIMEOUT, CHECK_INTERVAL)); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"AndroidWebView"}) |
| + public void testPauseGeolocationOnPause() throws Throwable { |
| + // Start a watch going. |
| + loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), |
| + RAW_HTML, "text/html", false); |
| + |
| + mAwContents.evaluateJavaScript("initiate_watchPosition();", null); |
| + |
| + assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + pushMockLocation(); |
| + return getPositionCountFromJS() > 3; |
| + } |
| + }, TEST_TIMEOUT, CHECK_INTERVAL)); |
| + |
| + assertTrue(mAwContents.getContentViewCore().isGeolocationActiveForTesting()); |
| + |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + mAwContents.onPause(); |
| + } |
| + }); |
| + |
| + assertFalse(mAwContents.getContentViewCore().isGeolocationActiveForTesting()); |
| + |
| + try { |
| + executeJavaScriptAndWaitForResult(mAwContents, mContentsClient, "positionCount = 0"); |
| + } catch (Exception e) { |
| + fail("Unable to clear positionCount"); |
| + } |
| + assertEquals(0, getPositionCountFromJS()); |
| + |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + mAwContents.onResume(); |
| + } |
| + }); |
| + |
| + assertTrue(mAwContents.getContentViewCore().isGeolocationActiveForTesting()); |
| + |
| + assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + pushMockLocation(); |
| + return getPositionCountFromJS() > 3; |
| + } |
| + }, TEST_TIMEOUT, CHECK_INTERVAL)); |
| + } |
| + |
| + @SmallTest |
| + @Feature({"AndroidWebView"}) |
| + public void testPauseAwContentsBeforeNavigating() throws Throwable { |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + mAwContents.onPause(); |
| + } |
| + }); |
| + |
| + // Start a watch going. |
| + loadDataSync(mAwContents, mContentsClient.getOnPageFinishedHelper(), |
| + RAW_HTML, "text/html", false); |
| + |
| + mAwContents.evaluateJavaScript("initiate_watchPosition();", null); |
| + |
| + assertEquals(0, getPositionCountFromJS()); |
| + assertFalse(mAwContents.getContentViewCore().isGeolocationActiveForTesting()); |
| + |
| + getInstrumentation().runOnMainSync(new Runnable() { |
| + @Override |
| + public void run() { |
| + mAwContents.onResume(); |
| + } |
| + }); |
| + |
| + assertTrue(mAwContents.getContentViewCore().isGeolocationActiveForTesting()); |
| + |
| + assertTrue(CriteriaHelper.pollForCriteria(new Criteria() { |
| + @Override |
| + public boolean isSatisfied() { |
| + pushMockLocation(); |
| + return getPositionCountFromJS() > 3; |
| + } |
| + }, TEST_TIMEOUT, CHECK_INTERVAL)); |
| + |
| + } |
| +} |