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.5KB

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