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.
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
= {
21 'dir': 'chrome/common/extensions/apples'
23 'serveFrom': 'apples-dir',
28 'dir': 'chrome/common/extensions'
34 'dir': 'chrome/common/extensions',
35 'owner': 'GoogleChrome',
36 'repo': 'hello-world',
39 'github-provider-with-dir': {
42 'dir': 'chrome/common/extensions/tomatoes/are/a',
48 'serveFrom': 'tomatoes-dir/are/a',
50 'dir': 'chrome/common/extensions/tomatoes/are/a'
60 'content_providers.json': json
.dumps(_CONTENT_PROVIDERS
),
65 'gala.txt': 'gala apples',
67 'granny smith.txt': 'granny smith apples',
73 'vegetable.txt': 'no they aren\'t',
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.
88 def __init__(self
, file_system
):
89 self
._file
_system
= file_system
92 def Create(self
, owner
, repo
):
93 self
._calls
.append((owner
, repo
))
94 return self
._file
_system
96 def GetAndReset(self
):
102 class ContentProvidersTest(unittest
.TestCase
):
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
),
115 self
._github
_fs
_provider
,
116 self
._gcs
_fs
_provider
)
118 def testSimpleRootPath(self
):
119 provider
= self
._content
_providers
.GetByName('apples')
122 provider
.GetContentAndType('gala.txt').Get().content
)
124 'granny smith apples',
125 provider
.GetContentAndType('green/granny smith.txt').Get().content
)
127 def testComplexRootPath(self
):
128 provider
= self
._content
_providers
.GetByName('tomatoes')
131 provider
.GetContentAndType('vegetable.txt').Get().content
)
134 provider
.GetContentAndType('fruit/cherry.txt').Get().content
)
136 def testParentRootPath(self
):
137 provider
= self
._content
_providers
.GetByName('bananas')
140 provider
.GetContentAndType('apples/gala.txt').Get().content
)
142 def testSimpleServlet(self
):
143 provider
, serve_from
, path
= self
._content
_providers
.GetByServeFrom(
145 self
.assertEqual('apples', provider
.name
)
146 self
.assertEqual('apples-dir', serve_from
)
147 self
.assertEqual('', path
)
148 provider
, serve_from
, path
= self
._content
_providers
.GetByServeFrom(
150 self
.assertEqual('apples', provider
.name
)
151 self
.assertEqual('apples-dir', serve_from
)
152 self
.assertEqual('', path
)
153 provider
, serve_from
, path
= self
._content
_providers
.GetByServeFrom(
154 'apples-dir/are/forever')
155 self
.assertEqual('apples', provider
.name
)
156 self
.assertEqual('apples-dir', serve_from
)
157 self
.assertEqual('are/forever', path
)
159 def testComplexServlet(self
):
160 provider
, serve_from
, path
= self
._content
_providers
.GetByServeFrom(
161 'tomatoes-dir/are/a')
162 self
.assertEqual('tomatoes', provider
.name
)
163 self
.assertEqual('tomatoes-dir/are/a', serve_from
)
164 self
.assertEqual('', path
)
165 provider
, serve_from
, path
= self
._content
_providers
.GetByServeFrom(
166 'tomatoes-dir/are/a/fruit/they/are')
167 self
.assertEqual('tomatoes', provider
.name
)
168 self
.assertEqual('tomatoes-dir/are/a', serve_from
)
169 self
.assertEqual('fruit/they/are', path
)
171 def testEmptyStringServlet(self
):
172 provider
, serve_from
, path
= self
._content
_providers
.GetByServeFrom(
174 self
.assertEqual('bananas', provider
.name
)
175 self
.assertEqual('', serve_from
)
176 self
.assertEqual('tomatoes-dir/are', path
)
177 provider
, serve_from
, path
= self
._content
_providers
.GetByServeFrom('')
178 self
.assertEqual('bananas', provider
.name
)
179 self
.assertEqual('', serve_from
)
180 self
.assertEqual('', path
)
182 @DisableLogging('error')
183 def testProviderNotFound(self
):
184 self
.assertEqual(None, self
._content
_providers
.GetByName('cabbages'))
186 def testGithubContentProvider(self
):
187 provider
, serve_from
, path
= self
._content
_providers
.GetByServeFrom(
188 'gh/apples/green/granny smith.txt')
189 self
.assertEqual('github-provider', provider
.name
)
190 self
.assertEqual('gh', serve_from
)
191 self
.assertEqual('apples/green/granny smith.txt', path
)
192 self
.assertEqual([('GoogleChrome', 'hello-world')],
193 self
._github
_fs
_provider
.GetAndReset())
195 'granny smith apples',
196 provider
.GetContentAndType(path
).Get().content
)
198 def testGithubContentProviderWithDir(self
):
199 provider
, serve_from
, path
= self
._content
_providers
.GetByServeFrom(
200 'gh2/fruit/cherry.txt')
201 self
.assertEqual('github-provider-with-dir', provider
.name
)
202 self
.assertEqual('gh2', serve_from
)
203 self
.assertEqual('fruit/cherry.txt', path
)
204 self
.assertEqual([('SomeOwner', 'some-repo')],
205 self
._github
_fs
_provider
.GetAndReset())
208 provider
.GetContentAndType(path
).Get().content
)
210 if __name__
== '__main__':