|
| 1 | +/* |
| 2 | + Copyright 2026 Will Winder |
| 3 | +
|
| 4 | + This file is part of Universal Gcode Sender (UGS). |
| 5 | +
|
| 6 | + UGS is free software: you can redistribute it and/or modify |
| 7 | + it under the terms of the GNU General Public License as published by |
| 8 | + the Free Software Foundation, either version 3 of the License, or |
| 9 | + (at your option) any later version. |
| 10 | +
|
| 11 | + UGS is distributed in the hope that it will be useful, |
| 12 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + GNU General Public License for more details. |
| 15 | +
|
| 16 | + You should have received a copy of the GNU General Public License |
| 17 | + along with UGS. If not, see <http://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | +package com.willwinder.ugs.nbp.lib.services; |
| 20 | + |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | +import org.openide.filesystems.FileObject; |
| 25 | +import org.openide.filesystems.FileUtil; |
| 26 | + |
| 27 | +import javax.swing.AbstractAction; |
| 28 | +import javax.swing.Action; |
| 29 | +import java.awt.event.ActionEvent; |
| 30 | +import java.io.IOException; |
| 31 | + |
| 32 | +import static org.junit.Assert.assertNotNull; |
| 33 | +import static org.junit.Assert.assertNull; |
| 34 | + |
| 35 | +public class ActionRegistrationServiceTest { |
| 36 | + private static final String CATEGORY = "TestActionRegistrationService"; |
| 37 | + private static final String MENU_PATH = "Menu/TestActionRegistrationService"; |
| 38 | + private static final String LOCAL_MENU_PATH = "Menu/Test Action Registration Service"; |
| 39 | + private static final String SHORTCUT = "D-RIGHT"; |
| 40 | + private static final String OTHER_SHORTCUT = "D-LEFT"; |
| 41 | + private static final String NO_SHORTCUT = "D-UP"; |
| 42 | + |
| 43 | + private ActionRegistrationService service; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void setUp() throws IOException { |
| 47 | + cleanUp(); |
| 48 | + service = new ActionRegistrationService(); |
| 49 | + } |
| 50 | + |
| 51 | + @After |
| 52 | + public void tearDown() throws IOException { |
| 53 | + cleanUp(); |
| 54 | + } |
| 55 | + |
| 56 | + @Test |
| 57 | + public void registerActionCreatesActionAndShortcutForNewAction() throws IOException { |
| 58 | + registerAction("test-new-action", SHORTCUT); |
| 59 | + |
| 60 | + assertNotNull(getAction("test-new-action")); |
| 61 | + assertNotNull(getShortcut(SHORTCUT)); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void registerActionDoesNotRecreateRemovedShortcutForExistingAction() throws IOException { |
| 66 | + registerAction("test-existing-action", SHORTCUT); |
| 67 | + getShortcut(SHORTCUT).delete(); |
| 68 | + |
| 69 | + registerAction("test-existing-action", SHORTCUT); |
| 70 | + |
| 71 | + assertNotNull(getAction("test-existing-action")); |
| 72 | + assertNotNull(getMenuItem("test-existing-action")); |
| 73 | + assertNull(getShortcut(SHORTCUT)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void registerActionWithoutShortcutDoesNotCreateShortcut() throws IOException { |
| 78 | + registerAction("test-null-shortcut", null); |
| 79 | + registerAction("test-empty-shortcut", ""); |
| 80 | + |
| 81 | + assertNotNull(getAction("test-null-shortcut")); |
| 82 | + assertNotNull(getAction("test-empty-shortcut")); |
| 83 | + assertNull(getShortcut(NO_SHORTCUT)); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void registerNewActionStillCreatesShortcutAfterAnotherShortcutWasRemoved() throws IOException { |
| 88 | + registerAction("test-existing-action", SHORTCUT); |
| 89 | + getShortcut(SHORTCUT).delete(); |
| 90 | + |
| 91 | + registerAction("test-other-action", OTHER_SHORTCUT); |
| 92 | + |
| 93 | + assertNull(getShortcut(SHORTCUT)); |
| 94 | + assertNotNull(getAction("test-other-action")); |
| 95 | + assertNotNull(getShortcut(OTHER_SHORTCUT)); |
| 96 | + } |
| 97 | + |
| 98 | + private void registerAction(String id, String shortcut) throws IOException { |
| 99 | + service.registerAction(id, id, CATEGORY, shortcut, MENU_PATH, 100, LOCAL_MENU_PATH, createAction()); |
| 100 | + } |
| 101 | + |
| 102 | + private Action createAction() { |
| 103 | + return new AbstractAction() { |
| 104 | + @Override |
| 105 | + public void actionPerformed(ActionEvent event) { |
| 106 | + // Not used by these registration tests. |
| 107 | + } |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + private FileObject getAction(String id) { |
| 112 | + return FileUtil.getConfigFile("Actions/" + CATEGORY + "/" + id + ".instance"); |
| 113 | + } |
| 114 | + |
| 115 | + private FileObject getMenuItem(String id) { |
| 116 | + return FileUtil.getConfigFile(MENU_PATH + "/" + id + ".shadow"); |
| 117 | + } |
| 118 | + |
| 119 | + private FileObject getShortcut(String shortcut) { |
| 120 | + FileObject keymaps = FileUtil.getConfigFile("Keymaps"); |
| 121 | + String currentKeymap = "NetBeans"; |
| 122 | + if (keymaps != null && keymaps.getAttribute("currentKeymap") != null) { |
| 123 | + currentKeymap = keymaps.getAttribute("currentKeymap").toString(); |
| 124 | + } |
| 125 | + |
| 126 | + return FileUtil.getConfigFile("Keymaps/" + currentKeymap + "/" + shortcut + ".shadow"); |
| 127 | + } |
| 128 | + |
| 129 | + private void cleanUp() throws IOException { |
| 130 | + deleteIfExists(FileUtil.getConfigFile("Actions/" + CATEGORY)); |
| 131 | + deleteIfExists(FileUtil.getConfigFile(MENU_PATH)); |
| 132 | + deleteIfExists(getShortcut(SHORTCUT)); |
| 133 | + deleteIfExists(getShortcut(OTHER_SHORTCUT)); |
| 134 | + deleteIfExists(getShortcut(NO_SHORTCUT)); |
| 135 | + } |
| 136 | + |
| 137 | + private void deleteIfExists(FileObject fileObject) throws IOException { |
| 138 | + if (fileObject != null) { |
| 139 | + fileObject.delete(); |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments