001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.wikitext.widgets;
004:
005: import fitnesse.wiki.InMemoryPage;
006: import fitnesse.wiki.PageCrawler;
007: import fitnesse.wiki.PathParser;
008: import fitnesse.wiki.WikiPage;
009:
010: public class ContentsTreeWidgetTest extends AbstractWidget {
011: private WikiPage root;
012:
013: private WikiPage parent;
014:
015: private WikiPage childOne;
016:
017: private WikiPage singleChildParent;
018:
019: private PageCrawler crawler;
020:
021: public void setUp() throws Exception {
022: root = InMemoryPage.makeRoot("RooT");
023: crawler = root.getPageCrawler();
024: parent = crawler.addPage(root, PathParser.parse("ParentOne"),
025: "parent");
026: childOne = crawler.addPage(parent,
027: PathParser.parse("ChildOne"), "content");
028: crawler.addPage(childOne, PathParser.parse("ChildOfChildOne"),
029: "content");
030: crawler
031: .addPage(parent, PathParser.parse("ChildTwo"),
032: "content");
033: crawler.addPage(parent, PathParser.parse("ChildThree"),
034: "content");
035: crawler.addPage(root, PathParser.parse("ParentTwo"),
036: "parent two");
037:
038: singleChildParent = crawler.addPage(root, PathParser
039: .parse("SingleChildParent"), "parent");
040: crawler.addPage(singleChildParent, PathParser
041: .parse("SingleChild"), "whatever");
042: }
043:
044: public void testStartCollapsedRendering() throws Exception {
045: String rendered = renderCollapsedHierarchicalContentsTreeWidget();
046: assertSubString("<img ", rendered);
047: assertNotSubString(ContentsTreeWidget.MINUS_IMAGE, rendered);
048: assertSubString(ContentsTreeWidget.PLUS_IMAGE, rendered);
049: }
050:
051: public void testMinusImagePresent() throws Exception {
052: String rendered = renderHierarchicalContentsTreeWidget();
053: assertSubString(ContentsTreeWidget.MINUS_IMAGE, rendered);
054: }
055:
056: public void testLastLinkImagePresent() throws Exception {
057: String rendered = renderHierarchicalContentsTreeWidget();
058: assertSubString(ContentsTreeWidget.LAST_LINK_IMAGE, rendered);
059: }
060:
061: public void testLinkImagePresent() throws Exception {
062: String rendered = renderHierarchicalContentsTreeWidget();
063: assertSubString(ContentsTreeWidget.LINK_IMAGE, rendered);
064: }
065:
066: public void testStaticImagePresent() throws Exception {
067: String rendered = renderNormalContentsTreeWidget();
068: assertSubString(ContentsTreeWidget.STATIC_IMAGE_CLASS, rendered);
069: }
070:
071: public void testToggleImageJavascriptAnchor() throws Exception {
072: String rendered = renderHierarchicalContentsTreeWidget();
073: assertSubString("<img ", rendered);
074: assertSubString(ContentsTreeWidget.TOGGLE_IMAGE_CLASS, rendered);
075: assertSubString("<a href=\"javascript:toggleTreeNode(",
076: rendered);
077: }
078:
079: public void testToggleImagePresent() throws Exception {
080: String rendered = renderHierarchicalContentsTreeWidget();
081: assertSubString(ContentsTreeWidget.TOGGLE_IMAGE_CLASS, rendered);
082: }
083:
084: public void testOuterDivPresent() throws Exception {
085: String rendered = renderNormalContentsTreeWidget();
086: assertSubString(ContentsTreeWidget.OUTER_DIV_CLASS, rendered);
087: }
088:
089: public void testHierarchicalNodes() throws Exception {
090: addGrandChild();
091: addGreatGrandChild();
092: String rendered = renderNormalContentsTreeWidget(parent);
093: // refactor me!
094: int p1 = rendered.indexOf(ContentsTreeWidget.NODE_DIV_CLASS);
095: assertTrue(rendered, p1 != -1);
096: int p2 = rendered
097: .indexOf(ContentsTreeWidget.NODE_DIV_CLASS, p1);
098: assertTrue(rendered, p2 != -1);
099: p1 = rendered.indexOf(ContentsTreeWidget.NODE_DIV_CLASS, p2);
100: assertTrue(rendered, p1 != -1);
101: p2 = rendered.indexOf(ContentsTreeWidget.NODE_DIV_CLASS, p1);
102: assertTrue(rendered, p2 != -1);
103: }
104:
105: public void testNodeDivForTwoChildren() throws Exception {
106: String rendered = renderNormalContentsTreeWidget(parent);
107: int p1 = rendered.indexOf(ContentsTreeWidget.NODE_DIV_CLASS);
108: assertTrue(rendered, p1 != -1);
109: int p2 = rendered
110: .indexOf(ContentsTreeWidget.NODE_DIV_CLASS, p1);
111: assertTrue(rendered, p2 != -1);
112: }
113:
114: public void testNodeDivForOneChild() throws Exception {
115: String rendered = renderNormalContentsTreeWidget(singleChildParent);
116: assertSubString(ContentsTreeWidget.NODE_DIV_CLASS, rendered);
117: }
118:
119: public void testLinkForOneChild() throws Exception {
120: String rendered = renderNormalContentsTreeWidget(singleChildParent);
121: assertSubString(ContentsTreeWidget.NODE_DIV_CLASS, rendered);
122: assertSubString(
123: "<a href=\"SingleChildParent.SingleChild\">"
124: + "<img src=\"/files/images/treeview/folder.gif\" "
125: + "width=\"22\" height=\"16\" class=\"treeIcon\" alt=\"Container\"/>SingleChild</a>",
126: rendered);
127: }
128:
129: public void testLinksForMultipleChildren() throws Exception {
130: String rendered = renderNormalContentsTreeWidget();
131: assertSubString(ContentsTreeWidget.NODE_DIV_CLASS, rendered);
132: assertSubString(
133: "<a href=\"ParentOne.ChildOne\">"
134: + "<img src=\"/files/images/treeview/folder.gif\" "
135: + "width=\"22\" height=\"16\" class=\"treeIcon\" alt=\"Container\"/>ChildOne</a>",
136: rendered);
137: assertSubString(
138: "<a href=\"ParentOne.ChildTwo\">"
139: + "<img src=\"/files/images/treeview/folder.gif\" "
140: + "width=\"22\" height=\"16\" class=\"treeIcon\" alt=\"Container\"/>ChildTwo</a>",
141: rendered);
142: assertSubString(
143: "<a href=\"ParentOne.ChildThree\">"
144: + "<img src=\"/files/images/treeview/folder.gif\" "
145: + "width=\"22\" height=\"16\" class=\"treeIcon\" alt=\"Container\"/>ChildThree</a>",
146: rendered);
147: }
148:
149: public void testContentsTreeOnRoot() throws Exception {
150: ContentsTreeWidget widget = new ContentsTreeWidget(
151: new WidgetRoot(root), "!contentstree\n");
152: String html = widget.render();
153: assertHasRegexp("ParentOne", html);
154: assertHasRegexp("ParentTwo", html);
155: }
156:
157: public void testIsNotHierarchical() throws Exception {
158: assertFalse(new ContentsTreeWidget(new WidgetRoot(parent),
159: "!contentstree\n").isRecursive());
160: }
161:
162: public void testIsHierarchical() throws Exception {
163: assertTrue(new ContentsTreeWidget(new WidgetRoot(parent),
164: "!contentstree -R\n").isRecursive());
165: }
166:
167: public void testStartCollapsed() throws Exception {
168: ContentsTreeWidget widget = new ContentsTreeWidget(
169: new WidgetRoot(root), "!contentstree -RC\n");
170: assertTrue(widget.startCollapsed() == true);
171: widget = new ContentsTreeWidget(new WidgetRoot(root),
172: "!contentstree -R\n");
173: assertTrue(widget.startCollapsed() == false);
174: }
175:
176: public void testMatch() throws Exception {
177: assertMatchEquals("!contentstree\n", "!contentstree");
178: assertMatchEquals("!contentstree -R\n", "!contentstree -R");
179: assertMatchEquals("!contentstree -RC\n", "!contentstree -RC");
180: assertMatchEquals("!contentstree\r", "!contentstree");
181: assertMatchEquals("!contentstree -R\r", "!contentstree -R");
182: assertMatchEquals("!contentstree -RC\r", "!contentstree -RC");
183: assertMatchEquals(" !contentstree\n", null);
184: assertMatchEquals(" !contentstree -R\n", null);
185: assertMatchEquals("!contentstree zap\n", null);
186: assertMatchEquals("!contentstree \n", "!contentstree ");
187: }
188:
189: private String renderNormalContentsTreeWidget(WikiPage parent)
190: throws Exception {
191: return new ContentsTreeWidget(new WidgetRoot(parent),
192: "!contentstree\n").render();
193: }
194:
195: private String renderNormalContentsTreeWidget() throws Exception {
196: return new ContentsTreeWidget(new WidgetRoot(parent),
197: "!contentstree\n").render();
198: }
199:
200: private String renderHierarchicalContentsTreeWidget()
201: throws Exception {
202: return new ContentsTreeWidget(new WidgetRoot(parent),
203: "!contentstree -R\n").render();
204: }
205:
206: private String renderCollapsedHierarchicalContentsTreeWidget()
207: throws Exception {
208: return new ContentsTreeWidget(new WidgetRoot(parent),
209: "!contentstree -RC\n").render();
210: }
211:
212: private void addGrandChild() throws Exception {
213: crawler.addPage(parent.getChildPage("ChildOne"), PathParser
214: .parse("GrandChild"), "content");
215: }
216:
217: private void addGreatGrandChild() throws Exception {
218: crawler.addPage(parent.getChildPage("ChildOne").getChildPage(
219: "GrandChild"), PathParser.parse("GreatGrandChild"),
220: "content");
221: }
222:
223: protected String getRegexp() {
224: return ContentsTreeWidget.REGEXP;
225: }
226: }
|