Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

ActionEditorDialogTest.java 14KB

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