Thursday, February 07, 2013

Unit testing KColorButton

Around two weeks ago, a bug about was reported in which Okular crashed when changing the color of highlighted text. After a bit of debugging i found that the bug was actually in kdelibs in the KColorButton class.

This is a class I had never looked internally so it was a bit hard to decide where to start looking for a fix. Then I decided to look at the git log and realized the last commit introduced by a fix for color selection not working correctly on double click.

I took my chances and saw that reverting that patch actually removed the crash, so now I knew where to look :-)

After a few minutes looking at the code i came up with a 3-line patch that fixed the crash and still made double click work correctly. I'm so awesome \o/

But that wasn't enough, I needed to *prove* I had fixed it, so i decided to create some tests, that was a bit harder since it involved "clicking" in buttons and stuff, but QTest to the rescue and a few hours later I had this nice unit test that will warn us if that breaks again :-)

So next time you fix a bug, unit test it! And don't complain it's graphical, that's no excuse!

And of course the tests run just fine in even if they show buttons/dialogs :-)