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

File ClassEditorTest.java

 

Code metrics

0
148
14
1
438
263
14
0.09
10.57
14
1

Classes

Class Line # Actions
ClassEditorTest 46 148 0% 14 0
1.0100%
 

Contributing tests

This file is covered by 12 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.apache.commons.lang3.StringUtils;
25    import org.junit.Assert;
26    import org.junit.Test;
27    import org.openqa.selenium.WebDriver;
28    import org.openqa.selenium.support.ui.ExpectedCondition;
29    import org.openqa.selenium.support.ui.WebDriverWait;
30    import org.xwiki.appwithinminutes.test.po.ApplicationClassEditPage;
31    import org.xwiki.appwithinminutes.test.po.ClassFieldEditPane;
32    import org.xwiki.appwithinminutes.test.po.EntryEditPage;
33    import org.xwiki.appwithinminutes.test.po.LongTextClassFieldEditPane;
34    import org.xwiki.test.ui.browser.IgnoreBrowser;
35    import org.xwiki.test.ui.browser.IgnoreBrowsers;
36    import org.xwiki.test.ui.po.ViewPage;
37    import org.xwiki.test.ui.po.editor.ObjectEditPage;
38    import org.xwiki.xclass.test.po.ClassSheetPage;
39   
40    /**
41    * Tests the application class editor.
42    *
43    * @version $Id: e03645b7515c14b7c2b4f6433bf902270f8a5210 $
44    * @since 3.4M1
45    */
 
46    public class ClassEditorTest extends AbstractClassEditorTest
47    {
48    /**
49    * The message displayed when the canvas is empty.
50    */
51    public static final String EMPTY_CANVAS_HINT = "Drag fields from the palette and drop them in this area.";
52   
53    /**
54    * Tests that the hint is displayed only when the canvas is empty.
55    */
 
56  1 toggle @Test
57    @IgnoreBrowsers({
58    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
59    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
60    public void testEmptyCanvasHint()
61    {
62  1 Assert.assertTrue(editor.getContent().contains(EMPTY_CANVAS_HINT));
63  1 ClassFieldEditPane field = editor.addField("Short Text");
64  1 Assert.assertFalse(editor.getContent().contains(EMPTY_CANVAS_HINT));
65  1 field.delete().clickYes();
66  1 Assert.assertTrue(editor.getContent().contains(EMPTY_CANVAS_HINT));
67    }
68   
69    /**
70    * Tests that the field display is updated when the configuration panel is closed.
71    */
 
72  1 toggle @Test
73    @IgnoreBrowsers({
74    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
75    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
76    public void testApplyConfigurationChanges()
77    {
78  1 LongTextClassFieldEditPane longTextField =
79    new LongTextClassFieldEditPane(editor.addField("Long Text").getName());
80  1 longTextField.openConfigPanel();
81  1 longTextField.setRows(3);
82  1 longTextField.setEditor("Text");
83  1 longTextField.closeConfigPanel();
84  1 Assert.assertEquals(3, longTextField.getPreviewRows());
85    }
86   
87    /**
88    * Tests that class fields can be deleted and that documents having objects of that class are updated.
89    */
 
90  1 toggle @Test
91    @IgnoreBrowsers({
92    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
93    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
94    public void testDeleteField()
95    {
96    // Add two fields.
97  1 editor.addField("Boolean").setPrettyName("Available");
98  1 editor.addField("Date").setPrettyName("Day");
99   
100    // Save and assert they are present.
101  1 ViewPage classView = editor.clickSaveAndView();
102  1 Assert.assertTrue(classView.getContent().contains("Available (boolean1: Boolean)"));
103  1 Assert.assertTrue(classView.getContent().contains("Day (date1: Date)"));
104   
105    // Edit again and delete one of the fields.
106  1 classView.edit();
107  1 new ClassFieldEditPane("boolean1").delete().clickYes();
108   
109    // Save and check if the field was removed.
110  1 classView = new ApplicationClassEditPage().clickSaveAndView();
111  1 Assert.assertFalse(classView.getContent().contains("Available (boolean1: Boolean)"));
112  1 Assert.assertTrue(classView.getContent().contains("Day (date1: Date)"));
113   
114    // Edit the class template and see if the deleted field is now deprecated.
115  1 ObjectEditPage objectEditor = new ClassSheetPage().clickTemplateLink().editObjects();
116  1 String className = String.format("%s.%s", getTestClassName(), getTestMethodName());
117  1 Assert.assertTrue(objectEditor.isPropertyDeprecated(className, "boolean1"));
118  1 Assert.assertFalse(objectEditor.isPropertyDeprecated(className, "date1"));
119    }
120   
121    /**
122    * Tests that class fields can be reordered.
123    */
 
124  1 toggle @Test
125    @IgnoreBrowsers({
126    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
127    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
128    public void testReorderFields()
129    {
130    // Add two class fields.
131  1 editor.addField("Date").setPrettyName("Start Date");
132  1 editor.addField("Date").setPrettyName("End Date");
133   
134    // Save and edit the class template in in-line edit mode.
135  1 editor.clickSaveAndView();
136  1 new ClassSheetPage().clickTemplateLink().edit();
137   
138    // Assert the order of the form fields.
139  1 List<String> fieldNames = new EntryEditPage().getFieldNames();
140  1 Assert.assertEquals("date1", fieldNames.get(0));
141  1 Assert.assertEquals("date2", fieldNames.get(1));
142   
143    // Go back to the class editor.
144  1 getDriver().navigate().back();
145  1 getDriver().navigate().back();
146  1 new ViewPage().edit();
147  1 editor = new ApplicationClassEditPage();
148   
149    // Change the order of the class fields.
150  1 editor.moveFieldBefore("date2", "date1");
151   
152    // Save and edit the class template again.
153  1 editor.clickSaveAndView();
154  1 new ClassSheetPage().clickTemplateLink().edit();
155   
156    // Assert the order of the form fields.
157  1 fieldNames = new EntryEditPage().getFieldNames();
158  1 Assert.assertEquals("date2", fieldNames.get(0));
159  1 Assert.assertEquals("date1", fieldNames.get(1));
160    }
161   
162    /**
163    * Tests that class fields can be renamed.
164    */
 
165  1 toggle @Test
166    @IgnoreBrowsers({
167    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
168    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
169    public void testRenameField()
170    {
171    // Add a class field.
172  1 editor.addField("Number").setDefaultValue("13");
173   
174    // Save and edit the class template.
175  1 editor.clickSaveAndView();
176  1 new ClassSheetPage().clickTemplateLink().edit();
177   
178    // Change the field value.
179  1 EntryEditPage inlineEditor = new EntryEditPage();
180  1 Assert.assertEquals("13", inlineEditor.getValue("number1"));
181  1 inlineEditor.setValue("number1", "27");
182   
183    // Save and edit again the class.
184  1 inlineEditor.clickSaveAndView();
185  1 getUtil().gotoPage(getTestClassName(), getTestMethodName());
186  1 new ViewPage().edit();
187   
188    // Rename the class field.
189  1 ClassFieldEditPane field = new ClassFieldEditPane("number1");
190  1 field.openConfigPanel();
191  1 field.setName("age");
192   
193    // Save and edit again the class template.
194  1 new ApplicationClassEditPage().clickSaveAndView();
195  1 new ClassSheetPage().clickTemplateLink().edit();
196  1 Assert.assertEquals("27", new EntryEditPage().getValue("age"));
197    }
198   
199    /**
200    * Tests that invalid field names are not allowed.
201    */
 
202  1 toggle @Test
203    @IgnoreBrowsers({
204    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
205    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
206    public void testInvalidFieldName()
207    {
208  1 String invalidFieldNameErrorMessage = "Property names must follow these naming rules:";
209   
210  1 ClassFieldEditPane field = editor.addField("Static List");
211  1 field.openConfigPanel();
212  1 field.setName("3times");
213    // Save the page and expect the error.
214  1 editor.getSaveAndViewButton().click();
215  1 waitForPageSourceContains(invalidFieldNameErrorMessage);
216   
217  1 getDriver().navigate().back();
218  1 editor = new ApplicationClassEditPage();
219  1 field = editor.addField("User");
220  1 field.openConfigPanel();
221    // Unfortunately we don't allow Unicode letters because they are not fully supported in tag names.
222    // See XWIKI-7306: The class editor doesn't validate properly the field names
223  1 field.setName("\u021Bar\u0103");
224    // Save the page and expect the error.
225  1 editor.getSaveAndViewButton().click();
226  1 waitForPageSourceContains(invalidFieldNameErrorMessage);
227   
228  1 getDriver().navigate().back();
229  1 editor = new ApplicationClassEditPage();
230  1 field = editor.addField("Group");
231  1 field.openConfigPanel();
232  1 field.setName("alice>bob");
233    // Save the page and expect the error.
234  1 editor.getSaveAndViewButton().click();
235  1 waitForPageSourceContains(invalidFieldNameErrorMessage);
236    }
237   
238    /**
239    * Tests that two class fields can't have the same name.
240    */
 
241  1 toggle @Test
242    @IgnoreBrowsers({
243    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
244    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
245    public void testDuplicateFieldName()
246    {
247  1 ClassFieldEditPane field = editor.addField("Short Text");
248  1 field.setPrettyName("Alice");
249  1 field.openConfigPanel();
250  1 field.setName("carol");
251   
252  1 field = editor.addField("Short Text");
253  1 field.setPrettyName("Bob");
254  1 field.openConfigPanel();
255  1 field.setName("carol");
256   
257    // Save the page and expect the error.
258  1 editor.getSaveAndViewButton().click();
259  1 waitForPageSourceContains("The class has two fields with the same name: carol");
260    }
261   
262    /**
263    * Tests that swapping field names is not allowed.
264    */
 
265  1 toggle @Test
266    @IgnoreBrowsers({
267    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
268    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
269    public void testSwapFieldNames()
270    {
271  1 ClassFieldEditPane field = editor.addField("Short Text");
272  1 field.openConfigPanel();
273  1 field.setName("alice");
274   
275  1 field = editor.addField("Number");
276  1 field.openConfigPanel();
277  1 field.setName("bob");
278   
279  1 editor.clickSaveAndView().edit();
280  1 editor = new ApplicationClassEditPage();
281   
282  1 field = new ClassFieldEditPane("alice");
283  1 field.openConfigPanel();
284  1 field.setName("bob");
285   
286  1 field = new ClassFieldEditPane("bob");
287  1 field.openConfigPanel();
288  1 field.setName("alice");
289   
290    // Save the page and expect the error.
291  1 editor.getSaveAndViewButton().click();
292  1 waitForPageSourceContains("The class has two fields with the same name: alice");
293    }
294   
295    /**
296    * Tests the options to update the class sheet and the class template.
297    */
 
298  1 toggle @Test
299    @IgnoreBrowsers({
300    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
301    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
302    public void testUpdateSheetAndTemplate()
303    {
304    // The options panel is not displayed if the class template and sheet don't exists.
305  1 Assert.assertFalse(editor.getContent().contains("Update class template"));
306   
307    // Add a class field.
308  1 editor.addField("Number");
309   
310    // Save and edit again.
311  1 editor.clickSaveAndView().edit();
312  1 editor = new ApplicationClassEditPage();
313   
314    // Set default value for the previously added field.
315  1 new ClassFieldEditPane("number1").setDefaultValue("9");
316   
317    // Add a new field
318  1 editor.addField("Database List");
319   
320    // The options panel should be displayed now.
321  1 editor.setUpdateClassSheet(false);
322   
323    // Save and edit the template.
324  1 editor.clickSaveAndView();
325  1 new ClassSheetPage().clickTemplateLink().edit();
326   
327    // The sheet should display only the first field.
328  1 EntryEditPage inlineEditor = new EntryEditPage();
329  1 List<String> fieldNames = inlineEditor.getFieldNames();
330  1 Assert.assertEquals(1, fieldNames.size());
331  1 Assert.assertEquals("number1", fieldNames.get(0));
332   
333    // Assert the value of the first field. The class template should have been updated.
334  1 Assert.assertEquals("9", inlineEditor.getValue("number1"));
335    }
336   
337    /**
338    * Tests the Save & Continue button.
339    */
 
340  1 toggle @Test
341    @IgnoreBrowsers({
342    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
343    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
344    public void testSaveAndContinue()
345    {
346  1 editor.addField("Date");
347  1 editor.clickSaveAndContinue();
348   
349    // Check if the field was added.
350  1 ViewPage viewer = editor.clickCancel();
351  1 Assert.assertTrue(viewer.getContent().contains("Date (date1: Date)"));
352   
353    // Edit again. This time check the error message.
354  1 viewer.edit();
355  1 editor = new ApplicationClassEditPage();
356   
357    // Try to set the field name to an invalid value.
358  1 ClassFieldEditPane field = new ClassFieldEditPane("date1");
359  1 field.openConfigPanel();
360  1 field.setName("-delta");
361   
362  1 editor.clickSaveAndContinue(false);
363  1 editor.waitForNotificationErrorMessage("Failed to save the page.");
364   
365    // Double check that the field wasn't renamed.
366  1 Assert.assertTrue(editor.clickCancel().getContent().contains("Date (date1: Date)"));
367    }
368   
369    /**
370    * Tests that fields names are auto-generated properly.
371    */
 
372  1 toggle @Test
373    @IgnoreBrowsers({
374    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
375    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
376    public void testFieldNameAutoGeneration()
377    {
378    // Add a class field and set its name to an auto-generated field name for a different type.
379  1 ClassFieldEditPane field = editor.addField("Short Text");
380  1 field.openConfigPanel();
381  1 field.setName("number1");
382   
383  1 editor.clickSaveAndContinue();
384   
385    // Add a new field of the type implied by the name set to the previous field.
386  1 field = editor.addField("Number");
387  1 field.openConfigPanel();
388  1 Assert.assertEquals("number2", field.getName());
389   
390    // Save and assert both fields have been added.
391  1 ViewPage viewer = editor.clickSaveAndView();
392  1 Assert.assertTrue(viewer.getContent().contains("Short Text (number1: String)"));
393  1 Assert.assertTrue(viewer.getContent().contains("Number (number2: Number)"));
394    }
395   
396    /**
397    * Test that Save And Continue supports field renames.
398    */
 
399  1 toggle @Test
400    @IgnoreBrowsers({
401    @IgnoreBrowser(value = "internet.*", version = "8\\.*", reason = "See http://jira.xwiki.org/browse/XE-1146"),
402    @IgnoreBrowser(value = "internet.*", version = "9\\.*", reason = "See http://jira.xwiki.org/browse/XE-1177")})
403    public void testRenameWithSaveAndContinue()
404    {
405  1 ClassFieldEditPane field = editor.addField("Short Text");
406  1 editor.clickSaveAndContinue();
407   
408    // Rename the field.
409  1 field.openConfigPanel();
410  1 field.setName("title");
411  1 editor.clickSaveAndContinue();
412   
413    // Rename the field for a second time.
414    // NOTE: The IDs have been changed so we must recreate the class field edit pane.
415  1 new ClassFieldEditPane("title").setName("city");
416   
417    // Save and assert the field was added with the right name.
418  1 Assert.assertTrue(editor.clickSaveAndView().getContent().contains("Short Text (city: String)"));
419    }
420   
421    /**
422    * Waits until the page source contains the given text or the timeout expires.
423    * <p>
424    * NOTE: Normally we shoudn't need to use this method, i.e. we should be able to assert the source page directly
425    * because Selenium should wait until the page is loaded but this doesn't happen all the time for some reason..
426    */
 
427  5 toggle private void waitForPageSourceContains(final String text)
428    {
429  5 new WebDriverWait(getDriver(), getDriver().getTimeout()).until(new ExpectedCondition<Boolean>()
430    {
 
431  5 toggle @Override
432    public Boolean apply(WebDriver driver)
433    {
434  5 return StringUtils.contains(getDriver().getPageSource(), text);
435    }
436    });
437    }
438    }