|
| 1 | +/* |
| 2 | + * BSD-style license; for more info see http://pmd.sourceforge.net/license.html |
| 3 | + */ |
| 4 | + |
| 5 | + |
| 6 | +package net.sourceforge.pmd.eclipse.ui.preferences.br; |
| 7 | + |
| 8 | +import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.allOf; |
| 9 | +import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.widgetOfType; |
| 10 | +import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withMnemonic; |
| 11 | +import static org.eclipse.swtbot.swt.finder.matchers.WidgetMatcherFactory.withStyle; |
| 12 | +import static org.eclipse.swtbot.swt.finder.waits.Conditions.waitForWidget; |
| 13 | +import static org.junit.Assert.assertTrue; |
| 14 | +import static org.junit.Assert.fail; |
| 15 | + |
| 16 | +import java.util.ArrayList; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Locale; |
| 19 | +import java.util.concurrent.TimeUnit; |
| 20 | +import java.util.stream.Collectors; |
| 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.jface.preference.PreferenceDialog; |
| 26 | +import org.eclipse.swt.SWT; |
| 27 | +import org.eclipse.swt.widgets.Button; |
| 28 | +import org.eclipse.swtbot.swt.finder.waits.WaitForObjectCondition; |
| 29 | +import org.eclipse.swtbot.swt.finder.widgets.SWTBotCheckBox; |
| 30 | +import org.eclipse.swtbot.swt.finder.widgets.SWTBotShell; |
| 31 | +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTree; |
| 32 | +import org.eclipse.swtbot.swt.finder.widgets.SWTBotTreeItem; |
| 33 | +import org.eclipse.ui.PlatformUI; |
| 34 | +import org.eclipse.ui.dialogs.PreferencesUtil; |
| 35 | +import org.hamcrest.Matcher; |
| 36 | +import org.junit.After; |
| 37 | +import org.junit.Before; |
| 38 | +import org.junit.Test; |
| 39 | + |
| 40 | +import net.sourceforge.pmd.eclipse.AbstractSWTBotTest; |
| 41 | + |
| 42 | +public class PMDPreferencePage2Test extends AbstractSWTBotTest { |
| 43 | + private List<IStatus> errors; |
| 44 | + private ILogListener logListener; |
| 45 | + |
| 46 | + @Before |
| 47 | + public void setup() { |
| 48 | + errors = new ArrayList<>(); |
| 49 | + logListener = new ILogListener() { |
| 50 | + @Override |
| 51 | + public void logging(IStatus status, String plugin) { |
| 52 | + if (status.getSeverity() != IStatus.ERROR) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + String statusText = status.toString().toLowerCase(Locale.ROOT); |
| 57 | + if (statusText.contains("pmd") || statusText.contains("unhandled event loop exception")) { |
| 58 | + errors.add(status); |
| 59 | + } |
| 60 | + } |
| 61 | + }; |
| 62 | + Platform.getLog(PlatformUI.class).addLogListener(logListener); |
| 63 | + } |
| 64 | + |
| 65 | + @After |
| 66 | + public void cleanup() { |
| 67 | + Platform.getLog(PlatformUI.class).removeLogListener(logListener); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Simple smoke test to open the rule configuration preference pages. |
| 72 | + * @see PMDPreferencePage2 |
| 73 | + */ |
| 74 | + @Test |
| 75 | + public void openPMDRuleConfiguration() { |
| 76 | + openPreferences(); |
| 77 | + SWTBotShell preferencesDialog = bot.shell("Preferences"); |
| 78 | + SWTBotTreeItem pmdItem = preferencesDialog.bot().tree(0).getTreeItem("PMD"); |
| 79 | + pmdItem.click(); |
| 80 | + pmdItem.expand(); |
| 81 | + pmdItem.getNode("Rule Configuration").click(); |
| 82 | + |
| 83 | + |
| 84 | + Matcher<Button> matcher = allOf(widgetOfType(Button.class), |
| 85 | + withMnemonic("Use global rule management"), |
| 86 | + withStyle(SWT.CHECK, "SWT.CHECK")); |
| 87 | + WaitForObjectCondition<Button> waitForWidget = waitForWidget(matcher); |
| 88 | + preferencesDialog.bot().waitUntil(waitForWidget, TimeUnit.SECONDS.toMillis(15)); |
| 89 | + SWTBotCheckBox globalRuleManagementCheckbox = new SWTBotCheckBox(waitForWidget.get(0), matcher); |
| 90 | + globalRuleManagementCheckbox.click(); |
| 91 | + |
| 92 | + SWTBotTree ruleTable = preferencesDialog.bot().tree(1); |
| 93 | + int rowCount = ruleTable.rowCount(); |
| 94 | + assertTrue(rowCount > 0); |
| 95 | + |
| 96 | + // select first rule |
| 97 | + ruleTable.getAllItems()[0].click(); |
| 98 | + |
| 99 | + preferencesDialog.bot().button("Cancel").click(); |
| 100 | + |
| 101 | + if (!errors.isEmpty()) { |
| 102 | + fail("There are " + errors.size() + " errors:\n" |
| 103 | + + errors.stream().map(IStatus::getMessage).collect(Collectors.joining("\n"))); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private void openPreferences() { |
| 108 | + // due to macosx, where the Preferences is not part of the Window menu but the application |
| 109 | + // menu, we programmatically open the preferences dialog and not try to find it via the menu. |
| 110 | + bot.getDisplay().execute(() -> { |
| 111 | + PreferenceDialog dialog = PreferencesUtil.createPreferenceDialogOn(bot.shell().widget, null, null, errors); |
| 112 | + dialog.open(); |
| 113 | + }); |
| 114 | + } |
| 115 | +} |
0 commit comments