1 // Copyright 2004-2008 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
.Tests
.ViewComponents
17 using System
.Collections
;
18 using System
.Collections
.Generic
;
20 using Castle
.MonoRail
.Framework
.Helpers
;
21 using Castle
.MonoRail
.Framework
.ViewComponents
;
22 using Castle
.MonoRail
.TestSupport
;
23 using NUnit
.Framework
;
26 public class DiggStylePaginationTestCase
: BaseViewComponentTest
28 private DiggStylePagination diggComponent
;
29 private IPaginatedPage singlePage
, secondPageOfThree
;
34 diggComponent
= new DiggStylePagination();
36 singlePage
= new Page(new string[] {"a", "b", "c"}
, 1, 4, 1);
37 secondPageOfThree
= new Page(new string[] {"a", "b", "c", "d"}
, 2, 4, 10);
39 BuildEngineContext("area", "controller", "action");
43 public void Terminate()
48 [Test
, ExpectedException(typeof(ViewComponentException
), "The DiggStylePagination requires a view component " +
49 "parameter named 'page' which should contain 'IPaginatedPage' instance"
51 public void ThrowsExceptionIfNoPageWasSupplied()
53 diggComponent
.Page
= null;
54 diggComponent
.Initialize();
58 public void PageWithNoLinksInvokesStartAndEndSections()
60 List
<string> actions
= new List
<string>();
62 SectionRender
["startblock"] = delegate { actions.Add("started"); }
;
63 SectionRender
["endblock"] = delegate { actions.Add("ended"); }
;
64 SectionRender
["link"] = delegate { actions.Add("link"); }
;
66 diggComponent
.Page
= singlePage
;
67 Request
.FilePath
= "/something";
68 PrepareViewComponent(diggComponent
);
69 diggComponent
.Render();
71 Assert
.AreEqual(2, actions
.Count
);
72 Assert
.AreEqual("started", actions
[0]);
73 Assert
.AreEqual("ended", actions
[1]);
77 public void PageWithLinksInvokesStartAndEndAndLinkSections()
79 List
<string> actions
= new List
<string>();
81 SectionRender
["startblock"] = delegate { actions.Add("started"); }
;
82 SectionRender
["endblock"] = delegate { actions.Add("ended"); }
;
83 SectionRender
["link"] = delegate { actions.Add("link"); }
;
85 diggComponent
.Page
= secondPageOfThree
;
86 Request
.FilePath
= "/something";
87 PrepareViewComponent(diggComponent
);
88 diggComponent
.Render();
90 Assert
.AreEqual(6, actions
.Count
);
91 Assert
.AreEqual("started", actions
[0]);
92 Assert
.AreEqual("link", actions
[1]);
93 Assert
.AreEqual("link", actions
[2]);
94 Assert
.AreEqual("link", actions
[3]);
95 Assert
.AreEqual("link", actions
[4]);
96 Assert
.AreEqual("ended", actions
[5]);
100 public void PageWithNoLinksPrintsNoLinks()
102 SectionRender
["startblock"] = delegate(IDictionary context
, TextWriter writer
) { writer.Write("started"); }
;
103 SectionRender
["endblock"] = delegate(IDictionary context
, TextWriter writer
) { writer.Write("ended"); }
;
105 diggComponent
.UseInlineStyle
= false;
106 diggComponent
.Page
= singlePage
;
107 Request
.FilePath
= "/something";
108 PrepareViewComponent(diggComponent
);
109 diggComponent
.Render();
111 Assert
.AreEqual("started<span class=\"disabled\">« prev</span>\r\n" +
112 "<span class=\"current\">1</span>\r\n" +
113 "<span class=\"disabled\">next »</span>ended", Output
);
117 public void PageWithPrevNextPrintsCustomizedLinks()
119 SectionRender
["startblock"] = delegate(IDictionary context
, TextWriter writer
) { writer.Write("started"); }
;
120 SectionRender
["endblock"] = delegate(IDictionary context
, TextWriter writer
) { writer.Write("ended"); }
;
121 SectionRender
["prev"] = delegate(IDictionary context
, TextWriter writer
) { writer.Write("customprev"); }
;
122 SectionRender
["next"] = delegate(IDictionary context
, TextWriter writer
) { writer.Write("customnext"); }
;
124 diggComponent
.UseInlineStyle
= false;
125 diggComponent
.Page
= singlePage
;
126 Request
.FilePath
= "/something";
127 PrepareViewComponent(diggComponent
);
128 diggComponent
.Render();
130 Assert
.AreEqual("started<span class=\"disabled\">customprev</span>\r\n" +
131 "<span class=\"current\">1</span>\r\n" +
132 "<span class=\"disabled\">customnext</span>ended", Output
);
136 public void PageWithLinksPrintsLinks()
138 SectionRender
["startblock"] = delegate(IDictionary context
, TextWriter writer
) { writer.Write("started"); }
;
139 SectionRender
["endblock"] = delegate(IDictionary context
, TextWriter writer
) { writer.Write("ended"); }
;
140 SectionRender
["link"] = delegate(IDictionary context
, TextWriter writer
)
142 writer
.Write(" <{0} {1} {2}> ", context
["pageIndex"], context
["url"], context
["text"]);
145 diggComponent
.UseInlineStyle
= false;
146 diggComponent
.Page
= secondPageOfThree
;
147 Request
.FilePath
= "/something";
148 PrepareViewComponent(diggComponent
);
149 diggComponent
.Render();
151 Assert
.AreEqual("started <1 /something?page=1 « prev> " +
152 "<1 /something?page=1 1> \r\n<span class=\"current\">2</span>\r\n " +
153 "<3 /something?page=3 3> <3 /something?page=3 next »> ended", Output
);
157 public void SupportsSections()
159 Assert
.IsTrue(diggComponent
.SupportsSection("startblock"), "Supports startblock Section");
160 Assert
.IsTrue(diggComponent
.SupportsSection("endblock"), "Supports endblock Section");
161 Assert
.IsTrue(diggComponent
.SupportsSection("link"), "Supports Links Section");
162 Assert
.IsFalse(diggComponent
.SupportsSection("NotSupported"), "Unsupported section");