001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: * Brad Reynolds - bug 116920
011: *******************************************************************************/package org.eclipse.jface.tests.databinding.scenarios;
012:
013: import org.eclipse.core.databinding.DataBindingContext;
014: import org.eclipse.core.databinding.beans.BeansObservables;
015: import org.eclipse.jface.databinding.swt.SWTObservables;
016: import org.eclipse.jface.examples.databinding.model.Account;
017: import org.eclipse.jface.examples.databinding.model.Adventure;
018: import org.eclipse.jface.examples.databinding.model.SampleData;
019: import org.eclipse.jface.examples.databinding.model.Transportation;
020: import org.eclipse.jface.tests.databinding.BindingTestSuite;
021: import org.eclipse.swt.SWT;
022: import org.eclipse.swt.widgets.Label;
023: import org.eclipse.swt.widgets.Text;
024:
025: import com.ibm.icu.text.NumberFormat;
026:
027: /**
028: * To run the tests in this class, right-click and select "Run As JUnit Plug-in
029: * Test". This will also start an Eclipse instance. To clean up the launch
030: * configuration, open up its "Main" tab and select "[No Application] - Headless
031: * Mode" as the application to run.
032: */
033:
034: public class TextControlScenario extends ScenariosTestCase {
035:
036: private Text text;
037:
038: private Adventure adventure;
039:
040: private Transportation transportation;
041:
042: Account account;
043:
044: protected void setUp() throws Exception {
045: super .setUp();
046: // do any setup work here
047: adventure = SampleData.WINTER_HOLIDAY;
048: transportation = SampleData.EXECUTIVE_JET;
049: account = SampleData.PRESIDENT;
050: text = new Text(getComposite(), SWT.BORDER);
051: }
052:
053: protected void tearDown() throws Exception {
054: text.dispose();
055: text = null;
056: super .tearDown();
057: }
058:
059: public void testScenario01() {
060: // Bind the adventure "name" property to a text field
061: // Change the UI and verify the model changes
062: // Change the model and verify the UI changes
063: getDbc().bindValue(
064: SWTObservables.observeText(text, SWT.Modify),
065: BeansObservables.observeValue(adventure, "name"), null,
066: null);
067:
068: assertEquals(adventure.getName(), text.getText());
069: text.setText("England");
070: text.notifyListeners(SWT.FocusOut, null);
071: assertEquals("England", adventure.getName());
072: adventure.setName("France");
073: assertEquals("France", text.getText());
074: adventure.setName("Germany");
075: spinEventLoop(0);
076: assertEquals("Germany", text.getText());
077: }
078:
079: public void testScenario02() {
080:
081: // Bind the transportation "price" property to a text field
082: // This is a Double.TYPE so we check that conversion and validation
083: // occurs
084: // Change the UI and verify the model changes
085: // Change the model and verify the UI changes
086: getDbc().bindValue(
087: SWTObservables.observeText(text, SWT.Modify),
088: BeansObservables.observeValue(transportation, "price"),
089: null, null);
090:
091: NumberFormat numberFormat = NumberFormat.getInstance();
092:
093: assertEquals(numberFormat.format(transportation.getPrice()),
094: text.getText());
095: text.setText("9876.54");
096: text.notifyListeners(SWT.FocusOut, null);
097: assertEquals(9876.54, transportation.getPrice(), 0);
098:
099: transportation.setPrice(1234.56);
100: assertEquals(numberFormat.format(transportation.getPrice()),
101: text.getText());
102: }
103:
104: // public void testScenario03() {
105: // // Show that the Escape key can be pressed in the middle of editing and
106: // // the value will revert
107: // // the updatePolicy for this test is TIME_LATE so it occurs when focus
108: // // is lost from the Text control
109: // getDbc().bindValue(SWTObservables.observeText(text, SWT.FocusOut),
110: // BeansObservables.observeValue(adventure, "name"),
111: // null, null);
112: //
113: // String currentText = text.getText();
114: // text.setText("Switzerland");
115: // // We do not notify FocusOut
116: // // Verify that the model hasn't changed
117: // assertEquals(currentText, adventure.getName());
118: // Event event = new Event();
119: // event.character = SWT.ESC;
120: // event.keyCode = 27;
121: // text.notifyListeners(SWT.KeyDown, event);
122: // // Verify that the text has reverted
123: // assertEquals(currentText, text.getText());
124: // // And that the model didn't change
125: // assertEquals(adventure.getName(), currentText);
126: // // Now change the GUI and commit this change
127: // currentText = "Austria";
128: // text.setText(currentText);
129: // text.notifyListeners(SWT.FocusOut, null);
130: // assertEquals(text.getText(), adventure.getName());
131: // // Now change the text again and press escape a second time
132: // text.setText("Turkey");
133: // // Send escape
134: // text.notifyListeners(SWT.KeyDown, event);
135: // // Verify it has reverted to "Austria" and not any other value, i.e. the
136: // // last value it displayed
137: // assertEquals(currentText, text.getText());
138: //
139: // }
140:
141: // public void testScenario04() {
142: // // Show that the Escape key can be pressed in the middle of editing and
143: // // the value will revert
144: // // the updatePolicy for this test is TIME_EARLY so it occurs when each
145: // // keystroke occurs
146: // getDbc().bindValue(SWTObservables.observeText(text, SWT.Modify),
147: // BeansObservables.observeValue(adventure, "name"),
148: // null, null);
149: //
150: // String originalName = adventure.getName();
151: // // Change the text field character by character and ensure that the
152: // // model changes
153: // String newName = "Switzerland";
154: // for (int i = 0; i < newName.length(); i++) {
155: // text.setText(newName.substring(0, i + 1));
156: // // Verify the model has changed
157: // assertEquals(newName.substring(0, i + 1), adventure.getName());
158: // }
159: //
160: // // Now send an escape key and verify that the model reverts
161: // Event event = new Event();
162: // event.character = SWT.ESC;
163: // event.keyCode = 27;
164: // text.notifyListeners(SWT.KeyDown, event);
165: // assertEquals(adventure.getName(), originalName);
166: //
167: // // Now send "Austria" key by key
168: // newName = "Austria";
169: // for (int i = 0; i < newName.length(); i++) {
170: // text.setText(newName.substring(0, i + 1));
171: // // Verify the model has changed
172: // assertEquals(newName.substring(0, i + 1), adventure.getName());
173: // }
174: // // Send a focus lost event to commit the change
175: // text.notifyListeners(SWT.FocusOut, null);
176: // // Send an escape key
177: // text.notifyListeners(SWT.KeyDown, event);
178: // // Verify that the model has changed and has not reverted
179: // assertEquals(newName, adventure.getName());
180: // }
181:
182: /**
183: * public void testScenario05(){ // Show that nesting of properties works.
184: * Adventure has defaultLodging and Lodging has name getDbc().bind(text,new
185: * Property(adventure,"defaultLodging.name"),null); // Verify the GUI is
186: * showing the model value
187: * assertEquals(text.getText(),adventure.getDefaultLodging().getName()); }
188: */
189: public void testScenario06() {
190: // // Show that partial validation works for TIME_EARLY
191: // // We are using TIME_EARLY to verify that invalid states are not sent
192: // to the model
193: // getSWTObservableFactory().setUpdateTime(DataBindingContext.TIME_EARLY);
194: // getDbc().bind(text, new Property(account, "phone"), new BindSpec(new
195: // PhoneConverter(),new PhoneValidator()));
196: // // Verify we have no error message for partial validation or full
197: // validation yet
198: // assertTrue(((String)getDbc().getPartialValidationMessage().getValue()).length()
199: // == 0);
200: // assertTrue(((String)getDbc().getValidationMessage().getValue()).length()
201: // == 0);
202: // // Update some of the phone number
203: // String originalPhoneNumber = account.getPhone();
204: // text.setText("999");
205: // // Verify that the phone number is partially invalid and there is no
206: // validation message
207: // assertTrue(((String)getDbc().getPartialValidationMessage().getValue()).length()
208: // > 0);
209: // assertTrue(((String)getDbc().getValidationMessage().getValue()).length()
210: // == 0);
211: // // And that the model has not changed
212: // assertEquals(account.getPhone(),originalPhoneNumber);
213: // // Verify that fixing the phone removes the error and the model is
214: // updated too
215: // text.setText("999-888-7777");
216: // assertTrue(((String)getDbc().getPartialValidationMessage().getValue()).length()
217: // == 0);
218: // assertEquals(account.getPhone(),"9998887777");
219: }
220:
221: public void testScenario07() {
222: // // Show that partial validation works for TIME_LATE
223: // getSWTObservableFactory().setUpdateTime(DataBindingContext.TIME_LATE);
224: // getDbc().bind(text, new Property(account, "phone"), new BindSpec(new
225: // PhoneConverter(),new PhoneValidator()));
226: // // Update some of the phone number
227: // String originalPhoneNumber = account.getPhone();
228: // text.setText("222");
229: // // Verify that we have no completion validation message and a partial
230: // one
231: // assertTrue(((String)getDbc().getPartialValidationMessage().getValue()).length()
232: // > 0);
233: // assertTrue(((String)getDbc().getValidationMessage().getValue()).length()
234: // == 0);
235: // // Fix the error
236: // text.setText("222-333-4444");
237: // // Verify that the errors are both fixed
238: // assertTrue(((String)getDbc().getPartialValidationMessage().getValue()).length()
239: // == 0);
240: // assertTrue(((String)getDbc().getValidationMessage().getValue()).length()
241: // == 0);
242: // // The model should not be changed
243: // assertEquals(originalPhoneNumber,account.getPhone());
244: // // Lose focus and verify that the complete validation message is
245: // fixed
246: // text.notifyListeners(SWT.FocusOut,null);
247: // assertTrue(((String)getDbc().getValidationMessage().getValue()).length()
248: // == 0);
249: // // The model should be changed
250: // assertEquals("2223334444",account.getPhone());
251: }
252:
253: public void testScenario08() {
254:
255: if (BindingTestSuite.failingTestsDisabled(this )) {
256: return;
257: }
258:
259: // Show that the CustomBeanBindSupportFactory will automatically pick up
260: // the
261: // validator on the MaxNumberOfPeople property
262:
263: DataBindingContext dbc = getDbc();
264:
265: dbc.bindValue(SWTObservables.observeText(text, SWT.Modify),
266: BeansObservables.observeValue(adventure,
267: "maxNumberOfPeople"),
268: new CustomBeanUpdateValueStrategy(), null);
269:
270: // make sure we can set a value inside the validator's range
271: text.setText("4");
272: assertEquals(4, adventure.getMaxNumberOfPeople());
273: // Now try to set a value outside the validator's range
274: text.setText("999");
275: assertEquals(4, adventure.getMaxNumberOfPeople());
276: dbc.dispose();
277: }
278:
279: public void testScenario09() {
280: // Verify direct binding between a Text and Label following bugzilla
281: // 118696
282: Label label = new Label(getComposite(), SWT.NONE);
283: getDbc().bindValue(
284: SWTObservables.observeText(text, SWT.FocusOut),
285: SWTObservables.observeText(label), null, null);
286:
287: // Change the text
288: text.setText("Frog");
289: // Verify the label does not change
290: assertTrue(label.getText().length() == 0);
291: // Lose focus from the text field
292: text.notifyListeners(SWT.FocusOut, null);
293: assertEquals(label.getText(), "Frog");
294:
295: }
296:
297: public void testScenario10() {
298: // Verify direct binding between a Text and Label following bugzilla
299: // 118696 with TIME_EARLY
300: Label label = new Label(getComposite(), SWT.NONE);
301: getDbc().bindValue(
302: SWTObservables.observeText(text, SWT.Modify),
303: SWTObservables.observeText(label), null, null);
304:
305: // Change the text
306: String newTextValue = "Frog";
307: for (int i = 0; i < newTextValue.length(); i++) {
308: text.setText(newTextValue.substring(0, i + 1));
309: // Verify the label has changed key by key
310: assertEquals(text.getText(), label.getText());
311: }
312: // Lose focus
313: text.notifyListeners(SWT.FocusOut, null);
314: // Verify the text and label are the same following a lose focus
315: assertEquals(text.getText(), label.getText());
316: }
317:
318: }
|