package com.codeborne.selenide.impl;

import org.junit.Test;

import java.io.File;
import java.util.List;

import static java.io.File.separatorChar;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

public class ScreenShotLaboratoryTest {
  ScreenShotLaboratory screenshots = new ScreenShotLaboratory() {
    @Override protected long timestamp() {
      return 12356789L;
    }

    @Override
    public String takeScreenShot(String fileName) {
      addToHistory(new File(fileName));
      return fileName;
    }
  };

  @Test
  public void composesScreenshotNameFromTestClassAndMethod() {
    String expected = "MyTest/helloWorldTest.12356789".replace('/', separatorChar);
    assertEquals(expected, screenshots.takeScreenShot("MyTest", "helloWorldTest"));

    String expectedFileName = ("com/codeborne/selenide/integrationtests/SelenideMethodsTest/" +
        "userCanListMatchingSubElements.12356789").replace('/', separatorChar);
    assertEquals(expectedFileName,
        screenshots.takeScreenShot("com.codeborne.selenide." +
            "integrationtests.SelenideMethodsTest", "userCanListMatchingSubElements"));
  }

  @Test
  public void composesScreenshotNameAsTimestampPlusCounter() {
    assertEquals("12356789.0", screenshots.takeScreenShot());
    assertEquals("12356789.1", screenshots.takeScreenShot());
    assertEquals("12356789.2", screenshots.takeScreenShot());
  }

  @Test
  public void screenshotsCanByGroupedByTests() {
    screenshots.startContext("ui/MyTest/test_some_method/");
    assertEquals("ui/MyTest/test_some_method/12356789.0", screenshots.takeScreenShot());
    assertEquals("ui/MyTest/test_some_method/12356789.1", screenshots.takeScreenShot());
    assertEquals("ui/MyTest/test_some_method/12356789.2", screenshots.takeScreenShot());

    List<File> contextScreenshots = screenshots.finishContext();
    assertEquals(3, contextScreenshots.size());
    assertEquals("ui/MyTest/test_some_method/12356789.0", contextScreenshots.get(0).toString());
    assertEquals("ui/MyTest/test_some_method/12356789.1", contextScreenshots.get(1).toString());
    assertEquals("ui/MyTest/test_some_method/12356789.2", contextScreenshots.get(2).toString());
  }

  @Test
  public void collectsAllScreenshots() {
    screenshots.startContext("ui/MyTest/test_some_method/");
    screenshots.takeScreenShot();
    screenshots.takeScreenShot();
    screenshots.finishContext();
    screenshots.startContext("ui/YourTest/test_another_method/");
    screenshots.takeScreenShot();
    screenshots.finishContext();
    screenshots.takeScreenShot();
    screenshots.takeScreenShot();

    List<File> allScreenshots = screenshots.getScreenshots();
    assertEquals(5, allScreenshots.size());
    assertEquals("ui/MyTest/test_some_method/12356789.0", allScreenshots.get(0).toString());
    assertEquals("ui/MyTest/test_some_method/12356789.1", allScreenshots.get(1).toString());
    assertEquals("ui/YourTest/test_another_method/12356789.2", allScreenshots.get(2).toString());
    assertEquals("12356789.3", allScreenshots.get(3).toString());
    assertEquals("12356789.4", allScreenshots.get(4).toString());
  }

  @Test
  public void canGetLastScreenshot() {
    assertNull(screenshots.getLastScreenshot());
    
    screenshots.takeScreenShot();
    assertEquals("12356789.0", screenshots.getLastScreenshot().toString());

    screenshots.takeScreenShot();
    assertEquals("12356789.1", screenshots.getLastScreenshot().toString());

    screenshots.takeScreenShot();
    assertEquals("12356789.2", screenshots.getLastScreenshot().toString());
  }
}

@@ -1929,32 +1929,41 @@
    assertEquals(
+new File(
 %22ui/MyTest/test_
@@ -1985,16 +1985,17 @@
 56789.0%22
+)
 , contex
@@ -2009,35 +2009,24 @@
 shots.get(0)
-.toString()
 );%0A    asser
@@ -2025,32 +2025,41 @@
    assertEquals(
+new File(
 %22ui/MyTest/test_
@@ -2081,16 +2081,17 @@
 56789.1%22
+)
 , contex
@@ -2105,35 +2105,24 @@
 shots.get(1)
-.toString()
 );%0A    asser
@@ -2121,32 +2121,41 @@
    assertEquals(
+new File(
 %22ui/MyTest/test_
@@ -2177,16 +2177,17 @@
 56789.2%22
+)
 , contex
@@ -2201,35 +2201,24 @@
 shots.get(2)
-.toString()
 );%0A  %7D%0A%0A  @T
@@ -2741,32 +2741,41 @@
    assertEquals(
+new File(
 %22ui/MyTest/test_
@@ -2797,16 +2797,17 @@
 56789.0%22
+)
 , allScr
@@ -2821,27 +2821,16 @@
 s.get(0)
-.toString()
 );%0A    a
@@ -2833,32 +2833,41 @@
    assertEquals(
+new File(
 %22ui/MyTest/test_
@@ -2889,16 +2889,17 @@
 56789.1%22
+)
 , allScr
@@ -2913,27 +2913,16 @@
 s.get(1)
-.toString()
 );%0A    a
@@ -2929,24 +2929,33 @@
 ssertEquals(
+new File(
 %22ui/YourTest
@@ -2986,16 +2986,17 @@
 56789.2%22
+)
 , allScr
@@ -3010,27 +3010,16 @@
 s.get(2)
-.toString()
 );%0A    a

fix of file paths comparison in ScreenShotLaboratoryTest