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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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.UIClassTestRunner;
  29. import com.dmdirc.harness.ui.ClassFinder;
  30. import com.dmdirc.harness.ui.UITestIface;
  31. import com.dmdirc.addons.ui_swing.dialogs.StandardInputDialog;
  32. import com.dmdirc.addons.ui_swing.dialogs.actioneditor.ActionEditorDialog;
  33. import java.lang.reflect.InvocationTargetException;
  34. import javax.swing.JPanel;
  35. import javax.swing.SwingUtilities;
  36. import javax.swing.text.JTextComponent;
  37. import org.fest.swing.core.EventMode;
  38. import org.fest.swing.core.matcher.JButtonByTextMatcher;
  39. import org.fest.swing.finder.JOptionPaneFinder;
  40. import org.fest.swing.finder.WindowFinder;
  41. import org.fest.swing.fixture.DialogFixture;
  42. import org.fest.swing.fixture.JOptionPaneFixture;
  43. import org.junit.After;
  44. import org.junit.Before;
  45. import org.junit.BeforeClass;
  46. import org.junit.Test;
  47. import org.junit.runner.RunWith;
  48. import static org.junit.Assert.*;
  49. @RunWith(UIClassTestRunner.class)
  50. public class ActionsManagerDialogTest implements UITestIface {
  51. private DialogFixture window;
  52. @BeforeClass
  53. public static void setUpClass() throws InvalidIdentityFileException {
  54. Main.setUI(new SwingController());
  55. IdentityManager.load();
  56. ActionManager.init();
  57. }
  58. @Before
  59. public void setUp() {
  60. removeGroups();
  61. }
  62. @After
  63. public void tearDown() throws InterruptedException, InvocationTargetException {
  64. SwingUtilities.invokeAndWait(new Runnable() {
  65. @Override
  66. public void run() {
  67. close();
  68. }
  69. });
  70. removeGroups();
  71. }
  72. protected void close() {
  73. if (window != null) {
  74. window.cleanUp();
  75. }
  76. }
  77. protected void removeGroups() {
  78. if (ActionManager.getGroups().containsKey("amd-ui-test1")) {
  79. ActionManager.removeGroup("amd-ui-test1");
  80. }
  81. if (ActionManager.getGroups().containsKey("amd-ui-test2")) {
  82. ActionManager.removeGroup("amd-ui-test2");
  83. }
  84. }
  85. @Test
  86. public void testAddGroup() throws InterruptedException {
  87. setupWindow();
  88. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  89. .button(JButtonByTextMatcher.withText("Add")).click();
  90. DialogFixture newwin = WindowFinder.findDialog(StandardInputDialog.class)
  91. .withTimeout(5000).using(window.robot);
  92. newwin.requireVisible();
  93. assertEquals("New action group", newwin.target.getTitle());
  94. newwin.button(JButtonByTextMatcher.withText("Cancel")).click();
  95. newwin.requireNotVisible();
  96. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  97. .button(JButtonByTextMatcher.withText("Add")).click();
  98. newwin = WindowFinder.findDialog(StandardInputDialog.class)
  99. .withTimeout(5000).using(window.robot);
  100. newwin.requireVisible();
  101. newwin.button(JButtonByTextMatcher.withText("OK")).requireDisabled();
  102. newwin.textBox(new ClassFinder<JTextComponent>(javax.swing.JTextField.class,
  103. null)).enterText("amd-ui-test1");
  104. newwin.button(JButtonByTextMatcher.withText("OK")).requireEnabled().click();
  105. window.list().requireSelectedItems("amd-ui-test1");
  106. assertTrue(ActionManager.getGroups().containsKey("amd-ui-test1"));
  107. }
  108. @Test
  109. public void testDeleteGroup() {
  110. ActionManager.makeGroup("amd-ui-test1");
  111. setupWindow();
  112. window.list().selectItem("amd-ui-test1").requireSelectedItems("amd-ui-test1");
  113. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  114. .button(JButtonByTextMatcher.withText("Delete")).requireEnabled().click();
  115. JOptionPaneFixture newwin = JOptionPaneFinder.findOptionPane()
  116. .withTimeout(5000).using(window.robot);
  117. newwin.buttonWithText("No").click();
  118. assertTrue(ActionManager.getGroups().containsKey("amd-ui-test1"));
  119. window.list().selectItem("amd-ui-test1").requireSelectedItems("amd-ui-test1");
  120. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  121. .button(JButtonByTextMatcher.withText("Delete")).click();
  122. newwin = JOptionPaneFinder.findOptionPane()
  123. .withTimeout(5000).using(window.robot);
  124. newwin.buttonWithText("Yes").click();
  125. assertTrue(window.list().selection().length != 1 ||
  126. !window.list().selection()[0].equals("amd-ui-test1"));
  127. assertFalse(ActionManager.getGroups().containsKey("amd-ui-test1"));
  128. }
  129. @Test
  130. public void testEnablingGroupButtons() {
  131. ActionManager.makeGroup("amd-ui-test1");
  132. setupWindow();
  133. window.list().selectItem("performs").requireSelectedItems("performs");
  134. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  135. .button(JButtonByTextMatcher.withText("Delete")).requireDisabled();
  136. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  137. .button(JButtonByTextMatcher.withText("Edit")).requireDisabled();
  138. window.list().selectItem("amd-ui-test1").requireSelectedItems("amd-ui-test1");
  139. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  140. .button(JButtonByTextMatcher.withText("Delete")).requireEnabled();
  141. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  142. .button(JButtonByTextMatcher.withText("Edit")).requireEnabled();
  143. }
  144. @Test
  145. public void testAddAction() {
  146. ActionManager.makeGroup("amd-ui-test1");
  147. setupWindow();
  148. window.list().selectItem("amd-ui-test1").requireSelectedItems("amd-ui-test1");
  149. window.panel(new ClassFinder<JPanel>(ActionsGroupPanel.class, null))
  150. .button(JButtonByTextMatcher.withText("Add")).click();
  151. DialogFixture newwin = WindowFinder.findDialog(ActionEditorDialog.class)
  152. .withTimeout(5000).using(window.robot);
  153. newwin.requireVisible();
  154. }
  155. public void editGroupCheck(final String button) {
  156. ActionManager.makeGroup("amd-ui-test1");
  157. setupWindow();
  158. window.list().selectItem("amd-ui-test1").requireSelectedItems("amd-ui-test1");
  159. window.panel(new ClassFinder<JPanel>(JPanel.class, "Groups"))
  160. .button(JButtonByTextMatcher.withText("Edit")).requireEnabled().click();
  161. DialogFixture newwin = WindowFinder.findDialog(StandardInputDialog.class)
  162. .withTimeout(5000).using(window.robot);
  163. newwin.requireVisible();
  164. assertEquals("Edit action group", newwin.target.getTitle());
  165. assertEquals("amd-ui-test1",
  166. newwin.textBox(new ClassFinder<JTextComponent>(javax.swing.JTextField.class,
  167. null)).target.getText());
  168. newwin.textBox(new ClassFinder<JTextComponent>(javax.swing.JTextField.class,
  169. null)).deleteText().enterText("amd-ui-test2");
  170. newwin.button(JButtonByTextMatcher.withText(button)).requireEnabled().click();
  171. }
  172. @Test
  173. public void testEditGroupCancel() {
  174. editGroupCheck("Cancel");
  175. assertTrue(ActionManager.getGroups().containsKey("amd-ui-test1"));
  176. assertFalse(ActionManager.getGroups().containsKey("amd-ui-test2"));
  177. }
  178. @Test
  179. public void testEditGroupOK() {
  180. editGroupCheck("OK");
  181. assertFalse(ActionManager.getGroups().containsKey("amd-ui-test1"));
  182. assertTrue(ActionManager.getGroups().containsKey("amd-ui-test2"));
  183. }
  184. protected void setupWindow() {
  185. window = new DialogFixture(ActionsManagerDialog.getActionsManagerDialog(null, null));
  186. window.robot.settings().eventMode(EventMode.AWT);
  187. window.show();
  188. }
  189. }