Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Tools / Scripts / webkitpy / layout_tests / layout_tests_mover_unittest.py
1 # Copyright (C) 2013 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 logging
30 import unittest
31
32 from webkitpy.common.host_mock import MockHost
33 from webkitpy.common.checkout.scm.scm_mock import MockSCM
34 from webkitpy.common.system.filesystem_mock import MockFileSystem
35 from webkitpy.layout_tests.layout_tests_mover import LayoutTestsMover
36 from webkitpy.layout_tests.port import base
37
38
39 class MockPort(base.Port):
40
41     def __init__(self, **kwargs):
42         # This sets up a mock FileSystem and SCM using that FileSystem.
43         host = MockHost()
44         super(MockPort, self).__init__(host, host.port_factory.all_port_names()[0], **kwargs)
45
46         host.filesystem.maybe_make_directory(self._absolute_path('platform'))
47         host.filesystem.maybe_make_directory(self._absolute_path('existing_directory'))
48         host.filesystem.write_text_file(self._absolute_path('existing_file.txt'), '')
49         host.filesystem.write_text_file(self._absolute_path('TestExpectations'), """
50 crbug.com/42 [ Debug ] origin/path/test.html [ Pass Timeout Failure ]
51 crbug.com/42 [ Win ] origin/path [ Slow ]
52 crbug.com/42 [ Release ] origin [ Crash ]
53 """)
54         host.filesystem.write_text_file(self._absolute_path('existing_directory_with_contents', 'test.html'), '')
55         host.filesystem.write_text_file(self._absolute_path('origin', 'path', 'test.html'), """
56 <script src="local_script.js">
57 <script src="../../unmoved/remote_script.js">
58 <script src='../../unmoved/remote_script_single_quotes.js'>
59 <script href="../../unmoved/remote_script.js">
60 <script href='../../unmoved/remote_script_single_quotes.js'>
61 <script href="">
62 """)
63         host.filesystem.write_text_file(self._absolute_path('origin', 'path', 'test.css'), """
64 url('../../unmoved/url_function.js')
65 url("../../unmoved/url_function_double_quotes.js")
66 url(../../unmoved/url_function_no_quotes.js)
67 url('')
68 url()
69 """)
70         host.filesystem.write_text_file(self._absolute_path('origin', 'path', 'test.js'), """
71 importScripts('../../unmoved/import_scripts_function.js')
72 importScripts("../../unmoved/import_scripts_function_double_quotes.js")
73 importScripts('')
74 """)
75         host.filesystem.write_text_file(self._absolute_path('unmoved', 'test.html'), """
76 <script src="local_script.js">
77 <script src="../origin/path/remote_script.js">
78 """)
79
80     def _absolute_path(self, *paths):
81         return self.host.scm().absolute_path('LayoutTests', *paths)
82
83     def layout_tests_dir(self):
84         return self._absolute_path()
85
86
87 class LayoutTestsMoverTest(unittest.TestCase):
88
89     def setUp(self):
90         port = MockPort()
91         self._port = port
92         self._filesystem = self._port.host.filesystem
93         self._mover = LayoutTestsMover(port=self._port)
94
95     def test_non_existent_origin_raises(self):
96         self.assertRaises(Exception, self._mover.move, 'non_existent', 'destination')
97
98     def test_origin_outside_layout_tests_directory_raises(self):
99         self.assertRaises(Exception, self._mover.move, '../outside', 'destination')
100
101     def test_file_destination_raises(self):
102         self.assertRaises(Exception, self._mover.move, 'origin/path', 'existing_file.txt')
103
104     def test_destination_outside_layout_tests_directory_raises(self):
105         self.assertRaises(Exception, self._mover.move, 'origin/path', '../outside')
106
107     def test_basic_operation(self):
108         self._mover.move('origin/path', 'destination')
109         self.assertFalse(self._filesystem.exists(self._port._absolute_path('origin/path')))
110         self.assertTrue(self._filesystem.isfile(self._port._absolute_path('destination/test.html')))
111
112     def test_move_to_existing_directory(self):
113         self._mover.move('origin/path', 'existing_directory')
114         self.assertFalse(self._filesystem.exists(self._port._absolute_path('origin', 'path')))
115         self.assertTrue(self._filesystem.isfile(self._port._absolute_path('existing_directory', 'test.html')))
116
117     def test_collision_in_existing_directory_raises(self):
118         self.assertRaises(Exception, self._mover.move, 'origin/path', 'existing_directory_with_contents')
119
120     def test_move_to_layout_tests_root(self):
121         self._mover.move('origin/path', '')
122         self.assertFalse(self._filesystem.exists(self._port._absolute_path('origin', 'path')))
123         self.assertTrue(self._filesystem.isfile(self._port._absolute_path('test.html')))
124
125     def test_moved_reference_in_moved_file_not_updated(self):
126         self._mover.move('origin/path', 'destination')
127         self.assertTrue('src="local_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html')))
128
129     def test_unmoved_reference_in_unmoved_file_not_updated(self):
130         self._mover.move('origin/path', 'destination')
131         self.assertTrue('src="local_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('unmoved', 'test.html')))
132
133     def test_moved_reference_in_unmoved_file_is_updated(self):
134         self._mover.move('origin/path', 'destination')
135         self.assertTrue('src="../destination/remote_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('unmoved', 'test.html')))
136
137     def test_unmoved_reference_in_moved_file_is_updated(self):
138         self._mover.move('origin/path', 'destination')
139         self.assertTrue('src="../unmoved/remote_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html')))
140
141     def test_references_in_html_file_are_updated(self):
142         self._mover.move('origin/path', 'destination')
143         self.assertTrue('src="../unmoved/remote_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html')))
144         self.assertTrue('src=\'../unmoved/remote_script_single_quotes.js\'' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html')))
145         self.assertTrue('href="../unmoved/remote_script.js"' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html')))
146         self.assertTrue('href=\'../unmoved/remote_script_single_quotes.js\'' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html')))
147         self.assertTrue('href=""' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.html')))
148
149     def test_references_in_css_file_are_updated(self):
150         self._mover.move('origin/path', 'destination')
151         self.assertTrue('url(\'../unmoved/url_function.js\')' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.css')))
152         self.assertTrue('url("../unmoved/url_function_double_quotes.js")' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.css')))
153         self.assertTrue('url(../unmoved/url_function_no_quotes.js)' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.css')))
154         self.assertTrue('url(\'\')' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.css')))
155         self.assertTrue('url()' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.css')))
156
157     def test_references_in_javascript_file_are_updated(self):
158         self._mover.move('origin/path', 'destination')
159         self.assertTrue('importScripts(\'../unmoved/import_scripts_function.js\')' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.js')))
160         self.assertTrue('importScripts("../unmoved/import_scripts_function_double_quotes.js")' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.js')))
161         self.assertTrue('importScripts(\'\')' in self._filesystem.read_text_file(self._port._absolute_path('destination', 'test.js')))
162
163     def test_expectation_is_updated(self):
164         self._mover.move('origin/path', 'destination')
165         self.assertFalse('origin/path/test.html' in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations')))
166         self.assertTrue('crbug.com/42 [ Debug ] destination/test.html [ Pass Timeout Failure ]'
167                         in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations')))
168
169     def test_directory_expectation_is_updated(self):
170         self._mover.move('origin/path', 'destination')
171         self.assertFalse('origin/path' in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations')))
172         self.assertTrue('crbug.com/42 [ Win ] destination [ Slow ]' in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations')))
173
174     def test_expectation_is_added_when_subdirectory_moved(self):
175         self._mover.move('origin/path', 'destination')
176         self.assertTrue('crbug.com/42 [ Release ] origin [ Crash ]' in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations')))
177         self.assertTrue('crbug.com/42 [ Release ] destination [ Crash ]' in self._filesystem.read_text_file(self._port._absolute_path('TestExpectations')))