platofrm: set fsnotifier glibc compatibility only for i386/amd64
[idea/community.git] / platform / platform-tests / testSrc / com / intellij / openapi / editor / richcopy / SyntaxInfoConstructionTest.java
1 /*
2  * Copyright 2000-2014 JetBrains s.r.o.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.intellij.openapi.editor.richcopy;
17
18 import com.intellij.openapi.application.ApplicationManager;
19 import com.intellij.openapi.editor.Editor;
20 import com.intellij.openapi.editor.LogicalPosition;
21 import com.intellij.openapi.editor.SelectionModel;
22 import com.intellij.openapi.editor.ex.EditorEx;
23 import com.intellij.openapi.editor.impl.DocumentImpl;
24 import com.intellij.openapi.editor.richcopy.model.ColorRegistry;
25 import com.intellij.openapi.editor.richcopy.model.MarkupHandler;
26 import com.intellij.openapi.editor.richcopy.model.SyntaxInfo;
27 import com.intellij.openapi.util.text.StringUtil;
28 import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase;
29 import com.intellij.ui.JBColor;
30 import junit.framework.TestCase;
31
32 /**
33  * @author Denis Zhdanov
34  * @since 3/27/13 11:11 AM
35  */
36 public class SyntaxInfoConstructionTest extends LightPlatformCodeInsightFixtureTestCase {
37   public void testBlockSelection() {
38     String text =
39       "package org;\n" +
40       "\n" +
41       "public class TestClass {\n" +
42       "\n" +
43       "    int field;\n" +
44       "\n" +
45       "    public int getField() {\n" +
46       "        return field;\n" +
47       "    }\n" +
48       "}";
49     init(text);
50
51     int blockSelectionStartOffset = text.indexOf("public int");
52     Editor editor = myFixture.getEditor();
53     LogicalPosition blockSelectionStartPosition = editor.offsetToLogicalPosition(blockSelectionStartOffset);
54     LogicalPosition blockSelectionEndPosition = new LogicalPosition(
55       blockSelectionStartPosition.line + 2,
56       editor.offsetToLogicalPosition(text.indexOf('{', blockSelectionStartOffset)).column + 1);
57     editor.getSelectionModel().setBlockSelection(blockSelectionStartPosition, blockSelectionEndPosition);
58
59     verifySyntaxInfo("foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=public int \n" +
60                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=getField() {\n" +
61                      "text=\n" +
62                      "\n" +
63                      "text=    \n" +
64                      "foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=return \n" +
65                      "foreground=java.awt.Color[r=102,g=14,b=122],text=field\n" +
66                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=;\n" +
67                      "text=\n" +
68                      "\n" +
69                      "text=}\n");
70   }
71
72   public void testColumnModeBlockSelection() {
73     String text =
74       "package org;\n" +
75       "\n" +
76       "public class TestClass {\n" +
77       "\n" +
78       "    int field;\n" +
79       "\n" +
80       "    public int getField() {\n" +
81       "        return field;\n" +
82       "    }\n" +
83       "}";
84     init(text);
85
86     int blockSelectionStartOffset = text.indexOf("public int");
87     Editor editor = myFixture.getEditor();
88     ((EditorEx) editor).setColumnMode(true);
89     LogicalPosition blockSelectionStartPosition = editor.offsetToLogicalPosition(blockSelectionStartOffset);
90     LogicalPosition blockSelectionEndPosition = new LogicalPosition(
91       blockSelectionStartPosition.line + 2,
92       editor.offsetToLogicalPosition(text.indexOf('{', blockSelectionStartOffset)).column + 1);
93     editor.getSelectionModel().setBlockSelection(blockSelectionStartPosition, blockSelectionEndPosition);
94
95     verifySyntaxInfo("foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=public int \n" +
96                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=getField() {\n" +
97                      "text=\n" +
98                      "\n" +
99                      "text=    \n" +
100                      "foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=return \n" +
101                      "foreground=java.awt.Color[r=102,g=14,b=122],text=field\n" +
102                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=;\n" +
103                      "text=\n" +
104                      "\n" +
105                      "text=}\n");
106   }
107
108   public void testColumnModeBlockSelectionWithGaps() {
109     String text =
110       "public class TestClass {\n" +
111       "\n" +
112       "    int field;\n" +
113       "\n" +
114       "    int otherField;\n" +
115       "}";
116     init(text);
117
118     int blockSelectionStartOffset = text.indexOf("int");
119     Editor editor = myFixture.getEditor();
120     ((EditorEx) editor).setColumnMode(true);
121     LogicalPosition blockSelectionStartPosition = editor.offsetToLogicalPosition(blockSelectionStartOffset);
122     LogicalPosition blockSelectionEndPosition = new LogicalPosition(blockSelectionStartPosition.line + 2, blockSelectionStartPosition.column + 16);
123     editor.getSelectionModel().setBlockSelection(blockSelectionStartPosition, blockSelectionEndPosition);
124
125     verifySyntaxInfo("foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=int \n" +
126                      "foreground=java.awt.Color[r=102,g=14,b=122],text=field\n" +
127                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=;\n" +
128                      "text=\n" +
129                      "\n" +
130                      "text=\n" +
131                      "\n" +
132                      "foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=int \n" +
133                      "foreground=java.awt.Color[r=102,g=14,b=122],text=otherField\n" +
134                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=;\n");
135   }
136
137   public void testRegularSelection() {
138     // We want to exclude unnecessary indents from the pasted results.
139     String text =
140       "package org;\n" +
141       "\n" +
142       "public class TestClass {\n" +
143       "\n" +
144       "    int field;\n" +
145       "\n" +
146       "    public int getField() {\n" +
147       "        return field;\n" +
148       "    }\n" +
149       "}";
150     init(text);
151
152     int selectionStart = text.indexOf("public int");
153     int selectionEnd = text.indexOf('}', selectionStart) + 1;
154     SelectionModel selectionModel = myFixture.getEditor().getSelectionModel();
155     selectionModel.setSelection(selectionStart, selectionEnd);
156
157     String expected = "foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=public int \n" +
158                       "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=getField() {\n" +
159                       "\n" +
160                       "text=    \n" +
161                       "foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=return \n" +
162                       "foreground=java.awt.Color[r=102,g=14,b=122],text=field\n" +
163                       "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=;\n" +
164                       "\n" +
165                       "text=}\n";
166
167     TestCase.assertEquals(expected, getSyntaxInfo());
168     
169     selectionModel.setSelection(selectionStart - 2, selectionEnd);
170     TestCase.assertEquals(expected, getSyntaxInfo());
171
172     selectionModel.setSelection(selectionStart - 4, selectionEnd);
173     TestCase.assertEquals(expected, getSyntaxInfo());
174   }
175
176   public void testIncorrectFirstLineCalculationOffset() {
177     init("\"tr\" #> <selection>template.statusList.sortBy</selection>(_.index).map(fromStatus =>");
178
179     verifySyntaxInfo("fontStyle=0,text=template.statusList.sortBy\n");
180   }
181
182   public void testJavadoc() {
183     init(
184       "package org;\n" +
185       "\n" +
186       "import java.io.Serializable;\n" +
187       "\n" +
188       "<selection>/**\n" +
189       " * Code in <code>here</code>\n" +
190       " * <strong>Hi</strong> man\n" +
191       " * @param <T>\n" +
192       " </selection>*/\n" +
193       "public interface SampleTest<T> extends Serializable {\n" +
194       "    boolean isNotNull();\n" +
195       "    T getValue();\n" +
196       "}"
197     );
198     verifySyntaxInfo("foreground=java.awt.Color[r=128,g=128,b=128],fontStyle=2,text=/**\n" +
199                      "\n" +
200                      "text= * Code in \n" +
201                      "background=java.awt.Color[r=226,g=255,b=226],text=<code>\n" +
202                      "background=java.awt.Color[r=255,g=255,b=255],text=here\n" +
203                      "background=java.awt.Color[r=226,g=255,b=226],text=</code>\n" +
204                      "background=java.awt.Color[r=255,g=255,b=255],text=\n" +
205                      "\n" +
206                      "text= * \n" +
207                      "background=java.awt.Color[r=226,g=255,b=226],text=<strong>\n" +
208                      "background=java.awt.Color[r=255,g=255,b=255],text=Hi\n" +
209                      "background=java.awt.Color[r=226,g=255,b=226],text=</strong>\n" +
210                      "background=java.awt.Color[r=255,g=255,b=255],text= man\n" +
211                      "\n" +
212                      "text= * \n" +
213                      "fontStyle=3,text=@param \n" +
214                      "foreground=java.awt.Color[r=61,g=61,b=61],text=<T>\n" +
215                      "\n" +
216                      "text= \n");
217   }
218
219   public void testIndentStrippingWhenFirstLineIsMostIndented() throws Exception {
220     init("public class Test {\n" +
221          "<selection>  int field;\n" +
222          "}</selection>");
223     verifySyntaxInfo("text=  \n" +
224                      "foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=int \n" +
225                      "foreground=java.awt.Color[r=102,g=14,b=122],text=field\n" +
226                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=;\n" +
227                      "\n" +
228                      "text=}\n");
229   }
230
231   public void testIndentStrippingWhenSelectionEndIsBeforeNonWsCharactersOnTheLine() throws Exception {
232     init("public class Test {\n" +
233          "<selection>  int field;\n" +
234          "</selection>}");
235     verifySyntaxInfo("foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=int \n" +
236                      "foreground=java.awt.Color[r=102,g=14,b=122],text=field\n" +
237                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=;\n" +
238                      "\n");
239   }
240
241   public void testSlashRSeparator() throws Exception {
242     String text = "package org;\r" +
243                   "\r" +
244                   "public class TestClass {\r" +
245                   "\r" +
246                   "    int field;\r" +
247                   "\r" +
248                   "    public int getField() {\r" +
249                   "        return field;\r" +
250                   "    }\r" +
251                   "}";
252     initWithCustomLineSeparators(text);
253     int selectionStart = text.indexOf("public int");
254     int selectionEnd = text.indexOf('}', selectionStart);
255     myFixture.getEditor().getSelectionModel().setSelection(selectionStart, selectionEnd);
256
257     verifySyntaxInfo("foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=public int \n" +
258                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=getField() {\n" +
259                      "\n" +
260                      "text=    \n" +
261                      "foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=return \n" +
262                      "foreground=java.awt.Color[r=102,g=14,b=122],text=field\n" +
263                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=;\n" +
264                      "\n" +
265                      "text=}\n");
266   }
267
268   public void testSlashRSlashNSeparator() throws Exception {
269     String text = "package org;\r\n" +
270                   "\r\n" +
271                   "public class TestClass {\r\n" +
272                   "\r\n" +
273                   "    int field;\r\n" +
274                   "\r\n" +
275                   "    public int getField() {\r\n" +
276                   "        return field;\r\n" +
277                   "    }\r\n" +
278                   "}";
279     initWithCustomLineSeparators(text);
280     int selectionStart = text.indexOf("public int");
281     int selectionEnd = text.indexOf('}', selectionStart);
282     myFixture.getEditor().getSelectionModel().setSelection(selectionStart, selectionEnd);
283
284     verifySyntaxInfo("foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=public int \n" +
285                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=getField() {\n" +
286                      "\n" +
287                      "text=    \n" +
288                      "foreground=java.awt.Color[r=0,g=0,b=128],fontStyle=1,text=return \n" +
289                      "foreground=java.awt.Color[r=102,g=14,b=122],text=field\n" +
290                      "foreground=java.awt.Color[r=0,g=0,b=0],fontStyle=0,text=;\n" +
291                      "\n" +
292                      "text=}\n");
293   }
294
295   private String getSyntaxInfo() {
296     final StringBuilder builder = new StringBuilder();
297     final Editor editor = myFixture.getEditor();
298     String selectedText = editor.getSelectionModel().getSelectedText(true);
299     assertNotNull(selectedText);
300     final String text = StringUtil.convertLineSeparators(selectedText);
301
302     TextWithMarkupProcessor processor = new TextWithMarkupProcessor() {
303       @Override
304       void createResult(SyntaxInfo syntaxInfo, Editor editor) {
305         final ColorRegistry colorRegistry = syntaxInfo.getColorRegistry();
306         assertEquals(JBColor.BLACK, colorRegistry.dataById(syntaxInfo.getDefaultForeground()));
307         assertEquals(JBColor.WHITE, colorRegistry.dataById(syntaxInfo.getDefaultBackground()));
308         assertEquals((float)getFontSize(), syntaxInfo.getFontSize(), 0.01f);
309         syntaxInfo.processOutputInfo(new MarkupHandler() {
310           @Override
311           public void handleText(int startOffset, int endOffset) throws Exception {
312             builder.append("text=").append(text.substring(startOffset, endOffset)).append('\n');
313           }
314
315           @Override
316           public void handleForeground(int foregroundId) throws Exception {
317             builder.append("foreground=").append(colorRegistry.dataById(foregroundId)).append(',');
318           }
319
320           @Override
321           public void handleBackground(int backgroundId) throws Exception {
322             builder.append("background=").append(colorRegistry.dataById(backgroundId)).append(',');
323           }
324
325           @Override
326           public void handleFont(int fontNameId) throws Exception {
327             assertEquals(1, fontNameId);
328           }
329
330           @Override
331           public void handleStyle(int style) throws Exception {
332             builder.append("fontStyle=").append(style).append(',');
333           }
334
335           @Override
336           public boolean canHandleMore() {
337             return true;
338           }
339         });
340       }
341     };
342     SelectionModel selectionModel = editor.getSelectionModel();
343     processor.collectTransferableData(myFixture.getFile(), editor, selectionModel.getBlockSelectionStarts(), selectionModel.getBlockSelectionEnds());
344
345     return builder.toString();
346   }
347
348   private void init(String text) {
349     myFixture.configureByText(getTestName(true) + ".java", text);
350     myFixture.doHighlighting();
351   }
352
353   private void initWithCustomLineSeparators(final String text) {
354     myFixture.configureByText(getTestName(true) + ".java", "");
355     final DocumentImpl document = (DocumentImpl)myFixture.getEditor().getDocument();
356     document.setAcceptSlashR(true);
357     ApplicationManager.getApplication().runWriteAction(new Runnable() {
358       @Override
359       public void run() {
360         document.setText(text);
361       }
362     });
363     myFixture.doHighlighting();
364   }
365
366   private void verifySyntaxInfo(String info) {
367     assertEquals(info, getSyntaxInfo());
368   }
369
370   private int getFontSize() {
371     return myFixture.getEditor().getColorsScheme().getEditorFontSize();
372   }
373
374   @Override
375   protected boolean isWriteActionRequired() {
376     return false;
377   }
378 }