"""
Test IPython/Jupyter display hooks
"""

import pytest

import pikepdf


@pytest.fixture
def pal(resources):
    return pikepdf.open(resources / 'pal-1bit-trivial.pdf')


def test_display_page(pal):
    page0 = pal.pages[0]
    mimebundle = page0._repr_mimebundle_(include=None, exclude=None)
    assert 'application/pdf' in mimebundle


def test_display_image(pal):
    im0 = pal.pages[0].Resources.XObject['/Im0']
    pim = pikepdf.PdfImage(im0)
    result = pim._repr_png_()
    assert result[1:4] == b'PNG'


def test_display_pdf(pal):
    mimebundle = pal._repr_mimebundle_(
        include=['application/pdf'], exclude=['text/css']
    )
    assert 'application/pdf' in mimebundle and mimebundle['application/pdf'].startswith(
        b'%PDF'
    )


def test_object_key_completion(pal):
    page0 = pal.pages[0]
    assert '/Type' in page0._ipython_key_completions_()

@@ -44,22 +44,100 @@
 %0Aimport 
-pytest
+shutil%0Aimport subprocess%0Afrom io import BytesIO%0A%0Aimport pytest%0Afrom PIL import Image
 %0A%0Aimport
@@ -262,16 +262,20 @@
 display_
+raw_
 page(pal
@@ -273,32 +273,36 @@
 _page(pal):%0A    
+raw_
 page0 = pal.page
@@ -323,16 +323,20 @@
 undle = 
+raw_
 page0._r
@@ -355,78 +355,1469 @@
 le_(
-include=None, exclude=None)%0A    assert 'application/pdf' in mimebundle
+%0A        include=%5B'application/pdf'%5D, exclude=%5B'application/malware'%5D%0A    )%0A    assert 'application/pdf' in mimebundle%0A%0A%0Adef test_display_rich_page(pal):%0A    page0 = pikepdf.Page(pal.pages%5B0%5D)%0A    mimebundle = page0._repr_mimebundle_(%0A        include=%5B'application/pdf'%5D, exclude=%5B'application/malware'%5D%0A    )%0A    assert 'application/pdf' in mimebundle%0A%0A%0Adef test_draw_page(pal, monkeypatch):%0A    # Test page drawing error handling independent of whether mudraw is installed%0A%0A    page0 = pikepdf.Page(pal.pages%5B0%5D)%0A%0A    def raise_filenotfound(prog_args, *args, **kwargs):%0A        raise FileNotFoundError(prog_args%5B0%5D)%0A%0A    monkeypatch.setattr(pikepdf._methods, 'run', raise_filenotfound)%0A    mimebundle = page0._repr_mimebundle_(%0A        include=%5B'image/png'%5D, exclude=%5B'application/pdf'%5D%0A    )%0A    assert (%0A        'image/png' not in mimebundle%0A    ), %22Generated image/png when mudraw() was rigged to fail%22%0A%0A    def return_simple_png(prog_args, *args, **kwargs):%0A        im = Image.new('1', (1, 1))%0A        bio = BytesIO()%0A        im.save(bio, format='PNG')%0A        bio.seek(0)%0A        return subprocess.CompletedProcess(prog_args, 0, stdout=bio.read(), stderr=b'')%0A%0A    monkeypatch.setattr(pikepdf._methods, 'run', return_simple_png)%0A    mimebundle = page0._repr_mimebundle_(%0A        include=%5B'image/png'%5D, exclude=%5B'application/pdf'%5D%0A    )%0A    assert (%0A        'image/png' in mimebundle%0A    ), %22Did not generate image/png when mudraw() was rigged to succeed%22
 %0A%0A%0Ad
@@ -2348,12 +2348,74 @@
 pletions_()%0A
+    assert page0.MediaBox._ipython_key_completions_() is None%0A

improve coverage of mudraw behavior