11
11
12
12
import static org .junit .jupiter .api .Assertions .assertTrue ;
13
13
14
+ import java .util .ArrayList ;
14
15
import java .util .Collection ;
16
+ import java .util .List ;
15
17
import java .util .concurrent .atomic .AtomicBoolean ;
16
18
17
19
import org .eclipse .swt .widgets .Composite ;
22
24
import org .eclipse .jface .viewers .ComboBoxCellEditor ;
23
25
import org .eclipse .ui .PlatformUI ;
24
26
25
- import org .eclipse .draw2d .Figure ;
26
27
import org .eclipse .draw2d .IFigure ;
27
28
import org .eclipse .draw2d .geometry .Point ;
28
29
29
30
import org .eclipse .gef .EditDomain ;
30
31
import org .eclipse .gef .EditPart ;
32
+ import org .eclipse .gef .EditPartViewer ;
31
33
import org .eclipse .gef .GraphicalEditPart ;
32
34
import org .eclipse .gef .Request ;
33
35
import org .eclipse .gef .commands .Command ;
34
- import org .eclipse .gef .editparts . AbstractGraphicalEditPart ;
36
+ import org .eclipse .gef .test . utils . TestGraphicalEditPart ;
35
37
import org .eclipse .gef .tools .DirectEditManager ;
36
38
import org .eclipse .gef .ui .parts .AbstractEditPartViewer ;
37
39
40
+ import org .junit .jupiter .api .BeforeEach ;
38
41
import org .junit .jupiter .api .Test ;
39
42
40
43
public class DirectEditManagerTest {
44
+ private Shell shell ;
45
+ private EditDomain editDomain ;
46
+ private TestDirectEditManager editManager ;
47
+ private List <Throwable > throwables ;
48
+
49
+ @ BeforeEach
50
+ public void setUp () {
51
+ shell = PlatformUI .getWorkbench ().getDisplay ()
52
+ .syncCall (() -> PlatformUI .getWorkbench ().getDisplay ().getActiveShell ());
53
+ editDomain = new EditDomain ();
54
+ throwables = new ArrayList <>();
55
+ editManager = null ;
56
+ }
41
57
42
- @ SuppressWarnings ("static-method" )
43
58
@ Test
44
59
public void testComboCellEditorChangeProcessed () {
45
- Shell shell = PlatformUI .getWorkbench ().getDisplay ()
46
- .syncCall (() -> PlatformUI .getWorkbench ().getDisplay ().getActiveShell ());
47
- EditDomain editDomain = new EditDomain ();
48
- TestDirectEditManager editManager = new TestDirectEditManager (createDummyEditPart (shell , editDomain ));
49
60
AtomicBoolean comboChangeCommandSubmitted = new AtomicBoolean ();
50
61
editDomain .getCommandStack ().addCommandStackEventListener (event -> comboChangeCommandSubmitted .set (true ));
51
62
63
+ testWith (new DirectGraphicalEditPart () {
64
+ @ Override
65
+ public Command getCommand (Request request ) {
66
+ return new Command () {
67
+ };
68
+ }
69
+ });
70
+
71
+ assertTrue (comboChangeCommandSubmitted .get ());
72
+ assertTrue (throwables .isEmpty (), () -> "Internal exception occurred: " + throwables ); //$NON-NLS-1$
73
+ }
74
+
75
+ @ Test
76
+ public void testComboCellEditorDisposedTwice () {
77
+ testWith (new DirectGraphicalEditPart () {
78
+ @ Override
79
+ public Command getCommand (Request request ) {
80
+ return new Command () {
81
+ @ Override
82
+ public void execute () {
83
+ editManager .bringDown ();
84
+ }
85
+ };
86
+ }
87
+ });
88
+
89
+ assertTrue (throwables .isEmpty (), () -> "Internal exception occurred: " + throwables ); //$NON-NLS-1$
90
+ }
91
+
92
+ private void testWith (GraphicalEditPart editPart ) {
93
+ editManager = new TestDirectEditManager (editPart );
94
+
52
95
PlatformUI .getWorkbench ().getDisplay ().syncExec (() -> {
53
96
editManager .show ();
54
97
// Change value of combo cell editor
55
98
editManager .getCellEditor ().setValue (0 );
56
99
// Make combo cell editor lose focus to apply its value
57
100
shell .setFocus ();
58
101
});
59
-
60
- assertTrue (comboChangeCommandSubmitted .get ());
61
102
}
62
103
63
- private static class TestDirectEditManager extends DirectEditManager {
104
+ private class TestDirectEditManager extends DirectEditManager {
64
105
public TestDirectEditManager (GraphicalEditPart source ) {
65
106
super (source , ComboBoxCellEditor .class , cellEditor -> {
66
107
});
@@ -73,7 +114,22 @@ protected CellEditor createCellEditorOn(Composite composite) {
73
114
74
115
@ Override
75
116
public CellEditor getCellEditor () {
76
- return super .getCellEditor ();
117
+ try {
118
+ return super .getCellEditor ();
119
+ } catch (Throwable exception ) {
120
+ throwables .add (exception );
121
+ throw exception ;
122
+ }
123
+ }
124
+
125
+ @ Override
126
+ public void bringDown () {
127
+ try {
128
+ super .bringDown ();
129
+ } catch (Throwable exception ) {
130
+ throwables .add (exception );
131
+ throw exception ;
132
+ }
77
133
}
78
134
79
135
@ Override
@@ -85,49 +141,32 @@ protected void initCellEditor() {
85
141
}
86
142
}
87
143
88
- private static GraphicalEditPart createDummyEditPart (Control control , EditDomain editDomain ) {
89
- return new AbstractGraphicalEditPart () {
90
- @ Override
91
- protected void createEditPolicies () {
92
- }
93
-
94
- @ Override
95
- public org .eclipse .gef .EditPartViewer getViewer () {
96
- return new AbstractEditPartViewer () {
97
- @ Override
98
- public EditPart findObjectAtExcluding (Point location , Collection <IFigure > exclusionSet ,
99
- Conditional conditional ) {
100
- return null ;
101
- }
102
-
103
- @ Override
104
- public Control createControl (Composite parent ) {
105
- return parent ;
106
- }
107
-
108
- @ Override
109
- public Control getControl () {
110
- return control ;
111
- }
112
-
113
- @ Override
114
- public org .eclipse .gef .EditDomain getEditDomain () {
115
- return editDomain ;
116
- }
117
- };
118
- }
119
-
120
- @ Override
121
- public Command getCommand (Request request ) {
122
- return new Command () {
123
- };
124
- }
125
-
126
- @ Override
127
- protected IFigure createFigure () {
128
- return new Figure ();
129
- }
130
- };
144
+ private class DirectGraphicalEditPart extends TestGraphicalEditPart {
145
+ @ Override
146
+ public EditPartViewer getViewer () {
147
+ return new AbstractEditPartViewer () {
148
+ @ Override
149
+ public EditPart findObjectAtExcluding (Point location , Collection <IFigure > exclusionSet ,
150
+ Conditional conditional ) {
151
+ return null ;
152
+ }
153
+
154
+ @ Override
155
+ public Control createControl (Composite parent ) {
156
+ return parent ;
157
+ }
158
+
159
+ @ Override
160
+ public Control getControl () {
161
+ return shell ;
162
+ }
163
+
164
+ @ Override
165
+ public EditDomain getEditDomain () {
166
+ return editDomain ;
167
+ }
168
+ };
169
+ }
131
170
}
132
171
133
172
}
0 commit comments