Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Tools / Scripts / webkitpy / common / checkout / baselineoptimizer_unittest.py
1 # Copyright (C) 2011 Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
7 #    * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 #    * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 #    * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 import unittest
30
31 from webkitpy.common.checkout.baselineoptimizer import BaselineOptimizer
32 from webkitpy.common.checkout.scm.scm_mock import MockSCM
33 from webkitpy.common.host_mock import MockHost
34 from webkitpy.common.webkit_finder import WebKitFinder
35
36
37 class ExcludingMockSCM(MockSCM):
38     def __init__(self, exclusion_list, filesystem=None, executive=None):
39         MockSCM.__init__(self, filesystem, executive)
40         self._exclusion_list = exclusion_list
41
42     def exists(self, path):
43         if path in self._exclusion_list:
44             return False
45         return MockSCM.exists(self, path)
46
47     def delete(self, path):
48         return self.delete_list([path])
49
50     def delete_list(self, paths):
51         for path in paths:
52             if path in self._exclusion_list:
53                 raise Exception("File is not SCM managed: " + path)
54         return MockSCM.delete_list(self, paths)
55
56     def move(self, origin, destination):
57         if origin in self._exclusion_list:
58             raise Exception("File is not SCM managed: " + origin)
59         return MockSCM.move(self, origin, destination)
60
61
62 class BaselineOptimizerTest(unittest.TestCase):
63     def test_move_baselines(self):
64         host = MockHost(scm=ExcludingMockSCM(['/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/another/test-expected.txt']))
65         host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/platform/win/another/test-expected.txt', 'result A')
66         host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/another/test-expected.txt', 'result A')
67         host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/another/test-expected.txt', 'result B')
68         baseline_optimizer = BaselineOptimizer(host, host.port_factory.all_port_names(), skip_scm_commands=False)
69         baseline_optimizer._move_baselines('another/test-expected.txt', {
70             '/mock-checkout/third_party/WebKit/LayoutTests/platform/win': 'aaa',
71             '/mock-checkout/third_party/WebKit/LayoutTests/platform/mac': 'aaa',
72             '/mock-checkout/third_party/WebKit/LayoutTests': 'bbb',
73         }, {
74             '/mock-checkout/third_party/WebKit/LayoutTests': 'aaa',
75         })
76         self.assertEqual(host.filesystem.read_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/another/test-expected.txt'), 'result A')
77
78     def test_move_baselines_skip_scm_commands(self):
79         host = MockHost(scm=ExcludingMockSCM(['/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/another/test-expected.txt']))
80         host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/platform/win/another/test-expected.txt', 'result A')
81         host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/another/test-expected.txt', 'result A')
82         host.filesystem.write_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/another/test-expected.txt', 'result B')
83         baseline_optimizer = BaselineOptimizer(host, host.port_factory.all_port_names(), skip_scm_commands=True)
84         baseline_optimizer._move_baselines('another/test-expected.txt', {
85             '/mock-checkout/third_party/WebKit/LayoutTests/platform/win': 'aaa',
86             '/mock-checkout/third_party/WebKit/LayoutTests/platform/mac': 'aaa',
87             '/mock-checkout/third_party/WebKit/LayoutTests': 'bbb',
88         }, {
89             '/mock-checkout/third_party/WebKit/LayoutTests/platform/linux': 'bbb',
90             '/mock-checkout/third_party/WebKit/LayoutTests': 'aaa',
91         })
92         self.assertEqual(host.filesystem.read_binary_file('/mock-checkout/third_party/WebKit/LayoutTests/another/test-expected.txt'), 'result A')
93
94         self.assertEqual(baseline_optimizer._files_to_delete, [
95             '/mock-checkout/third_party/WebKit/LayoutTests/platform/win/another/test-expected.txt',
96         ])
97
98         self.assertEqual(baseline_optimizer._files_to_add, [
99             '/mock-checkout/third_party/WebKit/LayoutTests/another/test-expected.txt',
100             '/mock-checkout/third_party/WebKit/LayoutTests/platform/linux/another/test-expected.txt',
101         ])
102
103     def _assertOptimization(self, results_by_directory, expected_new_results_by_directory, baseline_dirname='', expected_files_to_delete=None, host=None):
104         if not host:
105             host = MockHost()
106         fs = host.filesystem
107         webkit_base = WebKitFinder(fs).webkit_base()
108         baseline_name = 'mock-baseline-expected.txt'
109
110         for dirname, contents in results_by_directory.items():
111             path = fs.join(webkit_base, 'LayoutTests', dirname, baseline_name)
112             fs.write_binary_file(path, contents)
113
114         baseline_optimizer = BaselineOptimizer(host, host.port_factory.all_port_names(), skip_scm_commands=expected_files_to_delete is not None)
115         self.assertTrue(baseline_optimizer.optimize(fs.join(baseline_dirname, baseline_name)))
116
117         for dirname, contents in expected_new_results_by_directory.items():
118             path = fs.join(webkit_base, 'LayoutTests', dirname, baseline_name)
119             if contents is None:
120                 self.assertTrue(not fs.exists(path) or path in baseline_optimizer._files_to_delete)
121             else:
122                 self.assertEqual(fs.read_binary_file(path), contents)
123
124         # Check that the files that were in the original set have been deleted where necessary.
125         for dirname in results_by_directory:
126             path = fs.join(webkit_base, 'LayoutTests', dirname, baseline_name)
127             if not dirname in expected_new_results_by_directory:
128                 self.assertTrue(not fs.exists(path) or path in baseline_optimizer._files_to_delete)
129
130         if expected_files_to_delete:
131             self.assertEqual(baseline_optimizer._files_to_delete, expected_files_to_delete)
132
133     def test_linux_redundant_with_win(self):
134         self._assertOptimization({
135             'platform/win': '1',
136             'platform/linux': '1',
137         }, {
138             'platform/win': '1',
139         })
140
141     def test_covers_mac_win_linux(self):
142         self._assertOptimization({
143             'platform/mac': '1',
144             'platform/win': '1',
145             'platform/linux': '1',
146             '': None,
147         }, {
148             '': '1',
149         })
150
151     def test_overwrites_root(self):
152         self._assertOptimization({
153             'platform/mac': '1',
154             'platform/win': '1',
155             'platform/linux': '1',
156             '': '2',
157         }, {
158             '': '1',
159         })
160
161     def test_no_new_common_directory(self):
162         self._assertOptimization({
163             'platform/mac': '1',
164             'platform/linux': '1',
165             '': '2',
166         }, {
167             'platform/mac': '1',
168             'platform/linux': '1',
169             '': '2',
170         })
171
172
173     def test_local_optimization(self):
174         self._assertOptimization({
175             'platform/mac': '1',
176             'platform/linux': '1',
177             'platform/linux-x86': '1',
178         }, {
179             'platform/mac': '1',
180             'platform/linux': '1',
181         })
182
183     def test_local_optimization_skipping_a_port_in_the_middle(self):
184         self._assertOptimization({
185             'platform/mac-snowleopard': '1',
186             'platform/win': '1',
187             'platform/linux-x86': '1',
188         }, {
189             'platform/mac-snowleopard': '1',
190             'platform/win': '1',
191         })
192
193     def test_baseline_redundant_with_root(self):
194         self._assertOptimization({
195             'platform/mac': '1',
196             'platform/win': '2',
197             '': '2',
198         }, {
199             'platform/mac': '1',
200             '': '2',
201         })
202
203     def test_root_baseline_unused(self):
204         self._assertOptimization({
205             'platform/mac': '1',
206             'platform/win': '2',
207             '': '3',
208         }, {
209             'platform/mac': '1',
210             'platform/win': '2',
211         })
212
213     def test_root_baseline_unused_and_non_existant(self):
214         self._assertOptimization({
215             'platform/mac': '1',
216             'platform/win': '2',
217         }, {
218             'platform/mac': '1',
219             'platform/win': '2',
220         })
221
222     def test_virtual_root_redundant_with_actual_root(self):
223         self._assertOptimization({
224             'virtual/softwarecompositing': '2',
225             'compositing': '2',
226         }, {
227             'virtual/softwarecompositing': None,
228             'compositing': '2',
229         }, baseline_dirname='virtual/softwarecompositing')
230
231     def test_virtual_root_redundant_with_ancestors(self):
232         self._assertOptimization({
233             'virtual/softwarecompositing': '2',
234             'platform/mac/compositing': '2',
235             'platform/win/compositing': '2',
236         }, {
237             'virtual/softwarecompositing': None,
238             'compositing': '2',
239         }, baseline_dirname='virtual/softwarecompositing')
240
241     def test_virtual_root_redundant_with_ancestors_skip_scm_commands(self):
242         self._assertOptimization({
243             'virtual/softwarecompositing': '2',
244             'platform/mac/compositing': '2',
245             'platform/win/compositing': '2',
246         }, {
247             'virtual/softwarecompositing': None,
248             'compositing': '2',
249         },
250         baseline_dirname='virtual/softwarecompositing',
251         expected_files_to_delete=[
252             '/mock-checkout/third_party/WebKit/LayoutTests/virtual/softwarecompositing/mock-baseline-expected.txt',
253             '/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/compositing/mock-baseline-expected.txt',
254             '/mock-checkout/third_party/WebKit/LayoutTests/platform/win/compositing/mock-baseline-expected.txt',
255         ])
256
257     def test_virtual_root_redundant_with_ancestors_skip_scm_commands_with_file_not_in_scm(self):
258         self._assertOptimization({
259             'virtual/softwarecompositing': '2',
260             'platform/mac/compositing': '2',
261             'platform/win/compositing': '2',
262         }, {
263             'virtual/softwarecompositing': None,
264             'compositing': '2',
265         },
266         baseline_dirname='virtual/softwarecompositing',
267         expected_files_to_delete=[
268             '/mock-checkout/third_party/WebKit/LayoutTests/platform/mac/compositing/mock-baseline-expected.txt',
269             '/mock-checkout/third_party/WebKit/LayoutTests/platform/win/compositing/mock-baseline-expected.txt',
270         ],
271         host=MockHost(scm=ExcludingMockSCM(['/mock-checkout/third_party/WebKit/LayoutTests/virtual/softwarecompositing/mock-baseline-expected.txt'])))
272
273     def test_virtual_root_not_redundant_with_ancestors(self):
274         self._assertOptimization({
275             'virtual/softwarecompositing': '2',
276             'platform/mac/compositing': '1',
277         }, {
278             'virtual/softwarecompositing': '2',
279             'platform/mac/compositing': '1',
280         }, baseline_dirname='virtual/softwarecompositing')