| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the QtTest module of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #include "qtestbasicstreamer.h" | 
|---|
| 43 | #include "qtestlogger_p.h" | 
|---|
| 44 | #include "qtestelement.h" | 
|---|
| 45 | #include "qtestelementattribute.h" | 
|---|
| 46 | #include "QtTest/private/qtestlog_p.h" | 
|---|
| 47 | #include "qtestassert.h" | 
|---|
| 48 |  | 
|---|
| 49 | #include <stdio.h> | 
|---|
| 50 | #include <stdlib.h> | 
|---|
| 51 |  | 
|---|
| 52 | #ifndef Q_OS_WIN | 
|---|
| 53 | #include <unistd.h> | 
|---|
| 54 | #endif | 
|---|
| 55 |  | 
|---|
| 56 | QT_BEGIN_NAMESPACE | 
|---|
| 57 |  | 
|---|
| 58 | namespace QTest | 
|---|
| 59 | { | 
|---|
| 60 | static FILE *stream = 0; | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | QTestBasicStreamer::QTestBasicStreamer() | 
|---|
| 64 | :testLogger(0) | 
|---|
| 65 | { | 
|---|
| 66 | } | 
|---|
| 67 |  | 
|---|
| 68 | QTestBasicStreamer::~QTestBasicStreamer() | 
|---|
| 69 | {} | 
|---|
| 70 |  | 
|---|
| 71 | void QTestBasicStreamer::formatStart(const QTestElement *element, QTestCharBuffer *formatted) const | 
|---|
| 72 | { | 
|---|
| 73 | if(!element || !formatted ) | 
|---|
| 74 | return; | 
|---|
| 75 | formatted->data()[0] = '\0'; | 
|---|
| 76 | } | 
|---|
| 77 |  | 
|---|
| 78 | void QTestBasicStreamer::formatEnd(const QTestElement *element, QTestCharBuffer *formatted) const | 
|---|
| 79 | { | 
|---|
| 80 | if(!element || !formatted ) | 
|---|
| 81 | return; | 
|---|
| 82 | formatted->data()[0] = '\0'; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | void QTestBasicStreamer::formatBeforeAttributes(const QTestElement *element, QTestCharBuffer *formatted) const | 
|---|
| 86 | { | 
|---|
| 87 | if(!element || !formatted ) | 
|---|
| 88 | return; | 
|---|
| 89 | formatted->data()[0] = '\0'; | 
|---|
| 90 | } | 
|---|
| 91 |  | 
|---|
| 92 | void QTestBasicStreamer::formatAfterAttributes(const QTestElement *element, QTestCharBuffer *formatted) const | 
|---|
| 93 | { | 
|---|
| 94 | if(!element || !formatted ) | 
|---|
| 95 | return; | 
|---|
| 96 | formatted->data()[0] = '\0'; | 
|---|
| 97 | } | 
|---|
| 98 |  | 
|---|
| 99 | void QTestBasicStreamer::formatAttributes(const QTestElement *, const QTestElementAttribute *attribute, QTestCharBuffer *formatted) const | 
|---|
| 100 | { | 
|---|
| 101 | if(!attribute || !formatted ) | 
|---|
| 102 | return; | 
|---|
| 103 | formatted->data()[0] = '\0'; | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | void QTestBasicStreamer::output(QTestElement *element) const | 
|---|
| 107 | { | 
|---|
| 108 | if(!element) | 
|---|
| 109 | return; | 
|---|
| 110 |  | 
|---|
| 111 | outputElements(element); | 
|---|
| 112 | } | 
|---|
| 113 |  | 
|---|
| 114 | void QTestBasicStreamer::outputElements(QTestElement *element, bool) const | 
|---|
| 115 | { | 
|---|
| 116 | QTestCharBuffer buf; | 
|---|
| 117 | bool hasChildren; | 
|---|
| 118 | /* | 
|---|
| 119 | Elements are in reverse order of occurrence, so start from the end and work | 
|---|
| 120 | our way backwards. | 
|---|
| 121 | */ | 
|---|
| 122 | while (element && element->nextElement()) { | 
|---|
| 123 | element = element->nextElement(); | 
|---|
| 124 | } | 
|---|
| 125 | while (element) { | 
|---|
| 126 | hasChildren = element->childElements(); | 
|---|
| 127 |  | 
|---|
| 128 | formatStart(element, &buf); | 
|---|
| 129 | outputString(buf.data()); | 
|---|
| 130 |  | 
|---|
| 131 | formatBeforeAttributes(element, &buf); | 
|---|
| 132 | outputString(buf.data()); | 
|---|
| 133 |  | 
|---|
| 134 | outputElementAttributes(element, element->attributes()); | 
|---|
| 135 |  | 
|---|
| 136 | formatAfterAttributes(element, &buf); | 
|---|
| 137 | outputString(buf.data()); | 
|---|
| 138 |  | 
|---|
| 139 | if(hasChildren) | 
|---|
| 140 | outputElements(element->childElements(), true); | 
|---|
| 141 |  | 
|---|
| 142 | formatEnd(element, &buf); | 
|---|
| 143 | outputString(buf.data()); | 
|---|
| 144 |  | 
|---|
| 145 | element = element->previousElement(); | 
|---|
| 146 | } | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | void QTestBasicStreamer::outputElementAttributes(const QTestElement* element, QTestElementAttribute *attribute) const | 
|---|
| 150 | { | 
|---|
| 151 | QTestCharBuffer buf; | 
|---|
| 152 | while(attribute){ | 
|---|
| 153 | formatAttributes(element, attribute, &buf); | 
|---|
| 154 | outputString(buf.data()); | 
|---|
| 155 | attribute = attribute->nextElement(); | 
|---|
| 156 | } | 
|---|
| 157 | } | 
|---|
| 158 |  | 
|---|
| 159 | void QTestBasicStreamer::outputString(const char *msg) const | 
|---|
| 160 | { | 
|---|
| 161 | QTEST_ASSERT(QTest::stream); | 
|---|
| 162 |  | 
|---|
| 163 | ::fputs(msg, QTest::stream); | 
|---|
| 164 | ::fflush(QTest::stream); | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | void QTestBasicStreamer::startStreaming() | 
|---|
| 168 | { | 
|---|
| 169 | QTEST_ASSERT(!QTest::stream); | 
|---|
| 170 |  | 
|---|
| 171 | const char *out = QTestLog::outputFileName(); | 
|---|
| 172 | if (!out) { | 
|---|
| 173 | QTest::stream = stdout; | 
|---|
| 174 | return; | 
|---|
| 175 | } | 
|---|
| 176 | #if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(Q_OS_WINCE) | 
|---|
| 177 | if (::fopen_s(&QTest::stream, out, "wt")) { | 
|---|
| 178 | #else | 
|---|
| 179 | QTest::stream = ::fopen(out, "wt"); | 
|---|
| 180 | if (!QTest::stream) { | 
|---|
| 181 | #endif | 
|---|
| 182 | printf("Unable to open file for logging: %s", out); | 
|---|
| 183 | ::exit(1); | 
|---|
| 184 | } | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | bool QTestBasicStreamer::isTtyOutput() | 
|---|
| 188 | { | 
|---|
| 189 | QTEST_ASSERT(QTest::stream); | 
|---|
| 190 |  | 
|---|
| 191 | #if defined(Q_OS_WIN) || defined(Q_OS_INTEGRITY) | 
|---|
| 192 | return true; | 
|---|
| 193 | #else | 
|---|
| 194 | static bool ttyoutput = isatty(fileno(QTest::stream)); | 
|---|
| 195 | return ttyoutput; | 
|---|
| 196 | #endif | 
|---|
| 197 | } | 
|---|
| 198 |  | 
|---|
| 199 | void QTestBasicStreamer::stopStreaming() | 
|---|
| 200 | { | 
|---|
| 201 | QTEST_ASSERT(QTest::stream); | 
|---|
| 202 | if (QTest::stream != stdout) | 
|---|
| 203 | fclose(QTest::stream); | 
|---|
| 204 |  | 
|---|
| 205 | QTest::stream = 0; | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | void QTestBasicStreamer::setLogger(const QTestLogger *tstLogger) | 
|---|
| 209 | { | 
|---|
| 210 | testLogger = tstLogger; | 
|---|
| 211 | } | 
|---|
| 212 |  | 
|---|
| 213 | const QTestLogger *QTestBasicStreamer::logger() const | 
|---|
| 214 | { | 
|---|
| 215 | return testLogger; | 
|---|
| 216 | } | 
|---|
| 217 |  | 
|---|
| 218 | QT_END_NAMESPACE | 
|---|
| 219 |  | 
|---|