// Copyright 2004-2008 Castle Project - http://www.castleproject.org/ // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. namespace Castle.MonoRail.TestSupport { using System; using System.Collections.Generic; using System.IO; using Castle.MonoRail.Framework; using Castle.MonoRail.Framework.Test; /// /// Base class to test view components. /// /// /// /// The following test makes sure the component rendered the inner sections correctly. /// /// [TestFixture] /// public class DiggStylePaginationTestCase : BaseViewComponentTest /// { /// private DiggStylePagination diggComponent; /// private IPaginatedPage singlePage, secondPageOfThree; /// /// [SetUp] /// public void Init() /// { /// diggComponent = new DiggStylePagination(); /// singlePage = new Page(new string[] { "a", "b", "c" }, 1, 4, 1); /// secondPageOfThree = new Page(new string[] { "a", "b", "c", "d" }, 2, 4, 10); /// } /// /// [TearDown] /// public void Terminate() /// { /// CleanUp(); /// } /// /// [Test] /// public void PageWithNoLinksInvokesStartAndEndSections() /// { /// List<string> actions = new List<string>(); /// // pass mock inner sections to component /// SectionRender["startblock"] = delegate(IDictionary context, TextWriter writer) { actions.Add("started"); }; /// SectionRender["endblock"] = delegate(IDictionary context, TextWriter writer) { actions.Add("ended"); }; /// SectionRender["link"] = delegate(IDictionary context, TextWriter writer) { actions.Add("link"); }; /// /// diggComponent.Page = singlePage; /// /// PrepareViewComponent(diggComponent); /// diggComponent.Render(); /// /// // make sure component "rendered" inner sections /// Assert.AreEqual(2, actions.Count); /// Assert.AreEqual("started", actions[0]); /// Assert.AreEqual("ended", actions[1]); /// } /// } /// /// /// /// /// You must call before testing a view component instance /// and you should call after each test case (use the TearDown). /// public abstract class BaseViewComponentTest : BaseControllerTest { private IMockViewComponentContext componentContext; private IViewEngine viewEngine; private StringWriter writer; /// /// Use this dictionary to add inner sections as available inner sections to /// the view component. /// public IDictionary SectionRender; /// /// This delegate is called when the viewcomponent renders its body. /// public TestSectionRender OnBodyRender; /// /// This delegate is called when the viewcomponent renders a view /// public TestViewRender OnViewRender; /// /// Initializes a new instance of the class. /// protected BaseViewComponentTest() { CleanUp(); } /// /// Initialize the view component with mock services it needs to /// be functional. /// /// The component instance. protected void PrepareViewComponent(ViewComponent component) { if (Context == null) { BuildEngineContext("", "Controller", "Action"); } viewEngine = BuildViewEngine(); componentContext = BuildViewComponentContext(component.GetType().Name); component.Init(Context, componentContext); } /// /// Gets the output -- ie what the viewcomponent wrote to the output stream. /// /// The output. protected string Output { get { return writer.ToString(); } } /// /// Cleans the up all state created to test a view component. /// protected void CleanUp() { writer = new StringWriter(); SectionRender = new Dictionary(StringComparer.InvariantCultureIgnoreCase); OnBodyRender = null; OnViewRender = null; } /// /// Builds the view engine. /// /// protected virtual IViewEngine BuildViewEngine() { return new ViewEngineStub(".view", ".jsview", true); } /// /// Builds the view component context. /// /// Name of the view component. /// protected virtual IMockViewComponentContext BuildViewComponentContext(string viewComponentName) { StubViewComponentContext compContext = new StubViewComponentContext(viewComponentName, writer, viewEngine); compContext.SectionRender = SectionRender; compContext.OnBodyRender = OnBodyRender; compContext.OnViewRender = OnViewRender; return compContext; } } }