Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / content_providers_test.py
blob7ced6da6885f3a98b3185253348c7c46a59c9c25
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 import json
7 import unittest
9 from compiled_file_system import CompiledFileSystem
10 from content_providers import ContentProviders
11 from extensions_paths import EXTENSIONS
12 from object_store_creator import ObjectStoreCreator
13 from test_file_system import TestFileSystem
14 from test_util import DisableLogging
17 _CONTENT_PROVIDERS = {
18 'apples': {
19 'chromium': {
20 'dir': 'chrome/common/extensions/apples'
22 'serveFrom': 'apples-dir',
24 'bananas': {
25 'serveFrom': '',
26 'chromium': {
27 'dir': 'chrome/common/extensions'
30 'github-provider': {
31 'serveFrom': 'gh',
32 'github': {
33 'dir': 'chrome/common/extensions',
34 'owner': 'GoogleChrome',
35 'repo': 'hello-world',
38 'github-provider-with-dir': {
39 'serveFrom': 'gh2',
40 'github': {
41 'dir': 'chrome/common/extensions/tomatoes/are/a',
42 'owner': 'SomeOwner',
43 'repo': 'some-repo',
46 'tomatoes': {
47 'serveFrom': 'tomatoes-dir/are/a',
48 'chromium': {
49 'dir': 'chrome/common/extensions/tomatoes/are/a'
55 _FILE_SYSTEM_DATA = {
56 'docs': {
57 'templates': {
58 'json': {
59 'content_providers.json': json.dumps(_CONTENT_PROVIDERS),
63 'apples': {
64 'gala.txt': 'gala apples',
65 'green': {
66 'granny smith.txt': 'granny smith apples',
69 'tomatoes': {
70 'are': {
71 'a': {
72 'vegetable.txt': 'no they aren\'t',
73 'fruit': {
74 'cherry.txt': 'cherry tomatoes',
82 class _MockGithubFileSystemProvider(object):
83 '''A GithubFileSystemProvider imitation which records every call to Create
84 and returns them from GetAndReset.
85 '''
87 def __init__(self, file_system):
88 self._file_system = file_system
89 self._calls = []
91 def Create(self, owner, repo):
92 self._calls.append((owner, repo))
93 return self._file_system
95 def GetAndReset(self):
96 calls = self._calls
97 self._calls = []
98 return calls
101 class ContentProvidersTest(unittest.TestCase):
102 def setUp(self):
103 test_file_system = TestFileSystem(_FILE_SYSTEM_DATA, relative_to=EXTENSIONS)
104 self._github_fs_provider = _MockGithubFileSystemProvider(test_file_system)
105 self._content_providers = ContentProviders(
106 CompiledFileSystem.Factory(ObjectStoreCreator.ForTest()),
107 test_file_system,
108 self._github_fs_provider)
110 def testSimpleRootPath(self):
111 provider = self._content_providers.GetByName('apples')
112 self.assertEqual(
113 'gala apples',
114 provider.GetContentAndType('gala.txt').Get().content)
115 self.assertEqual(
116 'granny smith apples',
117 provider.GetContentAndType('green/granny smith.txt').Get().content)
119 def testComplexRootPath(self):
120 provider = self._content_providers.GetByName('tomatoes')
121 self.assertEqual(
122 'no they aren\'t',
123 provider.GetContentAndType('vegetable.txt').Get().content)
124 self.assertEqual(
125 'cherry tomatoes',
126 provider.GetContentAndType('fruit/cherry.txt').Get().content)
128 def testParentRootPath(self):
129 provider = self._content_providers.GetByName('bananas')
130 self.assertEqual(
131 'gala apples',
132 provider.GetContentAndType('apples/gala.txt').Get().content)
134 def testSimpleServlet(self):
135 provider, path = self._content_providers.GetByServeFrom('apples-dir')
136 self.assertEqual('apples', provider.name)
137 self.assertEqual('', path)
138 provider, path = self._content_providers.GetByServeFrom(
139 'apples-dir/are/forever')
140 self.assertEqual('apples', provider.name)
141 self.assertEqual('are/forever', path)
143 def testComplexServlet(self):
144 provider, path = self._content_providers.GetByServeFrom(
145 'tomatoes-dir/are/a')
146 self.assertEqual('tomatoes', provider.name)
147 self.assertEqual('', path)
148 provider, path = self._content_providers.GetByServeFrom(
149 'tomatoes-dir/are/a/fruit/they/are')
150 self.assertEqual('tomatoes', provider.name)
151 self.assertEqual('fruit/they/are', path)
153 def testEmptyStringServlet(self):
154 provider, path = self._content_providers.GetByServeFrom('tomatoes-dir/are')
155 self.assertEqual('bananas', provider.name)
156 self.assertEqual('tomatoes-dir/are', path)
157 provider, path = self._content_providers.GetByServeFrom('')
158 self.assertEqual('bananas', provider.name)
159 self.assertEqual('', path)
161 @DisableLogging('error')
162 def testProviderNotFound(self):
163 self.assertEqual(None, self._content_providers.GetByName('cabbages'))
165 def testGithubContentProvider(self):
166 provider, path = self._content_providers.GetByServeFrom(
167 'gh/apples/green/granny smith.txt')
168 self.assertEqual('github-provider', provider.name)
169 self.assertEqual('apples/green/granny smith.txt', path)
170 self.assertEqual([('GoogleChrome', 'hello-world')],
171 self._github_fs_provider.GetAndReset())
172 self.assertEqual(
173 'granny smith apples',
174 provider.GetContentAndType(path).Get().content)
176 def testGithubContentProviderWithDir(self):
177 provider, path = self._content_providers.GetByServeFrom(
178 'gh2/fruit/cherry.txt')
179 self.assertEqual('github-provider-with-dir', provider.name)
180 self.assertEqual('fruit/cherry.txt', path)
181 self.assertEqual([('SomeOwner', 'some-repo')],
182 self._github_fs_provider.GetAndReset())
183 self.assertEqual(
184 'cherry tomatoes',
185 provider.GetContentAndType(path).Get().content)
187 if __name__ == '__main__':
188 unittest.main()