Mister Goodcat
Purrfect software since 1882

Unit test adventures - part 1

Some days ago I added a feature to an existing application, and when I was testing it, I felt that some calculations in the already existing parts of the software were wrong. After digging through the code I found that an external mathematical library is used, which was developed by a colleague some time ago. Due to its nature (being a pretty basic library), it was well-resourced with unit tests, and a short test run showed all of them succeeding. 

I sat down and did some old-school pen and paper calculations, and the results clearly showed that the algorithm in question was way off. I went to see the colleague responsible for the library and it turned out that he found a groundbreaking new technique of creating unit tests that saved him quite some time during the development. Apparently, you just need to follow these three simple rules: 1.) Write a stub for what you want to test. 2.) Debug the (failing) test and... 3.) Put the result of that test run as expected results into the asserts...

Very groundbreaking indeed.



  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Silverlight 3 GDR 1 (3.0.40723.0)

This evening, I was presented with an update to Silverlight by Microsoft Update ("Update for Microsoft Silverlight (KB970363)"). The included text sounded more than a general feature description of Silverlight 3 than a change log:

This update to Silverlight improves security and reliability, enhances graphical performance and creative effects, expands the range of supported media formats, broadens support for rich internet applications, and enables Silverlight applications to run outside of the browser.

The provided link also did not reveal any information on what really has changed by this silent release. After looking around the web (there do not seem to be any release notes), it seems like the only thing that has changed is a buffering issue with wmv streams. See the answer from Jimmy Lewis in the Silverlight forums as well as an article at ars technica.

I wish Microsoft would also provide official change logs for every release.

Btw.: if you are a Silverlight developer, you have to install the developer edition instead of the normal runtime. You can get the 40723 (GDR1) release here: http://go2.microsoft.com/fwlink/?LinkID=150228 - you can check if you're using the latest version by following this link (restart your browser after updating first).

-

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Breaking changes in Silverlight's navigation framework

Today I was working with a small Silverlight application that is using the new navigation feature. Basically I adapted what Tim Heuer (thank you for your great videos and posts!) shows in his tutorial here. Apparently that sample has not been updated for the final version of Silverlight 3. When you watch that video and download the sample code, you'll notice that although it will compile under Silverlight 3 RTM, running it will result in a white screen. In Firefox that is -- curiously enough IE partly shows the sample, but it's not working. Unfortunately, you don't receive any runtime exceptions, even with the debugger attached. In the error console however, the following Java Script exception is logged:


Error: Unhandled Error in Silverlight Application
Code: 4004 
Category: ManagedRuntimeError     
Message: System.ArgumentException: Navigation is only supported to relative URIs that are fragments, or begin with '/', or which contain ';component/'.
Parameter name: uri
[...]


So the first thing is to change all the URIs used in the sample and add a preceding slash to them. In particular, you need to change the Tag properties of the HyperlinkButtons in MainPage.xaml, the parameter of the call to Navigations.Navigate(...) in CustomerList.xaml.cs, and of course the mappings defined in the resources of App.xaml (hint: you could also use the NavigateUri property of the hyperlink buttons instead of Tim's approach, which makes the sample even easier).

<navcore:UriMapper x:Key="uriMapper">
    <navcore:UriMapping Uri="/Home" MappedUri="/Views/HomePage.xaml" />
    <navcore:UriMapping Uri="/About-Us" MappedUri="/Views/AboutPage.xaml" />
    <navcore:UriMapping Uri="/Customers" MappedUri="/Views/CustomerList.xaml" />
    <navcore:UriMapping Uri="/Customer/{id}"
        MappedUri
="/Views/CustomerDetail.xaml?id={id}" />
</
navcore:UriMapper>

(Precede all occurrences of the URIs with a slash, exemplarily shown for the UriMapper here)

After that, you'll receive a different Java Script exception:

Error: Unhandled Error in Silverlight Application No XAML was found at the location '/Home'.
[...]

The reason for that is a bit more tricky to find out. In the beta version of Silverlight 3, it was sufficient to provide an UriMapper with a certain key (e.g. in the App's resources), and that magically was used throughout the application. Silverlight 3 RTM requires the UriMapper property of a navigation frame to be set explicitly. You can bind that property (in MainPage.xaml) to the static resource defined in App.xaml:

<navigation:Frame x:Name="MainFrame" UriMapper="{StaticResource uriMapper}"

Now the application basically is working: you can navigate through the pages, and all content is displayed correctly. You can also use the corresponding methods to go back and forward programmatically. However, you'll notice that the navigation buttons of the browser won't work. The address bar won't be updated also, and neither will deep linking work. Why is that? Well, it took me four hours (shame on me?) to find out why, and that evil little detail is the reason I've decided to write this post after all.

I started reading through the breaking changes document, but I couldn't find anything helpful regarding my problem. I played around with some of the Frame's properties, like "JournalOwnership", which sounded promising but didn't change anything either. Of course I tried searching the web, but apparently few or no people ran into this issue. Then I tried a different approach and built a new Silverlight navigation application (Visual Studio has this nice template...) from scratch. Of course it worked like a charm. And I couldn't see anything that was done differently from my own application. But this template already is pretty sophisticated, using styling and all, so I began to take out all the unneeded stuff until nothing more than maybe 20 lines of code were left. Basically two hyperlink buttons, the frame, and two pages, consisting of nothing more than text blocks. And still it worked, whereas my own, equally pared-down application did not. In desperation, I started hosting the application in IIS (no difference), and then finally started the Silverlight project directly (instead of using the development server) - and BAM! It worked.

This gave me the final push I needed. There must be something wrong with the generated page, because when you start the Silverlight application directly, it dynamically generates a temporary HTML page, which obviously was different in some detail from the HTML page of the web project in the sample code. And then I saw it: the IFrame used for the history had an id attribute set, whereas the HTML page from the sample had not. I added

<iframe id="_sl_historyFrame" ...

to the IFrame tag and the sample worked instantly...

This is one of the bad examples I'll remember for the next years, where a small hint in a release notes document or similar would have saved me hours of frustration. I also hope Tim will update his tutorial soon because probably others will run into this issue too, especially because his sample is not marked as outdated or for SL3 beta only.




  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Taming the treeview

It seemed like a simple task. In one of our Silverlight applications, we're using the tree view control from the Silverlight Toolkit. A colleague of mine was instructed to remove all of the hover and selection effects from the tree view items. After struggling with what seemed like a five minute job for more than two hours, he gave up and came to me for advice. Here's what the default design of the tree view looks like, with one of the items selected:

Tree view selection

When you want to change parts of a control's default behavior, it's always a good idea to use Expression Blend instead of Visual Studio, because it gives you the possibility to copy the existing templates and use them to work from there. In Visual Studio, you would have to build a new template from scratch, which often is tedious work, especially for complex controls. In Expression Blend, you simply select the control and the according menu entry:

 

You have to add a key that identifies your style so you can apply it to controls, and you also have to choose where you want to store the new style.

Once you've created the new tree view item style, you can edit its template:

In the current case, after inspecting the available states you'll find out that there are two rectangles ('select' and 'inactive') in this template, which are made visible in the 'Selected' (and 'SelectedInactive', respectively) state by manipulating their opacity property. We just wanted to remove those effects completely, so I simply deleted both rectangles. If you wanted to adjust the colors or other parts of the effects' appearance, that's the place where you would do that too.

Well, after applying the new ItemContainerStyle to my sample data, I was surprised to find out that not all of the effects were gone. In fact, hovering an item with the mouse still brought up a light blue rectangle around the item's content. I checked again with the template and realized it didn't even have the involved states defined. So where does this additional highlighting come from?

The answer is that the header component within the item template is build from child components as well. Simply select it and choose to edit its template:

You can see that the header is built from another rectangle (with the suspicious name "hover") and the content itself. The hover rectangle is made visible depending on the current state.

After removing that rectangle, we finally had a tree view control without any effects for selection and hovering at all (but with all the other functionality, like expanding and collapsing items, left intact).

When I look around the net, I see a lot of people struggle with finding the locations where properties like these are manipulated. Even with the help of Expression Blend it can be confusing, and simple tasks like "how do I turn off x" or "how do I change the color of y" can turn into amazingly time-consuming nightmares. Once you get used to the concept however, things will get better every day :). Hopefully I could give you a small push into the right direction for your next tree view item emergency...



  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Attempted self-introduction

Programmers have no easy lifes. It's amazing how many people cannot get typical stereotypes out of their minds: they think of us as confused, long-haired, disheveled, overweight guys, somehow nuts or at least from a different world, sitting in front of their keyboards, a bottle of Coke to the left, cold pizza to the right, hacking mysterious combinations of text into a computer until sunrise. Virgins in their mid-30s, still living at mama's, and due to the lack of sun over the years turned paler than MJ himself. People who cannot clothe a normal thought in words because all the abstract work has fried their brains. Talking - if at all - only about compiler switches, the number 42 and telling jokes which cannot be understood by 99,9% of the human race. Persons that get more excited about Lost than solutions to global warming and wish more for an even bigger monitor than world peace. Spending all their free time on computer games, able to recite from 400 episodes of The Simpsons and only leaving the house for Star Trek conventions.


I am of course totally different. I hate Star Trek.


:]


The truth is, I am a computer enthusiast, ever have been that way since my father brought a C64 into our home back when I was a kid. I enjoy my work as software developer so much that I'm one of the lucky guys who actually think work is fun... and because I've learned a lot from people all around the world who kindly share their experiences online, I decided to give something back to the community. Meow!



  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati