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 test_file_system
import TestFileSystem
16 from third_party
.handlebar
import Handlebar
18 _REDIRECTS_JSON
= json
.dumps({
19 'oldfile.html': 'storage.html',
20 'index.html': 'https://developers.google.com/chrome',
25 ('# Header 1 #', u
'<h1 id="header-1">Header 1</h1>'),
26 ('1. Foo\n', u
'<ol>\n<li>Foo</li>\n</ol>'),
27 ('![alt text](/path/img.jpg "Title")\n',
28 '<p><img alt="alt text" src="/path/img.jpg" title="Title" /></p>'),
29 ('* Unordered item 1', u
'<ul>\n<li>Unordered item 1</li>\n</ul>')
32 # Test file system data which exercises many different mimetypes.
35 'a.txt': 'a.txt content',
36 'b.txt': 'b.txt content',
38 'd.txt': 'd.txt content',
43 'a.txt': 'a.txt content',
44 'b.txt': 'b.txt content',
46 'd.txt': 'd.txt content',
50 'img.png': 'img.png content',
51 'read.txt': 'read.txt content',
52 'redirects.json': _REDIRECTS_JSON
,
53 'run.js': 'run.js content',
54 'site.css': 'site.css content',
55 'storage.html': 'storage.html content',
56 'markdown.md': '\n'.join(text
[0] for text
in _MARKDOWN_CONTENT
)
60 class ContentProviderUnittest(unittest
.TestCase
):
62 self
._content
_provider
= self
._CreateContentProvider
()
64 def _CreateContentProvider(self
, supports_zip
=False):
65 test_file_system
= TestFileSystem(_TEST_DATA
)
66 return ContentProvider(
68 CompiledFileSystem
.Factory(ObjectStoreCreator
.ForTest()),
70 # TODO(kalman): Test supports_templates=False.
71 supports_templates
=True,
72 supports_zip
=supports_zip
)
74 def _assertContent(self
, content
, content_type
, content_and_type
):
75 # Assert type so that str is differentiated from unicode.
76 self
.assertEqual(type(content
), type(content_and_type
.content
))
77 self
.assertEqual(content
, content_and_type
.content
)
78 self
.assertEqual(content_type
, content_and_type
.content_type
)
80 def testPlainText(self
):
82 u
'a.txt content', 'text/plain',
83 self
._content
_provider
.GetContentAndType('dir/a.txt').Get())
85 u
'd.txt content', 'text/plain',
86 self
._content
_provider
.GetContentAndType('dir/c/d.txt').Get())
88 u
'read.txt content', 'text/plain',
89 self
._content
_provider
.GetContentAndType('read.txt').Get())
91 unicode(_REDIRECTS_JSON
, 'utf-8'), 'application/json',
92 self
._content
_provider
.GetContentAndType('redirects.json').Get())
94 u
'run.js content', 'application/javascript',
95 self
._content
_provider
.GetContentAndType('run.js').Get())
97 u
'site.css content', 'text/css',
98 self
._content
_provider
.GetContentAndType('site.css').Get())
100 def testTemplate(self
):
101 content_and_type
= self
._content
_provider
.GetContentAndType(
102 'storage.html').Get()
103 self
.assertEqual(Handlebar
, type(content_and_type
.content
))
104 content_and_type
.content
= content_and_type
.content
.source
105 self
._assertContent
(u
'storage.html content', 'text/html', content_and_type
)
109 'img.png content', 'image/png',
110 self
._content
_provider
.GetContentAndType('img.png').Get())
112 def testZipTopLevel(self
):
113 zip_content_provider
= self
._CreateContentProvider
(supports_zip
=True)
114 content_and_type
= zip_content_provider
.GetContentAndType('dir.zip').Get()
115 zipfile
= ZipFile(StringIO(content_and_type
.content
))
116 content_and_type
.content
= zipfile
.namelist()
118 ['dir/a.txt', 'dir/b.txt', 'dir/c/d.txt'], 'application/zip',
121 def testZip2ndLevel(self
):
122 zip_content_provider
= self
._CreateContentProvider
(supports_zip
=True)
123 content_and_type
= zip_content_provider
.GetContentAndType(
124 'dir2/dir3.zip').Get()
125 zipfile
= ZipFile(StringIO(content_and_type
.content
))
126 content_and_type
.content
= zipfile
.namelist()
128 ['dir3/a.txt', 'dir3/b.txt', 'dir3/c/d.txt'], 'application/zip',
131 def testMarkdown(self
):
132 content_and_type
= self
._content
_provider
.GetContentAndType(
133 'markdown.html').Get()
134 content_and_type
.content
= content_and_type
.content
.source
135 self
._assertContent
('\n'.join(text
[1] for text
in _MARKDOWN_CONTENT
),
136 'text/html', content_and_type
)
138 def testNotFound(self
):
141 self
._content
_provider
.GetContentAndType('oops').Get
)
144 if __name__
== '__main__':