| Index: chrome/android/javatests/src/org/chromium/chrome/browser/omnibox/geo/GeolocationHeaderTest.java
|
| diff --git a/chrome/android/javatests/src/org/chromium/chrome/browser/omnibox/geo/GeolocationHeaderTest.java b/chrome/android/javatests/src/org/chromium/chrome/browser/omnibox/geo/GeolocationHeaderTest.java
|
| index 272278efceef71bc3828194b07d68eae5d1fc9fc..1a4cc47857469181f170b482da2d56e326acc332 100644
|
| --- a/chrome/android/javatests/src/org/chromium/chrome/browser/omnibox/geo/GeolocationHeaderTest.java
|
| +++ b/chrome/android/javatests/src/org/chromium/chrome/browser/omnibox/geo/GeolocationHeaderTest.java
|
| @@ -4,36 +4,36 @@
|
|
|
| package org.chromium.chrome.browser.omnibox.geo;
|
|
|
| -import android.content.Context;
|
| import android.location.Location;
|
| import android.location.LocationManager;
|
| import android.os.Build;
|
| import android.os.SystemClock;
|
| -import android.test.InstrumentationTestCase;
|
| -import android.test.UiThreadTest;
|
| import android.test.suitebuilder.annotation.SmallTest;
|
|
|
| +import org.chromium.base.ContextUtils;
|
| +import org.chromium.base.ThreadUtils;
|
| import org.chromium.base.library_loader.ProcessInitException;
|
| import org.chromium.base.test.util.Feature;
|
| -import org.chromium.chrome.browser.init.ChromeBrowserInitializer;
|
| +import org.chromium.chrome.browser.ChromeActivity;
|
| import org.chromium.chrome.browser.preferences.website.ContentSetting;
|
| import org.chromium.chrome.browser.preferences.website.GeolocationInfo;
|
| +import org.chromium.chrome.browser.tab.Tab;
|
| +import org.chromium.chrome.test.ChromeActivityTestCaseBase;
|
|
|
| /**
|
| * Tests for GeolocationHeader and GeolocationTracker.
|
| */
|
| -public class GeolocationHeaderTest extends InstrumentationTestCase {
|
| -
|
| +public class GeolocationHeaderTest extends ChromeActivityTestCaseBase<ChromeActivity> {
|
| private static final String SEARCH_URL_1 = "https://www.google.com/search?q=potatoes";
|
| private static final String SEARCH_URL_2 = "https://www.google.co.jp/webhp?#q=dinosaurs";
|
|
|
| + public GeolocationHeaderTest() {
|
| + super(ChromeActivity.class);
|
| + }
|
| +
|
| @SmallTest
|
| @Feature({"Location"})
|
| - @UiThreadTest
|
| public void testGeolocationHeader() throws ProcessInitException {
|
| - Context targetContext = getInstrumentation().getTargetContext();
|
| - ChromeBrowserInitializer.getInstance(targetContext).handleSynchronousStartup();
|
| -
|
| setMockLocation(20.3, 155.8, System.currentTimeMillis());
|
|
|
| // X-Geo should be sent for Google search results page URLs.
|
| @@ -55,41 +55,64 @@ public class GeolocationHeaderTest extends InstrumentationTestCase {
|
|
|
| // X-Geo shouldn't be sent when location is disallowed for https origin.
|
| // If https origin doesn't have a location setting, fall back to value for http origin.
|
| - assertNotNull(getHeaderWithPermissions(ContentSetting.ALLOW, ContentSetting.ALLOW));
|
| - assertNotNull(getHeaderWithPermissions(ContentSetting.ALLOW, ContentSetting.DEFAULT));
|
| - assertNotNull(getHeaderWithPermissions(ContentSetting.ALLOW, ContentSetting.BLOCK));
|
| - assertNotNull(getHeaderWithPermissions(ContentSetting.DEFAULT, ContentSetting.ALLOW));
|
| - assertNotNull(getHeaderWithPermissions(ContentSetting.DEFAULT, ContentSetting.DEFAULT));
|
| - assertNull(getHeaderWithPermissions(ContentSetting.DEFAULT, ContentSetting.BLOCK));
|
| - assertNull(getHeaderWithPermissions(ContentSetting.BLOCK, ContentSetting.ALLOW));
|
| - assertNull(getHeaderWithPermissions(ContentSetting.BLOCK, ContentSetting.DEFAULT));
|
| - assertNull(getHeaderWithPermissions(ContentSetting.BLOCK, ContentSetting.BLOCK));
|
| + checkHeaderWithPermissions(ContentSetting.ALLOW, ContentSetting.ALLOW, false);
|
| + checkHeaderWithPermissions(ContentSetting.ALLOW, ContentSetting.DEFAULT, false);
|
| + checkHeaderWithPermissions(ContentSetting.ALLOW, ContentSetting.BLOCK, false);
|
| + checkHeaderWithPermissions(ContentSetting.DEFAULT, ContentSetting.ALLOW, false);
|
| + checkHeaderWithPermissions(ContentSetting.DEFAULT, ContentSetting.DEFAULT, false);
|
| + checkHeaderWithPermissions(ContentSetting.DEFAULT, ContentSetting.BLOCK, true);
|
| + checkHeaderWithPermissions(ContentSetting.BLOCK, ContentSetting.ALLOW, true);
|
| + checkHeaderWithPermissions(ContentSetting.BLOCK, ContentSetting.DEFAULT, true);
|
| + checkHeaderWithPermissions(ContentSetting.BLOCK, ContentSetting.BLOCK, true);
|
|
|
| // X-Geo should be sent only with non-stale locations.
|
| long now = System.currentTimeMillis();
|
| long oneHour = 60 * 60 * 1000;
|
| long oneWeek = 7 * 24 * 60 * 60 * 1000;
|
| - assertNotNull(getHeaderWithLocation(20.3, 155.8, now));
|
| - assertNotNull(getHeaderWithLocation(20.3, 155.8, now - oneHour));
|
| - assertNull(getHeaderWithLocation(20.3, 155.8, now - oneWeek));
|
| + checkHeaderWithLocation(20.3, 155.8, now, false);
|
| + checkHeaderWithLocation(20.3, 155.8, now - oneHour, false);
|
| + checkHeaderWithLocation(20.3, 155.8, now - oneWeek, true);
|
| GeolocationTracker.setLocationForTesting(null);
|
| assertNullHeader(SEARCH_URL_1, false);
|
| }
|
|
|
| - private String getHeaderWithPermissions(ContentSetting httpsPermission,
|
| - ContentSetting httpPermission) {
|
| - GeolocationInfo infoHttps = new GeolocationInfo("https://www.google.de", null, false);
|
| - GeolocationInfo infoHttp = new GeolocationInfo("http://www.google.de", null, false);
|
| - infoHttps.setContentSetting(httpsPermission);
|
| - infoHttp.setContentSetting(httpPermission);
|
| - return GeolocationHeader.getGeoHeader(getInstrumentation().getTargetContext(),
|
| - "https://www.google.de/search?q=kartoffelsalat", false);
|
| + private void checkHeaderWithPermissions(final ContentSetting httpsPermission,
|
| + final ContentSetting httpPermission, final boolean shouldBeNull) {
|
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + GeolocationInfo infoHttps =
|
| + new GeolocationInfo("https://www.google.de", null, false);
|
| + GeolocationInfo infoHttp = new GeolocationInfo("http://www.google.de", null, false);
|
| + infoHttps.setContentSetting(httpsPermission);
|
| + infoHttp.setContentSetting(httpPermission);
|
| + String header = GeolocationHeader.getGeoHeader(ContextUtils.getApplicationContext(),
|
| + "https://www.google.de/search?q=kartoffelsalat",
|
| + getActivity().getActivityTab());
|
| + assertHeaderState(header, shouldBeNull);
|
| + }
|
| + });
|
| }
|
|
|
| - private String getHeaderWithLocation(double latitute, double longitude, long time) {
|
| - setMockLocation(latitute, longitude, time);
|
| - return GeolocationHeader.getGeoHeader(getInstrumentation().getTargetContext(),
|
| - SEARCH_URL_1, false);
|
| + private void checkHeaderWithLocation(final double latitute, final double longitude,
|
| + final long time, final boolean shouldBeNull) {
|
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + setMockLocation(latitute, longitude, time);
|
| + String header = GeolocationHeader.getGeoHeader(ContextUtils.getApplicationContext(),
|
| + SEARCH_URL_1, getActivity().getActivityTab());
|
| + assertHeaderState(header, shouldBeNull);
|
| + }
|
| + });
|
| + }
|
| +
|
| + private void assertHeaderState(String header, boolean shouldBeNull) {
|
| + if (shouldBeNull) {
|
| + assertNull(header);
|
| + } else {
|
| + assertNotNull(header);
|
| + }
|
| }
|
|
|
| private void setMockLocation(double latitute, double longitude, long time) {
|
| @@ -105,13 +128,38 @@ public class GeolocationHeaderTest extends InstrumentationTestCase {
|
| GeolocationTracker.setLocationForTesting(location);
|
| }
|
|
|
| - private void assertNullHeader(String url, boolean isIncognito) {
|
| - Context targetContext = getInstrumentation().getTargetContext();
|
| - assertNull(GeolocationHeader.getGeoHeader(targetContext, url, isIncognito));
|
| + private void assertNullHeader(final String url, final boolean isIncognito) {
|
| + try {
|
| + final Tab tab = loadUrlInNewTab("about:blank", isIncognito);
|
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + assertNull(GeolocationHeader.getGeoHeader(
|
| + ContextUtils.getApplicationContext(), url, tab));
|
| + }
|
| + });
|
| + } catch (InterruptedException e) {
|
| + fail(e.getMessage());
|
| + }
|
| + }
|
| +
|
| + private void assertNonNullHeader(final String url, final boolean isIncognito) {
|
| + try {
|
| + final Tab tab = loadUrlInNewTab("about:blank", isIncognito);
|
| + ThreadUtils.runOnUiThreadBlocking(new Runnable() {
|
| + @Override
|
| + public void run() {
|
| + assertNotNull(GeolocationHeader.getGeoHeader(
|
| + ContextUtils.getApplicationContext(), url, tab));
|
| + }
|
| + });
|
| + } catch (InterruptedException e) {
|
| + fail(e.getMessage());
|
| + }
|
| }
|
|
|
| - private void assertNonNullHeader(String url, boolean isIncognito) {
|
| - Context targetContext = getInstrumentation().getTargetContext();
|
| - assertNotNull(GeolocationHeader.getGeoHeader(targetContext, url, isIncognito));
|
| + @Override
|
| + public void startMainActivity() throws InterruptedException {
|
| + startMainActivityOnBlankPage();
|
| }
|
| }
|
|
|