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

File TabsTest.java

 

Code metrics

0
145
18
1
407
253
18
0.12
8.06
18
1

Classes

Class Line # Actions
TabsTest 33 145 0% 18 0
1.0100%
 

Contributing tests

This file is covered by 17 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 java.util.Date;
23   
24    import org.junit.Test;
25    import org.openqa.selenium.By;
26    import org.openqa.selenium.Keys;
27    import org.xwiki.test.wysiwyg.framework.AbstractWysiwygTestCase;
28   
29    import com.thoughtworks.selenium.Wait;
30   
31    import static org.junit.Assert.*;
32   
 
33    public class TabsTest extends AbstractWysiwygTestCase
34    {
 
35  1 toggle @Test
36    public void testMultipleClicksOnTheSameTab()
37    {
38  1 setContent("<strong>foo</strong>");
39  1 switchToWysiwyg(false);
40  1 switchToWysiwyg();
41  1 switchToSource();
42  1 assertSourceText("**foo**");
43    }
44   
45    /**
46    * Tests that XWIKI-3834 remains fixed.
47    */
 
48  1 toggle @Test
49    public void testMultipleSwitches()
50    {
51  1 StringBuffer content = new StringBuffer();
52    // We put quite a lot of content so that the conversion is not immediate.
53  1 content.append("<strong>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor "
54    + "incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation "
55    + "ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
56    + "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non "
57    + "proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
58  1 content.append("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt "
59    + "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco "
60    + "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
61    + "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non "
62    + "proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
63  1 content.append("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt "
64    + "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco "
65    + "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
66    + "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non "
67    + "proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
68  1 content.append("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt "
69    + "ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco "
70    + "laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in "
71    + "voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non "
72    + "proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</strong>");
73  1 setContent(content.toString());
74   
75    // We go back and forth multiple times to be sure that it is not a matter of chance.
76  1 switchToSource(false);
77  1 switchToWysiwyg(false);
78  1 switchToSource(false);
79  1 switchToWysiwyg(false);
80  1 switchToSource();
81   
82  1 assertFalse(getSourceText().contains("strong"));
83    }
84   
85    /**
86    * @see XWIKI-4079: Links are lost when switching to Source in the WYSIWYG editor.
87    */
 
88  1 toggle @Test
89    public void testLinksAreNotLostWhenSwitchingToSourceTab()
90    {
91  1 String content = "Visit [[XWiki>>http://www.xwiki.org]] and our [[blog>>Blog.WebHome]].";
92  1 switchToSource();
93  1 setSourceText(content);
94  1 switchToWysiwyg();
95  1 switchToSource();
96  1 assertSourceText(content);
97    }
98   
99    /**
100    * @see XWIKI-3965: Relative images are not displayed when switching from Source tab to Wysiwyg tab.
101    */
 
102  1 toggle @Test
103    public void testContextDocumentIsPreserved()
104    {
105    // Uploading an image to the current document is difficult. Instead we use a context sensitive velocity script.
106  1 clickEditPageInWikiSyntaxEditor();
107  1 setFieldValue("content", "{{velocity}}$doc.fullName{{/velocity}}");
108  1 clickEditPageInWysiwyg();
109  1 waitForEditorToLoad();
110  1 String expected = getRichTextArea().getText();
111  1 switchToSource();
112  1 switchToWysiwyg();
113  1 assertEquals(expected, getRichTextArea().getText());
114    }
115   
116    /**
117    * Switches to source tab while the rich text area is still loading. The source text must remain unchanged.
118    */
 
119  1 toggle @Test
120    public void testSwitchToSourceWhileWysiwygIsLoading()
121    {
122  1 switchToSource();
123  1 StringBuffer sourceText = new StringBuffer();
124  1 sourceText.append("{{code language=\"java\"}}\n");
125  1 sourceText.append("public interface Command {\n");
126  1 sourceText.append(" boolean execute(String parameter);\n");
127  1 sourceText.append("}\n");
128  1 sourceText.append("{{/code}}");
129  1 setSourceText(sourceText.toString());
130    // Set the cursor position before "language" to see if it is preserved.
131  1 getSourceTextArea().sendKeys(Keys.HOME, Keys.PAGE_UP, Keys.chord(Keys.CONTROL, Keys.ARROW_RIGHT),
132    Keys.ARROW_RIGHT);
133    // Switch to WYSIWYG tab but don't wait for the rich text area to load.
134  1 switchToWysiwyg(false);
135    // Switch back to source tab.
136  1 switchToSource();
137  1 getSourceTextArea().sendKeys("x");
138    // Check the source text. We don't assert the cursor position directly because it isn't available when the test
139    // is run in background.
140  1 assertSourceText(sourceText.substring(0, 7) + "x" + sourceText.substring(7));
141    }
142   
143    /**
144    * Switches to source tab and waits for the conversion. The cursor should be placed at the start.
145    *
146    * @see XWIKI-4392: Place the caret at the beginning of the content when swtching to WYSIWYG Source editor.
147    */
 
148  1 toggle @Test
149    public void testSwitchToSourceWithHTMLChangesAndWait()
150    {
151  1 typeText("1");
152  1 applyStyleTitle1();
153  1 switchToSource();
154  1 getSourceTextArea().sendKeys("x");
155  1 assertSourceText("x= 1 =");
156    }
157   
158    /**
159    * Switches to source tab without changing the HTML. The source selection should be preserved.
160    */
 
161  1 toggle @Test
162    public void testSwitchToSourceWithoutHTMLChanges()
163    {
164  1 switchToSource();
165  1 String sourceText = "one **two** three";
166  1 setSourceText(sourceText);
167    // Move the caret between the first two *.
168  1 getSourceTextArea().sendKeys(Keys.chord(Keys.CONTROL, Keys.ARROW_LEFT, Keys.ARROW_LEFT), Keys.ARROW_LEFT);
169  1 switchToWysiwyg();
170    // Switch back to source tab without changing the HTML.
171  1 switchToSource();
172  1 getSourceTextArea().sendKeys("x");
173    // Check the source text.
174  1 assertSourceText(sourceText.substring(0, 5) + "x" + sourceText.substring(5));
175    }
176   
177    /**
178    * Switches to source tab but doesn't wait for the result. Comes back to source tab when the result is received.
179    */
 
180  1 toggle @Test
181    public void testSwitchToSourceWithHTMLChangesAndDontWait()
182    {
183  1 setContent("<!--startmacro:code|-|language=\"java\"|-|public class Apple extends Fruit {\n"
184    + " public String getColor() {\n return Colors.RED;\n }\n}--><!--stopmacro-->");
185    // Switch to source tab but don't wait for the conversion result.
186  1 switchToSource(false);
187    // Switch back to WYSIWYG tab.
188  1 switchToWysiwyg();
189    // Wait for the conversion result on the WYSIWYG tab.
190  1 new Wait()
191    {
 
192  1 toggle public boolean until()
193    {
194  1 return getSourceText().length() > 1;
195    }
196    }.wait("Conversion takes too long!");
197    // Switch to source tab without waiting. The source text was already received.
198  1 switchToSource(false);
199  1 assertTrue(getSourceTextArea().isEnabled());
200  1 getSourceTextArea().sendKeys("x");
201  1 assertSourceText("x{{code language=\"java\"}}\npublic class Apple extends Fruit {\n"
202    + " public String getColor() {\n return Colors.RED;\n }\n}\n{{/code}}");
203    }
204   
205    /**
206    * Test if the editor switches back to WYSIWYG tab when the conversion from HTML to source text fails.
207    */
 
208  1 toggle @Test
209    public void testSwitchToSourceFailure()
210    {
211    // Put some bogus content in the rich text area. This content should cause a conversion exception.
212  1 String content = "<span class=\"wikiexternallink\"><a href=\"mailto:x@y.z\">xyz</a></span><!--stopwikilink-->";
213  1 setContent(content);
214    // Try to switch to source. Don't wait because the plain text area will remain disabled.
215  1 switchToSource(false);
216    // The editor switches back to WYSIWYG tab if it catches a conversion exception.
217  1 waitForElement("//div[@class = 'gwt-TabBarItem gwt-TabBarItem-selected']/div[. = 'WYSIWYG']");
218    // The HTML mustn't change.
219  1 assertContent(content);
220    // Let's see if we can correct the mistake.
221  1 setContent("<p>Should be fine now.</p>");
222  1 switchToSource();
223  1 assertSourceText("Should be fine now.");
224    }
225   
226    /**
227    * Tests if the switch to source action can be canceled.
228    */
 
229  1 toggle @Test
230    public void testCancelSwitchToSource()
231    {
232    // Put some content in the rich text area.
233  1 setContent("<h1>Heading</h1><p>paragraph</p><ul><li>list</li></ul><table><tr><td>cell</td></tr></table>");
234    // Place the caret inside the heading.
235  1 moveCaret("document.body.firstChild.firstChild", 3);
236    // Switch to source but don't wait till the conversion is done.
237  1 switchToSource(false);
238    // Switch back to rich text area, before receiving the source text.
239  1 switchToWysiwyg();
240    // Change the rich text.
241  1 typeText("X");
242    // Switch to source again, this time with a different rich text. Wait for the conversion to end.
243  1 switchToSource();
244    // Check the result.
245  1 assertSourceText("= HeaXding =\n\nparagraph\n\n* list\n\n|cell");
246    }
247   
248    /**
249    * @see XWIKI-4517: NullPointerException thrown when switching to Source tab before the rich text area has finished
250    * loading
251    */
 
252  1 toggle @Test
253    public void testSwitchToSourceBeforeWysiwygLoad()
254    {
255    // Switch to source and put some content that takes time to render.
256  1 switchToSource();
257  1 String sourceText =
258    "{{code}}long x = 1L;{{/code}}\n\n{{html}}" + new Date().getTime()
259    + "{{/html}}\n\n{{velocity}}$doc.name{{/velocity}}";
260  1 setSourceText(sourceText);
261  1 clickEditSaveAndContinue();
262    // Reload the page to be able to switch to source tab before the rich text area finishes loading.
263  1 getSelenium().runScript("window.location.reload(true)");
264    // Switch to source tab as soon as the source tab is available.
265  1 waitForElement("//div[@class = 'gwt-TabBarItem']/div[. = 'Source']");
266  1 switchToSource();
267    // Verify the source text.
268  1 assertSourceText(sourceText);
269    // NOTE: If the WYSIWYG editor is loaded when the page loads then the source text area is unusable until after
270    // the load event fires which happens after all the external resource including in-line frames are completely
271    // loaded. So even though we can switch to source tab before the rich text area finishes loading, we might not
272    // be able to use the plain text area. As a consequence we can't write a more elaborate test. The solution is to
273    // load the editor after the host page finishes loading.
274    }
275   
276    /**
277    * Switches to WYSIWYG tab while the source text area is still loading (i.e. waiting for a HTML to source conversion
278    * result). The rich text must remain unchanged.
279    */
 
280  1 toggle @Test
281    public void testSwitchToWysiwygWhileSourceIsLoading()
282    {
283  1 String content =
284    "before <!--startmacro:code|-|language=\"java\"|-|\\npublic class Apple extends Fruit {\n"
285    + " public String getColor() {\n return Colors.RED;\n }\n}\n--><!--stopmacro--> after";
286  1 setContent(content);
287    // Select some text to see if the selection is preserved.
288  1 select("document.body.firstChild", 3, "document.body.firstChild", 6);
289    // Switch to source but don't wait for the conversion result.
290  1 switchToSource(false);
291    // Switch back to WYSIWYG tab before the conversion result is received.
292  1 switchToWysiwyg();
293    // Overwrite the selected text.
294  1 typeText("#");
295    // Check the result.
296  1 assertContent(content.substring(0, 3) + "#" + content.substring(6));
297    }
298   
299    /**
300    * Switches to WYSIWYG tab without changing the source text. The DOM selection should be preserved.
301    */
 
302  1 toggle @Test
303    public void testSwitchToWysiwygWithoutSourceChanges()
304    {
305  1 setContent("<em>alice</em> and <strong>bob</strong>");
306  1 select("document.body.firstChild.firstChild", 3, "document.body.lastChild.firstChild", 1);
307    // Switch to source and wait for the conversion result.
308  1 switchToSource();
309  1 assertSourceText("//alice// and **bob**");
310    // Switch back to WYSIWYG tab without modifying the source text.
311  1 switchToWysiwyg();
312    // Test is the content and the selection were preserved.
313  1 pushToolBarButton("Clear Formatting");
314  1 assertContent("<em>ali</em>ce and b<strong>ob</strong>");
315    }
316   
317    /**
318    * Switches to WYSIWYG tab and waits for the conversion. The caret should be placed at the start.
319    */
 
320  1 toggle @Test
321    public void testSwitchToWysiwygWithSourceChangesAndWait()
322    {
323  1 switchToSource();
324  1 setSourceText("**X**Wiki");
325  1 switchToWysiwyg();
326  1 typeText("+");
327  1 assertContent("<p><strong>+X</strong>Wiki</p>");
328    }
329   
330    /**
331    * Switches to WYSIWYG tab but doesn't wait for the rich text area to finish loading. Comes back to WYSIWYG tab when
332    * the rich text area is loaded.
333    */
 
334  1 toggle @Test
335    public void testSwitchToWysiwygWithHTMLChangesAndDontWait()
336    {
337  1 switchToSource();
338  1 String sourceText = "before {{code language=\"java\"}}private static final long x = 1L;{{/code}}";
339  1 setSourceText(sourceText);
340    // Switch to WYSIWYG tab but don't wait for the rich text area to load.
341  1 switchToWysiwyg(false);
342    // Switch back to source tab and wait there for the rich text area to load.
343  1 switchToSource();
344  1 getDriver().waitUntilElementDisappears(By.xpath("//div[@class = 'xRichTextEditor']//div[@class = 'loading']"));
345    // Switch again to WYSIWYG tab to check the result.
346  1 switchToWysiwyg();
347  1 typeText("#");
348  1 switchToSource();
349  1 assertSourceText("#" + sourceText);
350    }
351   
352    /**
353    * Tests if the switch to WYSIWYG tab action can be canceled.
354    */
 
355  1 toggle @Test
356    public void testCancelSwitchToWysiwyg()
357    {
358    // Switch to source tab and insert some content that takes time to render. A code macro is perfect for this.
359  1 switchToSource();
360  1 StringBuilder sourceText = new StringBuilder();
361  1 sourceText.append("{{code language=\"java\"}}\n");
362  1 sourceText.append("public final class Apple extends Fruit {\n");
363  1 sourceText.append(" public String getColor() {\n");
364  1 sourceText.append(" return \"red\";\n");
365  1 sourceText.append(" }\n");
366  1 sourceText.append("}\n");
367  1 sourceText.append("{{/code}}");
368  1 setSourceText(sourceText.toString());
369    // Place the caret before "Apple".
370  1 getSourceTextArea().sendKeys(Keys.HOME, Keys.PAGE_UP, Keys.ARROW_DOWN,
371    Keys.chord(Keys.CONTROL, Keys.ARROW_RIGHT, Keys.ARROW_RIGHT, Keys.ARROW_RIGHT), Keys.ARROW_RIGHT);
372    // Switch to rich text but don't wait till the rich text area finishes loading.
373  1 switchToWysiwyg(false);
374    // Switch back to source before the rich text area is reloaded.
375  1 switchToSource();
376    // Change the content.
377  1 getSourceTextArea().sendKeys("X");
378    // Switch to WYSIWYG tab again, this time with a different source text. Wait for the rich text area to load.
379  1 switchToWysiwyg();
380    // Check the result.
381  1 switchToSource();
382  1 getSourceTextArea().sendKeys("Y");
383  1 assertSourceText(sourceText.substring(0, 44) + "XY" + sourceText.substring(44));
384    }
385   
386    /**
387    * Switches to source tab, changes the source text then switches back to WYSIWYG tab and undoes the change.
388    */
 
389  1 toggle @Test
390    public void testUndoSourceChange()
391    {
392  1 typeText("1");
393  1 applyStyleTitle1();
394  1 switchToSource();
395    // Change the source text.
396  1 getSourceTextArea().sendKeys(Keys.ARROW_RIGHT, Keys.ARROW_RIGHT, Keys.ARROW_RIGHT, "2");
397    // Switch to WYSIWYG tab and undo the change.
398  1 switchToWysiwyg();
399    // The tool bar is not updated right away. We have to wait for the undo push button to become enabled.
400  1 waitForPushButton(TOOLBAR_BUTTON_UNDO_TITLE, true);
401  1 clickUndoButton();
402    // Check the result.
403  1 switchToSource();
404    // NOTE: This is not the right result since the heading style was removed also. This needs to be fixed.
405  1 assertSourceText("1");
406    }
407    }