Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9471d5d801f31b2c78e20e3e3ae8f3fe3cb18580 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*******************************************************************************
 * Copyright (c) 2008, 2010 Oracle. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0, which accompanies this distribution
 * and is available at http://www.eclipse.org/legal/epl-v10.html.
 *
 * Contributors:
 *     Oracle - initial API and implementation
 ******************************************************************************/
package org.eclipse.jpt.common.ui.tests.internal.util;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import org.eclipse.jpt.common.ui.internal.util.ControlSwitcher;
import org.eclipse.jpt.common.ui.internal.util.SWTUtil;
import org.eclipse.jpt.utility.internal.ReflectionTools;
import org.eclipse.jpt.utility.internal.Transformer;
import org.eclipse.jpt.utility.internal.model.value.SimplePropertyValueModel;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.part.PageBook;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

@SuppressWarnings("nls")
public final class ControlSwitcherTest {

	private PageBook pageBook;
	private Composite pane1;
	private Composite pane2;
	private Composite parent;

	private Composite buildPane1() {

		if (pane1 == null) {

			pane1 = new Composite(pageBook, SWT.NULL);
			pane1.setLayout(new GridLayout(2, false));

			Label label = new Label(pane1, SWT.NULL);
			label.setText("&Test2:");

			Text text = new Text(pane1, SWT.BORDER);
			text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

			Combo combo = new Combo(pane1, SWT.BORDER);

			GridData data = new GridData(GridData.FILL_HORIZONTAL);
			data.horizontalSpan = 2;
			combo.setLayoutData(data);
		}

		return pane1;
	}

	private Composite buildPane2() {

		if (pane2 == null) {

			pane2 = new Composite(pageBook, SWT.NULL);
			pane2.setLayout(new GridLayout(2, false));

			Label label = new Label(pane2, SWT.NULL);
			label.setText("&Test1:");

			Text text = new Text(pane2, SWT.BORDER);
			text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		}

		return pane2;
	}

	private Transformer<Boolean, Control> buildTransformer() {
		return new Transformer<Boolean, Control>() {
			public Control transform(Boolean value) {
				return (value == null) ? null : (value ? pane1 : pane2);
			}
		};
	}

	@Before
	public void setUp() {
		parent = new Composite(SWTUtil.getShell(), SWT.NONE);
		parent.setLayout(new GridLayout());

		pageBook = new PageBook(parent, SWT.NULL);
		pageBook.setLayoutData(new GridData());
	}

	@After
	public void tearDown() {

		if (parent != null) {

			parent.dispose();

			parent   = null;
			pageBook = null;
		}
	}

	@Test
	public void testSwitch() {

		SimplePropertyValueModel<Boolean> switchHolder = new SimplePropertyValueModel<Boolean>();
		Transformer<Boolean, Control> transformer = buildTransformer();

		pane1 = buildPane1();
		pane1.setVisible(false);

		pane2 = buildPane2();
		pane2.setVisible(false);

		new ControlSwitcher(
			switchHolder,
			transformer,
			pageBook
		);

		// Test 1
		switchHolder.setValue(true);
		Control control = (Control) ReflectionTools.getFieldValue(pageBook, "currentPage");

		assertNotNull(
			"The page book's page shouldn't be null",
			control
		);

		assertSame(
			"The current pane should be pane1",
			pane1,
			control
		);

		Point pane1Size = pane1.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		Point pageBookSize = pageBook.computeSize(SWT.DEFAULT, SWT.DEFAULT);

		assertEquals(
			"The width of the PageBook should be the same as the width of pane1",
			pane1Size.x,
			pageBookSize.x
		);

		assertEquals(
			"The height of the PageBook should be the same as the height of pane1",
			pane1Size.y,
			pageBookSize.y
		);

		// Test 2
		switchHolder.setValue(false);
		control = (Control) ReflectionTools.getFieldValue(pageBook, "currentPage");

		assertNotNull(
			"The page book's page shouldn't be null",
			control
		);

		assertSame(
			"The current pane should be pane2",
			pane2,
			control
		);

		Point pane2Size = pane2.computeSize(SWT.DEFAULT, SWT.DEFAULT);
		pageBookSize = pageBook.computeSize(SWT.DEFAULT, SWT.DEFAULT);

		assertEquals(
			"The width of the PageBook should be the same as the width of pane2",
			pane2Size.x,
			pageBookSize.x
		);

		assertEquals(
			"The height of the PageBook should be the same as the height of pane2",
			pane2Size.y,
			pageBookSize.y
		);
	}
}

Back to the top