Crossfire JXClient, Trunk
RenderStateTestRec.java
Go to the documentation of this file.
1 package com.realtime.crossfire.jxclient.gui.log;
2 
3 import java.util.List;
4 import org.jetbrains.annotations.NotNull;
5 import org.junit.Assert;
6 
10 public class RenderStateTestRec {
11 
15  public static final int FONT_HEIGHT = 11;
16 
20  public static final int HEIGHT = 10*FONT_HEIGHT;
21 
25  @NotNull
26  private final RenderState rs;
27 
31  @NotNull
32  private final Buffer buffer;
33 
38  public RenderStateTestRec(final int maxLines) {
39  buffer = new TestBuffer(maxLines, 200, FONT_HEIGHT);
41  rs = new RenderState();
42  assert buffer != null;
44 
45  final BufferListener bufferListener = new BufferListener() {
46 
47  @Override
48  public void lineAdded() {
49  assert rs != null;
50  assert buffer != null;
52  }
53 
54  @Override
55  public void lineReplaced() {
56  assert rs != null;
57  assert buffer != null;
59  }
60 
61  @Override
62  public void linesRemoved(@NotNull final List<Line> lines) {
63  assert rs != null;
64  assert buffer != null;
65  rs.linesRemoved(buffer, lines);
66  }
67 
68  };
69  assert buffer != null;
70  buffer.addBufferListener(bufferListener);
71  }
72 
77  @NotNull
78  public Buffer getBuffer() {
79  return buffer;
80  }
81 
86  public void scrollTo(final int y) {
87  rs.scrollTo(buffer, y);
88  }
89 
95  public void checkState(final int expectedTopIndex, final int expectedTopOffset) {
96  final int expectedScrollPos = expectedTopIndex*FONT_HEIGHT+expectedTopOffset;
97  final int topIndex = rs.getTopIndex();
98  final int topOffset = rs.getTopOffset();
99  final int scrollPos = rs.getScrollPos();
100  Assert.assertEquals(formatState(expectedTopIndex, expectedTopOffset, expectedScrollPos), formatState(topIndex, topOffset, scrollPos));
101  }
102 
110  @NotNull
111  private String formatState(final int topIndex, final int topOffset, final int scrollPos) {
112  return "top="+topIndex+"/"+topOffset+" pos="+scrollPos+"/"+buffer.getTotalHeight();
113  }
114 
115 }
com.realtime.crossfire.jxclient.gui.log.RenderStateTestRec.getBuffer
Buffer getBuffer()
Returns the tested Buffer instance.
Definition: RenderStateTestRec.java:78
com.realtime.crossfire.jxclient.gui.log.BufferListener
Interface for listeners for changes of Buffer contents.
Definition: BufferListener.java:33
com.realtime.crossfire.jxclient.gui.log.RenderStateTestRec.FONT_HEIGHT
static final int FONT_HEIGHT
The height of the font in pixels.
Definition: RenderStateTestRec.java:15
com.realtime.crossfire.jxclient.gui.log.RenderStateTestRec.scrollTo
void scrollTo(final int y)
Calls RenderState#scrollTo(Buffer, int).
Definition: RenderStateTestRec.java:86
com.realtime.crossfire.jxclient.gui.log.RenderState.linesReplaced
void linesReplaced(@NotNull final Buffer buffer)
Some lines have been replaced at the end of the buffer.
Definition: RenderState.java:147
com.realtime.crossfire.jxclient.gui.log.RenderState.linesAdded
void linesAdded(@NotNull final Buffer buffer)
Some lines have been added to the buffer.
Definition: RenderState.java:130
com.realtime.crossfire.jxclient.gui.log.Buffer.addBufferListener
void addBufferListener(@NotNull final BufferListener listener)
Adds a listener to notify of changes.
Definition: Buffer.java:310
com.realtime.crossfire.jxclient.gui.log.RenderStateTestRec.formatState
String formatState(final int topIndex, final int topOffset, final int scrollPos)
Returns a text representation of the state.
Definition: RenderStateTestRec.java:111
com.realtime.crossfire.jxclient.gui.log.RenderStateTestRec
Encapsulates the state.
Definition: RenderStateTestRec.java:10
com.realtime.crossfire.jxclient.gui.log.RenderStateTestRec.HEIGHT
static final int HEIGHT
Assumed height of log window.
Definition: RenderStateTestRec.java:20
com.realtime.crossfire.jxclient.gui.log.RenderState.setHeight
void setHeight(@NotNull final Buffer buffer, final int h)
Sets the viewable height in pixel.
Definition: RenderState.java:88
com.realtime.crossfire.jxclient.gui.log.RenderState.getTopOffset
int getTopOffset()
Returns the number of pixels to shift the first displayed line.
Definition: RenderState.java:215
com.realtime.crossfire.jxclient.gui.log.RenderStateTestRec.checkState
void checkState(final int expectedTopIndex, final int expectedTopOffset)
Checks that the RenderState instance contains expected values.
Definition: RenderStateTestRec.java:95
com.realtime.crossfire.jxclient.gui.log.Buffer
Manages the contents of the contents of a log window.
Definition: Buffer.java:41
com.realtime.crossfire.jxclient.gui.log.RenderStateTestRec.RenderStateTestRec
RenderStateTestRec(final int maxLines)
Creates a new instance.
Definition: RenderStateTestRec.java:38
com.realtime.crossfire.jxclient.gui.log.RenderState.scrollTo
void scrollTo(@NotNull final Buffer buffer, final int y)
Scrolls to the given pixel location.
Definition: RenderState.java:271
com.realtime.crossfire.jxclient.gui.log.RenderState
Encapsulates the state for a scroll bar.
Definition: RenderState.java:35
com.realtime.crossfire.jxclient.gui.log.RenderState.getTopIndex
int getTopIndex()
Returns the index of the first line to display.
Definition: RenderState.java:205
com.realtime.crossfire.jxclient.gui.log.Buffer.setShowTimestamps
void setShowTimestamps(final boolean showTimestamps)
Sets whether timestamps are shown.
Definition: Buffer.java:162
com.realtime.crossfire.jxclient.gui.log.RenderStateTestRec.rs
final RenderState rs
The tested RenderState instance.
Definition: RenderStateTestRec.java:26
com.realtime.crossfire.jxclient.gui.log.RenderState.linesRemoved
void linesRemoved(@NotNull final Buffer buffer, @NotNull final Collection< Line > lines)
Some lines have been removed from the buffer.
Definition: RenderState.java:161
com.realtime.crossfire.jxclient.gui.log.Buffer.getTotalHeight
int getTotalHeight()
Returns the total height of all lines.
Definition: Buffer.java:275
com.realtime.crossfire.jxclient.gui.log.RenderState.getScrollPos
int getScrollPos()
Returns the location of the view area in pixels.
Definition: RenderState.java:225
com.realtime.crossfire.jxclient.gui.log.RenderStateTestRec.buffer
final Buffer buffer
The tested Buffer instance.
Definition: RenderStateTestRec.java:32
com.realtime.crossfire.jxclient.gui.log.TestBuffer
A Buffer implementation for tests.
Definition: TestBuffer.java:11