List<PropertyDescriptor> mockDescriptors = Arrays.asList(buildMockDescriptor(a), buildMockDescriptor(b));
Mockito.when(comp.getFieldDescriptors()).thenReturn(mockDescriptors);
// Mocking Swing components tends to fail on different platforms, so stub
CustomVisualControl mockControl = new CustomVisualControl() {
private static final long serialVersionUID = -7026155085259171397L;
private Object object = null;
@Override
public void setValue(Object value) { this.object = value; }
@Override
public Object getValue() { return object; }
@Override
public void setMutable(boolean mutable) {}
};
Mockito.when(comp.getAsset(CustomVisualControl.class)).thenReturn(mockControl);
PropertyDescriptor lastPropertyDescriptor = mockDescriptors.get(mockDescriptors.size() - 1);
// Build a new info view
InfoView infoView = new InfoView(comp, info);
// First, verify that the appropriate get method has been called
// (this implies that info view was populated with extended descriptors upon instantiation)
for (PropertyDescriptor mockDescriptor : mockDescriptors) {
switch (mockDescriptor.getVisualControlDescriptor()) {
case CheckBox:
case ComboBox:
Mockito.verify(mockDescriptor.getPropertyEditor(), Mockito.times(1)).getValue();
break;
case Label:
case TextField:
Mockito.verify(mockDescriptor.getPropertyEditor(), Mockito.times(1)).getAsText();
break;
}
}
ActionEvent mockEvent = Mockito.mock(ActionEvent.class);
// Act as if user changed a property
switch (lastPropertyDescriptor.getVisualControlDescriptor()) {
case CheckBox: {
JCheckBox checkBox = findLastComponentOfType(infoView, JCheckBox.class);
checkBox.doClick();
break;
}
case ComboBox: {
@SuppressWarnings("rawtypes")
JComboBox comboBox = findLastComponentOfType(infoView, JComboBox.class);
comboBox.setSelectedItem("other");
break;
}
case Label: {
// Non-editable
break;
}
case TextField: {
JTextField textField = findLastComponentOfType(infoView, JTextField.class);
textField.setText("changed");
for (ActionListener listener : textField.getActionListeners()) {
// Don't invoke listeners related to platform-specific Swing implementation
// (may lead to inconsistent behavior when running tests on other platforms)
if (listener.getClass().getName().startsWith("gov.nasa.arc.mct")) {
listener.actionPerformed(mockEvent);
}
}
break;
}
case TextArea: {
JTextArea textArea = findLastComponentOfType(infoView, JTextArea.class);
textArea.setText("changed");
for (FocusListener listener : textArea.getFocusListeners()) {
// Don't invoke listeners related to platform-specific Swing implementation
// (may lead to inconsistent behavior when running tests on other platforms)
if (listener.getClass().getName().startsWith("gov.nasa.arc.mct")) {
listener.focusLost(Mockito.mock(FocusEvent.class));
}
}
break;
}
case Custom: {
CustomVisualControl control = findLastComponentOfType(infoView, CustomVisualControl.class);
control.setValue("changed");
try {
Method m = CustomVisualControl.class.getDeclaredMethod("fireChange");
m.setAccessible(true);
m.invoke(control);
} catch (Exception e) {