2 # Copyright 2013 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 from cStringIO
import StringIO
9 from zipfile
import ZipFile
11 from compiled_file_system
import CompiledFileSystem
12 from content_provider
import ContentProvider
13 from file_system
import FileNotFoundError
14 from object_store_creator
import ObjectStoreCreator
15 from path_canonicalizer
import PathCanonicalizer
16 from test_file_system
import TestFileSystem
17 from third_party
.motemplate
import Motemplate
19 _REDIRECTS_JSON
= json
.dumps({
20 'oldfile.html': 'storage.html',
21 'index.html': 'https://developers.google.com/chrome',
26 ('# Header 1 #', u
'<h1 id="header-1">Header 1</h1>'),
27 ('1. Foo\n', u
'<ol>\n<li>Foo</li>\n</ol>'),
28 ('![alt text](/path/img.jpg "Title")\n',
29 '<p><img alt="alt text" src="/path/img.jpg" title="Title" /></p>'),
30 ('* Unordered item 1', u
'<ul>\n<li>Unordered item 1</li>\n</ul>')
33 # Test file system data which exercises many different mimetypes.
36 'a.txt': 'a.txt content',
37 'b.txt': 'b.txt content',
39 'd.txt': 'd.txt content',
44 'a.txt': 'a.txt content',
45 'b.txt': 'b.txt content',
47 'd.txt': 'd.txt content',
52 'index.html': 'index.html content 1'
55 'index.html': 'index.html content 2'
58 'notindex.html': 'notindex.html content'
61 'index.md': '\n'.join(text
[0] for text
in _MARKDOWN_CONTENT
)
63 'dir.txt': 'dir.txt content',
64 'dir5.html': 'dir5.html content',
65 'img.png': 'img.png content',
66 'index.html': 'index.html content',
67 'read.txt': 'read.txt content',
68 'redirects.json': _REDIRECTS_JSON
,
69 'noextension': 'noextension content',
70 'run.js': 'run.js content',
71 'site.css': 'site.css content',
72 'storage.html': 'storage.html content',
73 'markdown.md': '\n'.join(text
[0] for text
in _MARKDOWN_CONTENT
)
77 class ContentProviderUnittest(unittest
.TestCase
):
79 self
._test
_file
_system
= TestFileSystem(_TEST_DATA
)
80 self
._content
_provider
= self
._CreateContentProvider
()
82 def _CreateContentProvider(self
, supports_zip
=False):
83 object_store_creator
= ObjectStoreCreator
.ForTest()
84 return ContentProvider(
86 CompiledFileSystem
.Factory(object_store_creator
),
87 self
._test
_file
_system
,
89 default_extensions
=('.html', '.md'),
90 # TODO(kalman): Test supports_templates=False.
91 supports_templates
=True,
92 supports_zip
=supports_zip
)
94 def _assertContent(self
, content
, content_type
, content_and_type
):
95 # Assert type so that str is differentiated from unicode.
96 self
.assertEqual(type(content
), type(content_and_type
.content
))
97 self
.assertEqual(content
, content_and_type
.content
)
98 self
.assertEqual(content_type
, content_and_type
.content_type
)
100 def _assertTemplateContent(self
, content
, path
, version
):
101 content_and_type
= self
._content
_provider
.GetContentAndType(path
).Get()
102 self
.assertEqual(Motemplate
, type(content_and_type
.content
))
103 content_and_type
.content
= content_and_type
.content
.source
104 self
._assertContent
(content
, 'text/html', content_and_type
)
105 self
.assertEqual(version
, self
._content
_provider
.GetVersion(path
).Get())
107 def _assertMarkdownContent(self
, content
, path
, version
):
108 content_and_type
= self
._content
_provider
.GetContentAndType(path
).Get()
109 content_and_type
.content
= content_and_type
.content
.source
110 self
._assertContent
(content
, 'text/html', content_and_type
)
111 self
.assertEqual(version
, self
._content
_provider
.GetVersion(path
).Get())
113 def testPlainText(self
):
115 u
'a.txt content', 'text/plain',
116 self
._content
_provider
.GetContentAndType('dir/a.txt').Get())
118 u
'd.txt content', 'text/plain',
119 self
._content
_provider
.GetContentAndType('dir/c/d.txt').Get())
121 u
'read.txt content', 'text/plain',
122 self
._content
_provider
.GetContentAndType('read.txt').Get())
124 unicode(_REDIRECTS_JSON
, 'utf-8'), 'application/json',
125 self
._content
_provider
.GetContentAndType('redirects.json').Get())
127 u
'run.js content', 'application/javascript',
128 self
._content
_provider
.GetContentAndType('run.js').Get())
130 u
'site.css content', 'text/css',
131 self
._content
_provider
.GetContentAndType('site.css').Get())
133 def testTemplate(self
):
134 self
._assertTemplateContent
(u
'storage.html content', 'storage.html', '0')
135 self
._test
_file
_system
.IncrementStat('storage.html')
136 self
._assertTemplateContent
(u
'storage.html content', 'storage.html', '1')
140 'img.png content', 'image/png',
141 self
._content
_provider
.GetContentAndType('img.png').Get())
143 def testZipTopLevel(self
):
144 zip_content_provider
= self
._CreateContentProvider
(supports_zip
=True)
145 content_and_type
= zip_content_provider
.GetContentAndType('dir.zip').Get()
146 zipfile
= ZipFile(StringIO(content_and_type
.content
))
147 content_and_type
.content
= zipfile
.namelist()
149 ['dir/a.txt', 'dir/b.txt', 'dir/c/d.txt'], 'application/zip',
152 def testZip2ndLevel(self
):
153 zip_content_provider
= self
._CreateContentProvider
(supports_zip
=True)
154 content_and_type
= zip_content_provider
.GetContentAndType(
155 'dir2/dir3.zip').Get()
156 zipfile
= ZipFile(StringIO(content_and_type
.content
))
157 content_and_type
.content
= zipfile
.namelist()
159 ['dir3/a.txt', 'dir3/b.txt', 'dir3/c/d.txt'], 'application/zip',
162 def testCanonicalZipPaths(self
):
163 # Without supports_zip the path is canonicalized as a file.
166 self
._content
_provider
.GetCanonicalPath('dir.zip'))
169 self
._content
_provider
.GetCanonicalPath('diR.zip'))
170 # With supports_zip the path is canonicalized as the zip file which
171 # corresponds to the canonical directory.
172 zip_content_provider
= self
._CreateContentProvider
(supports_zip
=True)
175 zip_content_provider
.GetCanonicalPath('dir.zip'))
178 zip_content_provider
.GetCanonicalPath('diR.zip'))
180 def testMarkdown(self
):
181 expected_content
= '\n'.join(text
[1] for text
in _MARKDOWN_CONTENT
)
182 self
._assertMarkdownContent
(expected_content
, 'markdown', '0')
183 self
._test
_file
_system
.IncrementStat('markdown.md')
184 self
._assertMarkdownContent
(expected_content
, 'markdown', '1')
186 def testNotFound(self
):
189 self
._content
_provider
.GetContentAndType('oops').Get
)
191 def testIndexRedirect(self
):
192 self
._assertTemplateContent
(u
'index.html content', '', '0')
193 self
._assertTemplateContent
(u
'index.html content 1', 'dir4', '0')
194 self
._assertTemplateContent
(u
'dir5.html content', 'dir5', '0')
195 self
._assertMarkdownContent
(
196 '\n'.join(text
[1] for text
in _MARKDOWN_CONTENT
),
200 'noextension content', 'text/plain',
201 self
._content
_provider
.GetContentAndType('noextension').Get())
204 self
._content
_provider
.GetContentAndType('dir6').Get
)
206 def testRefresh(self
):
207 # Not entirely sure what to test here, but get some code coverage.
208 self
._content
_provider
.Refresh().Get()
211 if __name__
== '__main__':