1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.MonoRail
.Framework
.Test
18 using System
.Collections
;
19 using System
.Collections
.Specialized
;
20 using System
.Collections
.Generic
;
24 /// Used to hook a viewcomponent call to render a nested section
26 /// <param name="context">The content available to the section</param>
27 /// <param name="writer">The writer</param>
28 public delegate void TestSectionRender(IDictionary context
, TextWriter writer
);
31 /// Used to hook a viewcomponent call to render a view template
33 /// <param name="name">view name</param>
34 /// <param name="context">The content available to the view</param>
35 /// <param name="writer">The writer</param>
36 public delegate void TestViewRender(string name
, IDictionary context
, TextWriter writer
);
39 /// Represents a mock implementation of <see cref="IMockViewComponentContext"/> for unit test purposes.
41 public class MockViewComponentContext
: IMockViewComponentContext
43 private string viewToRender
;
44 private string componentName
;
45 private TextWriter writer
;
46 private IDictionary contextVars
= new HybridDictionary(true);
47 private IDictionary componentParameters
= new HybridDictionary(true);
48 private IViewEngine viewEngine
;
49 private IDictionary
<string, TestSectionRender
> section2delegate
;
52 /// Event that is raised when a section is rendered by the viewcomponent.
54 public TestSectionRender OnBodyRender
;
57 /// Event that is raised when a view is rendered by the viewcomponent.
59 public TestViewRender OnViewRender
;
62 /// Initializes a new instance of the <see cref="MockViewComponentContext"/> class.
64 protected MockViewComponentContext()
66 section2delegate
= new Dictionary
<string, TestSectionRender
>(StringComparer
.InvariantCultureIgnoreCase
);
70 /// Initializes a new instance of the <see cref="MockViewComponentContext"/> class.
72 /// <param name="componentName">Name of the component.</param>
73 /// <param name="writer">The writer.</param>
74 /// <param name="viewEngine">The view engine.</param>
75 public MockViewComponentContext(string componentName
, TextWriter writer
, IViewEngine viewEngine
) : this()
78 this.componentName
= componentName
;
79 this.viewEngine
= viewEngine
;
83 /// Gets or sets the section render dictionary.
85 /// <value>The section render.</value>
86 public IDictionary
<string, TestSectionRender
> SectionRender
88 get { return section2delegate; }
89 set { section2delegate = value; }
92 #region IViewComponentContext
95 /// Gets the name of the component.
97 /// <value>The name of the component.</value>
98 public virtual string ComponentName
100 get { return componentName; }
104 /// Determines whether the current component declaration on the view
105 /// has the specified section.
107 /// <param name="sectionName">Name of the section.</param>
109 /// <c>true</c> if the specified section exists; otherwise, <c>false</c>.
111 public bool HasSection(string sectionName
)
113 return section2delegate
.ContainsKey(sectionName
);
117 /// Renders the view specified to the writer.
119 /// <param name="name">The view template name</param>
120 /// <param name="writer">A writer to output</param>
121 public void RenderView(string name
, TextWriter writer
)
123 if (OnViewRender
!= null)
125 OnViewRender(name
, contextVars
, writer
);
130 /// Renders the component body.
132 public void RenderBody()
138 /// Renders the body into the specified <see cref="TextWriter"/>
140 /// <param name="writer">The writer.</param>
141 public void RenderBody(TextWriter writer
)
143 if (OnBodyRender
!= null)
145 OnBodyRender(contextVars
, writer
);
150 /// Renders the the specified section.
151 /// No exception will the throw if the section cannot be found.
153 /// <param name="sectionName">Name of the section.</param>
154 public void RenderSection(string sectionName
)
156 RenderSection(sectionName
, writer
);
160 /// Renders the the specified section.
161 /// No exception will the throw if the section cannot be found.
163 /// <param name="sectionName">Name of the section.</param>
164 /// <param name="writer">The writer to output the section content.</param>
165 public void RenderSection(string sectionName
, TextWriter writer
)
167 if (section2delegate
.ContainsKey(sectionName
))
169 section2delegate
[sectionName
](contextVars
, writer
);
174 /// Gets the writer used to render the view component
176 /// <value>The writer.</value>
177 public virtual TextWriter Writer
179 get { return writer; }
183 /// Gets the dictionary that holds variables for the
184 /// view and for the view component
186 /// <value>The context vars.</value>
187 public virtual IDictionary ContextVars
189 get { return contextVars; }
193 /// Gets the component parameters that the view has passed
196 /// <value>The component parameters.</value>
197 public virtual IDictionary ComponentParameters
199 get { return componentParameters; }
203 /// Gets or sets the view to render.
205 /// <value>The view to render.</value>
206 public string ViewToRender
208 get { return viewToRender; }
209 set { viewToRender = value; }
213 /// Gets the view engine instance.
215 /// <value>The view engine.</value>
216 public virtual IViewEngine ViewEngine
218 get { return viewEngine; }