Skip to content

Commit

Permalink
Small unit tests for WebcamPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
sarxos committed Sep 14, 2017
1 parent a04d1ca commit b64caaf
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,37 @@ 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() {

if (!open) {
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;
}

Expand All @@ -70,6 +86,7 @@ public void close() {
open = false;
}

@Override
public boolean isOpen() {
return open;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
public class DummyDriver implements WebcamDriver {

private static final List<WebcamDevice> DEVICES = new ArrayList<WebcamDevice>(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;
}
Expand All @@ -38,5 +38,4 @@ public List<WebcamDevice> getDevices() {
public boolean isThreadSafe() {
return false;
}

}

0 comments on commit b64caaf

Please sign in to comment.