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.html.HtmlElement;
006: import fitnesse.html.HtmlTag;
007: import fitnesse.html.RawHtml;
008: import fitnesse.wiki.MockWikiPage;
009:
010: import java.util.Iterator;
011: import java.util.LinkedList;
012: import java.util.List;
013:
014: public class CollapsableWidgetTest extends AbstractWidget {
015: public void testRegExp() throws Exception {
016: assertMatches("!* Some title\n content \n*!");
017: assertMatches("!*> Some title\n content \n*!");
018: assertMatches("!********** Some title\n content \n**************!");
019: assertMatches("!* title\n * list\r*!");
020:
021: assertNoMatch("!* title content *!");
022: assertNoMatch("!*missing a space\n content \n*!");
023: assertNoMatch("!* Some title\n content *!\n");
024: assertNoMatch("!* Some title\n content *!...");
025: }
026:
027: protected String getRegexp() {
028: return CollapsableWidget.REGEXP;
029: }
030:
031: public void testRender() throws Exception {
032: CollapsableWidget widget = new CollapsableWidget(
033: new MockWidgetRoot(), "!* title\ncontent\n*!");
034: String html = widget.render();
035: assertSubString("title", html);
036: assertSubString("content", html);
037: assertSubString("collapsableOpen.gif", html);
038: }
039:
040: public void testExpandedOrCollapsed() throws Exception {
041: CollapsableWidget widget = new CollapsableWidget(
042: new MockWidgetRoot(), "!* title\ncontent\n*!");
043: assertTrue(widget.expanded);
044:
045: widget = new CollapsableWidget(new MockWidgetRoot(),
046: "!*> title\ncontent\n*!");
047: assertFalse(widget.expanded);
048: }
049:
050: public void testRenderCollapsedSection() throws Exception {
051: CollapsableWidget widget = new CollapsableWidget(
052: new MockWidgetRoot(), "!*> title\ncontent\n*!");
053: String html = widget.render();
054: assertSubString("class=\"hidden\"", html);
055: assertNotSubString("class=\"collapsable\"", html);
056: assertSubString("collapsableClosed.gif", html);
057: }
058:
059: public void testTwoCollapsableSections() throws Exception {
060: String text = "!* section1\nsection1 content\n*!\n"
061: + "!* section2\nsection2 content\n*!\n";
062: WidgetRoot widgetRoot = new WidgetRoot(text, new MockWikiPage());
063: String html = widgetRoot.render();
064: assertSubString("<span class=\"meta\">section1</span>", html);
065: assertSubString("<span class=\"meta\">section2</span>", html);
066: }
067:
068: public void testEatsNewlineAtEnd() throws Exception {
069: String text = "!* section1\nsection1 content\n*!\n";
070: WidgetRoot widgetRoot = new WidgetRoot(text, new MockWikiPage());
071: String html = widgetRoot.render();
072: assertNotSubString("<br>", html);
073: }
074:
075: public void testMakeCollapsableSecion() throws Exception {
076: CollapsableWidget widget = new CollapsableWidget(
077: new MockWidgetRoot());
078: HtmlTag outerTag = widget.makeCollapsableSection(new RawHtml(
079: "title"), new RawHtml("content"));
080: assertEquals("div", outerTag.tagName());
081: assertEquals("collapse_rim", outerTag.getAttribute("class"));
082:
083: List childTags = removeNewlineTags(outerTag);
084: HtmlTag anchor = (HtmlTag) childTags.get(0);
085: assertEquals("a", anchor.tagName());
086:
087: HtmlElement title = (HtmlElement) childTags.get(1);
088: assertEquals("title", title.html());
089:
090: HtmlTag contentDiv = (HtmlTag) childTags.get(2);
091: assertEquals("div", contentDiv.tagName());
092: assertEquals("collapsable", contentDiv.getAttribute("class"));
093:
094: HtmlElement content = (HtmlElement) removeNewlineTags(
095: contentDiv).get(0);
096: assertEquals("content", content.html());
097: }
098:
099: public void testWeirdBugThatUncleBobEncountered() throws Exception {
100: try {
101: new CollapsableWidget(new MockWidgetRoot(),
102: "!* Title\n * list element\n*!\n");
103: new CollapsableWidget(new MockWidgetRoot(),
104: "!* Title\n * list element\r\n*!\n");
105: } catch (Exception e) {
106: e.printStackTrace();
107: fail("no exception expected." + e.getMessage());
108: }
109: }
110:
111: private List removeNewlineTags(HtmlTag tag) throws Exception {
112: List childTags = new LinkedList(tag.childTags);
113: for (Iterator iterator = childTags.iterator(); iterator
114: .hasNext();) {
115: HtmlElement element = (HtmlElement) iterator.next();
116: if ("".equals(element.html().trim()))
117: iterator.remove();
118: }
119: return childTags;
120: }
121:
122: }
|