WebKitTools/DumpRenderTree/chromium/TestEventPrinter.cpp
changeset 2 303757a437d3
parent 0 4f2f89ce4247
equal deleted inserted replaced
0:4f2f89ce4247 2:303757a437d3
     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 handleImage(const char* actualHash, const char* expectedHash, const unsigned char* imageData, size_t imageSize, const char* fileName) const;
       
    46     void handleImageFooter() const;
       
    47     void handleTestFooter(bool dumpedAnything) const;
       
    48 };
       
    49 
       
    50 class TestShellPrinter : public TestEventPrinter {
       
    51 public:
       
    52     TestShellPrinter() {}
       
    53     void handleTestHeader(const char* url) const;
       
    54     void handleTimedOut() const;
       
    55     void handleTextHeader() const;
       
    56     void handleTextFooter() const;
       
    57     void handleImage(const char* actualHash, const char* expectedHash, const unsigned char* imageData, size_t imageSize, const char* fileName) const;
       
    58     void handleImageFooter() const;
       
    59     void handleTestFooter(bool dumpedAnything) const;
       
    60 };
       
    61 
       
    62 TestEventPrinter* TestEventPrinter::createDRTPrinter()
       
    63 {
       
    64     return new DRTPrinter;
       
    65 }
       
    66 
       
    67 TestEventPrinter* TestEventPrinter::createTestShellPrinter()
       
    68 {
       
    69     return new TestShellPrinter;
       
    70 }
       
    71 
       
    72 // ----------------------------------------------------------------
       
    73 
       
    74 void DRTPrinter::handleTestHeader(const char*) const
       
    75 {
       
    76 }
       
    77 
       
    78 void DRTPrinter::handleTimedOut() const
       
    79 {
       
    80     fprintf(stderr, "FAIL: Timed out waiting for notifyDone to be called\n");
       
    81     fprintf(stdout, "FAIL: Timed out waiting for notifyDone to be called\n");
       
    82 }
       
    83 
       
    84 void DRTPrinter::handleTextHeader() const
       
    85 {
       
    86     printf("Content-Type: text/plain\n");
       
    87 }
       
    88 
       
    89 void DRTPrinter::handleTextFooter() const
       
    90 {
       
    91     printf("#EOF\n");
       
    92 }
       
    93 
       
    94 void DRTPrinter::handleImage(const char* actualHash, const char* expectedHash, const unsigned char* imageData, size_t imageSize, const char*) const
       
    95 {
       
    96     ASSERT(actualHash);
       
    97     printf("\nActualHash: %s\n", actualHash);
       
    98     if (expectedHash && expectedHash[0])
       
    99         printf("\nExpectedHash: %s\n", expectedHash);
       
   100     if (imageData && imageSize) {
       
   101         printf("Content-Type: image/png\n");
       
   102         printf("Content-Length: %lu\n", imageSize);
       
   103         if (fwrite(imageData, 1, imageSize, stdout) != imageSize) {
       
   104             fprintf(stderr, "Short write to stdout.\n");
       
   105             exit(1);
       
   106         }
       
   107     }
       
   108 }
       
   109 
       
   110 void DRTPrinter::handleImageFooter() const
       
   111 {
       
   112     printf("#EOF\n");
       
   113 }
       
   114 
       
   115 void DRTPrinter::handleTestFooter(bool) const
       
   116 {
       
   117 }
       
   118 
       
   119 // ----------------------------------------------------------------
       
   120 
       
   121 void TestShellPrinter::handleTestHeader(const char* url) const
       
   122 {
       
   123     printf("#URL:%s\n", url);
       
   124 }
       
   125 
       
   126 void TestShellPrinter::handleTimedOut() const
       
   127 {
       
   128     puts("#TEST_TIMED_OUT\n");
       
   129 }
       
   130 
       
   131 void TestShellPrinter::handleTextHeader() const
       
   132 {
       
   133 }
       
   134 
       
   135 void TestShellPrinter::handleTextFooter() const
       
   136 {
       
   137 }
       
   138 
       
   139 void TestShellPrinter::handleImage(const char* actualHash, const char*, const unsigned char* imageData, size_t imageSize, const char* fileName) const
       
   140 {
       
   141     ASSERT(actualHash);
       
   142     if (imageData && imageSize) {
       
   143         ASSERT(fileName);
       
   144         FILE* fp = fopen(fileName, "wb");
       
   145         if (!fp) {
       
   146             perror(fileName);
       
   147             exit(EXIT_FAILURE);
       
   148         }
       
   149         if (fwrite(imageData, 1, imageSize, fp) != imageSize) {
       
   150             perror(fileName);
       
   151             fclose(fp);
       
   152             exit(EXIT_FAILURE);
       
   153         }
       
   154         fclose(fp);
       
   155     }
       
   156     printf("#MD5:%s\n", actualHash);
       
   157 }
       
   158 
       
   159 void TestShellPrinter::handleImageFooter() const
       
   160 {
       
   161 }
       
   162 
       
   163 void TestShellPrinter::handleTestFooter(bool dumpedAnything) const
       
   164 {
       
   165     if (dumpedAnything)
       
   166         printf("#EOF\n");
       
   167 }