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

File AlignmentTest.java

 

Code metrics

0
172
15
1
411
240
15
0.09
11.47
15
1

Classes

Class Line # Actions
AlignmentTest 30 172 0% 15 0
1.0100%
 

Contributing tests

This file is covered by 7 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.xwiki.test.wysiwyg.framework.AbstractWysiwygTestCase;
24   
25    /**
26    * Functional tests for alignment support inside the WYSIWYG editor.
27    *
28    * @version $Id: 6936cf9ba48a2fd4a04e8cf6444fee36dc2871aa $
29    */
 
30    public class AlignmentTest extends AbstractWysiwygTestCase
31    {
32    /**
33    * The title of the tool bar button used to align text to the left.
34    */
35    public static final String TOOLBAR_BUTTON_ALIGN_LEFT_TITLE = "Align Left";
36   
37    /**
38    * The title of the tool bar button used to center the text.
39    */
40    public static final String TOOLBAR_BUTTON_ALIGN_CENTER_TITLE = "Centered";
41   
42    /**
43    * The title of the tool bar button used to align text to the right.
44    */
45    public static final String TOOLBAR_BUTTON_ALIGN_RIGHT_TITLE = "Align Right";
46   
47    /**
48    * The title of the tool bar button used to justify text.
49    */
50    public static final String TOOLBAR_BUTTON_ALIGN_FULL_TITLE = "Justified";
51   
52    /**
53    * Tests if the text directly under body can be aligned.
54    */
 
55  1 toggle @Test
56    public void testAlignBody()
57    {
58  1 clickAlignCenterButton();
59  1 typeText("a");
60  1 switchToSource();
61  1 assertSourceText("(% style=\"text-align: center;\" %)\na");
62   
63  1 setSourceText("");
64  1 switchToWysiwyg();
65   
66  1 typeText("a");
67    // Wait for the tool bar to be updated, otherwise clicking on the toggle button has no effect.
68  1 waitForToggleButton(TOOLBAR_BUTTON_ALIGN_RIGHT_TITLE, true);
69  1 clickAlignRightButton();
70  1 typeText("b");
71  1 switchToSource();
72  1 assertSourceText("(% style=\"text-align: right;\" %)\nab");
73   
74  1 setSourceText("");
75  1 switchToWysiwyg();
76   
77  1 typeText("abc");
78  1 select("document.body.firstChild", 1, "document.body.firstChild", 2);
79    // Wait for the tool bar to be updated, otherwise clicking on the toggle button has no effect.
80  1 waitForToggleButton(TOOLBAR_BUTTON_ALIGN_FULL_TITLE, true);
81  1 clickAlignFullButton();
82  1 typeText("x");
83  1 switchToSource();
84  1 assertSourceText("(% style=\"text-align: justify;\" %)\naxc");
85   
86  1 setSourceText("");
87  1 switchToWysiwyg();
88   
89  1 typeText("a");
90  1 typeShiftEnter();
91  1 typeText("b");
92  1 selectNode("document.body.childNodes[2]");
93    // Wait for the tool bar to be updated, otherwise clicking on the toggle button has no effect.
94  1 waitForToggleButton(TOOLBAR_BUTTON_ALIGN_LEFT_TITLE, true);
95  1 clickAlignLeftButton();
96  1 typeText("x");
97  1 switchToSource();
98  1 assertSourceText("(% style=\"text-align: left;\" %)\na\nx");
99    }
100   
101    /**
102    * Tests if a single paragraph can be aligned.
103    */
 
104  1 toggle @Test
105    public void testAlignParagraph()
106    {
107    // Create the paragraph.
108  1 applyStyleTitle1();
109  1 applyStylePlainText();
110   
111    // Wait for the tool bar to be updated.
112  1 waitForPushButton(TOOLBAR_BUTTON_UNDO_TITLE, true);
113    // Check the default alignment.
114  1 boolean defaultAlignFull = isToggleButtonDown(TOOLBAR_BUTTON_ALIGN_FULL_TITLE);
115   
116    // Center text.
117  1 clickAlignCenterButton();
118  1 typeText("a");
119  1 waitForAlignCenterDetected(true);
120  1 switchToSource();
121  1 assertSourceText("(% style=\"text-align: center;\" %)\na");
122   
123    // Assert again the center alignment after coming back to WYSIWYG editor.
124  1 switchToWysiwyg();
125  1 waitForAlignCenterDetected(true);
126   
127  1 typeShiftEnter();
128  1 typeText("b");
129  1 clickAlignRightButton();
130  1 waitForAlignRightDetected(true);
131  1 switchToSource();
132  1 assertSourceText("(% style=\"text-align: right;\" %)\na\nb");
133   
134    // Assert again the right alignment after coming back to WYSIWYG editor.
135  1 switchToWysiwyg();
136  1 waitForAlignRightDetected(true);
137   
138  1 selectNode("document.body.firstChild.childNodes[2]");
139  1 clickAlignFullButton();
140  1 typeText("c");
141  1 waitForAlignFullDetected(true);
142  1 switchToSource();
143  1 assertSourceText("(% style=\"text-align: justify;\" %)\na\nc");
144   
145    // Assert again the full alignment after coming back to WYSIWYG editor.
146  1 switchToWysiwyg();
147  1 waitForAlignFullDetected(true);
148   
149    // Remove the full alignment (toggle full alignment off).
150  1 clickAlignFullButton();
151    // If paragraphs are justified by default then the "Justified" button remains toggled.
152  1 waitForAlignFullDetected(defaultAlignFull);
153  1 switchToSource();
154  1 assertSourceText("a\nc");
155  1 switchToWysiwyg();
156   
157  1 typeText("x");
158  1 clickAlignLeftButton();
159  1 waitForAlignLeftDetected(true);
160  1 switchToSource();
161  1 assertSourceText("(% style=\"text-align: left;\" %)\na\ncx");
162   
163    // Assert again the left alignment after coming back to WYSIWYG editor.
164  1 switchToWysiwyg();
165  1 waitForAlignLeftDetected(true);
166    }
167   
168    /**
169    * Tests if a table cell can be aligned.
170    */
 
171  1 toggle @Test
172    public void testAlignTableCell()
173    {
174  1 switchToSource();
175  1 setSourceText("|=a|=b\n|c|d");
176  1 switchToWysiwyg();
177  1 clickAlignRightButton();
178  1 waitForAlignRightDetected(true);
179  1 switchToSource();
180  1 assertSourceText("|=(% style=\"text-align: right;\" %)a|=b\n|c|d");
181   
182    // Assert again the right alignment after coming back to WYSIWYG editor.
183  1 switchToWysiwyg();
184  1 waitForAlignRightDetected(true);
185   
186  1 typeTextThenEnter("x");
187  1 clickAlignFullButton();
188  1 waitForAlignFullDetected(true);
189  1 switchToSource();
190  1 assertSourceText("|=(% style=\"text-align: justify;\" %)x\na|=b\n|c|d");
191  1 switchToWysiwyg();
192   
193  1 selectNodeContents("document.body.getElementsByTagName('td')[0]");
194  1 clickAlignCenterButton();
195  1 waitForAlignCenterDetected(true);
196  1 switchToSource();
197  1 assertSourceText("|=(% style=\"text-align: justify;\" %)x\na|=b\n|(% style=\"text-align: center;\" %)c|d");
198  1 switchToWysiwyg();
199   
200  1 selectNodeContents("document.body.getElementsByTagName('td')[0]");
201  1 waitForAlignCenterDetected(true);
202    }
203   
204    /**
205    * Tests if more paragraphs can be aligned at once.
206    */
 
207  1 toggle @Test
208    public void testAlignParagraphs()
209    {
210  1 switchToSource();
211  1 setSourceText("ab\n\ncd");
212  1 switchToWysiwyg();
213   
214  1 moveCaret("document.body.getElementsByTagName('p')[0].firstChild", 1);
215  1 clickAlignCenterButton();
216  1 waitForAlignCenterDetected(true);
217   
218  1 select("document.body.getElementsByTagName('p')[0].firstChild", 1,
219    "document.body.getElementsByTagName('p')[1].firstChild", 1);
220  1 waitForAlignCenterDetected(false);
221  1 clickAlignRightButton();
222  1 waitForAlignRightDetected(true);
223  1 switchToSource();
224  1 assertSourceText("(% style=\"text-align: right;\" %)\nab\n\n(% style=\"text-align: right;\" %)\ncd");
225   
226    // Assert again the right alignment after coming back to WYSIWYG editor.
227  1 switchToWysiwyg();
228  1 waitForAlignRightDetected(true);
229   
230    // Remove the right alignment (toggle off the 'Align right' button).
231  1 select("document.body.getElementsByTagName('p')[0].firstChild", 1,
232    "document.body.getElementsByTagName('p')[1].firstChild", 1);
233  1 clickAlignRightButton();
234  1 waitForAlignRightDetected(false);
235  1 switchToSource();
236  1 assertSourceText("ab\n\ncd");
237    }
238   
239    /**
240    * Tests if more table cells can be aligned at once.
241    */
 
242  1 toggle @Test
243    public void testAlignTableCells()
244    {
245  1 switchToSource();
246  1 setSourceText("|ab|cd");
247  1 switchToWysiwyg();
248   
249  1 moveCaret("document.body.getElementsByTagName('td')[1].firstChild", 2);
250  1 clickAlignRightButton();
251  1 waitForAlignRightDetected(true);
252   
253  1 select("document.body.getElementsByTagName('td')[0].firstChild", 0,
254    "document.body.getElementsByTagName('td')[1].firstChild", 1);
255  1 waitForAlignRightDetected(false);
256  1 clickAlignCenterButton();
257  1 waitForAlignCenterDetected(true);
258  1 switchToSource();
259  1 assertSourceText("|(% style=\"text-align: center;\" %)ab|(% style=\"text-align: center;\" %)cd");
260   
261    // Assert again the center alignment after coming back to WYSIWYG editor.
262  1 switchToWysiwyg();
263  1 waitForAlignCenterDetected(true);
264   
265    // Remove the center alignment (toggle off the 'Align center' button).
266  1 select("document.body.getElementsByTagName('td')[0].firstChild", 1,
267    "document.body.getElementsByTagName('td')[1].firstChild", 2);
268  1 clickAlignCenterButton();
269  1 waitForAlignCenterDetected(false);
270  1 switchToSource();
271  1 assertSourceText("|ab|cd");
272    }
273   
274    /**
275    * Makes a selection that includes a paragraph and a table cell and aligns them.
276    */
 
277  1 toggle @Test
278    public void testSelectAndAlignParagraphAndTableCell()
279    {
280  1 switchToSource();
281  1 setSourceText("ab\nxy\n\n|cd\n12|ef");
282  1 switchToWysiwyg();
283   
284    // Align the paragraph to the right.
285  1 clickAlignRightButton();
286  1 waitForAlignRightDetected(true);
287   
288    // Select the paragraph and the first table cell and align them full.
289  1 select("document.body.getElementsByTagName('p')[0].lastChild", 2,
290    "document.body.getElementsByTagName('td')[0].firstChild", 0);
291  1 waitForAlignRightDetected(false);
292  1 clickAlignFullButton();
293  1 waitForAlignFullDetected(true);
294  1 switchToSource();
295  1 assertSourceText("(% style=\"text-align: justify;\" %)\nab\nxy\n\n|(% style=\"text-align: justify;\" %)cd\n12|ef");
296   
297    // Assert again the full alignment after coming back to WYSIWYG editor.
298  1 switchToWysiwyg();
299  1 waitForAlignFullDetected(true);
300   
301    // Remove the full alignment (toggle off the 'Align full' button).
302  1 select("document.body.getElementsByTagName('p')[0].firstChild", 1,
303    "document.body.getElementsByTagName('td')[0].lastChild", 0);
304  1 clickAlignFullButton();
305  1 waitForAlignFullDetected(false);
306  1 switchToSource();
307  1 assertSourceText("ab\nxy\n\n|cd\n12|ef");
308    }
309   
310    /**
311    * Tests if a paragraph inside a table cell can be aligned.
312    */
 
313  1 toggle @Test
314    public void testAlignParagraphInsideTableCell()
315    {
316  1 switchToSource();
317  1 setSourceText("|(((ab\n\ncd)))|ef");
318  1 switchToWysiwyg();
319   
320    // Place the caret inside the first paragraph from the first table cell.
321  1 moveCaret("document.body.getElementsByTagName('p')[0].firstChild", 2);
322  1 clickAlignRightButton();
323  1 waitForAlignRightDetected(true);
324  1 switchToSource();
325  1 assertSourceText("|(((\n(% style=\"text-align: right;\" %)\nab\n\ncd\n)))|ef");
326   
327    // Assert again the right alignment after coming back to WYSIWYG editor.
328  1 switchToWysiwyg();
329  1 waitForAlignRightDetected(true);
330    }
331   
332    /**
333    * Clicks the 'Align left' button from the tool bar.
334    */
 
335  2 toggle protected void clickAlignLeftButton()
336    {
337  2 pushToolBarButton(TOOLBAR_BUTTON_ALIGN_LEFT_TITLE);
338    }
339   
340    /**
341    * Clicks the 'Align center' button from the tool bar.
342    */
 
343  6 toggle protected void clickAlignCenterButton()
344    {
345  6 pushToolBarButton(TOOLBAR_BUTTON_ALIGN_CENTER_TITLE);
346    }
347   
348    /**
349    * Clicks the 'Align right' button from the tool bar.
350    */
 
351  8 toggle protected void clickAlignRightButton()
352    {
353  8 pushToolBarButton(TOOLBAR_BUTTON_ALIGN_RIGHT_TITLE);
354    }
355   
356    /**
357    * Clicks the 'Align full' button from the tool bar.
358    */
 
359  6 toggle protected void clickAlignFullButton()
360    {
361  6 pushToolBarButton(TOOLBAR_BUTTON_ALIGN_FULL_TITLE);
362    }
363   
364    /**
365    * Waits for the left alignment toggle button to have the specified state.
366    *
367    * @param detected {@code true} to wait till the left alignment is detected, {@code false} to wait till it is
368    * undetected
369    */
 
370  2 toggle protected void waitForAlignLeftDetected(boolean detected)
371    {
372  2 triggerToolbarUpdate();
373  2 waitForToggleButtonState(TOOLBAR_BUTTON_ALIGN_LEFT_TITLE, detected);
374    }
375   
376    /**
377    * Waits for the centered alignment toggle button to have the specified state.
378    *
379    * @param detected {@code true} to wait till the centered alignment is detected, {@code false} to wait till it is
380    * undetected
381    */
 
382  9 toggle protected void waitForAlignCenterDetected(boolean detected)
383    {
384  9 triggerToolbarUpdate();
385  9 waitForToggleButtonState(TOOLBAR_BUTTON_ALIGN_CENTER_TITLE, detected);
386    }
387   
388    /**
389    * Waits for the right alignment toggle button to have the specified state.
390    *
391    * @param detected {@code true} to wait till the right alignment is detected, {@code false} to wait till it is
392    * undetected
393    */
 
394  13 toggle protected void waitForAlignRightDetected(boolean detected)
395    {
396  13 triggerToolbarUpdate();
397  13 waitForToggleButtonState(TOOLBAR_BUTTON_ALIGN_RIGHT_TITLE, detected);
398    }
399   
400    /**
401    * Waits for the justified alignment toggle button to have the specified state.
402    *
403    * @param detected {@code true} to wait till the justified alignment is detected, {@code false} to wait till it is
404    * undetected
405    */
 
406  7 toggle protected void waitForAlignFullDetected(boolean detected)
407    {
408  7 triggerToolbarUpdate();
409  7 waitForToggleButtonState(TOOLBAR_BUTTON_ALIGN_FULL_TITLE, detected);
410    }
411    }