content_providers_test.py revision 0f1bc08d4cfcc34181b0b5cbf065c40f687bf740
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 object_store_creator import ObjectStoreCreator
12from test_file_system import TestFileSystem
13from test_util import DisableLogging
14
15
16_HOST = 'https://developer.chrome.com'
17
18
19_CONTENT_PROVIDERS = {
20  'apples': {
21    'chromium': {
22      'dir': 'apples'
23    },
24    'serveFrom': 'apples-dir',
25  },
26  'bananas': {
27    'serveFrom': '',
28    'chromium': {
29      'dir': ''
30    },
31  },
32  'tomatoes': {
33    'serveFrom': 'tomatoes-dir/are/a',
34    'chromium': {
35      'dir': 'tomatoes/are/a'
36    },
37  },
38}
39
40
41_FILE_SYSTEM_DATA = {
42  'docs': {
43    'templates': {
44      'json': {
45        'content_providers.json': json.dumps(_CONTENT_PROVIDERS),
46      },
47    },
48  },
49  'apples': {
50    'gala.txt': 'gala apples',
51    'green': {
52      'granny smith.txt': 'granny smith apples',
53    },
54  },
55  'tomatoes': {
56    'are': {
57      'a': {
58        'vegetable.txt': 'no they aren\'t',
59        'fruit': {
60          'cherry.txt': 'cherry tomatoes',
61        },
62      },
63    },
64  },
65}
66
67
68class ContentProvidersTest(unittest.TestCase):
69  def setUp(self):
70    self._content_providers = ContentProviders(
71        CompiledFileSystem.Factory(ObjectStoreCreator.ForTest()),
72        TestFileSystem(_FILE_SYSTEM_DATA))
73
74  def testSimpleRootPath(self):
75    provider = self._content_providers.GetByName('apples')
76    self.assertEqual(
77        'gala apples',
78        provider.GetContentAndType(_HOST, 'gala.txt').Get().content)
79    self.assertEqual(
80        'granny smith apples',
81        provider.GetContentAndType(_HOST, 'green/granny smith.txt').Get()
82            .content)
83
84  def testComplexRootPath(self):
85    provider = self._content_providers.GetByName('tomatoes')
86    self.assertEqual(
87        'no they aren\'t',
88        provider.GetContentAndType(_HOST, 'vegetable.txt').Get().content)
89    self.assertEqual(
90        'cherry tomatoes',
91        provider.GetContentAndType(_HOST, 'fruit/cherry.txt').Get().content)
92
93  def testEmptyRootPath(self):
94    provider = self._content_providers.GetByName('bananas')
95    self.assertEqual(
96        'gala apples',
97        provider.GetContentAndType(_HOST, 'apples/gala.txt').Get().content)
98
99  def testSimpleServlet(self):
100    provider, path = self._content_providers.GetByServeFrom('apples-dir')
101    self.assertEqual('apples', provider.name)
102    self.assertEqual('', path)
103    provider, path = self._content_providers.GetByServeFrom(
104        'apples-dir/are/forever')
105    self.assertEqual('apples', provider.name)
106    self.assertEqual('are/forever', path)
107
108  def testComplexServlet(self):
109    provider, path = self._content_providers.GetByServeFrom(
110        'tomatoes-dir/are/a')
111    self.assertEqual('tomatoes', provider.name)
112    self.assertEqual('', path)
113    provider, path = self._content_providers.GetByServeFrom(
114        'tomatoes-dir/are/a/fruit/they/are')
115    self.assertEqual('tomatoes', provider.name)
116    self.assertEqual('fruit/they/are', path)
117
118  def testEmptyStringServlet(self):
119    provider, path = self._content_providers.GetByServeFrom('tomatoes-dir/are')
120    self.assertEqual('bananas', provider.name)
121    self.assertEqual('tomatoes-dir/are', path)
122    provider, path = self._content_providers.GetByServeFrom('')
123    self.assertEqual('bananas', provider.name)
124    self.assertEqual('', path)
125
126  @DisableLogging('error')
127  def testProviderNotFound(self):
128    self.assertEqual(None, self._content_providers.GetByName('cabbages'))
129
130
131if __name__ == '__main__':
132  unittest.main()
133