public void shouldBeAbleToPersistAndRestoreMultipleStates() {
// For saving the visual category:
Category visualCategory = FILE;
ICategorizer categorizer = mock(ICategorizer.class);
given(categorizer.hasCategory(Mockito.<ICategory> any())).willReturn(true);
@SuppressWarnings("unchecked")
TreePathValueProvider valueProvider = new TreePathValueProvider(
categorizer, mock(IProvider.class), mock(IConverter.class),
visualCategory);
// For saving the column widths:
int width = 101;
TreeColumn column = new TreeColumn(tree, SWT.NONE);
column.setWidth(width);
// For saving the selected categories:
final List<Category> expectedCategories = asList(DATE, FILE);
final List<Category> actualCategories = Lists.newArrayList();
ICategoryProvider categoryProvider = mock(ICategoryProvider.class);
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
for (Object obj : invocation.getArguments()) {
actualCategories.add((Category) obj);
}
return null;
}
}).when(categoryProvider)
.setSelected(expectedCategories.toArray(new Category[0]));
// Save the states:
helper
.saveCategories(expectedCategories.toArray(new Category[0]))
.saveVisualCategory(visualCategory)
.saveColumnWidths(new TreeColumn[]{column});
// Change the states:
categoryProvider.setSelected(FILE);
valueProvider.setVisualCategory(DATE);
column.setWidth(1);
// Restore:
helper
.restoreCategories(categoryProvider)
.restoreVisualCategory(valueProvider)
.restoreColumnWidths(new TreeColumn[]{column});
assertThat(actualCategories, equalTo(expectedCategories));
assertThat(column.getWidth(), equalTo(width));
assertThat(valueProvider.getVisualCategory(),
equalTo((ICategory) visualCategory));
}