}
@Test
public void testSaveAndInitialSelection()
{
UserPicker userPicker = new UserClassFieldEditPane(editor.addField("User").getName()).getUserPicker();
userPicker.sendKeys("thomas").waitForSuggestions().sendKeys(Keys.ENTER);
editor.clickSaveAndView().edit();
UserClassFieldEditPane userField = new UserClassFieldEditPane("user1");
userPicker = userField.getUserPicker().waitToLoad();
List<UserElement> selectedUsers = userPicker.getAcceptedSuggestions();
Assert.assertEquals(1, selectedUsers.size());
assertUserElement(selectedUsers.get(0), "Thomas Mortagne");
Assert.assertEquals("", userPicker.getValue());
// Enable multiple selection.
userField.openConfigPanel();
userField.setMultipleSelect(true);
userField.closeConfigPanel();
// Re-take the user picker because the display has been reloaded.
userPicker = userField.getUserPicker();
// Select one more user.
userPicker.waitToLoad().sendKeys("admin").waitForSuggestions().sendKeys(Keys.ENTER);
editor.clickSaveAndContinue();
editor.clickCancel().edit();
userPicker = new UserClassFieldEditPane("user1").getUserPicker().waitToLoad();
selectedUsers = userPicker.getAcceptedSuggestions();
Assert.assertEquals(2, selectedUsers.size());
assertUserElement(selectedUsers.get(0), "Thomas Mortagne");
assertUserElement(selectedUsers.get(1), "Administrator", "Admin", "noavatar.png");
Assert.assertEquals("", userPicker.getValue());
// We should be able to input free text also.
userPicker.sendKeys("foobar").waitForSuggestions().sendKeys(Keys.ESCAPE).waitForSuggestionsToFadeOut();
editor.clickSaveAndContinue();
editor.clickCancel().edit();
userPicker = new UserClassFieldEditPane("user1").getUserPicker().waitToLoad();
selectedUsers = userPicker.getAcceptedSuggestions();
Assert.assertEquals(3, selectedUsers.size());
assertUserElement(selectedUsers.get(2), "foobar", "foobar", "noavatar.png");
Assert.assertEquals("", userPicker.getValue());
// Delete the fake user.
selectedUsers.get(2).delete();
Assert.assertEquals(2, userPicker.getAcceptedSuggestions().size());
// Delete all selected users.
userPicker.getClearSelectionLink().click();
editor.clickSaveAndContinue();
editor.clickCancel().edit();
userPicker = new UserClassFieldEditPane("user1").getUserPicker().waitToLoad();
Assert.assertEquals(0, userPicker.getAcceptedSuggestions().size());
Assert.assertEquals("", userPicker.getValue());
}