Developing in this way is kind of strange for me (since it's new). I
can see the good things it yields by moving the logic to the
controller, and using interfaces for communicating. It makes it easier
to test, and also easier to switch the technology for the View (right
now WinForms, could easily change to
ASP.NET, since the communication
is through the interfaces).
I wasn't sure what exactly you could mock, but I think I was heading
in your direction (virtual/abstract only).
I'll post my OnStartup code. Note that I kind of abandoned this whole
concept since I couldn't get it working, and I now want to rewrite
this piece to make it mockable. Right now it's a mix of... I don't
know what!
(MainForm implements the IMainView interface)
private void MainForm_Load(object sender, EventArgs e)
{
Controller.OnStartup();
}
(OnStartup exists in the MainViewController, that takes one parameter
of type IMainView in the constructor)
(Through the View, the UI can be accessed)
public void OnStartup()
{
VistaAPI.TreeViewStyles styles = VistaAPI.TreeViewStyles.None;
styles = VistaAPI.TreeViewStyles.AutoScroll;
styles |= VistaAPI.TreeViewStyles.FadeInOut;
styles |= VistaAPI.TreeViewStyles.SemiTransparentSelection;
//I should probably add IntPtr GetLibraryHandle to the interface
and get { return Library.Handle; } in the UI?
//By the way, "Library" is the TreeView
VistaAPI.TreeView.SetStyle(View.Library.Handle, styles);
ImageList icons = TreeViewIconSet.GetIcons(View.IconPath);
//Here I am lost...
TreeViewIconSet.ApplyIcons(icons, View.Library);
}
public static ImageList GetIcons(string iconPath)
{
ImageList icons = new ImageList();
icons.Images.Add("Administration", Image.FromFile(iconPath +
@"\Administration.ico"));
icons.Images.Add("Add", Image.FromFile(iconPath + @"\Add.ico"));
icons.Images.Add("Delete", Image.FromFile(iconPath +
@"\Delete.ico"));
icons.Images.Add("Search", Image.FromFile(iconPath +
@"\Search.ico"));
icons.Images.Add("Library", Image.FromFile(iconPath +
@"\Library.ico"));
icons.Images.Add("Folder", Image.FromFile(iconPath +
@"\Folder.ico"));
icons.Images.Add("Document", Image.FromFile(iconPath +
@"\Document.ico"));
icons.Images.Add("Unlock", Image.FromFile(iconPath +
@"\Unlock.ico"));
icons.ColorDepth = ColorDepth.Depth32Bit;
return icons;
}
public static void ApplyIcons(ImageList iconList, TreeView treeView)
{
if (iconList == null || treeView == null)
throw new ArgumentNullException((iconList == null ?
"iconList" : "treeView"));
if (iconList.Images.Count != 8)
throw new ArgumentException("All icons not found in the
ImageList (should be 8)", "iconList.Images.Count");
if (treeView.Nodes.Count != 2)
throw new ArgumentException("Not enough root nodes in the
TreeView", "treeView.Nodes.Count");
else
{
if (treeView.Nodes[0].Nodes.Count != 3)
throw new ArgumentException("Not enough sub nodes in the
first node of the TreeView", "treeView.Nodes[0].Nodes.Count");
}
treeView.ImageList = iconList;
TreeNode adminRoot = treeView.Nodes[0];
adminRoot.ImageIndex = treeView.ImageList.Images.IndexOfKey
("Administration");
adminRoot.SelectedImageIndex = adminRoot.ImageIndex;
TreeNode[] adminChildren = { adminRoot.Nodes[0], adminRoot.Nodes
[1], adminRoot.Nodes[2] };
adminChildren[0].ImageIndex = treeView.ImageList.Images.IndexOfKey
("Add");
adminChildren[0].SelectedImageIndex = adminChildren[0].ImageIndex;
adminChildren[1].ImageIndex = treeView.ImageList.Images.IndexOfKey
("Delete");
adminChildren[1].SelectedImageIndex = adminChildren[1].ImageIndex;
adminChildren[2].ImageIndex = treeView.ImageList.Images.IndexOfKey
("Search");
adminChildren[2].SelectedImageIndex = adminChildren[2].ImageIndex;
TreeNode libraryRoot = treeView.Nodes[1];
libraryRoot.ImageIndex = treeView.ImageList.Images.IndexOfKey
("Library");
libraryRoot.SelectedImageIndex = libraryRoot.ImageIndex;
}
I hope I didn't scare you away with the code, haha.
Thanks for your help so far.
On Oct 31, 9:00 am, Emanuele DelBono <
codiceplast...@gmail.com> wrote:
> Usually you don't test user interface "logic", but you try to move out
> the logic in the controllers, that's why in the examples you saw the
> interfaces.
>
> Consider that of an object you can mock only the virtual and the
> abstract methods and if you take a treeview or an ImageList I don't
> know what you can mock...I think most of their members are not
> virtual.
>
> So, try to extract an interface from you UserControl that has very
> simple methods like (AddImage, GetNode, etc...) and no logic (you
> don't have if, for, foreach in the UI).
> Now you can mock the interface and test the controller logic.
>
> HTH...anyway if you want you can post some code to get a real example.
>
> emahttp://
blog.codiceplastico.com