EntitiesList implements IsWidget { protected PaginationConstraints createPaginationCallback(final AbstractEntityManager.SearchResponse searchResponse) { if (searchResponse != null) { final int page = searchResponse.getPage(); final int total = searchResponse.getTotal(); final int totalPagesRounded = (int) Math.ceil(total / (double) searchResponse.getPageSize()); EntitiesList.this.totalPages = total > -1 ? total / searchResponse.getPageSize() : -1; final boolean hasNextPage = totalPages > -1 ? page < totalPagesRounded : searchResponse.hasNextPage(); final boolean notInFistPage = page > 1; final boolean isLastPageButtonEnabled = EntitiesList.this.totalPages > -1 && hasNextPage; return (new PaginationConstraints() { @Override public boolean isFirstPageEnabled() { return notInFistPage; } @Override public boolean isFirstPageVisible() { return notInFistPage; } @Override public boolean isPrevPageEnabled() { return notInFistPage; } @Override public boolean isPrevPageVisible() { return notInFistPage; } @Override public int getCurrentPage() { return page; } @Override public boolean isNextPageEnabled() { return hasNextPage; } @Override public boolean isNextPageVisible() { return hasNextPage; } @Override public boolean isLastPageEnabled() { return isLastPageButtonEnabled; } @Override public boolean isLastPageVisible() { return isLastPageButtonEnabled; } @Override public Integer getTotal() { return total > -1 ? total : null; } }); } return null; } @Inject  EntitiesList(LoadingBox loadingBox,
                        View view); void show(final AbstractEntityManager.SearchResponse<T> response,
                     final Callback<T> callback); void select(final String identifier); void unselect(final String identifier); void clear(); void setPageSize(int pageSize); void setEmptyEntitiesText(String emptyEntitiesText); void setEntityTitleSize(HeadingSize headingSize); @Override Widget asWidget(); public View view; }
@Test public void testCreatePaginationCallbackSinglePage() throws Exception { final Integer size = 10; List<User> users = buildUsersList(size); final boolean hasNextPage = false; final int page = 1; final int pageSize = 50; final AbstractEntityManager.SearchResponse<User> searchResponse = createSearchResponse(users, size, hasNextPage, "", page, pageSize); EntitiesList.PaginationConstraints constraints = presenter.createPaginationCallback(searchResponse); assertFalse(constraints.isFirstPageEnabled()); assertFalse(constraints.isFirstPageVisible()); assertFalse(constraints.isNextPageEnabled()); assertFalse(constraints.isNextPageVisible()); assertFalse(constraints.isPrevPageEnabled()); assertFalse(constraints.isPrevPageVisible()); assertFalse(constraints.isLastPageEnabled()); assertFalse(constraints.isLastPageVisible()); assertEquals(constraints.getTotal(), size); assertEquals(constraints.getCurrentPage(), page); }
@Test public void testCreatePaginationCallbackMultiplePagesAtFirstPage() throws Exception { final Integer size = 10; List<User> users = buildUsersList(size); final boolean hasNextPage = false; final int page = 1; final int pageSize = 5; final AbstractEntityManager.SearchResponse<User> searchResponse = createSearchResponse(users, size, hasNextPage, "", page, pageSize); EntitiesList.PaginationConstraints constraints = presenter.createPaginationCallback(searchResponse); assertFalse(constraints.isFirstPageEnabled()); assertFalse(constraints.isFirstPageVisible()); assertTrue(constraints.isNextPageEnabled()); assertTrue(constraints.isNextPageVisible()); assertFalse(constraints.isPrevPageEnabled()); assertFalse(constraints.isPrevPageVisible()); assertTrue(constraints.isLastPageEnabled()); assertTrue(constraints.isLastPageVisible()); assertEquals(constraints.getTotal(), size); assertEquals(constraints.getCurrentPage(), page); }
@Test public void testCreatePaginationCallbackMultiplePagesAtSecondPage() throws Exception { final Integer size = 10; List<User> users = buildUsersList(size); final boolean hasNextPage = false; final int page = 2; final int pageSize = 5; final AbstractEntityManager.SearchResponse<User> searchResponse = createSearchResponse(users, size, hasNextPage, "", page, pageSize); EntitiesList.PaginationConstraints constraints = presenter.createPaginationCallback(searchResponse); assertTrue(constraints.isFirstPageEnabled()); assertTrue(constraints.isFirstPageVisible()); assertFalse(constraints.isNextPageEnabled()); assertFalse(constraints.isNextPageVisible()); assertTrue(constraints.isPrevPageEnabled()); assertTrue(constraints.isPrevPageVisible()); assertFalse(constraints.isLastPageEnabled()); assertFalse(constraints.isLastPageVisible()); assertEquals(constraints.getTotal(), size); assertEquals(constraints.getCurrentPage(), page); }