public void testSetKeyboardSelectedRowChangePage() {
HasData<String> listView = new MockHasData<String>();
MockView<String> view = new MockView<String>();
HasDataPresenter<String> presenter = new HasDataPresenter<String>(listView, view, 10, null);
presenter.setVisibleRange(new Range(10, 10));
populatePresenter(presenter);
presenter.flush();
// Default policy is CHANGE_PAGE.
assertEquals(KeyboardPagingPolicy.CHANGE_PAGE, presenter.getKeyboardPagingPolicy());
// Default to row 0.
assertEquals(0, presenter.getKeyboardSelectedRow());
view.assertKeyboardSelectedRowEmpty();
// Move to middle.
presenter.setKeyboardSelectedRow(1, false, false);
assertEquals("test 11", presenter.getKeyboardSelectedRowValue());
presenter.flush();
assertEquals(1, presenter.getKeyboardSelectedRow());
assertEquals("test 11", presenter.getKeyboardSelectedRowValue());
view.assertKeyboardSelectedRow(0, false);
view.assertKeyboardSelectedRow(1, true);
// Move to same row (should not early out).
presenter.setKeyboardSelectedRow(1, false, true);
assertEquals("test 11", presenter.getKeyboardSelectedRowValue());
presenter.flush();
assertEquals(1, presenter.getKeyboardSelectedRow());
assertEquals("test 11", presenter.getKeyboardSelectedRowValue());
view.assertKeyboardSelectedRow(1, false);
view.assertKeyboardSelectedRow(1, true);
// Move to last row.
presenter.setKeyboardSelectedRow(9, false, false);
assertEquals("test 19", presenter.getKeyboardSelectedRowValue());
presenter.flush();
assertEquals(9, presenter.getKeyboardSelectedRow());
assertEquals("test 19", presenter.getKeyboardSelectedRowValue());
view.assertKeyboardSelectedRow(1, false);
view.assertKeyboardSelectedRow(9, true);
assertEquals(10, presenter.getVisibleRange().getStart());
assertEquals(10, presenter.getVisibleRange().getLength());
// Move to next page.
presenter.setKeyboardSelectedRow(10, false, false);
populatePresenter(presenter);
assertNull(presenter.getKeyboardSelectedRowValue());
presenter.flush();
assertEquals(0, presenter.getKeyboardSelectedRow());
assertEquals("test 20", presenter.getKeyboardSelectedRowValue());
view.assertReplaceAllChildrenCalled(10);
view.assertKeyboardSelectedRowEmpty();
assertEquals(20, presenter.getVisibleRange().getStart());
assertEquals(10, presenter.getVisibleRange().getLength());
// Negative index.
presenter.setKeyboardSelectedRow(-1, false, false);
populatePresenter(presenter);
assertNull(presenter.getKeyboardSelectedRowValue());
presenter.flush();
assertEquals(9, presenter.getKeyboardSelectedRow());
assertEquals("test 19", presenter.getKeyboardSelectedRowValue());
view.assertReplaceAllChildrenCalled(10);
view.assertKeyboardSelectedRowEmpty();
assertEquals(10, presenter.getVisibleRange().getStart());
assertEquals(10, presenter.getVisibleRange().getLength());
// Negative index out of range.
presenter.setVisibleRange(new Range(3, 10));
presenter.setKeyboardSelectedRow(3, false, false);
populatePresenter(presenter);
presenter.flush();
presenter.setKeyboardSelectedRow(-4, false, false);
populatePresenter(presenter);