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

ActionEditorDialogTest.java 14KB

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