mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-05 10:49:34 +02:00
GT-3035 - Restore Integration Tests - restored missing test files; fast
tests
This commit is contained in:
parent
822e5a0610
commit
4dc8e77172
45 changed files with 6609 additions and 0 deletions
|
@ -0,0 +1,122 @@
|
|||
/* ###
|
||||
* IP: GHIDRA
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package docking.widgets.imagepanel;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.awt.Image;
|
||||
|
||||
import javax.swing.Icon;
|
||||
import javax.swing.JFrame;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import docking.test.AbstractDockingTest;
|
||||
import resources.ResourceManager;
|
||||
import resources.icons.EmptyIcon;
|
||||
|
||||
public class ImagePanelTest extends AbstractDockingTest {
|
||||
|
||||
private JFrame frame;
|
||||
private ImagePanel imagePanel;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
Icon emptyIcon = new EmptyIcon(32, 32);
|
||||
Image emptyImage = ResourceManager.getImageIcon(emptyIcon).getImage();
|
||||
imagePanel = new ImagePanel(emptyImage);
|
||||
|
||||
frame = new JFrame("ImagePanel Test");
|
||||
frame.getContentPane().add(imagePanel);
|
||||
frame.setSize(400, 400);
|
||||
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
frame.dispose();
|
||||
}
|
||||
|
||||
private void reset() {
|
||||
imagePanel.setZoomFactor(1.0f);
|
||||
|
||||
assertTrue("Unable to reset zoom factor",
|
||||
Float.compare(imagePanel.getZoomFactor(), 1.0f) == 0);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZoom_Neutral() {
|
||||
reset();
|
||||
|
||||
imagePanel.setZoomFactor(1.0f);
|
||||
|
||||
assertTrue("Zoom factor not set to 1.0x",
|
||||
Float.compare(imagePanel.getZoomFactor(), 1.0f) == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZoom_10Point0f() {
|
||||
reset();
|
||||
|
||||
imagePanel.setZoomFactor(10.0f);
|
||||
|
||||
assertTrue("Zoom factor not set to 10.0x",
|
||||
Float.compare(imagePanel.getZoomFactor(), 10.0f) == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZoom_0Point05() {
|
||||
reset();
|
||||
|
||||
imagePanel.setZoomFactor(0.05f);
|
||||
|
||||
assertTrue("Zoom factor not set to 0.05x",
|
||||
Float.compare(imagePanel.getZoomFactor(), 0.05f) == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZoom_20Point0() {
|
||||
reset();
|
||||
|
||||
imagePanel.setZoomFactor(20.0f);
|
||||
|
||||
assertTrue("Zoom factor not set to 20.0x; should be 10.0x",
|
||||
Float.compare(imagePanel.getZoomFactor(), 10.0f) == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZoom_0Point001() {
|
||||
reset();
|
||||
|
||||
imagePanel.setZoomFactor(0.001f);
|
||||
|
||||
assertTrue("Zoom factor not set to 0.001x; should be 0.05x",
|
||||
Float.compare(imagePanel.getZoomFactor(), 0.05f) == 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testZoom_3Point75() {
|
||||
reset();
|
||||
|
||||
imagePanel.setZoomFactor(3.75f);
|
||||
|
||||
assertTrue("Zoom factor not set to 3.75x; should be 4.0x",
|
||||
Float.compare(imagePanel.getZoomFactor(), 4.0f) == 0);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,519 @@
|
|||
/* ###
|
||||
* IP: GHIDRA
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package docking.widgets.textfield;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.hamcrest.CoreMatchers.nullValue;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.RenderingHints.Key;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
import java.awt.font.FontRenderContext;
|
||||
import java.awt.font.GlyphVector;
|
||||
import java.awt.geom.AffineTransform;
|
||||
import java.awt.image.*;
|
||||
import java.awt.image.renderable.RenderableImage;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.PrintStream;
|
||||
import java.text.AttributedCharacterIterator;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.swing.DebugGraphics;
|
||||
import javax.swing.RepaintManager;
|
||||
|
||||
import org.hamcrest.CoreMatchers;
|
||||
import org.junit.Test;
|
||||
|
||||
public class HexOrDecimalInputTest {
|
||||
|
||||
@Test
|
||||
public void testSetAllowNegative() {
|
||||
HexOrDecimalInput input = new HexOrDecimalInput();
|
||||
|
||||
Long newValue = -1L;
|
||||
input.setValue(newValue);
|
||||
|
||||
assertThat(input.getValue(), is(newValue));
|
||||
|
||||
input.setAllowNegative(false);
|
||||
assertThat(input.getValue(), nullValue());
|
||||
|
||||
newValue = 20L;
|
||||
input.setValue(newValue);
|
||||
assertThat(input.getValue(), is(newValue));
|
||||
|
||||
newValue = -100L;
|
||||
input.setValue(newValue);
|
||||
assertThat(input.getValue(), nullValue());
|
||||
|
||||
input.setAllowNegative(true);
|
||||
newValue = -100L;
|
||||
input.setValue(newValue);
|
||||
assertThat(input.getValue(), is(newValue));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCustomPaint() {
|
||||
|
||||
HexOrDecimalInput input = new HexOrDecimalInput();
|
||||
RepaintManager repaintManager = RepaintManager.currentManager(input);
|
||||
repaintManager.setDoubleBufferingEnabled(false);
|
||||
|
||||
SpyPrintStream spy = new SpyPrintStream();
|
||||
DebugGraphics.setLogStream(spy);
|
||||
|
||||
DebugGraphics debugGraphics = new DebugGraphics(scratchGraphics());
|
||||
debugGraphics.setDebugOptions(DebugGraphics.LOG_OPTION);
|
||||
|
||||
Graphics2D g2d = new Graphics2DAdapter(debugGraphics);
|
||||
input.paintComponent(g2d);
|
||||
assertThat(spy.toString(), CoreMatchers.containsString("Dec"));
|
||||
|
||||
spy.reset();
|
||||
input.setHexMode();
|
||||
input.paintComponent(g2d);
|
||||
assertThat(spy.toString(), CoreMatchers.containsString("Hex"));
|
||||
|
||||
spy.reset();
|
||||
input.setDecimalMode();
|
||||
input.paintComponent(g2d);
|
||||
assertThat(spy.toString(), CoreMatchers.containsString("Dec"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testToggleHexModeFromKeybinding() {
|
||||
HexOrDecimalInput input = new HexOrDecimalInput();
|
||||
Long value = 10L;
|
||||
input.setValue(value);
|
||||
|
||||
assertThat(input.getValue(), is(value));
|
||||
|
||||
toggleMode(input);
|
||||
|
||||
assertThat(input.getValue(), is(0xAL));
|
||||
|
||||
toggleMode(input);
|
||||
|
||||
assertThat(input.getValue(), is(value));
|
||||
}
|
||||
|
||||
//==================================================================================================
|
||||
// Inner Classes
|
||||
//==================================================================================================
|
||||
|
||||
private void toggleMode(final HexOrDecimalInput input) {
|
||||
KeyEvent event = new KeyEvent(input, 0, System.currentTimeMillis(), 0, KeyEvent.VK_M, 'm');
|
||||
KeyListener[] keyListeners = input.getKeyListeners();
|
||||
for (KeyListener listener : keyListeners) {
|
||||
listener.keyPressed(event);
|
||||
}
|
||||
}
|
||||
|
||||
private Graphics scratchGraphics() {
|
||||
BufferedImage image = new BufferedImage(100, 20, BufferedImage.TYPE_INT_BGR);
|
||||
return image.getGraphics();
|
||||
}
|
||||
|
||||
private static class SpyPrintStream extends PrintStream {
|
||||
private static ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
|
||||
SpyPrintStream() {
|
||||
super(baos);
|
||||
}
|
||||
|
||||
void reset() {
|
||||
baos.reset();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return baos.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* An adapter to turn a Graphics into a Graphics2D. This implementation satisfies the base
|
||||
* methods needed for the test. So, many operations are stubbed or are exceptional.
|
||||
*/
|
||||
private static class Graphics2DAdapter extends Graphics2D {
|
||||
|
||||
private Graphics delegate;
|
||||
|
||||
Graphics2DAdapter(Graphics g) {
|
||||
this.delegate = g;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Shape s) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean drawImage(Image img, AffineTransform xform, ImageObserver obs) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawRenderedImage(RenderedImage img, AffineTransform xform) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawRenderableImage(RenderableImage img, AffineTransform xform) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawString(String str, int x, int y) {
|
||||
delegate.drawString(str, x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawString(String str, float x, float y) {
|
||||
delegate.drawString(str, (int) x, (int) y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawString(AttributedCharacterIterator iterator, int x, int y) {
|
||||
delegate.drawString(iterator, x, y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawString(AttributedCharacterIterator iterator, float x, float y) {
|
||||
delegate.drawString(iterator, (int) x, (int) y);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawGlyphVector(GlyphVector g, float x, float y) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fill(Shape s) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public GraphicsConfiguration getDeviceConfiguration() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setComposite(Composite comp) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPaint(Paint paint) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setStroke(Stroke s) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRenderingHint(Key hintKey, Object hintValue) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRenderingHint(Key hintKey) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRenderingHints(Map<?, ?> hints) {
|
||||
// no-op
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addRenderingHints(Map<?, ?> hints) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RenderingHints getRenderingHints() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void translate(int x, int y) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void translate(double tx, double ty) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rotate(double theta) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rotate(double theta, double x, double y) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scale(double sx, double sy) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void shear(double shx, double shy) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void transform(AffineTransform Tx) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setTransform(AffineTransform Tx) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public AffineTransform getTransform() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Paint getPaint() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Composite getComposite() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBackground(Color color) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getBackground() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Stroke getStroke() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clip(Shape s) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public FontRenderContext getFontRenderContext() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Graphics create() {
|
||||
return delegate.create();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Color getColor() {
|
||||
return delegate.getColor();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setColor(Color c) {
|
||||
delegate.setColor(c);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setPaintMode() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setXORMode(Color c1) {
|
||||
delegate.setXORMode(c1);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Font getFont() {
|
||||
return delegate.getFont();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFont(Font font) {
|
||||
delegate.setFont(font);
|
||||
}
|
||||
|
||||
@Override
|
||||
public FontMetrics getFontMetrics(Font f) {
|
||||
return delegate.getFontMetrics();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Rectangle getClipBounds() {
|
||||
return delegate.getClipBounds();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clipRect(int x, int y, int width, int height) {
|
||||
delegate.clipRect(x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setClip(int x, int y, int width, int height) {
|
||||
delegate.setClip(x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Shape getClip() {
|
||||
return delegate.getClip();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setClip(Shape clip) {
|
||||
delegate.setClip(clip);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void copyArea(int x, int y, int width, int height, int dx, int dy) {
|
||||
delegate.copyArea(x, y, width, height, dx, dy);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawLine(int x1, int y1, int x2, int y2) {
|
||||
delegate.drawLine(x1, y1, x2, y2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillRect(int x, int y, int width, int height) {
|
||||
delegate.fillRect(x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearRect(int x, int y, int width, int height) {
|
||||
delegate.clearRect(x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawRoundRect(int x, int y, int width, int height, int arcWidth,
|
||||
int arcHeight) {
|
||||
delegate.drawRoundRect(x, y, width, height, arcWidth, arcHeight);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillRoundRect(int x, int y, int width, int height, int arcWidth,
|
||||
int arcHeight) {
|
||||
delegate.fillRoundRect(x, y, width, height, arcWidth, arcHeight);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawOval(int x, int y, int width, int height) {
|
||||
delegate.drawOval(x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillOval(int x, int y, int width, int height) {
|
||||
delegate.fillOval(x, y, width, height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
|
||||
delegate.drawArc(x, y, width, height, startAngle, arcAngle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) {
|
||||
delegate.fillArc(x, y, width, height, startAngle, arcAngle);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawPolyline(int[] xPoints, int[] yPoints, int nPoints) {
|
||||
delegate.drawPolyline(xPoints, yPoints, nPoints);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void drawPolygon(int[] xPoints, int[] yPoints, int nPoints) {
|
||||
delegate.drawPolygon(xPoints, yPoints, nPoints);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fillPolygon(int[] xPoints, int[] yPoints, int nPoints) {
|
||||
delegate.fillPolygon(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
|
||||
return delegate.drawImage(img, x, y, observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean drawImage(Image img, int x, int y, int width, int height,
|
||||
ImageObserver observer) {
|
||||
return delegate.drawImage(img, x, y, width, height, observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean drawImage(Image img, int x, int y, Color bgcolor, ImageObserver observer) {
|
||||
return delegate.drawImage(img, x, y, bgcolor, observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean drawImage(Image img, int x, int y, int width, int height, Color bgcolor,
|
||||
ImageObserver observer) {
|
||||
return delegate.drawImage(img, x, y, width, height, bgcolor, observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
|
||||
int sx2, int sy2, ImageObserver observer) {
|
||||
return delegate.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean drawImage(Image img, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1,
|
||||
int sx2, int sy2, Color bgcolor, ImageObserver observer) {
|
||||
return delegate.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, bgcolor,
|
||||
observer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
delegate.dispose();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,290 @@
|
|||
/* ###
|
||||
* IP: GHIDRA
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package docking.widgets.textfield;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.util.concurrent.atomic.AtomicIntegerArray;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.event.ChangeEvent;
|
||||
import javax.swing.event.ChangeListener;
|
||||
|
||||
import org.junit.*;
|
||||
|
||||
import docking.test.AbstractDockingTest;
|
||||
|
||||
public class IntegerTextFieldTest extends AbstractDockingTest {
|
||||
|
||||
private JFrame frame;
|
||||
private IntegerTextField field;
|
||||
private JTextField textField;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
||||
field = new IntegerTextField(10);
|
||||
field.setShowNumberMode(true);
|
||||
textField = (JTextField) field.getComponent();
|
||||
frame = new JFrame("Test");
|
||||
frame.getContentPane().add(field.getComponent());
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
frame.setVisible(false);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDefaultState() {
|
||||
assertNull(field.getValue());// no value
|
||||
assertEquals(0, field.getIntValue());// the "int value" return for null is 0
|
||||
assertEquals(0, field.getLongValue());
|
||||
assertTrue(!field.isHexMode());
|
||||
assertNull(field.getMaxValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeValidDecimalNumber() {
|
||||
triggerText(textField, "123");
|
||||
assertEquals(123, field.getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testTypeValidHexNumber() {
|
||||
triggerText(textField, "0x2abcdef");
|
||||
assertEquals(0x2abcdef, field.getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidCharsIgnored() {
|
||||
triggerText(textField, "123ghijklmnopqrstuvwxyz4");
|
||||
assertEquals(1234, field.getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHexCharsIgnoredInDecimalMode() {
|
||||
assertTrue(!field.isHexMode());
|
||||
triggerText(textField, "123ghijklmnopqrstuvwxyz4");
|
||||
assertEquals(1234, field.getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testXchangesHexMode() {
|
||||
assertTrue(!field.isHexMode());
|
||||
triggerText(textField, "0");
|
||||
assertTrue(!field.isHexMode());
|
||||
triggerText(textField, "x");
|
||||
assertTrue(field.isHexMode());
|
||||
triggerBackspaceKey(textField);
|
||||
assertTrue(!field.isHexMode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHexModeWithoutPrefix() {
|
||||
triggerText(textField, "abc");// not allowed when using hex prefix, so expect empty
|
||||
assertEquals(null, field.getValue());
|
||||
|
||||
field.setAllowsHexPrefix(false);
|
||||
field.setHexMode();
|
||||
triggerText(textField, "abc");
|
||||
assertEquals(0xabc, field.getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegative() {
|
||||
triggerText(textField, "-123");
|
||||
assertEquals(-123, field.getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeHex() {
|
||||
triggerText(textField, "-0xa");
|
||||
assertEquals(-10, field.getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeNotAllowed() {
|
||||
field.setAllowNegativeValues(false);
|
||||
triggerText(textField, "-123");
|
||||
assertEquals(123, field.getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetNegativeWithCurrentNegativeValue() {
|
||||
field.setValue(-123);
|
||||
field.setAllowNegativeValues(false);
|
||||
assertEquals(null, field.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMax() {
|
||||
field.setMaxValue(BigInteger.valueOf(13l));
|
||||
triggerText(textField, "12");
|
||||
assertEquals(12, field.getIntValue());
|
||||
|
||||
field.setValue(null);
|
||||
triggerText(textField, "13");
|
||||
assertEquals(13, field.getIntValue());
|
||||
|
||||
field.setValue(null);
|
||||
triggerText(textField, "14");// four should be ignored
|
||||
assertEquals(1, field.getIntValue());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetMaxToValueSmallerThanCurrent() {
|
||||
field.setValue(500);
|
||||
field.setMaxValue(BigInteger.valueOf(400));
|
||||
assertEquals(400, field.getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxInHex() {
|
||||
field.setMaxValue(BigInteger.valueOf(0xd));
|
||||
triggerText(textField, "0xc");
|
||||
assertEquals(12, field.getIntValue());
|
||||
|
||||
field.setValue(null);
|
||||
triggerText(textField, "0xd");
|
||||
assertEquals(13, field.getIntValue());
|
||||
|
||||
field.setValue(null);
|
||||
triggerText(textField, "0xe");// e should be ignored
|
||||
assertEquals(0, field.getIntValue());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSwitchingHexMode() {
|
||||
field.setValue(255);
|
||||
assertEquals("255", field.getText());
|
||||
field.setHexMode();
|
||||
assertEquals("0xff", field.getText());
|
||||
field.setDecimalMode();
|
||||
assertEquals("255", field.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangeListenerAfterValidInput() {
|
||||
TestChangeListener listener = new TestChangeListener();
|
||||
field.addChangeListener(listener);
|
||||
|
||||
triggerText(textField, "123");
|
||||
assertEquals(3, listener.count);
|
||||
assertEquals(1, listener.values.get(0));
|
||||
assertEquals(12, listener.values.get(1));
|
||||
assertEquals(123, listener.values.get(2));
|
||||
|
||||
triggerBackspaceKey(textField);
|
||||
assertEquals(12, listener.values.get(3));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testChangeListenerAfterSwitchingModes() {
|
||||
triggerText(textField, "123");
|
||||
|
||||
TestChangeListener listener = new TestChangeListener();
|
||||
field.addChangeListener(listener);
|
||||
|
||||
setHexMode();
|
||||
|
||||
assertEquals(2, listener.count);
|
||||
assertEquals(123, listener.values.get(1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNegativeHexFromValue() {
|
||||
field.setValue(-255);
|
||||
setHexMode();
|
||||
assertEquals("-0xff", field.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNullValue() {
|
||||
field.setValue(12);
|
||||
assertEquals("12", field.getText());
|
||||
field.setValue(null);
|
||||
assertEquals("", field.getText());
|
||||
assertEquals(0, field.getIntValue());
|
||||
assertEquals(0l, field.getLongValue());
|
||||
assertEquals(null, field.getValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHexValueInDontRequireHexPrefixMode() {
|
||||
field.setAllowsHexPrefix(false);
|
||||
field.setHexMode();
|
||||
field.setValue(255);
|
||||
assertEquals("ff", field.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetNotAllowNegativeModeWhileCurrentValueIsNegative() {
|
||||
field.setValue(-10);
|
||||
field.setAllowNegativeValues(false);
|
||||
assertEquals("", field.getText());
|
||||
assertEquals(0, field.getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSetLongValue() {
|
||||
field.setValue(100L);
|
||||
assertEquals(100L, field.getLongValue());
|
||||
assertEquals(100, field.getIntValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSettingNegativeNumberWhenNegativesArentAllowed() {
|
||||
field.setValue(10);
|
||||
field.setAllowNegativeValues(false);
|
||||
field.setValue(-10);
|
||||
assertEquals("", field.getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUseHexPrefixUpdatesTextField() {
|
||||
field.setAllowsHexPrefix(false);
|
||||
field.setHexMode();
|
||||
field.setValue(255);
|
||||
assertEquals("ff", field.getText());
|
||||
field.setAllowsHexPrefix(true);
|
||||
assertEquals("0xff", field.getText());
|
||||
}
|
||||
|
||||
private void setHexMode() {
|
||||
runSwing(() -> field.setHexMode());
|
||||
waitForSwing();
|
||||
}
|
||||
|
||||
class TestChangeListener implements ChangeListener {
|
||||
volatile int count;
|
||||
private AtomicIntegerArray values = new AtomicIntegerArray(10);
|
||||
|
||||
@Override
|
||||
public void stateChanged(ChangeEvent e) {
|
||||
values.set(count++, field.getIntValue());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,67 @@
|
|||
/* ###
|
||||
* IP: GHIDRA
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package ghidra.util.task;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import docking.test.AbstractDockingTest;
|
||||
import generic.test.AbstractGenericTest;
|
||||
|
||||
public class TaskMonitorSplitterTest extends AbstractDockingTest {
|
||||
TaskMonitor baseMonitor;
|
||||
|
||||
public TaskMonitorSplitterTest() {
|
||||
super();
|
||||
baseMonitor = new TaskMonitorComponent();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBasicUse() {
|
||||
TaskMonitor[] monitors = TaskMonitorSplitter.splitTaskMonitor(baseMonitor, 4);
|
||||
|
||||
monitors[0].initialize(100);
|
||||
monitors[0].setProgress(1);
|
||||
assertEquals(1, monitors[0].getProgress());
|
||||
assertEquals(TaskMonitorSplitter.MONITOR_SIZE / 400, baseMonitor.getProgress());
|
||||
|
||||
monitors[0].incrementProgress(1);
|
||||
assertEquals(2 * TaskMonitorSplitter.MONITOR_SIZE / 400, baseMonitor.getProgress());
|
||||
|
||||
monitors[0].setProgress(10);
|
||||
assertEquals(10 * TaskMonitorSplitter.MONITOR_SIZE / 400, baseMonitor.getProgress());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMaxSettings() {
|
||||
TaskMonitor[] monitors = TaskMonitorSplitter.splitTaskMonitor(baseMonitor, 4);
|
||||
|
||||
monitors[0].initialize(100);
|
||||
monitors[0].setProgress(50);
|
||||
assertEquals(50 * TaskMonitorSplitter.MONITOR_SIZE / 400, baseMonitor.getProgress());
|
||||
|
||||
monitors[0].setMaximum(25);
|
||||
assertEquals(25, monitors[0].getMaximum());
|
||||
assertEquals(TaskMonitorSplitter.MONITOR_SIZE / 4, baseMonitor.getProgress());
|
||||
|
||||
monitors[0].setMaximum(100);
|
||||
assertEquals(25 * TaskMonitorSplitter.MONITOR_SIZE / 400, baseMonitor.getProgress());
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue