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.beans.BeansObservables;
014: import org.eclipse.jface.databinding.swt.SWTObservables;
015: import org.eclipse.jface.examples.databinding.model.Adventure;
016: import org.eclipse.jface.examples.databinding.model.SampleData;
017: import org.eclipse.swt.SWT;
018: import org.eclipse.swt.widgets.Button;
019:
020: /**
021: * To run the tests in this class, right-click and select "Run As JUnit Plug-in
022: * Test". This will also start an Eclipse instance. To clean up the launch
023: * configuration, open up its "Main" tab and select "[No Application] - Headless
024: * Mode" as the application to run.
025: */
026:
027: public class ButtonControlScenario extends ScenariosTestCase {
028:
029: private Adventure adventure;
030:
031: private Button button;
032:
033: protected void setUp() throws Exception {
034: super .setUp();
035: // do any setup work here
036: button = new Button(getComposite(), SWT.CHECK);
037: adventure = SampleData.WINTER_HOLIDAY;
038: }
039:
040: protected void tearDown() throws Exception {
041: button.dispose();
042: super .tearDown();
043: }
044:
045: public void testScenario01() {
046: // Bind the button's selection to the adventure "isPetsAllowed"
047: getDbc()
048: .bindValue(
049: SWTObservables.observeSelection(button),
050: BeansObservables.observeValue(adventure,
051: "petsAllowed"), null, null);
052:
053: // Check the model and GUI are in the same state
054: assertEquals(button.getSelection(), adventure.isPetsAllowed());
055: // Change the model and check the GUI is updated
056: boolean newBoolean = !adventure.isPetsAllowed();
057: adventure.setPetsAllowed(newBoolean);
058: assertEquals(newBoolean, adventure.isPetsAllowed());
059: assertEquals(button.getSelection(), newBoolean);
060: // Change the GUI and check the model
061: newBoolean = !newBoolean;
062: button.setSelection(newBoolean);
063: button.notifyListeners(SWT.Selection, null);
064: assertEquals(newBoolean, adventure.isPetsAllowed());
065: newBoolean = !newBoolean;
066: final boolean finalNewBoolean = newBoolean;
067: adventure.setPetsAllowed(finalNewBoolean);
068: spinEventLoop(0);
069: assertEquals(newBoolean, button.getSelection());
070:
071: }
072:
073: public void testScenario02() {
074: // Test with an SWT.Toggle button
075: button.dispose();
076: button = new Button(getComposite(), SWT.TOGGLE);
077: // Bind the button's selection to the adventure "isPetsAllowed"
078: getDbc()
079: .bindValue(
080: SWTObservables.observeSelection(button),
081: BeansObservables.observeValue(adventure,
082: "petsAllowed"), null, null);
083:
084: // Check the model and GUI are in the same state
085: assertEquals(button.getSelection(), adventure.isPetsAllowed());
086: // Change the model and check the GUI is updated
087: boolean newBoolean = !adventure.isPetsAllowed();
088: adventure.setPetsAllowed(newBoolean);
089: assertEquals(newBoolean, adventure.isPetsAllowed());
090: assertEquals(button.getSelection(), newBoolean);
091: // Change the GUI and check the model
092: newBoolean = !newBoolean;
093: button.setSelection(newBoolean);
094: button.notifyListeners(SWT.Selection, null);
095: assertEquals(newBoolean, adventure.isPetsAllowed());
096: }
097:
098: public void testScenario03() {
099: // Test with an SWT.Radio button
100: button.dispose();
101: button = new Button(getComposite(), SWT.RADIO);
102:
103: // Bind the button's selection to the adventure "isPetsAllowed"
104: getDbc()
105: .bindValue(
106: SWTObservables.observeSelection(button),
107: BeansObservables.observeValue(adventure,
108: "petsAllowed"), null, null);
109:
110: // Check the model and GUI are in the same state
111: assertEquals(button.getSelection(), adventure.isPetsAllowed());
112: // Change the model and check the GUI is updated
113: boolean newBoolean = !adventure.isPetsAllowed();
114: adventure.setPetsAllowed(newBoolean);
115: assertEquals(newBoolean, adventure.isPetsAllowed());
116: assertEquals(button.getSelection(), newBoolean);
117: // Change the GUI and check the model
118: newBoolean = !newBoolean;
119: button.setSelection(newBoolean);
120: button.notifyListeners(SWT.Selection, null);
121: assertEquals(newBoolean, adventure.isPetsAllowed());
122: }
123: }
|