package com.opera.core.systems;

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;

import com.opera.core.systems.model.ScreenShotReply;

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");

    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");

    // Confirm that a png is not returned when the hash matches
    Assert.assertNotSame(0, one.length());
    Assert.assertEquals(0, two.length());
  }

  @Test
  public void testRealPng() throws Exception {
    ScreenShotReply reply = driver.saveScreenshot(0);
    byte[] png = reply.getPng();

    Assert.assertTrue("PNG magic bytes match",
       png[0] == (byte) 0x89 &&
       png[1] == (byte) 0x50 &&
       png[2] == (byte) 0x4E &&
       png[3] == (byte) 0x47 &&
       png[4] == (byte) 0x0D &&
       png[5] == (byte) 0x0A &&
       png[6] == (byte) 0x1A &&
       png[7] == (byte) 0x0A
    );


  }
}

@@ -138,16 +138,45 @@
 ler32;%0A%0A
+import org.junit.AfterClass;%0A
 import o
@@ -863,16 +863,164 @@
 );%0A  %7D%0A%0A
+  @AfterClass%0A  public static void tearDownAfterClass() throws Exception %7B%0A    new File(%22one.png%22).delete();%0A    new File(%22two.png%22).delete();%0A  %7D%0A%0A
   privat
@@ -1506,37 +1506,32 @@
 saveScreenshot(%22
-/tmp/
 one.png%22);%0A    S
@@ -1560,37 +1560,32 @@
 saveScreenshot(%22
-/tmp/
 two.png%22);%0A%0A    
@@ -1694,37 +1694,32 @@
   String one = %22
-/tmp/
 one.png%22, two = 
@@ -1711,37 +1711,32 @@
 ne.png%22, two = %22
-/tmp/
 two.png%22;%0A%0A    r
@@ -1959,37 +1959,32 @@
 saveScreenshot(%22
-/tmp/
 one.png%22);%0A    S
@@ -2013,37 +2013,32 @@
 saveScreenshot(%22
-/tmp/
 two.png%22);%0A%0A    
@@ -2159,21 +2159,16 @@
  one = %22
-/tmp/
 one.png%22
@@ -2176,21 +2176,16 @@
  two = %22
-/tmp/
 two.png%22
@@ -2607,37 +2607,32 @@
 saveScreenshot(%22
-/tmp/
 one.png%22);%0A    S
@@ -2662,37 +2662,32 @@
 saveScreenshot(%22
-/tmp/
 two.png%22);%0A%0A    
@@ -2926,37 +2926,32 @@
 saveScreenshot(%22
-/tmp/
 one.png%22);%0A    S
@@ -2979,37 +2979,32 @@
 saveScreenshot(%22
-/tmp/
 two.png%22, 2000);
@@ -3147,21 +3147,16 @@
 enshot(%22
-/tmp/
 one.png%22
@@ -3183,21 +3183,16 @@
 enshot(%22
-/tmp/
 two.png%22
@@ -3234,21 +3234,16 @@
 w File(%22
-/tmp/
 one.png%22
@@ -3274,13 +3274,8 @@
 le(%22
-/tmp/
 two.
@@ -3887,15 +3887,14 @@
     );%0A%0A
-%0A
   %7D%0A%7D%0A

Save images to current directory, instead of /tmp