/tests/Fest/jedit_tests/test/gui/SelectionForegroundColorTest.java

# · Java · 193 lines · 166 code · 17 blank · 10 comment · 7 complexity · e5017efdc3e6442c1fe0eb05420fb4be MD5 · raw file

  1. package test.gui;
  2. import static org.junit.Assert.assertTrue;
  3. import java.awt.Color;
  4. import java.io.File;
  5. import javax.swing.SwingUtilities;
  6. import org.fest.swing.core.matcher.JButtonMatcher;
  7. import org.fest.swing.finder.WindowFinder;
  8. import org.fest.swing.fixture.DialogFixture;
  9. import org.fest.swing.fixture.JCheckBoxFixture;
  10. import org.fest.swing.fixture.JPanelFixture;
  11. import org.fest.swing.image.ScreenshotTaker;
  12. import org.fest.swing.timing.Pause;
  13. import org.fest.util.Files;
  14. import org.gjt.sp.jedit.View;
  15. import org.gjt.sp.jedit.jEdit;
  16. import org.gjt.sp.jedit.buffer.JEditBuffer;
  17. import org.gjt.sp.jedit.options.GlobalOptions;
  18. import org.gjt.sp.jedit.testframework.TestUtils;
  19. import org.gjt.sp.jedit.textarea.JEditTextArea;
  20. import org.gjt.sp.jedit.textarea.Selection;
  21. import org.junit.AfterClass;
  22. import org.junit.BeforeClass;
  23. import org.junit.Test;
  24. public class SelectionForegroundColorTest
  25. {
  26. private static final String IMAGE1 = "selFgColorImage1.png";
  27. private static final String IMAGE2 = "selFgColorImage2.png";
  28. private static final String IMAGE3 = "selFgColorImage3.png";
  29. private static final String [] lines = new String[] {
  30. "#include <stdio.h>",
  31. "int main(int argc, char *argv[])",
  32. "{",
  33. "\tint i, a, b;",
  34. "\tfor (i=0; i<10; i++)",
  35. "\t{",
  36. "\t\tprintf(\"%d: aaaaaaaabbbbbbbbbbb ccccccccdddddddddd eeeeeeeeeeefffffffffffffff ggggggggggggggggghhhhhhhhhhhhhhh\", i);",
  37. "\t\ta = 5; b = 6; a++; b++; a += b; b += a; a *= b; b *= a; a = 5; b = 6; a++; b++; a += b; b += a; a *= b; b *= a;",
  38. "\t}",
  39. "\tprintf; for; next; continue; while; do; break; some_long_identifier, [even_longer_identifier]",
  40. "\tprintf(\"%d: aaaaaaaabbbbbbbbbbb ccccccccdddddddddd eeeeeeeeeeefffffffffffffff ggggggggggggggggghhhhhhhhhhhhhhh\", i);",
  41. "\ta = 5; b = 6; a++; b++; a += b; b += a; a *= b; b *= a; a = 5; b = 6; a++; b++; a += b; b += a; a *= b; b *= a;",
  42. "\tDummyEnd;",
  43. "}"
  44. };
  45. @BeforeClass
  46. public static void setUpjEdit() {
  47. TestUtils.beforeClass();
  48. }
  49. @AfterClass
  50. public static void tearDownjEdit() {
  51. TestUtils.afterClass();
  52. }
  53. private static void selectRanges(JEditTextArea ta)
  54. {
  55. // select some text segments
  56. String [] subStrings = new String[] {
  57. "main", "int", "intf", "<1", "+)", "\"%", "aabb", "eeff", "long_id",
  58. "ier]"
  59. };
  60. JEditBuffer buf = ta.getBuffer();
  61. for (String sub: subStrings)
  62. {
  63. for (int i = 0; i < buf.getLineCount(); i++)
  64. {
  65. String line = buf.getLineText(i);
  66. int index = line.indexOf(sub);
  67. if (index == -1)
  68. continue;
  69. index += buf.getLineStartOffset(i);
  70. ta.addToSelection(new Selection.Range(index, index + sub.length()));
  71. }
  72. }
  73. ta.addToSelection(new Selection.Range(ta.getLineStartOffset(
  74. lines.length - 4) + 10, ta.getLineStartOffset(
  75. lines.length - 2) + 3));
  76. }
  77. @Test
  78. public void testOptionPane() {
  79. // set the jEdit properties for the option pane to known values
  80. jEdit.setBooleanProperty("view.selectionFg", false);
  81. jEdit.setColorProperty("view.selectionFgColor", Color.white);
  82. // open the options and select the options pane
  83. TestUtils.jEditFrame().menuItemWithPath("Utilities",
  84. "Global Options...").click();
  85. DialogFixture optionsDialog = WindowFinder.findDialog(
  86. GlobalOptions.class).withTimeout(5000).using(TestUtils.robot());
  87. TestUtils.selectPath(optionsDialog.tree(), new String[] {
  88. "jEdit", "Text Area"});
  89. JPanelFixture pane = optionsDialog.panel("textarea");
  90. assertTrue("TextAreaOptionPane not found", pane != null);
  91. // test that all checkboxes are present and click them
  92. JCheckBoxFixture checkbox = pane.checkBox("selectionFg");
  93. assertTrue("Cannot find selectionFg checkbox in TextAreaOptionPane",
  94. checkbox != null);
  95. checkbox.requireNotSelected();
  96. checkbox.click();
  97. // click the OK button on the options dialog
  98. optionsDialog.button(JButtonMatcher.withText("OK")).click();
  99. // wait a second to make sure jEdit has time to save the properties
  100. Pause.pause( 1000 );
  101. // test that the properties were set correctly
  102. boolean b = jEdit.getBooleanProperty("view.selectionFg", false);
  103. assertTrue("selectionFg is not checked", b);
  104. final View view = TestUtils.view();
  105. final JEditTextArea ta = view.getTextArea();
  106. final JEditBuffer buf = ta.getBuffer();
  107. try
  108. {
  109. SwingUtilities.invokeAndWait(new Runnable() {
  110. public void run() {
  111. ta.setCaretBlinkEnabled(false);
  112. for (String line: lines)
  113. buf.insert(buf.getLength(), line + "\n");
  114. buf.setMode("c");
  115. // select some text segments
  116. selectRanges(ta);
  117. ta.repaint();
  118. }
  119. });
  120. }
  121. catch (Exception e)
  122. {
  123. e.printStackTrace();
  124. }
  125. String refDir = Files.currentFolder().getPath() + File.separator +
  126. "resources" + File.separator;
  127. String tempPath = Files.temporaryFolderPath() + File.separator;
  128. ScreenshotTaker screenshotTaker = new ScreenshotTaker();
  129. String image = tempPath + IMAGE1;
  130. new File(image).delete();
  131. screenshotTaker.saveComponentAsPng(ta, image);
  132. String ref = refDir + IMAGE1;
  133. assertTrue("Normal mode images differ", TestUtils.compareFiles(image, ref));
  134. // Test horizontal scrolling
  135. try
  136. {
  137. SwingUtilities.invokeAndWait(new Runnable() {
  138. public void run() {
  139. ta.setCaretPosition(ta.getLineEndOffset(6) - 5);
  140. selectRanges(ta);
  141. ta.scrollToCaret(true);
  142. ta.repaint();
  143. }
  144. });
  145. }
  146. catch (Exception e)
  147. {
  148. e.printStackTrace();
  149. }
  150. image = tempPath + IMAGE2;
  151. new File(image).delete();
  152. screenshotTaker.saveComponentAsPng(ta, image);
  153. ref = refDir + IMAGE2;
  154. assertTrue("Horz scroll images differ", TestUtils.compareFiles(image, ref));
  155. // Test soft-wrap
  156. try
  157. {
  158. SwingUtilities.invokeAndWait(new Runnable() {
  159. public void run() {
  160. buf.setProperty("wrap", "soft");
  161. buf.propertiesChanged();
  162. ta.repaint();
  163. }
  164. });
  165. }
  166. catch (Exception e)
  167. {
  168. e.printStackTrace();
  169. }
  170. image = tempPath + IMAGE3;
  171. new File(image).delete();
  172. screenshotTaker.saveComponentAsPng(ta, image);
  173. ref = refDir + IMAGE3;
  174. assertTrue("Soft-wrap images differ", TestUtils.compareFiles(image, ref));
  175. }
  176. }