1# Copyright (C) 2013 Adobe Systems Incorporated. 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
5# are met:
6#
7# 1. Redistributions of source code must retain the above
8#    copyright notice, this list of conditions and the following
9#    disclaimer.
10# 2. Redistributions in binary form must reproduce the above
11#    copyright notice, this list of conditions and the following
12#    disclaimer in the documentation and/or other materials
13#    provided with the distribution.
14#
15# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER "AS IS" AND ANY
16# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
19# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
20# OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
24# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
25# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27
28import os
29import unittest
30
31from webkitpy.common.system.outputcapture import OutputCapture
32from webkitpy.w3c.test_parser import TestParser
33
34
35options = {'all': False, 'no_overwrite': False}
36
37
38class TestParserTest(unittest.TestCase):
39
40    def test_analyze_test_reftest_one_match(self):
41        test_html = """<head>
42<link rel="match" href="green-box-ref.xht" />
43</head>
44"""
45        test_path = '/some/madeup/path/'
46        parser = TestParser(options, test_path + 'somefile.html')
47        test_info = parser.analyze_test(test_contents=test_html)
48
49        self.assertNotEqual(test_info, None, 'did not find a test')
50        self.assertTrue('test' in test_info.keys(), 'did not find a test file')
51        self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
52        self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
53        self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
54        self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
55
56    def test_analyze_test_reftest_multiple_matches(self):
57        test_html = """<head>
58<link rel="match" href="green-box-ref.xht" />
59<link rel="match" href="blue-box-ref.xht" />
60<link rel="match" href="orange-box-ref.xht" />
61</head>
62"""
63        oc = OutputCapture()
64        oc.capture_output()
65        try:
66            test_path = '/some/madeup/path/'
67            parser = TestParser(options, test_path + 'somefile.html')
68            test_info = parser.analyze_test(test_contents=test_html)
69        finally:
70            _, _, logs = oc.restore_output()
71
72        self.assertNotEqual(test_info, None, 'did not find a test')
73        self.assertTrue('test' in test_info.keys(), 'did not find a test file')
74        self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
75        self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
76        self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
77        self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
78
79        self.assertEqual(logs, 'Multiple references are not supported. Importing the first ref defined in somefile.html\n')
80
81    def test_analyze_test_reftest_match_and_mismatch(self):
82        test_html = """<head>
83<link rel="match" href="green-box-ref.xht" />
84<link rel="match" href="blue-box-ref.xht" />
85<link rel="mismatch" href="orange-box-notref.xht" />
86</head>
87"""
88        oc = OutputCapture()
89        oc.capture_output()
90
91        try:
92            test_path = '/some/madeup/path/'
93            parser = TestParser(options, test_path + 'somefile.html')
94            test_info = parser.analyze_test(test_contents=test_html)
95        finally:
96            _, _, logs = oc.restore_output()
97
98        self.assertNotEqual(test_info, None, 'did not find a test')
99        self.assertTrue('test' in test_info.keys(), 'did not find a test file')
100        self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
101        self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
102        self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
103        self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
104
105        self.assertEqual(logs, 'Multiple references are not supported. Importing the first ref defined in somefile.html\n')
106
107    def test_analyze_test_reftest_with_ref_support_Files(self):
108        """ Tests analyze_test() using a reftest that has refers to a reference file outside of the tests directory and the reference file has paths to other support files """
109
110        test_html = """<html>
111<head>
112<link rel="match" href="../reference/green-box-ref.xht" />
113</head>
114"""
115        ref_html = """<head>
116<link href="support/css/ref-stylesheet.css" rel="stylesheet" type="text/css">
117<style type="text/css">
118    background-image: url("../../support/some-image.png")
119</style>
120</head>
121<body>
122<div><img src="../support/black96x96.png" alt="Image download support must be enabled" /></div>
123</body>
124</html>
125"""
126        test_path = '/some/madeup/path/'
127        parser = TestParser(options, test_path + 'somefile.html')
128        test_info = parser.analyze_test(test_contents=test_html, ref_contents=ref_html)
129
130        self.assertNotEqual(test_info, None, 'did not find a test')
131        self.assertTrue('test' in test_info.keys(), 'did not find a test file')
132        self.assertTrue('reference' in test_info.keys(), 'did not find a reference file')
133        self.assertTrue(test_info['reference'].startswith(test_path), 'reference path is not correct')
134        self.assertTrue('refsupport' in test_info.keys(), 'there should be refsupport files for this test')
135        self.assertEquals(len(test_info['refsupport']), 3, 'there should be 3 support files in this reference')
136        self.assertFalse('jstest' in test_info.keys(), 'test should not have been analyzed as a jstest')
137
138    def test_analyze_jstest(self):
139        """ Tests analyze_test() using a jstest """
140
141        test_html = """<head>
142<link href="/resources/testharness.css" rel="stylesheet" type="text/css">
143<script src="/resources/testharness.js"></script>
144</head>
145"""
146        test_path = '/some/madeup/path/'
147        parser = TestParser(options, test_path + 'somefile.html')
148        test_info = parser.analyze_test(test_contents=test_html)
149
150        self.assertNotEqual(test_info, None, 'test_info is None')
151        self.assertTrue('test' in test_info.keys(), 'did not find a test file')
152        self.assertFalse('reference' in test_info.keys(), 'shold not have found a reference file')
153        self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
154        self.assertTrue('jstest' in test_info.keys(), 'test should be a jstest')
155
156    def test_analyze_pixel_test_all_true(self):
157        """ Tests analyze_test() using a test that is neither a reftest or jstest with all=False """
158
159        test_html = """<html>
160<head>
161<title>CSS Test: DESCRIPTION OF TEST</title>
162<link rel="author" title="NAME_OF_AUTHOR" />
163<style type="text/css"><![CDATA[
164CSS FOR TEST
165]]></style>
166</head>
167<body>
168CONTENT OF TEST
169</body>
170</html>
171"""
172        # Set options to 'all' so this gets found
173        options['all'] = True
174
175        test_path = '/some/madeup/path/'
176        parser = TestParser(options, test_path + 'somefile.html')
177        test_info = parser.analyze_test(test_contents=test_html)
178
179        self.assertNotEqual(test_info, None, 'test_info is None')
180        self.assertTrue('test' in test_info.keys(), 'did not find a test file')
181        self.assertFalse('reference' in test_info.keys(), 'shold not have found a reference file')
182        self.assertFalse('refsupport' in test_info.keys(), 'there should be no refsupport files for this test')
183        self.assertFalse('jstest' in test_info.keys(), 'test should not be a jstest')
184
185    def test_analyze_pixel_test_all_false(self):
186        """ Tests analyze_test() using a test that is neither a reftest or jstest, with -all=False """
187
188        test_html = """<html>
189<head>
190<title>CSS Test: DESCRIPTION OF TEST</title>
191<link rel="author" title="NAME_OF_AUTHOR" />
192<style type="text/css"><![CDATA[
193CSS FOR TEST
194]]></style>
195</head>
196<body>
197CONTENT OF TEST
198</body>
199</html>
200"""
201        # Set all to false so this gets skipped
202        options['all'] = False
203
204        test_path = '/some/madeup/path/'
205        parser = TestParser(options, test_path + 'somefile.html')
206        test_info = parser.analyze_test(test_contents=test_html)
207
208        self.assertEqual(test_info, None, 'test should have been skipped')
209
210    def test_analyze_non_html_file(self):
211        """ Tests analyze_test() with a file that has no html"""
212        # FIXME: use a mock filesystem
213        parser = TestParser(options, os.path.join(os.path.dirname(__file__), 'test_parser.py'))
214        test_info = parser.analyze_test()
215        self.assertEqual(test_info, None, 'no tests should have been found in this file')
216