ViewImpl implements View { @SuppressWarnings("unchecked") @Override public void addToSlot(Object slot, IsWidget content) { if (hasWidgetSlots.containsKey(slot)) { if (orderedSlots.containsKey(slot)) { final List<Comparable<Comparable<?>>> list = orderedSlots.get(slot); final int index = Collections.binarySearch(list, (Comparable<Comparable<?>>) content); final int insertIdx; if (index > 0) { insertIdx = index; } else { insertIdx = -index - 1; } list.add(insertIdx, (Comparable<Comparable<?>>) content); final InsertPanel insertPanel = (InsertPanel) hasWidgetSlots.get(slot); insertPanel.insert(content.asWidget(), insertIdx); } else { hasWidgetSlots.get(slot).add(content.asWidget()); } } }  @SuppressWarnings("unchecked") @Override void addToSlot(Object slot, IsWidget content); @Override void removeFromSlot(Object slot, IsWidget content); @SuppressWarnings("unchecked") @Override void setInSlot(Object slot, IsWidget content); @Override Widget asWidget();  }
@Test public void testInsertSingle() { final Widget c = mock(Widget.class); final ComparableContent content = new ComparableContent("TEST", c); view.addToSlot(slot, content); verify(container).insert(c, 0); }
@Test public void testInsertBack() { final Widget a = mock(Widget.class); final Widget b = mock(Widget.class); final Widget c = mock(Widget.class); final ComparableContent aaa = new ComparableContent("AAA", a); final ComparableContent bbb = new ComparableContent("BBB", b); final ComparableContent ccc = new ComparableContent("CCC", c); view.addToSlot(slot, aaa); view.addToSlot(slot, bbb); view.addToSlot(slot, ccc); final InOrder inOrder = inOrder(container); inOrder.verify(container).insert(a, 0); inOrder.verify(container).insert(b, 1); inOrder.verify(container).insert(c, 2); }
@Test public void testInsertFront() { final Widget a = mock(Widget.class); final Widget b = mock(Widget.class); final Widget c = mock(Widget.class); final ComparableContent aaa = new ComparableContent("AAA", a); final ComparableContent bbb = new ComparableContent("BBB", b); final ComparableContent ccc = new ComparableContent("CCC", c); view.addToSlot(slot, ccc); view.addToSlot(slot, bbb); view.addToSlot(slot, aaa); final InOrder inOrder = inOrder(container); inOrder.verify(container).insert(c, 0); inOrder.verify(container).insert(b, 0); inOrder.verify(container).insert(a, 0); }
@Test public void testInsertRandomCheckOrder() { final List<String> names = new ArrayList<>(); doAnswer(new Answer<Void>() { @Override public Void answer(InvocationOnMock invocationOnMock) throws Throwable { final Widget w = (Widget) invocationOnMock.getArguments()[0]; final int idx = (int) invocationOnMock.getArguments()[1]; names.add(idx, w.getTitle()); return null; } }).when(container).insert(any(Widget.class), anyInt()); for (int i = 0; i < 1000; ++i) { final String name = Double.toString(Math.random()); final Widget w = mock(Widget.class); when(w.getTitle()).thenReturn(name); final ComparableContent cc = new ComparableContent(name, w); view.addToSlot(slot, cc); } assertTrue("Container should have 1000 members.", names.size() == 1000); final List<String> namesCopy = new ArrayList<>(names); Collections.sort(names); assertEquals("Container elements should be sorted.", namesCopy, names); }