Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / tvcm / module_unittest.py
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 """Tests for the module module, which contains Module and related classes."""
6
7 import unittest
8
9 from tvcm import fake_fs
10 from tvcm import module
11 from tvcm import strip_js_comments
12 from tvcm import resource_loader
13 from tvcm import project as project_module
14
15 class ModuleIntegrationTests(unittest.TestCase):
16   def test_module(self):
17     fs = fake_fs.FakeFS()
18     fs.AddFile('/src/x.html', """
19 <!DOCTYPE html>
20 <link rel="import" href="/y.html">
21 <link rel="import" href="/z.html">
22 <script>
23 'use strict';
24 </script>
25 """)
26     fs.AddFile('/src/y.html', """
27 <!DOCTYPE html>
28 <link rel="import" href="/z.html">
29 """)
30     fs.AddFile('/src/z.html', """
31 <!DOCTYPE html>
32 """)
33     fs.AddFile('/src/tvcm.html', '<!DOCTYPE html>')
34     with fs:
35       project = project_module.Project(['/src/'],
36                                        include_tvcm_paths=False)
37       loader = resource_loader.ResourceLoader(project)
38       x_module = loader.LoadModule('x')
39
40       self.assertEquals([loader.loaded_modules['y'],
41                          loader.loaded_modules['z']],
42                         x_module.dependent_modules)
43
44       already_loaded_set = set()
45       load_sequence = []
46       x_module.ComputeLoadSequenceRecursive(load_sequence, already_loaded_set)
47
48       self.assertEquals([loader.loaded_modules['z'],
49                          loader.loaded_modules['y'],
50                          x_module],
51                         load_sequence)
52
53   def testBasic(self):
54     fs = fake_fs.FakeFS()
55     fs.AddFile('/x/src/my_module.html', """
56 <!DOCTYPE html>
57 <link rel="import" href="/tvcm/foo.html">
58 });
59 """)
60     fs.AddFile('/x/tvcm/foo.html', """
61 <!DOCTYPE html>
62 });
63 """);
64     project = project_module.Project(['/x'],
65                                      include_tvcm_paths=False)
66     loader = resource_loader.ResourceLoader(project)
67     with fs:
68       my_module = loader.LoadModule(module_name = 'src.my_module')
69       dep_names = [x.name for x in my_module.dependent_modules]
70       self.assertEquals(['tvcm.foo'], dep_names)
71
72   def testDepsExceptionContext(self):
73     fs = fake_fs.FakeFS()
74     fs.AddFile('/x/src/my_module.html', """
75 <!DOCTYPE html>
76 <link rel="import" href="/tvcm/foo.html">
77 """)
78     fs.AddFile('/x/tvcm/foo.html', """
79 <!DOCTYPE html>
80 <link rel="import" href="missing.html">
81 """);
82     project = project_module.Project(['/x'],
83                                      include_tvcm_paths=False)
84     loader = resource_loader.ResourceLoader(project)
85     with fs:
86       exc = None
87       try:
88         my_module = loader.LoadModule(module_name = 'src.my_module')
89         assertFalse('Expected an exception')
90       except module.DepsException, e:
91         exc = e
92       self.assertEquals(
93         ['src.my_module', 'tvcm.foo'],
94         exc.context)
95
96
97
98   def testGetAllDependentFilenamesRecursive(self):
99     fs = fake_fs.FakeFS()
100     fs.AddFile('/x/y/z/foo.html', """
101 <!DOCTYPE html>
102 <link rel="import" href="/z/foo2.html">
103 <link rel="stylesheet" href="/z/foo.css">
104 <script src="/bar.js"></script>
105 """)
106     fs.AddFile('/x/y/z/foo.css', """
107 .x .y {
108     background-image: url(foo.jpeg);
109 }
110 """)
111     fs.AddFile('/x/y/z/foo.jpeg', '')
112     fs.AddFile('/x/y/z/foo2.html', """
113 <!DOCTYPE html>
114 """);
115     fs.AddFile('/x/raw/bar.js', 'hello');
116     project = project_module.Project(['/x/y', '/x/raw/'],
117                                      include_tvcm_paths=False)
118     loader = resource_loader.ResourceLoader(project)
119     with fs:
120       my_module = loader.LoadModule(module_name='z.foo')
121       self.assertEquals(1, len(my_module.dependent_raw_scripts))
122
123       dependent_filenames = my_module.GetAllDependentFilenamesRecursive()
124       self.assertEquals([
125         '/x/y/z/foo.html',
126         '/x/raw/bar.js',
127         '/x/y/z/foo.css',
128         '/x/y/z/foo.jpeg',
129         '/x/y/z/foo2.html'
130       ], dependent_filenames)