Google Website Translator Gadget

Showing posts with label Silverlight. Show all posts
Showing posts with label Silverlight. Show all posts

Monday, 22 July 2013

Silverlight 5 PivotViewer Localization

We’ve recently started using the Silverlight 5 PivotViewer control in On Key Express as a great way for visualizing the Work Orders showing the work list that needs to be completed/has been completed.  Here is some screenshots showing the different “cards” of information for the Work Orders at different zoom levels.

image

Small Card View

image

Large Card View

One of the great features of On Key Express is the ability for clients to translate the system into any language.  This is great feature for customers who have engineers working across the globe as they can customise the system into any language they which to support.  We supply the default English translation but the clients can even decide to change the English translation if they have certain client specific terminology they want to use.

This language feature however imposes an important restriction of any third party control that we use – we need the ability to translate any resource displayed by the control.  We make extensive use of the excellent Telerik RadControls for Silverlight.  Fortunately these controls are fully localizable by hooking up a custom ResourceManager to override the default resources being used by the Grids and other controls. 

The PivotViewer control has quite a few resources.  It ships with support for a few languages out-of-the-box through providing the resources in separate System.Windows.Controls.Pivot.resources.dll resource assemblies.  By setting the ItemCulture property you are able to use the different out-of-the-box translations shipped with the control.  However, unlike the Telerik controls there is however no easy way to hook into the control to override the resources being used.  We can create additional resources assemblies for different languages but as mentioned previously, the client decides what additional languages they want to support. 

We therefore needed a way to hook into the PivotViewer to inject our own Resource Provider that will use the client provided resource translations.  Technically we store the clients translations in a database that is periodically synchronised with the client devices.  After looking at the PivotViewer using Reflector, we confirmed that it makes use of the usual an internal static Resources class that is generated whenever you add .resx files to a project.  The Resources wrapper internally makes use of a ResourceManager that uses the current culture to access the language specific resource file.  We needed the ability to intercept the calls being made by this ResourceManager.  We also wanted the ability to still use the existing Microsoft resource assembly as the default fallback mechanism for any of the resources that we don’t want the clients to translate.  There includes the numerous exceptions and other design time resources which the clients aren’t interested in. 

With this in mind, we created the following PivotViewerResourceManager wrapper class:

Notice that the class is a simple ResourceManager wrapper around the existing Microsoft resource assembly.  When override the GetString method to allow us to first do an external lookup for the resource using our IResourceProvider interface.  If we do not have an external translation, we simply delegate to the Microsoft provided resource.  We adopted the convention of prefixing all the client provided translations for the PivotViewer with the “MSPivot” prefix as it makes it easier to identify all the translations related directly to the control.  With all of this in place we simply added a trigger to the PivotViewer XAML to load and inject our PivotViewerResourceManager when the control is loaded via the InjectResourceManager method.

Here is a sample screen shot of our ResourceManager in action with the PivotViewer.  Notice that some of the resources for which we provide translations are show in a different language whilst the rest of the control falls back to using the default MS provider resources. 

image

Sweet!

Friday, 28 December 2012

Async support for running Silverlight Unit Tests

One of the great new features included with .NET 4.5 and Visual Studio 2012 is the async/await support for writing more elegant multi-threaded code in C# or Visual Basic.  Support for writing tests that make use of the async/await keywords is available for the Microsoft Test Framework as well as most of the other popular xUnit testing frameworks like NUnit.  In addition to providing support for async/await baked into the compilers for Visual Studio 2012 and .NET 4.5, Microsoft has also released the Async Targeting pack for Visual Studio 2012 that enables projects targeting .NET 4.0 or Silverlight 5 to use the Async language feature in C# and Visual Basic code.

Using it in your Silverlight 5 code is quite handy – especially when invoking web services from the client.  However, the Microsoft Test Framework for Silverlight has not been extended to support running these asynchronous tests.  However, in a recent blog post by Morten Nielsen he shows how it is possible to go about adding this support to the Silverlight Test Framework.  He provides a customized version of the Silverlight Test Framework, but also urges us to go vote for getting this added directly to the official Silverlight Toolkit. 

I know a lot of development shops like ourselves still have quite a substantial amount of Silverlight code to maintain, so please go and vote for the issue to get Microsoft to add support for it in the Silverlight Toolkit.

Wednesday, 08 February 2012

Silverlight 5 Upgrade Woes

We recently struggled to upgrade the UI of On Key, Pragma’s Enterprise Asset Management System (EAMS) to Silverlight 5.  We struggled with getting Silverlight 5 binaries for a lot of the open source projects (Moq, Castle.Core), but it was the lack of development tooling support in Visual Studio 2010 that really disappointed us.  I can understand that the open source projects are slack in providing support for Silverlight 5.  We solved the problem by getting the source code and creating Silverlight 5 binaries ourselves.  However our hands are cut off when it comes the development tooling in Visual Studio.  Expression Blend 5 is still only in preview edition with no word on when a final version that supports the SL 5 RTW will be provided.  Code Analysis (FxCop) and Code Metrics analysis are broken for Silverlight 5 projects.  I find it very disappointing that MS didn’t make sure that all parts of the development tooling surrounding Silverlight 5 was working by the time that Silverlight 5 went RTW.  Hopefully these issues will be resolved soon, but one sort of gets the impression that with no more momentum behind Silverlight it might take quite a while Sad smile

Monday, 11 April 2011

Pragma On Key Silverlight Localization

Globe

One of the big features that we are adding to Version 5.4 of On Key, Pragma’s Enterprise Asset Management System, is the ability to use the system in different languages.  It seems like the first language we will support in addition to English will be Brazilian Portuguese  to support customers for our Pragma Brazil service company.  So I’ve been spending some time during the past week or two looking at the various aspects of On Key that we need to localize to determine the best solutions for getting everything localized.  In this post I will cover some useful information I discovered in my research.

Saturday, 31 October 2009

Integrating your Silverlight Test Run Results into TeamCity

We’ve been using TeamCity with great success at our company to do continuous integration.  We have build configurations defined for building and deploying our On Key suite of software;  for running our suites of tests and also for generating our static code analysis metrics. 

Problem

One of the problems we faced was integrating the Silverlight UI test results generated using the Microsoft Silverlight Test Framework into TeamCity.  TeamCity comes with out-of-the-box support to automatically pick up and display the tests results generated by the NUnit and MSTest report formats.  However, the Microsoft Silverlight Test Framework works differently to NUnit and MSTest as it runs as a Silverlight application.  The same Silverlight security sandbox restrictions therefore also apply to the Silverlight Test Framework.  This makes it more difficult to get the test results out of the framework as the results are not written to a file like NUnit or MSTest.  Fortunately TeamCity allows you to integrate the test results of any framework by writing special ServiceMessage commands as part of your build output.  TeamCity will listen for these commands and interpret and display the information as part of your build results on the portal. 

A Solution That Works

So we initially solved the problem by automating the test run and screen scraping the results from the Silverlight Test run page.  From the screen scraped results we created TeamCity Service Messages and wrote these messages as part of our build output.  I got the screen scraping idea from this blog post by Jonas Follesoe.  This worked, but it was a clumsy solution at its best as parsing the html, looking for specific div’s to find out whether the tests failed or not was very error prone.  We also had no guarantee that the HTML format for the test report would stay the same between subsequent releases of the Silverlight Test Framework.  However, the real show stopper was that we ran into an issue with TeamCity when the tests failed.  The problem occurred because the test exception would contain invalid XML characters.  When TeamCity tried to run the failed tests, the XML RPC communication between the build agent and TeamCity server would fail due to the invalid characters in the XML stream.  This resulted in the build going into a loop as the TeamCity Server was not able to pick up any response from the Build Agent it was trying to run the build on.

A Solution That Rocks

Instead of trying to panelbeat the existing solution, I decided to investigate alternatives and started by using trusty Reflector to look at the source code of the Silverlight Test Framework.  I wanted to see whether there was not a more elegant way in which to report on the test results.  (Btw, you don’t need to use Reflector – you can download the source code of the Silverlight Test Framework as it is included as part of the Silverlight Toolkit).  Sure enough, I discovered that the framework already included the necessary extensibility points for plugging in your own reporting mechanism.   The LogProvider base class provides the core API for creating your own logger that the Silverlight Test Framework will then call into for you to process the test results.  There is also a TestReportService that seems like the mechanism to use to write the log output by exporting it via a service call.

So the idea was to implement my own TeamCityServiceMessageLogProvider that would write the Test results into ServiceMessages that TeamCity understands.  The implementation turned out to be quite straightforward (download from here).  The Logger opens up an IsolatedStorageFileStream and just writes the test results received to the stream.  You plug the logger into the Silverlight Test Framework by adding it as an additional LogProvider in your App startup:

private void Application_Startup(object sender, StartupEventArgs e)
{
   AppSettings.Initialize();

   UnitTestSettings settings = UnitTestSystem.CreateDefaultSettings();
   settings.LogProviders.Add(new TeamCityServiceMessageLogProvider());

   RootVisual = UnitTestSystem.CreateTestPage(settings);
}
So this took care of getting the results in the right format, but I still had to figure out how to get the results out of the Silverlight Test Framework into TeamCity.

Some further reflectoring showed that these extensibility areas also seemed to already exist within the Silverlight Test Framework, but I was unable to get them wired up and working correctly.  So I resorted to implementing my own solution by using a WebClient to upload the streamed results onto our web server for reporting to TeamCity. The first thing I had to do was to hook into the Publishing event of the Test framework to allow my custom logger to upload the results.  For this I had to implement the ITestSettingsLogProvider interface that provides an Initialize method that will be invoked by the Silverlight Test Framework.  I then hooked into the Publishing event as follows:

public void Initialize(TestHarnessSettings settings)
{
  Settings = settings;
  UnitTestHarness testHarness = Settings.TestHarness as UnitTestHarness;
  if (testHarness != null)
  {
    testHarness.Publishing += ((sender, e) => PostTestResults());
  }
  
  Store = IsolatedStorageFile.GetUserStoreForApplication();
  Stream = Store.CreateFile(LogFile);
  Writer = new StreamWriter(Stream);
}
In the PostTestResults method, I upload the results on to the server:
private void PostTestResults()
{
   WebClient client = new WebClient();
   client.OpenWriteCompleted += (sender, e) =>
                                {
                                    Stream input = e.UserState as Stream;
                                    Stream output = e.Result;

                                    byte[] buffer =  new byte[4096];
                                    int bytesRead = 0;
                                    input.Position = 0;
                                    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
                                    {
                                        output.Write(buffer, 0, bytesRead);
                                    }
                                    output.Close();
                                    input.Close();
                                };

   Writer.Flush();
   client.OpenWriteAsync(new Uri("http://localhost/OK52/UnitTest.aspx?Results=true", UriKind.Absolute), "POST", Stream);
}
Notice the use of the Results=true query string parameter.

On the server side, we have a UnitTest.aspx web page that we use as the start page for our Silverlight Tests.  A nice feature that I picked up from another excellent Jonas Follesoe post is that you can pass query string parameters onto the test page and use these as initialization parameters for your Silverlight application.  We use this to restrict the suite of tests being run by using the Tag feature of the Silverlight Test Framework -  i.e. http://localhost/OK52/UnitTest.aspx?Tag=Staff to only run the Staff tests.  We also use this to configure the user that is logging on to the system – i.e. http://localhost/OK52/UnitTest.aspx?Tag=Staff&User=Carel&Password=secret.  When reporting the test results, we post to the same UnitTest.aspx page but send through the Results=true query string parameter to indicate that we want to upload the test results onto the server.  The results are then written to a file on the server for further processing as illustrated below:

public void Page_Load(object sender, EventArgs e)
{
  if (string.IsNullOrEmpty(Request.QueryString["Results"]))
  {
    if (!string.IsNullOrEmpty(Request.QueryString["Tag"]))
    {
      Tests.InitParameters = "Tag=" + Request.QueryString["Tag"] + ",";
    }
    if (!string.IsNullOrEmpty(Request.QueryString["UserName"]))
    {
      Tests.InitParameters += "UserName=" + Request.QueryString["UserName"] + ",";
    }
    if (!string.IsNullOrEmpty(Request.QueryString["Password"]))
    {
      Tests.InitParameters += "Password=" + Request.QueryString["Password"] + ",";
    }
    if (!string.IsNullOrEmpty(Request.QueryString["DB"]))
    {
      Tests.InitParameters += "DB=" + Request.QueryString["DB"] + ",";
    }
  }
  else
  {
    StreamReader inStream = new StreamReader(Context.Request.InputStream);
  
    string filePath = Server.MapPath(@"~\Logs\SLTests.log");
    FileStream outstream = File.Open(filePath, FileMode.Create, FileAccess.Write);
    
    // Read from the input stream in 4K chunks and save to output stream
    const int bufferLen = 4096;
    char[] buffer = new char[bufferLen];
    int bytesRead = 0;
    while ((bytesRead = inStream.Read(buffer, 0, bufferLen)) > 0)
    {
      outstream.Write(Encoding.UTF8.GetBytes(buffer, 0, bytesRead), 0, bytesRead);
    }
    outstream.Close();
    inStream.Close();
  }
}          
Once the file has been uploaded, it is a simple matter of echoing the test results back to the TeamCity server by writing it to the NUnit TestContext: 
[TestFixture]
[Category("UI")]
public class RunOnKey5ClientIntegrationTests : BaseTest
{
  private const string OnKeyUri = "http://localhost:80/OK52/UnitTest.aspx";
  private const string LogPath = @"..\..\..\Pragma.OnKey5.Server\Logs\";
  private const string SilverlightTestsLog = "SLTests.log";
  
  [Test]
  public void RunSilverlightTests_UsingTeamCityServiceMessageLogger_ToShowResultsOnTeamCityPortal()
  {
    // Clear the old results
    if (File.Exists(LogPath + SilverlightTestsLog))
    {
      File.Delete(LogPath + SilverlightTestsLog);
    }
  
    using (FileSystemWatcher watcher = new FileSystemWatcher(LogPath, SilverlightTestsLog))
    {
      Manager.LaunchNewBrowser();
  
      // Navigate to the VDir hosting the tests
      ActiveBrowser.NavigateTo(new Uri(OnKeyUri));
  
      watcher.EnableRaisingEvents = true;
      WaitForChangedResult result = watcher.WaitForChanged(WatcherChangeTypes.Created);
      if (!result.TimedOut)
      {
          // Echo the TeamCity ServiceMessages so that the test results can be picked by the Portal
          string[] lines = File.ReadAllLines(LogPath + SilverlightTestsLog, Encoding.UTF8);
          foreach (string line in lines)
          {
              TestContext.Out.WriteLine(line);
          }
      }
    }
  }
}
You’ll notice that the whole test run is managed as a NUnit test fixture that is flagged with “UI” as a Category.  This allows me to use the nunit-console.exe to run the specific tests using the /include=UI command line parameter.  I use the FileSystemWatcher class to wait until the SLTests.log file has been published and them echo the results to TeamCity by writing it to the NUnit TestContext.

Conclusion

I hope you’ll find this information useful for publishing the results of your own Silverlight Test Runs during your automated builds.  Jeff Wilcox has mentioned that more of the build automation extensibility points of the Silverlight Test Framework will be made known in future releases.  Until such time this is one way of doing it.