d5ee3f506a5abdbf2a6ea9687b45499e8f57757e
[WebKit-https.git] / 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.system.filesystem_mock import MockFileSystem
33 from webkitpy.tool.mocktool import MockSCM
34
35
36 class TestBaselineOptimizer(BaselineOptimizer):
37     def __init__(self, mock_results_by_directory):
38         BaselineOptimizer.__init__(self, MockSCM(), MockFileSystem())
39         self._mock_results_by_directory = mock_results_by_directory
40
41     # We override this method for testing so we don't have to construct an
42     # elaborate mock file system.
43     def _read_results_by_directory(self, baseline_name):
44         return self._mock_results_by_directory
45
46
47 class BaselineOptimizerTest(unittest.TestCase):
48     def _assertOptimization(self, results_by_directory, expected_new_results_by_directory):
49         baseline_optimizer = TestBaselineOptimizer(results_by_directory)
50         _, new_results_by_directory = baseline_optimizer._find_optimal_result_placement('mock-baseline.png')
51         self.assertEqual(new_results_by_directory, expected_new_results_by_directory)
52
53     def test_move_baselines(self):
54         fs = MockFileSystem()
55         fs.write_binary_file('/mock-checkout/LayoutTests/platform/chromium-win/another/test-expected.txt', 'result A')
56         fs.write_binary_file('/mock-checkout/LayoutTests/platform/chromium-cg-mac/another/test-expected.txt', 'result A')
57         fs.write_binary_file('/mock-checkout/LayoutTests/platform/chromium/another/test-expected.txt', 'result B')
58         baseline_optimizer = BaselineOptimizer(MockSCM(), fs)
59         baseline_optimizer._move_baselines('another/test-expected.txt', {
60             'LayoutTests/platform/chromium-win': 'aaa',
61             'LayoutTests/platform/chromium-cg-mac': 'aaa',
62             'LayoutTests/platform/chromium': 'bbb',
63         }, {
64             'LayoutTests/platform/chromium': 'aaa',
65         })
66         self.assertEqual(fs.read_binary_file('/mock-checkout/LayoutTests/platform/chromium/another/test-expected.txt'), 'result A')
67
68     def test_chromium_linux_redundant_with_win(self):
69         self._assertOptimization({
70             'LayoutTests/platform/chromium-win': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
71             'LayoutTests/platform/chromium-linux': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
72         }, {
73             'LayoutTests/platform/chromium-win': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
74         })
75
76     def test_chromium_covers_mac_win_linux(self):
77         self._assertOptimization({
78             'LayoutTests/platform/chromium-cg-mac': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
79             'LayoutTests/platform/chromium-win': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
80             'LayoutTests/platform/chromium-linux': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
81         }, {
82             'LayoutTests/platform/chromium': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
83         })
84
85     def test_chromium_mac_redundant_with_apple_mac(self):
86         self._assertOptimization({
87             'LayoutTests/platform/chromium-cg-mac-snowleopard': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
88             'LayoutTests/platform/mac-snowleopard': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
89         }, {
90             'LayoutTests/platform/mac-snowleopard': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
91         })
92
93     def test_mac_future(self):
94         self._assertOptimization({
95             'LayoutTests/platform/mac-snowleopard': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
96         }, {
97             'LayoutTests/platform/mac-snowleopard': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
98         })
99
100     def test_qt_unknown(self):
101         self._assertOptimization({
102             'LayoutTests/platform/qt': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
103         }, {
104             'LayoutTests/platform/qt': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
105         })
106
107     def test_common_directory_includes_root(self):
108         # Note: The resulting directories are "wrong" in the sense that
109         # enacting this plan would change semantics. However, this test case
110         # demonstrates that we don't throw an exception in this case. :)
111         self._assertOptimization({
112             'LayoutTests/platform/gtk': 'e8608763f6241ddacdd5c1ef1973ba27177d0846',
113             'LayoutTests/platform/qt': 'bcbd457d545986b7abf1221655d722363079ac87',
114             'LayoutTests/platform/chromium-win': '3764ac11e1f9fbadd87a90a2e40278319190a0d3',
115             'LayoutTests/platform/mac': 'e8608763f6241ddacdd5c1ef1973ba27177d0846',
116         }, {
117             'LayoutTests/platform/qt': 'bcbd457d545986b7abf1221655d722363079ac87',
118             'LayoutTests/platform/chromium-win': '3764ac11e1f9fbadd87a90a2e40278319190a0d3',
119             'LayoutTests': 'e8608763f6241ddacdd5c1ef1973ba27177d0846',
120         })
121
122         self._assertOptimization({
123             'LayoutTests/platform/chromium-win': '23a30302a6910f8a48b1007fa36f3e3158341834',
124             'LayoutTests': '9c876f8c3e4cc2aef9519a6c1174eb3432591127',
125             'LayoutTests/platform/chromium-cg-mac': '23a30302a6910f8a48b1007fa36f3e3158341834',
126             'LayoutTests/platform/chromium-mac': '23a30302a6910f8a48b1007fa36f3e3158341834',
127         }, {
128             'LayoutTests/platform/chromium': '23a30302a6910f8a48b1007fa36f3e3158341834',
129             'LayoutTests': '9c876f8c3e4cc2aef9519a6c1174eb3432591127',
130         })
131
132     def test_complex_shadowing(self):
133         self._assertOptimization({
134             'LayoutTests/platform/chromium-win': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
135             'LayoutTests/platform/mac': '5daa78e55f05d9f0d1bb1f32b0cd1bc3a01e9364',
136             'LayoutTests/platform/chromium-win-xp': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
137             'LayoutTests/platform/chromium-cg-mac-leopard': '65e7d42f8b4882b29d46dc77bb879dd41bc074dc',
138             'LayoutTests/platform/mac-leopard': '7ad045ece7c030e2283c5d21d9587be22bcba56e',
139             'LayoutTests/platform/chromium-win-vista': 'f83af9732ce74f702b8c9c4a3d9a4c6636b8d3bd',
140             'LayoutTests/platform/win': '5b1253ef4d5094530d5f1bc6cdb95c90b446bec7',
141             'LayoutTests/platform/chromium-linux': 'f52fcdde9e4be8bd5142171cd859230bd4471036',
142         }, {
143             'LayoutTests/platform/chromium-win': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
144             'LayoutTests/platform/mac': '5daa78e55f05d9f0d1bb1f32b0cd1bc3a01e9364',
145             'LayoutTests/platform/chromium-win-xp': '462d03b9c025db1b0392d7453310dbee5f9a9e74',
146             'LayoutTests/platform/chromium-cg-mac-leopard': '65e7d42f8b4882b29d46dc77bb879dd41bc074dc',
147             'LayoutTests/platform/mac-leopard': '7ad045ece7c030e2283c5d21d9587be22bcba56e',
148             'LayoutTests/platform/chromium-win-vista': 'f83af9732ce74f702b8c9c4a3d9a4c6636b8d3bd',
149             'LayoutTests/platform/win': '5b1253ef4d5094530d5f1bc6cdb95c90b446bec7',
150             'LayoutTests/platform/chromium-linux': 'f52fcdde9e4be8bd5142171cd859230bd4471036'
151         })