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.

ObservableListDecoratorTest.java 3.7KB

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