Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / content_provider_test.py
blob3e4f6475f5795f95aae23ca7839138efa06029f9
1 #!/usr/bin/env python
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
7 import json
8 import unittest
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',
24 _MARKDOWN_CONTENT = (
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.
33 _TEST_DATA = {
34 'dir': {
35 'a.txt': 'a.txt content',
36 'b.txt': 'b.txt content',
37 'c': {
38 'd.txt': 'd.txt content',
41 'dir2': {
42 'dir3': {
43 'a.txt': 'a.txt content',
44 'b.txt': 'b.txt content',
45 'c': {
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):
61 def setUp(self):
62 self._content_provider = self._CreateContentProvider()
64 def _CreateContentProvider(self, supports_zip=False):
65 test_file_system = TestFileSystem(_TEST_DATA)
66 return ContentProvider(
67 'foo',
68 CompiledFileSystem.Factory(ObjectStoreCreator.ForTest()),
69 test_file_system,
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):
81 self._assertContent(
82 u'a.txt content', 'text/plain',
83 self._content_provider.GetContentAndType('dir/a.txt').Get())
84 self._assertContent(
85 u'd.txt content', 'text/plain',
86 self._content_provider.GetContentAndType('dir/c/d.txt').Get())
87 self._assertContent(
88 u'read.txt content', 'text/plain',
89 self._content_provider.GetContentAndType('read.txt').Get())
90 self._assertContent(
91 unicode(_REDIRECTS_JSON, 'utf-8'), 'application/json',
92 self._content_provider.GetContentAndType('redirects.json').Get())
93 self._assertContent(
94 u'run.js content', 'application/javascript',
95 self._content_provider.GetContentAndType('run.js').Get())
96 self._assertContent(
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)
107 def testImage(self):
108 self._assertContent(
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()
117 self._assertContent(
118 ['dir/a.txt', 'dir/b.txt', 'dir/c/d.txt'], 'application/zip',
119 content_and_type)
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()
127 self._assertContent(
128 ['dir3/a.txt', 'dir3/b.txt', 'dir3/c/d.txt'], 'application/zip',
129 content_and_type)
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):
139 self.assertRaises(
140 FileNotFoundError,
141 self._content_provider.GetContentAndType('oops').Get)
144 if __name__ == '__main__':
145 unittest.main()