Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / template_data_source_test.py
1 #!/usr/bin/env python
2 # Copyright (c) 2012 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
6 import json
7 import unittest
8
9 from extensions_paths import SERVER2
10 from server_instance import ServerInstance
11 from template_data_source import TemplateDataSource
12 from test_util import DisableLogging, ReadFile
13 from third_party.motemplate import Motemplate
14
15 def _ReadFile(*path):
16   return ReadFile(SERVER2, 'test_data', 'template_data_source', *path)
17
18 def _CreateTestDataSource(base_dir):
19   '''TemplateDataSource is not instantiated directly, rather, its methods
20   are invoked through a subclass of it, which has as its only data the
21   directory in which TemplateDataSource methods should act on. Thus, we test
22   TemplateDataSource indirectly through the TestDataSource class
23   '''
24   return TestDataSource(ServerInstance.ForLocal(),
25                         '%stest_data/template_data_source/%s/' %
26                         (SERVER2, base_dir))
27
28
29 class TestDataSource(TemplateDataSource):
30   '''Provides a subclass we can use to test the TemplateDataSource methods
31   '''
32   def __init__(self, server_instance, base_dir):
33     type(self)._BASE = base_dir
34     TemplateDataSource.__init__(self, server_instance)
35
36
37 class TemplateDataSourceTest(unittest.TestCase):
38
39   def testSimple(self):
40     test_data_source = _CreateTestDataSource('simple')
41     template_a1 = Motemplate(_ReadFile('simple', 'test1.html'))
42     context = [{}, {'templates': {}}]
43     self.assertEqual(
44         template_a1.Render(*context).text,
45         test_data_source.get('test1').Render(*context).text)
46     template_a2 = Motemplate(_ReadFile('simple', 'test2.html'))
47     self.assertEqual(
48         template_a2.Render(*context).text,
49         test_data_source.get('test2').Render(*context).text)
50
51   @DisableLogging('warning')
52   def testNotFound(self):
53     test_data_source = _CreateTestDataSource('simple')
54     self.assertEqual(None, test_data_source.get('junk'))
55
56   @DisableLogging('warning')
57   def testPartials(self):
58     test_data_source = _CreateTestDataSource('partials')
59     context = json.loads(_ReadFile('partials', 'input.json'))
60     self.assertEqual(
61         _ReadFile('partials', 'test_expected.html'),
62         test_data_source.get('test_tmpl').Render(
63             context, test_data_source).text)
64
65
66 if __name__ == '__main__':
67   unittest.main()