- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / common / extensions / docs / server2 / test_file_system_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 from copy import deepcopy
7 from file_system import FileNotFoundError, StatInfo
8 from test_file_system import TestFileSystem, _MoveTo
9 import unittest
10
11
12 _TEST_DATA = {
13   '404.html': '404.html contents',
14   'apps': {
15     'a11y.html': 'a11y.html contents',
16     'about_apps.html': 'about_apps.html contents',
17     'fakedir': {
18       'file.html': 'file.html contents'
19     }
20   },
21   'extensions': {
22     'activeTab.html': 'activeTab.html contents',
23     'alarms.html': 'alarms.html contents'
24   }
25 }
26
27
28 def _Get(fn):
29   '''Returns a function which calls Future.Get on the result of |fn|.
30   '''
31   return lambda *args: fn(*args).Get()
32
33
34 class TestFileSystemTest(unittest.TestCase):
35   def testEmptyFileSystem(self):
36     self._TestMetasyntacticPaths(TestFileSystem({}))
37
38   def testNonemptyFileNotFoundErrors(self):
39     fs = TestFileSystem(deepcopy(_TEST_DATA))
40     self._TestMetasyntacticPaths(fs)
41     self.assertRaises(FileNotFoundError, _Get(fs.Read), ['404.html/'])
42     self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo/'])
43     self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo.html'])
44     self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo.html'])
45     self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo/',
46                                                          'apps/foo.html'])
47     self.assertRaises(FileNotFoundError, _Get(fs.Read), ['apps/foo/',
48                                                          'apps/a11y.html'])
49
50   def _TestMetasyntacticPaths(self, fs):
51     self.assertRaises(FileNotFoundError, _Get(fs.Read), ['foo'])
52     self.assertRaises(FileNotFoundError, _Get(fs.Read), ['bar/'])
53     self.assertRaises(FileNotFoundError, _Get(fs.Read), ['bar/baz'])
54     self.assertRaises(FileNotFoundError, _Get(fs.Read), ['foo',
55                                                          'bar/',
56                                                          'bar/baz'])
57     self.assertRaises(FileNotFoundError, fs.Stat, 'foo')
58     self.assertRaises(FileNotFoundError, fs.Stat, 'bar/')
59     self.assertRaises(FileNotFoundError, fs.Stat, 'bar/baz')
60
61   def testNonemptySuccess(self):
62     fs = TestFileSystem(deepcopy(_TEST_DATA))
63     self.assertEqual('404.html contents', fs.ReadSingle('404.html').Get())
64     self.assertEqual('404.html contents', fs.ReadSingle('/404.html').Get())
65     self.assertEqual('a11y.html contents',
66                      fs.ReadSingle('apps/a11y.html').Get())
67     self.assertEqual(['404.html', 'apps/', 'extensions/'],
68                      sorted(fs.ReadSingle('/').Get()))
69     self.assertEqual(['a11y.html', 'about_apps.html', 'fakedir/'],
70                      sorted(fs.ReadSingle('apps/').Get()))
71     self.assertEqual(['a11y.html', 'about_apps.html', 'fakedir/'],
72                      sorted(fs.ReadSingle('/apps/').Get()))
73
74   def testReadFiles(self):
75     fs = TestFileSystem(deepcopy(_TEST_DATA))
76     self.assertEqual('404.html contents',
77                      fs.ReadSingle('404.html').Get())
78     self.assertEqual('404.html contents',
79                      fs.ReadSingle('/404.html').Get())
80     self.assertEqual('a11y.html contents',
81                      fs.ReadSingle('apps/a11y.html').Get())
82     self.assertEqual('a11y.html contents',
83                      fs.ReadSingle('/apps/a11y.html').Get())
84     self.assertEqual('file.html contents',
85                      fs.ReadSingle('apps/fakedir/file.html').Get())
86     self.assertEqual('file.html contents',
87                      fs.ReadSingle('/apps/fakedir/file.html').Get())
88
89   def testReadDirs(self):
90     fs = TestFileSystem(deepcopy(_TEST_DATA))
91     self.assertEqual(['404.html', 'apps/', 'extensions/'],
92                      sorted(fs.ReadSingle('/').Get()))
93     self.assertEqual(['a11y.html', 'about_apps.html', 'fakedir/'],
94                      sorted(fs.ReadSingle('/apps/').Get()))
95     self.assertEqual(['a11y.html', 'about_apps.html', 'fakedir/'],
96                      sorted(fs.ReadSingle('apps/').Get()))
97     self.assertEqual(['file.html'], fs.ReadSingle('/apps/fakedir/').Get())
98     self.assertEqual(['file.html'], fs.ReadSingle('apps/fakedir/').Get())
99
100   def testStat(self):
101     fs = TestFileSystem(deepcopy(_TEST_DATA))
102     self.assertRaises(FileNotFoundError, fs.Stat, 'foo')
103     self.assertRaises(FileNotFoundError, fs.Stat, '404.html/')
104     self.assertEquals(StatInfo('0'), fs.Stat('404.html'))
105     self.assertEquals(StatInfo('0', child_versions={
106                         'activeTab.html': '0',
107                         'alarms.html': '0',
108                       }), fs.Stat('extensions/'))
109
110     fs.IncrementStat()
111     self.assertEquals(StatInfo('1'), fs.Stat('404.html'))
112     self.assertEquals(StatInfo('1', child_versions={
113                         'activeTab.html': '1',
114                         'alarms.html': '1',
115                       }), fs.Stat('extensions/'))
116
117     fs.IncrementStat(path='404.html')
118     self.assertEquals(StatInfo('2'), fs.Stat('404.html'))
119     self.assertEquals(StatInfo('1', child_versions={
120                         'activeTab.html': '1',
121                         'alarms.html': '1',
122                       }), fs.Stat('extensions/'))
123
124     fs.IncrementStat()
125     self.assertEquals(StatInfo('3'), fs.Stat('404.html'))
126     self.assertEquals(StatInfo('2', child_versions={
127                         'activeTab.html': '2',
128                         'alarms.html': '2',
129                       }), fs.Stat('extensions/'))
130
131     fs.IncrementStat(path='extensions/')
132     self.assertEquals(StatInfo('3'), fs.Stat('404.html'))
133     self.assertEquals(StatInfo('3', child_versions={
134                         'activeTab.html': '2',
135                         'alarms.html': '2',
136                       }), fs.Stat('extensions/'))
137
138     fs.IncrementStat(path='extensions/alarms.html')
139     self.assertEquals(StatInfo('3'), fs.Stat('404.html'))
140     self.assertEquals(StatInfo('3', child_versions={
141                         'activeTab.html': '2',
142                         'alarms.html': '3',
143                       }), fs.Stat('extensions/'))
144
145   def testMoveTo(self):
146     self.assertEqual({'foo': {'a': 'b', 'c': 'd'}},
147                      _MoveTo('foo', {'a': 'b', 'c': 'd'}))
148     self.assertEqual({'foo': {'bar': {'a': 'b', 'c': 'd'}}},
149                      _MoveTo('foo/bar', {'a': 'b', 'c': 'd'}))
150     self.assertEqual({'foo': {'bar': {'baz': {'a': 'b'}}}},
151                      _MoveTo('foo/bar/baz', {'a': 'b'}))
152
153
154 if __name__ == '__main__':
155   unittest.main()