Browse Source

Convert inputtextframe tests to uispec4j

Replace selected text when using paste dialog

Fixes issue 4162

Change-Id: I0d4997c8ced84efbc1ed31276b48133ffcce909a
Reviewed-on: http://gerrit.dmdirc.com/1261
Automatic-Compile: DMDirc Local Commits <dmdirc@googlemail.com>
Reviewed-by: Chris Smith <chris@dmdirc.com>
tags/0.6.4
Greg 14 years ago
parent
commit
0b53e3261e

+ 30
- 0
src/com/dmdirc/addons/ui_swing/components/SwingInputField.java View File

@@ -439,6 +439,36 @@ public class SwingInputField extends JComponent implements InputField,
439 439
         });
440 440
     }
441 441
 
442
+    /**
443
+     * Sets the start index of the selection for this component.
444
+     *
445
+     * @param selectionStart Start index
446
+     */
447
+    public void setSelectionStart(final int selectionStart) {
448
+        UIUtilities.invokeLater(new Runnable() {
449
+
450
+            @Override
451
+            public void run() {
452
+                textField.setSelectionStart(selectionStart);
453
+            }
454
+        });
455
+    }
456
+
457
+    /**
458
+     * Sets the end index of the selection for this component
459
+     *
460
+     * @param selectionEnd End index
461
+     */
462
+    public void setSelectionEnd(final int selectionEnd) {
463
+        UIUtilities.invokeLater(new Runnable() {
464
+
465
+            @Override
466
+            public void run() {
467
+                textField.setSelectionEnd(selectionEnd);
468
+            }
469
+        });
470
+    }
471
+
442 472
     /** 
443 473
      * {@inheritDoc}
444 474
      * 

+ 1
- 0
src/com/dmdirc/addons/ui_swing/components/frames/InputTextFrame.java View File

@@ -364,6 +364,7 @@ public abstract class InputTextFrame extends TextFrame implements InputWindow,
364 364
         //check theres something to paste
365 365
         if (clipboard != null && (clipboardLines = getSplitLine(clipboard)).length
366 366
                 > 1) {
367
+            getInputField().replaceSelection("");
367 368
             final int caretPosition = getInputField().getCaretPosition();
368 369
             final String inputFieldText = getInputField().getText();
369 370
             final String text = inputFieldText.substring(0, caretPosition)

+ 61
- 0
test/com/dmdirc/addons/ui_swing/ClassComponentMatcher.java View File

@@ -0,0 +1,61 @@
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
+
23
+package com.dmdirc.addons.ui_swing;
24
+
25
+import java.awt.Component;
26
+
27
+import org.uispec4j.Panel;
28
+import org.uispec4j.finder.ComponentMatcher;
29
+
30
+/**
31
+ * Simple UISpec4J component matcher that matches by type.
32
+ */
33
+public class ClassComponentMatcher implements ComponentMatcher {
34
+
35
+    private final Panel comp;
36
+    private final Class clazz;
37
+
38
+    /**
39
+     * Creates a new component matcher, searching for any components matching
40
+     * the specified class in the specified component.
41
+     *
42
+     * @param comp Component to search in
43
+     * @param clazz Class to search for
44
+     */
45
+    public ClassComponentMatcher(final Panel comp, final Class clazz) {
46
+        this.comp = comp;
47
+        this.clazz = clazz;
48
+    }
49
+
50
+    /**
51
+     * {@inheritDoc}
52
+     * 
53
+     * @param component Component to match against
54
+     * 
55
+     * @return true iif the component matches
56
+     */
57
+    @Override
58
+    public boolean matches(final Component component) {
59
+        return component    .getClass() == clazz;
60
+    }
61
+}

+ 114
- 141
test/com/dmdirc/addons/ui_swing/components/frames/InputTextFrameTest.java View File

@@ -23,43 +23,39 @@
23 23
 package com.dmdirc.addons.ui_swing.components.frames;
24 24
 
25 25
 import com.dmdirc.Main;
26
+import com.dmdirc.WritableFrameContainer;
27
+import com.dmdirc.addons.ui_swing.ClassComponentMatcher;
28
+import com.dmdirc.addons.ui_swing.MainFrame;
26 29
 import com.dmdirc.config.ConfigManager;
27 30
 import com.dmdirc.config.IdentityManager;
28 31
 import com.dmdirc.config.InvalidIdentityFileException;
29
-import com.dmdirc.harness.TestConfigManagerMap;
30
-import com.dmdirc.harness.TestWritableFrameContainer;
31
-import com.dmdirc.harness.ui.ClassFinder;
32
-import com.dmdirc.ui.WindowManager;
33 32
 import com.dmdirc.addons.ui_swing.SwingController;
34
-import com.dmdirc.addons.ui_swing.UIUtilities;
33
+import com.dmdirc.addons.ui_swing.SwingWindowFactory;
35 34
 import com.dmdirc.addons.ui_swing.components.TextAreaInputField;
36 35
 import com.dmdirc.plugins.PluginManager;
37
-import com.dmdirc.ui.interfaces.InputWindow;
36
+import com.dmdirc.ui.messages.IRCDocument;
38 37
 
39
-import java.awt.event.KeyEvent;
40 38
 
41
-import org.fest.swing.core.KeyPressInfo;
42
-import org.fest.swing.core.matcher.DialogMatcher;
43
-import org.fest.swing.core.matcher.JButtonMatcher;
44
-import org.fest.swing.fixture.DialogFixture;
45
-import org.fest.swing.fixture.FrameFixture;
46
-import org.fest.swing.fixture.JInternalFrameFixture;
47 39
 import org.junit.After;
48 40
 import org.junit.Before;
49
-import org.junit.BeforeClass;
50 41
 import org.junit.Test;
51
-
52
-public class InputTextFrameTest {
53
-
54
-    static FrameFixture mainframe;
55
-    static JInternalFrameFixture window;
56
-    static TestConfigManagerMap cmmap;
57
-    static TestWritableFrameContainer<InputWindow> owner;
58
-    static SwingController controller;
59
-
60
-    @BeforeClass
61
-    public static void setUpClass() throws InvalidIdentityFileException {
62
-        IdentityManager.load();
42
+import org.uispec4j.Trigger;
43
+import org.uispec4j.UISpecTestCase;
44
+import org.uispec4j.Window;
45
+import org.uispec4j.interception.WindowInterceptor;
46
+import static org.mockito.Mockito.*;
47
+
48
+public class InputTextFrameTest extends UISpecTestCase {
49
+
50
+    static InputTextFrame tf;
51
+    static String text;
52
+
53
+    static {
54
+        try {
55
+            IdentityManager.load();
56
+        } catch (InvalidIdentityFileException ex) {
57
+            //Ignore
58
+        }
63 59
         IdentityManager.getAddonIdentity().setOption("test", "windowMenuItems",
64 60
                 "1");
65 61
         IdentityManager.getAddonIdentity().setOption("test",
@@ -69,142 +65,119 @@ public class InputTextFrameTest {
69 65
                 "textpanebackground", "");
70 66
         IdentityManager.getAddonIdentity().setOption("test", "desktopbackground",
71 67
                 "");
72
-        controller = new SwingController();
73
-        controller.setDomain("test");
74
-        controller.onLoad();
75
-
76 68
         Main.ensureExists(PluginManager.getPluginManager(), "tabcompletion");
77 69
 
78
-        UIUtilities.initUISettings();
79
-
80
-        mainframe = new FrameFixture(controller.getMainWindow());
81
-    }
82
-
83
-    @Before
84
-    public void setUp() {
85
-        cmmap = new TestConfigManagerMap();
86
-        cmmap.settings.put("ui.pasteProtectionLimit", "1");
87
-
88
-        owner = new TestWritableFrameContainer<InputWindow>(512, cmmap,
89
-                InputWindow.class);
90
-        setupWindow(cmmap);
91
-    }
92
-
93
-    @After
94
-    public void tearDown() {
95
-        UIUtilities.invokeAndWait(new Runnable() {
96
-
97
-            @Override
98
-            public void run() {
99
-                window.close();
100
-                controller.getWindowFactory().getSwingWindow(owner).close();
101
-                WindowManager.removeWindow(owner);
102
-            }
103
-        });
70
+        final IRCDocument document = mock(IRCDocument.class);
71
+        @SuppressWarnings("unchecked")
72
+        final WritableFrameContainer<CustomInputFrame> container = mock(
73
+                WritableFrameContainer.class);
74
+        final ConfigManager config = mock(ConfigManager.class);
75
+        when(container.getDocument()).thenReturn(document);
76
+        when(container.getConfigManager()).thenReturn(config);
77
+        when(config.getOption(anyString(), anyString())).thenReturn("mirc");
78
+        final SwingController controller = mock(SwingController.class);
79
+        when(controller.getDomain()).thenReturn("test");
80
+        final SwingWindowFactory wf = new SwingWindowFactory(controller);
81
+        when(controller.getWindowFactory()).thenReturn(wf);
82
+        final MainFrame mainFrame = new MainFrame(controller);
83
+        when(controller.getMainFrame()).thenReturn(mainFrame);
84
+
85
+        tf = new CustomInputFrame(controller, container);
86
+        text = "line1\nline2";
104 87
     }
105 88
 
106 89
     @Test
107 90
     public void testPasteDialogContents() throws InterruptedException {
108
-        UIUtilities.invokeAndWait(new Runnable() {
109
-
110
-            @Override
111
-            public void run() {
112
-                ((InputTextFrame) window.target).doPaste("line1\nline2");
113
-
114
-                final DialogFixture dlg = mainframe.dialog(DialogMatcher.
115
-                        withTitle(
116
-                        "DMDirc: Multi-line paste").andShowing());
117
-
118
-                dlg.requireVisible().button(JButtonMatcher.withText("Edit")).
119
-                        click();
120
-                dlg.textBox(new ClassFinder<TextAreaInputField>(
121
-                        TextAreaInputField.class,
122
-                        null)).requireText("line1\nline2");
123
-                dlg.target.dispose();
124
-            }
125
-        });
91
+        final Window dialog = getDialog();
92
+        dialog.titleEquals("DMDirc: Multi-line paste").check();
93
+        dialog.getButton("Edit").click();
94
+        assertTrue(dialog.getTextBox(new ClassComponentMatcher(dialog,
95
+                TextAreaInputField.class)).getText().equals(text));
126 96
     }
127 97
 
128 98
     @Test
129 99
     public void testPasteDialogWithTextBefore() throws InterruptedException {
130
-        UIUtilities.invokeAndWait(new Runnable() {
131
-
132
-            @Override
133
-            public void run() {
134
-                window.textBox().enterText("testing:");
135
-                ((InputTextFrame) window.target).doPaste("line1\nline2");
136
-
137
-                final DialogFixture dlg = mainframe.dialog(DialogMatcher.
138
-                        withTitle("DMDirc: Multi-line paste").andShowing());
139
-
140
-                dlg.requireVisible().button(JButtonMatcher.withText("Edit")).
141
-                        click();
142
-                dlg.textBox(new ClassFinder<TextAreaInputField>(
143
-                        TextAreaInputField.class, null)).requireText(
144
-                        "testing:line1\nline2");
145
-                dlg.target.dispose();
146
-            }
147
-        });
100
+        tf.getInputField().setText("testing:");
101
+        final Window dialog = getDialog();
102
+        dialog.titleEquals("DMDirc: Multi-line paste").check();
103
+        dialog.getButton("Edit").click();
104
+        assertTrue(dialog.getTextBox(new ClassComponentMatcher(dialog,
105
+                TextAreaInputField.class)).getText().equals("testing:" + text));
148 106
     }
149 107
 
150 108
     @Test
151 109
     public void testPasteDialogWithTextAfter() throws InterruptedException {
152
-        UIUtilities.invokeAndWait(new Runnable() {
153
-
154
-            @Override
155
-            public void run() {
156
-                window.textBox().enterText("<- testing").pressAndReleaseKey(
157
-                        KeyPressInfo.keyCode(KeyEvent.VK_HOME));
158
-                ((InputTextFrame) window.target).doPaste("line1\nline2");
159
-
160
-                final DialogFixture dlg = mainframe.dialog(DialogMatcher.
161
-                        withTitle(
162
-                        "DMDirc: Multi-line paste").andShowing());
163
-
164
-                dlg.requireVisible().button(JButtonMatcher.withText("Edit")).
165
-                        click();
166
-                dlg.textBox(new ClassFinder<TextAreaInputField>(
167
-                        TextAreaInputField.class,
168
-                        null)).requireText("line1\nline2<- testing");
169
-                dlg.target.dispose();
170
-            }
171
-        });
110
+        tf.getInputField().setText(":testing");
111
+        tf.getInputField().setCaretPosition(0);
112
+        final Window dialog = getDialog();
113
+        dialog.titleEquals("DMDirc: Multi-line paste").check();
114
+        dialog.getButton("Edit").click();
115
+        assertTrue(dialog.getTextBox(new ClassComponentMatcher(dialog,
116
+                TextAreaInputField.class)).getText().equals(text + ":testing"));
172 117
     }
173 118
 
174 119
     @Test
175 120
     public void testPasteDialogWithTextAround() throws InterruptedException {
176
-        UIUtilities.invokeAndWait(new Runnable() {
177
-
178
-            @Override
179
-            public void run() {
180
-                window.textBox().enterText("testing:<- testing").selectText(8, 8);
181
-                ((InputTextFrame) window.target).doPaste("line1\nline2");
182
-
183
-                final DialogFixture dlg = mainframe.dialog(DialogMatcher.
184
-                        withTitle(
185
-                        "DMDirc: Multi-line paste").andShowing());
186
-
187
-                dlg.requireVisible().button(JButtonMatcher.withText("Edit")).
188
-                        click();
189
-                dlg.textBox(new ClassFinder<TextAreaInputField>(
190
-                        TextAreaInputField.class,
191
-                        null)).requireText("testing:line1\nline2<- testing");
192
-                dlg.target.dispose();
193
-            }
194
-        });
121
+        tf.getInputField().setText("testing::testing");
122
+        tf.getInputField().setCaretPosition(8);
123
+        final Window dialog = getDialog();
124
+        dialog.titleEquals("DMDirc: Multi-line paste").check();
125
+        dialog.getButton("Edit").click();
126
+        assertEquals("testing:" + text + ":testing",
127
+                dialog.getTextBox(new ClassComponentMatcher(dialog,
128
+                TextAreaInputField.class)).getText());
195 129
     }
196 130
 
197
-    protected void setupWindow(final ConfigManager configManager) {
198
-        final CustomInputFrame titf = new CustomInputFrame(controller, owner);
199
-
200
-        owner.setTitle("testing123");
201
-        owner.addWindow(titf);
202
-
203
-        WindowManager.addWindow(owner);
131
+    @Test
132
+    public void testPasteDialogWithSelection() {
133
+        tf.getInputField().setText("testing:SELECTED:testing");
134
+        tf.getInputField().setSelectionStart(8);
135
+        tf.getInputField().setSelectionEnd(16);
136
+        final Window dialog = getDialog();
137
+        dialog.titleEquals("DMDirc: Multi-line paste").check();
138
+        dialog.getButton("Edit").click();
139
+        assertEquals("testing:" + text + ":testing",
140
+                dialog.getTextBox(new ClassComponentMatcher(dialog,
141
+                TextAreaInputField.class)).getText());
142
+    }
204 143
 
205
-        titf.open();
144
+    /**
145
+     * Creates a new paste dialog with the specified text for the specified
146
+     * frame.
147
+     *
148
+     * @param frame Parent frame
149
+     * @param text Text to "paste"
150
+     *
151
+     * @return Wrapped Dialog
152
+     */
153
+    private Window getDialog() {
154
+        return WindowInterceptor.run(new PasteDialogTrigger(tf, text));
155
+    }
206 156
 
207
-        window = new JInternalFrameFixture(mainframe.robot, titf);
208
-        window.robot.settings().eventPostingDelay(250);
157
+    /**
158
+     * Creates a new paste dialog for the specified frame with the specified text.
159
+     */
160
+    private class PasteDialogTrigger implements Trigger {
161
+
162
+        private InputTextFrame frame;
163
+        private String text;
164
+
165
+        /**
166
+         * Creates a new paste dialog for the specified frame with the specified text.
167
+         *
168
+         * @param frame Parent frame
169
+         * @param text Text to "paste"
170
+         */
171
+        public PasteDialogTrigger(final InputTextFrame frame, final String text) {
172
+            this.frame = frame;
173
+            this.text = text;
174
+        }
175
+
176
+        /** {@inheritDoc} */
177
+        @Override
178
+        public void run() throws Exception {
179
+            frame.doPaste(text);
180
+        }
209 181
     }
182
+
210 183
 }

Loading…
Cancel
Save