1#!/usr/bin/env python
2# Copyright (C) 2010 Google Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions
6# are met:
7#
8# 1.  Redistributions of source code must retain the above copyright
9#     notice, this list of conditions and the following disclaimer.
10# 2.  Redistributions in binary form must reproduce the above copyright
11#     notice, this list of conditions and the following disclaimer in the
12#     documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
15# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17# DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
18# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24
25import os
26import re
27import sys
28import testoutput
29import unittest
30
31
32class FakeFile(object):
33    def __init__(self, filename, contents="fake contents"):
34        self._filename = filename
35        self._contents = contents
36
37    def name(self):
38        return self._filename
39
40    def contents(self):
41        return self._contents
42
43
44class FakeTestOutput(testoutput.TestOutput):
45    def __init__(self, platform, output_type, contents, is_expected=False):
46        self._output_type = output_type
47        self._contents = contents
48        self._is_expected = is_expected
49        actual = 'actual'
50        if is_expected:
51            actual = 'expected'
52        test_name = 'anonymous-test-%s.txt' % actual
53        file = FakeFile(test_name, contents)
54        super(FakeTestOutput, self).__init__(platform, output_type, [file])
55
56    def contents(self):
57        return self._contents
58
59    def retarget(self, platform):
60        return FakeTestOutput(platform, self._output_type, self._contents, self._is_expected)
61
62
63class TestOutputTest(unittest.TestCase):
64    def _check_name(self, filename, expected_test_name):
65        # FIXME: should consider using MockFileSystem here so as to not
66        # have to worry about platform-specific path separators.
67        if sys.platform == 'win32':
68            filename = filename.replace('/', os.path.sep)
69            expected_test_name = expected_test_name.replace('/', os.path.sep)
70        r = testoutput.TextTestOutput(None, FakeFile(filename))
71        self.assertEquals(expected_test_name, r.name())
72
73    def _check_platform(self, filename, expected_platform):
74        # FIXME: should consider using MockFileSystem here so as to not
75        # have to worry about platform-specific path separators.
76        if sys.platform == 'win32':
77            filename = filename.replace('/', os.path.sep)
78        r = testoutput.TextTestOutput(None, FakeFile(filename))
79        self.assertEquals(expected_platform, r.platform())
80
81    def test_extracts_name_correctly(self):
82        self._check_name('LayoutTests/fast/dom/a-expected.txt', 'fast/dom/a')
83        self._check_name('LayoutTests/fast/dom/a-actual.txt', 'fast/dom/a')
84        self._check_name('LayoutTests/platform/win/fast/a-expected.txt', 'fast/a')
85        self._check_name('LayoutTests/platform/win/fast/a-expected.checksum', 'fast/a')
86        self._check_name('fast/dom/test-expected.txt', 'fast/dom/test')
87        self._check_name('layout-test-results/fast/a-actual.checksum', 'fast/a')
88
89    def test_extracts_platform_correctly(self):
90        self._check_platform('LayoutTests/platform/win/fast/a-expected.txt', 'win')
91        self._check_platform('platform/win/fast/a-expected.txt', 'win')
92        self._check_platform('platform/mac/fast/a-expected.txt', 'mac')
93        self._check_platform('fast/a-expected.txt', None)
94
95    def test_outputs_from_an_actual_file_are_marked_as_such(self):
96        r = testoutput.TextTestOutput(None, FakeFile('test-actual.txt'))
97        self.assertTrue(r.is_actual())
98
99    def test_outputs_from_an_expected_file_are_not_actual(self):
100        r = testoutput.TextTestOutput(None, FakeFile('test-expected.txt'))
101        self.assertFalse(r.is_actual())
102
103    def test_is_new_baseline_for(self):
104        expected = testoutput.TextTestOutput('mac', FakeFile('test-expected.txt'))
105        actual = testoutput.TextTestOutput('mac', FakeFile('test-actual.txt'))
106        self.assertTrue(actual.is_new_baseline_for(expected))
107        self.assertFalse(expected.is_new_baseline_for(actual))
108
109    def test__eq__(self):
110        r1 = testoutput.TextTestOutput('mac', FakeFile('test-expected.txt', 'contents'))
111        r2 = testoutput.TextTestOutput('mac', FakeFile('test-expected.txt', 'contents'))
112        r3 = testoutput.TextTestOutput('win', FakeFile('test-expected.txt', 'contents'))
113
114        self.assertEquals(r1, r2)
115        self.assertEquals(r1, r2.retarget('mac'))
116        self.assertNotEquals(r1, r2.retarget('win'))
117
118    def test__hash__(self):
119        r1 = testoutput.TextTestOutput('mac', FakeFile('test-expected.txt', 'contents'))
120        r2 = testoutput.TextTestOutput('mac', FakeFile('test-expected.txt', 'contents'))
121        r3 = testoutput.TextTestOutput(None, FakeFile('test-expected.txt', None))
122
123        x = set([r1, r2])
124        self.assertEquals(1, len(set([r1, r2])))
125        self.assertEquals(2, len(set([r1, r2, r3])))
126
127    def test_image_diff_is_invoked_for_image_outputs_without_checksum(self):
128        r1 = testoutput.ImageTestOutput('mac', FakeFile('test-expected.png', 'asdf'), FakeFile('test-expected.checksum', 'check'))
129        r2 = testoutput.ImageTestOutput('mac', FakeFile('test-expected.png', 'asdf'), None)
130
131        # Default behaviour is to just compare on image contents.
132        self.assertTrue(r1.same_content(r2))
133
134        class AllImagesAreDifferent(object):
135            def same_image(self, image1, image2):
136                return False
137
138        # But we can install other image differs.
139        testoutput.ImageTestOutput.image_differ = AllImagesAreDifferent()
140
141        self.assertFalse(r1.same_content(r2))
142
143if __name__ == "__main__":
144    unittest.main()
145