PageRenderTime 86ms CodeModel.GetById 67ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

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

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