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

File NativeJavaScriptApiTest.java

 

Code metrics

0
157
11
1
328
213
11
0.07
14.27
11
1

Classes

Class Line # Actions
NativeJavaScriptApiTest 32 157 0% 11 0
1.0100%
 

Contributing tests

This file is covered by 9 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    import static org.junit.Assert.*;
26   
27    /**
28    * Functional tests for the native JavaScript API exposed by the WYSIWYG editor.
29    *
30    * @version $Id: 2465c20e750d519200888b5efc127f18e8679769 $
31    */
 
32    public class NativeJavaScriptApiTest extends AbstractWysiwygTestCase
33    {
34    /**
35    * Functional tests for:
36    * <ul>
37    * <li>WysiwygEditor#getPlainTextArea()</li>
38    * <li>WysiwygEditor#getRichTextArea()</li>
39    * </ul>
40    * .
41    */
 
42  1 toggle @Test
43    public void testTextAreaElementsGetters()
44    {
45  1 insertEditor("editor", "displayTabs: true");
46   
47    // Test plain text editor.
48  1 assertEquals("xPlainTextEditor", getSelenium().getEval("window.editor.getPlainTextArea().className"));
49  1 assertEquals("textarea", getSelenium().getEval("window.editor.getPlainTextArea().nodeName").toLowerCase());
50   
51    // Test rich text editor.
52  1 assertEquals("gwt-RichTextArea", getSelenium().getEval("window.editor.getRichTextArea().className"));
53  1 assertEquals("iframe", getSelenium().getEval("window.editor.getRichTextArea().nodeName").toLowerCase());
54    }
55   
56    /**
57    * Functional test for {@code WysiwygEditor#getSourceText()} when the rich text area is enabled. The rich text area
58    * is disable when the source tab is active.
59    */
 
60  1 toggle @Test
61    public void testGetSourceTextWhenRichTextAreaIsEnabled()
62    {
63  1 insertEditor("editor", "syntax: 'xwiki/2.0'");
64  1 setContent("<em>xwiki</em> is the <strong>best</strong>!<br/>");
65  1 assertEquals("//xwiki// is the **best**!", getSourceText("editor"));
66    }
67   
68    /**
69    * Functional test for {@code WysiwygEditor#getSourceText()} when the rich text area is disabled. The rich text area
70    * is disable when the source tab is active.
71    */
 
72  1 toggle @Test
73    public void testGetSourceTextWhenRichTextAreaIsDisabled()
74    {
75  1 insertEditor("editor", "syntax: 'xwiki/2.0',\ndisplayTabs: true,\ndefaultEditor: 'wysiwyg'");
76  1 setContent("<h1>Veni, <em>vidi</em>, vici<br/></h1>");
77  1 switchToSource();
78  1 assertEquals("= Veni, //vidi//, vici =", getSourceText("editor"));
79   
80    // Type something in the plain text area and see if we get it.
81  1 getSourceTextArea().sendKeys("x");
82  1 assertEquals("x= Veni, //vidi//, vici =", getSourceText("editor"));
83    }
84   
85    /**
86    * Functional test for {@code WysiwygEditor#release()}.
87    */
 
88  1 toggle @Test
89    public void testRelease()
90    {
91  1 switchToSource();
92  1 StringBuffer content = new StringBuffer();
93  1 content.append("{{velocity}}\n");
94  1 content.append("{{html}}\n");
95  1 content.append("#wysiwyg_import(true)\n");
96  1 content.append("<div id=\"wrapper\"></div>\n");
97  1 content.append("<div><button onclick=\"loadEditor();\">Load Editor</button></div>\n");
98  1 content.append("<script type=\"text/javascript\">\n");
99  1 content.append("function loadEditor() {\n");
100  1 content.append(" if (window.editor) {\n");
101  1 content.append(" editor.release();\n");
102  1 content.append(" delete window.editor;\n");
103  1 content.append(" }\n");
104  1 content.append(" Wysiwyg.onModuleLoad(function() {\n");
105  1 content.append(" document.getElementById('wrapper').innerHTML = '<textarea id=\"test\"></textarea>';\n");
106  1 content.append(" editor = new WysiwygEditor({hookId: 'test', syntax: 'xwiki/2.0'});\n");
107  1 content.append(" });\n");
108  1 content.append("}\n");
109  1 content.append("</script>\n");
110  1 content.append("{{/html}}\n");
111  1 content.append("{{/velocity}}");
112  1 setSourceText(content.toString());
113  1 clickEditSaveAndView();
114   
115  1 clickButtonWithText("Load Editor");
116  1 waitForCondition("typeof window.editor == 'object'");
117  1 waitForEditorToLoad();
118  1 focusRichTextArea();
119   
120  1 typeText("x");
121  1 assertEquals("x", getSourceText("editor"));
122   
123    // "y" (lower case only) is misinterpreted.
124    // See http://jira.openqa.org/browse/SIDE-309
125    // See http://jira.openqa.org/browse/SRC-385
126  1 typeText("Y");
127  1 clickButtonWithText("Load Editor");
128  1 waitForCondition("typeof window.editor == 'object'");
129  1 waitForEditorToLoad();
130  1 focusRichTextArea();
131   
132  1 typeText("z");
133  1 applyStyleTitle1();
134  1 assertEquals("= z =", getSourceText("editor"));
135    }
136   
137    /**
138    * @see XWIKI-4067: Trying to edit a missing object property with the new WYSIWYG editor can lead to infinite
139    * include recursion.
140    */
 
141  1 toggle @Test
142    public void testEditMissingProperty()
143    {
144    // Save the current location to be able to get back.
145  1 String location = getSelenium().getLocation();
146   
147    // Create a new page and set its content. This page should have a XWiki.TagClass object attached.
148  1 open("Test", "Alice", "edit", "editor=wiki");
149    // We have to set the content of the page in order to detect if the value of the missing property is empty.
150  1 setFieldValue("content", "I want ice cream");
151  1 clickEditSaveAndView();
152   
153    // Go back to the previous location.
154  1 open(location);
155  1 waitForEditorToLoad();
156  1 switchToSource();
157    // Load the WYSIWYG editor for a property that doesn't exist.
158  1 StringBuffer content = new StringBuffer();
159  1 content.append("{{velocity}}\n");
160  1 content.append("{{html}}\n");
161    // xyz is not a property of the XWiki.TagClass.
162  1 content.append("<textarea id=\"XWiki.TagClass_0_xyz\"></textarea>\n");
163  1 content.append("#wysiwyg_editProperty($xwiki.getDocument('Test.Alice') 'XWiki.TagClass_0_xyz' false)\n");
164  1 content.append("<script type=\"text/javascript\">\n");
165  1 content.append("document.observe('xwiki:wysiwyg:created', function(event) {\n");
166  1 content.append(" window.editor = event.memo.instance;\n");
167  1 content.append("});\n");
168  1 content.append("</script>\n");
169  1 content.append("{{/html}}\n");
170  1 content.append("{{/velocity}}");
171  1 setSourceText(content.toString());
172  1 clickEditSaveAndView();
173   
174  1 waitForCondition("typeof window.editor == 'object'");
175  1 waitForEditorToLoad();
176    // Check the WYSIWYG editor input value.
177  1 assertEquals("", getSourceText("editor"));
178    }
179   
180    /**
181    * @see XWIKI-4519: Add the ability to execute commands on the rich text area from JavaScript.
182    */
 
183  1 toggle @Test
184    public void testCommandManagerApi()
185    {
186  1 insertEditor("editor", "syntax: 'xwiki/2.0'");
187  1 focusRichTextArea();
188  1 typeText("x");
189  1 selectNodeContents("document.body.firstChild");
190  1 assertTrue(Boolean.valueOf(getSelenium().getEval("window.editor.getCommandManager().isSupported('bold')")));
191  1 assertTrue(Boolean.valueOf(getSelenium().getEval("window.editor.getCommandManager().isEnabled('bold')")));
192  1 assertFalse(Boolean.valueOf(getSelenium().getEval("window.editor.getCommandManager().isExecuted('bold')")));
193  1 assertTrue(Boolean.valueOf(getSelenium().getEval("window.editor.getCommandManager().execute('bold')")));
194  1 assertTrue(Boolean.valueOf(getSelenium().getEval("window.editor.getCommandManager().isExecuted('bold')")));
195  1 assertEquals("**x**", getSourceText("editor"));
196    }
197   
198    /**
199    * Tests the WYSIWYG events are fired properly.
200    */
 
201  1 toggle @Test
202    public void testEvents()
203    {
204    // Insert the code that creates the editor.
205  1 switchToSource();
206  1 StringBuffer content = new StringBuffer();
207  1 content.append("{{velocity}}\n");
208  1 content.append("{{html}}\n");
209  1 content.append("#wysiwyg_import(false)\n");
210  1 content.append("<div id=\"log\"></div>\n");
211  1 content.append("<textarea id=\"source\"></textarea>\n");
212  1 content.append("<script type=\"text/javascript\">\n");
213  1 content.append("['created', 'loaded', 'showingSource', 'showSource',"
214    + " 'showingWysiwyg', 'showWysiwyg'].each(function(actionName) {\n");
215  1 content.append(" document.observe('xwiki:wysiwyg:' + actionName, function(event) {\n");
216  1 content.append(" if (!window.editor || event.memo.instance == window.editor) {\n");
217  1 content.append(" $('log').appendChild(document.createTextNode(event.eventName + ' '));\n");
218  1 content.append(" }\n");
219  1 content.append(" });\n");
220  1 content.append("});\n");
221  1 content.append("document.observe('xwiki:dom:loaded', function() {\n");
222  1 content.append(" Wysiwyg.onModuleLoad(function() {\n");
223  1 content.append(" editor = new WysiwygEditor({\n");
224  1 content.append(" hookId: 'source',\n");
225  1 content.append(" syntax: 'xwiki/2.0',\n");
226  1 String inputURL =
227    getUrl(this.getClass().getSimpleName(), "_" + getTestMethodName(), "edit", "xpage=wysiwyginput");
228  1 content.append(" inputURL: '" + inputURL + "',\n");
229  1 content.append(" displayTabs: true,\n");
230  1 content.append(" defaultEditor: 'wysiwyg'\n");
231  1 content.append(" });\n");
232  1 content.append(" });\n");
233  1 content.append("});\n");
234  1 content.append("</script>\n");
235  1 content.append("{{/html}}\n");
236  1 content.append("{{/velocity}}");
237  1 setSourceText(content.toString());
238  1 clickEditSaveAndView();
239   
240    // Wait for the editor to be created.
241  1 waitForCondition("typeof window.editor == 'object'");
242  1 waitForEditorToLoad();
243  1 focusRichTextArea();
244   
245    // Switch to source tab and back.
246  1 typeText("1");
247  1 switchToSource();
248  1 setSourceText("2");
249  1 switchToWysiwyg();
250   
251    // Check the log.
252  1 assertEquals("xwiki:wysiwyg:created xwiki:wysiwyg:loaded xwiki:wysiwyg:showWysiwyg xwiki:wysiwyg:showingSource"
253    + " xwiki:wysiwyg:showSource xwiki:wysiwyg:showingWysiwyg xwiki:wysiwyg:showWysiwyg ", getSelenium()
254    .getEval("window.document.getElementById('log').innerHTML"));
255    }
256   
257    /**
258    * Tests if the configuration parameters for a WYSIWYG editor instance are accessible through the JavaScript API.
259    */
 
260  1 toggle @Test
261    public void testAccessConfigurationParameters()
262    {
263  1 String hookId = "XWiki.ArticleClass_0_description";
264  1 insertEditor(hookId, "syntax: 'xwiki/2.0',\nxyz: 'abc'");
265  1 assertEquals("abc", getSelenium().getEval("window['" + hookId + "'].getParameter('xyz')"));
266  1 assertEquals("3", getSelenium().getEval("window['" + hookId + "'].getParameterNames().length"));
267    }
268   
269    /**
270    * Tests that we can get a reference to a WYSIWYG editor instance by knowing its hookId.
271    */
 
272  1 toggle @Test
273    public void testAccessWysiwygEditorInstanceByHookId()
274    {
275  1 String hookId = "XWiki.ArticleClass_0_description";
276  1 insertEditor(hookId, "syntax: 'xwiki/2.0'");
277  1 assertEquals(hookId,
278    getSelenium().getEval("window.Wysiwyg.getInstance('" + hookId + "').getParameter('hookId')"));
279    }
280   
281    /**
282    * Inserts a WYSIWYG editor into the current page.
283    *
284    * @param name the name of JavaScript variable to be used for accessing the editor
285    * @param config additional configuration parameters to give to the created editor
286    */
 
287  6 toggle protected void insertEditor(String name, String config)
288    {
289    // Insert the code that creates the editor.
290  6 switchToSource();
291  6 StringBuilder content = new StringBuilder();
292  6 content.append("{{velocity}}\n");
293  6 content.append("{{html}}\n");
294  6 content.append("#wysiwyg_import(false)\n");
295  6 content.append("<textarea id=\"" + name + "\"></textarea>\n");
296  6 content.append("<script type=\"text/javascript\">\n");
297  6 content.append("document.observe('xwiki:dom:loaded', function() {\n");
298  6 content.append(" Wysiwyg.onModuleLoad(function() {\n");
299  6 content.append(" window['" + name + "'] = new WysiwygEditor({\n");
300  6 content.append(" hookId: '" + name + "',\n");
301  6 content.append(" " + config + "\n");
302  6 content.append(" });\n");
303  6 content.append(" });\n");
304  6 content.append("});\n");
305  6 content.append("</script>\n");
306  6 content.append("{{/html}}\n");
307  6 content.append("{{/velocity}}");
308  6 setSourceText(content.toString());
309  6 clickEditSaveAndView();
310   
311    // Wait for the editor to be created.
312  6 waitForCondition("typeof window['" + name + "'] == 'object'");
313  6 waitForEditorToLoad();
314    }
315   
316    /**
317    * @param editorName the name of a JavaScript variable holding a reference to a WysiwygEditor instance
318    * @return the source text of the specified editor
319    */
 
320  7 toggle protected String getSourceText(String editorName)
321    {
322  7 getSelenium().getEval(
323    "delete window.sourceText;\nwindow['" + editorName
324    + "'].getSourceText(function(result){window.sourceText = result;})");
325  7 waitForCondition("typeof window.sourceText == 'string'");
326  7 return getSelenium().getEval("window.sourceText");
327    }
328    }