/**
 *
 */
package com.opera.core.systems;


import static org.junit.Assert.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.zip.Adler32;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class ScreenshotTest extends TestBase {

  private OperaWebElement text;
  private OperaWebElement radioLittle;
  private OperaWebElement radioSome;
  private OperaWebElement radioLots;

  @Before
  public void setUp() throws Exception {
    getFixture("test.html");

    text = (OperaWebElement) driver.findElementById("input_email");
    radioSome = (OperaWebElement) driver.findElementById("radio_some");
    radioLittle = (OperaWebElement) driver.findElementById("radio_little");
    radioLots = (OperaWebElement) driver.findElementById("radio_lots");
  }

  private static int digest(String filename) throws IOException {
    FileInputStream fis= new FileInputStream(filename);
    byte[] data = new byte[fis.available()];

    int i = 0;
    int c;
    while ((c = fis.read()) != -1) {
      data[i++] = (byte) c;
    }
    fis.close();

    final Adler32 digester = new Adler32();
    digester.update(data);

    return (int) digester.getValue();
  }

  @Test
  public void testElementsSame() throws Exception {
    String one = radioLittle.saveScreenshot("/tmp/one.png");
    String two = radioSome.saveScreenshot("/tmp/two.png");

    Assert.assertEquals(one, two);
  }

  @Test
  public void testWrittenFilesSame() throws Exception {
    String one = "/tmp/one.png", two = "/tmp/two.png";

    radioLittle.saveScreenshot(one);
    radioSome.saveScreenshot(two);

    Assert.assertEquals(digest(one), digest(two));
  }

  @Test
  public void testElementsNotSame() throws Exception {
    String one = radioLittle.saveScreenshot("/tmp/one.png");
    String two = radioLots.saveScreenshot("/tmp/two.png");

    Assert.assertNotSame(one, two);
  }

  @Test
  public void testWrittenFilesNotSame() throws Exception {
    String one = "/tmp/one.png", two = "/tmp/two.png";

    radioLittle.saveScreenshot(one);
    radioLots.saveScreenshot(two);

    Assert.assertNotSame(digest(one), digest(two));
  }

  @Test
  public void testFlash() throws Exception {
    getFixture("flash.html");
    OperaWebElement img = (OperaWebElement) driver.findElementById("img_container");
    OperaWebElement flash = (OperaWebElement) driver.findElementById("flash_container");

    String imgMD5 = img.saveScreenshot("/tmp/one.png");
    String flashMD5 = flash.saveScreenshot("/tmp/two.png");

    Assert.assertEquals(imgMD5, flashMD5);
  }

  @Test
  public void testTimout() throws Exception {
    getFixture("timer.html");
    OperaWebElement text = (OperaWebElement) driver.findElementById("one");

    driver.saveScreenshot(timeout, hashes)

    String original = text.saveScreenshot("/tmp/one.png");
    String changed = text.saveScreenshot("/tmp/two.png", 2000);

    Assert.assertNotSame(original, changed);
  }

  @Test
  public void testHashes() throws Exception {
    String md5 = text.saveScreenshot("/tmp/one.png");
    text.saveScreenshot("/tmp/two.png", 0, true, md5);

    File one = new File("/tmp/one.png");
    File two = new File("/tmp/two.png");

    Assert.assertNotSame(0, one.length());
    Assert.assertEquals(0, two.length());
  }
}

@@ -259,16 +259,70 @@
 .Test;%0A%0A
+import com.opera.core.systems.model.ScreenShotReply;%0A%0A
 public c
@@ -2815,52 +2815,8 @@
 );%0A%0A
-    driver.saveScreenshot(timeout, hashes)%0A%0A
     
@@ -3224,32 +3224,96 @@
 tmp/two.png%22);%0A%0A
+    // Confirm that a png is not returned when the hash matches%0A
     Assert.asser
@@ -3384,11 +3384,532 @@
 ());%0A  %7D
+%0A%0A  @Test%0A  public void testRealPng() throws Exception %7B%0A    ScreenShotReply reply = driver.saveScreenshot(0);%0A    byte%5B%5D png = reply.getPng();%0A%0A    System.out.println(%22bytes:%22);%0A    System.out.println(png);%0A%0A    Assert.assertTrue(%22PNG magic bytes match%22,%0A       png%5B0%5D == (byte) 0x89 &&%0A       png%5B1%5D == (byte) 0x50 &&%0A       png%5B2%5D == (byte) 0x4E &&%0A       png%5B3%5D == (byte) 0x47 &&%0A       png%5B4%5D == (byte) 0x0D &&%0A       png%5B5%5D == (byte) 0x0A &&%0A       png%5B6%5D == (byte) 0x1A &&%0A       png%5B7%5D == (byte) 0x0A%0A    );%0A%0A%0A  %7D
 %0A%7D%0A

Add test to check that returned image is a png