FreenectTest.java
Go to the documentation of this file.
1 package org.openkinect.freenect;
2 
3 import static org.hamcrest.Matchers.*;
4 import static org.junit.Assert.*;
5 import static org.junit.Assume.*;
6 
7 import java.nio.ByteBuffer;
8 import java.util.ArrayList;
9 import java.util.List;
10 
11 import org.hamcrest.collection.IsEmptyCollection;
12 import org.junit.AfterClass;
13 import org.junit.BeforeClass;
14 import org.junit.Test;
16 
17 public class FreenectTest {
18  static Context ctx;
19  static Device dev;
20 
21  @BeforeClass
22  public static void initKinect() {
23  ctx = Freenect.createContext();
24  ctx.setLogHandler(new Jdk14LogHandler());
26  if (ctx.numDevices() > 0) {
27  dev = ctx.openDevice(0);
28  } else {
29  System.err.println("WARNING: No kinects detected, hardware tests will be implicitly passed.");
30  }
31  }
32 
33  @AfterClass
34  public static void shutdownKinect() {
35  if (ctx != null)
36  if (dev != null) {
37  dev.close();
38  }
39  ctx.shutdown();
40  }
41 
42  protected void moveAndWait(Device device, int degrees) throws InterruptedException {
43  device.refreshTiltState();
44  if (device.getTiltAngle() >= (degrees - 2) && device.getTiltAngle() <= (degrees + 2)) {
45  return;
46  }
47  assertThat(device.setTiltAngle(degrees), is(0));
48 
49  while (device.getTiltStatus() == TiltStatus.STOPPED) {
50  device.refreshTiltState();
51  }
52 
53  if (device.getTiltStatus() == TiltStatus.MOVING) {
54  while (device.getTiltStatus() == TiltStatus.MOVING) {
55  device.refreshTiltState();
56  }
57  }
58 
59  if (device.getTiltStatus() == TiltStatus.STOPPED) {
60  while (device.getTiltAngle() < -32) {
61  device.refreshTiltState();
62  }
63  }
64  }
65 
66  @Test(timeout = 10000)
67  public void testSetTiltAngle() throws InterruptedException {
68  assumeThat(dev, is(not(nullValue())));
69 
71  dev.refreshTiltState();
72 
73  moveAndWait(dev, 0);
74  assertThat(dev.getTiltAngle(), is(closeTo(0, 2)));
75 
76  moveAndWait(dev, 20);
77  assertThat(dev.getTiltAngle(), is(closeTo(20, 2)));
78 
79  moveAndWait(dev, -20);
80  assertThat(dev.getTiltAngle(), is(closeTo(-20, 2)));
81 
82  moveAndWait(dev, 0);
83  assertThat(dev.getTiltAngle(), is(closeTo(0, 2)));
84  }
85 
86  @Test(timeout = 5000)
87  public void testLogEvents() throws InterruptedException {
88  assumeThat(dev, is(not(nullValue())));
89 
91  final List<String> messages = new ArrayList<String>();
92  ctx.setLogHandler(new LogHandler() {
93  @Override
94  public void onMessage(Device dev, LogLevel level, String msg) {
95  messages.add(msg);
96  }
97  });
98  dev.startVideo(new VideoHandler() {
99  @Override
100  public void onFrameReceived(FrameMode mode, ByteBuffer frame, int timestamp) {
101  }
102  });
103  Thread.sleep(500);
104  dev.stopVideo();
106  ctx.setLogHandler(new Jdk14LogHandler());
107  assertThat(messages, is(not(IsEmptyCollection.<String>empty()))); // wtf hamcrest, fix this!
108  }
109 
110  @Test(timeout = 2000)
111  public void testDepth() throws InterruptedException {
112  assumeThat(dev, is(not(nullValue())));
113 
114  final Object lock = new Object();
115  final long start = System.nanoTime();
116  dev.startDepth(new DepthHandler() {
117  int frameCount = 0;
118 
119  @Override
120  public void onFrameReceived(FrameMode mode, ByteBuffer frame, int timestamp) {
121  frameCount++;
122  if (frameCount == 30) {
123  synchronized (lock) {
124  lock.notify();
125  System.out.format("Got %d depth frames in %4.2fs%n", frameCount,
126  (((double) System.nanoTime() - start) / 1000000000));
127  }
128  }
129  }
130  });
131  synchronized (lock) {
132  lock.wait(2000);
133  }
134  }
135 
136  @Test(timeout = 2000)
137  public void testVideo() throws InterruptedException {
138  assumeThat(dev, is(not(nullValue())));
139 
140  final Object lock = new Object();
141  final long start = System.nanoTime();
142  dev.startVideo(new VideoHandler() {
143  int frameCount = 0;
144 
145  @Override
146  public void onFrameReceived(FrameMode mode, ByteBuffer frame, int timestamp) {
147  frameCount++;
148  if (frameCount == 30) {
149  synchronized (lock) {
150  lock.notify();
151  System.out.format("Got %d video frames in %4.2fs%n", frameCount,
152  (((double) System.nanoTime() - start) / 1000000000));
153  }
154  }
155  }
156  });
157  synchronized (lock) {
158  lock.wait(2000);
159  }
160  }
161 }
int startDepth(DepthHandler handler)
Device openDevice(int index)
int frame
Definition: regview.c:72
void setLogLevel(LogLevel level)
void setLogHandler(LogHandler handler)
void moveAndWait(Device device, int degrees)
MyFreenectDevice * device
int startVideo(VideoHandler handler)


libfreenect
Author(s): Hector Martin, Josh Blake, Kyle Machulis, OpenKinect community
autogenerated on Mon Jun 10 2019 13:46:42