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

File ImageTest.java

 

Code metrics

0
435
31
1
916
606
31
0.07
14.03
31
1

Classes

Class Line # Actions
ImageTest 35 435 0% 31 0
1.0100%
 

Contributing tests

This file is covered by 18 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.wysiwyg;
21   
22    import org.junit.Test;
23    import org.openqa.selenium.By;
24    import org.xwiki.test.wysiwyg.framework.AbstractWysiwygTestCase;
25    import org.xwiki.test.wysiwyg.framework.XWikiExplorer;
26   
27    import static org.junit.Assert.*;
28   
29    /**
30    * Tests the image insert and edit plugin. For the moment, it does not test the upload new image feature, since it needs
31    * special selenium setup.
32    *
33    * @version $Id: a892d8954c549eb8ea5361a08ec3b9ce3e9adcf5 $
34    */
 
35    public class ImageTest extends AbstractWysiwygTestCase
36    {
37    public static final String MENU_IMAGE = "Image";
38   
39    public static final String MENU_INSERT_ATTACHED_IMAGE = "Attached Image...";
40   
41    public static final String MENU_INSERT_EXTERNAL_IMAGE = "External Image...";
42   
43    public static final String MENU_EDIT_IMAGE = "Edit Image...";
44   
45    public static final String MENU_REMOVE_IMAGE = "Remove Image";
46   
47    public static final String STEP_SELECTOR = "xSelectorAggregatorStep";
48   
49    public static final String STEP_EXPLORER = "xImagesExplorer";
50   
51    public static final String STEP_EXTERNAL_IMAGE = "xExternalImage";
52   
53    public static final String STEP_CONFIG = "xImageConfig";
54   
55    public static final String STEP_CURRENT_PAGE_SELECTOR = "xImagesSelector";
56   
57    public static final String STEP_UPLOAD = "xUploadPanel";
58   
59    public static final String TAB_CURRENT_PAGE = "Current page";
60   
61    public static final String TAB_ALL_PAGES = "All pages";
62   
63    public static final String BUTTON_SELECT = "Select";
64   
65    public static final String BUTTON_INSERT_IMAGE = "Insert Image";
66   
67    public static final String BUTTON_CHANGE_IMAGE = "Change Image";
68   
69    public static final String BUTTON_IMAGE_SETTINGS = "Image Settings";
70   
71    public static final String BUTTON_PREVIOUS = "Previous";
72   
73    public static final String INPUT_WIDTH = "//div[contains(@class, \"xSizePanel\")]//input[1]";
74   
75    public static final String INPUT_HEIGHT = "//div[contains(@class, \"xSizePanel\")]//input[2]";
76   
77    public static final String INPUT_ALT = "//div[contains(@class, \"xAltPanel\")]//input";
78   
79    public static final String IMAGES_LIST = "//div[contains(@class, 'xListBox')]";
80   
81    public static final String FILE_UPLOAD_INPUT = "//input[contains(@class, 'gwt-FileUpload')]";
82   
83    public static final String INPUT_EXTERNAL_IMAGE_LOCATION = "//input[@title = 'Image location']";
84   
85    public static final String ERROR_MSG_CLASS = "xErrorMsg";
86   
87    public static final String SPACE_SELECTOR = "//div[@class=\"xPageChooser\"]//select[1]";
88   
89    public static final String PAGE_SELECTOR = "//div[@class=\"xPageChooser\"]//select[2]";
90   
91    /**
92    * The object used to assert the state of the XWiki Explorer tree.
93    */
94    private XWikiExplorer explorer;
95   
 
96  18 toggle @Override
97    public void setUp()
98    {
99  18 super.setUp();
100   
101  18 this.explorer = new XWikiExplorer(getDriver());
102    }
103   
104    /**
105    * Test adding an image from a page different from the current one.
106    */
 
107  1 toggle @Test
108    public void testInsertImageFromAllPages()
109    {
110  1 String imageSpace = "XWiki";
111  1 String imagePage = "AdminSheet";
112  1 String imageFile = "registration.png";
113  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
114  1 waitForStepToLoad(STEP_SELECTOR);
115    // switch to all pages view
116  1 clickTab(TAB_ALL_PAGES);
117  1 waitForStepToLoad(STEP_EXPLORER);
118  1 selectImage(imageSpace, imagePage, imageFile);
119  1 clickButtonWithText(BUTTON_SELECT);
120  1 waitForStepToLoad(STEP_CONFIG);
121  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
122  1 waitForDialogToClose();
123   
124  1 switchToSource();
125  1 assertSourceText("[[image:" + imageSpace + "." + imagePage + "@" + imageFile + "]]");
126    }
127   
128    /**
129    * Test add and edit an image from a page different from the current one.
130    */
 
131  1 toggle @Test
132    public void testInsertAndEditImageFromAllPages()
133    {
134  1 String imageSpace = "Blog";
135  1 String imagePage = "Categories";
136  1 String imageFile = "icon.png";
137   
138  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
139  1 waitForStepToLoad(STEP_SELECTOR);
140    // switch to all pages view
141  1 clickTab(TAB_ALL_PAGES);
142  1 waitForStepToLoad(STEP_EXPLORER);
143  1 selectImage(imageSpace, imagePage, imageFile);
144  1 clickButtonWithText(BUTTON_SELECT);
145  1 waitForStepToLoad(STEP_CONFIG);
146  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
147  1 moveCaret("document.body", 1);
148  1 typeText("x");
149   
150    // cannot select the image otherwise but like this: click won't work, nor push button
151  1 selectNode("document.body.firstChild");
152  1 openImageDialog(MENU_EDIT_IMAGE);
153  1 waitForStepToLoad(STEP_EXPLORER);
154    // test that the page is the right page
155  1 assertImageSelected(imageSpace, imagePage, imageFile);
156  1 clickButtonWithText(BUTTON_SELECT);
157  1 waitForStepToLoad(STEP_CONFIG);
158    // the image alt text should be now present
159  1 assertEquals(imageFile, getSelenium().getValue(INPUT_ALT));
160  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
161   
162  1 switchToSource();
163  1 assertSourceText("[[image:" + imageSpace + "." + imagePage + "@" + imageFile + "]]x");
164    }
165   
166    /**
167    * Test adding and editing an image with parameters preserves parameters values.
168    */
 
169  1 toggle @Test
170    public void testInsertAndEditImageWithParameters()
171    {
172  1 String imageSpace = "XWiki";
173  1 String imagePage = "AdminSheet";
174  1 String imageFile = "rights.png";
175   
176  1 applyStyleTitle1();
177  1 typeText("H");
178  1 typeEnter();
179   
180  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
181  1 waitForStepToLoad(STEP_SELECTOR);
182    // switch to all pages view
183  1 clickTab(TAB_ALL_PAGES);
184  1 waitForStepToLoad(STEP_EXPLORER);
185  1 selectImage(imageSpace, imagePage, imageFile);
186  1 clickButtonWithText(BUTTON_SELECT);
187  1 waitForStepToLoad(STEP_CONFIG);
188   
189  1 getSelenium().type(INPUT_WIDTH, "200");
190  1 getSelenium().type(INPUT_ALT, "No parking sign");
191  1 selectAlignment("CENTER");
192  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
193   
194    // Place the caret after the inserted image.
195  1 getRichTextArea().executeScript("window.getSelection().collapseToEnd()");
196   
197  1 typeText("x");
198   
199  1 switchToSource();
200  1 assertSourceText("= H =\n\n[[image:XWiki.AdminSheet@rights.png||alt=\"No parking sign\" "
201    + "style=\"display: block; margin-left: auto; margin-right: auto;\" width=\"200\"]]" + "x");
202  1 switchToWysiwyg();
203   
204    // now edit
205  1 selectNode("document.body.getElementsByTagName('img')[0]");
206  1 openImageDialog(MENU_EDIT_IMAGE);
207  1 waitForStepToLoad(STEP_EXPLORER);
208    // test that the page is the right page
209  1 assertImageSelected(imageSpace, imagePage, imageFile);
210  1 clickButtonWithText(BUTTON_SELECT);
211  1 waitForStepToLoad(STEP_CONFIG);
212  1 assertEquals("200", getSelenium().getValue(INPUT_WIDTH));
213  1 assertEquals("No parking sign", getSelenium().getValue(INPUT_ALT));
214  1 assertTrue(isAlignmentSelected("CENTER"));
215    // To reset the image size we have to leave both width and height empty.
216  1 getSelenium().type(INPUT_WIDTH, "");
217  1 getSelenium().type(INPUT_HEIGHT, "");
218  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
219  1 waitForDialogToClose();
220   
221  1 switchToSource();
222  1 assertSourceText("= H =\n\n[[image:XWiki.AdminSheet@rights.png||alt=\"No parking sign\" "
223    + "style=\"display: block; margin-left: auto; margin-right: auto;\"]]x");
224    }
225   
226    /**
227    * Test that the insert image dialog defaults to the current page, and the page selection is preserved across
228    * multiple inserts.
229    */
 
230  1 toggle @Test
231    public void testDefaultSelection()
232    {
233  1 String imageSpace = "XWiki";
234  1 String imagePage = "AdminSheet";
235  1 String imageFile1 = "import.png";
236  1 String imageFile2 = "lf.png";
237   
238  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
239  1 waitForStepSelector();
240    // test that the default loaded view is the current page view
241  1 assertElementPresent("//div[contains(@class, \"" + STEP_CURRENT_PAGE_SELECTOR + "\")]");
242    // now switch view
243  1 clickTab(TAB_ALL_PAGES);
244  1 waitForStepToLoad(STEP_EXPLORER);
245  1 selectImage(imageSpace, imagePage, imageFile1);
246  1 clickButtonWithText(BUTTON_SELECT);
247  1 waitForStepToLoad(STEP_CONFIG);
248  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
249  1 waitForDialogToClose();
250   
251  1 moveCaret("document.body", 1);
252   
253  1 typeText("x");
254   
255    // now second image
256  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
257  1 waitForStepToLoad(STEP_SELECTOR);
258    // test that the default loaded view is the current page view
259  1 assertElementPresent("//div[contains(@class, \"" + STEP_CURRENT_PAGE_SELECTOR + "\")]");
260    // now switch view
261  1 clickTab(TAB_ALL_PAGES);
262  1 waitForStepToLoad(STEP_EXPLORER);
263    // test that the selectors are positioned to the old page
264  1 assertEquals(imageSpace, getSelenium().getSelectedValue(SPACE_SELECTOR));
265  1 assertEquals(imagePage, getSelenium().getSelectedValue(PAGE_SELECTOR));
266    // and select the new one
267  1 selectImage(imageFile2);
268  1 clickButtonWithText(BUTTON_SELECT);
269  1 waitForStepToLoad(STEP_CONFIG);
270  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
271  1 waitForDialogToClose();
272   
273  1 switchToSource();
274  1 assertSourceText("[[image:XWiki.AdminSheet@import.png]]x[[image:XWiki.AdminSheet@lf.png]]");
275    }
276   
277    /**
278    * Test that hitting the previous button in the configuration dialog preserves the image selector selection.
279    */
 
280  1 toggle @Test
281    public void testPreviousPreservesSelection()
282    {
283  1 String imageSpace = "XWiki";
284  1 String imagePage = "AdminSheet";
285  1 String imageFile1 = "export.png";
286  1 String imageFile2 = "import.png";
287   
288  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
289  1 waitForStepToLoad(STEP_SELECTOR);
290  1 clickTab(TAB_ALL_PAGES);
291  1 waitForStepToLoad(STEP_EXPLORER);
292  1 selectImage(imageSpace, imagePage, imageFile1);
293   
294  1 clickButtonWithText(BUTTON_SELECT);
295  1 waitForStepToLoad(STEP_CONFIG);
296  1 clickButtonWithText(BUTTON_CHANGE_IMAGE);
297   
298  1 waitForStepToLoad(STEP_EXPLORER);
299    // wait for the inner selector to load
300  1 getDriver().waitUntilElementIsVisible(
301    By.xpath("//*[contains(@class, \"" + STEP_EXPLORER + "\")]//*[contains(@class, \""
302    + STEP_CURRENT_PAGE_SELECTOR + "\")]"));
303  1 assertImageSelected(imageSpace, imagePage, imageFile1);
304   
305  1 selectImage(imageSpace, imagePage, imageFile2);
306  1 clickButtonWithText(BUTTON_SELECT);
307  1 waitForStepToLoad(STEP_CONFIG);
308  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
309   
310    // test that the correct image has been inserted
311  1 switchToSource();
312  1 assertSourceText("[[image:" + imageSpace + "." + imagePage + "@" + imageFile2 + "]]");
313    }
314   
315    /**
316    * Tests that an image can be removed from the menu, as well as using the delete key.
317    */
 
318  1 toggle @Test
319    public void testRemoveImage()
320    {
321  1 switchToSource();
322  1 setSourceText("[[image:xwiki:XWiki.AdminSheet@import.png]]Mmmh, cheese!"
323    + "[[image:xwiki:XWiki.AdminSheet@export.png]]");
324  1 switchToWysiwyg();
325  1 selectNode("document.body.firstChild.childNodes[2]");
326  1 clickMenu(MENU_IMAGE);
327  1 assertTrue(isMenuEnabled(MENU_REMOVE_IMAGE));
328  1 clickMenu(MENU_REMOVE_IMAGE);
329   
330  1 switchToSource();
331  1 assertSourceText("[[image:xwiki:XWiki.AdminSheet@import.png]]Mmmh, cheese!");
332  1 switchToWysiwyg();
333   
334  1 selectNode("document.body.firstChild.firstChild");
335  1 typeDelete();
336   
337  1 switchToSource();
338  1 assertSourceText("Mmmh, cheese!");
339    }
340   
341    /**
342    * Test that selecting the "Upload new image" option leads to the upload file dialog.
343    */
 
344  1 toggle @Test
345    public void testNewImageOptionLoadsFileUploadStep()
346    {
347  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
348  1 waitForStepToLoad(STEP_CURRENT_PAGE_SELECTOR);
349   
350    // wait for the default option to load and then click it
351  1 getDriver().waitUntilElementIsVisible(By.className("xNewImagePreview"));
352  1 getSelenium().click("//div[contains(@class, \"xNewImagePreview\")]");
353  1 clickButtonWithText(BUTTON_SELECT);
354   
355  1 waitForStepToLoad(STEP_UPLOAD);
356  1 closeDialog();
357   
358  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
359  1 waitForStepToLoad(STEP_SELECTOR);
360  1 clickTab(TAB_ALL_PAGES);
361  1 waitForStepToLoad(STEP_EXPLORER);
362   
363    // wait for the default option to show up and then click it
364  1 String newImageLocator = "//div[@class = 'xImagesExplorer']//div[@class = 'xNewImagePreview']";
365  1 waitForElement(newImageLocator);
366  1 getSelenium().click(newImageLocator);
367  1 clickButtonWithText(BUTTON_SELECT);
368   
369  1 waitForStepToLoad(STEP_UPLOAD);
370  1 closeDialog();
371    }
372   
373    /**
374    * Test that editing an image and not changing its location preserves a full reference and does not change it to a
375    * relative one.
376    */
 
377  1 toggle @Test
378    public void testEditImagePreservesFullReferences()
379    {
380  1 switchToSource();
381  1 setSourceText("[[image:xwiki:XWiki.AdminSheet@import.png]] [[image:XWiki.AdminSheet@export.png]]");
382  1 switchToWysiwyg();
383   
384    // edit first image
385  1 selectNode("document.body.firstChild.firstChild");
386  1 openImageDialog(MENU_EDIT_IMAGE);
387  1 waitForStepToLoad(STEP_SELECTOR);
388  1 waitForStepToLoad(STEP_EXPLORER);
389  1 assertImageSelected("XWiki", "AdminSheet", "import.png");
390   
391  1 clickButtonWithText(BUTTON_SELECT);
392  1 waitForStepToLoad(STEP_CONFIG);
393   
394  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
395  1 waitForDialogToClose();
396   
397    // edit second image too
398  1 selectNode("document.body.firstChild.childNodes[2]");
399  1 openImageDialog(MENU_EDIT_IMAGE);
400  1 waitForStepToLoad(STEP_SELECTOR);
401  1 waitForStepToLoad(STEP_EXPLORER);
402  1 assertImageSelected("XWiki", "AdminSheet", "export.png");
403   
404  1 clickButtonWithText(BUTTON_SELECT);
405  1 waitForStepToLoad(STEP_CONFIG);
406   
407  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
408  1 waitForDialogToClose();
409   
410  1 switchToSource();
411  1 assertSourceText("[[image:xwiki:XWiki.AdminSheet@import.png]] [[image:XWiki.AdminSheet@export.png]]");
412    }
413   
414    /**
415    * Test that, upon editing an image which is the label of a link, the link is preserved.
416    * <p>
417    * See http://jira.xwiki.org/jira/browse/XWIKI-3784.
418    */
 
419  1 toggle @Test
420    public void testEditImageWithLink()
421    {
422    // add all the image & link, otherwise it will not reproduce, it only reproduces if container is body
423  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
424  1 waitForStepToLoad(STEP_SELECTOR);
425    // switch to all pages view
426  1 clickTab(TAB_ALL_PAGES);
427  1 waitForStepToLoad(STEP_EXPLORER);
428  1 selectImage("XWiki", "AdminSheet", "registration.png");
429  1 clickButtonWithText(BUTTON_SELECT);
430  1 waitForStepToLoad(STEP_CONFIG);
431   
432  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
433  1 waitForDialogToClose();
434   
435  1 selectNode("document.body.firstChild");
436   
437    // add link around the image
438  1 clickMenu(LinkTest.MENU_LINK);
439  1 clickMenu(LinkTest.MENU_WIKI_PAGE);
440  1 waitForDialogToLoad();
441  1 waitForStepToLoad(STEP_SELECTOR);
442    // get the all pages tree
443  1 clickTab(TAB_ALL_PAGES);
444  1 waitForStepToLoad(LinkTest.STEP_EXPLORER);
445  1 explorer.waitForIt().findAndSelectPage("First blog post");
446  1 clickButtonWithText(BUTTON_SELECT);
447  1 waitForStepToLoad("xLinkConfig");
448  1 assertEquals("registration.png", getInputValue(LinkTest.LABEL_INPUT_TITLE));
449    // check that the label is readonly
450  1 assertFalse(getSelenium().isEditable("//input[@title=\"" + LinkTest.LABEL_INPUT_TITLE + "\"]"));
451   
452  1 clickButtonWithText("Create Link");
453  1 waitForDialogToClose();
454   
455    // edit image
456  1 selectNode("document.body.firstChild.firstChild");
457  1 openImageDialog(MENU_EDIT_IMAGE);
458  1 waitForStepToLoad(STEP_SELECTOR);
459  1 waitForStepToLoad(STEP_EXPLORER);
460  1 assertImageSelected("XWiki", "AdminSheet", "registration.png");
461  1 selectImage("Sandbox", "WebHome", "XWikiLogo.png");
462  1 clickButtonWithText(BUTTON_SELECT);
463  1 waitForStepToLoad(STEP_CONFIG);
464    // clear the alt text
465  1 getSelenium().type(INPUT_ALT, "");
466    // We need to reset the image size because we changed the image source and the previous size will be compared
467    // either with the old image if the new image hasn't been loaded yet or with the new image if it was previously
468    // loaded. In the first case the size didn't change so it doesn't appear in the wiki syntax. In the second case
469    // the new image has a different size so the old size (if not changed) must be explicitly set in the wiki
470    // syntax.
471  1 getSelenium().type(INPUT_WIDTH, "");
472  1 getSelenium().type(INPUT_HEIGHT, "");
473  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
474  1 waitForDialogToClose();
475   
476  1 switchToSource();
477  1 assertSourceText("[[[[image:Sandbox.WebHome@XWikiLogo.png]]>>doc:Blog.BlogIntroduction]]");
478    }
479   
480    /**
481    * Tests that the option to upload a new image is selected by default when inserting a new image and when the edited
482    * image is not attached to the current page.
483    */
 
484  1 toggle @Test
485    public void testNewImageOptionIsSelectedByDefault()
486    {
487    // Insert a new image.
488  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
489    // Look on the current page selector.
490  1 waitForStepToLoad(STEP_CURRENT_PAGE_SELECTOR);
491  1 assertElementPresent("//div[@class = 'xImagesSelector']//" + "div[contains(@class, 'xListItem-selected')]"
492    + "//div[contains(@class, 'xNewImagePreview')]");
493    // Look on the all pages selector.
494  1 clickTab(TAB_ALL_PAGES);
495  1 waitForStepToLoad(STEP_EXPLORER);
496  1 assertElementPresent("//div[@class = 'xImagesExplorer']//" + "div[contains(@class, 'xListItem-selected')]"
497    + "//div[contains(@class, 'xNewImagePreview')]");
498  1 closeDialog();
499   
500    // Edit an image.
501  1 switchToSource();
502  1 setSourceText("[[image:xwiki:Sandbox.WebHome@XWikiLogo.png]]");
503  1 switchToWysiwyg();
504  1 selectNode("document.body.firstChild.firstChild");
505  1 openImageDialog(MENU_EDIT_IMAGE);
506  1 waitForStepToLoad(STEP_SELECTOR);
507    // Look on the current page selector.
508  1 clickTab(TAB_CURRENT_PAGE);
509  1 waitForStepToLoad(STEP_CURRENT_PAGE_SELECTOR);
510  1 assertElementPresent("//div[@class = 'xImagesSelector']//" + "div[contains(@class, 'xListItem-selected')]"
511    + "//div[contains(@class, 'xNewImagePreview')]");
512  1 closeDialog();
513    }
514   
515    /**
516    * Test that the validation errors in the image insert steps are hidden on the next display of the steps.
517    */
 
518  1 toggle @Test
519    public void testErrorIsHiddenOnNextDisplay()
520    {
521    // Get an error in the file upload step and check that on previous, next is not displayed anymore.
522  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
523  1 waitForStepToLoad(STEP_CURRENT_PAGE_SELECTOR);
524  1 assertElementPresent("//*[contains(@class, 'xListItem-selected')]//*[contains(@class, 'xNewImagePreview')]");
525  1 clickButtonWithText(BUTTON_SELECT);
526  1 waitForStepToLoad(STEP_UPLOAD);
527  1 clickButtonWithText(BUTTON_IMAGE_SETTINGS);
528  1 waitForStepToLoad(STEP_UPLOAD);
529  1 assertFieldErrorIsPresent("The file path was not set", FILE_UPLOAD_INPUT);
530  1 clickButtonWithText(BUTTON_PREVIOUS);
531  1 waitForStepToLoad(STEP_CURRENT_PAGE_SELECTOR);
532  1 clickButtonWithText(BUTTON_SELECT);
533  1 waitForStepToLoad(STEP_UPLOAD);
534  1 assertFieldErrorIsNotPresent();
535    // Get the error again, close, open and test that error is no longer there.
536  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
537  1 waitForStepToLoad(STEP_UPLOAD);
538  1 assertFieldErrorIsPresent("The file path was not set", FILE_UPLOAD_INPUT);
539  1 closeDialog();
540  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
541  1 waitForStepToLoad(STEP_CURRENT_PAGE_SELECTOR);
542  1 assertElementPresent("//*[contains(@class, 'xListItem-selected')]//*[contains(@class, 'xNewImagePreview')]");
543  1 clickButtonWithText(BUTTON_SELECT);
544  1 waitForStepToLoad(STEP_UPLOAD);
545  1 assertFieldErrorIsNotPresent();
546  1 closeDialog();
547    }
548   
549    /**
550    * Tests fast navigation in the images list: double click and enter advance to the next step.
551    */
 
552  1 toggle @Test
553    public void testFastNavigationToSelectImage()
554    {
555    // double click to select the new image option
556  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
557  1 waitForStepToLoad(STEP_CURRENT_PAGE_SELECTOR);
558    // click first to make sure selection is set
559  1 getSelenium().click("//*[contains(@class, 'xListItem')]//*[contains(@class, 'xNewImagePreview')]");
560  1 getSelenium().doubleClick("//*[contains(@class, 'xListItem')]//*[contains(@class, 'xNewImagePreview')]");
561  1 waitForStepToLoad(STEP_UPLOAD);
562  1 closeDialog();
563   
564    // enter to select the new image option
565  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
566  1 waitForStepToLoad(STEP_CURRENT_PAGE_SELECTOR);
567  1 getSelenium().click("//div[contains(@class, \"xNewImagePreview\")]");
568  1 getSelenium().typeKeys(IMAGES_LIST, "\\13");
569  1 waitForStepToLoad(STEP_UPLOAD);
570  1 closeDialog();
571   
572    // double click to add an image from another page
573  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
574  1 waitForStepToLoad(STEP_SELECTOR);
575  1 clickTab(TAB_ALL_PAGES);
576  1 selectLocation("XWiki", "AdminSheet");
577  1 getSelenium().click(getImageLocator("registration.png"));
578  1 getSelenium().doubleClick(getImageLocator("registration.png"));
579  1 waitForStepToLoad(STEP_CONFIG);
580  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
581   
582  1 switchToSource();
583  1 setSourceText("[[image:XWiki.AdminSheet@registration.png]]");
584  1 switchToWysiwyg();
585   
586    // Reset the image selection.
587  1 moveCaret("document.body", 0);
588   
589    // enter to test enter upload in all pages
590  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
591  1 waitForStepToLoad(STEP_SELECTOR);
592  1 clickTab(TAB_ALL_PAGES);
593  1 selectLocation("XWiki", "AdminSheet");
594  1 getSelenium().click("//div[@class = '" + STEP_EXPLORER + "']//div[contains(@class, \"xNewImagePreview\")]");
595  1 getSelenium().typeKeys("//div[@class = '" + STEP_EXPLORER + "']" + IMAGES_LIST, "\\13");
596  1 waitForStepToLoad(STEP_UPLOAD);
597  1 closeDialog();
598    }
599   
600    /**
601    * @see XWIKI-3741: Editing an image removes existing unknown custom parameters.
602    */
 
603  1 toggle @Test
604    public void testUneditableAttributesArePreserved()
605    {
606    // Insert an image with attribute that cannot be edited through the Edit Image wizard.
607  1 switchToSource();
608  1 setSourceText("[[image:XWiki.AdminSheet@export.png||id=\"foobar\" "
609    + "title=\"abc\" foo=\"bar\" style=\"margin-top:12px\"]]");
610  1 switchToWysiwyg();
611   
612    // Select the image and edit it.
613  1 selectNode("document.body.firstChild.firstChild");
614  1 openImageDialog(MENU_EDIT_IMAGE);
615  1 waitForStepToLoad(STEP_EXPLORER);
616  1 clickButtonWithText(BUTTON_SELECT);
617  1 waitForStepToLoad(STEP_CONFIG);
618  1 getSelenium().type(INPUT_WIDTH, "75px");
619  1 getSelenium().type(INPUT_HEIGHT, "7.5em");
620  1 selectAlignment("RIGHT");
621  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
622  1 waitForDialogToClose();
623   
624    // Check the result.
625  1 switchToSource();
626  1 assertSourceText("[[image:XWiki.AdminSheet@export.png||foo=\"bar\" id=\"foobar\" "
627    + "style=\"margin-top: 12px; height: 7.5em; float: right;\" title=\"abc\" width=\"75px\"]]");
628    }
629   
630    /**
631    * Tests that images attached to pages with special characters in their names are properly inserted.
632    */
 
633  1 toggle @Test
634    public void testInsertImageFromPageWithSpecialCharactersInName()
635    {
636    // Create a page with special characters in its name by copying a page with image attachments.
637  1 String spaceName = this.getClass().getSimpleName() + ":x.y@z";
638  1 String escapedSpaceName = spaceName.replaceAll("([\\:\\.])", "\\\\$1");
639  1 String pageName = getTestMethodName() + ":a.b@c";
640  1 String escapedPageName = pageName.replace(".", "\\.");
641  1 String pageFullName = String.format("%s.%s", escapedSpaceName, escapedPageName);
642  1 deletePage(spaceName, pageName);
643  1 assertTrue(copyPage("XWiki", "AdminSheet", escapedSpaceName, pageName));
644   
645    // Come back to the edited page.
646  1 open(this.getClass().getSimpleName(), getTestMethodName(), "edit", "editor=wysiwyg");
647  1 waitForEditorToLoad();
648   
649    // Insert an image from the created page.
650  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
651  1 waitForStepToLoad(STEP_SELECTOR);
652  1 clickTab(TAB_ALL_PAGES);
653  1 selectLocation(spaceName, pageName);
654  1 getSelenium().click(getImageLocator("export.png"));
655  1 clickButtonWithText(BUTTON_SELECT);
656  1 waitForStepToLoad(STEP_CONFIG);
657  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
658  1 waitForDialogToClose();
659   
660    // Check the result.
661  1 switchToSource();
662  1 assertSourceText(String.format("[[image:%s@export.png]]", pageFullName));
663  1 switchToWysiwyg();
664   
665    // Edit the inserted image.
666  1 openImageDialog(MENU_EDIT_IMAGE);
667  1 waitForStepToLoad(STEP_SELECTOR);
668  1 waitForStepToLoad(STEP_EXPLORER);
669  1 assertImageSelected(spaceName, pageName, "export.png");
670  1 selectImage(spaceName, pageName, "import.png");
671  1 clickButtonWithText(BUTTON_SELECT);
672  1 waitForStepToLoad(STEP_CONFIG);
673  1 getSelenium().type(INPUT_ALT, "");
674  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
675  1 waitForDialogToClose();
676   
677    // Check the result.
678  1 switchToSource();
679  1 assertSourceText(String.format("[[image:%s@import.png]]", pageFullName));
680    }
681   
682    /**
683    * Tests if the edited image is selected when the image selector wizard step is opened.
684    */
 
685  1 toggle @Test
686    public void testEditedImageIsSelected()
687    {
688    // Insert two different images.
689  1 switchToSource();
690  1 setSourceText("image:XWiki.AdminSheet@users.png\n\nimage:XWiki.AdminSheet@export.png");
691  1 switchToWysiwyg();
692   
693    // Edit the first image and check if it is selected in the image selector wizard step.
694  1 selectNode("document.body.getElementsByTagName('img')[0]");
695  1 openImageDialog(MENU_EDIT_IMAGE);
696  1 waitForStepToLoad(STEP_SELECTOR);
697  1 waitForStepToLoad(STEP_EXPLORER);
698  1 assertImageSelected("XWiki", "AdminSheet", "users.png");
699  1 closeDialog();
700   
701    // Edit the second image and check if it is selected in the image selector wizard step.
702  1 selectNode("document.body.getElementsByTagName('img')[1]");
703  1 openImageDialog(MENU_EDIT_IMAGE);
704  1 waitForStepToLoad(STEP_SELECTOR);
705  1 waitForStepToLoad(STEP_EXPLORER);
706  1 assertImageSelected("XWiki", "AdminSheet", "export.png");
707   
708    // Select a different image and refresh the image list to see if the edited image is reselected.
709  1 selectImage("presentation.png");
710  1 getSelenium().click("//div[@class=\"xPageChooser\"]//button[text()=\"Update\"]");
711  1 getDriver().waitUntilElementIsVisible(
712    By.xpath("//*[contains(@class, \"" + STEP_EXPLORER + "\")]//*[contains(@class, \""
713    + STEP_CURRENT_PAGE_SELECTOR + "\")]"));
714  1 waitForElement(getImageLocator("export.png"));
715    }
716   
717    /**
718    * Tests if an external image can be inserted.
719    */
 
720  1 toggle @Test
721    public void testInsertExternalImage()
722    {
723  1 openImageDialog(MENU_INSERT_EXTERNAL_IMAGE);
724  1 waitForStepToLoad(STEP_EXTERNAL_IMAGE);
725   
726    // Try to move to the next step without setting the image location.
727  1 clickButtonWithText(BUTTON_IMAGE_SETTINGS);
728  1 waitForStepToLoad(STEP_EXTERNAL_IMAGE);
729  1 assertFieldErrorIsPresent("Please specify the image location.", INPUT_EXTERNAL_IMAGE_LOCATION);
730   
731    // Set the image URL and insert the image.
732  1 String imageURL = "http://www.xwiki.org/xwiki/skins/toucan/logo.png";
733  1 getSelenium().type(INPUT_EXTERNAL_IMAGE_LOCATION, imageURL);
734  1 clickButtonWithText(BUTTON_IMAGE_SETTINGS);
735  1 waitForStepToLoad(STEP_CONFIG);
736   
737    // The alternative text should be set by default to the image URL.
738  1 assertEquals(imageURL, getSelenium().getValue(INPUT_ALT));
739  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
740  1 waitForDialogToClose();
741   
742    // Check the result.
743  1 switchToSource();
744  1 assertSourceText(String.format("[[image:%s]]", imageURL));
745    }
746   
747    /**
748    * Tests if an external image can be selected.
749    */
 
750  1 toggle @Test
751    public void testEditExternalImage()
752    {
753    // Insert an external image.
754  1 switchToSource();
755  1 String imageURL = "http://www.xwiki.org/xwiki/skins/toucan/logo.png";
756  1 String alternativeText = "xyz";
757  1 setSourceText(String.format("[[image:%s||alt=\"%s\" title=\"abc\"]]", imageURL, alternativeText));
758  1 switchToWysiwyg();
759   
760    // Edit the external image and change its location.
761  1 selectNode("document.body.getElementsByTagName('img')[0]");
762  1 openImageDialog(MENU_EDIT_IMAGE);
763  1 waitForStepToLoad(STEP_EXTERNAL_IMAGE);
764  1 assertEquals(imageURL, getSelenium().getValue(INPUT_EXTERNAL_IMAGE_LOCATION));
765   
766    // Change the image location.
767  1 imageURL = "http://www.xwiki.org/xwiki/skins/colibri/logo.png";
768  1 getSelenium().type(INPUT_EXTERNAL_IMAGE_LOCATION, imageURL);
769    // Quickly insert the image, skipping the settings step.
770  1 clickButtonWithText(BUTTON_INSERT_IMAGE);
771  1 waitForDialogToClose();
772   
773    // Check the result.
774  1 switchToSource();
775    // The image parameters must be preserved.
776  1 setSourceText(String.format("[[image:%s||alt=\"%s\" title=\"abc\"]]", imageURL, alternativeText));
777    }
778   
779    /**
780    * Tests that the configuration that limits the image selection to the current page works as expected.
781    */
 
782  1 toggle @Test
783    public void testImageSelectionLimitedToCurrentPage()
784    {
785  1 String allPagesTabLocator =
786    "//*[contains(@class, 'xStepsTabs')]//*[@class = 'gwt-TabBarItem' and . = '" + TAB_ALL_PAGES + "']";
787  1 String location = getSelenium().getLocation();
788   
789    // By default, attachment selection shoudn't be limited to the current page.
790  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
791  1 waitForStepToLoad("xSelectorAggregatorStep");
792  1 assertElementPresent(allPagesTabLocator);
793   
794    // Change the configuration.
795  1 open("XWiki", "WysiwygEditorConfig", "edit", "editor=object");
796  1 expandObject("XWiki.WysiwygEditorConfigClass", 0);
797  1 checkField("XWiki.WysiwygEditorConfigClass_0_imageSelectionLimited");
798  1 clickEditSaveAndContinue();
799   
800  1 try {
801  1 open(location);
802  1 waitForEditorToLoad();
803   
804    // The "All Pages" tab should be hidden now.
805  1 openImageDialog(MENU_INSERT_ATTACHED_IMAGE);
806  1 waitForStepToLoad("xSelectorAggregatorStep");
807  1 assertElementNotPresent(allPagesTabLocator);
808    } finally {
809    // Restore the configuration.
810  1 open("XWiki", "WysiwygEditorConfig", "edit", "editor=object");
811  1 expandObject("XWiki.WysiwygEditorConfigClass", 0);
812  1 checkField("XWiki.WysiwygEditorConfigClass_0_imageSelectionLimited_false");
813  1 clickEditSaveAndContinue();
814    }
815    }
816   
 
817  80 toggle private void waitForStepToLoad(String stepClass)
818    {
819  80 getDriver().waitUntilElementIsVisible(By.className(stepClass));
820    }
821   
 
822  9 toggle private void selectImage(String space, String page, String filename)
823    {
824  9 selectLocation(space, page);
825  9 selectImage(filename);
826    }
827   
 
828  12 toggle private void selectLocation(String space, String page)
829    {
830    // Note: The wiki selector is generated only in multiwiki mode, so for XE the first HTML select from the page
831    // chooser panel is in fact the space selector.
832  12 waitForElement(SPACE_SELECTOR + "/option[. = '" + space + "']");
833  12 getSelenium().select(SPACE_SELECTOR, space);
834   
835  12 waitForElement(PAGE_SELECTOR + "/option[. = '" + page + "']");
836  12 getSelenium().select(PAGE_SELECTOR, page);
837   
838    // If the list of spaces/pages is long then the space/page we want to select might not be visible when the drop
839    // down is opened so Selenium will try to scroll it into view. Unfortunately Selenium doesn't scroll only the
840    // drop down list but also the entire page. The select fields end up at the top of the page where, for some
841    // unknown reason, the drop down list remains opened after an option is selected. As a consequence clicking on
842    // the Update button has only the effect of closing the drop down without triggering the expected update. Make
843    // sure the drop down is closed by pressing the Tab key to move the focus to the Update button.
844  12 getSelenium().typeKeys(PAGE_SELECTOR, "\t");
845   
846  12 getSelenium().click("//div[@class=\"xPageChooser\"]//button[text()=\"Update\"]");
847  12 waitForElement("//*[@class = '" + STEP_EXPLORER + "']//*[contains(@class, '" + STEP_CURRENT_PAGE_SELECTOR
848    + "')]");
849    }
850   
 
851  11 toggle private void selectImage(String filename)
852    {
853  11 waitForElement(getImageLocator(filename));
854  11 getSelenium().click(getImageLocator(filename));
855    }
856   
 
857  26 toggle private String getImageLocator(String filename)
858    {
859  26 return "//div[@class=\"xImagesSelector\"]//img[@title=\"" + filename + "\"]";
860    }
861   
 
862  9 toggle private void assertImageSelected(String space, String page, String filename)
863    {
864  9 getDriver().waitUntilElementIsVisible(By.xpath(SPACE_SELECTOR + "/option[. = \"" + space + "\"]"));
865  9 assertEquals(space, getSelenium().getSelectedLabel(SPACE_SELECTOR));
866   
867  9 getDriver().waitUntilElementIsVisible(By.xpath(PAGE_SELECTOR + "/option[. = \"" + page + "\"]"));
868  9 assertEquals(page, getSelenium().getSelectedLabel(PAGE_SELECTOR));
869   
870  9 assertImageSelected(filename);
871    }
872   
 
873  14 toggle private void clickTab(String tabName)
874    {
875  14 String tabSelector = "//div[.='" + tabName + "']";
876  14 getSelenium().click(tabSelector);
877    }
878   
 
879  9 toggle private void assertImageSelected(String filename)
880    {
881  9 String imageItem =
882    "//div[@class=\"xImagesSelector\"]//div[contains(@class, \"xListItem-selected\")]//img[@title=\""
883    + filename + "\"]";
884  9 assertElementPresent(imageItem);
885    }
886   
 
887  2 toggle private void selectAlignment(String alignment)
888    {
889  2 getSelenium().click(
890    "//div[contains(@class, \"AlignPanel\")]//input[@name=\"alignment\" and @value=\"" + alignment + "\"]");
891    }
892   
 
893  1 toggle public boolean isAlignmentSelected(String alignment)
894    {
895  1 return getDriver().hasElementWithoutWaiting(
896    By.xpath("//div[contains(@class, \"AlignPanel\")]//input[@name=\"alignment\" and @value=\"" + alignment
897    + "\" and @checked=\"\"]"));
898    }
899   
 
900  31 toggle private void openImageDialog(String menuName)
901    {
902  31 clickMenu(MENU_IMAGE);
903  31 assertTrue(isMenuEnabled(menuName));
904  31 clickMenu(menuName);
905  31 waitForDialogToLoad();
906    }
907   
908    /**
909    * Wait for the step selector to load.
910    */
 
911  1 toggle private void waitForStepSelector()
912    {
913  1 getDriver().waitUntilElementIsVisible(
914    By.xpath("//table[contains(@class, 'xStepsTabs') and not(contains(@class, 'loading'))]"));
915    }
916    }