Mobile Zone is brought to you in partnership with:

Den is a DZone Zone Leader and has posted 460 posts at DZone. You can read more from them at their website. View Full User Profile

Windows Phone 7 - Testing applications both on the device and the emulator

08.19.2010
| 10478 views |
  • submit to reddit

Windows Phone 7 devices are finally in the wild. Developers around the US are able to use real hardware to test their applications in real-world conditions. However there is still a large percentage of developers who are only using the emulator. The question I was thinking about is, how would it be possible to find out whether the application is running inside the emulator or not?

The solution to this is the DeviceType enumeration, exposed by Environment.DeviceType property.

For example, if I would like to use the vibration capabilities of the phone, I can check whether I run on an emulator or not and choose the proper action accordingly.

if (Environment.DeviceType == DeviceType.Device)
{
VibrateController controller = VibrateController.Default;
controller.Start(new System.TimeSpan(0,0,10));
}
else
{
Debug.WriteLine("Vibration is disabled on the emulator.");
}

This is also a way to work with the device sensors, like the accelerometer. For example, if you know the application will be running on the emulator, then you might want to simulate accelerometer data. Otherwise, you can use real data. So, to say, if there is a set of properties that will represent the accelerometer movement values:

double X { get; set; }
double Y { get; set; }
double Z { get; set; }

You can set these properties accordingly:

private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
if (Environment.DeviceType == DeviceType.Device)
{
Accelerometer accelerometer = new Accelerometer();
accelerometer.ReadingChanged += new System.EventHandler<AccelerometerReadingEventArgs>(accelerometer_ReadingChanged);
}
else
{
Debug.WriteLine("Accelerometer data simulated by the mouse movement.");
this.MouseMove += new MouseEventHandler(MainPage_MouseMove);
Z = 0;
}
}

void MainPage_MouseMove(object sender, MouseEventArgs e)
{
X = e.GetPosition(this).X;
Y = e.GetPosition(this).Y;
}

void accelerometer_ReadingChanged(object sender, AccelerometerReadingEventArgs e)
{
X = e.X;
Y = e.Y;
Z = e.Z;
}

Yet another way to use this functionality is to limit the application to a demo version. For example, when the application runs inside the emulator, chances are it is used by the developer, therefore the demo flag could be set to false – you want access to full functionality. When the application runs on the device, the demo flag can be set to true, so that the application runs in limited mode.

The main drawback of this method is that practically you are writing chunks of code that are not used. When you are running on an actual device, extra space will be used for code that will never be triggered - the one that is only used by the emulator. Therefore my advice would be not to use this verification unless you really need it, like in the example above. Otherwise, I’d recommend to use it for testing purposes only.