001: /*
002: * Copyright 2007 Google Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005: * use this file except in compliance with the License. You may obtain a copy of
006: * the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013: * License for the specific language governing permissions and limitations under
014: * the License.
015: */
016: package com.google.gwt.user.client.ui;
017:
018: import com.google.gwt.core.client.GWT;
019: import com.google.gwt.junit.client.GWTTestCase;
020: import com.google.gwt.user.client.Element;
021: import com.google.gwt.user.client.Timer;
022: import com.google.gwt.user.client.ui.HasWidgetsTester.WidgetAdder;
023:
024: /**
025: * Tests the FormPanel.
026: *
027: * @see FormPanelTestServlet
028: */
029: public class FormPanelTest extends GWTTestCase {
030: public static boolean clicked = false;
031:
032: public String getModuleName() {
033: return "com.google.gwt.user.FormPanelTest";
034: }
035:
036: public void testAttachDetachOrder(HasWidgets container,
037: WidgetAdder adder) {
038: HasWidgetsTester.testAll(new FormPanel());
039: }
040:
041: public void testCancelSubmit() {
042: TextBox tb = new TextBox();
043: tb.setName("q");
044:
045: FormPanel form = new FormPanel();
046: form.setWidget(tb);
047: form.setAction("http://www.google.com/search");
048:
049: form.addFormHandler(new FormHandler() {
050: public void onSubmit(FormSubmitEvent event) {
051: event.setCancelled(true);
052: }
053:
054: public void onSubmitComplete(FormSubmitCompleteEvent event) {
055: fail("Form was cancelled and should not have been submitted");
056: }
057: });
058:
059: form.submit();
060: }
061:
062: /**
063: * Tests uploading a file using post & multipart encoding.
064: */
065: public void testFileUpload() {
066: final FormPanel form = new FormPanel();
067: form.setMethod(FormPanel.METHOD_POST);
068: form.setEncoding(FormPanel.ENCODING_MULTIPART);
069: assertEquals(FormPanel.ENCODING_MULTIPART, form.getEncoding());
070: form.setAction(GWT.getModuleBaseURL() + "formHandler");
071:
072: FileUpload file = new FileUpload();
073: file.setName("file0");
074: form.setWidget(file);
075:
076: RootPanel.get().add(form);
077:
078: delayTestFinish(5000);
079: form.addFormHandler(new FormHandler() {
080: public void onSubmit(FormSubmitEvent event) {
081: }
082:
083: public void onSubmitComplete(FormSubmitCompleteEvent event) {
084: // The server just echoes the contents of the request. The following
085: // string should have been present in it.
086: assertTrue(event
087: .getResults()
088: .indexOf(
089: "Content-Disposition: form-data; name=\"file0\";") != -1);
090: finishTest();
091: }
092: });
093: form.submit();
094: }
095:
096: /**
097: * Tests submitting using url-encoded get, with all form widgets (other than
098: * FileUpload, which requires post/multipart.
099: */
100: public void testMethodGet() {
101: final FormPanel form = new FormPanel();
102: form.setMethod(FormPanel.METHOD_GET);
103: form.setEncoding(FormPanel.ENCODING_URLENCODED);
104: form.setAction(GWT.getModuleBaseURL() + "formHandler");
105:
106: TextBox tb = new TextBox();
107: tb.setText("text");
108: tb.setName("tb");
109:
110: PasswordTextBox ptb = new PasswordTextBox();
111: ptb.setText("password");
112: ptb.setName("ptb");
113:
114: CheckBox cb0 = new CheckBox(), cb1 = new CheckBox();
115: cb1.setChecked(true);
116: cb0.setName("cb0");
117: cb1.setName("cb1");
118:
119: RadioButton rb0 = new RadioButton("foo");
120: RadioButton rb1 = new RadioButton("foo");
121: rb0.setChecked(true);
122: rb0.setName("rb0");
123: rb1.setName("rb1");
124:
125: ListBox lb = new ListBox();
126: lb.addItem("option0");
127: lb.addItem("option1");
128: lb.addItem("option2");
129: lb.setValue(0, "value0");
130: lb.setValue(1, "value1");
131: lb.setValue(2, "value2");
132: lb.setSelectedIndex(1);
133: lb.setName("lb");
134:
135: Hidden h = new Hidden("h", "v");
136:
137: FlowPanel panel = new FlowPanel();
138: panel.add(tb);
139: panel.add(ptb);
140: panel.add(cb0);
141: panel.add(cb1);
142: panel.add(rb0);
143: panel.add(rb1);
144: panel.add(lb);
145: panel.add(h);
146: form.setWidget(panel);
147: RootPanel.get().add(form);
148:
149: delayTestFinish(5000);
150:
151: form.addFormHandler(new FormHandler() {
152: public void onSubmit(FormSubmitEvent event) {
153: }
154:
155: public void onSubmitComplete(FormSubmitCompleteEvent event) {
156: // The server just echoes the query string. This is what it should look
157: // like.
158: assertTrue(event
159: .getResults()
160: .equals(
161: "tb=text&ptb=password&cb1=on&rb0=on&lb=value1&h=v"));
162: finishTest();
163: }
164: });
165:
166: form.submit();
167: }
168:
169: public void testSubmitAndHideDialog() {
170: final FormPanel form = new FormPanel();
171: form.setMethod(FormPanel.METHOD_GET);
172: form.setEncoding(FormPanel.ENCODING_URLENCODED);
173: form.setAction(GWT.getModuleBaseURL() + "formHandler");
174:
175: TextBox tb = new TextBox();
176: form.add(tb);
177: tb.setText("text");
178: tb.setName("tb");
179:
180: final DialogBox dlg = new DialogBox();
181: dlg.setWidget(form);
182: dlg.show();
183:
184: delayTestFinish(5000);
185: form.addFormHandler(new FormHandler() {
186: public void onSubmit(FormSubmitEvent event) {
187: }
188:
189: public void onSubmitComplete(FormSubmitCompleteEvent event) {
190: // Make sure we get our results back.
191: assertTrue(event.getResults().equals("tb=text"));
192: finishTest();
193:
194: // Hide the dialog on submit complete. This was causing problems at one
195: // point because hiding the dialog detached the form and iframe.
196: dlg.hide();
197: }
198: });
199:
200: form.submit();
201: }
202:
203: /**
204: * Tests submitting an alternate frame.
205: */
206: public void testSubmitFrame() {
207: final NamedFrame frame = new NamedFrame("myFrame");
208: FormPanel form = new FormPanel(frame);
209: form.setMethod(FormPanel.METHOD_POST);
210: form.setAction(GWT.getModuleBaseURL()
211: + "formHandler?sendHappyHtml");
212: RootPanel.get().add(form);
213: RootPanel.get().add(frame);
214:
215: delayTestFinish(10000);
216: Timer t = new Timer() {
217: public void run() {
218: // Make sure the frame got the contents we expected.
219: assertTrue(isHappyDivPresent(frame.getElement()));
220: finishTest();
221: }
222:
223: private native boolean isHappyDivPresent(Element iframe) /*-{
224: return !!iframe.contentWindow.document.getElementById(':)');
225: }-*/;
226: };
227:
228: // Wait 5 seconds before checking the results.
229: t.schedule(5000);
230: form.submit();
231: }
232: }
|