01: /*******************************************************************************
02: * Copyright (c) 2005, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: * Brad Reynolds - bug 116920
11: *******************************************************************************/package org.eclipse.jface.tests.databinding.scenarios;
12:
13: import org.eclipse.core.databinding.DataBindingContext;
14: import org.eclipse.core.databinding.beans.BeansObservables;
15: import org.eclipse.jface.databinding.swt.SWTObservables;
16: import org.eclipse.jface.examples.databinding.model.Adventure;
17: import org.eclipse.jface.examples.databinding.model.PriceModelObject;
18: import org.eclipse.jface.examples.databinding.model.SampleData;
19: import org.eclipse.swt.SWT;
20: import org.eclipse.swt.widgets.Spinner;
21:
22: /**
23: * To run the tests in this class, right-click and select "Run As JUnit Plug-in
24: * Test". This will also start an Eclipse instance. To clean up the launch
25: * configuration, open up its "Main" tab and select "[No Application] - Headless
26: * Mode" as the application to run.
27: */
28:
29: public class CustomConverterScenarios extends ScenariosTestCase {
30:
31: private Adventure skiTrip;
32:
33: protected void setUp() throws Exception {
34: super .setUp();
35: skiTrip = SampleData.WINTER_HOLIDAY;
36: }
37:
38: protected void tearDown() throws Exception {
39: // do any teardown work here
40: super .tearDown();
41: }
42:
43: public void testScenario01() {
44: DataBindingContext dbc = getDbc();
45: Spinner spinner_dollars = new Spinner(getComposite(), SWT.NONE);
46: spinner_dollars.setMaximum(10000);
47: Spinner spinner_cents = new Spinner(getComposite(), SWT.NONE);
48:
49: // The price object is a double which contains both the dollars and
50: // cents
51: // To allow this to be worked on with two separate spinner controls
52: // (which get and set int values)
53: // an intermediate object is used
54: PriceModelObject priceModel = new PriceModelObject();
55:
56: dbc.bindValue(BeansObservables
57: .observeValue(priceModel, "price"), BeansObservables
58: .observeValue(skiTrip, "price"), null, null);
59:
60: dbc.bindValue(SWTObservables.observeSelection(spinner_dollars),
61: BeansObservables.observeValue(priceModel, "dollars"),
62: null, null);
63:
64: dbc.bindValue(SWTObservables.observeSelection(spinner_cents),
65: BeansObservables.observeValue(priceModel, "cents"),
66: null, null);
67:
68: // spinEventLoop(1);
69: // Make sure that the selection on the spinner_dollars matches the
70: // dollars of the price
71: assertEquals(spinner_dollars.getSelection(), new Double(skiTrip
72: .getPrice()).intValue());
73: // Make sure that the selection on the spinner_cents matches the dollars
74: // of the price
75: Double doublePrice = new Double(skiTrip.getPrice());
76: double cents = 100 * doublePrice.doubleValue() - 100
77: * doublePrice.intValue();
78: assertEquals(spinner_cents.getSelection(), (int) cents);
79:
80: // Change the selection on the spinner_dollars to be $50 and make sure
81: // the model is updated with the cents included
82: spinner_dollars.setSelection(50);
83: double expectedPrice = 50 + cents / 100;
84: assertEquals(expectedPrice, skiTrip.getPrice(), 0.01);
85:
86: // Change the selection on the spinner_cents to be 27 and make sure the
87: // model is updated with the dollars included
88: spinner_cents.setSelection(27);
89: assertEquals(50.27, skiTrip.getPrice(), 0.01);
90:
91: // Change the model to be $60.99 dollars and make sure the
92: // spinner_dollars is 60 and spinner_cents is 99
93: skiTrip.setPrice(60.99);
94: assertEquals(60, spinner_dollars.getSelection());
95: assertEquals(99, spinner_cents.getSelection());
96:
97: }
98:
99: }
|