content_providers_test.py revision f2477e01787aa58f445919b809d89e252beef54f
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.
5
6import json
7import unittest
8
9from compiled_file_system import CompiledFileSystem
10from content_providers import ContentProviders
11from extensions_paths import EXTENSIONS
12from object_store_creator import ObjectStoreCreator
13from test_file_system import TestFileSystem
14from test_util import DisableLogging
15
16
17_CONTENT_PROVIDERS = {
18  'apples': {
19    'chromium': {
20      'dir': 'chrome/common/extensions/apples'
21    },
22    'serveFrom': 'apples-dir',
23  },
24  'bananas': {
25    'serveFrom': '',
26    'chromium': {
27      'dir': 'chrome/common/extensions'
28    },
29  },
30  'github-provider': {
31    'serveFrom': 'gh',
32    'github': {
33      'dir': 'chrome/common/extensions',
34      'owner': 'GoogleChrome',
35      'repo': 'hello-world',
36    },
37  },
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',
44    },
45  },
46  'tomatoes': {
47    'serveFrom': 'tomatoes-dir/are/a',
48    'chromium': {
49      'dir': 'chrome/common/extensions/tomatoes/are/a'
50    },
51  },
52}
53
54
55_FILE_SYSTEM_DATA = {
56  'docs': {
57    'templates': {
58      'json': {
59        'content_providers.json': json.dumps(_CONTENT_PROVIDERS),
60      },
61    },
62  },
63  'apples': {
64    'gala.txt': 'gala apples',
65    'green': {
66      'granny smith.txt': 'granny smith apples',
67    },
68  },
69  'tomatoes': {
70    'are': {
71      'a': {
72        'vegetable.txt': 'no they aren\'t',
73        'fruit': {
74          'cherry.txt': 'cherry tomatoes',
75        },
76      },
77    },
78  },
79}
80
81
82class _MockGithubFileSystemProvider(object):
83  '''A GithubFileSystemProvider imitation which records every call to Create
84  and returns them from GetAndReset.
85  '''
86
87  def __init__(self, file_system):
88    self._file_system = file_system
89    self._calls = []
90
91  def Create(self, owner, repo):
92    self._calls.append((owner, repo))
93    return self._file_system
94
95  def GetAndReset(self):
96    calls = self._calls
97    self._calls = []
98    return calls
99
100
101class 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)
109
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)
118
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)
127
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)
133
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)
142
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)
152
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)
160
161  @DisableLogging('error')
162  def testProviderNotFound(self):
163    self.assertEqual(None, self._content_providers.GetByName('cabbages'))
164
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)
175
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)
186
187if __name__ == '__main__':
188  unittest.main()
189