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

File ColorTest.java

 

Code metrics

2
64
11
1
234
112
12
0.19
5.82
11
1.09

Classes

Class Line # Actions
ColorTest 30 64 0% 12 1
0.98701398.7%
 

Contributing tests

This file is covered by 6 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 color support inside the WYSIWYG editor.
27    *
28    * @version $Id: b216e59c31587dcd0c17a4eb2ce1fa0011cb4fcf $
29    */
 
30    public class ColorTest extends AbstractWysiwygTestCase
31    {
32    /**
33    * Tests if the text color can be changed.
34    */
 
35  1 toggle @Test
36    public void testChangeTextColor()
37    {
38  1 typeText("abc");
39   
40    // Select 'b'.
41  1 select("document.body.firstChild", 1, "document.body.firstChild", 2);
42   
43    // Change the text color to red.
44  1 clickForegroundColorButton();
45  1 selectColor("rgb(255, 0, 0)");
46   
47    // Check the XWiki syntax.
48  1 switchToSource();
49  1 assertSourceText("a(% style=\"color: rgb(255, 0, 0);\" %)b(%%)c");
50  1 switchToWysiwyg();
51   
52    // Place the caret after 'b' in order to check if the current color is detected.
53  1 moveCaret("document.body.getElementsByTagName('span')[0].firstChild", 1);
54   
55    // Check if the editor detects the right color.
56  1 clickForegroundColorButton();
57  1 assertSelectedColor("rgb(255, 0, 0)", true);
58  1 hideColorPicker();
59    }
60   
61    /**
62    * Tests if the background color can be changed.
63    */
 
64  1 toggle @Test
65    public void testChangeBackgroundColor()
66    {
67  1 typeText("abc");
68   
69    // Select 'b'.
70  1 select("document.body.firstChild", 1, "document.body.firstChild", 2);
71   
72    // Change the text color to red.
73  1 clickBackgroundColorButton();
74  1 selectColor("rgb(255, 0, 0)");
75   
76    // Check the XWiki syntax.
77  1 switchToSource();
78  1 assertSourceText("a(% style=\"background-color: rgb(255, 0, 0);\" %)b(%%)c");
79  1 switchToWysiwyg();
80   
81    // Place the caret after 'b' in order to check if the current color is detected.
82  1 moveCaret("document.body.getElementsByTagName('span')[0].firstChild", 1);
83   
84    // Check if the editor detects the right color.
85  1 clickBackgroundColorButton();
86  1 assertSelectedColor("rgb(255, 0, 0)", true);
87  1 hideColorPicker();
88    }
89   
90    /**
91    * Tests if both the text color and the background color can be changed on the current selection.
92    */
 
93  1 toggle @Test
94    public void testChangeTextAndBackgroudColor()
95    {
96  1 switchToSource();
97  1 setSourceText("(% style=\"color: red; background-color:#777;\" %)foo");
98  1 switchToWysiwyg();
99   
100    // Select the text.
101  1 selectNodeContents("document.body.firstChild");
102   
103    // Change the text color.
104  1 clickForegroundColorButton();
105  1 selectColor("rgb(0, 255, 0)");
106   
107    // Change the background color.
108  1 clickBackgroundColorButton();
109  1 selectColor("rgb(252, 229, 205)");
110   
111  1 switchToSource();
112  1 assertSourceText("(% style=\"color: rgb(0, 255, 0); background-color: rgb(252, 229, 205);\" %)foo");
113    }
114   
115    /**
116    * Makes a text bold, changes its color and then removes the bold style.
117    */
 
118  1 toggle @Test
119    public void testRemoveBoldStyleFromAColoredText()
120    {
121    // Type some text and make it bold.
122  1 typeText("bar");
123  1 selectAllContent();
124  1 clickBoldButton();
125   
126    // Change the text color.
127  1 clickForegroundColorButton();
128  1 selectColor("rgb(0, 0, 255)");
129   
130    // Remove the bold style.
131  1 clickBoldButton();
132   
133    // Check the XWiki syntax.
134  1 switchToSource();
135  1 assertSourceText("(% style=\"color: rgb(0, 0, 255);\" %)bar");
136    }
137   
138    /**
139    * Types two words in different colors, selects both and tries to change their color.
140    *
141    * @see XWIKI-3564: Cannot change the text color after selecting text with different colors in IE
142    */
 
143  1 toggle @Test
144    public void testChangeTextColorAfterSelectingTextWithDifferentColors()
145    {
146    // Type the two words.
147  1 typeText("a z");
148   
149    // Select the first word and change its color to red.
150  1 select("document.body.firstChild", 0, "document.body.firstChild", 1);
151  1 clickForegroundColorButton();
152  1 selectColor("rgb(255, 0, 0)");
153   
154    // Select the second word and change its color to blue.
155  1 select("document.body.childNodes[1]", 1, "document.body.childNodes[1]", 2);
156  1 clickForegroundColorButton();
157  1 selectColor("rgb(0, 0, 255)");
158   
159    // Select both words and change their color to green.
160  1 selectAllContent();
161  1 clickForegroundColorButton();
162  1 selectColor("rgb(0, 255, 0)");
163   
164    // Check the XWiki syntax.
165  1 switchToSource();
166  1 assertSourceText("(% style=\"color: rgb(0, 255, 0);\" %)a z");
167    }
168   
169    /**
170    * Background-color CSS property is not inheritable so in order to detect its value we must iterate over all
171    * ancestors of the current text selection.
172    */
 
173  1 toggle @Test
174    public void testDetectNestedBackgroundColor()
175    {
176  1 switchToSource();
177  1 setSourceText("12 (% style=\"background-color:red\" %)34 "
178    + "(% style=\"background-color:red;color:yellow\" %)56 **78** 90");
179  1 switchToWysiwyg();
180    // Place the caret between 7 and 8
181  1 moveCaret("document.body.getElementsByTagName('strong')[0].firstChild", 1);
182    // Check the detected background color.
183  1 clickBackgroundColorButton();
184  1 assertSelectedColor("rgb(255, 0, 0)", true);
185  1 hideColorPicker();
186    }
187   
188    /**
189    * Clicks on the tool bar button for changing the text color.
190    */
 
191  7 toggle protected void clickForegroundColorButton()
192    {
193  7 pushToolBarButton("Font Color");
194    }
195   
196    /**
197    * Clicks on the tool bar button for changing the text background color.
198    */
 
199  4 toggle protected void clickBackgroundColorButton()
200    {
201  4 pushToolBarButton("Highlight Color");
202    }
203   
204    /**
205    * Selects the specified color from the color picker.
206    *
207    * @param rgbColor the RGB color to select
208    */
 
209  8 toggle protected void selectColor(String rgbColor)
210    {
211  8 getSelenium().click("//div[@class = 'colorCell' and @style = 'background-color: " + rgbColor + ";']");
212    }
213   
214    /**
215    * Asserts that the color selected by the color picker equals the given color.
216    *
217    * @param rgbColor the expected selected color
218    * @param dark {@code true} if the specified color is dark, {@code false} if it is light
219    */
 
220  3 toggle protected void assertSelectedColor(String rgbColor, boolean dark)
221    {
222  3 assertElementPresent(String.format(
223  3 "//div[contains(@class, 'colorCell-selected-%s') and @style = 'background-color: %s;']", dark ? "dark"
224    : "light", rgbColor));
225    }
226   
227    /**
228    * Hides the color picker by clicking outside.
229    */
 
230  3 toggle protected void hideColorPicker()
231    {
232  3 getRichTextArea().click();
233    }
234    }