cygprofile: increase timeouts to allow showing web contents
[chromium-blink-merge.git] / chrome / common / extensions / docs / server2 / content_providers_test.py
blob106969e7db84582a7d7e795372c7de3149d9184c
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 CHROME_EXTENSIONS
12 from gcs_file_system_provider import CloudStorageFileSystemProvider
13 from object_store_creator import ObjectStoreCreator
14 from test_file_system import TestFileSystem
15 from test_util import DisableLogging
18 _CONTENT_PROVIDERS = {
19 'apples': {
20 'chromium': {
21 'dir': 'chrome/common/extensions/apples'
23 'serveFrom': 'apples-dir',
25 'bananas': {
26 'serveFrom': '',
27 'chromium': {
28 'dir': 'chrome/common/extensions'
31 'github-provider': {
32 'serveFrom': 'gh',
33 'github': {
34 'dir': 'chrome/common/extensions',
35 'owner': 'GoogleChrome',
36 'repo': 'hello-world',
39 'github-provider-with-dir': {
40 'serveFrom': 'gh2',
41 'github': {
42 'dir': 'chrome/common/extensions/tomatoes/are/a',
43 'owner': 'SomeOwner',
44 'repo': 'some-repo',
47 'tomatoes': {
48 'serveFrom': 'tomatoes-dir/are/a',
49 'chromium': {
50 'dir': 'chrome/common/extensions/tomatoes/are/a'
56 _FILE_SYSTEM_DATA = {
57 'docs': {
58 'templates': {
59 'json': {
60 'content_providers.json': json.dumps(_CONTENT_PROVIDERS),
64 'apples': {
65 'gala.txt': 'gala apples',
66 'green': {
67 'granny smith.txt': 'granny smith apples',
70 'tomatoes': {
71 'are': {
72 'a': {
73 'vegetable.txt': 'no they aren\'t',
74 'fruit': {
75 'cherry.txt': 'cherry tomatoes',
83 class _MockGithubFileSystemProvider(object):
84 '''A GithubFileSystemProvider imitation which records every call to Create
85 and returns them from GetAndReset.
86 '''
88 def __init__(self, file_system):
89 self._file_system = file_system
90 self._calls = []
92 def Create(self, owner, repo):
93 self._calls.append((owner, repo))
94 return self._file_system
96 def GetAndReset(self):
97 calls = self._calls
98 self._calls = []
99 return calls
102 class ContentProvidersTest(unittest.TestCase):
103 def setUp(self):
104 object_store_creator = ObjectStoreCreator.ForTest()
105 test_file_system = TestFileSystem(_FILE_SYSTEM_DATA,
106 relative_to=CHROME_EXTENSIONS)
107 self._github_fs_provider = _MockGithubFileSystemProvider(test_file_system)
108 object_store_creator = ObjectStoreCreator.ForTest()
109 # TODO(mangini): create tests for GCS
110 self._gcs_fs_provider = CloudStorageFileSystemProvider(object_store_creator)
111 self._content_providers = ContentProviders(
112 object_store_creator,
113 CompiledFileSystem.Factory(object_store_creator),
114 test_file_system,
115 self._gcs_fs_provider)
117 def testSimpleRootPath(self):
118 provider = self._content_providers.GetByName('apples')
119 self.assertEqual(
120 'gala apples',
121 provider.GetContentAndType('gala.txt').Get().content)
122 self.assertEqual(
123 'granny smith apples',
124 provider.GetContentAndType('green/granny smith.txt').Get().content)
126 def testComplexRootPath(self):
127 provider = self._content_providers.GetByName('tomatoes')
128 self.assertEqual(
129 'no they aren\'t',
130 provider.GetContentAndType('vegetable.txt').Get().content)
131 self.assertEqual(
132 'cherry tomatoes',
133 provider.GetContentAndType('fruit/cherry.txt').Get().content)
135 def testParentRootPath(self):
136 provider = self._content_providers.GetByName('bananas')
137 self.assertEqual(
138 'gala apples',
139 provider.GetContentAndType('apples/gala.txt').Get().content)
141 def testSimpleServlet(self):
142 provider, serve_from, path = self._content_providers.GetByServeFrom(
143 'apples-dir')
144 self.assertEqual('apples', provider.name)
145 self.assertEqual('apples-dir', serve_from)
146 self.assertEqual('', path)
147 provider, serve_from, path = self._content_providers.GetByServeFrom(
148 'apples-dir/')
149 self.assertEqual('apples', provider.name)
150 self.assertEqual('apples-dir', serve_from)
151 self.assertEqual('', path)
152 provider, serve_from, path = self._content_providers.GetByServeFrom(
153 'apples-dir/are/forever')
154 self.assertEqual('apples', provider.name)
155 self.assertEqual('apples-dir', serve_from)
156 self.assertEqual('are/forever', path)
158 def testComplexServlet(self):
159 provider, serve_from, path = self._content_providers.GetByServeFrom(
160 'tomatoes-dir/are/a')
161 self.assertEqual('tomatoes', provider.name)
162 self.assertEqual('tomatoes-dir/are/a', serve_from)
163 self.assertEqual('', path)
164 provider, serve_from, path = self._content_providers.GetByServeFrom(
165 'tomatoes-dir/are/a/fruit/they/are')
166 self.assertEqual('tomatoes', provider.name)
167 self.assertEqual('tomatoes-dir/are/a', serve_from)
168 self.assertEqual('fruit/they/are', path)
170 def testEmptyStringServlet(self):
171 provider, serve_from, path = self._content_providers.GetByServeFrom(
172 'tomatoes-dir/are')
173 self.assertEqual('bananas', provider.name)
174 self.assertEqual('', serve_from)
175 self.assertEqual('tomatoes-dir/are', path)
176 provider, serve_from, path = self._content_providers.GetByServeFrom('')
177 self.assertEqual('bananas', provider.name)
178 self.assertEqual('', serve_from)
179 self.assertEqual('', path)
181 @DisableLogging('error')
182 def testProviderNotFound(self):
183 self.assertEqual(None, self._content_providers.GetByName('cabbages'))
185 # TODO: Re-enable these Github tests if we ever decide to restore our support
186 # for Github content providers.
187 # def testGithubContentProvider(self):
188 # provider, serve_from, path = self._content_providers.GetByServeFrom(
189 # 'gh/apples/green/granny smith.txt')
190 # self.assertEqual('github-provider', provider.name)
191 # self.assertEqual('gh', serve_from)
192 # self.assertEqual('apples/green/granny smith.txt', path)
193 # self.assertEqual([('GoogleChrome', 'hello-world')],
194 # self._github_fs_provider.GetAndReset())
195 # self.assertEqual(
196 # 'granny smith apples',
197 # provider.GetContentAndType(path).Get().content)
199 # def testGithubContentProviderWithDir(self):
200 # provider, serve_from, path = self._content_providers.GetByServeFrom(
201 # 'gh2/fruit/cherry.txt')
202 # self.assertEqual('github-provider-with-dir', provider.name)
203 # self.assertEqual('gh2', serve_from)
204 # self.assertEqual('fruit/cherry.txt', path)
205 # self.assertEqual([('SomeOwner', 'some-repo')],
206 # self._github_fs_provider.GetAndReset())
207 # self.assertEqual(
208 # 'cherry tomatoes',
209 # provider.GetContentAndType(path).Get().content)
211 if __name__ == '__main__':
212 unittest.main()