22 package com.realtime.crossfire.jxclient.gui.log;
25 import java.awt.FontFormatException;
26 import java.awt.Graphics2D;
27 import java.awt.GraphicsConfiguration;
28 import java.awt.GraphicsDevice;
29 import java.awt.GraphicsEnvironment;
30 import java.awt.Transparency;
31 import java.awt.image.BufferedImage;
32 import java.io.IOException;
33 import java.io.InputStream;
34 import java.util.Iterator;
35 import org.jetbrains.annotations.NotNull;
36 import org.junit.Assert;
37 import org.junit.Test;
50 if (GraphicsEnvironment.isHeadless()) {
64 if (GraphicsEnvironment.isHeadless()) {
70 rec.
checkResult(
""+
"buffer:\n"+
"line:\n"+
"segment:abc[]d]e\n");
78 if (GraphicsEnvironment.isHeadless()) {
93 rec.
checkResult(
""+
"buffer:\n"+
"line:\n"+
"segment:a \n"+
"segment:b \n"+
"segment:c\n"+
"line:\n"+
"segment:(bold)a \n"+
"segment:(bold)b \n"+
"segment:(bold)c\n"+
"line:\n"+
"segment:(italic)a \n"+
"segment:(italic)b \n"+
"segment:(italic)c\n"+
"line:\n"+
"segment:(underline)a \n"+
"segment:(underline)b \n"+
"segment:(underline)c\n"+
"line:\n"+
"segment:(fixed)a \n"+
"segment:(fixed)b \n"+
"segment:(fixed)c\n"+
"line:\n"+
"segment:(arcane)a \n"+
"segment:(arcane)b \n"+
"segment:(arcane)c\n"+
"line:\n"+
"segment:(hand)a \n"+
"segment:(hand)b \n"+
"segment:(hand)c\n"+
"line:\n"+
"segment:(strange)a \n"+
"segment:(strange)b \n"+
"segment:(strange)c\n"+
"line:\n"+
"segment:a \n"+
"segment:b \n"+
"segment:c\n"+
"line:\n"+
"segment:(red)a \n"+
"segment:(red)b \n"+
"segment:(red)c\n");
101 if (GraphicsEnvironment.isHeadless()) {
105 final Rec rec =
new Rec();
107 rec.
parser.
parse(
"Hello [b] all [b]crossfire[/b] members [/b]", null, rec.
buffer);
108 rec.
checkResult(
""+
"buffer:\n"+
"line:\n"+
"segment:a\n"+
"segment:(bold)b\n"+
"segment:(bold)(italic)c\n"+
"segment:(bold)(italic)(underline)d\n"+
"segment:(italic)(underline)e\n"+
"segment:(underline)f\n"+
"segment:g\n"+
"line:\n"+
"segment:Hello \n"+
"segment:(bold) \n"+
"segment:(bold)all \n"+
"segment:(bold)crossfire\n"+
"segment: \n"+
"segment:members \n");
116 if (GraphicsEnvironment.isHeadless()) {
120 final Rec rec =
new Rec();
121 rec.
parser.
parse(
"[b]a[fixed]b[arcane]c[hand]d[strange]e[print]f", null, rec.
buffer);
122 rec.
checkResult(
""+
"buffer:\n"+
"line:\n"+
"segment:(bold)a\n"+
"segment:(bold)(fixed)b\n"+
"segment:(bold)(arcane)c\n"+
"segment:(bold)(hand)d\n"+
"segment:(bold)(strange)e\n"+
"segment:(bold)f\n");
131 if (GraphicsEnvironment.isHeadless()) {
135 final Rec rec =
new Rec();
137 rec.
checkResult(
""+
"buffer:\n"+
"line:\n"+
"segment:a\n"+
"segment:(fixed)bc\n");
145 if (GraphicsEnvironment.isHeadless()) {
149 final Rec rec =
new Rec();
150 rec.
parser.
parse(
"[b]a[color=red]b[color=blue]c[color=green]d[/color]e[color=#01AB4F]f[/color]", null, rec.
buffer);
151 rec.
checkResult(
""+
"buffer:\n"+
"line:\n"+
"segment:(bold)a\n"+
"segment:(bold)(red)b\n"+
"segment:(bold)(blue)c\n"+
"segment:(bold)(green)d\n"+
"segment:(bold)e\n" +
"segment:(bold)(java.awt.Color[r=1,g=171,b=79])f\n");
159 if (GraphicsEnvironment.isHeadless()) {
163 final Rec rec =
new Rec();
166 rec.
checkResult(
""+
"buffer:\n"+
"line:\n"+
"segment:(bold)(italic)(underline)(hand)(red)first\n"+
"line:\n"+
"segment:second\n");
174 if (GraphicsEnvironment.isHeadless()) {
178 final Rec rec =
new Rec();
180 rec.
checkResult(
""+
"buffer:\n"+
"line:\n"+
"segment:first\n"+
"line:\n"+
"segment:(bold)second\n"+
"line:\n"+
"segment:(bold)th\n"+
"segment:ird\n"+
"line:\n"+
"segment:(italic)fourth\n");
188 if (GraphicsEnvironment.isHeadless()) {
192 final Rec rec =
new Rec();
194 rec.
checkResult(
""+
"buffer:\n"+
"line:\n"+
"segment:abc\n");
200 private static class Rec {
218 final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
219 final GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
220 final GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();
221 final BufferedImage image = graphicsConfiguration.createCompatibleImage(1, 1, Transparency.TRANSLUCENT);
222 final Graphics2D g = image.createGraphics();
226 try (
final InputStream fis = getClass().getClassLoader().getResourceAsStream(
"com/realtime/crossfire/jxclient/skins/ragnorok/fonts/regular.ttf")) {
228 font = Font.createFont(Font.TRUETYPE_FONT, fis);
229 }
catch (
final FontFormatException ex) {
230 Assert.fail(ex.getMessage());
231 throw new AssertionError(ex);
234 }
catch (
final IOException ex) {
235 Assert.fail(ex.getMessage());
236 throw new AssertionError(ex);
238 buffer =
new Buffer(
new Fonts(font, font, font, font), g.getFontRenderContext(), 100);
256 final StringBuilder sb =
new StringBuilder();
258 return sb.toString();
266 private static void dumpBuffer(@NotNull
final StringBuilder sb, @NotNull
final Buffer buffer) {
267 sb.append(
"buffer:\n");
268 synchronized (buffer.getSyncObject()) {
269 final Iterator<Line> it = buffer.iterator();
270 while (it.hasNext()) {
271 final Iterable<Segment> line = it.next();
282 private static void dumpLine(@NotNull
final StringBuilder sb, @NotNull
final Iterable<Segment> line) {
283 sb.append(
"line:\n");
284 for (
final Segment segment : line) {
final Buffer buffer
The default buffer.
final Parser parser
The default parser.
static void dumpLine(@NotNull final StringBuilder sb, @NotNull final Iterable< Segment > line)
Appends the contents of a Line to a StringBuilder.
Parser for parsing drawextinfo messages received from a Crossfire server to update a Buffer instance...
void testAttributesColor()
Checks that color attributes are correctly parsed.
void checkResult(@NotNull final String expected)
Checks for expected contents of buffer.
void testMultiLine()
Checks that multi-line messages are correctly parsed.
void testAttributesReset()
Checks that attributes are reset for each message.
Regression tests for Parser.
void testAttributes2()
Checks that attributes are correctly parsed.
static void dumpBuffer(@NotNull final StringBuilder sb, @NotNull final Buffer buffer)
Appends the contents of a Buffer to a StringBuilder.
Rec()
Creates a new instance.
One segment of a Line which should be displayed without changing text attributes. ...
String dumpBuffer()
Returns a string representation of buffer.
void testAttributes1()
Checks that attributes are correctly parsed.
void testDropUnClosedTag()
Checks that an un-closed tag is dropped.
Manages the contents of the contents of a log window.
void testAttributesIgnore()
Checks that unknown attributes are ignored.
void testAttributesFont1()
Checks that font attributes are correctly parsed.
void parse(@NotNull final CharSequence text, @Nullable final Color defaultColor, @NotNull final Buffer buffer)
Parses a text message.
void testEmpty()
Checks that an empty string does not add anything.
void testAttributesFont2()
Checks that font attributes are correctly parsed: [/fixed] is undefined/does not end [fixed] block...