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 16KB

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