You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActionsManagerDialogTest.java 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_swing.dialogs.actionsmanager;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.actions.ActionManager;
  25. import com.dmdirc.addons.ui_swing.SwingController;
  26. import com.dmdirc.config.IdentityManager;
  27. import com.dmdirc.config.InvalidIdentityFileException;
  28. import com.dmdirc.harness.ui.ClassFinder;
  29. import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
  30. import com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionEditorDialog;
  31. import java.lang.reflect.InvocationTargetException;
  32. import javax.swing.JPanel;
  33. import javax.swing.SwingUtilities;
  34. import javax.swing.text.JTextComponent;
  35. import org.fest.swing.core.EventMode;
  36. import org.fest.swing.core.matcher.JButtonByTextMatcher;
  37. import org.fest.swing.finder.JOptionPaneFinder;
  38. import org.fest.swing.finder.WindowFinder;
  39. import org.fest.swing.fixture.DialogFixture;
  40. import org.fest.swing.fixture.JOptionPaneFixture;
  41. import org.junit.After;
  42. import org.junit.Before;
  43. import org.junit.BeforeClass;
  44. import org.junit.Test;
  45. import static org.junit.Assert.*;
  46. public class ActionsManagerDialogTest {
  47. private DialogFixture window;
  48. @BeforeClass
  49. public static void setUpClass() throws InvalidIdentityFileException {
  50. Main.setUI(new SwingController());
  51. IdentityManager.load();
  52. ActionManager.init();
  53. }
  54. @Before
  55. public void setUp() {
  56. removeGroups();
  57. }
  58. @After
  59. public void tearDown() throws InterruptedException, InvocationTargetException {
  60. SwingUtilities.invokeAndWait(new Runnable() {
  61. @Override
  62. public void run() {
  63. close();
  64. }
  65. });
  66. removeGroups();
  67. }
  68. protected void close() {
  69. if (window != null) {
  70. window.cleanUp();
  71. }
  72. }
  73. protected void removeGroups() {
  74. if (ActionManager.getGroups().containsKey("amd-ui-test1")) {
  75. ActionManager.removeGroup("amd-ui-test1");
  76. }
  77. if (ActionManager.getGroups().containsKey("amd-ui-test2")) {
  78. ActionManager.removeGroup("amd-ui-test2");
  79. }
  80. }
  81. @Test
  82. public void testAddGroup() throws InterruptedException {
  83. setupWindow();
  84. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  85. .button(JButtonByTextMatcher.withText("Add")).click();
  86. DialogFixture newwin = WindowFinder.findDialog(StandardInputDialog.class)
  87. .withTimeout(5000).using(window.robot);
  88. newwin.requireVisible();
  89. assertEquals("New action group", newwin.target.getTitle());
  90. newwin.button(JButtonByTextMatcher.withText("Cancel")).click();
  91. newwin.requireNotVisible();
  92. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  93. .button(JButtonByTextMatcher.withText("Add")).click();
  94. newwin = WindowFinder.findDialog(StandardInputDialog.class)
  95. .withTimeout(5000).using(window.robot);
  96. newwin.requireVisible();
  97. newwin.button(JButtonByTextMatcher.withText("OK")).requireDisabled();
  98. newwin.textBox(new ClassFinder<JTextComponent>(javax.swing.JTextField.class,
  99. null)).enterText("amd-ui-test1");
  100. newwin.button(JButtonByTextMatcher.withText("OK")).requireEnabled().click();
  101. window.list().requireSelectedItems("amd-ui-test1");
  102. assertTrue(ActionManager.getGroups().containsKey("amd-ui-test1"));
  103. }
  104. @Test
  105. public void testDeleteGroup() {
  106. ActionManager.makeGroup("amd-ui-test1");
  107. setupWindow();
  108. window.list().selectItem("amd-ui-test1").requireSelectedItems("amd-ui-test1");
  109. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  110. .button(JButtonByTextMatcher.withText("Delete")).requireEnabled().click();
  111. JOptionPaneFixture newwin = JOptionPaneFinder.findOptionPane()
  112. .withTimeout(5000).using(window.robot);
  113. newwin.buttonWithText("No").click();
  114. assertTrue(ActionManager.getGroups().containsKey("amd-ui-test1"));
  115. window.list().selectItem("amd-ui-test1").requireSelectedItems("amd-ui-test1");
  116. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  117. .button(JButtonByTextMatcher.withText("Delete")).click();
  118. newwin = JOptionPaneFinder.findOptionPane()
  119. .withTimeout(5000).using(window.robot);
  120. newwin.buttonWithText("Yes").click();
  121. assertTrue(window.list().selection().length != 1 ||
  122. !window.list().selection()[0].equals("amd-ui-test1"));
  123. assertFalse(ActionManager.getGroups().containsKey("amd-ui-test1"));
  124. }
  125. @Test
  126. public void testEnablingGroupButtons() {
  127. ActionManager.makeGroup("amd-ui-test1");
  128. setupWindow();
  129. window.list().selectItem("performs").requireSelectedItems("performs");
  130. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  131. .button(JButtonByTextMatcher.withText("Delete")).requireDisabled();
  132. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  133. .button(JButtonByTextMatcher.withText("Edit")).requireDisabled();
  134. window.list().selectItem("amd-ui-test1").requireSelectedItems("amd-ui-test1");
  135. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  136. .button(JButtonByTextMatcher.withText("Delete")).requireEnabled();
  137. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  138. .button(JButtonByTextMatcher.withText("Edit")).requireEnabled();
  139. }
  140. @Test
  141. public void testAddAction() {
  142. ActionManager.makeGroup("amd-ui-test1");
  143. setupWindow();
  144. window.list().selectItem("amd-ui-test1").requireSelectedItems("amd-ui-test1");
  145. window.panel(new ClassFinder<JPanel>(ActionsGroupPanel.class, null))
  146. .button(JButtonByTextMatcher.withText("Add")).click();
  147. DialogFixture newwin = WindowFinder.findDialog(ActionEditorDialog.class)
  148. .withTimeout(5000).using(window.robot);
  149. newwin.requireVisible();
  150. }
  151. public void editGroupCheck(final String button) {
  152. ActionManager.makeGroup("amd-ui-test1");
  153. setupWindow();
  154. window.list().selectItem("amd-ui-test1").requireSelectedItems("amd-ui-test1");
  155. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  156. .button(JButtonByTextMatcher.withText("Edit")).requireEnabled().click();
  157. DialogFixture newwin = WindowFinder.findDialog(StandardInputDialog.class)
  158. .withTimeout(5000).using(window.robot);
  159. newwin.requireVisible();
  160. assertEquals("Edit action group", newwin.target.getTitle());
  161. assertEquals("amd-ui-test1",
  162. newwin.textBox(new ClassFinder<JTextComponent>(javax.swing.JTextField.class,
  163. null)).target.getText());
  164. newwin.textBox(new ClassFinder<JTextComponent>(javax.swing.JTextField.class,
  165. null)).deleteText().enterText("amd-ui-test2");
  166. newwin.button(JButtonByTextMatcher.withText(button)).requireEnabled().click();
  167. }
  168. @Test
  169. public void testEditGroupCancel() {
  170. editGroupCheck("Cancel");
  171. assertTrue(ActionManager.getGroups().containsKey("amd-ui-test1"));
  172. assertFalse(ActionManager.getGroups().containsKey("amd-ui-test2"));
  173. }
  174. @Test
  175. public void testEditGroupOK() {
  176. editGroupCheck("OK");
  177. assertFalse(ActionManager.getGroups().containsKey("amd-ui-test1"));
  178. assertTrue(ActionManager.getGroups().containsKey("amd-ui-test2"));
  179. }
  180. protected void setupWindow() {
  181. window = new DialogFixture(ActionsManagerDialog.getActionsManagerDialog(null, null));
  182. window.robot.settings().eventMode(EventMode.AWT);
  183. window.show();
  184. }
  185. }