/src/test/java/com/wet/wired/jrc/frame/compression/RunLengthThenGZCompressorTest.java

http://java-remote-control.googlecode.com/ · Java · 81 lines · 56 code · 25 blank · 0 comment · 3 complexity · 048eca2260a6b9a38dbb8a21f43c719a MD5 · raw file

  1. package com.wet.wired.jrc.frame.compression;
  2. import java.awt.Canvas;
  3. import java.awt.Image;
  4. import java.io.ByteArrayInputStream;
  5. import java.io.ByteArrayOutputStream;
  6. import java.io.IOException;
  7. import junit.framework.TestCase;
  8. import com.wet.wired.jrc.frame.Frame;
  9. public class RunLengthThenGZCompressorTest extends TestCase {
  10. public Frame blankFrame;
  11. public Frame blueFrame;
  12. public Frame newFrame;
  13. public void setUp() {
  14. blankFrame = new Frame(100,100);
  15. blankFrame.setAllPixels(0xFF000000);
  16. blueFrame = new Frame(100,100);
  17. blueFrame.setAllPixels(0xFF0000FF);
  18. newFrame = new Frame(100,100);
  19. }
  20. public void testCompressedSizeSmallerThanUncompressedSize() throws IOException {
  21. RunLengthThenGZCompressor rlgz = new RunLengthThenGZCompressor();
  22. Frame lastFrame = blankFrame;
  23. Frame currentFrame = blueFrame;
  24. ByteArrayOutputStream out = new ByteArrayOutputStream();
  25. rlgz.compressFrame(lastFrame, currentFrame, out);
  26. if(out.toByteArray().length > currentFrame.getDataSize()*4) {
  27. fail("Compressed size greater than original size");
  28. }
  29. }
  30. public void testCompressedSizeEqualsReportedSize() throws IOException {
  31. RunLengthThenGZCompressor rlgz = new RunLengthThenGZCompressor();
  32. Frame lastFrame = blankFrame;
  33. Frame currentFrame = blueFrame;
  34. ByteArrayOutputStream out = new ByteArrayOutputStream();
  35. int size = rlgz.compressFrame(lastFrame, currentFrame, out);
  36. assertEquals(out.toByteArray().length, size);
  37. }
  38. public void testDecompressedFrameSameAsCompressedFrame() throws IOException {
  39. RunLengthThenGZCompressor rlgz = new RunLengthThenGZCompressor();
  40. Frame lastFrame = blankFrame;
  41. Frame currentFrame = blueFrame;
  42. ByteArrayOutputStream out = new ByteArrayOutputStream();
  43. int size = rlgz.compressFrame(lastFrame, currentFrame, out);
  44. ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
  45. rlgz.decompressFrame(in, size, lastFrame, newFrame);
  46. assertEquals(currentFrame.getDataSize(),newFrame.getDataSize());
  47. for(int i=0;i<currentFrame.getDataSize();i++) {
  48. if(currentFrame.getData()[i]!=newFrame.getData()[i]) {
  49. fail("Decompressed data "+currentFrame.getData()[i]+" did not match "+newFrame.getData()[i]+" at index "+i);
  50. }
  51. }
  52. }
  53. public static Image loadImage(String fileName) {
  54. return new Canvas().getToolkit().createImage( fileName );
  55. }
  56. }