Monday, March 31, 2008

System.Windows.Forms.Form.Owner

Although this little chestnut has been in the CF since 2.0 (Nov 2005) I'd thought I'd mention it here as some people still don't know about it.

If you've written Windows applications for the desktop you'd probebly used this property. On devices what you used to have to do was to write a 'hack' something similar to the following when calling sub-forms from within your application:
using (Form1 form1 = new Form1())
{
this.Text = "";
string str = this.Text;
form1.ShowDialog();
this.Text = str;
}
NativeMethods.SetForegroundWindow(hwnd);
Quite ugly huh! You had to set the Text property for the form to blank before calling a sub-form so that you wouldn't get two entries visible in the memory applet under Settings. P/Invoking SetForegroundWindow was also required to ensure your app would come back to the foreground after closing the sub-form if you had viewed other windows during the lifecycle of the sub-form.

Now all you have to code is the following:
using (Form1 form1 = new Form1())
{
form1.Owner = this;
form1.ShowDialog();
}
Lovely!

Wednesday, March 19, 2008

XSLT Profiler for Visual Studio 2008 CTP

I wrote a blog recently regarding a Biztalk XSLT mapper tool named Xselerator which is a tool that allows you to debug XSLT and run translations. I mentioned at the time Visual Studio didn't support this, but now it does with this plug-in, but only on VS 2008 Team Suite with the performance tools feature installed.

Get it from here: http://www.microsoft.com/downloads/details.aspx?FamilyId=F43314ED-95B7-435F-95C5-0E326E64543B&displaylang=en

Monday, March 17, 2008

Rolling back Visual Studio 2008 projects to Visual Studio 2005

You might ask, why would I want to roll back my VS 2008 projects to VS 2005? Well if like me you were hasty in upgrading your solution and didn't relise or forgot that CF 1.0 projects are not supported under VS 2008 and upgrading will upgrade CF 1.0 to CF 2.0, then you have every reason to do so.

We still have CF 1.0 projects or project which is used for an autorun app which gets shipped on a memory card on PPC 2003 SE and later because CF 1.0 is installed in ROM from PPC 2003 SE. Doing this enables our application to self-install on cold-boots or power failures.

Luckily it isn't too difficult rolling back your projects (although you should use some kind of source control system to roll back to). If you don't a source control system or just curious how to do it, it is dead easy.

Simply open up the project and change the ToolsVersion attribute to 2.0. Change the TargetFrameworkVersion element to 2.0. You can get rid of the Import element which specifies CF 3.5. IE:
<import condition="'$(TargetFrameworkVersion)' == 'v3.5'"
project="$(MSBuildBinPath)\Microsoft.CompactFramework.CSharp.targets"></import>

Ensure a CF 1.0 or CF 2.0 exists. If you have a desktop project you might have a
<requiredtargetframework>3.5</requiredtargetframework>
for some of the framework assemblies, if rolling back, these can be deleted.

Rolling back the solution couldn't be easier. At the header of your solution file (.sln) you'll have the following:

Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008

Change the above to:

Microsoft Visual Studio Solution File, Format Version 9.00
# Visual Studio 2005

That's it!

Friday, March 14, 2008

Device Unit Testing using Visual Studio 2008 Team System

With the release of Visual Studio 2008 device unit testing or in fact desktop unit testing is now built into the IDE which makes unit testing alot easier than before. Note device unit testing is only supported on Visual Studio Team System 2008 with Test Edition or Team Suite. See here for a Visual Studio product comparison.

If you would like to learn more about what agile unit testing is see here.

With Visual Studio 2005 there were tools such as Test Driven.net, NUnit, MBUnit typically were used and still are today, in fact we still use NUnit mainly because we like it, it works and we have hundreds of tests written using it. There are differences with the above tools, we chose NUnit because it was ported from JUnit which worked also.

This article will cover how to use unit testing on a Windows Mobile device and show how simple it is. In fact I am very fond of it as it does in fact do alot of work for you that NUnit doesn't. Also the difference with the VS unit testing over NUnit is that in VS the unit testing is integrated in the IDE.

There are two ways to create unit tests in VS 2008, one is to create tests from production code or to create them by hand. Of course it is less work to create the tests from production code so this is what I'll talk about in this article.

1. Start off with a simple device class library. In this example I have created a simple calculator which adds two numbers together. The code looks like the following:
namespace Calculator
{
public class SimpleCalculator
{
public decimal Add(decimal num1, decimal num2)
{
return num1 + num2;
}
}
}
As you can see, it is very simple.

2. Right click the source for which you would like to create the test project, then select "Create Unit Tests..." option.



3. Select the method for which you would like to create the test.



4. Clicking on "Settings..." button will allow you to configure how the unit test wizard will create your test.

Most of the options are self explanatory. I always change the name to the name of the class coupled with "Fixture" ie in this case "SimpleCalculatorFixture". It stems from NUnit days.

The only option that needs to be clarified below is the "Mark all test results Inconclusive by default". This option forces the test to fail when run because it is not implemented. Of course it is good practice to honor this setting.



5. Clicking OK to the Test Generation Settings dialog then click OK again on the Create Unit Tests dialog will prompt you to enter a name for your new test project, enter a name and click OK.

6. After completing the above Visual Studio creates the test project and some test methods. quite neat when you think in the past you'd have to do all this yourself.

In the above example the test code VS generated looks like the following:
using Calculator;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;

namespace CalculatorTest
{

[TestClass()]
public class SimpleCalculatorTest
{
private TestContext testContextInstance;

public TestContext TestContext
{
get
{
return testContextInstance;
}
set
{
testContextInstance = value;
}
}

#region Additional test attributes
//
//You can use the following additional attributes as you write your tests:
//
//Use ClassInitialize to run code before running the first test in the class
//[ClassInitialize()]
//public static void MyClassInitialize(TestContext testContext)
//{
//}
//
//Use ClassCleanup to run code after all tests in a class have run
//[ClassCleanup()]
//public static void MyClassCleanup()
//{
//}
//
//Use TestInitialize to run code before running each test
//[TestInitialize()]
//public void MyTestInitialize()
//{
//}
//
//Use TestCleanup to run code after each test has run
//[TestCleanup()]
//public void MyTestCleanup()
//{
//}
//
#endregion


///
///A test for Add
///

[TestMethod()]
public void AddTest()
{
SimpleCalculator target = new SimpleCalculator();
Decimal num1 = new Decimal();
Decimal num2 = new Decimal();
Decimal expected = new Decimal();
Decimal actual;
actual = target.Add(num1, num2);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
}
}

As you can see from the above test code, it is very similar to how things are done using NUnit. Notice the similar attributes used to mark methods. The most notable one being the TestClass attribute the NUnit equivilent is TestFixture. These attributes and all testing methods etc can be found in a simple assembly named: Microsoft.VisualStudio.TestTools.UnitTesting.dll. You don't need to add this to your test project because Visual Studio has already done this for you. It doesn't get any easier than this!!

Many of the methods VS has added for us such as MyClassInitialize, MyClassCleanup etc are commented out they have been added for ease of implementation if required.

7. Now we have everything in place, lets see what happens when we run the test, bearing in mind we haven't written any tests yet, we have simply let VS do its job and create a test project for us.
/// 
///A test for Add
///

[TestMethod()]
public void AddTest()
{
SimpleCalculator target = new SimpleCalculator();
Decimal num1 = new Decimal();
Decimal num2 = new Decimal();
Decimal expected = new Decimal();
Decimal actual;
actual = target.Add(num1, num2);
Assert.AreEqual(expected, actual);
Assert.Inconclusive("Verify the correctness of this test method.");
}
Now running the test will execute the above, of course the line Assert.AreEqual will fail because we have already written our production code which adds the two numbers together so the result of this method call will not be equal. Visual Studio has guessed here what the likely result might be, clever but not quite clever enough!

So running the above test results in failure on my machine. I am attemping to run the test on an emulator. The error message I am getting is:

The test adapter ('Microsoft.VisualStudio.TestTools.TestTypes.Unit.UnitTestAdapter, Microsoft.VisualStudio.QualityTools.Tips.UnitTest.Adapter, Version=9.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a') required to execute this test could not be loaded. Check that the test adapter is installed properly. Exception of type 'Microsoft.VisualStudio.SmartDevice.TestHostAdapter.DeviceAgent.NetCFNotInstalledException' was thrown.
I wrote a blog regarding this error a while back here.

This basically means .NET CF 3.5 is not installed and VS doesn't deploy .NET CF 3.5 before executing the test if required even though the option under the test project properties Devices tab "Deploy the latest version of the .NET Compact Framework (including service packs)" is set.




You can simply deploy a Windows Forms application with the above option set and VS will deploy CF 3.5 just as it's always done, if you don't have a Windows Forms application, deploy the CAB file to the device/emulator manually then run it. After this the tests should execute.

8. Now I have fixed the above problem, lets try running the test again. This time I get the following results in the new Test Results window.



From the above Test Results window it is difficult to read the error as to why the test failed. Double clicking the error row loads the properties window for this particular test which gives us some more information as to why the test failed.




9. As we can see from the error message above the Asset.Inconclusive failed. If you remember when VS creates a test template, it inserts the Assert.Inconclusive which will fail when run. So to fix this problem, we simply change the test to the following:
        /// 
///A test for Add
///

[TestMethod()]
public void AddTest()
{
SimpleCalculator target = new SimpleCalculator();
Decimal num1 = new Decimal();
Decimal num2 = new Decimal();
Decimal expected = new Decimal();
Decimal actual;
actual = target.Add(num1, num2);
Assert.AreEqual(expected, actual);
}
We have simply removed the call to the static method Inconclusive.

Now if we run the above test we get the following:



Suprisingly, the test passes. It's not really that suprising because if you look at the AddTest method above, you'll notice that VS has created an instance of two decimal objects which both will be 0 and declared a returning type which isn't instanciated, but the AddTest method creates it and passes it back to the test method. So we are performing this calculation: 0 + 0. Of course the result will be 0 which is why the test passes.

Explore the Assert class, there are many methods to use against your tests much like NUnit. It seems the Testing framework in VS 2008 was built on NUnit and I like it alot!

Friday, March 07, 2008

Adding a block of XML to an existing XmlDocument object

Sometimes you have the need to add a block of XML (string) to an existing XmlDocument (DOM object).

Luckily you don't have to create nodes then import them etc, instead the class XmlDocumentFragment comes to our rescue which allows us to write much cleaner code in this senario - and we all love clean code!

Take the following peice of XML file named myXml.xml:
<Sys>
<Header\>
<Body\>
</Sys>
Now take the following code:
XmlDocument doc = new XmlDocument();
doc.Load("myXml.xml");

Now take the following XML block:
<Start id="7672-2322-2322-3324"/>
Now what if I wanted to add the above block of XML into the Body element of the doc XmlDocument object but I didn't have control over the schema -or maybe I don't care what the schema looks like or don't even know what it looks like, all I want to do is insert it into the body. I could use the CreateNode() method, but this is ugly and I have to know the schema, also this would add a maintance overhead as everytime the schema is changed, I'd have to change my code too.

Instead the XmlDocumentFragment class can be used. Take the following code:
XmlDocument doc = new XmlDocument();
string s = "<Start id="7672-2322-2322-3324"/>";
doc.Load("myXml.xml");
XmlDocumentFragment fragment = doc.CreateDocumentFragment();
fragment.InnerXml = s;
//Use GetElementsByTagName instead of XPath incase the schema changes.
XmlNodeList body = doc.GetElementsByTagName("Body");
if (body.Count > 0)
body[0].AppendChild(fragment);
That is all there is too it. The output of the above will now look like the following:
<Sys>
<Header\>
<Body>
<Start id="7672-2322-2322-3324"/>
</Body>
</Sys>