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.
6 from copy
import deepcopy
7 from file_system
import FileNotFoundError
, StatInfo
8 from mock_file_system
import MockFileSystem
9 from test_file_system
import TestFileSystem
13 '404.html': '404.html contents',
15 'a11y.html': 'a11y.html contents',
16 'about_apps.html': 'about_apps.html contents',
18 'file.html': 'file.html contents'
22 'activeTab.html': 'activeTab.html contents',
23 'alarms.html': 'alarms.html contents'
28 '''Returns a function which calls Future.Get on the result of |fn|.
30 return lambda *args
: fn(*args
).Get()
32 class MockFileSystemTest(unittest
.TestCase
):
33 def testCheckAndReset(self
):
34 fs
= MockFileSystem(TestFileSystem(deepcopy(_TEST_DATA
)))
36 self
.assertTrue(*fs
.CheckAndReset())
37 self
.assertFalse(*fs
.CheckAndReset(read_count
=1))
38 self
.assertFalse(*fs
.CheckAndReset(stat_count
=1))
40 future
= fs
.ReadSingle('apps/')
41 self
.assertTrue(*fs
.CheckAndReset(read_count
=1))
43 self
.assertTrue(*fs
.CheckAndReset(read_resolve_count
=1))
44 self
.assertFalse(*fs
.CheckAndReset(read_count
=1))
45 self
.assertTrue(*fs
.CheckAndReset())
47 future
= fs
.ReadSingle('apps/')
48 self
.assertFalse(*fs
.CheckAndReset(read_count
=2))
50 self
.assertFalse(*fs
.CheckAndReset(read_resolve_count
=2))
52 fs
.ReadSingle('extensions/').Get()
53 fs
.ReadSingle('extensions/').Get()
54 self
.assertTrue(*fs
.CheckAndReset(read_count
=2, read_resolve_count
=2))
55 self
.assertFalse(*fs
.CheckAndReset(read_count
=2, read_resolve_count
=2))
56 self
.assertTrue(*fs
.CheckAndReset())
58 fs
.ReadSingle('404.html').Get()
59 self
.assertTrue(*fs
.CheckAndReset(read_count
=1, read_resolve_count
=1))
60 future
= fs
.Read(['notfound.html', 'apps/'])
61 self
.assertTrue(*fs
.CheckAndReset(read_count
=1))
62 self
.assertRaises(FileNotFoundError
, future
.Get
)
63 self
.assertTrue(*fs
.CheckAndReset(read_resolve_count
=0))
68 self
.assertFalse(*fs
.CheckAndReset(stat_count
=42))
69 self
.assertFalse(*fs
.CheckAndReset(stat_count
=42))
70 self
.assertTrue(*fs
.CheckAndReset())
72 fs
.ReadSingle('404.html').Get()
76 *fs
.CheckAndReset(read_count
=1, read_resolve_count
=1, stat_count
=2))
77 self
.assertTrue(*fs
.CheckAndReset())
79 def testUpdates(self
):
80 fs
= MockFileSystem(TestFileSystem(deepcopy(_TEST_DATA
)))
82 self
.assertEqual(StatInfo('0', child_versions
={
87 self
.assertEqual(StatInfo('0'), fs
.Stat('404.html'))
88 self
.assertEqual(StatInfo('0', child_versions
={
90 'about_apps.html': '0',
93 self
.assertEqual('404.html contents', fs
.ReadSingle('404.html').Get())
96 '404.html': 'New version!'
99 self
.assertEqual(StatInfo('1', child_versions
={
104 self
.assertEqual(StatInfo('1'), fs
.Stat('404.html'))
105 self
.assertEqual(StatInfo('0', child_versions
={
107 'about_apps.html': '0',
109 }), fs
.Stat('apps/'))
110 self
.assertEqual('New version!', fs
.ReadSingle('404.html').Get())
113 '404.html': 'Newer version!',
121 self
.assertEqual(StatInfo('2', child_versions
={
126 self
.assertEqual(StatInfo('2'), fs
.Stat('404.html'))
127 self
.assertEqual(StatInfo('2', child_versions
={
129 'about_apps.html': '0',
131 }), fs
.Stat('apps/'))
132 self
.assertEqual(StatInfo('0'), fs
.Stat('apps/a11y.html'))
133 self
.assertEqual(StatInfo('2', child_versions
={
135 }), fs
.Stat('apps/fakedir/'))
136 self
.assertEqual(StatInfo('2'), fs
.Stat('apps/fakedir/file.html'))
137 self
.assertEqual(StatInfo('0', child_versions
={
138 'activeTab.html': '0',
140 }), fs
.Stat('extensions/'))
141 self
.assertEqual('Newer version!', fs
.ReadSingle('404.html').Get())
142 self
.assertEqual('yo', fs
.ReadSingle('apps/fakedir/file.html').Get())
144 if __name__
== '__main__':