Skip to content

Commit a801237

Browse files
sebthomHannesWell
authored andcommitted
fix: setting minimum > 100 via ScaleFieldEditor's ctor arg is ignored
1 parent 0bf8ce2 commit a801237

File tree

3 files changed

+194
-1
lines changed

3 files changed

+194
-1
lines changed

bundles/org.eclipse.jface/src/org/eclipse/jface/preference/ScaleFieldEditor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,10 @@ private void updateScale() {
317317
if (scale != null && !scale.isDisposed()) {
318318
scale.setMinimum(getMinimum());
319319
scale.setMaximum(getMaximum());
320+
// Reapplying the minimum to ensure that the scale's value is correctly
321+
// adjusted in scenarios where the new minimum exceeds the previous maximum.
322+
scale.setMinimum(getMinimum());
323+
320324
scale.setIncrement(getIncrement());
321325
scale.setPageIncrement(getPageIncrement());
322326
}

tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/preferences/AllPrefsTests.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,12 @@
1818
import org.junit.runners.Suite;
1919

2020
@RunWith(Suite.class)
21-
@Suite.SuiteClasses({ BooleanFieldEditorTest.class, StringFieldEditorTest.class, IntegerFieldEditorTest.class })
21+
@Suite.SuiteClasses({ //
22+
BooleanFieldEditorTest.class, //
23+
StringFieldEditorTest.class, //
24+
IntegerFieldEditorTest.class, //
25+
ScaleFieldEditorTest.class //
26+
})
2227
public class AllPrefsTests {
2328

2429
public static void main(String[] args) {
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Sebastian Thomschke and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Sebastian Thomschke - initial API and implementation
13+
*******************************************************************************/
14+
package org.eclipse.jface.tests.preferences;
15+
16+
import static org.junit.Assert.*;
17+
18+
import org.eclipse.jface.preference.PreferenceStore;
19+
import org.eclipse.jface.preference.ScaleFieldEditor;
20+
import org.eclipse.swt.SWT;
21+
import org.eclipse.swt.widgets.Scale;
22+
import org.eclipse.swt.widgets.Shell;
23+
import org.junit.After;
24+
import org.junit.Before;
25+
import org.junit.Test;
26+
27+
public class ScaleFieldEditorTest {
28+
29+
private static final String PREFKEY = "scaleValue";
30+
31+
private Shell shell;
32+
private ScaleFieldEditor editor;
33+
34+
@Before
35+
public void setUp() {
36+
shell = new Shell(SWT.NONE);
37+
editor = new ScaleFieldEditor(PREFKEY, "Test Scale", shell);
38+
}
39+
40+
@After
41+
public void tearDown() {
42+
if (shell != null && !shell.isDisposed()) {
43+
shell.dispose();
44+
}
45+
}
46+
47+
@Test
48+
public void testConstructorArgs() {
49+
// determine default OS-specific max (on Windows = 100)
50+
final int defaultMax;
51+
{
52+
final var scale = new Scale(shell, 0);
53+
defaultMax = scale.getMaximum();
54+
scale.dispose();
55+
}
56+
57+
// choose a new min that is higher than the default max to test proper bounds
58+
// handling when setting min to a value that is larger than the OS default max
59+
final var newMin = defaultMax + 1;
60+
final var newMax = defaultMax + 100;
61+
62+
editor.dispose();
63+
editor = new ScaleFieldEditor(PREFKEY, "Test Scale", shell, newMin, newMax, 3, 9);
64+
65+
assertEquals(newMin, editor.getMinimum());
66+
assertEquals(newMin, editor.getScaleControl().getMinimum());
67+
assertEquals(newMax, editor.getMaximum());
68+
assertEquals(newMax, editor.getScaleControl().getMaximum());
69+
70+
assertEquals(3, editor.getIncrement());
71+
assertEquals(3, editor.getScaleControl().getIncrement());
72+
73+
assertEquals(9, editor.getScaleControl().getPageIncrement());
74+
assertEquals(9, editor.getPageIncrement());
75+
}
76+
77+
@Test
78+
public void testGetterAndSetter() {
79+
final Scale widget = editor.getScaleControl();
80+
81+
assertNotNull(widget);
82+
83+
assertEquals(10, editor.getMaximum());
84+
assertEquals(10, widget.getMaximum());
85+
editor.setMaximum(999);
86+
assertEquals(999, editor.getMaximum());
87+
assertEquals(999, widget.getMaximum());
88+
89+
assertEquals(0, editor.getMinimum());
90+
assertEquals(0, widget.getMinimum());
91+
editor.setMinimum(99);
92+
assertEquals(99, editor.getMinimum());
93+
assertEquals(99, widget.getMinimum());
94+
95+
assertEquals(1, editor.getIncrement());
96+
assertEquals(1, widget.getIncrement());
97+
editor.setIncrement(9);
98+
assertEquals(9, editor.getIncrement());
99+
assertEquals(9, widget.getIncrement());
100+
101+
assertEquals(1, editor.getPageIncrement());
102+
assertEquals(1, widget.getPageIncrement());
103+
editor.setPageIncrement(99);
104+
assertEquals(99, editor.getPageIncrement());
105+
assertEquals(99, widget.getPageIncrement());
106+
107+
assertTrue(editor.getLabelControl(shell).isEnabled());
108+
assertTrue(widget.isEnabled());
109+
editor.setEnabled(false, shell);
110+
assertFalse(editor.getLabelControl(shell).isEnabled());
111+
assertFalse(widget.isEnabled());
112+
}
113+
114+
@Test
115+
public void testLoad() {
116+
final Scale widget = editor.getScaleControl();
117+
assertEquals(0, widget.getSelection());
118+
119+
final var prefStoreWithDefault = new PreferenceStore();
120+
prefStoreWithDefault.setDefault(PREFKEY, 2);
121+
122+
editor.setPreferenceStore(prefStoreWithDefault);
123+
124+
editor.load();
125+
assertEquals(2, widget.getSelection());
126+
127+
final var prefStoreWithDefaultAndValue = new PreferenceStore();
128+
prefStoreWithDefaultAndValue.setDefault(PREFKEY, 2);
129+
prefStoreWithDefaultAndValue.setValue(PREFKEY, 4);
130+
131+
editor.setPreferenceStore(prefStoreWithDefaultAndValue);
132+
133+
editor.load();
134+
assertEquals(4, widget.getSelection());
135+
}
136+
137+
@Test
138+
public void testLoadDefault() {
139+
final Scale widget = editor.getScaleControl();
140+
assertEquals(0, widget.getSelection());
141+
142+
final var prefStoreWithDefaultAndValue = new PreferenceStore();
143+
prefStoreWithDefaultAndValue.setDefault(PREFKEY, 2);
144+
prefStoreWithDefaultAndValue.setValue(PREFKEY, 4);
145+
146+
editor.setPreferenceStore(prefStoreWithDefaultAndValue);
147+
148+
editor.load();
149+
assertEquals(4, widget.getSelection());
150+
151+
editor.loadDefault();
152+
assertEquals(2, widget.getSelection());
153+
}
154+
155+
@Test
156+
public void testSetValueInWidget() {
157+
final Scale widget = editor.getScaleControl();
158+
assertEquals(0, widget.getSelection());
159+
160+
final int min = 100;
161+
final int max = 103;
162+
163+
editor.setMinimum(min);
164+
editor.setMaximum(max);
165+
166+
// by setting the minimum bound the selection changes accordingly
167+
assertEquals(min, widget.getSelection());
168+
169+
widget.setSelection(min - 1); // outside lower bound
170+
assertEquals(min, widget.getSelection());
171+
172+
widget.setSelection(min);
173+
assertEquals(min, widget.getSelection());
174+
175+
widget.setSelection(min + 1);
176+
assertEquals(min + 1, widget.getSelection());
177+
178+
widget.setSelection(max);
179+
assertEquals(max, widget.getSelection());
180+
181+
widget.setSelection(max + 1); // outside upper bound
182+
assertEquals(max, widget.getSelection());
183+
}
184+
}

0 commit comments

Comments
 (0)