|
| 1 | +package org.eclipse.jdt.debug.tests.ui; |
| 2 | + |
| 3 | +import static org.mockito.Mockito.mock; |
| 4 | +import static org.mockito.Mockito.when; |
| 5 | + |
| 6 | +import java.lang.reflect.InvocationTargetException; |
| 7 | +import java.lang.reflect.Method; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Collections; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +import org.eclipse.core.runtime.CoreException; |
| 13 | +import org.eclipse.core.runtime.ILogListener; |
| 14 | +import org.eclipse.core.runtime.IStatus; |
| 15 | +import org.eclipse.core.runtime.Platform; |
| 16 | +import org.eclipse.core.runtime.Status; |
| 17 | +import org.eclipse.debug.core.DebugPlugin; |
| 18 | +import org.eclipse.jdi.internal.ClassTypeImpl; |
| 19 | +import org.eclipse.jdi.internal.ReferenceTypeImpl; |
| 20 | +import org.eclipse.jdt.internal.debug.ui.DebugUIMessages; |
| 21 | +import org.eclipse.jdt.internal.debug.ui.ErrorDialogWithToggle; |
| 22 | +import org.eclipse.jdt.internal.debug.ui.IJDIPreferencesConstants; |
| 23 | +import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; |
| 24 | +import org.eclipse.jdt.internal.debug.ui.NoLineNumberAttributesStatusHandler; |
| 25 | +import org.eclipse.jface.dialogs.IDialogConstants; |
| 26 | +import org.eclipse.jface.preference.IPreferenceStore; |
| 27 | +import org.eclipse.osgi.util.NLS; |
| 28 | +import org.eclipse.swt.widgets.Button; |
| 29 | +import org.eclipse.test.OrderedTestSuite; |
| 30 | +import org.eclipse.ui.PlatformUI; |
| 31 | +import org.junit.Test; |
| 32 | + |
| 33 | +/** |
| 34 | + * This test is checking the {@link NoLineNumberAttributesStatusHandler} the functionality that the ok button click is saving the state in the |
| 35 | + * preference store. |
| 36 | + */ |
| 37 | +public class NoLineNumberAttributesStatusHandlerTest extends AbstractDebugUiTests { |
| 38 | + |
| 39 | + public static junit.framework.Test suite() { |
| 40 | + return new OrderedTestSuite(NoLineNumberAttributesStatusHandlerTest.class); |
| 41 | + } |
| 42 | + |
| 43 | + private final class ListLogListener implements ILogListener { |
| 44 | + private final List<IStatus> loggedEntries; |
| 45 | + |
| 46 | + private ListLogListener(List<IStatus> loggedEntries) { |
| 47 | + this.loggedEntries = loggedEntries; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void logging(IStatus status, String plugin) { |
| 52 | + if (status.isMultiStatus() && status.getChildren().length == 1) { |
| 53 | + loggedEntries.add(status.getChildren()[0]); |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public NoLineNumberAttributesStatusHandlerTest(String name) { |
| 59 | + super(name); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void testPreferenceSettings() throws CoreException, NoSuchMethodException, SecurityException, IllegalAccessException, InvocationTargetException { |
| 64 | + List<IStatus> loggedEntries = new ArrayList<>(); |
| 65 | + ILogListener listener = new ListLogListener(loggedEntries); |
| 66 | + |
| 67 | + Platform.addLogListener(listener); |
| 68 | + try { |
| 69 | + IPreferenceStore preferenceStore = JDIDebugUIPlugin.getDefault().getPreferenceStore(); |
| 70 | + |
| 71 | + IStatus status = new Status(IStatus.ERROR, "org.eclipse.jdt.debug", 162, "Teststatus", null); |
| 72 | + |
| 73 | + simulateErrorDialogWithToggleExecution(preferenceStore, status, true); |
| 74 | + assertFalse(preferenceStore.getBoolean(IJDIPreferencesConstants.PREF_ALERT_UNABLE_TO_INSTALL_BREAKPOINT)); |
| 75 | + assertTrue(loggedEntries.isEmpty()); |
| 76 | + triggerNoLineAttributesStatusHandler(status); |
| 77 | + assertEquals(0, Collections.frequency(loggedEntries, status)); |
| 78 | + |
| 79 | + simulateErrorDialogWithToggleExecution(preferenceStore, status, false); |
| 80 | + assertTrue(preferenceStore.getBoolean(IJDIPreferencesConstants.PREF_ALERT_UNABLE_TO_INSTALL_BREAKPOINT)); |
| 81 | + assertTrue(loggedEntries.isEmpty()); |
| 82 | + triggerNoLineAttributesStatusHandler(status); |
| 83 | + assertEquals(1, Collections.frequency(loggedEntries, status)); |
| 84 | + loggedEntries.clear(); |
| 85 | + |
| 86 | + simulateErrorDialogWithToggleExecution(preferenceStore, status, true); |
| 87 | + assertFalse(preferenceStore.getBoolean(IJDIPreferencesConstants.PREF_ALERT_UNABLE_TO_INSTALL_BREAKPOINT)); |
| 88 | + |
| 89 | + assertTrue(loggedEntries.isEmpty()); |
| 90 | + triggerNoLineAttributesStatusHandler(status); |
| 91 | + assertEquals(0, Collections.frequency(loggedEntries, status)); |
| 92 | + } finally { |
| 93 | + Platform.removeLogListener(listener); |
| 94 | + } |
| 95 | + |
| 96 | + } |
| 97 | + |
| 98 | + private void triggerNoLineAttributesStatusHandler(IStatus status) throws CoreException { |
| 99 | + ReferenceTypeImpl referenceTypeImpl = new ClassTypeImpl(null, null); |
| 100 | + referenceTypeImpl.setName("TestRefTypeName"); |
| 101 | + DebugPlugin.getDefault().getStatusHandler(status).handleStatus(status, referenceTypeImpl); |
| 102 | + } |
| 103 | + |
| 104 | + private void simulateErrorDialogWithToggleExecution(IPreferenceStore preferenceStore, IStatus status, boolean value) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { |
| 105 | + ErrorDialogWithToggle dialog; |
| 106 | + Method method; |
| 107 | + dialog = new ErrorDialogWithToggle(PlatformUI.getWorkbench().getModalDialogShellProvider().getShell(), DebugUIMessages.NoLineNumberAttributesStatusHandler_Java_Breakpoint_1, NLS.bind(DebugUIMessages.NoLineNumberAttributesStatusHandler_2, "HelloWorld"), status, IJDIPreferencesConstants.PREF_ALERT_UNABLE_TO_INSTALL_BREAKPOINT, DebugUIMessages.NoLineNumberAttributesStatusHandler_3, preferenceStore) { |
| 108 | + @Override |
| 109 | + protected org.eclipse.swt.widgets.Button getToggleButton() { |
| 110 | + Button mockedButton = mock(Button.class); |
| 111 | + when(mockedButton.getSelection()).thenReturn(value); |
| 112 | + return mockedButton; |
| 113 | + } |
| 114 | + }; |
| 115 | + method = ErrorDialogWithToggle.class.getDeclaredMethod("buttonPressed", Integer.TYPE); |
| 116 | + method.setAccessible(true); |
| 117 | + try { |
| 118 | + method.invoke(dialog, IDialogConstants.OK_ID); |
| 119 | + } finally { |
| 120 | + method.setAccessible(false); |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments