1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.test.ui.appwithinminutes

File UserClassFieldTest.java

 

Code metrics

6
150
7
1
317
206
12
0.08
21.43
7
1.71

Classes

Class Line # Actions
UserClassFieldTest 42 150 0% 12 4
0.975460197.5%
 

Contributing tests

This file is covered by 5 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.test.ui.appwithinminutes;
21   
22    import java.util.List;
23   
24    import org.junit.Assert;
25    import org.junit.BeforeClass;
26    import org.junit.Test;
27    import org.openqa.selenium.By;
28    import org.openqa.selenium.Keys;
29    import org.openqa.selenium.WebElement;
30    import org.xwiki.appwithinminutes.test.po.UserClassFieldEditPane;
31    import org.xwiki.test.ui.po.InlinePage;
32    import org.xwiki.test.ui.po.editor.UserPicker;
33    import org.xwiki.test.ui.po.editor.UserPicker.UserElement;
34    import org.xwiki.xclass.test.po.ClassSheetPage;
35   
36    /**
37    * Special class editor tests that address only the User class field type.
38    *
39    * @version $Id: f919dbd3b23b4719b7e667e776ec6daebdcf4520 $
40    * @since 4.5
41    */
 
42    public class UserClassFieldTest extends AbstractClassEditorTest
43    {
 
44  1 toggle @BeforeClass
45    public static void setUpClass() throws Exception
46    {
47    // Create 2 users.
48  1 getUtil().createUserAndLogin("tmortagne", "tmortagne", "first_name", "Thomas", "last_name", "Mortagne",
49    "avatar", "tmortagne.png");
50  1 getUtil().attachFile("XWiki", "tmortagne", "tmortagne.png",
51    UserClassFieldTest.class.getResourceAsStream("/appwithinminutes/tmortagne.png"), false);
52  1 getUtil().createUserAndLogin("Enygma2002", "Enygma2002", "first_name", "Eduard", "last_name", "Moraru",
53    "avatar", "Enygma2002.png");
54  1 getUtil().attachFile("XWiki", "Enygma2002", "Enygma2002.png",
55    UserClassFieldTest.class.getResourceAsStream("/appwithinminutes/Enygma2002.png"), false);
56    }
57   
 
58  1 toggle @Test
59    public void testSuggestions()
60    {
61  1 UserPicker userPicker = new UserClassFieldEditPane(editor.addField("User").getName()).getUserPicker();
62   
63    // The suggestions should be case-insensitive. Match the last name.
64  1 List<UserElement> suggestions = userPicker.sendKeys("mOr").waitForSuggestions().getSuggestions();
65  1 Assert.assertEquals(2, suggestions.size());
66  1 assertUserElement(suggestions.get(0), "Eduard Moraru", "Enygma2002");
67  1 assertUserElement(suggestions.get(1), "Thomas Mortagne");
68   
69    // Match the first name.
70  1 suggestions = userPicker.sendKeys(Keys.BACK_SPACE, Keys.BACK_SPACE, "As").waitForSuggestions().getSuggestions();
71  1 Assert.assertEquals(1, suggestions.size());
72  1 assertUserElement(suggestions.get(0), "Thomas Mortagne");
73   
74    // Match the alias.
75  1 suggestions = userPicker.sendKeys(Keys.BACK_SPACE, "20").waitForSuggestions().getSuggestions();
76  1 Assert.assertEquals(1, suggestions.size());
77  1 assertUserElement(suggestions.get(0), "Eduard Moraru", "Enygma2002");
78   
79    // The guest user shouldn't be suggested.
80  1 suggestions = userPicker.clear().sendKeys("guest").waitForSuggestions().getSuggestions();
81  1 Assert.assertEquals(1, suggestions.size());
82  1 Assert.assertEquals("User not found", suggestions.get(0).getText());
83   
84    // Default administrator user should be suggested.
85  1 suggestions = userPicker.clear().sendKeys("admin").waitForSuggestions().getSuggestions();
86  1 Assert.assertEquals(1, suggestions.size());
87  1 assertUserElement(suggestions.get(0), "Administrator", "Admin", "noavatar.png");
88   
89    // "a" should bring many suggestions. Also, a single letter should bring suggestions.
90  1 Assert.assertTrue(userPicker.clear().sendKeys("a").waitForSuggestions().getSuggestions().size() > 2);
91   
92    // An empty text input shouldn't bring any suggestions.
93  1 try {
94  1 userPicker.sendKeys(Keys.BACK_SPACE).waitForSuggestions();
95  0 Assert.fail();
96    } catch (Exception e) {
97    }
98   
99    // We should be able to close the list of suggestions using the escape key.
100  1 userPicker.sendKeys("mor").waitForSuggestions().sendKeys(Keys.ESCAPE).waitForSuggestionsToFadeOut();
101   
102    // The list of suggestions should close itself after a while.
103  1 userPicker.moveMouseOver().sendKeys(Keys.BACK_SPACE).waitForSuggestions().waitForSuggestionsToDisappear();
104   
105    // The list of suggestions should stay open if the mouse is over it.
106  1 userPicker.sendKeys(Keys.BACK_SPACE).waitForSuggestions().getSuggestions().get(0).moveMouseOver();
107  1 try {
108  1 userPicker.waitForSuggestionsToDisappear();
109  0 Assert.fail();
110    } catch (Exception e) {
111    }
112   
113    // .. and the list of suggestions should fade out when the mouse is moved out.
114  1 userPicker.moveMouseOver().waitForSuggestionsToFadeOut();
115    }
116   
117    /**
118    * Asserts the given user matches the expectations.
119    *
120    * @param user the user to assert
121    * @param name the expected name
122    * @param extra extra user fields (alias, image, etc.)
123    */
 
124  19 toggle private void assertUserElement(UserElement user, String name, String... extra)
125    {
126  19 String alias;
127  19 if (extra.length > 0) {
128  12 alias = extra[0];
129    } else {
130  7 String[] parts = name.split("\\s+");
131  7 if (parts.length > 1) {
132  7 alias = parts[0].toLowerCase().charAt(0) + parts[1].toLowerCase();
133    } else {
134  0 alias = name;
135    }
136    }
137   
138  19 String image = alias + ".png?";
139  19 if (extra.length > 1) {
140  6 image = extra[1];
141    }
142   
143  19 Assert.assertEquals(name, user.getName());
144  19 Assert.assertEquals(alias, user.getAlias());
145  19 WebElement avatar = user.getAvatar();
146  19 Assert.assertEquals(name, avatar.getAttribute("alt"));
147  19 Assert.assertTrue(avatar.getAttribute("src").contains("/" + image));
148    }
149   
 
150  1 toggle @Test
151    public void testSingleSelection()
152    {
153  1 UserPicker userPicker = new UserClassFieldEditPane(editor.addField("User").getName()).getUserPicker();
154   
155    // Use the keyboard.
156  1 userPicker.sendKeys("mor").waitForSuggestions().sendKeys(Keys.ARROW_DOWN, Keys.ARROW_DOWN, Keys.ENTER);
157  1 List<UserElement> selectedUsers = userPicker.waitForSuggestionsToFadeOut().getAcceptedSuggestions();
158  1 Assert.assertEquals(1, selectedUsers.size());
159  1 assertUserElement(selectedUsers.get(0), "Thomas Mortagne");
160  1 Assert.assertEquals("", userPicker.getValue());
161    // The link to clear the list of selected users should be displayed if at least 2 users are selected.
162  1 Assert.assertFalse(userPicker.getClearSelectionLink().isDisplayed());
163   
164    // Use the mouse. Since we have single selection by default, the previously selected user should be replaced.
165  1 userPicker.sendKeys("mor").waitForSuggestions().select("Enygma2002");
166  1 selectedUsers = userPicker.waitForSuggestionsToFadeOut().getAcceptedSuggestions();
167  1 Assert.assertEquals(1, selectedUsers.size());
168  1 assertUserElement(selectedUsers.get(0), "Eduard Moraru", "Enygma2002");
169  1 Assert.assertEquals("", userPicker.getValue());
170  1 Assert.assertFalse(userPicker.getClearSelectionLink().isDisplayed());
171   
172    // Delete the selected user.
173  1 selectedUsers.get(0).delete();
174  1 Assert.assertEquals(0, userPicker.getAcceptedSuggestions().size());
175  1 Assert.assertFalse(userPicker.getClearSelectionLink().isDisplayed());
176   
177    // When there is only one suggestion, Enter key should select it.
178  1 userPicker.sendKeys("admin").waitForSuggestions().sendKeys(Keys.ENTER);
179  1 selectedUsers = userPicker.waitForSuggestionsToFadeOut().getAcceptedSuggestions();
180  1 Assert.assertEquals(1, selectedUsers.size());
181  1 assertUserElement(selectedUsers.get(0), "Administrator", "Admin", "noavatar.png");
182  1 Assert.assertEquals("", userPicker.getValue());
183  1 Assert.assertFalse(userPicker.getClearSelectionLink().isDisplayed());
184    }
185   
 
186  1 toggle @Test
187    public void testMultipleSelection()
188    {
189  1 UserClassFieldEditPane userField = new UserClassFieldEditPane(editor.addField("User").getName());
190  1 userField.openConfigPanel();
191  1 userField.setMultipleSelect(true);
192  1 userField.closeConfigPanel();
193  1 UserPicker userPicker = userField.getUserPicker();
194   
195    // Select 2 users.
196  1 userPicker.sendKeys("tmortagne").waitForSuggestions().sendKeys(Keys.ENTER).waitForSuggestionsToFadeOut();
197  1 Assert.assertFalse(userPicker.getClearSelectionLink().isDisplayed());
198  1 userPicker.sendKeys("2002").waitForSuggestions().select("Enygma").waitForSuggestionsToFadeOut();
199  1 Assert.assertTrue(userPicker.getClearSelectionLink().isDisplayed());
200  1 List<UserElement> selectedUsers = userPicker.getAcceptedSuggestions();
201  1 Assert.assertEquals(2, selectedUsers.size());
202  1 assertUserElement(selectedUsers.get(0), "Thomas Mortagne");
203  1 assertUserElement(selectedUsers.get(1), "Eduard Moraru", "Enygma2002");
204  1 Assert.assertEquals("", userPicker.getValue());
205   
206    // Delete the first selected user.
207  1 selectedUsers.get(0).delete();
208  1 Assert.assertFalse(userPicker.getClearSelectionLink().isDisplayed());
209   
210    // Select another user.
211  1 userPicker.sendKeys("admin").waitForSuggestions().sendKeys(Keys.ENTER).waitForSuggestionsToFadeOut();
212  1 selectedUsers = userPicker.getAcceptedSuggestions();
213  1 Assert.assertEquals(2, selectedUsers.size());
214  1 assertUserElement(selectedUsers.get(0), "Eduard Moraru", "Enygma2002");
215  1 assertUserElement(selectedUsers.get(1), "Administrator", "Admin", "noavatar.png");
216  1 Assert.assertEquals("", userPicker.getValue());
217   
218    // Change the order of the selected users.
219  1 selectedUsers.get(1).moveBefore(selectedUsers.get(0));
220  1 assertUserElement(userPicker.getAcceptedSuggestions().get(0), "Administrator", "Admin", "noavatar.png");
221   
222    // Clear the list of selected users.
223  1 userPicker.getClearSelectionLink().click();
224  1 Assert.assertFalse(userPicker.getClearSelectionLink().isDisplayed());
225  1 Assert.assertEquals(0, userPicker.getAcceptedSuggestions().size());
226    }
227   
 
228  1 toggle @Test
229    public void testSaveAndInitialSelection()
230    {
231  1 UserPicker userPicker = new UserClassFieldEditPane(editor.addField("User").getName()).getUserPicker();
232  1 userPicker.sendKeys("thomas").waitForSuggestions().sendKeys(Keys.ENTER);
233  1 editor.clickSaveAndView().edit();
234   
235  1 UserClassFieldEditPane userField = new UserClassFieldEditPane("user1");
236  1 userPicker = userField.getUserPicker().waitToLoad();
237  1 List<UserElement> selectedUsers = userPicker.getAcceptedSuggestions();
238  1 Assert.assertEquals(1, selectedUsers.size());
239  1 assertUserElement(selectedUsers.get(0), "Thomas Mortagne");
240  1 Assert.assertEquals("", userPicker.getValue());
241   
242    // Enable multiple selection.
243  1 userField.openConfigPanel();
244  1 userField.setMultipleSelect(true);
245  1 userField.closeConfigPanel();
246   
247    // Re-take the user picker because the display has been reloaded.
248  1 userPicker = userField.getUserPicker();
249   
250    // Select one more user.
251  1 userPicker.waitToLoad().sendKeys("admin").waitForSuggestions().sendKeys(Keys.ENTER);
252  1 editor.clickSaveAndContinue();
253  1 editor.clickCancel().edit();
254   
255  1 userPicker = new UserClassFieldEditPane("user1").getUserPicker().waitToLoad();
256  1 selectedUsers = userPicker.getAcceptedSuggestions();
257  1 Assert.assertEquals(2, selectedUsers.size());
258  1 assertUserElement(selectedUsers.get(0), "Thomas Mortagne");
259  1 assertUserElement(selectedUsers.get(1), "Administrator", "Admin", "noavatar.png");
260  1 Assert.assertEquals("", userPicker.getValue());
261   
262    // We should be able to input free text also.
263  1 userPicker.sendKeys("foobar").waitForSuggestions().sendKeys(Keys.ESCAPE).waitForSuggestionsToFadeOut();
264  1 editor.clickSaveAndContinue();
265  1 editor.clickCancel().edit();
266   
267  1 userPicker = new UserClassFieldEditPane("user1").getUserPicker().waitToLoad();
268  1 selectedUsers = userPicker.getAcceptedSuggestions();
269  1 Assert.assertEquals(3, selectedUsers.size());
270  1 assertUserElement(selectedUsers.get(2), "foobar", "foobar", "noavatar.png");
271  1 Assert.assertEquals("", userPicker.getValue());
272   
273    // Delete the fake user.
274  1 selectedUsers.get(2).delete();
275  1 Assert.assertEquals(2, userPicker.getAcceptedSuggestions().size());
276   
277    // Delete all selected users.
278  1 userPicker.getClearSelectionLink().click();
279  1 editor.clickSaveAndContinue();
280  1 editor.clickCancel().edit();
281   
282  1 userPicker = new UserClassFieldEditPane("user1").getUserPicker().waitToLoad();
283  1 Assert.assertEquals(0, userPicker.getAcceptedSuggestions().size());
284  1 Assert.assertEquals("", userPicker.getValue());
285    }
286   
 
287  1 toggle @Test
288    public void testApplicationEntry()
289    {
290    // Create the application class.
291  1 UserPicker userPicker = new UserClassFieldEditPane(editor.addField("User").getName()).getUserPicker();
292  1 userPicker.sendKeys("thomas").waitForSuggestions().sendKeys(Keys.ENTER);
293  1 editor.clickSaveAndView();
294   
295    // Create the application entry.
296  1 ClassSheetPage classSheetPage = new ClassSheetPage();
297  1 InlinePage entryEditPage = classSheetPage.createNewDocument(getTestClassName(), getTestMethodName() + "Entry");
298   
299    // Assert the initial value.
300  1 String id = getTestClassName() + "." + getTestMethodName() + "_0_user1";
301  1 userPicker = new UserPicker(getDriver().findElement(By.id(id)));
302  1 List<UserElement> selectedUsers = userPicker.waitToLoad().getAcceptedSuggestions();
303  1 Assert.assertEquals(1, selectedUsers.size());
304  1 assertUserElement(selectedUsers.get(0), "Thomas Mortagne");
305  1 Assert.assertEquals("", userPicker.getValue());
306   
307    // Change the selected user.
308  1 userPicker.sendKeys("eduard").waitForSuggestions().sendKeys(Keys.ENTER).waitForSuggestionsToFadeOut();
309    // We wait for the page to load because Selenium doesn't do it all the time when Save & View is clicked.
310  1 entryEditPage.clickSaveAndView().waitUntilPageIsLoaded();
311   
312    // Assert the view mode.
313  1 List<WebElement> users = getDriver().findElements(By.className("user"));
314  1 Assert.assertEquals(1, users.size());
315  1 assertUserElement(new UserElement(users.get(0)), "Eduard Moraru", "Enygma2002");
316    }
317    }