This blog post was authored by:
- Sharath Raghavendra, Software Developer in Test, Operating Systems Group
- Roaa Sakr, Software Developer in Test, Operating Systems Group
- Cristina del Amo Casado, Principal Program Manager, Operating Systems Group
Geofencing is a new feature in Windows Phone 8.1 that allows an app to define one or more geographical regions for the system to track in the background and then alert the app when the phone enters or exits that area. The API is found in Windows.Devices.Geolocation.Geofencing.
This post provides a few tips and examples for how to test an app that uses geofencing using the Windows Phone Emulator in Visual Studio 2013. While the testing in the Windows Phone emulator doesn’t replace real-world testing, it can facilitate the early development or basic validation of app changes.
Note: Geofencing is also available on Windows 8.1, but this post is specific to Windows Phone. For more on this subject for Windows Store apps, refer to the earlier post, Testing and debugging your geofencing apps.
Windows Phone Emulator
Windows Phone Emulator is a desktop application that emulates a Windows Phone device. It provides a virtualized environment in which you can debug and test Windows Phone apps without a physical device. It also provides an isolated environment for application prototypes. For full details on using the emulator, see Test app features in Windows Phone Emulator and Windows Phone Emulator for Windows Phone 8.
The Location tool in the emulator provides developers the controls to simulate a location by adding pins to mapped locations or by creating a route on the map. It can also simulate various accuracy profiles for a route (urban, rural, suburban) and navigate the route at different speeds (fast, speed limit, biking, walking).
Tips when using the emulator to test geofences
Use Route mode: The new Route mode is the recommended mode in the emulator Location tool to simulate a geofence test.
Run multiple short simulations to avoid power budget impact: The Windows Phone Emulator simulates the geofence tracking in a way very similar to real devices. The Location system tracks positions and relative distances to nearest geofences. The system uses internal algorithms that optimize polling for positions to conserve the phone’s battery while not compromising performance for an app using geofences. So, to conserve battery in real devices, the polling for position to validate geofence conditions is not continuous; it’s adaptive. That means, polling requires a learning period and can be throttled to conserve battery if the user lingers around an area with nearby geofences. In the emulator, the learning period is time-consuming for the developer, so there are a few artifacts to speed it up. These artifacts, however, make the power budget kick in faster. Thus always plan for short simulations no longer than a few minutes.
Initiate the feeding of positions from your route before you inject your geofences: If the initial position fed to the system is really far away from the geofences you later define, the geofence tracking system can throttle back its polling interval to 15 minutes or more. This is because the emulator behaves like a real device, and the device doesn’t expect to suddenly move from New York to Seattle.
Start from a fresh state: Before you start each simulation, turn off and then turn back on the Location tool from the Settings page in the emulator’s Phone UI. This cleans up any initial position state that is too far away, resets the power budget, and so on). It’s especially important to do this, and retry the simulator, whenever a particular test fails to trigger expected geofence events.
To disable Location, go to the emulator’s Start screen by clicking the Windows logo at the bottom. Then click the right arrow below the tiles, pan down append open Settings. In Settings, pan down and click Location, then toggle Location Services off, and then back on.
To make it easier, add the location setting to your quick settings: go to Settings > notifications+actions and then tap one of the quick actions along the top. This gives you a list of settings where you can select location. Once you’ve done this, you can quickly access location when you open the Windows Phone 8.1 notification center by swiping down from the top of the screen.
Match the Speed profile to the nature of the geofences: When selecting a Speed profile and a test route in the Windows Phone Emulator, keep in mind the characteristics of the geofences added: a geofence with a large dwell time or a very small radius may not fire if the test route is simply too fast for the system to react to.
Background tasks for geofencing cannot launch more frequently than every 2 minutes: If you test geofences in the background, the emulator is capable of automatically starting background tasks. Remember though that the background task for a given app will not be launched more often than every 2 minutes (the first geofence notification triggers the background task almost immediately, but then subsequent background tasks for that app are launched every 2 minutes at most).
Default dwell time is 10 seconds: Remember that the default dwell time for a geofence is 10 seconds. You may want to tune this value for your scenarios.
The following examples show developers how to test their geofencing apps using the Windows Phone Emulator.
Example 1: Simulating routes for testing geofences
To show simulations, we created a very simple geofence app for this blog post that enables the user to create a geofence with different parameters and save them. This app has a map control where users can touch to pick the location in which the geofence will be added, and then it allows users to specify other parameters for the geofence (name, radius, type of triggers, etc). The app then waits for the geofence events from the system, which monitors the geofences and notifies the app appropriately. At that point the app alerts the user about the event, requesting feedback on whether the notification happened too soon, too late, or as expected.
In this example we’ll add a grocery store reminder as a test. The Geofence App sample is shown in Figure 1, side-by-side with the emulator’s Location tool. That tool is invoked by clicking the >> control on the bottom of the emulator’s toolbar.
Figure 1: Adding a geofence in the sample app
In this case we’re adding a geofence with a radius of 100 meters and a dwell time of 0, which means the system will notify us as soon as the enter/exit condition is detected.
Figure 2: Select a route and an Accuracy and Speed profile for the simulation
Figure 2 shows how we use the Location tool. First click the drop-down directly under the Location tab and select Route. We use the Route mode to select a test route that simulates the user’s path between two points (e.g. office to home) that we then select on the map. The Location tool will fill in the intermediate points in the route, adapting the points based on the Speed profile. We then select an Accuracy profile that best suits the area of the route selected and we also select the Speed profile – Driving (speed limit), Walking, etc. For this simulation we selected:
- A new generated route that passes through the defined geofence
- A Speed profile = Speed Limit
- An Accuracy profile = Suburban
With the Location tool, developers can apply any of the available Accuracy or Speed profiles to a route they load or create. This is left to the discretion of the developer.
- Selecting a Speed profile for a test route allows speeding up or slowing down the route simulation that characterizes user behavior (driving, walking, and biking). You can use the Speed profile to simulate the behavior of the system for different geofence sizes, according to the speed at which the user is moving.
- The Accuracy profile helps simulate variations in the accuracy of simulated positions for the selected profile. An Urban profile can simulate readily available and accurate WiFi-based positions while satellite positions might suffer from urban canyon effects. Similarly, a Rural profile can simulate poor or unavailable WiFi- and cell-based positions while satellite positions might be accurate. The Location system on the Windows Phone selects positioning technology based on various system considerations, including but not limited to, signal availability, battery considerations, and app requirements.
- Once a test route is selected and an appropriate accuracy and speed profile is set for the route, the developer can play the route and observe their app’s response.
Figure 3: The app gets a geofence entry event
Figure 3 shows the route simulation triggering the Location system to fire a geofence entry event for the grocery store. The app displays a dialog box for the user indicating a personalized message for the event.
The route simulation continues and Figure 4 shows the app receiving a geofence exit event.
Figure 4: The app gets a geofence exit event
The same geofence can be tested with a different route simulation from the one we saw in the previous figures. This is done by selecting a different Speed profile, different Accuracy profile, and even generating a different route. The screenshots in Figures 5 and 6 show how this same geofence can trigger for:
- A new generated route
- A Speed profile = Biking
- An Accuracy profile = Urban
Remember that before starting each simulation, turn off and then turn back on Location to start a fresh simulation without any throttling/power-budgeting impact from previous simulations.
Figure 5 shows how to select another test route with a different Accuracy and Speed profile (Accuracy profile – Urban. Speed profile – Biking). Figure 6 shows the app receiving a geofence entry event for the new route being simulated.
Figure 5: A test route to simulate a biking path
Figure 6: App gets a geofence entry event while in the biking simulation
Example 2: Using Live mode and Pin mode for testing geofences
While the Route mode is most helpful in simulating different scenarios and is best suited for testing geofencing, you can also use Live mode and Pin mode for testing.
Figure 7 shows how you set a position using Live mode. This position is outside the geofence the sample app has added. Figure 8 shows how you set a test position in Live mode that’s inside the geofence. The app receives the triggered geofence entry event.
Figure 7: Live mode – simulated location is outside geofence
Figure 8: Live mode – simulated location is now inside the geofence; app gets geofence entry event
Figure 9 shows how to use the Pin mode for testing. The app receives the geofence entry event upon simulating a set of location pins.
Figure 9: Pin mode – app receives geofence entry event upon simulation of set of pins
The emulator enables the basic validation of geofencing in both foreground mode or via background task notification. When using the Location tool for this validation, use the Route mode for a more realistic experience. Due to some limitations in the emulator, we added artificial position polling that can make the geofence track for power budgeting. Because of this, limit each test period to just 2-3 minutes; if you want to test for longer periods, disable and re-enable location through settings to reset the power budget. After thisyou can initiate a new round of tests.