Search in sources :

Example 1 with Window

use of elemental2.dom.Window in project kie-wb-common by kiegroup.

the class PrintHelperTest method testCopyStylesFromWindow.

@Test
public void testCopyStylesFromWindow() {
    final HTMLDocument document = mock(HTMLDocument.class);
    final HTMLDocument topDocument = mock(HTMLDocument.class);
    final Window window = mock(Window.class);
    final Window topWindow = mock(Window.class);
    final Element element = mock(Element.class);
    final Element head = mock(Element.class);
    final NodeList<Element> parentStyles = spy(new NodeList<>());
    parentStyles.length = 1;
    window.top = topWindow;
    element.innerHTML = ".page { background: red }";
    doReturn(element).when(parentStyles).item(0);
    doReturn(topDocument).when(helper).getWindowDocument(topWindow);
    doReturn(element).when(helper).asElement(element);
    doReturn(head).when(helper).asElement(head);
    doReturn(mock(Element.class)).when(helper).createElement("style");
    when(document.querySelector("head")).thenReturn(head);
    when(topDocument.querySelectorAll("style")).thenReturn(parentStyles);
    helper.copyStylesFromWindow(document, window);
    verify(head).appendChild(elementArgumentCaptor.capture());
    final Element copiedStyle = elementArgumentCaptor.getValue();
    assertEquals(".page { background: red }", copiedStyle.innerHTML);
}
Also used : Window(elemental2.dom.Window) HTMLDocument(elemental2.dom.HTMLDocument) Element(elemental2.dom.Element) HTMLElement(elemental2.dom.HTMLElement) HTMLBodyElement(elemental2.dom.HTMLBodyElement) Test(org.junit.Test)

Example 2 with Window

use of elemental2.dom.Window in project kie-wb-common by kiegroup.

the class PrintHelper method copyStylesFromWindow.

void copyStylesFromWindow(final HTMLDocument printDocument, final Window window) {
    final HTMLDocument topDocument = getWindowDocument(window.top);
    final NodeList<Element> parentStyles = topDocument.querySelectorAll("style");
    final Element documentHead = asElement(printDocument.querySelector("head"));
    for (int i = 0; i < parentStyles.length; i++) {
        final Element copiedStyle = createElement("style");
        copiedStyle.innerHTML = asElement(parentStyles.item(i)).innerHTML;
        documentHead.appendChild(copiedStyle);
    }
}
Also used : HTMLDocument(elemental2.dom.HTMLDocument) Element(elemental2.dom.Element) HTMLElement(elemental2.dom.HTMLElement)

Example 3 with Window

use of elemental2.dom.Window in project kie-wb-common by kiegroup.

the class PrintHelperTest method testPrint.

@Test
public void testPrint() {
    final HTMLElement element = mock(HTMLElement.class);
    final HTMLDocument printDocument = mock(HTMLDocument.class);
    final Window globalWindow = mock(Window.class);
    final Window printWindow = mock(Window.class);
    doReturn(globalWindow).when(helper).getGlobalWindow();
    doReturn(printDocument).when(helper).getWindowDocument(printWindow);
    doNothing().when(helper).writeElementIntoDocument(any(), any());
    doNothing().when(helper).changeMediaAttributesToAll(any());
    doNothing().when(helper).copyStylesFromWindow(any(), any());
    doNothing().when(helper).setupPrintCommandOnPageLoad(any(), any());
    when(globalWindow.open("", "_blank")).thenReturn(printWindow);
    helper.print(element);
    verify(helper).writeElementIntoDocument(element, printDocument);
    verify(helper).changeMediaAttributesToAll(printDocument);
    verify(helper).copyStylesFromWindow(printDocument, globalWindow);
    verify(helper).setupPrintCommandOnPageLoad(printDocument, printWindow);
}
Also used : Window(elemental2.dom.Window) HTMLElement(elemental2.dom.HTMLElement) HTMLDocument(elemental2.dom.HTMLDocument) Test(org.junit.Test)

Example 4 with Window

use of elemental2.dom.Window in project kie-wb-common by kiegroup.

the class PrintHelperTest method testSetupPrintCommandOnPageLoad.

@Test
public void testSetupPrintCommandOnPageLoad() {
    final HTMLDocument document = mock(HTMLDocument.class);
    final Window window = mock(Window.class);
    document.body = mock(HTMLBodyElement.class);
    doNothing().when(helper).setTimeout(any(), anyInt());
    helper.setupPrintCommandOnPageLoad(document, window);
    document.body.onload.onInvoke(mock(Event.class));
    verify(helper).setTimeout(commandArgumentCaptor.capture(), eq(10));
    commandArgumentCaptor.getValue().execute();
    verify(window).focus();
    verify(window).print();
    verify(window).close();
}
Also used : Window(elemental2.dom.Window) HTMLDocument(elemental2.dom.HTMLDocument) Event(elemental2.dom.Event) HTMLBodyElement(elemental2.dom.HTMLBodyElement) Test(org.junit.Test)

Example 5 with Window

use of elemental2.dom.Window in project kie-wb-common by kiegroup.

the class PrintHelper method print.

public void print(final HTMLElement element) {
    final Window globalWindow = getGlobalWindow();
    final Window printWindow = globalWindow.open("", "_blank");
    final HTMLDocument printDocument = getWindowDocument(printWindow);
    writeElementIntoDocument(element, printDocument);
    changeMediaAttributesToAll(printDocument);
    copyStylesFromWindow(printDocument, globalWindow);
    setupPrintCommandOnPageLoad(printDocument, printWindow);
}
Also used : Window(elemental2.dom.Window) HTMLDocument(elemental2.dom.HTMLDocument)

Aggregations

HTMLDocument (elemental2.dom.HTMLDocument)5 Window (elemental2.dom.Window)4 HTMLElement (elemental2.dom.HTMLElement)3 Test (org.junit.Test)3 Element (elemental2.dom.Element)2 HTMLBodyElement (elemental2.dom.HTMLBodyElement)2 Event (elemental2.dom.Event)1