initial import
[vuplus_webkit] / Tools / DumpRenderTree / chromium / TestEventPrinter.cpp
1 /*
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 are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include "config.h"
32 #include "TestEventPrinter.h"
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <wtf/Assertions.h>
37
38 class DRTPrinter : public TestEventPrinter {
39 public:
40     DRTPrinter() { }
41     void handleTestHeader(const char* url) const;
42     void handleTimedOut() const;
43     void handleTextHeader() const;
44     void handleTextFooter() const;
45     void handleAudioHeader() const;
46     void handleImage(const char* actualHash, const char* expectedHash, const unsigned char* imageData, size_t imageSize, const char* fileName) const;
47     void handleImageFooter() const;
48     void handleTestFooter(bool dumpedAnything) const;
49 };
50
51 class TestShellPrinter : public TestEventPrinter {
52 public:
53     TestShellPrinter() { }
54     void handleTestHeader(const char* url) const;
55     void handleTimedOut() const;
56     void handleTextHeader() const;
57     void handleTextFooter() const;
58     void handleAudioHeader() const;
59     void handleImage(const char* actualHash, const char* expectedHash, const unsigned char* imageData, size_t imageSize, const char* fileName) const;
60     void handleImageFooter() const;
61     void handleTestFooter(bool dumpedAnything) const;
62 };
63
64 TestEventPrinter::~TestEventPrinter()
65 {
66 }
67
68 PassOwnPtr<TestEventPrinter> TestEventPrinter::createDRTPrinter()
69 {
70     return adoptPtr(new DRTPrinter);
71 }
72
73 PassOwnPtr<TestEventPrinter> TestEventPrinter::createTestShellPrinter()
74 {
75     return adoptPtr(new TestShellPrinter);
76 }
77
78 // ----------------------------------------------------------------
79
80 void DRTPrinter::handleTestHeader(const char*) const
81 {
82 }
83
84 void DRTPrinter::handleTimedOut() const
85 {
86     fprintf(stderr, "FAIL: Timed out waiting for notifyDone to be called\n");
87     fprintf(stdout, "FAIL: Timed out waiting for notifyDone to be called\n");
88 }
89
90 void DRTPrinter::handleTextHeader() const
91 {
92     printf("Content-Type: text/plain\n");
93 }
94
95 void DRTPrinter::handleTextFooter() const
96 {
97     printf("#EOF\n");
98 }
99
100 void DRTPrinter::handleAudioHeader() const
101 {
102     printf("Content-Type: audio/wav\n");
103 }
104
105 void DRTPrinter::handleImage(const char* actualHash, const char* expectedHash, const unsigned char* imageData, size_t imageSize, const char*) const
106 {
107     ASSERT(actualHash);
108     printf("\nActualHash: %s\n", actualHash);
109     if (expectedHash && expectedHash[0])
110         printf("\nExpectedHash: %s\n", expectedHash);
111     if (imageData && imageSize) {
112         printf("Content-Type: image/png\n");
113         // Printf formatting for size_t on 32-bit, 64-bit, and on Windows is hard so just cast to an int.
114         printf("Content-Length: %d\n", static_cast<int>(imageSize));
115         if (fwrite(imageData, 1, imageSize, stdout) != imageSize) {
116             fprintf(stderr, "Short write to stdout.\n");
117             exit(1);
118         }
119     }
120 }
121
122 void DRTPrinter::handleImageFooter() const
123 {
124     printf("#EOF\n");
125 }
126
127 void DRTPrinter::handleTestFooter(bool) const
128 {
129 }
130
131 // ----------------------------------------------------------------
132
133 void TestShellPrinter::handleTestHeader(const char* url) const
134 {
135     printf("#URL:%s\n", url);
136 }
137
138 void TestShellPrinter::handleTimedOut() const
139 {
140     puts("#TEST_TIMED_OUT\n");
141 }
142
143 void TestShellPrinter::handleTextHeader() const
144 {
145 }
146
147 void TestShellPrinter::handleTextFooter() const
148 {
149 }
150
151 void TestShellPrinter::handleAudioHeader() const
152 {
153     printf("Content-Type: audio/wav\n");
154 }
155
156 void TestShellPrinter::handleImage(const char* actualHash, const char*, const unsigned char* imageData, size_t imageSize, const char* fileName) const
157 {
158     ASSERT(actualHash);
159     if (imageData && imageSize) {
160         ASSERT(fileName);
161         FILE* fp = fopen(fileName, "wb");
162         if (!fp) {
163             perror(fileName);
164             exit(EXIT_FAILURE);
165         }
166         if (fwrite(imageData, 1, imageSize, fp) != imageSize) {
167             perror(fileName);
168             fclose(fp);
169             exit(EXIT_FAILURE);
170         }
171         fclose(fp);
172     }
173     printf("#MD5:%s\n", actualHash);
174 }
175
176 void TestShellPrinter::handleImageFooter() const
177 {
178 }
179
180 void TestShellPrinter::handleTestFooter(bool dumpedAnything) const
181 {
182     if (dumpedAnything)
183         printf("#EOF\n");
184 }