OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.chrome.browser.omnibox.geo; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.location.Location; |
| 9 import android.location.LocationManager; |
| 10 import android.os.Build; |
| 11 import android.os.SystemClock; |
| 12 import android.test.InstrumentationTestCase; |
| 13 import android.test.UiThreadTest; |
| 14 import android.test.suitebuilder.annotation.SmallTest; |
| 15 |
| 16 import org.chromium.base.library_loader.LibraryProcessType; |
| 17 import org.chromium.base.library_loader.ProcessInitException; |
| 18 import org.chromium.base.test.util.Feature; |
| 19 import org.chromium.chrome.browser.preferences.website.ContentSetting; |
| 20 import org.chromium.chrome.browser.preferences.website.GeolocationInfo; |
| 21 import org.chromium.content.browser.BrowserStartupController; |
| 22 |
| 23 /** |
| 24 * Tests for GeolocationHeader and GeolocationTracker. |
| 25 */ |
| 26 public class GeolocationHeaderTest extends InstrumentationTestCase { |
| 27 |
| 28 private static final String SEARCH_URL_1 = "https://www.google.com/search?q=
potatoes"; |
| 29 private static final String SEARCH_URL_2 = "https://www.google.co.jp/webhp?#
q=dinosaurs"; |
| 30 |
| 31 @SmallTest |
| 32 @Feature({"Location"}) |
| 33 @UiThreadTest |
| 34 public void testGeolocationHeader() throws ProcessInitException { |
| 35 Context targetContext = getInstrumentation().getTargetContext(); |
| 36 BrowserStartupController.get(targetContext, LibraryProcessType.PROCESS_B
ROWSER) |
| 37 .startBrowserProcessesSync(false); |
| 38 |
| 39 setMockLocation(20.3, 155.8, System.currentTimeMillis()); |
| 40 |
| 41 // X-Geo should be sent for Google search results page URLs. |
| 42 assertNonNullHeader(SEARCH_URL_1, false); |
| 43 assertNonNullHeader(SEARCH_URL_2, false); |
| 44 |
| 45 // X-Geo shouldn't be sent in incognito mode. |
| 46 assertNullHeader(SEARCH_URL_1, true); |
| 47 assertNullHeader(SEARCH_URL_2, true); |
| 48 |
| 49 // X-Geo shouldn't be sent with URLs that aren't the Google search resul
ts page. |
| 50 assertNullHeader("invalid$url", false); |
| 51 assertNullHeader("https://www.chrome.fr/", false); |
| 52 assertNullHeader("https://www.google.com/", false); |
| 53 |
| 54 // X-Geo shouldn't be sent over HTTP. |
| 55 assertNullHeader("http://www.google.com/search?q=potatoes", false); |
| 56 assertNullHeader("http://www.google.com/webhp?#q=dinosaurs", false); |
| 57 |
| 58 // X-Geo shouldn't be sent when location is disallowed for https origin. |
| 59 // If https origin doesn't have a location setting, fall back to value f
or http origin. |
| 60 assertNotNull(getHeaderWithPermissions(ContentSetting.ALLOW, ContentSett
ing.ALLOW)); |
| 61 assertNotNull(getHeaderWithPermissions(ContentSetting.ALLOW, ContentSett
ing.DEFAULT)); |
| 62 assertNotNull(getHeaderWithPermissions(ContentSetting.ALLOW, ContentSett
ing.BLOCK)); |
| 63 assertNotNull(getHeaderWithPermissions(ContentSetting.DEFAULT, ContentSe
tting.ALLOW)); |
| 64 assertNotNull(getHeaderWithPermissions(ContentSetting.DEFAULT, ContentSe
tting.DEFAULT)); |
| 65 assertNull(getHeaderWithPermissions(ContentSetting.DEFAULT, ContentSetti
ng.BLOCK)); |
| 66 assertNull(getHeaderWithPermissions(ContentSetting.BLOCK, ContentSetting
.ALLOW)); |
| 67 assertNull(getHeaderWithPermissions(ContentSetting.BLOCK, ContentSetting
.DEFAULT)); |
| 68 assertNull(getHeaderWithPermissions(ContentSetting.BLOCK, ContentSetting
.BLOCK)); |
| 69 |
| 70 // X-Geo should be sent only with non-stale locations. |
| 71 long now = System.currentTimeMillis(); |
| 72 long oneHour = 60 * 60 * 1000; |
| 73 long oneWeek = 7 * 24 * 60 * 60 * 1000; |
| 74 assertNotNull(getHeaderWithLocation(20.3, 155.8, now)); |
| 75 assertNotNull(getHeaderWithLocation(20.3, 155.8, now - oneHour)); |
| 76 assertNull(getHeaderWithLocation(20.3, 155.8, now - oneWeek)); |
| 77 GeolocationTracker.setLocationForTesting(null); |
| 78 assertNullHeader(SEARCH_URL_1, false); |
| 79 } |
| 80 |
| 81 private String getHeaderWithPermissions(ContentSetting httpsPermission, |
| 82 ContentSetting httpPermission) { |
| 83 GeolocationInfo infoHttps = new GeolocationInfo("https://www.google.de",
null); |
| 84 GeolocationInfo infoHttp = new GeolocationInfo("http://www.google.de", n
ull); |
| 85 infoHttps.setContentSetting(httpsPermission); |
| 86 infoHttp.setContentSetting(httpPermission); |
| 87 return GeolocationHeader.getGeoHeader(getInstrumentation().getTargetCont
ext(), |
| 88 "https://www.google.de/search?q=kartoffelsalat", false); |
| 89 } |
| 90 |
| 91 private String getHeaderWithLocation(double latitute, double longitude, long
time) { |
| 92 setMockLocation(latitute, longitude, time); |
| 93 return GeolocationHeader.getGeoHeader(getInstrumentation().getTargetCont
ext(), |
| 94 SEARCH_URL_1, false); |
| 95 } |
| 96 |
| 97 private void setMockLocation(double latitute, double longitude, long time) { |
| 98 final Location location = new Location(LocationManager.NETWORK_PROVIDER)
; |
| 99 location.setLatitude(latitute); |
| 100 location.setLongitude(longitude); |
| 101 location.setAccuracy(20f); |
| 102 location.setTime(time); |
| 103 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { |
| 104 location.setElapsedRealtimeNanos(SystemClock.elapsedRealtimeNanos() |
| 105 + 1000000 * (time - System.currentTimeMillis())); |
| 106 } |
| 107 GeolocationTracker.setLocationForTesting(location); |
| 108 } |
| 109 |
| 110 private void assertNullHeader(String url, boolean isIncognito) { |
| 111 Context targetContext = getInstrumentation().getTargetContext(); |
| 112 assertNull(GeolocationHeader.getGeoHeader(targetContext, url, isIncognit
o)); |
| 113 } |
| 114 |
| 115 private void assertNonNullHeader(String url, boolean isIncognito) { |
| 116 Context targetContext = getInstrumentation().getTargetContext(); |
| 117 assertNotNull(GeolocationHeader.getGeoHeader(targetContext, url, isIncog
nito)); |
| 118 } |
| 119 } |
OLD | NEW |