Browse Source

Unit tests

Unit test for issue 3721

Change-Id: Ifc1e3a8c60576c0626098280d1eb93dea7fb7a3d
Reviewed-on: http://gerrit.dmdirc.com/839
Reviewed-by: Gregory Holmes <greg@dmdirc.com>
Automatic-Compile: Chris Smith <chris@dmdirc.com>
tags/0.6.3
Chris Smith 14 years ago
parent
commit
720d22f663

+ 1
- 1
src/com/dmdirc/addons/osd/OsdWindow.java View File

@@ -50,7 +50,7 @@ import net.miginfocom.swing.MigLayout;
50 50
  * about events to the user.
51 51
  * @author chris
52 52
  */
53
-public final class OsdWindow extends JDialog implements MouseListener,
53
+public class OsdWindow extends JDialog implements MouseListener,
54 54
         MouseMotionListener {
55 55
     
56 56
     /**

+ 95
- 0
test/com/dmdirc/addons/osd/OsdPolicyTest.java View File

@@ -0,0 +1,95 @@
1
+/*
2
+ * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes,
3
+ * Simon Mott
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ * SOFTWARE.
22
+ */
23
+
24
+package com.dmdirc.addons.osd;
25
+
26
+import java.util.ArrayList;
27
+import org.junit.Before;
28
+import org.junit.Test;
29
+import static org.junit.Assert.*;
30
+import static org.mockito.Mockito.*;
31
+
32
+public class OsdPolicyTest {
33
+
34
+    private OsdManager manager;
35
+
36
+    @Before
37
+    public void setup() {
38
+        final ArrayList<OsdWindow> windows = new ArrayList<OsdWindow>();
39
+
40
+        OsdWindow window = mock(OsdWindow.class);
41
+        when(window.getY()).thenReturn(75);
42
+        when(window.getHeight()).thenReturn(20);
43
+        when(window.isVisible()).thenReturn(false);
44
+        windows.add(window);
45
+
46
+        window = mock(OsdWindow.class);
47
+        when(window.getY()).thenReturn(100);
48
+        when(window.getHeight()).thenReturn(20);
49
+        when(window.isVisible()).thenReturn(true);
50
+        windows.add(window);
51
+
52
+        window = mock(OsdWindow.class);
53
+        when(window.getY()).thenReturn(125);
54
+        when(window.getHeight()).thenReturn(20);
55
+        when(window.isVisible()).thenReturn(true);
56
+        windows.add(window);
57
+
58
+        window = mock(OsdWindow.class);
59
+        when(window.getY()).thenReturn(150);
60
+        when(window.getHeight()).thenReturn(20);
61
+        when(window.isVisible()).thenReturn(true);
62
+        windows.add(window);
63
+
64
+        window = mock(OsdWindow.class);
65
+        when(window.getY()).thenReturn(175);
66
+        when(window.getHeight()).thenReturn(20);
67
+        when(window.isVisible()).thenReturn(false);
68
+        windows.add(window);
69
+
70
+        manager = mock(OsdManager.class);
71
+        when(manager.getWindowList()).thenReturn(windows);
72
+    }
73
+
74
+    @Test
75
+    public void testOnTop() {
76
+        assertEquals(42, OsdPolicy.ONTOP.getYPosition(manager, 42));
77
+    }
78
+
79
+    @Test
80
+    public void testClose() {
81
+        assertEquals(42, OsdPolicy.CLOSE.getYPosition(manager, 42));
82
+        verify(manager).closeAll();
83
+    }
84
+
85
+    @Test
86
+    public void testDown() {
87
+        assertTrue(OsdPolicy.DOWN.getYPosition(manager, 75) > 170);
88
+    }
89
+
90
+    @Test
91
+    public void testUp() {
92
+        assertTrue(OsdPolicy.UP.getYPosition(manager, 175) < 80);
93
+    }
94
+
95
+}

+ 48
- 80
test/com/dmdirc/addons/ui_swing/dialogs/actionsmanager/ActionsGroupPanelTest.java View File

@@ -26,23 +26,23 @@ import com.dmdirc.actions.Action;
26 26
 import com.dmdirc.actions.ActionGroup;
27 27
 import com.dmdirc.actions.CoreActionType;
28 28
 import com.dmdirc.actions.interfaces.ActionType;
29
+import com.dmdirc.harness.ui.WindowButtonHandler;
29 30
 import java.util.ArrayList;
30 31
 import java.util.Arrays;
31 32
 import org.junit.Before;
32 33
 import org.junit.Test;
33 34
 import org.uispec4j.Panel;
34 35
 import org.uispec4j.Table;
35
-import org.uispec4j.Trigger;
36 36
 import org.uispec4j.UISpecTestCase;
37
-import org.uispec4j.Window;
38
-import org.uispec4j.interception.WindowHandler;
39 37
 import org.uispec4j.interception.WindowInterceptor;
40
-import static org.junit.Assume.*;
41 38
 import static org.mockito.Mockito.*;
42 39
 
43 40
 public class ActionsGroupPanelTest extends UISpecTestCase {
44 41
 
45
-    private static Action action1, action2, action3, action4;
42
+    private Action action1, action2, action3, action4;
43
+    private ActionGroup group;
44
+    private Panel panel;
45
+    private Table table;
46 46
     
47 47
     @Before
48 48
     @Override
@@ -52,7 +52,7 @@ public class ActionsGroupPanelTest extends UISpecTestCase {
52 52
         when(action1.getTriggers()).thenReturn(new ActionType[] {
53 53
             CoreActionType.ACTION_CREATED, CoreActionType.ACTION_DELETED,
54 54
         });
55
-        when(action1.getResponse()).thenReturn(new String[0]);
55
+        when(action1.getResponse()).thenReturn(new String[] { "A" });
56 56
                 
57 57
         action2 = mock(Action.class);
58 58
         when(action2.getName()).thenReturn("name 2");
@@ -80,128 +80,95 @@ public class ActionsGroupPanelTest extends UISpecTestCase {
80 80
         when(action4.getResponse()).thenReturn(new String[] {
81 81
             "Response line 1",
82 82
             "Response line 2",
83
-            "Response line 3",
83
+            "Response line 4",
84 84
         });
85
+
86
+        group = mock(ActionGroup.class);
87
+        when(group.getActions()).thenReturn(new ArrayList<Action>(Arrays.asList(new Action[]{
88
+            action1, action2, action3, action4,
89
+        })));
90
+
91
+        panel = new Panel(new ActionsGroupPanel(null, group));
92
+        table = panel.getTable();
85 93
     }
86 94
 
87 95
     @Test
88 96
     public void testTable() {
89
-        final ActionGroup group = mock(ActionGroup.class);
90
-        when(group.getActions()).thenReturn(Arrays.asList(new Action[]{
91
-            action1, action2, action3, action4,
92
-        }));
97
+        assertTrue(table.getHeader().contentEquals("Name", "Trigger", "Response"));
93 98
 
94
-        final Panel panel = new Panel(new ActionsGroupPanel(null, group));
95
-        final Table table = panel.getTable();
99
+        assertTrue(table.contentEquals(new String[][]{
100
+            {"name 1", "Action created", "A"},
101
+            {"name 2", "Action created", ""},
102
+            {"name 3", "Action created", "Response line 1, Response line 2, Response line 3"},
103
+            {"name 4", "Action created", "Response line 1, Response line 2, Response line 4"},
104
+        }));
105
+    }
96 106
 
107
+    @Test
108
+    public void testTableSorting() {
97 109
         assertTrue(table.getHeader().contentEquals("Name", "Trigger", "Response"));
98 110
 
111
+        table.getHeader().click("Name");
112
+
113
+        assertTrue(table.contentEquals(new String[][]{
114
+            {"name 4", "Action created", "Response line 1, Response line 2, Response line 4"},
115
+            {"name 3", "Action created", "Response line 1, Response line 2, Response line 3"},
116
+            {"name 2", "Action created", ""},
117
+            {"name 1", "Action created", "A"},
118
+        }));
119
+
120
+        table.getHeader().click("Response");
121
+
99 122
         assertTrue(table.contentEquals(new String[][]{
100
-            {"name 1", "Action created", ""},
101 123
             {"name 2", "Action created", ""},
124
+            {"name 1", "Action created", "A"},
102 125
             {"name 3", "Action created", "Response line 1, Response line 2, Response line 3"},
103
-            {"name 4", "Action created", "Response line 1, Response line 2, Response line 3"},
126
+            {"name 4", "Action created", "Response line 1, Response line 2, Response line 4"},
104 127
         }));
105 128
     }
106 129
 
107 130
     @Test
108 131
     public void testDeletingCancel() {
109
-        final ActionGroup group = mock(ActionGroup.class);
110
-        when(group.getActions()).thenReturn(new ArrayList<Action>(Arrays.asList(new Action[]{
111
-            action1, action2, action3, action4,
112
-        })));
113
-
114
-        final Panel panel = new Panel(new ActionsGroupPanel(null, group));
115
-        final Table table = panel.getTable();
116
-
117 132
         table.selectRow(1);
118 133
 
119 134
         WindowInterceptor.init(panel.getButton("Delete").triggerClick())
120
-                .process(new WindowHandler() {
121
-
122
-            @Override
123
-            public Trigger process(final Window window) throws Exception {
124
-                assumeTrue("Confirm deletion".equals(window.getTitle()));
125
-
126
-                return window.getButton("No").triggerClick();
127
-            }
128
-        }).run();
135
+                .process(new WindowButtonHandler("Confirm deletion", "No")).run();
129 136
 
130 137
         assertTrue(table.contentEquals(new String[][]{
131
-            {"name 1", "Action created", ""},
138
+            {"name 1", "Action created", "A"},
132 139
             {"name 2", "Action created", ""},
133 140
             {"name 3", "Action created", "Response line 1, Response line 2, Response line 3"},
134
-            {"name 4", "Action created", "Response line 1, Response line 2, Response line 3"},
141
+            {"name 4", "Action created", "Response line 1, Response line 2, Response line 4"},
135 142
         }));
136 143
         verify(group, never()).deleteAction((Action) anyObject());
137 144
     }
138 145
 
139 146
     public void testDeletingOk() {
140
-        final ActionGroup group = mock(ActionGroup.class);
141
-        when(group.getActions()).thenReturn(new ArrayList<Action>(Arrays.asList(new Action[]{
142
-            action1, action2, action3, action4,
143
-        })));
144
-
145
-        final Panel panel = new Panel(new ActionsGroupPanel(null, group));
146
-        final Table table = panel.getTable();
147
-
148 147
         table.selectRow(1);
149 148
 
150 149
         WindowInterceptor.init(panel.getButton("Delete").triggerClick())
151
-                .process(new WindowHandler() {
152
-
153
-            @Override
154
-            public Trigger process(final Window window) throws Exception {
155
-                assumeTrue("Confirm deletion".equals(window.getTitle()));
156
-
157
-                return window.getButton("Yes").triggerClick();
158
-            }
159
-        }).run();
150
+                .process(new WindowButtonHandler("Confirm deletion", "Yes")).run();
160 151
 
161 152
         verify(group).deleteAction(same(action2));
162 153
     }
163 154
 
164 155
     public void testDeletingSorted() {
165
-        final ActionGroup group = mock(ActionGroup.class);
166
-        when(group.getActions()).thenReturn(new ArrayList<Action>(Arrays.asList(new Action[]{
167
-            action1, action2, action3, action4,
168
-        })));
169
-
170
-        final Panel panel = new Panel(new ActionsGroupPanel(null, group));
171
-        final Table table = panel.getTable();
172
-
173 156
         table.getHeader().click("Name");
174 157
 
175 158
         table.selectRow(1);
176 159
 
177 160
         WindowInterceptor.init(panel.getButton("Delete").triggerClick())
178
-                .process(new WindowHandler() {
179
-
180
-            @Override
181
-            public Trigger process(final Window window) throws Exception {
182
-                assumeTrue("Confirm deletion".equals(window.getTitle()));
183
-
184
-                return window.getButton("Yes").triggerClick();
185
-            }
186
-        }).run();
161
+                .process(new WindowButtonHandler("Confirm deletion", "Yes")).run();
187 162
 
188 163
         verify(group).deleteAction(same(action3));
189 164
     }
190 165
 
191 166
     public void testTableDeleting() {
192
-        final ActionGroup group = mock(ActionGroup.class);
193
-        when(group.getActions()).thenReturn(new ArrayList<Action>(Arrays.asList(new Action[]{
194
-            action1, action2, action3, action4,
195
-        })));
196
-
197
-        final Panel panel = new Panel(new ActionsGroupPanel(null, group));
198
-        final Table table = panel.getTable();
199
-
200 167
         assertTrue(table.contentEquals(new String[][]{
201
-            {"name 1", "Action created", ""},
168
+            {"name 1", "Action created", "A"},
202 169
             {"name 2", "Action created", ""},
203 170
             {"name 3", "Action created", "Response line 1, Response line 2, Response line 3"},
204
-            {"name 4", "Action created", "Response line 1, Response line 2, Response line 3"},
171
+            {"name 4", "Action created", "Response line 1, Response line 2, Response line 4"},
205 172
         }));
206 173
 
207 174
         when(group.getActions()).thenReturn(new ArrayList<Action>(Arrays.asList(new Action[]{
@@ -209,11 +176,12 @@ public class ActionsGroupPanelTest extends UISpecTestCase {
209 176
         })));
210 177
 
211 178
         ((ActionsGroupPanel) panel.getAwtComponent()).actionDeleted("name 2");
179
+        ((ActionsGroupPanel) panel.getAwtComponent()).actionDeleted("name 7");
212 180
 
213 181
         assertTrue(table.contentEquals(new String[][]{
214
-            {"name 1", "Action created", ""},
182
+            {"name 1", "Action created", "A"},
215 183
             {"name 3", "Action created", "Response line 1, Response line 2, Response line 3"},
216
-            {"name 4", "Action created", "Response line 1, Response line 2, Response line 3"},
184
+            {"name 4", "Action created", "Response line 1, Response line 2, Response line 4"},
217 185
         }));
218 186
     }
219 187
 

+ 46
- 0
test/com/dmdirc/harness/ui/WindowButtonHandler.java View File

@@ -0,0 +1,46 @@
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.harness.ui;
24
+
25
+import org.uispec4j.Trigger;
26
+import org.uispec4j.Window;
27
+import org.uispec4j.interception.WindowHandler;
28
+import static org.junit.Assume.*;
29
+
30
+public class WindowButtonHandler extends WindowHandler {
31
+
32
+    private final String title, button;
33
+
34
+    public WindowButtonHandler(final String title, final String button) {
35
+        this.title = title;
36
+        this.button = button;
37
+    }
38
+
39
+    @Override
40
+    public Trigger process(final Window window) throws Exception {
41
+        assumeTrue(title.equals(window.getTitle()));
42
+
43
+        return window.getButton(button).triggerClick();
44
+    }
45
+
46
+}

Loading…
Cancel
Save