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 EXTENSIONS
12 from object_store_creator
import ObjectStoreCreator
13 from test_file_system
import TestFileSystem
14 from test_util
import DisableLogging
17 _CONTENT_PROVIDERS
= {
20 'dir': 'chrome/common/extensions/apples'
22 'serveFrom': 'apples-dir',
27 'dir': 'chrome/common/extensions'
33 'dir': 'chrome/common/extensions',
34 'owner': 'GoogleChrome',
35 'repo': 'hello-world',
38 'github-provider-with-dir': {
41 'dir': 'chrome/common/extensions/tomatoes/are/a',
47 'serveFrom': 'tomatoes-dir/are/a',
49 'dir': 'chrome/common/extensions/tomatoes/are/a'
59 'content_providers.json': json
.dumps(_CONTENT_PROVIDERS
),
64 'gala.txt': 'gala apples',
66 'granny smith.txt': 'granny smith apples',
72 'vegetable.txt': 'no they aren\'t',
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.
87 def __init__(self
, file_system
):
88 self
._file
_system
= file_system
91 def Create(self
, owner
, repo
):
92 self
._calls
.append((owner
, repo
))
93 return self
._file
_system
95 def GetAndReset(self
):
101 class ContentProvidersTest(unittest
.TestCase
):
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()),
108 self
._github
_fs
_provider
)
110 def testSimpleRootPath(self
):
111 provider
= self
._content
_providers
.GetByName('apples')
114 provider
.GetContentAndType('gala.txt').Get().content
)
116 'granny smith apples',
117 provider
.GetContentAndType('green/granny smith.txt').Get().content
)
119 def testComplexRootPath(self
):
120 provider
= self
._content
_providers
.GetByName('tomatoes')
123 provider
.GetContentAndType('vegetable.txt').Get().content
)
126 provider
.GetContentAndType('fruit/cherry.txt').Get().content
)
128 def testParentRootPath(self
):
129 provider
= self
._content
_providers
.GetByName('bananas')
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())
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())
185 provider
.GetContentAndType(path
).Get().content
)
187 if __name__
== '__main__':