Refactored the Kernel registration fluent interface to be more readable, better suppo...
[castle.git] / MonoRail / Castle.MonoRail.Framework.Tests / ViewComponents / SelectStylePaginationTestCase.cs
blob0f11a1a00e53417c45b34a4e8c6fa2004b5e6e2c
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.Tests.ViewComponents
17 using System.Collections.Generic;
18 using Castle.MonoRail.Framework.Helpers;
19 using Castle.MonoRail.Framework.ViewComponents;
20 using NUnit.Framework;
21 using Castle.MonoRail.TestSupport;
23 [TestFixture]
24 public class SelectStylePaginationTestCase : BaseViewComponentTest
26 private SelectStylePagination component;
27 private IPaginatedPage emptyPage, singlePage, secondPageOfThree;
29 [SetUp]
30 public void Init()
32 component = new SelectStylePagination();
34 emptyPage = new Page(new string[0], 1, 10, 0);
35 singlePage = new Page(new string[] { "a", "b", "c" }, 1, 4, 1);
36 secondPageOfThree = new Page(new string[] { "a", "b", "c", "d" }, 2, 4, 10);
38 BuildEngineContext("area", "controller", "action");
41 [TearDown]
42 public void Terminate()
44 CleanUp();
47 [Test, ExpectedException(typeof(ViewComponentException), "The DiggStylePagination requires a view component " +
48 "parameter named 'page' which should contain 'IPaginatedPage' instance"
50 public void ThrowsExceptionIfNoPageWasSupplied()
52 component.Page = null;
53 component.Initialize();
56 [Test]
57 public void SectionsAreCorrectlyUsedWhenSupplied()
59 List<string> actions = new List<string>();
61 SectionRender["startblock"] = delegate { actions.Add("started"); };
62 SectionRender["endblock"] = delegate { actions.Add("ended"); };
63 SectionRender["first"] = delegate { actions.Add("first"); };
64 SectionRender["last"] = delegate { actions.Add("last"); };
65 SectionRender["next"] = delegate { actions.Add("next"); };
66 SectionRender["prev"] = delegate { actions.Add("prev"); };
67 SectionRender["link"] = delegate { actions.Add("link"); };
68 SectionRender["select"] = delegate { actions.Add("select"); };
70 component.Page = singlePage;
71 Request.FilePath = "/something";
72 PrepareViewComponent(component);
73 component.Render();
75 Assert.AreEqual(7, actions.Count);
76 Assert.AreEqual("started", actions[0]);
77 Assert.AreEqual("first", actions[1]);
78 Assert.AreEqual("prev", actions[2]);
79 Assert.AreEqual("select", actions[3]);
80 Assert.AreEqual("next", actions[4]);
81 Assert.AreEqual("last", actions[5]);
82 Assert.AreEqual("ended", actions[6]);
85 [Test]
86 public void LinksAreNotRenderedWhenPageIsEmpty()
88 List<string> actions = new List<string>();
90 SectionRender["startblock"] = delegate { actions.Add("started"); };
91 SectionRender["endblock"] = delegate { actions.Add("ended"); };
92 SectionRender["first"] = delegate { actions.Add("first"); };
93 SectionRender["last"] = delegate { actions.Add("last"); };
94 SectionRender["next"] = delegate { actions.Add("next"); };
95 SectionRender["prev"] = delegate { actions.Add("prev"); };
96 SectionRender["link"] = delegate { actions.Add("link"); };
97 SectionRender["select"] = delegate { actions.Add("select"); };
99 component.Page = emptyPage;
100 Request.FilePath = "/something";
101 PrepareViewComponent(component);
102 component.Render();
104 Assert.AreEqual(7, actions.Count);
105 Assert.AreEqual("started", actions[0]);
106 Assert.AreEqual("first", actions[1]);
107 Assert.AreEqual("prev", actions[2]);
108 Assert.AreEqual("select", actions[3]);
109 Assert.AreEqual("next", actions[4]);
110 Assert.AreEqual("last", actions[5]);
111 Assert.AreEqual("ended", actions[6]);
114 [Test]
115 public void LinksAreRenderedWhenPageIsInTheMiddle()
117 List<string> actions = new List<string>();
119 SectionRender["startblock"] = delegate { actions.Add("started"); };
120 SectionRender["endblock"] = delegate { actions.Add("ended"); };
121 SectionRender["first"] = delegate { actions.Add("first"); };
122 SectionRender["last"] = delegate { actions.Add("last"); };
123 SectionRender["next"] = delegate { actions.Add("next"); };
124 SectionRender["prev"] = delegate { actions.Add("prev"); };
125 SectionRender["link"] = delegate { actions.Add("link"); };
126 SectionRender["select"] = delegate { actions.Add("select"); };
128 component.Page = secondPageOfThree;
129 Request.FilePath = "/something";
130 PrepareViewComponent(component);
131 component.Render();
133 Assert.AreEqual(11, actions.Count);
134 Assert.AreEqual("started", actions[0]);
135 Assert.AreEqual("first", actions[1]);
136 Assert.AreEqual("link", actions[2]);
137 Assert.AreEqual("prev", actions[3]);
138 Assert.AreEqual("link", actions[4]);
139 Assert.AreEqual("select", actions[5]);
140 Assert.AreEqual("next", actions[6]);
141 Assert.AreEqual("link", actions[7]);
142 Assert.AreEqual("last", actions[8]);
143 Assert.AreEqual("link", actions[9]);
144 Assert.AreEqual("ended", actions[10]);
147 [Test]
148 public void OutputNoLinksForEmptyPage()
150 component.UseInlineStyle = false;
151 component.Page = emptyPage;
152 Request.FilePath = "/something";
153 PrepareViewComponent(component);
154 component.Render();
156 Assert.AreEqual("<div class=\"pagination\">\r\n" +
157 "<span class=\"disabled\">&laquo;&laquo;</span>" +
158 "<span class=\"disabled\">&laquo;</span>" +
159 "<select onchange=\"window.location.href = this.options[this.selectedIndex].value;\">\r\n" +
160 "</select>\r\n" +
161 "<span class=\"disabled\">&raquo;</span>" +
162 "<span class=\"disabled\">&raquo;&raquo;</span>\r\n" +
163 "</div>\r\n", Output);
166 [Test]
167 public void OutputLinksForPageInTheMiddle()
169 component.UseInlineStyle = false;
170 component.Page = secondPageOfThree;
171 Request.FilePath = "/fetch";
172 PrepareViewComponent(component);
173 component.Render();
175 Assert.AreEqual("<div class=\"pagination\">\r\n" +
176 "<a href=\"/fetch?page=1\">&laquo;&laquo;</a>\r\n" +
177 "<a href=\"/fetch?page=1\">&laquo;</a>\r\n" +
178 "<select onchange=\"window.location.href = this.options[this.selectedIndex].value;\">\r\n" +
179 "\t<option value=\"/fetch?page=1\">Page 1</option>\r\n" +
180 "\t<option value=\"/fetch?page=2\" selected=\"true\">Page 2</option>\r\n" +
181 "\t<option value=\"/fetch?page=3\">Page 3</option>\r\n" +
182 "</select>\r\n" +
183 "<a href=\"/fetch?page=3\">&raquo;</a>\r\n" +
184 "<a href=\"/fetch?page=3\">&raquo;&raquo;</a>\r\n\r\n" +
185 "</div>\r\n", Output);