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.ClientDocument;
020: import org.itsnat.core.ItsNatDocument;
021: import org.itsnat.core.domutil.ItsNatTreeWalker;
022: import org.itsnat.core.script.ScriptUtil;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.events.Event;
025:
026: public class FuncTestUsingBrowserExample {
027: protected FuncTestUsingBrowserTreeNode treeNode;
028: protected FuncTestUsingBrowserUtil util;
029:
030: public FuncTestUsingBrowserExample(
031: FuncTestUsingBrowserTreeNode treeNode) {
032: this .treeNode = treeNode;
033: this .util = new FuncTestUsingBrowserUtil(getItsNatDocument());
034: }
035:
036: public ItsNatDocument getItsNatDocument() {
037: return treeNode.getItsNatDocument();
038: }
039:
040: public void startTest() {
041: ItsNatDocument itsNatDoc = getItsNatDocument();
042: Runnable testCode = new Runnable() {
043: public void run() {
044: removeAll(); // clears a previous bad test
045:
046: // In alfabetical order
047: insertCity("Barcelona", 0);
048: insertCity("Madrid", 1);
049: insertCity("Sevilla", 2);
050: insertCity("Segovia", 3);
051: insertCity("Valencia", 4);
052: insertCity("Oviedores", 5);
053:
054: // Uh!, "Valencia" is not in alfabetical order
055: removeCity(4);
056:
057: insertCity("Valencia", 5);
058:
059: // Ah! "Oviedores" is wrong
060: updateCity("Oviedorio", 4);
061:
062: // Oh No! "Oviedorio" is wrong too!
063: updateCityInPlace("Oviedo", 4);
064:
065: boolean res = checkResult();
066:
067: removeAll(); // clears
068:
069: logResult(res);
070: }
071: };
072: ClientDocument client = itsNatDoc.getClientDocumentOwner();
073: client.startEventDispatcherThread(testCode);
074: }
075:
076: public void insertCity(String city, int pos) {
077: fillCityInputBox(city);
078: fillPosInputBox(pos);
079: pushInsertButton();
080: }
081:
082: public void updateCity(String city, int pos) {
083: fillCityInputBox(city);
084: fillPosInputBox(pos);
085: pushUpdateButton();
086: }
087:
088: public void updateCityInPlace(String city, int pos) {
089: Element elem = editInPlace(pos);
090: updateInputBoxInPlace(elem, city);
091: }
092:
093: public Element editInPlace(int pos) {
094: ItsNatDocument itsNatDoc = getItsNatDocument();
095: Element elem;
096: synchronized (itsNatDoc) {
097: elem = treeNode.getList().getItsNatListMultSelUI()
098: .getContentElementAt(pos);
099: }
100:
101: util.sendMouseEvent(elem, "dblclick");
102: return elem;
103: }
104:
105: public void updateInputBoxInPlace(Element parent, String value) {
106: Element inputElem = ItsNatTreeWalker
107: .getFirstChildElement(parent);
108: fillTextInput(inputElem, value);
109: util.sendHTMLEvent(inputElem, "blur");
110: }
111:
112: public void removeCity(int pos) {
113: selectCity(pos);
114: pushRemoveButton();
115: }
116:
117: public void fillCityInputBox(String city) {
118: fillTextInput(treeNode.getItemInput().getHTMLInputElement(),
119: city);
120: }
121:
122: public void fillPosInputBox(int pos) {
123: fillTextInput(treeNode.getPosInput().getHTMLInputElement(),
124: Integer.toString(pos));
125: }
126:
127: public void fillTextInput(Element elem, String value) {
128: ItsNatDocument itsNatDoc = getItsNatDocument();
129: synchronized (itsNatDoc) {
130: ScriptUtil script = itsNatDoc.getScriptUtil();
131: itsNatDoc.addCodeToSend(script.getSetPropertyCode(elem,
132: "value", value, true));
133: }
134:
135: util.sendHTMLEvent(elem, "change");
136: }
137:
138: public void pushInsertButton() {
139: util.clickElement(treeNode.getInsertButton()
140: .getHTMLInputElement());
141: }
142:
143: public void pushUpdateButton() {
144: util.clickElement(treeNode.getUpdateButton()
145: .getHTMLInputElement());
146: }
147:
148: public void pushRemoveButton() {
149: util.clickElement(treeNode.getRemoveButton()
150: .getHTMLInputElement());
151: }
152:
153: public void selectCity(int pos) {
154: selectCity(pos, false);
155: }
156:
157: public void selectCity(int pos, boolean shiftKey) {
158: ItsNatDocument itsNatDoc = getItsNatDocument();
159: Event event;
160:
161: Element cityElem;
162: synchronized (itsNatDoc) {
163: cityElem = treeNode.getList().getItsNatListMultSelUI()
164: .getContentElementAt(pos);
165: }
166:
167: util.sendMouseEvent(cityElem, "click", shiftKey);
168: }
169:
170: public void removeAll() {
171: ItsNatDocument itsNatDoc = getItsNatDocument();
172: int size;
173: synchronized (itsNatDoc) {
174: size = treeNode.getList().getItsNatListMultSelUI()
175: .getLength();
176: }
177: if (size == 0)
178: return;
179:
180: selectCity(0, false);
181: selectCity(size - 1, true);
182:
183: pushRemoveButton();
184: }
185:
186: public boolean checkResult() {
187: ItsNatDocument itsNatDoc = getItsNatDocument();
188: synchronized (itsNatDoc) {
189: ListModel model = treeNode.getList().getListModel();
190: int size = model.getSize();
191: if (size != 6)
192: return false;
193: String city = (String) model.getElementAt(4);
194: return city.equals("Oviedo");
195: }
196: }
197:
198: public void logResult(boolean res) {
199: String msg;
200: if (res)
201: msg = "Test OK";
202: else
203: msg = "Test WRONG! Don't touch anything!";
204:
205: ItsNatDocument itsNatDoc = getItsNatDocument();
206: synchronized (itsNatDoc) {
207: itsNatDoc.addCodeToSend("alert(\"" + msg + "\");");
208: }
209: }
210:
211: }
|