Tuesday, April 30, 2013

Getting Started with a Browser Dart Test Suite

One of the main reasons that I am switching the ICE Code Editor from JavaScript to Dart is for testing. I keep making all sorts of changes to a moderately large project with no safety net. With the Dart version, I will have type analysis safety net from the outset. Even so, I love me some tests.

The ICE Code Editor is very much a browser-based application. This means that the tests need a simple web page to serve as context for my tests. So I create the following as test/index.html:

There is not much to this page. The body has an <h1>, which serves no purpose other than to have something on the page when viewed in a browser. The two <script> tags load the test suite and start the dart engine.

As for the test suite, I start very simple by testing some default values in the Editor class. The test outline will look something like this:

The import statements pull in the libraries that are needed for testing: the unit test library, the class being tested, and 'dart:html' for simple DOM manipulation and querying. Next comes the main() function. All Dart scripts need a main() entry point, so I oblige. I start by jamming all of my tests directly inside the body of the main() function. Eventually, I will pull them out, but this will do for now. Lastly, I add a test group(). This is not strictly necessary, but I seem to recall that certain test output formatters appreciate a group().

With that, I am ready to test the class. It is possible to disable auto-update of the preview layer when code changes take place. I am going to test that an Editor instance has auto-update on by default. The test for this is simple enough:

And it passes. When I load the test page up in Dartium and check the console, I see:

Unfortunately, the bug that suppresses console output in DumpRenderTree stills seems to be in place. When I try to test from the command-line, I see console output indicating that the unit test suite is starting, but nothing else:

Before calling it a night, I get started on the js-interop testing, which I need for things like incorporating ACE code editor. I will leave the actual testing until tomorrow, but I would like to ensure that turning it on does not break anything. So I add it to the Editor definition:

This turns out to be due to the web page that provides context for the test suite. More specifically, it is not sufficient to start the Dart engine if I want to perform any kind of js-interop. There is additional setup required that, thankfully, is included in the browser dart package that is already part of my package dependencies. All I need do is replace this <script> tag in the web page: