Sunday, May 11, 2008

Windows Mobile Unit Tests setting the time

As if you didn't know, I am a fan of TDD (Test Driven Development). I try to work in a TDD manner as much as possible.
Unit testing for Windows Mobile applications has always been somewhat challenging. Visual Studio 2008 (professional and above) allows me to work in a TDD manner as I build software for the Windows Mobile platform.
I have been working on some software that needs the time to be changed as part of the tests. This might not be as simple as you think, but it's not hard as long as you know to set the local time not the system time on the device.

private SYSTEMTIME SetDateTime(DateTime dateTime)
{
SYSTEMTIME st = new SYSTEMTIME();
st.year = (short)dateTime.Year;
st.month = (short)dateTime.Month;
st.day = (short)dateTime.Day;
st.hour = (short)dateTime.Hour;
st.minute = (short)dateTime.Minute;
st.second = (short)dateTime.Second;
st.milliseconds = (short)dateTime.Millisecond;

return st;
}

public struct SYSTEMTIME
{
public short year;
public short month;
public short dayOfWeek;
public short day;
public short hour;
public short minute;
public short second;
public short milliseconds;
}

[DllImport("coredll.dll", SetLastError=true)]
static extern bool SetLocalTime(ref SYSTEMTIME time);

[TestMethod()]
public void HourIsOne()
{
DateTime savedDT = DateTime.Now;
SYSTEMTIME sysTime = SetDateTime(savedDT);
sysTime.hour = 1;

bool setSuccess = SetLocalTime(ref sysTime);
Assert.IsTrue(setSuccess, "Failed to SetLocalTime");
Assert.AreEqual(1, DateTime.Now.Hour,
"Hour should be 1 after setting it");

sysTime = SetDateTime(savedDT);
SetLocalTime(ref sysTime);
}

2 comments:

Teresa Halminton said...

All the basic information about every celebrity is available now on the largest database of celebrity heights where you can find all the information and heights of a singer, actor, businessman...

خرید و فروش سیم کارت ایرانسل said...

Thanks for sharing your information