Crossfire JXClient, Trunk  R20561
RenderStateTest.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.log;
23 
24 import java.awt.Font;
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.List;
35 import org.jetbrains.annotations.NotNull;
36 import org.junit.Assert;
37 import org.junit.Test;
38 
44 public class RenderStateTest {
45 
49  private static final int HEIGHT = 104;
50 
54  @Test
55  public void test1() {
56  if (GraphicsEnvironment.isHeadless()) {
57  return;
58  }
59 
60  final Rec rec = new Rec();
61  final Parser parser = new Parser();
62 
63  for (int i = 0; i < HEIGHT+10; i++) {
64  parser.parse("xxx"+i, null, rec.buffer);
65  }
66  rec.checkState(101, 0);
67 
68  // scroll to valid positions
69  rec.scrollTo(0);
70  rec.checkState(0, 0);
72  rec.checkState(5, 3);
74  rec.checkState(10, 0);
75 
76  // scroll to invalid positions
77  rec.scrollTo(-1);
78  rec.checkState(0, 0);
79  rec.scrollTo(-10);
80  rec.checkState(0, 0);
81 
82  // scroll to invalid positions
86  rec.checkState(101, 0);
87  }
88 
92  @Test
93  public void test2() {
94  if (GraphicsEnvironment.isHeadless()) {
95  return;
96  }
97 
98  final Rec rec = new Rec();
99  final Parser parser = new Parser();
100 
101  Assert.assertEquals(0, HEIGHT%Buffer.MIN_LINE_HEIGHT);
102 
103  rec.checkState(0, 0);
104  parser.parse("xxx1", null, rec.buffer);
105  rec.checkState(0, 0);
106  parser.parse("xxx2", null, rec.buffer);
107  rec.checkState(0, 0);
108 
109  // add lines to completely fill visible area
110  for (int i = 2; i < HEIGHT/Buffer.MIN_LINE_HEIGHT; i++) {
111  parser.parse("xxx3"+i, null, rec.buffer);
112  }
113  rec.checkState(0, 0);
114 
115  // add one more line ==> buffer sticks at bottom
116  parser.parse("xxx4", null, rec.buffer);
117  rec.checkState(1, 0);
118 
119  // add one more line ==> buffer sticks at bottom
120  parser.parse("xxx5", null, rec.buffer);
121  rec.checkState(2, 0);
122 
123  // scroll up one line
125  rec.checkState(1, 0);
126 
127  // add one more line ==> buffer sticks at scroll position
128  parser.parse("xxx6", null, rec.buffer);
129  rec.checkState(1, 0);
130 
131  // scroll back to bottom
133  rec.checkState(3, 0);
134 
135  // add one more line ==> buffer sticks at bottom
136  parser.parse("xxx7", null, rec.buffer);
137  rec.checkState(4, 0);
138 
139  // completely fill buffer
140  for (int i = HEIGHT/Buffer.MIN_LINE_HEIGHT+4; i < Buffer.MAX_LINES; i++) {
141  parser.parse("xxx8"+i, null, rec.buffer);
142  }
144 
145  // add one more line ==> buffer sticks at bottom
146  parser.parse("xxx9", null, rec.buffer);
148 
149  // scroll one line up
152 
153  // fill more lines ==> scroll position sticks
154  for (int i = 0; i < Buffer.MAX_LINES-HEIGHT/Buffer.MIN_LINE_HEIGHT-2; i++) {
155  parser.parse("xxx0"+i, null, rec.buffer);
156  }
157  rec.checkState(1, 0);
158  parser.parse("xxx1", null, rec.buffer);
159  rec.checkState(0, 0);
160 
161  // add one more line ==> scroll position hits top
162  parser.parse("xxx2", null, rec.buffer);
163  rec.checkState(0, 0);
164  }
165 
169  private static class Rec {
170 
174  @NotNull
175  private final RenderState rs;
176 
180  @NotNull
181  private final Buffer buffer;
182 
186  private Rec() {
187  final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
188  final GraphicsDevice graphicsDevice = graphicsEnvironment.getDefaultScreenDevice();
189  final GraphicsConfiguration graphicsConfiguration = graphicsDevice.getDefaultConfiguration();
190  final BufferedImage image = graphicsConfiguration.createCompatibleImage(1, 1, Transparency.TRANSLUCENT);
191  final Graphics2D g = image.createGraphics();
192  final Font font;
193  try {
194  try (final InputStream fis = getClass().getClassLoader().getResourceAsStream("com/realtime/crossfire/jxclient/skins/ragnorok/fonts/regular.ttf")) {
195  try {
196  font = Font.createFont(Font.TRUETYPE_FONT, fis);
197  } catch (final FontFormatException ex) {
198  Assert.fail();
199  throw new AssertionError(ex);
200  }
201  }
202  } catch (final IOException ex) {
203  Assert.fail();
204  throw new AssertionError(ex);
205  }
206  buffer = new Buffer(new Fonts(font, font, font, font), g.getFontRenderContext(), 100);
207  g.dispose();
208  rs = new RenderState();
209  assert buffer != null;
210  rs.setHeight(buffer, HEIGHT);
211 
212  final BufferListener bufferListener = new BufferListener() {
213 
214  @Override
215  public void lineAdded() {
216  assert rs != null;
217  assert buffer != null;
218  rs.linesAdded(buffer);
219  }
220 
221  @Override
222  public void lineReplaced() {
223  assert rs != null;
224  assert buffer != null;
225  rs.linesReplaced(buffer);
226  }
227 
228  @Override
229  public void linesRemoved(@NotNull final List<Line> lines) {
230  assert rs != null;
231  assert buffer != null;
232  rs.linesRemoved(buffer, lines);
233  }
234 
235  };
236  assert buffer != null;
237  buffer.addBufferListener(bufferListener);
238  }
239 
244  public void scrollTo(final int y) {
245  rs.scrollTo(buffer, y);
246  }
247 
254  public void checkState(final int expectedTopIndex, final int expectedTopOffset) {
255  final int expectedScrollPos = expectedTopIndex*Buffer.MIN_LINE_HEIGHT+expectedTopOffset;
256  final int topIndex = rs.getTopIndex();
257  final int topOffset = rs.getTopOffset();
258  final int scrollPos = rs.getScrollPos();
259  Assert.assertEquals(formatState(expectedTopIndex, expectedTopOffset, expectedScrollPos), formatState(topIndex, topOffset, scrollPos));
260  }
261 
269  @NotNull
270  public String formatState(final int topIndex, final int topOffset, final int scrollPos) {
271  return "top="+topIndex+"/"+topOffset+" pos="+scrollPos+"/"+buffer.getTotalHeight();
272  }
273 
274  }
275 
276 }
static final int MAX_LINES
The maximum number of lines the buffer can hold.
Definition: Buffer.java:46
String formatState(final int topIndex, final int topOffset, final int scrollPos)
Returns a text representation of the state.
int getTotalHeight()
Returns the total height of all lines.
Definition: Buffer.java:237
void scrollTo(final int y)
Calls RenderState#scrollTo(Buffer, int).
void checkState(final int expectedTopIndex, final int expectedTopOffset)
Checks that the RenderState instance contains expected values.
int getTopIndex()
Returns the index of the first line to display.
static final int HEIGHT
Assumed height of log window.
Parser for parsing drawextinfo messages received from a Crossfire server to update a Buffer instance...
Definition: Parser.java:37
void linesReplaced(@NotNull final Buffer buffer)
Some lines have been replaced at the end of the buffer.
void linesAdded(@NotNull final Buffer buffer)
Some lines have been added to the buffer.
void test2()
Checks that overflowing the buffer works as expected.
int getScrollPos()
Returns the location of the view area in pixels.
void linesRemoved(@NotNull final Buffer buffer, @NotNull final Collection< Line > lines)
Some lines have been removed from the buffer.
void addBufferListener(@NotNull final BufferListener listener)
Adds a listener to notify of changes.
Definition: Buffer.java:321
static final int MIN_LINE_HEIGHT
The minimal height of a line in pixels.
Definition: Buffer.java:51
void setHeight(@NotNull final Buffer buffer, final int h)
Sets the the viewable height in pixel.
int getTopOffset()
Returns the number of pixels to shift the first displayed line.
Manages the contents of the contents of a log window.
Definition: Buffer.java:41
void parse(@NotNull final CharSequence text, @Nullable final Color defaultColor, @NotNull final Buffer buffer)
Parses a text message.
Definition: Parser.java:111
Interface for listeners for changes of Buffer contents.
Encapsulates the state for a scroll bar.
final RenderState rs
The tested RenderState instance.
void scrollTo(@NotNull final Buffer buffer, final int y)
Scrolls to the given pixel location.