Professional Documents
Culture Documents
Test First GUIs
Test First GUIs
Using Model-View-Presenter
1
Copyright©2008-2009 Robert Walsh - All Rights Reserved
2
About the Presenter
Robert Walsh
President/CEO for EnvisionWare, Inc.
10 years experience developing software for public
and academic libraries
About 6 years experience with Agile Development
Methodologies
Benefits of MVP
Implementing MVP
Summary
Notification
of change
Classic MVC doesn’t work well with the modern rich client
tools because they design things so that the view handles all
the user events such as mouse and keyboard clicks. In
Model View Presenter, the view continues to handle these,
but then immediately delegates these to the presenter. The
presenter then decides what to do with the event,
communicating with the domain and the data in the view’s
controls.
- Martin Fowler
Source: http://www.martinfowler.com/eaaDev/ModelViewPresenter.html
Copyright©2008-2009 Robert Walsh - All Rights Reserved
What is Model-View-
10
Presenter?
Additional comments from Martin Fowler
Presenter
theView
theModel
View Model
thePresenter
View aView
Model aModel
Presenter aPresenter(aView, aModel);
aView->execute();
Presenter MockPresenter
View MockView
Model MockModel
SensingView
function initialize() {
view = new CustomerView();
dataSource = new CustomerDataSource();
presenter = new CustomerPresenter(view,
dataSource);
}
</script>
</head>
<body onload=”javascript:initialize()”>
...
</body>
Copyright©2008-2009 Robert Walsh - All Rights Reserved
Using MVP to Test User
27
Interfaces?
What happens when the
user selects from the drop
Select Customer
from List
customerSelected()
getCustomer()
updateFields()
updateControlStates()
CustomerPresenter.customerSelected() {
var customerId = this.view.getCustomer();
this.dataSource.getCustomer(customerId, this,
this.customerDataReceived);
}
CustomerPresenter.customerDataReceived(customer) {
this.updateFields(customer);
this.updateControlStates(customer);
}
CustomerPresenter.updateFields(customerData) {
this.view.setAddress(customerData.getAddress());
this.view.setPhoneNumber(customerData.getPhoneNumber());
...
}
CustomerPresenter.updateControlStates(customerData) {
this.view.enablePrevious(!customerData.isFirst());
this.view.enableNext(!customerData.isLast());
this.view.enableUpdate(false);
this.view.enableRevert(false);
}
Copyright©2008-2009 Robert Walsh - All Rights Reserved
Using MVP to Test User
32
Interfaces?
<html>
<head> If we are testing the Presenter...
<script type=”text/javascript”>
var view;
var presenter; we can use a Mock View and a
Mock Data Source
function initialize() {
view = new MockView();
dataSource = new MockDataSource();
presenter = new CustomerPresenter(view,
dataSource);
}
</script>
</head>
<body onload=”javascript:initialize()”>
...
The tests would simply verify that the proper
</body> methods were called in the view based on
“canned” responses from the model
Copyright©2008-2009 Robert Walsh - All Rights Reserved
Using MVP to Test User
33
Interfaces?
<html>
<head> If we are testing the View...
<script type=”text/javascript”>
var view;
var presenter; we can use a Mock Presenter
function initialize() {
view = new CustomerView();
presenter = new MockCustomerPresenter(view);
}
</script>
</head>
<body onload=”javascript:initialize()”>
...
</body> The tests would simply verify that the
proper methods were called in response
to actions initiated by the view
Presenter
ViewProxy and DlgProc Neither the Presenter
are tightly coupled nor the Model change
View Model
ViewProxy
DlgProc
View
Represents user interface
Presenter
Handles user input by interacting with the Model and
updating the View
Robert Walsh
President
EnvisionWare, Inc.
678-584-5911
rwalsh@envisionware.com
http://www.envisionware.com