package hudson.plugins.emailext.plugins;

import org.junit.Test;
import static org.junit.Assert.*;

/**
 * Tests for CssInliner
 * @author <a href="https://github.com/rahulsom">Rahul Somasunderam</a>
 */
public class CssInlinerTest {
  @Test
  public void testEmailWithoutCss() {
    String input = "<html>" +
        "  <head></head>" +
        "  <body>" +
        "    <span style='color: red;'>Red text</span>" +
        "  </body>" +
        "</html>";
    String output = process(input);
    assertEquals("<html><head></head><body><span style=\"color: red;\">Red text</span></body></html>", output);
  }

  @Test
  public void testEmailWithNormalCss() {
    String input = "<html>" +
        "  <head>" +
        "    <style>" +
        "      span {font-size: 10px;}" +
        "    </style>" +
        "  </head>" +
        "  <body>" +
        "    <span style='color: red;'>Red text</span>" +
        "  </body>" +
        "</html>";
    String output = process(input);
    assertEquals("<html><head><style> span {font-size: 10px;} </style></head>" +
        "<body><span style=\"color: red;\">Red text</span></body></html>", output);
  }

  @Test
  public void testEmailWithInlinedCss() {
    String input = "<html>" +
        "  <head>" +
        "    <style data-inline='true'>" +
        "      span {font-size: 10px;}" +
        "    </style>" +
        "  </head>" +
        "  <body>" +
        "    <span style='color: red;'>Red text</span>" +
        "  </body>" +
        "</html>";
    String output = process(input);
    assertEquals("<html><head></head>" +
        "<body><span style=\"font-size: 10px; color: red;\">Red text</span></body>" +
        "</html>", output);
  }

  @Test
  public void testEmailWithMixedCss() {
    String input = "<html>" +
        "  <head>" +
        "    <style data-inline='true'>" +
        "      span {font-size: 10px;}" +
        "    </style>" +
        "    <style>" +
        "      span {font-family: Verdana;}" +
        "    </style>" +
        "  </head>" +
        "  <body>" +
        "    <span style='color: red;'>Red text</span>" +
        "  </body>" +
        "</html>";
    String output = process(input);
    assertEquals("<html>" +
        "<head><style> span {font-family: Verdana;} </style></head>" +
        "<body><span style=\"font-size: 10px; color: red;\">Red text</span></body></html>", output);
  }

  @Test
  public void testImageInliningOff() {
    String input = "<html>" +
        "  <body>" +
        "    <img src='https://updates.jenkins-ci.org/icons/blank.gif' />" +
        "  </body>" +
        "</html>";

    String output = process(input);
    assertEquals("<html><head></head><body>" +
        "<img src=\"https://updates.jenkins-ci.org/icons/blank.gif\" /></body></html>", output);

  }

  @Test
  public void testImageInliningOn() {
    String input = "<html>" +
        "  <body>" +
        "    <img src='https://updates.jenkins-ci.org/icons/blank.gif' data-inline='true'/>" +
        "  </body>" +
        "</html>";

    String output = process(input);
    assertEquals("<html><head></head><body><img src=\"data:image/gif;base64," +
        "R0lGODlhFAAWAKEAAP///8z//wAAAAAAACH+TlRoaXMgYXJ0IGlzIGluIHRoZSBwdWJsaWMgZG9t\n" +
        "YWluLiBLZXZpbiBIdWdoZXMsIGtldmluaEBlaXQuY29tLCBTZXB0ZW1iZXIgMTk5NQAh+QQBAAAB\n" +
        "ACwAAAAAFAAWAAACE4yPqcvtD6OctNqLs968+w+GSQEAOw==\n" +
        "\" data-inline=\"true\" /></body></html>", output.replaceAll("\r", "\n"));

  }
  
  @Test
  public void testNoPrettify() {
      String input = "<html><head></head>\n" +
              "<body>\n" +
              "<table border=\"1\">\n" +
              "<tbody>\n" +
              "<tr>\n" +
              "<td> <b>TEXT</b> </td>\n" +
              "<td> <b>TEXT</b><pre>\n" +
              "line\n" +
              "line<v1 />line\n" +
              "line<v1 />line\n" +
              "</pre>\n" +
              "</td>\n" +
              "</tr></tbody></table></body></html>";
      String output = new CssInliner().process(input);
      assertEquals(input, output);
  }

  private String process(String input) {
    return new CssInliner().process(input).replaceAll(" +", " ").replaceAll("\n", "").replaceAll("> *<", "><");
  }
}

@@ -3053,37 +3053,51 @@
 input);%0A    
-assertEquals(
+String unprocessedExpect = 
 %22%3Chtml%3E%3Chead
@@ -3434,32 +3434,98 @@
 %3E%3C/body%3E%3C/html%3E%22
+;%0A    %0A    assertEquals(unprocessedExpect.replaceAll(%22%5B%5Cr%5Cn%5D%22, %22%22)
 , output.replace
@@ -3533,22 +3533,23 @@
 ll(%22
+%5B
 %5Cr
+%5Cn%5D
 %22, %22
-%5Cn
 %22));%0A
-%0A
   %7D%0A

Fix for LTS 1.509.1