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.

ActionEditorDialogTest.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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.actioneditor;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.actions.Action;
  25. import com.dmdirc.actions.ActionManager;
  26. import com.dmdirc.addons.ui_swing.SwingController;
  27. import com.dmdirc.config.IdentityManager;
  28. import com.dmdirc.config.InvalidIdentityFileException;
  29. import com.dmdirc.harness.ui.ClassFinder;
  30. import com.dmdirc.harness.ui.JRadioButtonByTextMatcher;
  31. import com.dmdirc.addons.ui_swing.components.ImageButton;
  32. import com.dmdirc.addons.ui_swing.components.text.TextLabel;
  33. import java.awt.Component;
  34. import java.util.regex.Matcher;
  35. import java.util.regex.Pattern;
  36. import javax.swing.JButton;
  37. import javax.swing.JPanel;
  38. import javax.swing.JTextField;
  39. import javax.swing.text.JTextComponent;
  40. import org.fest.swing.core.matcher.JButtonMatcher;
  41. import org.fest.swing.core.matcher.JLabelMatcher;
  42. import org.fest.swing.fixture.DialogFixture;
  43. import org.fest.swing.fixture.JLabelFixture;
  44. import org.fest.swing.fixture.JPanelFixture;
  45. import org.junit.After;
  46. import org.junit.Before;
  47. import org.junit.BeforeClass;
  48. import org.junit.Test;
  49. import static org.junit.Assert.*;
  50. public class ActionEditorDialogTest {
  51. private DialogFixture window;
  52. @BeforeClass
  53. public static void setUpClass() throws InvalidIdentityFileException {
  54. IdentityManager.load();
  55. Main.setUI(new SwingController());
  56. ActionManager.init();
  57. }
  58. @Before
  59. public void setUp() throws InvalidIdentityFileException {
  60. if (!ActionManager.getGroups().containsKey("amd-ui-test1")) {
  61. ActionManager.makeGroup("amd-ui-test1");
  62. }
  63. }
  64. @After
  65. public void tearDown() {
  66. if (window != null) {
  67. window.cleanUp();
  68. }
  69. if (ActionManager.getGroups().containsKey("amd-ui-test1")) {
  70. ActionManager.removeGroup("amd-ui-test1");
  71. }
  72. }
  73. @Test
  74. public void testName() {
  75. setupWindow(null);
  76. window.panel(new ClassFinder<ActionNamePanel>(ActionNamePanel.class, null)).
  77. textBox().requireEnabled().requireEditable().requireEmpty();
  78. window.button(JButtonMatcher.withText("OK")).requireDisabled();
  79. }
  80. @Test
  81. public void testIssue1785() {
  82. // Invalidating+validating name allows enables OK button despite invalid conditions
  83. // 'Fix' was disabling the add trigger button when name was invalid
  84. setupWindow(null);
  85. window.panel(new ClassFinder<ActionNamePanel>(ActionNamePanel.class, null)).
  86. textBox().requireEnabled().requireEditable().requireEmpty();
  87. window.panel(new ClassFinder<ActionTriggersPanel>(ActionTriggersPanel.class, null)).
  88. button(JButtonMatcher.withText("Add")).requireDisabled();
  89. }
  90. @Test
  91. public void testTriggerWithNoArgs() {
  92. setupWindow(null);
  93. window.panel(new ClassFinder<ActionNamePanel>(ActionNamePanel.class, null)).
  94. textBox().enterText("test1");
  95. final JPanelFixture triggers = window.panel(
  96. new ClassFinder<ActionTriggersPanel>(ActionTriggersPanel.class, null));
  97. triggers.comboBox().selectItem("Client closed");
  98. triggers.button(JButtonMatcher.withText("Add")).requireEnabled().
  99. click();
  100. window.panel(new ClassFinder<ActionConditionsPanel>(ActionConditionsPanel.class, null)).
  101. button(JButtonMatcher.withText("Add")).requireDisabled();
  102. }
  103. @Test
  104. public void testBasicTriggers() {
  105. setupWindow(null);
  106. final JPanelFixture triggers = window.panel(
  107. new ClassFinder<ActionTriggersPanel>(ActionTriggersPanel.class, null));
  108. triggers.comboBox().requireDisabled();
  109. window.panel(new ClassFinder<ActionNamePanel>(ActionNamePanel.class, null)).
  110. textBox().enterText("test1");
  111. final int items = triggers.comboBox().target.getItemCount();
  112. triggers.comboBox().requireEnabled().selectItem("Channel message received");
  113. triggers.button(JButtonMatcher.withText("Add")).requireEnabled().
  114. click();
  115. final JLabelFixture label =
  116. triggers.label(JLabelMatcher.withText("Channel message received"));
  117. label.requireVisible();
  118. assertTrue(items > triggers.comboBox().target.getItemCount());
  119. window.button(JButtonMatcher.withText("OK")).requireEnabled();
  120. window.panel(new ClassFinder<ActionNamePanel>(ActionNamePanel.class, null)).
  121. textBox().deleteText();
  122. triggers.button(new ClassFinder<ImageButton>(ImageButton.class, null)).
  123. requireDisabled();
  124. triggers.comboBox().requireDisabled();
  125. window.panel(new ClassFinder<ActionNamePanel>(ActionNamePanel.class, null)).
  126. textBox().enterText("test1");
  127. triggers.button(new ClassFinder<ImageButton>(ImageButton.class, null)).
  128. requireEnabled().click();
  129. for (Component comp : triggers.panel(new ClassFinder<ActionTriggersListPanel>(ActionTriggersListPanel.class,
  130. null)).target.getComponents()) {
  131. assertNotSame(label.target, comp);
  132. }
  133. assertEquals(items, triggers.comboBox().target.getItemCount());
  134. window.button(JButtonMatcher.withText("OK")).requireDisabled();
  135. }
  136. @Test
  137. public void testBasicConditionTrees() {
  138. setupWindow(null);
  139. window.panel(new ClassFinder<ActionNamePanel>(ActionNamePanel.class, null)).
  140. textBox().enterText("test1");
  141. final JPanelFixture triggers = window.panel(
  142. new ClassFinder<ActionTriggersPanel>(ActionTriggersPanel.class, null));
  143. triggers.comboBox().selectItem("Channel message received");
  144. triggers.button(JButtonMatcher.withText("Add")).requireEnabled().
  145. click();
  146. window.radioButton(new JRadioButtonByTextMatcher("All of the conditions are true")).
  147. requireEnabled().requireSelected();
  148. window.radioButton(new JRadioButtonByTextMatcher("At least one of the conditions is true")).
  149. requireEnabled();
  150. window.radioButton(new JRadioButtonByTextMatcher("The conditions match a custom rule")).
  151. requireEnabled();
  152. window.panel(new ClassFinder<ActionConditionsTreePanel>(ActionConditionsTreePanel.class,
  153. null)).textBox(new ClassFinder<JTextField>(JTextField.class,
  154. null)).requireDisabled();
  155. window.button(JButtonMatcher.withText("OK")).requireEnabled();
  156. window.radioButton(new JRadioButtonByTextMatcher("The conditions match a custom rule")).
  157. click().requireSelected();
  158. window.panel(new ClassFinder<ActionConditionsTreePanel>(ActionConditionsTreePanel.class,
  159. null)).textBox(new ClassFinder<JTextField>(JTextField.class,
  160. null)).requireEnabled().enterText("invalid");
  161. window.button(JButtonMatcher.withText("OK")).requireDisabled();
  162. }
  163. @Test
  164. public void testConditionText() {
  165. setupWindow(null);
  166. window.panel(new ClassFinder<ActionNamePanel>(ActionNamePanel.class, null)).
  167. textBox().enterText("test1");
  168. final JPanelFixture triggers = window.panel(
  169. new ClassFinder<ActionTriggersPanel>(ActionTriggersPanel.class, null));
  170. triggers.comboBox().selectItem("Channel message received");
  171. triggers.button(JButtonMatcher.withText("Add")).requireEnabled().
  172. click();
  173. window.panel(new ClassFinder<ActionConditionsPanel>(ActionConditionsPanel.class, null)).
  174. button(JButtonMatcher.withText("Add")).requireEnabled().
  175. click();
  176. Pattern pattern = Pattern.compile(".+<body>(.+)</body>.+", Pattern.DOTALL);
  177. Matcher matcher = pattern.matcher(window.panel(new ClassFinder<ActionConditionDisplayPanel>(ActionConditionDisplayPanel.class,
  178. null)).textBox(new ClassFinder<TextLabel>(TextLabel.class,
  179. null)).target.getText());
  180. matcher.find();
  181. assertEquals("<p style=\"margin-top: 0\">\n \n </p>", matcher.group(1).trim());
  182. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  183. null)).comboBox("argument").selectItem("message");
  184. matcher = pattern.matcher(window.panel(new ClassFinder<ActionConditionDisplayPanel>(ActionConditionDisplayPanel.class,
  185. null)).textBox(new ClassFinder<TextLabel>(TextLabel.class,
  186. null)).target.getText());
  187. matcher.find();
  188. assertEquals("The message's ...", matcher.group(1).trim());
  189. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  190. null)).comboBox("component").selectItem("content");
  191. matcher = pattern.matcher(window.panel(new ClassFinder<ActionConditionDisplayPanel>(ActionConditionDisplayPanel.class,
  192. null)).textBox(new ClassFinder<TextLabel>(TextLabel.class,
  193. null)).target.getText());
  194. matcher.find();
  195. assertEquals("The message's content ...", matcher.group(1).trim());
  196. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  197. null)).comboBox("comparison").selectItem("contains");
  198. matcher = pattern.matcher(window.panel(new ClassFinder<ActionConditionDisplayPanel>(ActionConditionDisplayPanel.class,
  199. null)).textBox(new ClassFinder<TextLabel>(TextLabel.class,
  200. null)).target.getText());
  201. matcher.find();
  202. assertEquals("The message's content contains ''", matcher.group(1).trim());
  203. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  204. null)).textBox().enterText("foo");
  205. matcher = pattern.matcher(window.panel(new ClassFinder<ActionConditionDisplayPanel>(ActionConditionDisplayPanel.class,
  206. null)).textBox(new ClassFinder<TextLabel>(TextLabel.class,
  207. null)).target.getText());
  208. matcher.find();
  209. assertEquals("The message's content contains 'foo'", matcher.group(1).trim());
  210. }
  211. @Test
  212. public void testIllegalCondition() {
  213. setupWindow(null);
  214. window.panel(new ClassFinder<ActionNamePanel>(ActionNamePanel.class, null)).
  215. textBox().enterText("test1");
  216. final JPanelFixture triggers = window.panel(
  217. new ClassFinder<ActionTriggersPanel>(ActionTriggersPanel.class, null));
  218. triggers.comboBox().selectItem("Channel message received");
  219. triggers.button(JButtonMatcher.withText("Add")).requireEnabled().click();
  220. window.button(JButtonMatcher.withText("OK")).requireEnabled();
  221. window.panel(new ClassFinder<ActionConditionsPanel>(ActionConditionsPanel.class, null)).
  222. button(JButtonMatcher.withText("Add")).requireEnabled().click();
  223. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  224. null)).comboBox("argument").requireEnabled();
  225. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  226. null)).comboBox("component").requireDisabled();
  227. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  228. null)).comboBox("comparison").requireDisabled();
  229. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  230. null)).textBox().requireDisabled();
  231. window.button(JButtonMatcher.withText("OK")).requireDisabled();
  232. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  233. null)).comboBox("argument").selectItem("message");
  234. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  235. null)).comboBox("component").requireEnabled();
  236. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  237. null)).comboBox("comparison").requireDisabled();
  238. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  239. null)).textBox().requireDisabled();
  240. window.button(JButtonMatcher.withText("OK")).requireDisabled();
  241. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  242. null)).comboBox("component").selectItem("content");
  243. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  244. null)).comboBox("comparison").requireEnabled();
  245. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  246. null)).textBox().requireDisabled();
  247. window.button(JButtonMatcher.withText("OK")).requireDisabled();
  248. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  249. null)).comboBox("comparison").selectItem("contains");
  250. window.panel(new ClassFinder<ActionConditionEditorPanel>(ActionConditionEditorPanel.class,
  251. null)).textBox().requireEnabled();
  252. window.button(JButtonMatcher.withText("OK")).requireEnabled();
  253. }
  254. protected void setupWindow(final Action action) {
  255. window = new DialogFixture(ActionEditorDialog.getActionEditorDialog(null,
  256. "amd-ui-test1", action));
  257. window.show();
  258. }
  259. }