| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.android_webview.test; | 5 package org.chromium.android_webview.test; |
| 6 | 6 |
| 7 import android.content.Context; |
| 7 import android.test.suitebuilder.annotation.MediumTest; | 8 import android.test.suitebuilder.annotation.MediumTest; |
| 8 import android.test.suitebuilder.annotation.SmallTest; | 9 import android.test.suitebuilder.annotation.SmallTest; |
| 9 import android.webkit.GeolocationPermissions; | 10 import android.webkit.GeolocationPermissions; |
| 10 | 11 |
| 11 import org.chromium.android_webview.AwContents; | 12 import org.chromium.android_webview.AwContents; |
| 13 import org.chromium.android_webview.AwSettings; |
| 12 import org.chromium.base.annotations.SuppressFBWarnings; | 14 import org.chromium.base.annotations.SuppressFBWarnings; |
| 13 import org.chromium.base.test.util.Feature; | 15 import org.chromium.base.test.util.Feature; |
| 14 import org.chromium.content.browser.LocationProviderFactory; | 16 import org.chromium.content.browser.LocationProviderFactory; |
| 15 import org.chromium.content.browser.test.util.MockLocationProvider; | 17 import org.chromium.content.browser.test.util.MockLocationProvider; |
| 16 | 18 |
| 17 import java.util.concurrent.Callable; | 19 import java.util.concurrent.Callable; |
| 18 | 20 |
| 19 /** | 21 /** |
| 20 * Test suite for Geolocation in AwContents. Smoke tests for | 22 * Test suite for Geolocation in AwContents. Smoke tests for |
| 21 * basic functionality, and tests to ensure the AwContents.onPause | 23 * basic functionality, and tests to ensure the AwContents.onPause |
| 22 * and onResume APIs affect Geolocation as expected. | 24 * and onResume APIs affect Geolocation as expected. |
| 23 */ | 25 */ |
| 24 public class GeolocationTest extends AwTestBase { | 26 public class GeolocationTest extends AwTestBase { |
| 25 private TestAwContentsClient mContentsClient; | 27 private TestAwContentsClient mContentsClient; |
| 26 private AwContents mAwContents; | 28 private AwContents mAwContents; |
| 27 private MockLocationProvider mMockLocationProvider; | 29 private MockLocationProvider mMockLocationProvider; |
| 30 private TestDependencyFactory mOverridenFactory; |
| 28 | 31 |
| 29 private static final String RAW_HTML = | 32 private static final String RAW_HTML = |
| 30 "<!DOCTYPE html>\n" | 33 "<!DOCTYPE html>\n" |
| 31 + "<html>\n" | 34 + "<html>\n" |
| 32 + " <head>\n" | 35 + " <head>\n" |
| 33 + " <title>Geolocation</title>\n" | 36 + " <title>Geolocation</title>\n" |
| 34 + " <script>\n" | 37 + " <script>\n" |
| 35 + " var positionCount = 0;\n" | 38 + " var positionCount = 0;\n" |
| 36 + " function gotPos(position) {\n" | 39 + " function gotPos(position) {\n" |
| 37 + " positionCount++;\n" | 40 + " positionCount++;\n" |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 @Override | 88 @Override |
| 86 public void setUp() throws Exception { | 89 public void setUp() throws Exception { |
| 87 super.setUp(); | 90 super.setUp(); |
| 88 mMockLocationProvider = new MockLocationProvider(); | 91 mMockLocationProvider = new MockLocationProvider(); |
| 89 LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider); | 92 LocationProviderFactory.setLocationProviderImpl(mMockLocationProvider); |
| 90 } | 93 } |
| 91 | 94 |
| 92 @Override | 95 @Override |
| 93 public void tearDown() throws Exception { | 96 public void tearDown() throws Exception { |
| 94 mMockLocationProvider.stopUpdates(); | 97 mMockLocationProvider.stopUpdates(); |
| 98 mOverridenFactory = null; |
| 95 super.tearDown(); | 99 super.tearDown(); |
| 96 } | 100 } |
| 97 | 101 |
| 102 @Override |
| 103 protected TestDependencyFactory createTestDependencyFactory() { |
| 104 return mOverridenFactory == null ? new TestDependencyFactory() : mOverri
denFactory; |
| 105 } |
| 106 |
| 98 private int getPositionCountFromJS() { | 107 private int getPositionCountFromJS() { |
| 99 int result = -1; | 108 int result = -1; |
| 100 try { | 109 try { |
| 101 result = Integer.parseInt(executeJavaScriptAndWaitForResult( | 110 result = Integer.parseInt(executeJavaScriptAndWaitForResult( |
| 102 mAwContents, mContentsClient, "positionCount")); | 111 mAwContents, mContentsClient, "positionCount")); |
| 103 } catch (Exception e) { | 112 } catch (Exception e) { |
| 104 fail("Unable to get positionCount"); | 113 fail("Unable to get positionCount"); |
| 105 } | 114 } |
| 106 return result; | 115 return result; |
| 107 } | 116 } |
| 108 | 117 |
| 109 private void ensureGeolocationRunning(final boolean running) throws Exceptio
n { | 118 private void ensureGeolocationRunning(final boolean running) throws Exceptio
n { |
| 110 pollInstrumentationThread(new Callable<Boolean>() { | 119 pollInstrumentationThread(new Callable<Boolean>() { |
| 111 @Override | 120 @Override |
| 112 public Boolean call() throws Exception { | 121 public Boolean call() throws Exception { |
| 113 return mMockLocationProvider.isRunning() == running; | 122 return mMockLocationProvider.isRunning() == running; |
| 114 } | 123 } |
| 115 }); | 124 }); |
| 116 } | 125 } |
| 117 | 126 |
| 127 private static class GeolocationOnInsecureOriginsTestDependencyFactory |
| 128 extends TestDependencyFactory { |
| 129 private boolean mAllow; |
| 130 public GeolocationOnInsecureOriginsTestDependencyFactory(boolean allow)
{ |
| 131 mAllow = allow; |
| 132 } |
| 133 |
| 134 @Override |
| 135 public AwSettings createAwSettings(Context context, boolean supportLegac
yQuirks) { |
| 136 return new AwSettings(context, false /* isAccessFromFileURLsGrantedB
yDefault */, |
| 137 supportLegacyQuirks, false /* allowEmptyDocumentPersistence
*/, mAllow); |
| 138 } |
| 139 } |
| 118 | 140 |
| 119 /** | 141 /** |
| 120 * Ensure that a call to navigator.getCurrentPosition works in WebView. | 142 * Ensure that a call to navigator.getCurrentPosition works in WebView. |
| 121 */ | 143 */ |
| 122 @MediumTest | 144 @MediumTest |
| 123 @Feature({"AndroidWebView"}) | 145 @Feature({"AndroidWebView"}) |
| 124 public void testGetPosition() throws Throwable { | 146 public void testGetPosition() throws Throwable { |
| 125 initAwContents(new GrantPermisionAwContentClient()); | 147 initAwContents(new GrantPermisionAwContentClient()); |
| 126 loadDataWithBaseUrlSync(mAwContents, mContentsClient.getOnPageFinishedHe
lper(), RAW_HTML, | 148 loadDataWithBaseUrlSync(mAwContents, mContentsClient.getOnPageFinishedHe
lper(), RAW_HTML, |
| 127 "text/html", false, "https://google.com/", "about:blank"); | 149 "text/html", false, "https://google.com/", "about:blank"); |
| (...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 pollInstrumentationThread(new Callable<Boolean>() { | 312 pollInstrumentationThread(new Callable<Boolean>() { |
| 291 @SuppressFBWarnings("DM_GC") | 313 @SuppressFBWarnings("DM_GC") |
| 292 @Override | 314 @Override |
| 293 public Boolean call() throws Exception { | 315 public Boolean call() throws Exception { |
| 294 Runtime.getRuntime().gc(); | 316 Runtime.getRuntime().gc(); |
| 295 return "deny".equals(getTitleOnUiThread(mAwContents)); | 317 return "deny".equals(getTitleOnUiThread(mAwContents)); |
| 296 } | 318 } |
| 297 }); | 319 }); |
| 298 } | 320 } |
| 299 | 321 |
| 322 @Feature({"AndroidWebView"}) |
| 323 @SmallTest |
| 324 public void testDenyOnInsecureOrigins() throws Throwable { |
| 325 mOverridenFactory = new GeolocationOnInsecureOriginsTestDependencyFactor
y(false); |
| 326 initAwContents(new GrantPermisionAwContentClient()); |
| 327 loadDataWithBaseUrlSync(mAwContents, mContentsClient.getOnPageFinishedHe
lper(), RAW_HTML, |
| 328 "text/html", false, "http://google.com/", "about:blank"); |
| 329 |
| 330 mAwContents.evaluateJavaScriptForTests("initiate_getCurrentPosition();",
null); |
| 331 |
| 332 pollInstrumentationThread(new Callable<Boolean>() { |
| 333 @SuppressFBWarnings("DM_GC") |
| 334 @Override |
| 335 public Boolean call() throws Exception { |
| 336 Runtime.getRuntime().gc(); |
| 337 return "deny".equals(getTitleOnUiThread(mAwContents)); |
| 338 } |
| 339 }); |
| 340 } |
| 341 |
| 342 @Feature({"AndroidWebView"}) |
| 343 @SmallTest |
| 344 public void testAllowOnInsecureOriginsByDefault() throws Throwable { |
| 345 initAwContents(new GrantPermisionAwContentClient()); |
| 346 loadDataWithBaseUrlSync(mAwContents, mContentsClient.getOnPageFinishedHe
lper(), RAW_HTML, |
| 347 "text/html", false, "http://google.com/", "about:blank"); |
| 348 |
| 349 mAwContents.evaluateJavaScriptForTests("initiate_getCurrentPosition();",
null); |
| 350 |
| 351 pollInstrumentationThread(new Callable<Boolean>() { |
| 352 @Override |
| 353 public Boolean call() throws Exception { |
| 354 return getPositionCountFromJS() > 0; |
| 355 } |
| 356 }); |
| 357 } |
| 358 |
| 300 } | 359 } |
| OLD | NEW |