Show More
@@ -1,118 +1,134 | |||||
1 | #----------------------------------------------------------------------------- |
|
1 | #----------------------------------------------------------------------------- | |
2 | # Copyright (C) 2010-2011 The IPython Development Team. |
|
2 | # Copyright (C) 2010-2011 The IPython Development Team. | |
3 | # |
|
3 | # | |
4 | # Distributed under the terms of the BSD License. |
|
4 | # Distributed under the terms of the BSD License. | |
5 | # |
|
5 | # | |
6 | # The full license is in the file COPYING.txt, distributed with this software. |
|
6 | # The full license is in the file COPYING.txt, distributed with this software. | |
7 | #----------------------------------------------------------------------------- |
|
7 | #----------------------------------------------------------------------------- | |
8 | import os |
|
8 | import os | |
9 |
|
9 | |||
10 | import nose.tools as nt |
|
10 | import nose.tools as nt | |
11 |
|
11 | |||
12 | from IPython.core import display |
|
12 | from IPython.core import display | |
13 | from IPython.core.getipython import get_ipython |
|
13 | from IPython.core.getipython import get_ipython | |
14 | from IPython.utils import path as ipath |
|
14 | from IPython.utils import path as ipath | |
15 |
|
15 | |||
16 | import IPython.testing.decorators as dec |
|
16 | import IPython.testing.decorators as dec | |
17 |
|
17 | |||
18 | def test_image_size(): |
|
18 | def test_image_size(): | |
19 | """Simple test for display.Image(args, width=x,height=y)""" |
|
19 | """Simple test for display.Image(args, width=x,height=y)""" | |
20 | thisurl = 'http://www.google.fr/images/srpr/logo3w.png' |
|
20 | thisurl = 'http://www.google.fr/images/srpr/logo3w.png' | |
21 | img = display.Image(url=thisurl, width=200, height=200) |
|
21 | img = display.Image(url=thisurl, width=200, height=200) | |
22 | 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_()) | |
23 | img = display.Image(url=thisurl, width=200) |
|
23 | img = display.Image(url=thisurl, width=200) | |
24 | 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_()) | |
25 | img = display.Image(url=thisurl) |
|
25 | img = display.Image(url=thisurl) | |
26 | nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_()) |
|
26 | nt.assert_equal(u'<img src="%s"/>' % (thisurl), img._repr_html_()) | |
27 |
|
27 | |||
28 | def test_retina_png(): |
|
28 | def test_retina_png(): | |
29 | here = os.path.dirname(__file__) |
|
29 | here = os.path.dirname(__file__) | |
30 | img = display.Image(os.path.join(here, "2x2.png"), retina=True) |
|
30 | img = display.Image(os.path.join(here, "2x2.png"), retina=True) | |
31 | nt.assert_equal(img.height, 1) |
|
31 | nt.assert_equal(img.height, 1) | |
32 | nt.assert_equal(img.width, 1) |
|
32 | nt.assert_equal(img.width, 1) | |
33 | data, md = img._repr_png_() |
|
33 | data, md = img._repr_png_() | |
34 | nt.assert_equal(md['width'], 1) |
|
34 | nt.assert_equal(md['width'], 1) | |
35 | nt.assert_equal(md['height'], 1) |
|
35 | nt.assert_equal(md['height'], 1) | |
36 |
|
36 | |||
37 | def test_retina_jpeg(): |
|
37 | def test_retina_jpeg(): | |
38 | here = os.path.dirname(__file__) |
|
38 | here = os.path.dirname(__file__) | |
39 | img = display.Image(os.path.join(here, "2x2.jpg"), retina=True) |
|
39 | img = display.Image(os.path.join(here, "2x2.jpg"), retina=True) | |
40 | nt.assert_equal(img.height, 1) |
|
40 | nt.assert_equal(img.height, 1) | |
41 | nt.assert_equal(img.width, 1) |
|
41 | nt.assert_equal(img.width, 1) | |
42 | data, md = img._repr_jpeg_() |
|
42 | data, md = img._repr_jpeg_() | |
43 | nt.assert_equal(md['width'], 1) |
|
43 | nt.assert_equal(md['width'], 1) | |
44 | nt.assert_equal(md['height'], 1) |
|
44 | nt.assert_equal(md['height'], 1) | |
45 |
|
45 | |||
46 | def test_image_filename_defaults(): |
|
46 | def test_image_filename_defaults(): | |
47 | '''test format constraint, and validity of jpeg and png''' |
|
47 | '''test format constraint, and validity of jpeg and png''' | |
48 | tpath = ipath.get_ipython_package_dir() |
|
48 | tpath = ipath.get_ipython_package_dir() | |
49 | nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'), |
|
49 | nt.assert_raises(ValueError, display.Image, filename=os.path.join(tpath, 'testing/tests/badformat.gif'), | |
50 | embed=True) |
|
50 | embed=True) | |
51 | nt.assert_raises(ValueError, display.Image) |
|
51 | nt.assert_raises(ValueError, display.Image) | |
52 | nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True) |
|
52 | nt.assert_raises(ValueError, display.Image, data='this is not an image', format='badformat', embed=True) | |
53 | from IPython.html import DEFAULT_STATIC_FILES_PATH |
|
53 | from IPython.html import DEFAULT_STATIC_FILES_PATH | |
54 | # check boths paths to allow packages to test at build and install time |
|
54 | # check boths paths to allow packages to test at build and install time | |
55 | imgfile = os.path.join(tpath, 'html/static/base/images/ipynblogo.png') |
|
55 | imgfile = os.path.join(tpath, 'html/static/base/images/ipynblogo.png') | |
56 | if not os.path.exists(imgfile): |
|
56 | if not os.path.exists(imgfile): | |
57 | imgfile = os.path.join(DEFAULT_STATIC_FILES_PATH, 'base/images/ipynblogo.png') |
|
57 | imgfile = os.path.join(DEFAULT_STATIC_FILES_PATH, 'base/images/ipynblogo.png') | |
58 | img = display.Image(filename=imgfile) |
|
58 | img = display.Image(filename=imgfile) | |
59 | nt.assert_equal('png', img.format) |
|
59 | nt.assert_equal('png', img.format) | |
60 | nt.assert_is_not_none(img._repr_png_()) |
|
60 | nt.assert_is_not_none(img._repr_png_()) | |
61 | img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False) |
|
61 | img = display.Image(filename=os.path.join(tpath, 'testing/tests/logo.jpg'), embed=False) | |
62 | nt.assert_equal('jpeg', img.format) |
|
62 | nt.assert_equal('jpeg', img.format) | |
63 | nt.assert_is_none(img._repr_jpeg_()) |
|
63 | nt.assert_is_none(img._repr_jpeg_()) | |
64 |
|
64 | |||
65 | def _get_inline_config(): |
|
65 | def _get_inline_config(): | |
66 | from IPython.kernel.zmq.pylab.config import InlineBackend |
|
66 | from IPython.kernel.zmq.pylab.config import InlineBackend | |
67 | return InlineBackend.instance() |
|
67 | return InlineBackend.instance() | |
68 |
|
68 | |||
69 | @dec.skip_without('matplotlib') |
|
69 | @dec.skip_without('matplotlib') | |
70 | def test_set_matplotlib_close(): |
|
70 | def test_set_matplotlib_close(): | |
71 | cfg = _get_inline_config() |
|
71 | cfg = _get_inline_config() | |
72 | cfg.close_figures = False |
|
72 | cfg.close_figures = False | |
73 | display.set_matplotlib_close() |
|
73 | display.set_matplotlib_close() | |
74 | assert cfg.close_figures |
|
74 | assert cfg.close_figures | |
75 | display.set_matplotlib_close(False) |
|
75 | display.set_matplotlib_close(False) | |
76 | assert not cfg.close_figures |
|
76 | assert not cfg.close_figures | |
77 |
|
77 | |||
78 | _fmt_mime_map = { |
|
78 | _fmt_mime_map = { | |
79 | 'png': 'image/png', |
|
79 | 'png': 'image/png', | |
80 | 'jpeg': 'image/jpeg', |
|
80 | 'jpeg': 'image/jpeg', | |
81 | 'pdf': 'application/pdf', |
|
81 | 'pdf': 'application/pdf', | |
82 | 'retina': 'image/png', |
|
82 | 'retina': 'image/png', | |
83 | 'svg': 'image/svg+xml', |
|
83 | 'svg': 'image/svg+xml', | |
84 | } |
|
84 | } | |
85 |
|
85 | |||
86 | @dec.skip_without('matplotlib') |
|
86 | @dec.skip_without('matplotlib') | |
87 | def test_set_matplotlib_formats(): |
|
87 | def test_set_matplotlib_formats(): | |
88 | from matplotlib.figure import Figure |
|
88 | from matplotlib.figure import Figure | |
89 | formatters = get_ipython().display_formatter.formatters |
|
89 | formatters = get_ipython().display_formatter.formatters | |
90 | for formats in [ |
|
90 | for formats in [ | |
91 | ('png',), |
|
91 | ('png',), | |
92 | ('pdf', 'svg'), |
|
92 | ('pdf', 'svg'), | |
93 | ('jpeg', 'retina', 'png'), |
|
93 | ('jpeg', 'retina', 'png'), | |
94 | (), |
|
94 | (), | |
95 | ]: |
|
95 | ]: | |
96 | active_mimes = {_fmt_mime_map[fmt] for fmt in formats} |
|
96 | active_mimes = {_fmt_mime_map[fmt] for fmt in formats} | |
97 | display.set_matplotlib_formats(*formats) |
|
97 | display.set_matplotlib_formats(*formats) | |
98 | for mime, f in formatters.items(): |
|
98 | for mime, f in formatters.items(): | |
99 | if mime in active_mimes: |
|
99 | if mime in active_mimes: | |
100 | nt.assert_in(Figure, f) |
|
100 | nt.assert_in(Figure, f) | |
101 | else: |
|
101 | else: | |
102 | nt.assert_not_in(Figure, f) |
|
102 | nt.assert_not_in(Figure, f) | |
103 |
|
103 | |||
104 | @dec.skip_without('matplotlib') |
|
104 | @dec.skip_without('matplotlib') | |
105 | def test_set_matplotlib_formats_kwargs(): |
|
105 | def test_set_matplotlib_formats_kwargs(): | |
106 | from matplotlib.figure import Figure |
|
106 | from matplotlib.figure import Figure | |
107 | ip = get_ipython() |
|
107 | ip = get_ipython() | |
108 | cfg = _get_inline_config() |
|
108 | cfg = _get_inline_config() | |
109 | cfg.print_figure_kwargs.update(dict(foo='bar')) |
|
109 | cfg.print_figure_kwargs.update(dict(foo='bar')) | |
110 | kwargs = dict(quality=10) |
|
110 | kwargs = dict(quality=10) | |
111 | display.set_matplotlib_formats('png', **kwargs) |
|
111 | display.set_matplotlib_formats('png', **kwargs) | |
112 | formatter = ip.display_formatter.formatters['image/png'] |
|
112 | formatter = ip.display_formatter.formatters['image/png'] | |
113 | f = formatter.lookup_by_type(Figure) |
|
113 | f = formatter.lookup_by_type(Figure) | |
114 | cell = f.__closure__[0].cell_contents |
|
114 | cell = f.__closure__[0].cell_contents | |
115 | expected = kwargs |
|
115 | expected = kwargs | |
116 | expected.update(cfg.print_figure_kwargs) |
|
116 | expected.update(cfg.print_figure_kwargs) | |
117 | nt.assert_equal(cell, expected) |
|
117 | nt.assert_equal(cell, expected) | |
118 |
|
118 | |||
|
119 | def test_displayobject_repr(): | |||
|
120 | h = display.HTML('<br />') | |||
|
121 | nt.assert_equal(repr(h), '<IPython.core.display.HTML object>') | |||
|
122 | h._show_mem_addr = True | |||
|
123 | nt.assert_equal( | |||
|
124 | repr(h), '<IPython.core.display.HTML object at %s>' % hex(id(h))) | |||
|
125 | h._show_mem_addr = False | |||
|
126 | nt.assert_equal(repr(h), '<IPython.core.display.HTML object>') | |||
|
127 | ||||
|
128 | j = display.Javascript('') | |||
|
129 | nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>') | |||
|
130 | j._show_mem_addr = True | |||
|
131 | nt.assert_equal( | |||
|
132 | repr(j), '<IPython.core.display.Javascript object at %s>' % hex(id(j))) | |||
|
133 | j._show_mem_addr = False | |||
|
134 | nt.assert_equal(repr(j), '<IPython.core.display.Javascript object>') |
General Comments 0
You need to be logged in to leave comments.
Login now