Static Analysis for finding I18N Issues

Static analysis tools are useful for quickly identifying issues with your code. Recently, my team has been evaluating Globalyzer, a static analysis tool for finding software internationalization issues. Globalyzer will find such issues as hard coded strings, or hard coded date/time formats. Our aim is to move the discovery of I18N issues from the localization QA phase, upstream to the development phase.

My first impressions are good. Globalyzer is quick to setup, easy to use, and scans code unbelievably quickly. I’ve scanned a project with 350,000 lines of code in less than a minute.

The key to an effective rollout of Globalyzer is developing rule sets per product. Rule sets essentially define the scanning criteria for a code base. For example, for a C++ project, we would start with the built in rule set for C++. Then after an initial scan, we analyze the results, and we may want to update the rule set. For example, we may want to configure the rule set to ignore calls to any logging functions, since we don’t localize log messages.

I’m hoping that we get the following usage from Globalyzer:

  • Globalyzer should help us get involved earlier in the development cycle.
  • Lots has been written about Agile localization. Globalyzer should help us certify sprints as I18N ready, for later localization.
  • We should find any serious I18N issues early in the project – thus reducing fire-fighting later on.

Longer term, I’d like for us to look at integrating Globalyzer with our build systems. I’ll be posting here periodically on our progress, and my experiences with using static analysis tools to find code level internationalization issues.

Automate your Android App Testing Using Robotium

I like that it’s the time of the year when I actually have some time to invest in blogging here. Updates have been pretty sporadic this year (another item to add to my list of resolutions once 2013 hits in a few days).

In this post, I’d like to talk a little about Robotium, and how you can use it to automate the QA of your Android applications. Mobile applications are perfect for Quality Assurance automation – they are usually small, have limited functionality when compared with desktop applications, and are rarely complex in terms of testing steps. Also, if your a solo mobile developer, you may not have the time (or money) to invest in proper quality assurance of your applications.

We’ve been using a Robotium based solution written in Java at my day job for a while, to validate the quality of our localized product builds, both as a unit testing tool and as an actual test automation tool used by our QA team.

One of the main reasons why we chose to invest time in Robotium is the fact that it offers flexibility in terms of how it recognizes objects within your app (e.g. like it would have to in order to click a button). This is especially important for us, as we want to develop a solution once, and have it support multiple language versions of our application. We also looked at MonkeyRunner, which ships with the Android SDK. MonkeyRunner uses Jython (a Python implementation in Java) scripts to walk through applications.

I prefer Robotium as MonkeyRunner lacks the tight UI integration offered by Robotium, (although MonkeyRunner is much easier to setup and run).

When developing automated test scripts using Robotium, we can use the text from a UI object to identify that object at run-time, for example the label on a button. What we do is compile the localized resources from all our languages with our test application, so if we switch our device language to German for example, the resources from the ‘values-de’ folder are loaded by our test application, and thus we can add new languages to our automation solution simply by re-packaging the APK file containing our test automation scripts, and it will run on any of our supported locales without code modifications.

One of the disadvantages of this approach, (and almost all UI based test automation solutions to be fair), is that the maintenance may be high – for example, you will probably lose 90% of your code in a UI refresh.

Creating a Simple Robotium Automated Test Case

  • First off, download the latest version of Robotium, (3.6 at the time of writing).
  • Now simply create a new Android application in the IDE of your choice, I prefer NetBeans due to familiarity, but I know most people like Eclipse for Android development.
  • Add the Robotium JAR as a reference in your project.

For the purpose of this example, let’s say we want to click the ‘Cancel’ button in the below screen capture. As I mentioned above, our Robotium code uses the string ID’s to find objects (based on the current device language, we will look for the value of that string ID for that language if available, and use that to look for the object on the current screen).

MMS Image

Code (Finally!)

package com.jc.robotium

import android.app.Activity;
import android.test.ActivityInstrumentationTestCase2;
import com.jayway.android.robotium.solo.Solo;
import android.content.res.Resources;
import android.content.Context;
import java.util.Locale;

public class RobotiumExample extends ActivityInstrumentationTestCase2 // Provides functional testing of an activity
{
    private static final String TARGET_PACKAGE_ID = "com.yourcompany.yourapp";
    private static final String LAUNCHER_ACTIVITY_FULL_CLASSNAME = "com.yourcompany.yourapp.Activities.MainActivity";
    private static Class<?> launcherActivityClass;
   
    private Solo solo;
    private Activity activity;
    private Resources res;
    private Context context;
   
    // This will launch the application specified above on the device
    static
    {
        try
        {
            launcherActivityClass = Class.forName(LAUNCHER_ACTIVITY_FULL_CLASSNAME);
        }
        catch(ClassNotFoundException e)
        {
            throw new RuntimeException(e);
        }
    }
   
    public RobotiumExample() throws ClassNotFoundException
    {
        super(TARGET_PACKAGE_ID, launcherActivityClass);
    }
   
    @Override
    protected void setUp() throws Exception
    {
        activity = getActivity();
        solo = new Solo(getInstrumentation(), activity);
        context = getInstrumentation().getContext();
        res = context.getResources();
    }
   
    public void testUI() throws Throwable
    {
        // ....
       
    // Robotium code begins here
       
    // Wait for the 'Cancel' button
    solo.waitForText(res.getString(R.string.cancel));  
       
    // We may want to take a screen capture...
    solo.takeScreenshot();
       
    // Click the cancel button
    solo.clickOnText(res.getString(R.string.cancel));
   
    // ....

    }
   
    @Override
    protected void tearDown() throws Exception
    {
        try
        {
            solo.finalize();
        }
        catch(Throwable e)
        {
            // Catch this
        }
        getActivity().finish();
        super.tearDown();
    }

Notice a few things above:

  • We are extending the ActivityInstrumentationTestCase2 class, which provides us with the ability to run test methods on the UI thread.
  • The method that contains your test logic must be prefixed with ‘test’, notice mine is called testUI().
  • We could have looked for the ‘Cancel’ button by passing the text ‘Cancel’ to the waitForText() and clickOnText() methods, but that would only work on the English version of our product. Instead we use the string ID, and look up the value of that string ID based on the current device locale.
  • If you use the ‘takeScreenshot()’ method I have shown an example of above, screen captures will be saved in ‘/sdcard/Robotium-Screenshots/’ on your test device.

Running the Test

Compile the APK containing your test automation script and install it on your test device, along with the APK under test. One ‘gotcha’ here with Robotium is that the APK under test and the APK containing the test logic must be signed with the same certificate. This may not be the case if your APK under test comes out of a build system of some sort. You can use resign.jar to re-sign your APK under test with the same signature as your APK containing the test logic (by running it on the same machine on which you compiled the test APK).

Once both are installed on a test device, we can launch the test from a command prompt via adb:

adb shell am instrument -e class com.jc.robotium.RobotiumExample -w com.jc.robotium/android.test.InstrumentationTestRunner

Conclusion

Robotium is a good test framework for getting automation for Android applications up and running very quickly. It has an active community and updates are released regularly, the latest version (3.6 at time of writing), also seems to be a lot more stable than previous versions.

On the flip side, since we are relying on text resources (which may change frequently), the maintenance on Robotium based automation solutions can be high if your UI changes a lot, which is probably a high possibility for a mobile application.

If your looking for an automation framework for your Android application that you can get up and running quickly, and use to run scripts across an Android application supported in many languages, I would recommend Robotium, and will be keeping a close eye on it as it develops further.

Basic Software i18n Check List

Part of something my team is currently working on involves defining a standard approach to assessing the world-readiness of a piece of software, be it a brand new product or a legacy product that has existed for a long time.

If your codebase is not correctly internationalized, you will face many problems if you ever want to localize your application for another market.

Most of these are general items that could apply to any product on any platform, be a it a website or a mobile application.

Here’s my first draft of my list. This may help you if you are planning of localizing your application to sell in other markets.

Strings

  • Have all strings been externalized?
  • Have all strings been approved by someone who is a native English speaker?
  • Is there overuse of any abbreviations or acronyms, e.g. ‘App Info’?
  • Is there anywhere that you are concatenating strings together?
  • Punctuation is text too! Are you using any hard-coded punctuation marks (.,!?) these need to be externalized?
  • Have you identified all trademarks, tokens and other ‘special’ elements in your text and communicated to localisation that these shouldn’t be translated?
  • For mobile applications, is your text short enough to fit the potential small screen size of mobile devices? Consider string length expands generally 20% – 30% after translation. Some phrases expand by a lot more than 30%. Abbreviations and acronyms are the worst offenders here.
  • Try to keep resources that shouldn’t be translated separate from those that should.
  • Don’t embed HTML mark up in string resources.

Source Files

  • Source files are a standard file format – no need to invent new resource file formats. A standard resource file format will exist for the platform for which you are developing.
  • Source files are UNICODE.
  • You need to be able to generate your localizable file set in one step, it’s ideally handled by the build process.

General UI

  • Can input fields accept accented characters?
  • Can input fields accept double byte characters?
  • Is there room for buttons to expand if necessary?
  • Are there any UI elements like check boxes or radio buttons that are placed across the screen? It is better to place these vertically.
  • Try to put labels for controls above the control, rather than beside it.

Web Applications

  • Is server set up to send UTF-8 character set?
  • Are all buttons resizable?
  • Are there any mouse-over buttons with text in them?
  • Are other elements on the screen able to expand in size?
  • Are links to help and other user documentation appropriately localized?
  • For HTML based UI, never use absolute positioning.
  • NEVER use HTML tables to achieve layout.
  • Try to avoid using fixed widths with HTML layouts.
  • Don’t use the ‘align’ property to align items left or right.
  • Externalize CSS to a separate file – never use inline CSS.

Images

  • Is there text in any image?
  • Are there any images that depict hand, body or facial gestures? e.g, the thumbs up gesture is offensive in some cultures.
  • Are there any icons or images that are very specific to one region? e.g. an American type mailbox indicating an email.

SQL

  • Use the nvarchar type to store all text.
  • Can all insert and update statements handle unexpected apostrophes? (O’Sullivan, d’accord for example)?
  • Can all insert and update statements handle accented and double byte characters?
  • For MySql, ensure it was installed with UTF-8 as the default character set.

Numbers & Dates

  • Do any parts of your application format numbers using a thousand separator or a decimal point? If so the formatting string must be read from the OS.
  • Do any parts of your application format currencies? If so the formatting string must be read from the OS.
  • Does changing the application locale change all numbers appropriately?
  • Are all time and date formats read from the OS based on locale?
  • Does changing the application locale change all dates appropriately?

Regular Expressions

  • Do any regular expressions use the construct [a-zA-Z]? These are not safe from an i18n point of view.
  • Never use a regular expression to find a price, as the currency will be different for different languages.

Collation/Sorting

  • Do any parts of your application sort alphabetically? If so then they need to be tested for i18n awareness.

SQL – Alter a database user’s password via a query

Here’s some handy SQL to alter the login of an existing user via a query. I recently lost my login to one of the databases I use for a project (OK, I didn’t lose it, I forgot it). Luckily I had another account I could login to the database with via SQL Server Management Studio, and execute the below query to reset the password of the one that I forgot.

This got me out of a bind:

GO
ALTER LOGIN [login_name] WITH DEFAULT_DATABASE=[database_name]
GO
USE [database_name]
GO
ALTER LOGIN [login_name] WITH PASSWORD=N'new_password' MUST_CHANGE
GO

Interacting with Microsoft Excel from C# using EPPlus – Part 1

I’ve been working on moving the reporting functionality of an existing application from HTML to Microsoft Excel format. I find HTML fine for creating simple reports or sometimes log files from C# applications, but these reports were churning out at around 50MB, which Internet Explorer was having serious problems dealing with. So, I decided to solve this issue by moving all reporting functionality in the application to Microsoft Excel format, which with hindsight I would have done in the original design.

EPPlus is an open source .NET library for reading an writing Excel files. I’ve used this in many projects, and have found it invaluable when the requirement to either read or write Excel files crops up. In this post, I’ll give examples of using EPPlus to write to a Microsoft Excel file.

Setup

Create a new C# console application project in Visual Studio. Download the EPPlus binaries from the Downloads section on the EPPlus CodePlex Site.

Extract the files and add EPPlus.dll as a reference to your project.

Writing to Excel

EPPlus writes Excel 2007/2010 files using the Open Office Xml format (xlsx). The first thing to do, after the initial setup has been completed, is to add the following imports to your code:

using OfficeOpenXml;
using OfficeOpenXml.Style;

Next, let’s create a new ExcelPackage and add some properties to it such as the author, title and company:

using (ExcelPackage p = new ExcelPackage())
{
    p.Workbook.Properties.Author = "Miles Dyson";
    p.Workbook.Properties.Title = "SkyNet Monthly Report";
    p.Workbook.Properties.Company = "Cyberdyne Systems";
   
    // The rest of our code will go here...

}

Now we’ll need to create a new worksheet where we will add our data:

    p.Workbook.Worksheets.Add("April 2012");
    ExcelWorksheet ws = p.Workbook.Worksheets[1]; // 1 is the position of the worksheet
    ws.Name = "April 2012";

We’ll be adding some simple data to this worksheet, contained in 3 columns. We might want to add a header to this worksheet with some column names, and some basic formatting, like making the column header background color something different.

This is simple to achieve using EPPlus:

    int rowIndex = 1;
    int colIndex = 1;

    do
    {
        // Set the background colours
        var cell = ws.Cells[rowIndex, colIndex];
        var fill = cell.Style.Fill;
        fill.PatternType = ExcelFillStyle.Solid;
        fill.BackgroundColor.SetColor(Color.LightGray);
        colIndex++;
    }
    while (colIndex != 4);

    // Set the cell values
    var cell_actionName = ws.Cells[1, 1];
    var cell_timeTaken = ws.Cells[1, 2];
    var cell_processorsUsed = ws.Cells[1, 3];

    cell_actionName.Value = "Action Name";
    cell_timeTaken.Value = "Time Taken";
    cell_processorUsed.Value = "Processing Unit";

The above two actions will be quite common if you use EPPlus to write to Excel files in a lot of different projects. I’d recommend created a static helper class to perform both of these functions (adding the properties and creating a header), I’ve done this with these and other common functions, and I’ve found it’s saved me some time.

Note that we haven’t actually saved the Excel file yet, it’s in memory but we haven’t saved it to disk. Before we do, let’s add some data to it as well as the header. For the purpose of this example, let’s say we already have the data (wherever it may have come from), defined as a List of hypothetical ProcessorAction Objects.

In order to write the data to the file, we can just iterate over this List and write a new row for each ProcessorAction Object to the Excel file:

    // Get hypothetical data...
    List<ProcessorAction> processorActions = DataAccessHelper.GetProcessorData(DataTime.Now);
   
    // Column indexes for clarity
    int actionColIndex = 1;
    int timeColIndex = 2;
    int processorColIndex = 3;

    int rowIndex = 2; // Row 1 is the header

    foreach(ProcessorAction p in processorActions)
    {
        // Action
        var actionCell = ws.Cells[rowIndex, actionColIndex];
        actionCell.Value = p.Action;        

        // Time
        var timeCell = ws.Cells[rowIndex, timeColIndex];
        timeCell.Value = p.Time;
       
        // Processor
        var processorCell = ws.Cells[rowIndex, processorColIndex];
        processorCell.Value = p.Processor;

        rowIndex++;
    }

Now that all our data is written, we want to save the Excel file for distribution:

    // Save the Excel file
     Byte[] bin = p.GetAsByteArray();
     File.WriteAllBytes(@"C:\Reports\Report.xlsx, bin);

Your file should save successfully. That’s the basics of writing to Excel files using EPPlus. In the next post, I’ll outline how to read data contained in an Excel file into memory.

Happy Coding ;-)

Interacting with web pages using Selenium WebDriver for C#

I’ve been using Selenium WebDriver for C# a lot lately, for a number of projects that involved interacting with a web browser in some manner. I’ve used a lot of applications and libraries over the past few years that provide this functionality, but I’ve never come across one as intuitive and reliable as Selenium WebDriver – if you work on any projects that involve interacting with a web browser to automate some process, you need to read this post.

In this post I’ll take you through the process of using Selenium WebDriver to automate some interaction with a web browser and hopefully show you how powerful Selenium is. We’ll take a simple scenario as an example – submitting a request to the Google search engine.

Boot up Visual Studio and create a new C# console application. You’ll need to download the Selenim WebDriver for C# ZIP from Google Code, and add the DLL’s it contains as references to your project. For this example, you are only required to add WebDriver.dll and Newtonsoft.Json.Net35.dll. You’ll need to add the following using statements also:

using OpenQA.Selenium;
using OpenQA.Selenium.IE;

Now your ready to write some code that can drive your web browser. First, we’ll need to create the object that can do just that. With Selenium WebDriver, that is an IWebDriver object. This object can be instantiated to control Internet Explorer, Firefox, or Chrome. In this example, I’m going to use Internet Explorer.

IWebDriver driver = new InternetExplorerDriver();

You could also use ChromeDriver or FirefoxDriver above.

Next, let’s navigate to the Google website. The following code will handle this. One of the nice things about Selenium is that this call won’t return until the page is loaded in the browser. Other frameworks and libraries return immediately, requiring you to add waits/sleeps in your code to ensure the page is actually loaded, which is a terrible approach.

driver.Navigate().GoToUrl("http://www.google.com");

Aside – Internet Explorer Problems

One setting has to be changed in Internet Explorer in order for Selenium WebDriver to work correctly. Your security zones need to have protected mode either enabled or disabled – it doesn’t matter which, as long as it’s the same for each zone. I’ve got it set to on for each zone on my machine. To achieve this follow these steps:

  1. Open IE and go to Internet Options.
  2. Go to the ‘Security’ tab.
  3. Click on each of the zones, i.e. ‘Internet’, ‘Local intranet’, ‘Trusted sites’ and ‘Restricted sites’ and ensure that the ‘Enable Protected Mode (requires restarting Internet Explorer)’ check box is checked.

At this point, build your project in Visual Studio and run it. If you have followed the above steps correctly you will see Internet Explorer open, and navigate to Google automatically.

Now to actually interact with the open web page, Google in this case. In order to submit a search term, we’ll need to interact with the search term text box (to enter a search term), and the ‘Google Search’ button (to submit the request).

To do this, we’ll need to look at the source code for the page, to find the information we’ll need to interact with these controls. From looking at the source code, we can see that the markup for these controls looks like the following (formatted and commented for clarity):

Google Search

So, lets define the search term text box, and enter a search search:

IWebElement searchTermTB = driver.FindElement(By.Name("q"));
searchTermTB.SendKeys("jimmy collins blog");

Take note of what we’re doing here – we’re using the browser object we defined earlier to find an element with the name ‘q’. This is another great thing about Selenium – we can use just about any element attribute to try to find it, you could also use the ID, class name, tag name, or even the XPATH to find an element on the page.

Now build and run your application – you will once again see Internet Explorer opening up, but this time you’ll also see the search term being entered.

The final step is to actually click the ‘Google Search’ button and submit the query. The same approach that we used to find and interact the search term text box is followed:

IWebElement searchBtn = driver.FindElement(By.Name("btnG"));
searchBtn.Click();

Running your application now will open up Google, enter your search term, and hit the search button. The final thing to do is some cleanup – you will notice that currently when your application runs, the browser is left open once it completes. All we have to do to close the browser is:

driver.Close();

That’s how easy Selenium is to use. The ideal scenario is that you have interaction with your development team, and get them to agree to providing static IDs on all controls, that don’t change between versions of your site (unless in the case of a substantial UI revamp). That would make it a simple task to provide re-usable automation that can automatically verify changes to your site, or be used for regression testing.

Java Needs Automatic Properties

One of my main grievances with the Java programming language is it’s lack of Automatic Properties. These have existed in C# since .NET 3.0.

Automatic Properties make property-declaration more concise when no additional logic is required in the property accessors. For example, let’s say we want to implement a simple Book abstraction in an application, with four simple properties for arguments sake.

This is how it may look in C#:

public class Book
{
    public string Name { get; set; }

    public string Author { get; set; }

    public string ISBN { get; set; }

    public string Genre { get; set; }
}

Nice and simple, and written in about 1 minute when coupled with Visual Studio’s intellisense etc.

Now, lets take a look at a class offering the same basic functionality in Java:

public class Book
{
    public String Name;
    public String Author;
    public String ISBN;
    public String Genre;
   
    public void setName(String name)
    {
        Name = name;
    }
   
    public String getName()
    {
        return Name;
    }
   
    public void setAuthor(String author)
    {
        Author = author;
    }
   
    public String getAuthor()
    {
        return Author;
    }
   
    public void setISBN(String isbn)
    {
        ISBN = isbn;
    }
   
    public String getISBN()
    {
        return ISBN;
    }
   
    public void setGenre(String genre)
    {
        Genre = genre;
    }
   
    public String getGenre()
    {
        return Genre;
    }
}

Just on a lines of code basis, it’s easy to see that C# wins overall. I understand that the designers of Java may not want to add this functionality to the language due to potentially breaking millions of current Java applications – but I’m sure it could be added in such a way that new applications could use it without breaking legacy applications.

Hell, a third party library, Project Lombok, already provides support for using Automatic Properties in Java, so it’s definitely possible.

I find this limitation really frustrating when working with Java.

Taking screenshots with Selenium WebDriver

Time to do something a bit more useful using Selenium WebDriver. Taking screenshots as we cycle through our web pages has obvious advantages for unit testing. Firstly we can just run an application overnight, have it visit every page on our website, and review the screenshots for obvious bugs the next morning before releasing to QA. Secondly, we can screenshot our localized websites, and have them linguistically reviewed, or hand off the English screenshots to our translators to provide context during the translation process.

Taking screenshots with WebDriver is exceptionally easy. The thing I like about it most, is that it takes a screenshot of just the area of the browser in which the content resides – no IE/FF/Chrome toolbars etc. But, it also takes the entire webpage – even content not visible on the screen at that time. This is a great feature. I’ve had the problem in the past with long web pages, ones for example that contain a EULA or other long document, where the screenshots had to be taken at intervals as the page scrolled. Obviously not the best process, and not very reusable code.

As mentioned in a previous post, I’m using the Selenium Client Driver for C#. I’ve created a library into which I’m adding useful functions like taking screenshots etc.

The code to take a screenshot is not complex:

using OpenQA.Selenium.IE;
using OpenQA.Selenium;
using System.Drawing.Imaging;

public void TakeScreenshot(IWebDriver driver, string saveLocation)
{
    ITakesScreenshot screenshotDriver = driver as ITakesScreenshot;
    Screenshot screenshot = screenshotDriver.GetScreenshot();
    screenshot.SaveAsFile(saveLocation, ImageFormat.Png);
}

The libraries referenced can be found in the Selenium Client Driver for C# package which can be downloaded from the Selenium website. These will need to be added to your C# project.

Notice in the above function we pass an ‘IWebDriver’ Object, as opposed to a specific InternetExplorerDriver, FirefoxDriver etc. This means this function can be used regardless of which browser you are dealing with.

Using the function is also a trival matter. Let’s browse to Google and take a screenshot, saving it in the root of the ‘C:\’ drive:

IWebDriver driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl("http://www.google.com");
TakeScreenshot(driver, @"C:\screenshot.png");

Note that the TakeScreenshot function can be customized to use any image format – in fact it should probably be a function parameter. I chose PNG simply because of the small size of the generated image.

Refactoring horrible nested if-else statements

I wrote this at some point this week, I was looking back at it tonight and released how truly awful it looks:

if(filePath.Contains(".CSS"))
    return true;
else
    return false;
else if(filePath.Contains(".JS"))
    return true;
else
    return false;
else if(filePath.Contains("_STR"))
    return true;
else
    return false;
else if(filePath.Contains(".VBS"))
    return true;
else
    return false;
else if(filePath.Contains(".HTM"))
    return true;
else
    return false;
else if(filePath.Contains(".BMP"))
    return true;
else
    return false;
else if(filePath.Contains("GIF"))
    return true;
else
    return false;

Terrible huh? What was I thinking?

How much better does this look:

bool copyFile;

copyFile = (filePath.Contains(".CSS"))      ? true:
               (filePath.Contains(".JS"))   ? true:
               (filePath.Contains("_STR"))  ? true:
               (filePath.Contains(".VBS"))  ? true:
               (filePath.Contains(".HTM"))  ? true:
               (filePath.Contains(".BMP"))  ? true:
               (filePath.Contains("GIF"))   ? true:
                                              false;
return copyFile;

I like it, impressed with that one for a Friday.

Handling Exceptions on Windows Phone 7

In this post I’ll outline how to deal with exceptions in a Windows Phone 7 application. Ideally when an exception happens, we want to display some meaningful information to the user that can be provided to the developer to aid in the debugging of the issue. The worst thing that can happen is for the application to crash and just exit. This can lead to frustration on the users part (as I’ve experienced with the official Twitter application on iOS 4.0), or worse dissuade a user from purchasing any other applications bearing your name.

In this example, when an exception occurs, we’ll ensure that we navigate to an error page, and display the stack trace. Aside, this is obviously not the best thing to display to an end user. The best approach may be to display a generic error message with an option to report the error, which then emails the exception information to your support address. I leave that as an exercise to the reader, in this example, we’ll just display the stack trace information directly to the user:

1. Add a new Windows Phone Portrait Page to your Windows Phone 7 project in Visual Studio. For the purpose of this example, I’ve named the page ‘Error.xaml’.

2. In ‘Error.xaml’, locate the ‘LayoutRoot’ Grid element, and replace its content with the following:

<Grid.RowDefinitions>
    <RowDefinition Height="Auto"/>
    <RowDefinition Height="*"/>
</Grid.RowDefinitions>

<!--TitlePanel contains the name of the application and page title-->
<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
    <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION"
                    Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock x:Name="PageTitle" Text="error" Margin="-3,-8,0,0"
                    Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>

<!--ContentPanel - place additional content here-->
<Grid x:Name="ContentPanel" Grid.Row="1">
    <Border BorderBrush="White">
        <TextBlock x:Name="ErrorText"
                       Style="{StaticResource PhoneTextSmallStyle}"
                       TextWrapping="Wrap" />
    </Border>
</Grid>

3. The page you created above will act as the container for any exceptions that we need to display. Next, open up ‘Error.xaml.cs’, and add the following code:

using System.Windows.Navigation;
...
...

public static Exception Exception;

// Executes when the user navigates to this page.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
    ErrorText.Text = Exception.ToString();
}

This sets up an Exception Object that is hooked up to ErrorText.Text upon navigating to the page.

4. Finally, we need to hook up an event handler that ensures that we navigate to this page whenever an unhandled exception occurs. This is done from ‘App.xaml.cs’. Open up ‘App.xaml.cs’, and replace the content of the ‘Application_UnhandledException’ function with the following code:

if (System.Diagnostics.Debugger.IsAttached)
{
    // An unhandled exception has occurred; break in the debugger
    System.Diagnostics.Debugger.Break();
}

// Running on a device / emulator without debugging
e.Handled = true;
Error.Exception = e.ExceptionObject;
(RootVisual as Microsoft.Phone.Controls.PhoneApplicationFrame).Source =
 new Uri("/Error.xaml", UriKind.Relative);

If you have completed the above steps correctly, then whenever an unhandled exception occurs in your application, the application will navigate to this page, and display the exception stack trace.

To verify these steps, add a button to your ‘MainPage.xaml’ and have it link to a page that doesn’t exist, e.g.

NavigationService.Navigate(new Uri("/NotHere.xaml", UriKind.Relative));

Launch the application in the emulator, and click the button you just created, (You’ll need to launch without debugging). The result should be navigation to ‘Error.xaml’ with the exception details:

Windows Phone Error Example

WPF – Binding Data in an XML file to a ComboBox

Some things in WPF are very different from Windows Forms programming. Lately, I’ve been working on my first real UI in which I’ve used WPF over Windows Forms. Using XAML takes a little bit of getting used to, but I’m finding that I have a lot more control over the UI, along with the obvious advantage of seperating the UI presentation from the actual application logic.

Just the other day I needed to create a ComboBox control on a section of my UI, the items in which would need to be loaded from an XML file at runtime. The XML file was to be included as a resource.

The XML file looked something like this:

<LanguageInfo>
<Language name="Arabic" lcid="14337" />
<Language name="Chinese (Simplified)" lcid="2052" />
</LanguageInfo>
...

I wanted the ‘name’ attribute from each Language node to be bound to my ComboBox. In order to do this, first we need to define the XML file as a resource in our XAML:

<Grid.Resources>
    <XmlDataProvider x:Key="LanguageInfo" Source="LanguageInfo.xml" XPath="LanguageInfo" />
</Grid.Resources>
...
...

Then, we just need to add the ItemsSource, SelectedValuePath, and DisplayMemberPath to the ComboBox XAML, specifying the resource we declared earlier, and the attribute that we want to bind:

<ComboBox Height="23"
          ItemsSource="{Binding Source={StaticResource LanguageInfo}, XPath=./Language}"
          DisplayMemberPath="@name"
          SelectedValuePath="@name"
          HorizontalAlignment="Left"
          SelectedItem="Arabic"
          Margin="90,55,0,0"
          Name="LanguageCombo"
          VerticalAlignment="Top"
          Width="120">
</ComboBox>

Note the ‘SelectedItem’ can be set as well from the XAML, It’s set to ‘Arabic’ in the above example.

SharpSVN: A Gentle Introduction

SharpSVN is a really useful library which encapsulates the functionality of the Subversion client so we can leverage it programmatically from .NET applications. I can think of many uses for this. In the past, in some of the automation frameworks I’ve worked with, updating the source code from SVN has been a manual task ran weekly. Using SharpSVN, we could write a simple console application to accomplish this. It could also be useful to automate repetitive SVN tasks in a development environment.

In this post, I’m going to outline the basics of using SharpSVN, and in the process create a simple C# console application to check-out some source code.

For starters, we’ll need to download the SharpSVN package from here. Next, create a C# console application in Visual Studio. You’ll need to target .NET 2.0.

Unzip the SharpSVN package, and add ‘SharpSVN.dll’ as a reference to your console application. Next, add the following code. We’ll pass the location we want to check-out from as an argument to the application:

string Repository = string.Empty;

if (args.Length != 1)
{
     Console.WriteLine("Usage: SharpSVN <Repository>");
}
else
{
     Repository = args[0];
}

I’m assuming here that you’re already authenticated with your SVN server. Now the code to actually perform the check-out, which is really simple:

using (SvnClient client = new SvnClient())
{
    try
    {
        SvnUriTarget target = new SvnUriTarget(Repository);

        if (client.CheckOut(target, @"C:\Working"))
        {
            Console.WriteLine("Successfully checked out '" + Repository + @"' to 'C:\Working'");
        }

    }
    catch(Exception e)
    {
        Console.WriteLine("Error occurred during check out: " + e.ToString());
    }

That’s it. The above code will check-out the source code from the SVN repository you passed as an argument to ‘C:\Working’.

The only downside I’ve seen with SharpSVN, is that it only seems to work when I target .NET 2.0. Maybe I could download the source and try to compile it under .NET 4.0, but I haven’t tried that.

I see huge usage for this library in future projects, and it’s certainly better that a previous solution I had implemented using the command line interface to the TortoiseSVN client.

Happy Coding…

C# – Logging to the Windows Event Viewer

In the past for any applications I’ve written in C#, I’ve always logged any information I needed to in a .txt file in the Windows %temp% directory. This was really quite a messy approach when I consider it now.

Logging from your applications can be useful for a couple of reasons:

  • Auditing: Depending on your level of logging, you can get a step by step view of what’s happening with your application. I find this useful for testing as I write code.
  • Diagnostics: This is the more obvious use of logging from your application – capturing a stack trace or other useful information in the event of any issues.

Using the Windows Event Viewer to capture auditing or diagnostic logging is a much better approach, as you can specify when you log what type of event this is, i.e. ‘Information’ (for auditing), or ‘Warning’ and ‘Error’ (for diagnostic logging). This makes it a lot easier to find errors, and makes any logging you do highly readable.

I’ve created this class which wraps the logging to the Event Viewer functionality:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Diagnostics;

namespace EventLoggingExample
{
    public class LoggingHelper
    {
        private string Application;                
        private string EventLogName;              

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="app">The application doing the logging</param>
        /// <param name="log">The log to write to in the Event Viewer</param>
        public LoggingHelper(string app, string log)
        {
            Application = app;
            EventLogName = log;

            // Create the event log if it doesn't exist
            if (!EventLog.SourceExists(Application))
            {
                EventLog.CreateEventSource(Application, EventLogName);
            }

        }

        /// <summary>
        /// Write to the event log
        /// </summary>
        /// <param name="message">The message to write</param>
        public void WriteToEventLog(string message, string type)
        {
            switch (type.ToUpper())
            {
                case "INFO":
                    EventLog.WriteEntry(Application, message, EventLogEntryType.Information);
                    break;
                case "ERROR":
                    EventLog.WriteEntry(Application, message, EventLogEntryType.Error);
                    break;
                case "WARN":
                    EventLog.WriteEntry(Application, message, EventLogEntryType.Warning);
                    break;
                default:
                    EventLog.WriteEntry(Application, message, EventLogEntryType.Information);
                    break;
            }  
        }
    }
}

To use it, just create an instance and log at will:

LoggingHelper log = new LoggingHelper("MyApplication", "MyAppLog");
log.WriteToEventLog("Some application information", "info");
log.WriteToEventLog("This is your first warning!", "warn");
log.WriteToEventLog("An error has occurred...", "error");

This is definetely something I’ll be adding to my utilities library.

ASP.NET MVC – Creating a DropDownList

I’ve been looking at the ASP.NET MVC framework for the past two weeks, and it has occurred to me that some of the simple things we may want to do when creating a web application may seem confusing to someone new to ASP.NET MVC – for example the task of creating a DropDownList control on a form. ASP.NET MVC provides a number of ‘HTML Helpers’ which we can easily use to construct the form items. ‘DropDownList’ is one of these HTML helpers we can use.

Let’s create a simple example form using some of these HTML helpers. To begin a form, we can use a helper, we just need to add this code to our View:

<% using (Html.BeginForm()){ %>

// Form data will go here

<% } %>

This creates the basic form code for us – no need to explicitly write any HTML code. Before adding the DropDownList control, we need to decide where we want to get the data which will bind to the list. We can either hard code the items, or use LINQ to SQL to grab them from a database at runtime.

Method 1 – Hardcoding the form items

With this approach, we just add the items to a list, and pass this list to ViewData, so we can access it from the View:

List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem        
{          
    Text = "Apple",          
    Value = "1"        
});
items.Add(new SelectListItem        
{            
    Text = "Banana",            
    Value = "2",            
    Selected = true        
});
items.Add(new SelectListItem        
{            
    Text = "Orange",            
    Value = "3"        
});

ViewData["DDLItems"] = items;
return ViewData;

Then, to actually display the DropDownList, we’d just need to add a single line to our View code, utilizing the DropDownList HTML helper:

<%= Html.DropDownList("DDLItems") %>

Method 2 – Using LINQ to SQL to get the data at runtime

We could also retrieve the list data from a database table at runtime using LINQ to SQL. In order for this approach to work, you will need to have generated LINQ to SQL classes for your database using the wizard in Visual Studio. Then we can easily write the code to retrieve the data:

// Get the list of supported languages (for example) from the DB
var db = new TransDBDataContext();
IEnumerable<SelectListItem> languages = db.trans_SupportedLanguages
       .Select(c => new SelectListItem
        {
              Value = Convert.ToString(c.ID),
              Text = c.Name.ToString()
        });

ViewData["SupportedLanguages"] = languages;
return View();

Again, to display the DropDownList, we’d just need to add a single line of code to the View:

<%= Html.DropDownList("SupportedLanguages") %>

From the above, you can see how easy it is to render form items using the HTML helpers provided by ASP.NET MVC.

For a full list of the helpers, check out the MSDN documentation here.

Windows Phone 7 Development – First Impressions

Since my Christmas leave from work has begun, I’ve had some time to really look at Windows Phone 7 development over the last 2 days, and have gotten really excited about it all. I’ve been meaning to do this for ages (since WP7 was released actually), but have always either been too busy or suffered procrastination (thanks Zen Habits!).

Some positives:

  • Development Environment – I think Microsoft has done an excellent job on Visual Studio 2010, and the WP7 development tools plug in seamlessly. If you don’t already have Visual Studio 2010, Microsoft is offering a special Express Editon for Windows Phone.
  • Developer Resources – There are a huge amount of resources available on Microsoft’s App Hub (often called ‘MarketPlace’ – the equivalent of Apple’s ‘App Store’), ranging from tutorials, walkthroughs of some key concepts and full application code examples.
  • The Windows Phone Emulator – This is installed as part of the developer tools, and it really is state of the art. I haven’t purchased an actual device running WP7, but have been using the emulator to test my inital application effort. There are some obvious things that won’t work on the emulator, for example anything to do with the accellerometers (the emulator assumes it is lying on a flat surface), but it’s perfect for testing your inital Windows Phone 7 applications.
  • Familiarity – If you’ve ever developed using C# on the Windows platform, you already have a huge start in WP7 development.
  • Developer Subscription – The cost of a yearly developers subscription, a mere 99 Euro, can easily be covered with very little downloads of your applications (should you even be bothered about it).

The only ‘negative’ I’ve found so far is that I’ve had to purchase a (long overdue) brand new Dell running Windows 7 in order to create my development environment. My previous machine, running Windows XP, is not supported by the Windows Phone 7 development tools, which seems strange to me, since XP is not scheduled to be EOL’d until 2014. It seems to be another move by Microsoft to push people to move to Windows 7 or (shudder) Windows Vista.

Windows Phone 7 development is one of the three areas I want to become proficient with in the first half of 2011. I had initially focused on the iOS platform, but when I thought about it, it didn’t make much sense, since I’m already familiar with C# and didn’t feel I’d gain any real advantage by learning Objective C. Also, rumours began to circulate this week regarding Microsoft getting into bed with Nokia, so WP7 will surely gain more momentum in the first half of 2011.

My first application, (well under way!), will be a simple Twitter client. The reason I chose this is that it will encompass many of the key concepts I’ll need to learn, such as designing user interfaces for WP7, storing information locally on a WP7 device, and accessing external information via API’s. I plan to complete this over Christmas – screenshots to follow once it is.

Aside: If you’re interested in getting into developing on the Windows Phone 7 platform, check out Jeff Blankenburg’s 31 Days of Windows Phone, it’s the best introductory article series I’ve found so far.