I have this unit test in a Windows Store unit test project, using this overload of GetGeopositionAsync of the Geolocator:
[TestMethod]
public async Task TestFunc()
{
var locator = new Windows.Devices.Geolocation.Geolocator();
var l = await
locator.GetGeopositionAsync(TimeSpan.MaxValue, TimeSpan.FromSeconds(10));
Assert.IsNotNull(l);
}
But not only does this test not pass, nor bunk out after 10 seconds – it never finishes (I waited for 10 minutes before killing it).
I should stress that this is a paired-down version of the test I actually wanted to run – I’m not trying to just unit-test Windows; equally as has been pointed out in John Deters’ answer I could use a fake instead.
I’ve also written another test that subscribed to a GeoLocator‘s StatusChanged and PositionChanged events (the latter should initialise the locator), and then waited for one minute. In the StatusChanged handler I trace out the current LocationStatus, in addition to tracing it out as soon as I’ve created the Geolocator.
The LocationStatus of the Geolocator in this test starts at NotInitialised, and never changes.
Now I’m thinking that this is a blocking issue to do with the fact that (quoting from the GetGeopositionAsync documentation in the link at the start):
The first use of the Geolocator object to call GetGeopositionAsync must be made on the UI thread so that the consent prompt can be shown to the user. For more information, see Guidelines for devices that access personal data.
So my guess is either:
- a) The call is being made on a UI thread (unlikely) but because there’s no UI surface (apart from the strange black screen that occasionally appears when debugging a windows store unit test), the UI can’t be displayed and so the call never completes.
- b) The call is not being made on the UI thread and because of that no UI can be shown, and so the call never completes.
Either way, I’m coming to the conclusion that it’s impossible to use Geolocator in a Windows Store unit test because it’s impossible for me to supply consent to my own unit test to access location services! Equally I’m dismayed that such a core component would allow itself to run indefinitely just because UI can’t be shown – that’s just poor.
Anyone had to face this? It really would be kinda good to be able to unit-test code that uses Geo-location!
Here’s what I’ve discovered.
On my desktop (behind a corporate proxy) the Geolocator never completes initialisation. As a result, no app can use my current location for anything – it’s not just limited to unit tests.
As soon as I run the code in the simulator, however, it all works fine.
I believe this is ultimately linked to the fact that my desktop has no location provider installed – since it doesn’t have a GPS receiver. What I find absolutely incredible is that there’s no fallback location provider for devices to use network location or, if there is, that it doesn’t work behind a corporate proxy.
Either way, as it stands, it’s not possible to test/debug any code on my desktop (in a unit test or within the app itself) that requires location from the
Geolocatorclass. Oh, apart from testing my handling for the situation where it never returns!