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
._gcs
_fs
_provider
)
117 def testSimpleRootPath(self
):
118 provider
= self
._content
_providers
.GetByName('apples')
121 provider
.GetContentAndType('gala.txt').Get().content
)
123 'granny smith apples',
124 provider
.GetContentAndType('green/granny smith.txt').Get().content
)
126 def testComplexRootPath(self
):
127 provider
= self
._content
_providers
.GetByName('tomatoes')
130 provider
.GetContentAndType('vegetable.txt').Get().content
)
133 provider
.GetContentAndType('fruit/cherry.txt').Get().content
)
135 def testParentRootPath(self
):
136 provider
= self
._content
_providers
.GetByName('bananas')
139 provider
.GetContentAndType('apples/gala.txt').Get().content
)
141 def testSimpleServlet(self
):
142 provider
, serve_from
, path
= self
._content
_providers
.GetByServeFrom(
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(
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(
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())
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())
209 # provider.GetContentAndType(path).Get().content)
211 if __name__
== '__main__':