TDD’ing the GUI
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?
- I find that using TDD produces better code faster
- Automated testing has been my friend
- 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
very very nice !
congratulation
For those who are scratching their heads as I did:
TDD = Test Driven Development
Daniel
Pretty cool!
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.
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.
@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.
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…
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
Really informative,fantastic list of tools
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);
I loved your post, very informative indeed!
Search your favourite Documents, Picture, Video, Achive, Music. It’s simple
it’s the first time to be here.Very happy to come here.
Very good article
Good article, thanks to the author to share.welcome come to
http://www.footballworldcupjerseys.com.
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!
interesting!
good good…this post deserves nothing Frown …hahaha just joking Tong …nice post
thanks for your sharing ROY, I appreciate this. keep up the good work
Hey great stuff, thank you for sharing this useful information and i will let know my friends as well.
I really liked your article. Keep up the good work.
You came up with a real good one this time.
Admiring the time and effort you put into your blog and detailed information you offer!
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.
I think it may be help all of you. Thanks a lot for enjoying this beauty blog with me.
Best canon Coffee Mugs! Funny, Cute, & Humorous Unique designs. Also find Travel Mugs, Coffee Cups also, or Create Photo Personalized Mugs & Drinkware
I think this is a wonderful post!