testing document conversion


Being able to properly read many different file formats is important for KOffice success. By ‘read’, I mean ‘convert to ODF’ because the conversion and reading is strictly separated in KOffice. KWord will convert a .doc file to a .odt file before loading it into the internal rendering and editing structure. There is even a nice separate program called ‘koconverter’ that can convert files on the command-line.

So far, there were no decent tests to avoid regressions in our filters. I have written a small framework (well, a shell script, but framework sounds better) that makes it simple to write tests. There are a number of tests there now for converting ppt files, but it would be great to have them for other input formats too. And here is where I hope you will help. All you need is a small input file that highlights a feature or problem and a small XSL file. The XSL file contains the test.

Look at this small example. Suppose you have a file, it can be a .doc, .docx or another office format. The file contains only one image and you want to have an automated test to verify that the ODF that is created also has one image. The following XSL file tests this:

If the number of image elements is not exactly one, the XSL transformation will abort with an error message.

So you see that the framework is written in such a way that writing tests is easy and fast. When reporting a bug in KOffice or koconverter you can help a lot by writing an XSL for our automated tests. You will see that this will speed up fixing the bug and it will help avoid regressions.

This way of testing is a bit unconventional: these are not unit tests but overall tests. Files are converted to ODF and the output file is checked. Not a small part is tested but the complete conversion is tested. A benefit is that the tests are independent of the programs doing the conversion. We just check the result. So the same method could be used on any programs that write out ODF files.

Here is how our tests in KOffice work. First we convert the input file to ODF with koconverter. An ODF is a zip file with many files and we usually want to check the content of the XML files. So after conversion with koconverter, the ODF file is uncompressed. Then an XSL transformation is run on the file content.xml.

In XSL on can report errors like this:

(You see that XML does not have to be too verbose.) The prefixes x: and s: in this snippet stand for http://www.w3.org/1999/XSL/Transform and urn:oasis:names:tc:opendocument:xmlns:style:1.0 respectively. The test checks if the fill-color for a particular part of the output document has the correct value. If not an error message is printed and the transformation stopped.

You can replay this example by checking out the tests:

That was the overview of how the tests work. Now let us look into one more complicated test. It has two files: background.ppt and background.xsl. background.ppt is the input file and background.xsl is the transformation that verifies the output of the transformation.

The file background.ppt has two frames, one of which must have a light blue (#bbe0e3) background. At the moment the frame gets a background color, but it is wrong. So when fixing this bug we first formulate what we want the result to be by writing an XSL file.

One XSL file can contain multiple tests. This test is called testSolidBackground:

We assign the second frame in content.xml to a variable:

Now we find the name of the style for this frame:

And find the style with that name:

Now we do a sanity check: do we even have a second frame?

And do we even have a style?

Now we test if the background is ‘solid’:

And we check the color:

That is all there is to it! Learning XSL if you do not know it yet is some
effort but one that will pay off. Once you have the XSL you can run ‘make test’
while fixing the bug. This will call the test for you which has as side-effect
that the conversion is run and the odf file unpacked.

I hope you all will start using this method for reporting and fixing filter bugs. I stop by starting you off with some links to XSL and XPath.


Post a comment