Home > TDD, XP > TDD’ing the GUI

TDD’ing the GUI

July 31st, 2007

Can’t be done, right?

We have some patterns that help us do this: MVP, Passive View, Supervising Controller, Presenter First. Rolling your own implementation of these patterns is challenging and time consuming. Additionally, we haven’t had any frameworks to make it any easier.

So that’s the problem I set out to solve. I’ve reached a point where I’ve been able create a proof of concept application with this framework I’ve been writing. I’m hoping to get some feedback from the community on this.

So what did I do?

I chose to solve the problem using the Passive View pattern. I wanted to be able to design a form with the Delphi form designer but I didn’t want to put any code in the form implementation. That left me with the problem of controlling the GUI from the controller object. Since the point of this exercise is to keep everything testable from a unit test I couldn’t have the controller interacting with the concrete GUI elements. So I came up with the following:


IView = interface
  GetViewElement(const ID : string) : IViewElement;
end;

IView has the responsibility of providing access to the view elements to the controller. So the controller can interact with these IViewElement interfaces instead of the concrete GUI elements (like TButton, TEdit, etc.).

What I didn’t want is to have to use special VCL components to make this work. First, because it would be too much trouble to create a full suite of components that implemented IViewElement and second because I wanted to be able retro fit an existing application to this style of design. To accomplish this I did the following:

TGuiAdaptor = class(TInterfacedObject, IViewElement)
public
  constructor Create(AControl : TControl)
end;

In GetViewElement the concrete view wraps concrete GUI elements with the TGuiAdaptor and returns it to the controller. So now we can choose to mock the view or, more appropriately to the purpose, use the concrete view for testing.

Having got this far I can now use TDD to express behavioral requirements in the form of unit tests. Have a look at the demo application included in the archive.

Why would I do this?

  1. I find that using TDD produces better code faster
  2. Automated testing has been my friend :)
  3. I hope I can convince someone at CodeGear that this is style of development is a Good Thing ™ and to enable the IDE to directly support this type of development

Be aware: This is a proof of concept release. I know there’s a great deal more work to go before this would be viable for production.

Here is the code: PassiveViewFramework.rar

TDD, XP , , ,

  1. salvador
    July 31st, 2007 at 09:27 | #1

    very very nice !
    congratulation

  2. Daniel
    July 31st, 2007 at 09:53 | #2

    For those who are scratching their heads as I did:

    TDD = Test Driven Development

    Daniel

  3. July 31st, 2007 at 14:23 | #3

    Pretty cool!

  4. Mauro Venturini
    August 2nd, 2007 at 00:23 | #4

    Cool!
    I was experimenting with separation beetween designer generated code and the main part of the application because it enables the use of a designer external to the IDE. As Highlander will not have a WinForms or WPF designer this would allow to design forms with a free or cheap version of VS and keep using Delphi for the serious (.NET 2/3/3.5) coding and debugging.
    With IDE direct support the designer catch on problem would vanish.

  5. Hans-Peter
    August 3rd, 2007 at 10:30 | #5

    Recently I read the same papers as mentioned. Very interesting!

    However I thought that the controller should update (in the TMainFormController.AddButtonClick procedure) a *model* (which would then notify the observers (the same controller in your example)) and not directly the view.

    Maybe this is overkill/too complicated but still imho the model (domain) layer is very important in the papers and cannot be left out.

  6. August 3rd, 2007 at 10:42 | #6

    @Hans-Peter

    Agreed. The model is an important piece.

    The problem I set out to solve was how to create a GUI without code, in the Delphi form designer and controlled from an external object. At the time, updating a model was not part of the problem. I am, however, working to that end.

    I guess I considered the model portion a bit easier since the hard part about test driven development is the GUI. Writing tests for the controller and model is considerably easier.

  7. November 18th, 2007 at 13:43 | #7

    At CodeGear, they have a framework called Zombie to interact with the application using Windows messaging system plus some nifty tricks.

    Check :
    http://www.stevetrefethen.com/blog/AutomatedTestingDelphiStyle.aspx

    or the related patent :
    http://www.patentstorm.us/patents/5790117.html

    A pity that CodeGear does not provide this killer app to the rest of us…

  8. mrbar2000
    January 31st, 2009 at 11:25 | #8

    Hi,

    Very good article. We are buildind the infra framework and I would like that you could help us to put a PassiveView framework into it. The Infra is a set of frameworks to build application totally object oriented.

    If you have interesting contacte by mail.

    Look this links:
    http://code.google.com/p/infra/
    http://groups.google.com/group/infradeploy

  9. April 1st, 2010 at 02:52 | #9

    Really informative,fantastic list of tools

  10. yap
    April 25th, 2010 at 22:24 | #10

    I compile PVDemoApp with Delphi7,
    click on ListTextEdit causes Access Violation occasionally.

    this issue can be tackled by adding
    fcontrolmethod:=nil;
    in
    constructor TGuiAdaptor.Create(AControl: TControl);

  11. April 28th, 2010 at 18:57 | #11

    I loved your post, very informative indeed!

  12. June 16th, 2010 at 04:31 | #12

    Search your favourite Documents, Picture, Video, Achive, Music. It’s simple

  13. June 18th, 2010 at 17:04 | #13

    it’s the first time to be here.Very happy to come here.

  14. July 9th, 2010 at 18:25 | #14

    Very good article

  15. dfdsh
    July 24th, 2010 at 09:23 | #15

    Good article, thanks to the author to share.welcome come to
    http://www.footballworldcupjerseys.com.

  16. July 27th, 2010 at 18:50 | #16

    it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity!

  17. July 27th, 2010 at 21:07 | #17

    interesting!

  18. August 3rd, 2010 at 01:29 | #18

    good good…this post deserves nothing Frown …hahaha just joking Tong …nice post

  19. August 3rd, 2010 at 18:06 | #19

    thanks for your sharing ROY, I appreciate this. keep up the good work

  20. August 6th, 2010 at 03:43 | #20

    Hey great stuff, thank you for sharing this useful information and i will let know my friends as well.

  21. August 6th, 2010 at 21:29 | #21

    I really liked your article. Keep up the good work.

  22. August 11th, 2010 at 13:12 | #22

    You came up with a real good one this time.

  23. August 18th, 2010 at 01:55 | #23

    Admiring the time and effort you put into your blog and detailed information you offer!

  24. August 21st, 2010 at 05:20 | #24

    I found your website perfect for my needs. It contains wonderful and helpful posts. I have read most of them and got a lot from them.

  25. August 23rd, 2010 at 05:42 | #25

    I think it may be help all of you. Thanks a lot for enjoying this beauty blog with me.

  26. September 1st, 2010 at 19:51 | #26

    Best canon Coffee Mugs! Funny, Cute, & Humorous Unique designs. Also find Travel Mugs, Coffee Cups also, or Create Photo Personalized Mugs & Drinkware

  27. September 6th, 2010 at 17:27 | #27

    I think this is a wonderful post!

  1. No trackbacks yet.

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word