1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.testutils.mockers;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.fail;
|
---|
5 |
|
---|
6 | import java.awt.Component;
|
---|
7 | import java.awt.GraphicsEnvironment;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Map;
|
---|
10 | import java.util.Optional;
|
---|
11 | import java.util.WeakHashMap;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
---|
14 | import org.openstreetmap.josm.tools.Logging;
|
---|
15 |
|
---|
16 | import mockit.Deencapsulation;
|
---|
17 | import mockit.Invocation;
|
---|
18 | import mockit.Mock;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * MockUp for {@link ExtendedDialog} allowing a test to pre-seed uses of {@link ExtendedDialog}
|
---|
22 | * with mock "responses". This works best with {@link ExtendedDialog}s which have their contents set
|
---|
23 | * through {@link ExtendedDialog#setContent(String)} as simple strings. In such a case, responses can
|
---|
24 | * be defined through a mapping from content {@link String}s to button indexes ({@link Integer}s) or
|
---|
25 | * button labels ({@link String}s). Example:
|
---|
26 | *
|
---|
27 | * <pre>
|
---|
28 | * new ExtendedDialogMocker(ImmutableMap.<String, Object>builder()
|
---|
29 | * .put("JOSM version 8,001 required for plugin baz_plugin.", "Download Plugin")
|
---|
30 | * .put("JOSM version 7,001 required for plugin dummy_plugin.", "Cancel")
|
---|
31 | * .put("Are you sure you want to do foo bar?", ExtendedDialog.DialogClosedOtherwise)
|
---|
32 | * .build()
|
---|
33 | * );
|
---|
34 | * </pre>
|
---|
35 | *
|
---|
36 | * Testing examples with more complicated contents would require overriding
|
---|
37 | * {@link #getString(ExtendedDialog)} or even {@link #getMockResult(ExtendedDialog)} with custom logic.
|
---|
38 | * The class is implemented as a number of small methods with the main aim being to allow overriding of
|
---|
39 | * only the parts necessary for a particular case.
|
---|
40 | *
|
---|
41 | * The default {@link #getMockResult(ExtendedDialog)} will raise an
|
---|
42 | * {@link AssertionError} on an {@link ExtendedDialog} activation without a
|
---|
43 | * matching mapping entry or if the named button doesn't exist.
|
---|
44 | *
|
---|
45 | * The public {@link #getMockResultMap()} method returns the modifiable result map to allow for situations
|
---|
46 | * where the desired result might need to be changed mid-test.
|
---|
47 | */
|
---|
48 | public class ExtendedDialogMocker extends BaseDialogMockUp<ExtendedDialog> {
|
---|
49 | /**
|
---|
50 | * Because we're unable to add fields to the mocked class, we need to use this external global
|
---|
51 | * mapping to be able to keep a note of the most recently set simple String contents of each
|
---|
52 | * {@link ExtendedDialog} instance - {@link ExtendedDialog} doesn't store this information
|
---|
53 | * itself, instead converting it directly into the embedded {@link Component}.
|
---|
54 | */
|
---|
55 | protected final Map<ExtendedDialog, String> simpleStringContentMemo = new WeakHashMap<>();
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Construct an {@link ExtendedDialogMocker} with an empty {@link #mockResultMap}.
|
---|
59 | */
|
---|
60 | public ExtendedDialogMocker() {
|
---|
61 | this(null);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Construct an {@link ExtendedDialogMocker} with the provided {@link #mockResultMap}.
|
---|
66 | * @param mockResultMap mapping of {@link ExtendedDialog} string contents to
|
---|
67 | * result button label or integer index.
|
---|
68 | */
|
---|
69 | public ExtendedDialogMocker(final Map<String, Object> mockResultMap) {
|
---|
70 | super(mockResultMap);
|
---|
71 | if (GraphicsEnvironment.isHeadless()) {
|
---|
72 | new WindowMocker();
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | protected int getButtonPositionFromLabel(final ExtendedDialog instance, final String label) {
|
---|
77 | final String[] bTexts = Deencapsulation.getField(instance, "bTexts");
|
---|
78 | final int position = Arrays.asList(bTexts).indexOf(label);
|
---|
79 | if (position == -1) {
|
---|
80 | fail("Unable to find button labeled \"" + label + "\". Instead found: " + Arrays.toString(bTexts));
|
---|
81 | }
|
---|
82 | return position;
|
---|
83 | }
|
---|
84 |
|
---|
85 | protected String getString(final ExtendedDialog instance) {
|
---|
86 | return Optional.ofNullable(this.simpleStringContentMemo.get(instance))
|
---|
87 | .orElseGet(() -> instance.toString());
|
---|
88 | }
|
---|
89 |
|
---|
90 | protected int getMockResult(final ExtendedDialog instance) {
|
---|
91 | final String stringContent = this.getString(instance);
|
---|
92 | final Object result = this.getMockResultMap().get(stringContent);
|
---|
93 |
|
---|
94 | if (result == null) {
|
---|
95 | fail(
|
---|
96 | "Unexpected ExtendedDialog content: " + stringContent
|
---|
97 | );
|
---|
98 | } else if (result instanceof Integer) {
|
---|
99 | return (Integer) result;
|
---|
100 | } else if (result instanceof String) {
|
---|
101 | // buttons are numbered with 1-based indexing
|
---|
102 | return 1 + this.getButtonPositionFromLabel(instance, (String) result);
|
---|
103 | }
|
---|
104 |
|
---|
105 | throw new IllegalArgumentException(
|
---|
106 | "ExtendedDialog contents mapped to unsupported type of Object: " + result
|
---|
107 | );
|
---|
108 | }
|
---|
109 |
|
---|
110 | protected Object[] getInvocationLogEntry(final ExtendedDialog instance, final int mockResult) {
|
---|
111 | return new Object[] {
|
---|
112 | mockResult,
|
---|
113 | this.getString(instance),
|
---|
114 | instance.getTitle()
|
---|
115 | };
|
---|
116 | }
|
---|
117 |
|
---|
118 | @Mock
|
---|
119 | private void setupDialog(final Invocation invocation) {
|
---|
120 | if (!GraphicsEnvironment.isHeadless()) {
|
---|
121 | invocation.proceed();
|
---|
122 | }
|
---|
123 | // else do nothing - WindowMocker-ed Windows doesn't work well enough for some of the
|
---|
124 | // component constructions
|
---|
125 | }
|
---|
126 |
|
---|
127 | @Mock
|
---|
128 | private void setVisible(final Invocation invocation, final boolean value) {
|
---|
129 | if (value == true) {
|
---|
130 | try {
|
---|
131 | final ExtendedDialog instance = invocation.getInvokedInstance();
|
---|
132 | final int mockResult = this.getMockResult(instance);
|
---|
133 | // TODO check validity of mockResult?
|
---|
134 | Deencapsulation.setField(instance, "result", mockResult);
|
---|
135 | Logging.info(
|
---|
136 | "{0} answering {1} to ExtendedDialog with content {2}",
|
---|
137 | this.getClass().getName(),
|
---|
138 | mockResult,
|
---|
139 | this.getString(instance)
|
---|
140 | );
|
---|
141 | this.getInvocationLogInternal().add(this.getInvocationLogEntry(instance, mockResult));
|
---|
142 | } catch (AssertionError e) {
|
---|
143 | // in case this exception gets ignored by the calling thread we want to signify this failure
|
---|
144 | // in the invocation log. it's hard to know what to add to the log in these cases as it's
|
---|
145 | // probably unsafe to call getInvocationLogEntry, so add the exception on its own.
|
---|
146 | this.getInvocationLogInternal().add(new Object[] {e});
|
---|
147 | throw e;
|
---|
148 | }
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | @Mock
|
---|
153 | private ExtendedDialog setContent(final Invocation invocation, final String message) {
|
---|
154 | final ExtendedDialog retval = invocation.proceed(message);
|
---|
155 | // must set this *after* the regular invocation else that will fall through to
|
---|
156 | // setContent(Component, boolean) which would overwrite it (with null)
|
---|
157 | this.simpleStringContentMemo.put((ExtendedDialog) invocation.getInvokedInstance(), message);
|
---|
158 | return retval;
|
---|
159 | }
|
---|
160 |
|
---|
161 | @Mock
|
---|
162 | private ExtendedDialog setContent(final Invocation invocation, final Component content, final boolean placeContentInScrollPane) {
|
---|
163 | this.simpleStringContentMemo.put((ExtendedDialog) invocation.getInvokedInstance(), null);
|
---|
164 | return invocation.proceed(content, placeContentInScrollPane);
|
---|
165 | }
|
---|
166 | }
|
---|