|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2025 Advantest Corporation 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 | + * Advantest Corporation - initial API and implementation |
| 13 | + *******************************************************************************/ |
| 14 | +package org.eclipse.jdt.debug.tests.ui; |
| 15 | + |
| 16 | +import static org.eclipse.jdt.internal.debug.ui.IJDIPreferencesConstants.PREF_ALERT_UNABLE_TO_INSTALL_BREAKPOINT; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.eclipse.core.runtime.ILogListener; |
| 23 | +import org.eclipse.core.runtime.IStatus; |
| 24 | +import org.eclipse.core.runtime.Platform; |
| 25 | +import org.eclipse.core.runtime.Status; |
| 26 | +import org.eclipse.debug.core.DebugPlugin; |
| 27 | +import org.eclipse.jdi.internal.ClassTypeImpl; |
| 28 | +import org.eclipse.jdi.internal.ReferenceTypeImpl; |
| 29 | +import org.eclipse.jdt.internal.debug.core.breakpoints.JavaLineBreakpoint; |
| 30 | +import org.eclipse.jdt.internal.debug.ui.DebugUIMessages; |
| 31 | +import org.eclipse.jdt.internal.debug.ui.ErrorDialogWithToggle; |
| 32 | +import org.eclipse.jdt.internal.debug.ui.JDIDebugUIPlugin; |
| 33 | +import org.eclipse.jdt.internal.debug.ui.NoLineNumberAttributesStatusHandler; |
| 34 | +import org.eclipse.jface.dialogs.ErrorDialog; |
| 35 | +import org.eclipse.jface.dialogs.IDialogConstants; |
| 36 | +import org.eclipse.jface.preference.IPreferenceStore; |
| 37 | +import org.eclipse.osgi.util.NLS; |
| 38 | +import org.eclipse.swt.events.ShellAdapter; |
| 39 | +import org.eclipse.swt.events.ShellEvent; |
| 40 | +import org.eclipse.swt.widgets.Button; |
| 41 | +import org.eclipse.swt.widgets.Shell; |
| 42 | +import org.eclipse.test.OrderedTestSuite; |
| 43 | +import org.eclipse.ui.PlatformUI; |
| 44 | +import org.junit.Test; |
| 45 | + |
| 46 | +/** |
| 47 | + * This test is checking the {@link NoLineNumberAttributesStatusHandler} the functionality that the ok button click is saving the state in the |
| 48 | + * preference store. |
| 49 | + */ |
| 50 | +public class NoLineNumberAttributesStatusHandlerTest extends AbstractDebugUiTests { |
| 51 | + |
| 52 | + public static junit.framework.Test suite() { |
| 53 | + return new OrderedTestSuite(NoLineNumberAttributesStatusHandlerTest.class); |
| 54 | + } |
| 55 | + |
| 56 | + private static final class ErrorDialogWithToggleRunnable implements Runnable { |
| 57 | + |
| 58 | + private final IPreferenceStore preferenceStore; |
| 59 | + private final IStatus status; |
| 60 | + private final boolean toggleValue; |
| 61 | + |
| 62 | + private ErrorDialogWithToggleRunnable(IPreferenceStore preferenceStore, IStatus status, boolean toggleValue) { |
| 63 | + this.preferenceStore = preferenceStore; |
| 64 | + this.status = status; |
| 65 | + this.toggleValue = toggleValue; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void run() { |
| 70 | + Shell shell = PlatformUI.getWorkbench().getModalDialogShellProvider().getShell(); |
| 71 | + class ErrorDialogWithToggleForTest extends ErrorDialogWithToggle { |
| 72 | + |
| 73 | + public ErrorDialogWithToggleForTest(Shell parentShell, String dialogTitle, String message, IStatus status, String preferenceKey, String toggleMessage, IPreferenceStore store) { |
| 74 | + super(parentShell, dialogTitle, message, status, preferenceKey, toggleMessage, store); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Overridden to make public. |
| 79 | + */ |
| 80 | + @Override |
| 81 | + public Button getToggleButton() { |
| 82 | + return super.getToggleButton(); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Overridden to make public. |
| 87 | + */ |
| 88 | + @Override |
| 89 | + public void buttonPressed(int id) { |
| 90 | + super.buttonPressed(id); |
| 91 | + } |
| 92 | + } |
| 93 | + ErrorDialogWithToggleForTest dialog = new ErrorDialogWithToggleForTest(shell, DebugUIMessages.NoLineNumberAttributesStatusHandler_Java_Breakpoint_1, NLS.bind(DebugUIMessages.NoLineNumberAttributesStatusHandler_2, "HelloWorld"), status, PREF_ALERT_UNABLE_TO_INSTALL_BREAKPOINT, DebugUIMessages.NoLineNumberAttributesStatusHandler_3, preferenceStore); |
| 94 | + final boolean originalMode = ErrorDialog.AUTOMATED_MODE; |
| 95 | + ErrorDialog.AUTOMATED_MODE = false; |
| 96 | + try { |
| 97 | + dialog.create(); |
| 98 | + dialog.getToggleButton().setSelection(toggleValue); |
| 99 | + dialog.getShell().addShellListener(new ShellAdapter() { |
| 100 | + @Override |
| 101 | + public void shellActivated(ShellEvent e) { |
| 102 | + // To see dialog: processUiEvents(500); |
| 103 | + processUiEvents(); |
| 104 | + dialog.buttonPressed(IDialogConstants.OK_ID); |
| 105 | + } |
| 106 | + }); |
| 107 | + dialog.open(); |
| 108 | + } catch (Exception e) { |
| 109 | + throw new RuntimeException(e); |
| 110 | + } finally { |
| 111 | + ErrorDialog.AUTOMATED_MODE = originalMode; |
| 112 | + dialog.close(); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + } |
| 117 | + |
| 118 | + private static final class ListLogListener implements ILogListener { |
| 119 | + private final List<IStatus> loggedEntries; |
| 120 | + |
| 121 | + private ListLogListener(List<IStatus> loggedEntries) { |
| 122 | + this.loggedEntries = loggedEntries; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void logging(IStatus status, String plugin) { |
| 127 | + if (status.isMultiStatus() && status.getChildren().length == 1) { |
| 128 | + loggedEntries.add(status.getChildren()[0]); |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public NoLineNumberAttributesStatusHandlerTest(String name) { |
| 134 | + super(name); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testPreferenceSettings() throws Exception { |
| 139 | + List<IStatus> loggedEntries = new ArrayList<>(); |
| 140 | + ILogListener listener = new ListLogListener(loggedEntries); |
| 141 | + |
| 142 | + Platform.addLogListener(listener); |
| 143 | + try { |
| 144 | + IPreferenceStore preferenceStore = JDIDebugUIPlugin.getDefault().getPreferenceStore(); |
| 145 | + |
| 146 | + IStatus status = new Status(IStatus.ERROR, "org.eclipse.jdt.debug", JavaLineBreakpoint.NO_LINE_NUMBERS, "Teststatus", null); |
| 147 | + |
| 148 | + // No errors should be logged after "don't tell me" preference is set |
| 149 | + boolean dontTellMeAgain = true; |
| 150 | + simulateErrorDialogWithToggleExecution(preferenceStore, status, dontTellMeAgain); |
| 151 | + assertFalse("Wrong preference set for alert", preferenceStore.getBoolean(PREF_ALERT_UNABLE_TO_INSTALL_BREAKPOINT)); |
| 152 | + assertTrue("Expected no logged entries but got: " + loggedEntries, loggedEntries.isEmpty()); |
| 153 | + triggerNoLineAttributesStatusHandler(status); |
| 154 | + assertEquals("Expected no logged entries but got: " + loggedEntries, 0, Collections.frequency(loggedEntries, status)); |
| 155 | + |
| 156 | + // Error should be logged if "don't tell me" preference is not set |
| 157 | + dontTellMeAgain = false; |
| 158 | + simulateErrorDialogWithToggleExecution(preferenceStore, status, dontTellMeAgain); |
| 159 | + assertTrue("Wrong preference set for alert", preferenceStore.getBoolean(PREF_ALERT_UNABLE_TO_INSTALL_BREAKPOINT)); |
| 160 | + assertTrue("Expected no logged entries but got: " + loggedEntries, loggedEntries.isEmpty()); |
| 161 | + triggerNoLineAttributesStatusHandler(status); |
| 162 | + assertEquals(1, Collections.frequency(loggedEntries, status)); |
| 163 | + loggedEntries.clear(); |
| 164 | + |
| 165 | + // No errors should be logged after "don't tell me" preference is set again |
| 166 | + dontTellMeAgain = true; |
| 167 | + simulateErrorDialogWithToggleExecution(preferenceStore, status, dontTellMeAgain); |
| 168 | + assertFalse("Wrong preference set for alert", preferenceStore.getBoolean(PREF_ALERT_UNABLE_TO_INSTALL_BREAKPOINT)); |
| 169 | + assertTrue("Expected no logged entries but got: " + loggedEntries, loggedEntries.isEmpty()); |
| 170 | + triggerNoLineAttributesStatusHandler(status); |
| 171 | + assertEquals("Expected no logged entries but got: " + loggedEntries, 0, Collections.frequency(loggedEntries, status)); |
| 172 | + } finally { |
| 173 | + Platform.removeLogListener(listener); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + private void triggerNoLineAttributesStatusHandler(IStatus status) { |
| 178 | + ReferenceTypeImpl referenceTypeImpl = new ClassTypeImpl(null, null); |
| 179 | + referenceTypeImpl.setName("TestRefTypeName"); |
| 180 | + NoLineNumberAttributesStatusHandler statusHandler = (NoLineNumberAttributesStatusHandler) DebugPlugin.getDefault().getStatusHandler(status); |
| 181 | + statusHandler.handleStatus(status, referenceTypeImpl); |
| 182 | + } |
| 183 | + |
| 184 | + private void simulateErrorDialogWithToggleExecution(IPreferenceStore preferenceStore, IStatus status, boolean toggleValue) throws Exception { |
| 185 | + ErrorDialogWithToggleRunnable runnable = new ErrorDialogWithToggleRunnable(preferenceStore, status, toggleValue); |
| 186 | + sync(runnable); |
| 187 | + } |
| 188 | +} |
0 commit comments