001: /*
002: * This file is not part of the ItsNat framework.
003: *
004: * Original source code use and closed source derivatives are authorized
005: * to third parties with no restriction or fee.
006: * The original source code is owned by the author.
007: *
008: * This program is distributed AS IS in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
011: *
012: * Author: Jose Maria Arranz Santamaria
013: * (C) Innowhere Software Services S.L., Spanish company, year 2007
014: */
015:
016: package org.itsnat.feashow.features.components.functest;
017:
018: import javax.swing.ListModel;
019: import org.itsnat.core.ItsNatDocument;
020: import org.itsnat.core.domutil.ItsNatTreeWalker;
021: import org.itsnat.core.event.DOMEvent;
022: import org.w3c.dom.Element;
023: import org.w3c.dom.events.Event;
024:
025: public class FuncTestNotBrowserExample {
026: protected FuncTestNotBrowserTreeNode treeNode;
027: protected FuncTestNotBrowserUtil util;
028:
029: public FuncTestNotBrowserExample(FuncTestNotBrowserTreeNode treeNode) {
030: this .treeNode = treeNode;
031: this .util = new FuncTestNotBrowserUtil(getItsNatDocument());
032: }
033:
034: public ItsNatDocument getItsNatDocument() {
035: return treeNode.getItsNatDocument();
036: }
037:
038: public void startTest() {
039: ItsNatDocument itsNatDoc = getItsNatDocument();
040:
041: removeAll(); // clears a previous bad test
042:
043: // In alfabetical order
044: insertCity("Barcelona", 0);
045: insertCity("Madrid", 1);
046: insertCity("Sevilla", 2);
047: insertCity("Segovia", 3);
048: insertCity("Valencia", 4);
049: insertCity("Oviedores", 5);
050:
051: // Uh!, "Valencia" is not in alfabetical order
052: removeCity(4);
053:
054: insertCity("Valencia", 5);
055:
056: // Ah! "Oviedores" is wrong
057: updateCity("Oviedorio", 4);
058:
059: // Oh No! "Oviedorio" is wrong too!
060: updateCityInPlace("Oviedo", 4);
061:
062: boolean res = checkResult();
063:
064: removeAll(); // clears
065:
066: logResult(res);
067: }
068:
069: public void insertCity(String city, int pos) {
070: fillCityInputBox(city);
071: fillPosInputBox(pos);
072: pushInsertButton();
073: }
074:
075: public void updateCity(String city, int pos) {
076: fillCityInputBox(city);
077: fillPosInputBox(pos);
078: pushUpdateButton();
079: }
080:
081: public void updateCityInPlace(String city, int pos) {
082: Element elem = editInPlace(pos);
083: updateInputBoxInPlace(elem, city);
084: }
085:
086: public Element editInPlace(int pos) {
087: ItsNatDocument itsNatDoc = getItsNatDocument();
088: Element elem = treeNode.getList().getItsNatListMultSelUI()
089: .getContentElementAt(pos);
090:
091: util.sendMouseEvent(elem, "dblclick");
092: return elem;
093: }
094:
095: public void updateInputBoxInPlace(Element parent, String value) {
096: Element inputElem = ItsNatTreeWalker
097: .getFirstChildElement(parent);
098: fillTextInput(inputElem, value);
099: util.sendHTMLEvent(inputElem, "blur");
100: }
101:
102: public void removeCity(int pos) {
103: selectCity(pos);
104: pushRemoveButton();
105: }
106:
107: public void fillCityInputBox(String city) {
108: fillTextInput(treeNode.getItemInput().getHTMLInputElement(),
109: city);
110: }
111:
112: public void fillPosInputBox(int pos) {
113: fillTextInput(treeNode.getPosInput().getHTMLInputElement(),
114: Integer.toString(pos));
115: }
116:
117: public void fillTextInput(Element elem, String value) {
118: ItsNatDocument itsNatDoc = getItsNatDocument();
119:
120: Event event = util.createHTMLEvent("change");
121: ((DOMEvent) event).setExtraParam("value", value); // ItsNatHTMLInputText transports the new value by this way
122: util.dispatchEvent(elem, event);
123: }
124:
125: public void pushInsertButton() {
126: util.clickElement(treeNode.getInsertButton()
127: .getHTMLInputElement());
128: }
129:
130: public void pushUpdateButton() {
131: util.clickElement(treeNode.getUpdateButton()
132: .getHTMLInputElement());
133: }
134:
135: public void pushRemoveButton() {
136: util.clickElement(treeNode.getRemoveButton()
137: .getHTMLInputElement());
138: }
139:
140: public void selectCity(int pos) {
141: selectCity(pos, false);
142: }
143:
144: public void selectCity(int pos, boolean shiftKey) {
145: ItsNatDocument itsNatDoc = getItsNatDocument();
146: Event event;
147:
148: Element cityElem = treeNode.getList().getItsNatListMultSelUI()
149: .getContentElementAt(pos);
150:
151: util.sendMouseEvent(cityElem, "click", shiftKey);
152: }
153:
154: public void removeAll() {
155: ItsNatDocument itsNatDoc = getItsNatDocument();
156: int size = treeNode.getList().getItsNatListMultSelUI()
157: .getLength();
158:
159: if (size == 0)
160: return;
161:
162: selectCity(0, false);
163: selectCity(size - 1, true);
164:
165: pushRemoveButton();
166: }
167:
168: public boolean checkResult() {
169: ItsNatDocument itsNatDoc = getItsNatDocument();
170:
171: ListModel model = treeNode.getList().getListModel();
172: int size = model.getSize();
173: if (size != 6)
174: return false;
175: String city = (String) model.getElementAt(4);
176: return city.equals("Oviedo");
177: }
178:
179: public void logResult(boolean res) {
180: String msg;
181: if (res)
182: msg = "Test OK";
183: else
184: msg = "Test WRONG! Don't touch anything!";
185:
186: ItsNatDocument itsNatDoc = getItsNatDocument();
187: itsNatDoc.addCodeToSend("alert(\"" + msg + "\");");
188: }
189:
190: }
|