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; |
| 6 |
| 7 import android.location.Location; |
| 8 import android.test.FlakyTest; |
| 9 |
| 10 import org.chromium.base.ThreadUtils; |
| 11 import org.chromium.chrome.browser.infobar.InfoBar; |
| 12 import org.chromium.chrome.browser.infobar.InfoBarContainer; |
| 13 import org.chromium.chrome.test.ChromeActivityTestCaseBase; |
| 14 import org.chromium.chrome.test.util.InfoBarTestAnimationListener; |
| 15 import org.chromium.chrome.test.util.InfoBarUtil; |
| 16 import org.chromium.chrome.test.util.TestHttpServerClient; |
| 17 import org.chromium.content.browser.LocationProviderAdapter; |
| 18 import org.chromium.content.browser.LocationProviderFactory; |
| 19 import org.chromium.content.browser.LocationProviderFactory.LocationProvider; |
| 20 import org.chromium.content.browser.test.util.CallbackHelper; |
| 21 |
| 22 import java.util.List; |
| 23 |
| 24 /** |
| 25 * Test suite for Geo-Location functionality. |
| 26 * |
| 27 * These tests rely on the device being specially setup (which the bots do autom
atically): |
| 28 * - Global location is enabled. |
| 29 * - Google location is enabled. |
| 30 */ |
| 31 public class GeolocationTest extends ChromeActivityTestCaseBase<ChromeActivity>
{ |
| 32 private static final String LOCATION_PROVIDER_MOCK = "locationProviderMock"; |
| 33 private static final double LATITUDE = 51.01; |
| 34 private static final double LONGITUDE = 0.23; |
| 35 private static final float ACCURACY = 10; |
| 36 |
| 37 private InfoBarTestAnimationListener mListener; |
| 38 |
| 39 public GeolocationTest() { |
| 40 super(ChromeActivity.class); |
| 41 } |
| 42 |
| 43 @Override |
| 44 protected void setUp() throws Exception { |
| 45 super.setUp(); |
| 46 |
| 47 InfoBarContainer container = |
| 48 getActivity().getTabModelSelector().getCurrentTab().getInfoBarCo
ntainer(); |
| 49 mListener = new InfoBarTestAnimationListener(); |
| 50 container.setAnimationListener(mListener); |
| 51 |
| 52 LocationProviderFactory.setLocationProviderImpl(new LocationProvider() { |
| 53 private boolean mIsRunning; |
| 54 |
| 55 @Override |
| 56 public boolean isRunning() { |
| 57 return mIsRunning; |
| 58 } |
| 59 |
| 60 @Override |
| 61 public void start(boolean gpsEnabled) { |
| 62 mIsRunning = true; |
| 63 } |
| 64 |
| 65 @Override |
| 66 public void stop() { |
| 67 mIsRunning = false; |
| 68 } |
| 69 }); |
| 70 } |
| 71 |
| 72 /** |
| 73 * Verify Geolocation creates an InfoBar and receives a mock location. |
| 74 * |
| 75 * Fails frequently. |
| 76 * Bug 141518 |
| 77 * @Smoke |
| 78 * @MediumTest |
| 79 * @Feature({"Location", "Main"}) |
| 80 * |
| 81 * @throws Exception |
| 82 */ |
| 83 @FlakyTest |
| 84 public void testGeolocationPlumbing() throws Exception { |
| 85 final String url = TestHttpServerClient.getUrl( |
| 86 "chrome/test/data/geolocation/geolocation_on_load.html"); |
| 87 |
| 88 Tab tab = getActivity().getActivityTab(); |
| 89 final CallbackHelper loadCallback = new CallbackHelper(); |
| 90 TabObserver observer = new EmptyTabObserver() { |
| 91 @Override |
| 92 public void onLoadStopped(Tab tab) { |
| 93 // If the device has a cached non-mock location, we won't get ba
ck our |
| 94 // lat/long, so checking that it has "#pass" is sufficient. |
| 95 if (tab.getUrl().startsWith(url + "#pass|")) { |
| 96 loadCallback.notifyCalled(); |
| 97 } |
| 98 } |
| 99 }; |
| 100 tab.addObserver(observer); |
| 101 loadUrl(url); |
| 102 assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished()
); |
| 103 List<InfoBar> infoBars = getActivity().getActivityTab().getInfoBarContai
ner().getInfoBars(); |
| 104 assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(info
Bars.get(0))); |
| 105 |
| 106 sendLocation(createMockLocation(LATITUDE, LONGITUDE, ACCURACY)); |
| 107 loadCallback.waitForCallback(0); |
| 108 tab.removeObserver(observer); |
| 109 } |
| 110 |
| 111 /** |
| 112 * Verify Geolocation creates an InfoBar and receives multiple locations. |
| 113 * |
| 114 * Bug 141518 |
| 115 * @MediumTest |
| 116 * @Feature({"Location"}) |
| 117 * |
| 118 * @throws Exception |
| 119 */ |
| 120 @FlakyTest |
| 121 public void testGeolocationWatch() throws Exception { |
| 122 final String url = TestHttpServerClient.getUrl( |
| 123 "chrome/test/data/geolocation/geolocation_on_load.html"); |
| 124 |
| 125 Tab tab = getActivity().getActivityTab(); |
| 126 final CallbackHelper loadCallback0 = new CallbackHelper(); |
| 127 TabObserver observer = new EmptyTabObserver() { |
| 128 @Override |
| 129 public void onLoadStopped(Tab tab) { |
| 130 // If the device has a cached non-mock location, we won't get ba
ck our |
| 131 // lat/long, so checking that it has "#pass" is sufficient. |
| 132 if (tab.getUrl().startsWith(url + "#pass|0|")) { |
| 133 loadCallback0.notifyCalled(); |
| 134 } |
| 135 } |
| 136 }; |
| 137 tab.addObserver(observer); |
| 138 loadUrl(url); |
| 139 assertTrue("InfoBar not added.", mListener.addInfoBarAnimationFinished()
); |
| 140 |
| 141 List<InfoBar> infoBars = getActivity().getActivityTab().getInfoBarContai
ner().getInfoBars(); |
| 142 assertTrue("OK button wasn't found", InfoBarUtil.clickPrimaryButton(info
Bars.get(0))); |
| 143 |
| 144 sendLocation(createMockLocation(LATITUDE, LONGITUDE, ACCURACY)); |
| 145 |
| 146 loadCallback0.waitForCallback(0); |
| 147 tab.removeObserver(observer); |
| 148 |
| 149 // Send another location: it'll update the URL again, so increase the co
unt. |
| 150 final CallbackHelper loadCallback1 = new CallbackHelper(); |
| 151 observer = new EmptyTabObserver() { |
| 152 @Override |
| 153 public void onLoadStopped(Tab tab) { |
| 154 // If the device has a cached non-mock location, we won't get ba
ck our |
| 155 // lat/long, so checking that it has "#pass" is sufficient. |
| 156 if (tab.getUrl().startsWith(url + "#pass|1|")) { |
| 157 loadCallback1.notifyCalled(); |
| 158 } |
| 159 } |
| 160 }; |
| 161 tab.addObserver(observer); |
| 162 sendLocation(createMockLocation(LATITUDE + 1, LONGITUDE - 1, ACCURACY -
1)); |
| 163 loadCallback1.waitForCallback(0); |
| 164 tab.removeObserver(observer); |
| 165 } |
| 166 |
| 167 @Override |
| 168 public void startMainActivity() throws InterruptedException { |
| 169 startMainActivityOnBlankPage(); |
| 170 } |
| 171 |
| 172 private Location createMockLocation(double latitude, double longitude, float
accuracy) { |
| 173 Location mockLocation = new Location(LOCATION_PROVIDER_MOCK); |
| 174 mockLocation.setLatitude(latitude); |
| 175 mockLocation.setLongitude(longitude); |
| 176 mockLocation.setAccuracy(accuracy); |
| 177 mockLocation.setTime(System.currentTimeMillis()); |
| 178 return mockLocation; |
| 179 } |
| 180 |
| 181 private void sendLocation(final Location location) { |
| 182 ThreadUtils.runOnUiThreadBlocking(new Runnable() { |
| 183 @Override |
| 184 public void run() { |
| 185 LocationProviderAdapter.newLocationAvailable( |
| 186 location.getLatitude(), location.getLongitude(), |
| 187 location.getTime() / 1000.0, |
| 188 location.hasAltitude(), location.getAltitude(), |
| 189 location.hasAccuracy(), location.getAccuracy(), |
| 190 location.hasBearing(), location.getBearing(), |
| 191 location.hasSpeed(), location.getSpeed()); |
| 192 } |
| 193 }); |
| 194 } |
| 195 } |
OLD | NEW |