##// END OF EJS Templates
Don't try to open temporary file twice in video embed test...
Thomas Kluyver -
Show More
@@ -1,191 +1,192 b''
1 1 # Copyright (c) IPython Development Team.
2 2 # Distributed under the terms of the Modified BSD License.
3 3
4 4 import json
5 5 import tempfile
6 6 import os
7 7 import warnings
8 8
9 9 import nose.tools as nt
10 10
11 11 from IPython.core import display
12 12 from IPython.core.getipython import get_ipython
13 from IPython.utils.tempdir import NamedFileInTemporaryDirectory
13 14 from IPython import paths as ipath
14 15
15 16 import IPython.testing.decorators as dec
16 17
17 18 def test_image_size():
18 19 """Simple test for display.Image(args, width=x,height=y)"""
19 20 thisurl = 'http://www.google.fr/images/srpr/logo3w.png'
20 21 img = display.Image(url=thisurl, width=200, height=200)
21 22 nt.assert_equal(u'<img src="%s" width="200" height="200"/>' % (thisurl), img._repr_html_())
22 23 img = display.Image(url=thisurl, width=200)
23 24 nt.assert_equal(u'<img src="%s" width="200"/>' % (thisurl), img._repr_html_())
24 25 img = display.Image(url=thisurl)
25 26 nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_())
26 27 img = display.Image(url=thisurl, unconfined=True)
27 28 nt.assert_equal(u'<img src="%s" class="unconfined"/>' % (thisurl), img._repr_html_())
28 29
29 30 def test_retina_png():
30 31 here = os.path.dirname(__file__)
31 32 img = display.Image(os.path.join(here, "2x2.png"), retina=True)
32 33 nt.assert_equal(img.height, 1)
33 34 nt.assert_equal(img.width, 1)
34 35 data, md = img._repr_png_()
35 36 nt.assert_equal(md['width'], 1)
36 37 nt.assert_equal(md['height'], 1)
37 38
38 39 def test_retina_jpeg():
39 40 here = os.path.dirname(__file__)
40 41 img = display.Image(os.path.join(here, "2x2.jpg"), retina=True)
41 42 nt.assert_equal(img.height, 1)
42 43 nt.assert_equal(img.width, 1)
43 44 data, md = img._repr_jpeg_()
44 45 nt.assert_equal(md['width'], 1)
45 46 nt.assert_equal(md['height'], 1)
46 47
47 48 def test_base64image():
48 49 display.Image("iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAWJLR0QAiAUdSAAAAAlwSFlzAAALEwAACxMBAJqcGAAAAAd0SU1FB94BCRQnOqNu0b4AAAAKSURBVAjXY2AAAAACAAHiIbwzAAAAAElFTkSuQmCC")
49 50
50 51 def test_image_filename_defaults():
51 52 '''test format constraint, and validity of jpeg and png'''
52 53 tpath = ipath.get_ipython_package_dir()
53 54 nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'),
54 55 embed=True)
55 56 nt.assert_raises(ValueError, display.Image)
56 57 nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True)
57 58 # check boths paths to allow packages to test at build and install time
58 59 imgfile = os.path.join(tpath, 'core/tests/2x2.png')
59 60 img = display.Image(filename=imgfile)
60 61 nt.assert_equal('png', img.format)
61 62 nt.assert_is_not_none(img._repr_png_())
62 63 img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False)
63 64 nt.assert_equal('jpeg', img.format)
64 65 nt.assert_is_none(img._repr_jpeg_())
65 66
66 67 def _get_inline_config():
67 68 from ipykernel.pylab.config import InlineBackend
68 69 return InlineBackend.instance()
69 70
70 71 @dec.skip_without('matplotlib')
71 72 def test_set_matplotlib_close():
72 73 cfg = _get_inline_config()
73 74 cfg.close_figures = False
74 75 display.set_matplotlib_close()
75 76 assert cfg.close_figures
76 77 display.set_matplotlib_close(False)
77 78 assert not cfg.close_figures
78 79
79 80 _fmt_mime_map = {
80 81 'png': 'image/png',
81 82 'jpeg': 'image/jpeg',
82 83 'pdf': 'application/pdf',
83 84 'retina': 'image/png',
84 85 'svg': 'image/svg+xml',
85 86 }
86 87
87 88 @dec.skip_without('matplotlib')
88 89 def test_set_matplotlib_formats():
89 90 from matplotlib.figure import Figure
90 91 formatters = get_ipython().display_formatter.formatters
91 92 for formats in [
92 93 ('png',),
93 94 ('pdf', 'svg'),
94 95 ('jpeg', 'retina', 'png'),
95 96 (),
96 97 ]:
97 98 active_mimes = {_fmt_mime_map[fmt] for fmt in formats}
98 99 display.set_matplotlib_formats(*formats)
99 100 for mime, f in formatters.items():
100 101 if mime in active_mimes:
101 102 nt.assert_in(Figure, f)
102 103 else:
103 104 nt.assert_not_in(Figure, f)
104 105
105 106 @dec.skip_without('matplotlib')
106 107 def test_set_matplotlib_formats_kwargs():
107 108 from matplotlib.figure import Figure
108 109 ip = get_ipython()
109 110 cfg = _get_inline_config()
110 111 cfg.print_figure_kwargs.update(dict(foo='bar'))
111 112 kwargs = dict(quality=10)
112 113 display.set_matplotlib_formats('png', **kwargs)
113 114 formatter = ip.display_formatter.formatters['image/png']
114 115 f = formatter.lookup_by_type(Figure)
115 116 cell = f.__closure__[0].cell_contents
116 117 expected = kwargs
117 118 expected.update(cfg.print_figure_kwargs)
118 119 nt.assert_equal(cell, expected)
119 120
120 121 def test_displayobject_repr():
121 122 h = display.HTML('<br />')
122 123 nt.assert_equal(repr(h), '<IPython.core.display.HTML object>')
123 124 h._show_mem_addr = True
124 125 nt.assert_equal(repr(h), object.__repr__(h))
125 126 h._show_mem_addr = False
126 127 nt.assert_equal(repr(h), '<IPython.core.display.HTML object>')
127 128
128 129 j = display.Javascript('')
129 130 nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')
130 131 j._show_mem_addr = True
131 132 nt.assert_equal(repr(j), object.__repr__(j))
132 133 j._show_mem_addr = False
133 134 nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>')
134 135
135 136 def test_json():
136 137 d = {'a': 5}
137 138 lis = [d]
138 139 j = display.JSON(d)
139 140 nt.assert_equal(j._repr_json_(), d)
140 141
141 142 with warnings.catch_warnings(record=True) as w:
142 143 warnings.simplefilter("always")
143 144 j = display.JSON(json.dumps(d))
144 145 nt.assert_equal(len(w), 1)
145 146 nt.assert_equal(j._repr_json_(), d)
146 147
147 148 j = display.JSON(lis)
148 149 nt.assert_equal(j._repr_json_(), lis)
149 150
150 151 with warnings.catch_warnings(record=True) as w:
151 152 warnings.simplefilter("always")
152 153 j = display.JSON(json.dumps(lis))
153 154 nt.assert_equal(len(w), 1)
154 155 nt.assert_equal(j._repr_json_(), lis)
155 156
156 157 def test_video_embedding():
157 158 """use a tempfile, with dummy-data, to ensure that video embedding doesn't crash"""
158 159 v = display.Video("http://ignored")
159 160 assert not v.embed
160 161 html = v._repr_html_()
161 162 nt.assert_not_in('src="data:', html)
162 163 nt.assert_in('src="http://ignored"', html)
163 164
164 165 with nt.assert_raises(ValueError):
165 166 v = display.Video(b'abc')
166 167
167 with tempfile.NamedTemporaryFile(suffix='.mp4') as f:
168 with open(f.name,'wb') as f:
169 f.write(b'abc')
168 with NamedFileInTemporaryDirectory('test.mp4') as f:
169 f.write(b'abc')
170 f.close()
170 171
171 172 v = display.Video(f.name)
172 173 assert not v.embed
173 174 html = v._repr_html_()
174 175 nt.assert_not_in('src="data:', html)
175 176
176 177 v = display.Video(f.name, embed=True)
177 178 html = v._repr_html_()
178 179 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
179 180
180 181 v = display.Video(f.name, embed=True, mimetype='video/other')
181 182 html = v._repr_html_()
182 183 nt.assert_in('src="data:video/other;base64,YWJj"',html)
183 184
184 185 v = display.Video(b'abc', embed=True, mimetype='video/mp4')
185 186 html = v._repr_html_()
186 187 nt.assert_in('src="data:video/mp4;base64,YWJj"',html)
187 188
188 189 v = display.Video(u'YWJj', embed=True, mimetype='video/xyz')
189 190 html = v._repr_html_()
190 191 nt.assert_in('src="data:video/xyz;base64,YWJj"',html)
191 192
General Comments 0
You need to be logged in to leave comments. Login now