diff --git a/webcam-capture/src/test/java/com/github/sarxos/webcam/WebcamPanelTest.java b/webcam-capture/src/test/java/com/github/sarxos/webcam/WebcamPanelTest.java new file mode 100644 index 00000000..93436c98 --- /dev/null +++ b/webcam-capture/src/test/java/com/github/sarxos/webcam/WebcamPanelTest.java @@ -0,0 +1,149 @@ +package com.github.sarxos.webcam; + +import java.awt.Dimension; +import java.awt.image.BufferedImage; + +import javax.swing.JFrame; + +import org.assertj.core.api.Assertions; +import org.junit.Test; + +import com.github.sarxos.webcam.WebcamPanel.DrawMode; +import com.github.sarxos.webcam.ds.test.DummyDriver; + + +public class WebcamPanelTest { + + @Test + public void test_size() throws InterruptedException { + + Webcam.setDriver(new DummyDriver()); + + final Webcam w = Webcam.getDefault(); + final WebcamPanel p = new WebcamPanel(w); + + w.open(); + p.repaint(); + + final BufferedImage bi = w.getImage(); + final Dimension d = p.getPreferredSize(); + + Assertions + .assertThat(d.getWidth()) + .isEqualTo(bi.getWidth()); + Assertions + .assertThat(d.getHeight()) + .isEqualTo(bi.getHeight()); + + p.stop(); + w.close(); + } + + @Test + public void test_sizeSpecified() throws InterruptedException { + + Webcam.setDriver(new DummyDriver()); + + final Webcam w = Webcam.getDefault(); + final WebcamPanel p = new WebcamPanel(w, new Dimension(256, 345), false); + + w.open(); + p.repaint(); + + final Dimension d = p.getPreferredSize(); + + Assertions + .assertThat(d.getWidth()) + .isEqualTo(256); + Assertions + .assertThat(d.getHeight()) + .isEqualTo(345); + + p.stop(); + w.close(); + } + + @Test + public void test_modeFill() throws InterruptedException { + + Webcam.setDriver(new DummyDriver()); + + final Webcam w = Webcam.getDefault(); + w.open(); + + final WebcamPanel p = new WebcamPanel(w, new Dimension(256, 345), false); + p.setDrawMode(DrawMode.FILL); + p.start(); + + Assertions + .assertThat(p.getDrawMode()) + .isEqualTo(DrawMode.FILL); + + final JFrame frame = new JFrame(); + frame.setContentPane(p); + frame.pack(); + + Thread.sleep(100); + + frame.dispose(); + + p.stop(); + w.close(); + } + + @Test + public void test_modeFit() throws InterruptedException { + + Webcam.setDriver(new DummyDriver()); + + final Webcam w = Webcam.getDefault(); + w.open(); + + final WebcamPanel p = new WebcamPanel(w, new Dimension(256, 345), false); + p.setDrawMode(DrawMode.FIT); + p.start(); + + Assertions + .assertThat(p.getDrawMode()) + .isEqualTo(DrawMode.FIT); + + final JFrame frame = new JFrame(); + frame.setContentPane(p); + frame.pack(); + + Thread.sleep(100); + + frame.dispose(); + + p.stop(); + w.close(); + } + + @Test + public void test_modeNone() throws InterruptedException { + + Webcam.setDriver(new DummyDriver()); + + final Webcam w = Webcam.getDefault(); + w.open(); + + final WebcamPanel p = new WebcamPanel(w, new Dimension(256, 345), false); + p.setDrawMode(DrawMode.NONE); + p.start(); + + Assertions + .assertThat(p.getDrawMode()) + .isEqualTo(DrawMode.NONE); + + final JFrame frame = new JFrame(); + frame.setContentPane(p); + frame.pack(); + + Thread.sleep(100); + + frame.dispose(); + + p.stop(); + w.close(); + } +} diff --git a/webcam-capture/src/test/java/com/github/sarxos/webcam/ds/test/DummyDevice.java b/webcam-capture/src/test/java/com/github/sarxos/webcam/ds/test/DummyDevice.java index e9979fcf..ef1a33d0 100644 --- a/webcam-capture/src/test/java/com/github/sarxos/webcam/ds/test/DummyDevice.java +++ b/webcam-capture/src/test/java/com/github/sarxos/webcam/ds/test/DummyDevice.java @@ -42,6 +42,12 @@ public void setResolution(Dimension size) { this.size = size; } + private int mx = 1; + private int my = 1; + private int r = 10; + private int x = r; + private int y = r; + @Override public BufferedImage getImage() { @@ -49,14 +55,24 @@ public BufferedImage getImage() { throw new WebcamException("Not open"); } - BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB); - Graphics2D g2 = bi.createGraphics(); + final BufferedImage bi = new BufferedImage(size.width, size.height, BufferedImage.TYPE_INT_RGB); + final Graphics2D g2 = bi.createGraphics(); g2.setColor(Color.RED); g2.fillRect(0, 0, size.width, size.height); - g2.drawString(getName(), 20, 20); + g2.setColor(Color.BLACK); + g2.drawString(getName(), 10, 20); + g2.setColor(Color.WHITE); + g2.drawOval(x += mx, y += my, r, r); g2.dispose(); bi.flush(); + if (x <= 0 + r || x >= size.width - r) { + mx = -mx; + } + if (y <= 0 + r || y >= size.height - r) { + my = -my; + } + return bi; } @@ -70,6 +86,7 @@ public void close() { open = false; } + @Override public boolean isOpen() { return open; } diff --git a/webcam-capture/src/test/java/com/github/sarxos/webcam/ds/test/DummyDriver.java b/webcam-capture/src/test/java/com/github/sarxos/webcam/ds/test/DummyDriver.java index 8258e7ca..3b44a1a5 100644 --- a/webcam-capture/src/test/java/com/github/sarxos/webcam/ds/test/DummyDriver.java +++ b/webcam-capture/src/test/java/com/github/sarxos/webcam/ds/test/DummyDriver.java @@ -11,15 +11,15 @@ public class DummyDriver implements WebcamDriver { private static final List DEVICES = new ArrayList(Arrays.asList(new WebcamDevice[] { - new DummyDevice(), - new DummyDevice(), - new DummyDevice(), - new DummyDevice(), + new DummyDevice(), + new DummyDevice(), + new DummyDevice(), + new DummyDevice(), })); private static DummyDriver instance = null; - public DummyDriver() throws InstantiationException { + public DummyDriver() { if (instance == null) { instance = this; } @@ -38,5 +38,4 @@ public List getDevices() { public boolean isThreadSafe() { return false; } - }