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.

InputTextFrameTest.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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.components.frames;
  23. import com.dmdirc.Main;
  24. import com.dmdirc.WritableFrameContainer;
  25. import com.dmdirc.addons.ui_swing.ClassComponentMatcher;
  26. import com.dmdirc.addons.ui_swing.MainFrame;
  27. import com.dmdirc.config.ConfigManager;
  28. import com.dmdirc.config.IdentityManager;
  29. import com.dmdirc.config.InvalidIdentityFileException;
  30. import com.dmdirc.addons.ui_swing.SwingController;
  31. import com.dmdirc.addons.ui_swing.SwingWindowFactory;
  32. import com.dmdirc.addons.ui_swing.components.TextAreaInputField;
  33. import com.dmdirc.plugins.PluginManager;
  34. import com.dmdirc.ui.messages.IRCDocument;
  35. import org.junit.After;
  36. import org.junit.Before;
  37. import org.junit.Test;
  38. import org.uispec4j.Trigger;
  39. import org.uispec4j.UISpecTestCase;
  40. import org.uispec4j.Window;
  41. import org.uispec4j.interception.WindowInterceptor;
  42. import static org.mockito.Mockito.*;
  43. public class InputTextFrameTest extends UISpecTestCase {
  44. static InputTextFrame tf;
  45. static String text;
  46. static {
  47. try {
  48. IdentityManager.load();
  49. } catch (InvalidIdentityFileException ex) {
  50. //Ignore
  51. }
  52. IdentityManager.getAddonIdentity().setOption("test", "windowMenuItems",
  53. "1");
  54. IdentityManager.getAddonIdentity().setOption("test",
  55. "windowMenuScrollInterval", "1");
  56. IdentityManager.getAddonIdentity().setOption("test", "debugEDT", "false");
  57. IdentityManager.getAddonIdentity().setOption("test",
  58. "textpanebackground", "");
  59. IdentityManager.getAddonIdentity().setOption("test", "desktopbackground",
  60. "");
  61. Main.ensureExists(PluginManager.getPluginManager(), "tabcompletion");
  62. final IRCDocument document = mock(IRCDocument.class);
  63. @SuppressWarnings("unchecked")
  64. final WritableFrameContainer<CustomInputFrame> container = mock(
  65. WritableFrameContainer.class);
  66. final ConfigManager config = mock(ConfigManager.class);
  67. when(container.getDocument()).thenReturn(document);
  68. when(container.getConfigManager()).thenReturn(config);
  69. when(config.getOption(anyString(), anyString())).thenReturn("mirc");
  70. final SwingController controller = mock(SwingController.class);
  71. when(controller.getDomain()).thenReturn("test");
  72. final SwingWindowFactory wf = new SwingWindowFactory(controller);
  73. when(controller.getWindowFactory()).thenReturn(wf);
  74. final MainFrame mainFrame = new MainFrame(controller);
  75. when(controller.getMainFrame()).thenReturn(mainFrame);
  76. tf = new CustomInputFrame(controller, container);
  77. text = "line1\nline2";
  78. }
  79. @Test
  80. public void testPasteDialogContents() throws InterruptedException {
  81. final Window dialog = getDialog();
  82. dialog.titleEquals("DMDirc: Multi-line paste").check();
  83. dialog.getButton("Edit").click();
  84. assertTrue(dialog.getTextBox(new ClassComponentMatcher(dialog,
  85. TextAreaInputField.class)).getText().equals(text));
  86. }
  87. @Test
  88. public void testPasteDialogWithTextBefore() throws InterruptedException {
  89. tf.getInputField().setText("testing:");
  90. final Window dialog = getDialog();
  91. dialog.titleEquals("DMDirc: Multi-line paste").check();
  92. dialog.getButton("Edit").click();
  93. assertTrue(dialog.getTextBox(new ClassComponentMatcher(dialog,
  94. TextAreaInputField.class)).getText().equals("testing:" + text));
  95. }
  96. @Test
  97. public void testPasteDialogWithTextAfter() throws InterruptedException {
  98. tf.getInputField().setText(":testing");
  99. tf.getInputField().setCaretPosition(0);
  100. final Window dialog = getDialog();
  101. dialog.titleEquals("DMDirc: Multi-line paste").check();
  102. dialog.getButton("Edit").click();
  103. assertTrue(dialog.getTextBox(new ClassComponentMatcher(dialog,
  104. TextAreaInputField.class)).getText().equals(text + ":testing"));
  105. }
  106. @Test
  107. public void testPasteDialogWithTextAround() throws InterruptedException {
  108. tf.getInputField().setText("testing::testing");
  109. tf.getInputField().setCaretPosition(8);
  110. final Window dialog = getDialog();
  111. dialog.titleEquals("DMDirc: Multi-line paste").check();
  112. dialog.getButton("Edit").click();
  113. assertEquals("testing:" + text + ":testing",
  114. dialog.getTextBox(new ClassComponentMatcher(dialog,
  115. TextAreaInputField.class)).getText());
  116. }
  117. @Test
  118. public void testPasteDialogWithSelection() {
  119. tf.getInputField().setText("testing:SELECTED:testing");
  120. tf.getInputField().setSelectionStart(8);
  121. tf.getInputField().setSelectionEnd(16);
  122. final Window dialog = getDialog();
  123. dialog.titleEquals("DMDirc: Multi-line paste").check();
  124. dialog.getButton("Edit").click();
  125. assertEquals("testing:" + text + ":testing",
  126. dialog.getTextBox(new ClassComponentMatcher(dialog,
  127. TextAreaInputField.class)).getText());
  128. }
  129. /**
  130. * Creates a new paste dialog with the specified text for the specified
  131. * frame.
  132. *
  133. * @param frame Parent frame
  134. * @param text Text to "paste"
  135. *
  136. * @return Wrapped Dialog
  137. */
  138. private Window getDialog() {
  139. return WindowInterceptor.run(new PasteDialogTrigger(tf, text));
  140. }
  141. /**
  142. * Creates a new paste dialog for the specified frame with the specified text.
  143. */
  144. private class PasteDialogTrigger implements Trigger {
  145. private InputTextFrame frame;
  146. private String text;
  147. /**
  148. * Creates a new paste dialog for the specified frame with the specified text.
  149. *
  150. * @param frame Parent frame
  151. * @param text Text to "paste"
  152. */
  153. public PasteDialogTrigger(final InputTextFrame frame, final String text) {
  154. this.frame = frame;
  155. this.text = text;
  156. }
  157. /** {@inheritDoc} */
  158. @Override
  159. public void run() throws Exception {
  160. frame.doPaste(text);
  161. }
  162. }
  163. }