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