Parcourir la source

Add some tests for ObservableListDecorator

Change-Id: I33b73ffae31972928697ca885d3e4445ca98bbc4
Reviewed-on: http://gerrit.dmdirc.com/2211
Reviewed-by: Greg Holmes <greg@dmdirc.com>
Automatic-Compile: DMDirc Build Manager
tags/0.7rc1
Chris Smith il y a 12 ans
Parent
révision
41ba445b6f

+ 4
- 2
src/com/dmdirc/util/ObservableListDecorator.java Voir le fichier

@@ -191,8 +191,10 @@ public class ObservableListDecorator<T> implements ObservableList<T> {
191 191
 
192 192
         list.clear();
193 193
 
194
-        listeners.getCallable(ListObserver.class).onItemsRemoved(this, 0,
195
-                length - 1);
194
+        if (length > 0) {
195
+            listeners.getCallable(ListObserver.class).onItemsRemoved(this, 0,
196
+                    length - 1);
197
+        }
196 198
     }
197 199
 
198 200
     /** {@inheritDoc} */

+ 104
- 0
test/com/dmdirc/util/ObservableListDecoratorTest.java Voir le fichier

@@ -0,0 +1,104 @@
1
+/*
2
+ * Copyright (c) 2006-2011 DMDirc Developers
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.util;
24
+
25
+import java.util.Arrays;
26
+import java.util.List;
27
+import org.junit.Test;
28
+import java.util.LinkedList;
29
+
30
+import org.junit.Before;
31
+import static org.mockito.Mockito.*;
32
+
33
+public class ObservableListDecoratorTest {
34
+
35
+    private List<String> list;
36
+    private ObservableList<String> obslist;
37
+    private ListObserver observer;
38
+
39
+    @Before
40
+    public void setup() {
41
+        list = new LinkedList<String>();
42
+        obslist = new ObservableListDecorator<String>(list);
43
+        observer = mock(ListObserver.class);
44
+        obslist.addListListener(observer);
45
+    }
46
+
47
+    @Test
48
+    public void testAddingSingleEntryFiresListener() {
49
+        obslist.add("test");
50
+        verify(observer).onItemsAdded(obslist, 0, 0);
51
+        obslist.add("test");
52
+        verify(observer).onItemsAdded(obslist, 1, 1);
53
+    }
54
+
55
+    @Test
56
+    public void testAddingSingleEntryAtIndexFiresListener() {
57
+        list.addAll(Arrays.asList("one", "two", "three", "four"));
58
+        obslist.add(1, "one point five");
59
+        verify(observer).onItemsAdded(obslist, 1, 1);
60
+    }
61
+
62
+    @Test
63
+    public void testAddingRangeFiresListener() {
64
+        obslist.addAll(Arrays.asList("one", "two", "three", "four"));
65
+        verify(observer).onItemsAdded(obslist, 0, 3);
66
+        obslist.addAll(Arrays.asList("one", "two", "three", "four"));
67
+        verify(observer).onItemsAdded(obslist, 4, 7);
68
+    }
69
+
70
+    @Test
71
+    public void testAddingRangeAtIndexFiresListener() {
72
+        list.addAll(Arrays.asList("one", "two", "three", "four"));
73
+        obslist.addAll(1, Arrays.asList("one", "two", "three", "four"));
74
+        verify(observer).onItemsAdded(obslist, 1, 5);
75
+    }
76
+
77
+    @Test
78
+    public void testClearingFiresListener() {
79
+        list.addAll(Arrays.asList("one", "two", "three", "four"));
80
+        obslist.clear();
81
+        verify(observer).onItemsRemoved(obslist, 0, 3);
82
+    }
83
+
84
+    @Test
85
+    public void testClearingEmptyListDoesntFireListener() {
86
+        obslist.clear();
87
+        verify(observer, never()).onItemsRemoved(anyObject(), anyInt(), anyInt());
88
+    }
89
+
90
+    @Test
91
+    public void testRemovingByIndexFiresListener() {
92
+        list.addAll(Arrays.asList("one", "two", "three", "four"));
93
+        obslist.remove(2);
94
+        verify(observer).onItemsRemoved(obslist, 2, 2);
95
+    }
96
+
97
+    @Test
98
+    public void testRemovingByObjectFiresListener() {
99
+        list.addAll(Arrays.asList("one", "two", "three", "four"));
100
+        obslist.remove("three");
101
+        verify(observer).onItemsRemoved(obslist, 2, 2);
102
+    }
103
+
104
+}

Chargement…
Annuler
Enregistrer