tests/auto/qcssparser/tst_qcssparser.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    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 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #include <QtTest/QtTest>
       
    42 #include <QtXml/QtXml>
       
    43 #if defined(Q_OS_WINCE)
       
    44 #include <QtGui/QFontDatabase>
       
    45 #endif
       
    46 
       
    47 //TESTED_CLASS=QCss
       
    48 //TESTED_FILES=gui/text/qcssparser.cpp gui/text/qcssparser_p.h
       
    49 
       
    50 #include "private/qcssparser_p.h"
       
    51 
       
    52 class tst_QCssParser : public QObject
       
    53 {
       
    54     Q_OBJECT
       
    55 
       
    56 public slots:
       
    57     void initTestCase();
       
    58     void cleanupTestCase();
       
    59 
       
    60 private slots:
       
    61     void scanner_data();
       
    62     void scanner();
       
    63     void term_data();
       
    64     void term();
       
    65     void expr_data();
       
    66     void expr();
       
    67     void import();
       
    68     void media();
       
    69     void page();
       
    70     void ruleset();
       
    71     void selector_data();
       
    72     void selector();
       
    73     void prio();
       
    74     void escapes();
       
    75     void malformedDeclarations_data();
       
    76     void malformedDeclarations();
       
    77     void invalidAtKeywords();
       
    78     void marginValue();
       
    79     void marginValue_data();
       
    80     void colorValue_data();
       
    81     void colorValue();
       
    82     void styleSelector_data();
       
    83     void styleSelector();
       
    84     void specificity_data();
       
    85     void specificity();
       
    86     void specificitySort_data();
       
    87     void specificitySort();
       
    88     void rulesForNode_data();
       
    89     void rulesForNode();
       
    90     void shorthandBackgroundProperty_data();
       
    91     void shorthandBackgroundProperty();
       
    92     void pseudoElement_data();
       
    93     void pseudoElement();
       
    94     void gradient_data();
       
    95     void gradient();
       
    96     void extractFontFamily_data();
       
    97     void extractFontFamily();
       
    98     void extractBorder_data();
       
    99     void extractBorder();
       
   100     void noTextDecoration();
       
   101     void quotedAndUnquotedIdentifiers();
       
   102 
       
   103 private:
       
   104 #if defined(Q_OS_WINCE)
       
   105     int m_timesFontId;
       
   106 #endif
       
   107 };
       
   108 
       
   109 void tst_QCssParser::initTestCase()
       
   110 {
       
   111 #if defined(Q_OS_WINCE)
       
   112     QFontDatabase fontDB;
       
   113     m_timesFontId = -1;
       
   114     if (!fontDB.families().contains("Times New Roman")) {
       
   115         m_timesFontId = QFontDatabase::addApplicationFont("times.ttf");
       
   116         QVERIFY(m_timesFontId != -1);
       
   117     }
       
   118 #endif
       
   119 }
       
   120 
       
   121 void tst_QCssParser::cleanupTestCase()
       
   122 {
       
   123 #if defined(Q_OS_WINCE)
       
   124     if (m_timesFontId != -1)
       
   125         QFontDatabase::removeApplicationFont(m_timesFontId);
       
   126 #endif
       
   127 }
       
   128 
       
   129 void tst_QCssParser::scanner_data()
       
   130 {
       
   131     QTest::addColumn<QString>("input");
       
   132     QTest::addColumn<QString>("output");
       
   133 
       
   134 #if !defined(Q_OS_IRIX) && !defined(Q_OS_WINCE) && !defined(Q_OS_SYMBIAN)
       
   135     QDir d(SRCDIR);
       
   136 #else
       
   137     QDir d(QDir::current());
       
   138 #endif
       
   139     d.cd("testdata");
       
   140     d.cd("scanner");
       
   141     foreach (QFileInfo test, d.entryInfoList(QDir::Dirs | QDir::NoDotAndDotDot)) {
       
   142         QString dir = test.absoluteFilePath() + QDir::separator();
       
   143         QTest::newRow(qPrintable(test.baseName()))
       
   144             << dir + "input"
       
   145             << dir + "output"
       
   146             ;
       
   147     }
       
   148 }
       
   149 
       
   150 
       
   151 static char *tokenName(QCss::TokenType t)
       
   152 {
       
   153     switch (t) {
       
   154         case QCss::NONE: return "NONE";
       
   155         case QCss::S: return "S";
       
   156         case QCss::CDO: return "CDO";
       
   157         case QCss::CDC: return "CDC";
       
   158         case QCss::INCLUDES: return "INCLUDES";
       
   159         case QCss::DASHMATCH: return "DASHMATCH";
       
   160         case QCss::LBRACE: return "LBRACE";
       
   161         case QCss::PLUS: return "PLUS";
       
   162         case QCss::GREATER: return "GREATER";
       
   163         case QCss::COMMA: return "COMMA";
       
   164         case QCss::STRING: return "STRING";
       
   165         case QCss::INVALID: return "INVALID";
       
   166         case QCss::IDENT: return "IDENT";
       
   167         case QCss::HASH: return "HASH";
       
   168         case QCss::ATKEYWORD_SYM: return "ATKEYWORD_SYM";
       
   169         case QCss::EXCLAMATION_SYM: return "EXCLAMATION_SYM";
       
   170         case QCss::LENGTH: return "LENGTH";
       
   171         case QCss::PERCENTAGE: return "PERCENTAGE";
       
   172         case QCss::NUMBER: return "NUMBER";
       
   173         case QCss::FUNCTION: return "FUNCTION";
       
   174         case QCss::COLON: return "COLON";
       
   175         case QCss::SEMICOLON: return "SEMICOLON";
       
   176         case QCss::RBRACE: return "RBRACE";
       
   177         case QCss::SLASH: return "SLASH";
       
   178         case QCss::MINUS: return "MINUS";
       
   179         case QCss::DOT: return "DOT";
       
   180         case QCss::STAR: return "STAR";
       
   181         case QCss::LBRACKET: return "LBRACKET";
       
   182         case QCss::RBRACKET: return "RBRACKET";
       
   183         case QCss::EQUAL: return "EQUAL";
       
   184         case QCss::LPAREN: return "LPAREN";
       
   185         case QCss::RPAREN: return "RPAREN";
       
   186         case QCss::OR: return "OR";
       
   187     }
       
   188     return "";
       
   189 }
       
   190 
       
   191 static void debug(const QVector<QCss::Symbol> &symbols, int index = -1)
       
   192 {
       
   193     qDebug() << "all symbols:";
       
   194     for (int i = 0; i < symbols.count(); ++i)
       
   195         qDebug() << "(" << i << "); Token:" << tokenName(symbols.at(i).token) << "; Lexem:" << symbols.at(i).lexem();
       
   196     if (index != -1)
       
   197         qDebug() << "failure at index" << index;
       
   198 }
       
   199 
       
   200 //static void debug(const QCss::Parser &p) { debug(p.symbols); }
       
   201 
       
   202 void tst_QCssParser::scanner()
       
   203 {
       
   204     QFETCH(QString, input);
       
   205     QFETCH(QString, output);
       
   206 
       
   207     QFile inputFile(input);
       
   208     QVERIFY(inputFile.open(QIODevice::ReadOnly|QIODevice::Text));
       
   209     QVector<QCss::Symbol> symbols;
       
   210     QCss::Scanner::scan(QCss::Scanner::preprocess(QString::fromUtf8(inputFile.readAll())), &symbols);
       
   211 
       
   212     QVERIFY(symbols.count() > 1);
       
   213     QVERIFY(symbols.last().token == QCss::S);
       
   214     QVERIFY(symbols.last().lexem() == QLatin1String("\n"));
       
   215     symbols.remove(symbols.count() - 1, 1);
       
   216 
       
   217     QFile outputFile(output);
       
   218     QVERIFY(outputFile.open(QIODevice::ReadOnly|QIODevice::Text));
       
   219     QStringList lines;
       
   220     while (!outputFile.atEnd()) {
       
   221         QString line = QString::fromUtf8(outputFile.readLine());
       
   222         if (line.endsWith(QLatin1Char('\n')))
       
   223             line.chop(1);
       
   224         lines.append(line);
       
   225     }
       
   226 
       
   227     if (lines.count() != symbols.count()) {
       
   228         debug(symbols);
       
   229         QCOMPARE(lines.count(), symbols.count());
       
   230     }
       
   231 
       
   232     for (int i = 0; i < lines.count(); ++i) {
       
   233         QStringList l = lines.at(i).split(QChar::fromLatin1('|'));
       
   234         QCOMPARE(l.count(), 2);
       
   235         const QString expectedToken = l.at(0);
       
   236         const QString expectedLexem = l.at(1);
       
   237         QString actualToken = QString::fromLatin1(tokenName(symbols.at(i).token));
       
   238         if (actualToken != expectedToken) {
       
   239             debug(symbols, i);
       
   240             QCOMPARE(actualToken, expectedToken);
       
   241         }
       
   242         if (symbols.at(i).lexem() != expectedLexem) {
       
   243             debug(symbols, i);
       
   244             QCOMPARE(symbols.at(i).lexem(), expectedLexem);
       
   245         }
       
   246     }
       
   247 }
       
   248 
       
   249 Q_DECLARE_METATYPE(QCss::Value)
       
   250 
       
   251 void tst_QCssParser::term_data()
       
   252 {
       
   253     QTest::addColumn<bool>("parseSuccess");
       
   254     QTest::addColumn<QString>("css");
       
   255     QTest::addColumn<QCss::Value>("expectedValue");
       
   256 
       
   257     QCss::Value val;
       
   258 
       
   259     val.type = QCss::Value::Percentage;
       
   260     val.variant = QVariant(double(200));
       
   261     QTest::newRow("percentage") << true << "200%" << val;
       
   262 
       
   263     val.type = QCss::Value::Length;
       
   264     val.variant = QString("10px");
       
   265     QTest::newRow("px") << true << "10px" << val;
       
   266 
       
   267     val.type = QCss::Value::Length;
       
   268     val.variant = QString("10cm");
       
   269     QTest::newRow("cm") << true << "10cm" << val;
       
   270 
       
   271     val.type = QCss::Value::Length;
       
   272     val.variant = QString("10mm");
       
   273     QTest::newRow("mm") << true << "10mm" << val;
       
   274 
       
   275     val.type = QCss::Value::Length;
       
   276     val.variant = QString("10pt");
       
   277     QTest::newRow("pt") << true << "10pt" << val;
       
   278 
       
   279     val.type = QCss::Value::Length;
       
   280     val.variant = QString("10pc");
       
   281     QTest::newRow("pc") << true << "10pc" << val;
       
   282 
       
   283     val.type = QCss::Value::Length;
       
   284     val.variant = QString("42in");
       
   285     QTest::newRow("inch") << true << "42in" << val;
       
   286 
       
   287     val.type = QCss::Value::Length;
       
   288     val.variant = QString("10deg");
       
   289     QTest::newRow("deg") << true << "10deg" << val;
       
   290 
       
   291     val.type = QCss::Value::Length;
       
   292     val.variant = QString("10rad");
       
   293     QTest::newRow("rad") << true << "10rad" << val;
       
   294 
       
   295     val.type = QCss::Value::Length;
       
   296     val.variant = QString("10grad");
       
   297     QTest::newRow("grad") << true << "10grad" << val;
       
   298 
       
   299     val.type = QCss::Value::Length;
       
   300     val.variant = QString("10ms");
       
   301     QTest::newRow("time") << true << "10ms" << val;
       
   302 
       
   303     val.type = QCss::Value::Length;
       
   304     val.variant = QString("10s");
       
   305     QTest::newRow("times") << true << "10s" << val;
       
   306 
       
   307     val.type = QCss::Value::Length;
       
   308     val.variant = QString("10hz");
       
   309     QTest::newRow("hz") << true << "10hz" << val;
       
   310 
       
   311     val.type = QCss::Value::Length;
       
   312     val.variant = QString("10khz");
       
   313     QTest::newRow("khz") << true << "10khz" << val;
       
   314 
       
   315     val.type = QCss::Value::Length;
       
   316     val.variant = QString("10myunit");
       
   317     QTest::newRow("dimension") << true << "10myunit" << val;
       
   318 
       
   319     val.type = QCss::Value::Percentage;
       
   320 
       
   321     val.type = QCss::Value::Percentage;
       
   322     val.variant = QVariant(double(-200));
       
   323     QTest::newRow("minuspercentage") << true << "-200%" << val;
       
   324 
       
   325     val.type = QCss::Value::Length;
       
   326     val.variant = QString("10em");
       
   327     QTest::newRow("ems") << true << "10em" << val;
       
   328 
       
   329     val.type = QCss::Value::String;
       
   330     val.variant = QVariant(QString("foo"));
       
   331     QTest::newRow("string") << true << "\"foo\"" << val;
       
   332 
       
   333     val.type = QCss::Value::Function;
       
   334     val.variant = QVariant(QStringList() << "myFunc" << "23, (nested text)");
       
   335     QTest::newRow("function") << true << "myFunc(23, (nested text))" << val;
       
   336 
       
   337     QTest::newRow("function_failure") << false << "myFunction((blah)" << val;
       
   338     QTest::newRow("function_failure2") << false << "+myFunc(23, (nested text))" << val;
       
   339 
       
   340     val.type = QCss::Value::Color;
       
   341     val.variant = QVariant(QColor("#12ff34"));
       
   342     QTest::newRow("hexcolor") << true << "#12ff34" << val;
       
   343 
       
   344     val.type = QCss::Value::Color;
       
   345     val.variant = QVariant(QColor("#ffbb00"));
       
   346     QTest::newRow("hexcolor2") << true << "#fb0" << val;
       
   347 
       
   348     QTest::ignoreMessage(QtWarningMsg, "QCssParser::parseHexColor: Unknown color name '#cafebabe'");
       
   349     QTest::newRow("hexcolor_failure") << false << "#cafebabe" << val;
       
   350 
       
   351     val.type = QCss::Value::Uri;
       
   352     val.variant = QString("www.kde.org");
       
   353     QTest::newRow("uri1") << true << "url(\"www.kde.org\")" << val;
       
   354 
       
   355     QTest::newRow("uri2") << true << "url(www.kde.org)" << val;
       
   356 
       
   357     val.type = QCss::Value::KnownIdentifier;
       
   358     val.variant = int(QCss::Value_Italic);
       
   359     QTest::newRow("italic") << true << "italic" << val;
       
   360 
       
   361     val.type = QCss::Value::KnownIdentifier;
       
   362     val.variant = int(QCss::Value_Italic);
       
   363     QTest::newRow("ItaLIc") << true << "ItaLIc" << val;
       
   364 }
       
   365 
       
   366 void tst_QCssParser::term()
       
   367 {
       
   368     QFETCH(bool, parseSuccess);
       
   369     QFETCH(QString, css);
       
   370     QFETCH(QCss::Value, expectedValue);
       
   371 
       
   372     QCss::Parser parser(css);
       
   373     QCss::Value val;
       
   374     QVERIFY(parser.testTerm());
       
   375     QCOMPARE(parser.parseTerm(&val), parseSuccess);
       
   376     if (parseSuccess) {
       
   377         QCOMPARE(int(val.type), int(expectedValue.type));
       
   378         if (val.variant != expectedValue.variant) {
       
   379             qDebug() << "val.variant:" << val.variant << "expectedValue.variant:" << expectedValue.variant;
       
   380             QCOMPARE(val.variant, expectedValue.variant);
       
   381         }
       
   382     }
       
   383 }
       
   384 
       
   385 Q_DECLARE_METATYPE(QVector<QCss::Value>)
       
   386 
       
   387 void tst_QCssParser::expr_data()
       
   388 {
       
   389     QTest::addColumn<bool>("parseSuccess");
       
   390     QTest::addColumn<QString>("css");
       
   391     QTest::addColumn<QVector<QCss::Value> >("expectedValues");
       
   392 
       
   393     QVector<QCss::Value> values;
       
   394     QCss::Value val;
       
   395 
       
   396     QCss::Value comma;
       
   397     comma.type = QCss::Value::TermOperatorComma;
       
   398 
       
   399     val.type = QCss::Value::Identifier;
       
   400     val.variant = QLatin1String("foo");
       
   401     values << val;
       
   402     values << comma;
       
   403     val.variant = QLatin1String("bar");
       
   404     values << val;
       
   405     values << comma;
       
   406     val.variant = QLatin1String("baz");
       
   407     values << val;
       
   408     QTest::newRow("list") << true << "foo, bar, baz" << values;
       
   409     values.clear();
       
   410 }
       
   411 
       
   412 void tst_QCssParser::expr()
       
   413 {
       
   414     QFETCH(bool, parseSuccess);
       
   415     QFETCH(QString, css);
       
   416     QFETCH(QVector<QCss::Value>, expectedValues);
       
   417 
       
   418     QCss::Parser parser(css);
       
   419     QVector<QCss::Value> values;
       
   420     QVERIFY(parser.testExpr());
       
   421     QCOMPARE(parser.parseExpr(&values), parseSuccess);
       
   422     if (parseSuccess) {
       
   423         QCOMPARE(values.count(), expectedValues.count());
       
   424 
       
   425         for (int i = 0; i < values.count(); ++i) {
       
   426             QCOMPARE(int(values.at(i).type), int(expectedValues.at(i).type));
       
   427             QCOMPARE(values.at(i).variant, expectedValues.at(i).variant);
       
   428         }
       
   429     }
       
   430 }
       
   431 
       
   432 void tst_QCssParser::import()
       
   433 {
       
   434     QCss::Parser parser("@import \"plainstring\";");
       
   435     QVERIFY(parser.testImport());
       
   436     QCss::ImportRule rule;
       
   437     QVERIFY(parser.parseImport(&rule));
       
   438     QCOMPARE(rule.href, QString("plainstring"));
       
   439 
       
   440     parser = QCss::Parser("@import url(\"www.kde.org\") print/*comment*/,screen;");
       
   441     QVERIFY(parser.testImport());
       
   442     QVERIFY(parser.parseImport(&rule));
       
   443     QCOMPARE(rule.href, QString("www.kde.org"));
       
   444     QCOMPARE(rule.media.count(), 2);
       
   445     QCOMPARE(rule.media.at(0), QString("print"));
       
   446     QCOMPARE(rule.media.at(1), QString("screen"));
       
   447 }
       
   448 
       
   449 void tst_QCssParser::media()
       
   450 {
       
   451     QCss::Parser parser("@media print/*comment*/,screen /*comment to ignore*/{ }");
       
   452     QVERIFY(parser.testMedia());
       
   453     QCss::MediaRule rule;
       
   454     QVERIFY(parser.parseMedia(&rule));
       
   455     QCOMPARE(rule.media.count(), 2);
       
   456     QCOMPARE(rule.media.at(0), QString("print"));
       
   457     QCOMPARE(rule.media.at(1), QString("screen"));
       
   458     QVERIFY(rule.styleRules.isEmpty());
       
   459 }
       
   460 
       
   461 void tst_QCssParser::page()
       
   462 {
       
   463     QCss::Parser parser("@page :first/*comment to ignore*/{ }");
       
   464     QVERIFY(parser.testPage());
       
   465     QCss::PageRule rule;
       
   466     QVERIFY(parser.parsePage(&rule));
       
   467     QCOMPARE(rule.selector, QString("first"));
       
   468     QVERIFY(rule.declarations.isEmpty());
       
   469 }
       
   470 
       
   471 void tst_QCssParser::ruleset()
       
   472 {
       
   473     {
       
   474         QCss::Parser parser("p/*foo*/{ }");
       
   475         QVERIFY(parser.testRuleset());
       
   476         QCss::StyleRule rule;
       
   477         QVERIFY(parser.parseRuleset(&rule));
       
   478         QCOMPARE(rule.selectors.count(), 1);
       
   479         QCOMPARE(rule.selectors.at(0).basicSelectors.count(), 1);
       
   480         QCOMPARE(rule.selectors.at(0).basicSelectors.at(0).elementName, QString("p"));
       
   481         QVERIFY(rule.declarations.isEmpty());
       
   482     }
       
   483 
       
   484     {
       
   485         QCss::Parser parser("p/*comment*/,div{ }");
       
   486         QVERIFY(parser.testRuleset());
       
   487         QCss::StyleRule rule;
       
   488         QVERIFY(parser.parseRuleset(&rule));
       
   489         QCOMPARE(rule.selectors.count(), 2);
       
   490         QCOMPARE(rule.selectors.at(0).basicSelectors.count(), 1);
       
   491         QCOMPARE(rule.selectors.at(0).basicSelectors.at(0).elementName, QString("p"));
       
   492         QCOMPARE(rule.selectors.at(1).basicSelectors.count(), 1);
       
   493         QCOMPARE(rule.selectors.at(1).basicSelectors.at(0).elementName, QString("div"));
       
   494         QVERIFY(rule.declarations.isEmpty());
       
   495     }
       
   496 
       
   497     {
       
   498         QCss::Parser parser(":before, :after { }");
       
   499         QVERIFY(parser.testRuleset());
       
   500         QCss::StyleRule rule;
       
   501         QVERIFY(parser.parseRuleset(&rule));
       
   502         QCOMPARE(rule.selectors.count(), 2);
       
   503 
       
   504         QCOMPARE(rule.selectors.at(0).basicSelectors.count(), 1);
       
   505         QCOMPARE(rule.selectors.at(0).basicSelectors.at(0).pseudos.count(), 1);
       
   506         QCOMPARE(rule.selectors.at(0).basicSelectors.at(0).pseudos.at(0).name, QString("before"));
       
   507 
       
   508         QCOMPARE(rule.selectors.at(1).basicSelectors.count(), 1);
       
   509         QCOMPARE(rule.selectors.at(1).basicSelectors.at(0).pseudos.count(), 1);
       
   510         QCOMPARE(rule.selectors.at(1).basicSelectors.at(0).pseudos.at(0).name, QString("after"));
       
   511 
       
   512         QVERIFY(rule.declarations.isEmpty());
       
   513     }
       
   514 
       
   515 }
       
   516 
       
   517 Q_DECLARE_METATYPE(QCss::Selector)
       
   518 
       
   519 void tst_QCssParser::selector_data()
       
   520 {
       
   521     QTest::addColumn<QString>("css");
       
   522     QTest::addColumn<QCss::Selector>("expectedSelector");
       
   523 
       
   524     {
       
   525         QCss::Selector sel;
       
   526         QCss::BasicSelector basic;
       
   527 
       
   528         basic.elementName = "p";
       
   529         basic.relationToNext = QCss::BasicSelector::MatchNextSelectorIfPreceeds;
       
   530         sel.basicSelectors << basic;
       
   531 
       
   532         basic = QCss::BasicSelector();
       
   533         basic.elementName = "div";
       
   534         sel.basicSelectors << basic;
       
   535 
       
   536         QTest::newRow("comment") << QString("p/* */+ div") << sel;
       
   537     }
       
   538 
       
   539     {
       
   540         QCss::Selector sel;
       
   541         QCss::BasicSelector basic;
       
   542 
       
   543         basic.elementName = QString();
       
   544         sel.basicSelectors << basic;
       
   545 
       
   546         QTest::newRow("any") << QString("*") << sel;
       
   547     }
       
   548 
       
   549     {
       
   550         QCss::Selector sel;
       
   551         QCss::BasicSelector basic;
       
   552 
       
   553         basic.elementName = "e";
       
   554         sel.basicSelectors << basic;
       
   555 
       
   556         QTest::newRow("element") << QString("e") << sel;
       
   557     }
       
   558 
       
   559     {
       
   560         QCss::Selector sel;
       
   561         QCss::BasicSelector basic;
       
   562 
       
   563         basic.elementName = "e";
       
   564         basic.relationToNext = QCss::BasicSelector::MatchNextSelectorIfAncestor;
       
   565         sel.basicSelectors << basic;
       
   566 
       
   567         basic.elementName = "f";
       
   568         basic.relationToNext = QCss::BasicSelector::NoRelation;
       
   569         sel.basicSelectors << basic;
       
   570 
       
   571         QTest::newRow("descendant") << QString("e f") << sel;
       
   572     }
       
   573 
       
   574     {
       
   575         QCss::Selector sel;
       
   576         QCss::BasicSelector basic;
       
   577 
       
   578         basic.elementName = "e";
       
   579         basic.relationToNext = QCss::BasicSelector::MatchNextSelectorIfParent;
       
   580         sel.basicSelectors << basic;
       
   581 
       
   582         basic.elementName = "f";
       
   583         basic.relationToNext = QCss::BasicSelector::NoRelation;
       
   584         sel.basicSelectors << basic;
       
   585 
       
   586         QTest::newRow("parent") << QString("e > f") << sel;
       
   587     }
       
   588 
       
   589     {
       
   590         QCss::Selector sel;
       
   591         QCss::BasicSelector basic;
       
   592 
       
   593         basic.elementName = "e";
       
   594         QCss::Pseudo pseudo;
       
   595         pseudo.name = "first-child";
       
   596         basic.pseudos.append(pseudo);
       
   597         sel.basicSelectors << basic;
       
   598 
       
   599         QTest::newRow("first-child") << QString("e:first-child") << sel;
       
   600     }
       
   601 
       
   602     {
       
   603         QCss::Selector sel;
       
   604         QCss::BasicSelector basic;
       
   605 
       
   606         basic.elementName = "e";
       
   607         QCss::Pseudo pseudo;
       
   608         pseudo.name = "c";
       
   609         pseudo.function = "lang";
       
   610         basic.pseudos.append(pseudo);
       
   611         sel.basicSelectors << basic;
       
   612 
       
   613         QTest::newRow("lang") << QString("e:lang(c)") << sel;
       
   614     }
       
   615 
       
   616     {
       
   617         QCss::Selector sel;
       
   618         QCss::BasicSelector basic;
       
   619 
       
   620         basic.elementName = "e";
       
   621         basic.relationToNext = QCss::BasicSelector::MatchNextSelectorIfPreceeds;
       
   622         sel.basicSelectors << basic;
       
   623 
       
   624         basic.elementName = "f";
       
   625         basic.relationToNext = QCss::BasicSelector::NoRelation;
       
   626         sel.basicSelectors << basic;
       
   627 
       
   628         QTest::newRow("precede") << QString("e + f") << sel;
       
   629     }
       
   630 
       
   631     {
       
   632         QCss::Selector sel;
       
   633         QCss::BasicSelector basic;
       
   634 
       
   635         basic.elementName = "e";
       
   636         QCss::AttributeSelector attrSel;
       
   637         attrSel.name = "foo";
       
   638         basic.attributeSelectors << attrSel;
       
   639         sel.basicSelectors << basic;
       
   640 
       
   641         QTest::newRow("attr") << QString("e[foo]") << sel;
       
   642     }
       
   643 
       
   644     {
       
   645         QCss::Selector sel;
       
   646         QCss::BasicSelector basic;
       
   647 
       
   648         basic.elementName = "e";
       
   649         QCss::AttributeSelector attrSel;
       
   650         attrSel.name = "foo";
       
   651         attrSel.value = "warning";
       
   652         attrSel.valueMatchCriterium = QCss::AttributeSelector::MatchEqual;
       
   653         basic.attributeSelectors << attrSel;
       
   654         sel.basicSelectors << basic;
       
   655 
       
   656         QTest::newRow("attr-equal") << QString("e[foo=\"warning\"]") << sel;
       
   657     }
       
   658 
       
   659     {
       
   660         QCss::Selector sel;
       
   661         QCss::BasicSelector basic;
       
   662 
       
   663         basic.elementName = "e";
       
   664         QCss::AttributeSelector attrSel;
       
   665         attrSel.name = "foo";
       
   666         attrSel.value = "warning";
       
   667         attrSel.valueMatchCriterium = QCss::AttributeSelector::MatchContains;
       
   668         basic.attributeSelectors << attrSel;
       
   669         sel.basicSelectors << basic;
       
   670 
       
   671         QTest::newRow("attr-contains") << QString("e[foo~=\"warning\"]") << sel;
       
   672     }
       
   673 
       
   674     {
       
   675         QCss::Selector sel;
       
   676         QCss::BasicSelector basic;
       
   677 
       
   678         basic.elementName = "e";
       
   679         QCss::AttributeSelector attrSel;
       
   680         attrSel.name = "lang";
       
   681         attrSel.value = "en";
       
   682         attrSel.valueMatchCriterium = QCss::AttributeSelector::MatchBeginsWith;
       
   683         basic.attributeSelectors << attrSel;
       
   684         sel.basicSelectors << basic;
       
   685 
       
   686         QTest::newRow("attr-contains") << QString("e[lang|=\"en\"]") << sel;
       
   687     }
       
   688 
       
   689     {
       
   690         QCss::Selector sel;
       
   691         QCss::BasicSelector basic;
       
   692 
       
   693         basic.elementName = "div";
       
   694 
       
   695         QCss::AttributeSelector attrSel;
       
   696         attrSel.name = "class";
       
   697         attrSel.valueMatchCriterium = QCss::AttributeSelector::MatchContains;
       
   698         attrSel.value = "warning";
       
   699         basic.attributeSelectors.append(attrSel);
       
   700 
       
   701         attrSel.value = "foo";
       
   702         basic.attributeSelectors.append(attrSel);
       
   703 
       
   704         sel.basicSelectors << basic;
       
   705 
       
   706         QTest::newRow("class") << QString("div.warning.foo") << sel;
       
   707     }
       
   708 
       
   709     {
       
   710         QCss::Selector sel;
       
   711         QCss::BasicSelector basic;
       
   712 
       
   713         basic.elementName = "e";
       
   714         basic.ids << "myid";
       
   715         sel.basicSelectors << basic;
       
   716 
       
   717         QTest::newRow("id") << QString("e#myid") << sel;
       
   718     }
       
   719 }
       
   720 
       
   721 void tst_QCssParser::selector()
       
   722 {
       
   723     QFETCH(QString, css);
       
   724     QFETCH(QCss::Selector, expectedSelector);
       
   725 
       
   726     QCss::Parser parser(css);
       
   727     QVERIFY(parser.testSelector());
       
   728     QCss::Selector selector;
       
   729     QVERIFY(parser.parseSelector(&selector));
       
   730 
       
   731     QCOMPARE(selector.basicSelectors.count(), expectedSelector.basicSelectors.count());
       
   732     for (int i = 0; i < selector.basicSelectors.count(); ++i) {
       
   733         const QCss::BasicSelector sel = selector.basicSelectors.at(i);
       
   734         const QCss::BasicSelector expectedSel = expectedSelector.basicSelectors.at(i);
       
   735         QCOMPARE(sel.elementName, expectedSel.elementName);
       
   736         QCOMPARE(int(sel.relationToNext), int(expectedSel.relationToNext));
       
   737 
       
   738         QCOMPARE(sel.pseudos.count(), expectedSel.pseudos.count());
       
   739         for (int i = 0; i < sel.pseudos.count(); ++i) {
       
   740             QCOMPARE(sel.pseudos.at(i).name, expectedSel.pseudos.at(i).name);
       
   741             QCOMPARE(sel.pseudos.at(i).function, expectedSel.pseudos.at(i).function);
       
   742         }
       
   743 
       
   744         QCOMPARE(sel.attributeSelectors.count(), expectedSel.attributeSelectors.count());
       
   745         for (int i = 0; i < sel.attributeSelectors.count(); ++i) {
       
   746             QCOMPARE(sel.attributeSelectors.at(i).name, expectedSel.attributeSelectors.at(i).name);
       
   747             QCOMPARE(sel.attributeSelectors.at(i).value, expectedSel.attributeSelectors.at(i).value);
       
   748             QCOMPARE(int(sel.attributeSelectors.at(i).valueMatchCriterium), int(expectedSel.attributeSelectors.at(i).valueMatchCriterium));
       
   749         }
       
   750     }
       
   751 }
       
   752 
       
   753 void tst_QCssParser::prio()
       
   754 {
       
   755     {
       
   756         QCss::Parser parser("!important");
       
   757         QVERIFY(parser.testPrio());
       
   758     }
       
   759     {
       
   760         QCss::Parser parser("!impOrTAnt");
       
   761         QVERIFY(parser.testPrio());
       
   762     }
       
   763     {
       
   764         QCss::Parser parser("!\"important\"");
       
   765         QVERIFY(!parser.testPrio());
       
   766         QCOMPARE(parser.index, 0);
       
   767     }
       
   768     {
       
   769         QCss::Parser parser("!importbleh");
       
   770         QVERIFY(!parser.testPrio());
       
   771         QCOMPARE(parser.index, 0);
       
   772     }
       
   773 }
       
   774 
       
   775 void tst_QCssParser::escapes()
       
   776 {
       
   777     QCss::Parser parser("\\hello");
       
   778     parser.test(QCss::IDENT);
       
   779     QCOMPARE(parser.lexem(), QString("hello"));
       
   780 }
       
   781 
       
   782 void tst_QCssParser::malformedDeclarations_data()
       
   783 {
       
   784     QTest::addColumn<QString>("css");
       
   785 
       
   786     QTest::newRow("1") << QString("p { color:green }");
       
   787     QTest::newRow("2") << QString("p { color:green; color }  /* malformed declaration missing ':', value */");
       
   788     QTest::newRow("3") << QString("p { color:red;   color; color:green }  /* same with expected recovery */");
       
   789     QTest::newRow("4") << QString("p { color:green; color: } /* malformed declaration missing value */");
       
   790     QTest::newRow("5") << QString("p { color:red;   color:; color:green } /* same with expected recovery */");
       
   791     QTest::newRow("6") << QString("p { color:green; color{;color:maroon} } /* unexpected tokens { } */");
       
   792     QTest::newRow("7") << QString("p { color:red;   color{;color:maroon}; color:green } /* same with recovery */");
       
   793 }
       
   794 
       
   795 void tst_QCssParser::malformedDeclarations()
       
   796 {
       
   797     QFETCH(QString, css);
       
   798     QCss::Parser parser(css);
       
   799     QVERIFY(parser.testRuleset());
       
   800     QCss::StyleRule rule;
       
   801     QVERIFY(parser.parseRuleset(&rule));
       
   802 
       
   803     QCOMPARE(rule.selectors.count(), 1);
       
   804     QCOMPARE(rule.selectors.at(0).basicSelectors.count(), 1);
       
   805     QCOMPARE(rule.selectors.at(0).basicSelectors.at(0).elementName, QString("p"));
       
   806 
       
   807     QVERIFY(rule.declarations.count() >= 1);
       
   808     QCOMPARE(int(rule.declarations.last().d->propertyId), int(QCss::Color));
       
   809     QCOMPARE(rule.declarations.last().d->values.count(), 1);
       
   810     QCOMPARE(int(rule.declarations.last().d->values.at(0).type), int(QCss::Value::Identifier));
       
   811     QCOMPARE(rule.declarations.last().d->values.at(0).variant.toString(), QString("green"));
       
   812 }
       
   813 
       
   814 void tst_QCssParser::invalidAtKeywords()
       
   815 {
       
   816     QCss::Parser parser(""
       
   817     "@three-dee {"
       
   818     "  @background-lighting {"
       
   819     "    azimuth: 30deg;"
       
   820     "    elevation: 190deg;"
       
   821     "  }"
       
   822     "  h1 { color: red }"
       
   823     "}"
       
   824     "h1 { color: blue }");
       
   825 
       
   826     QCss::StyleSheet sheet;
       
   827     QVERIFY(parser.parse(&sheet));
       
   828 
       
   829     QCOMPARE(sheet.styleRules.count() + sheet.nameIndex.count(), 1);
       
   830     QCss::StyleRule rule =  (!sheet.styleRules.isEmpty()) ?
       
   831             sheet.styleRules.at(0) : *sheet.nameIndex.begin();
       
   832 
       
   833     QCOMPARE(rule.selectors.count(), 1);
       
   834     QCOMPARE(rule.selectors.at(0).basicSelectors.count(), 1);
       
   835     QCOMPARE(rule.selectors.at(0).basicSelectors.at(0).elementName, QString("h1"));
       
   836 
       
   837     QCOMPARE(rule.declarations.count(), 1);
       
   838     QCOMPARE(int(rule.declarations.at(0).d->propertyId), int(QCss::Color));
       
   839     QCOMPARE(rule.declarations.at(0).d->values.count(), 1);
       
   840     QCOMPARE(int(rule.declarations.at(0).d->values.at(0).type), int(QCss::Value::Identifier));
       
   841     QCOMPARE(rule.declarations.at(0).d->values.at(0).variant.toString(), QString("blue"));
       
   842 }
       
   843 
       
   844 Q_DECLARE_METATYPE(QColor)
       
   845 
       
   846 void tst_QCssParser::colorValue_data()
       
   847 {
       
   848     QTest::addColumn<QString>("css");
       
   849     QTest::addColumn<QColor>("expectedColor");
       
   850 
       
   851     QTest::newRow("identifier") << "color: black" << QColor("black");
       
   852     QTest::newRow("string") << "color: \"green\"" << QColor("green");
       
   853     QTest::newRow("hexcolor") << "color: #12af0e" << QColor(0x12, 0xaf, 0x0e);
       
   854     QTest::newRow("functional1") << "color: rgb(21, 45, 73)" << QColor(21, 45, 73);
       
   855     QTest::newRow("functional2") << "color: rgb(100%, 0%, 100%)" << QColor(0xff, 0, 0xff);
       
   856     QTest::newRow("rgba") << "color: rgba(10, 20, 30, 40)" << QColor(10, 20, 30, 40);
       
   857     QTest::newRow("rgb") << "color: rgb(10, 20, 30, 40)" << QColor(10, 20, 30, 40);
       
   858     QTest::newRow("hsl") << "color: hsv(10, 20, 30)" << QColor::fromHsv(10, 20, 30, 255);
       
   859     QTest::newRow("hsla") << "color: hsva(10, 20, 30, 40)" << QColor::fromHsv(10, 20, 30, 40);
       
   860     QTest::newRow("invalid1") << "color: rgb(why, does, it, always, rain, on, me)" << QColor();
       
   861     QTest::newRow("invalid2") << "color: rgba(i, meant, norway)" << QColor();
       
   862     QTest::newRow("role") << "color: palette(base)" << qApp->palette().color(QPalette::Base);
       
   863     QTest::newRow("role2") << "color: palette( window-text ) " << qApp->palette().color(QPalette::WindowText);
       
   864     QTest::newRow("transparent") << "color: transparent" << QColor(Qt::transparent);
       
   865 }
       
   866 
       
   867 void tst_QCssParser::colorValue()
       
   868 {
       
   869     QFETCH(QString, css);
       
   870     QFETCH(QColor, expectedColor);
       
   871 
       
   872     QCss::Parser parser(css);
       
   873     QCss::Declaration decl;
       
   874     QVERIFY(parser.parseNextDeclaration(&decl));
       
   875     const QColor col = decl.colorValue();
       
   876     QVERIFY(expectedColor.isValid() == col.isValid());
       
   877     QCOMPARE(col, expectedColor);
       
   878 }
       
   879 
       
   880 class DomStyleSelector : public QCss::StyleSelector
       
   881 {
       
   882 public:
       
   883     inline DomStyleSelector(const QDomDocument &doc, const QCss::StyleSheet &sheet)
       
   884         : doc(doc)
       
   885     {
       
   886         styleSheets.append(sheet);
       
   887     }
       
   888 
       
   889     virtual QStringList nodeNames(NodePtr node) const { return QStringList(reinterpret_cast<QDomElement *>(node.ptr)->tagName()); }
       
   890     virtual QString attribute(NodePtr node, const QString &name) const { return reinterpret_cast<QDomElement *>(node.ptr)->attribute(name); }
       
   891     virtual bool hasAttribute(NodePtr node, const QString &name) const { return reinterpret_cast<QDomElement *>(node.ptr)->hasAttribute(name); }
       
   892     virtual bool hasAttributes(NodePtr node) const { return reinterpret_cast<QDomElement *>(node.ptr)->hasAttributes(); }
       
   893 
       
   894     virtual bool isNullNode(NodePtr node) const {
       
   895         return reinterpret_cast<QDomElement *>(node.ptr)->isNull();
       
   896     }
       
   897     virtual NodePtr parentNode(NodePtr node) const {
       
   898         NodePtr parent;
       
   899         parent.ptr = new QDomElement(reinterpret_cast<QDomElement *>(node.ptr)->parentNode().toElement());
       
   900         return parent;
       
   901     }
       
   902     virtual NodePtr duplicateNode(NodePtr node) const {
       
   903         NodePtr n;
       
   904         n.ptr = new QDomElement(*reinterpret_cast<QDomElement *>(node.ptr));
       
   905         return n;
       
   906     }
       
   907     virtual NodePtr previousSiblingNode(NodePtr node) const {
       
   908         NodePtr sibling;
       
   909         sibling.ptr = new QDomElement(reinterpret_cast<QDomElement *>(node.ptr)->previousSiblingElement());
       
   910         return sibling;
       
   911     }
       
   912     virtual void freeNode(NodePtr node) const {
       
   913         delete reinterpret_cast<QDomElement *>(node.ptr);
       
   914     }
       
   915 
       
   916 private:
       
   917     QDomDocument doc;
       
   918 };
       
   919 
       
   920 Q_DECLARE_METATYPE(QDomDocument)
       
   921 
       
   922 void tst_QCssParser::marginValue_data()
       
   923 {
       
   924     QTest::addColumn<QString>("css");
       
   925     QTest::addColumn<QString>("expectedMargin");
       
   926 
       
   927     QFont f;
       
   928     int ex = QFontMetrics(f).xHeight();
       
   929     int em = QFontMetrics(f).height();
       
   930 
       
   931     QTest::newRow("one value") << "margin: 1px" << "1 1 1 1";
       
   932     QTest::newRow("two values") << "margin: 1px 2px" << "1 2 1 2";
       
   933     QTest::newRow("three value") << "margin: 1px 2px 3px" << "1 2 3 2";
       
   934     QTest::newRow("four values") << "margin: 1px 2px 3px 4px" << "1 2 3 4";
       
   935     QTest::newRow("default px") << "margin: 1 2 3 4" << "1 2 3 4";
       
   936     QTest::newRow("no unit") << "margin: 1 2 3 4" << "1 2 3 4";
       
   937     QTest::newRow("em") << "margin: 1ex 2ex 3ex 4ex" << QString("%1 %2 %3 %4").arg(ex).arg(2*ex).arg(3*ex).arg(4*ex);
       
   938     QTest::newRow("ex") << "margin: 1 2em 3px 4ex" << QString("%1 %2 %3 %4").arg(1).arg(2*em).arg(3).arg(4*ex);
       
   939 
       
   940     f.setPointSize(20);
       
   941     f.setBold(true);
       
   942     ex = QFontMetrics(f).xHeight();
       
   943     em = QFontMetrics(f).height();
       
   944     QTest::newRow("em2") << "font: bold 20pt; margin: 1ex 2ex 3ex 4ex" << QString("%1 %2 %3 %4").arg(ex).arg(2*ex).arg(3*ex).arg(4*ex);
       
   945     QTest::newRow("ex2") << "margin: 1 2em 3px 4ex; font-size: 20pt; font-weight: bold;" << QString("%1 %2 %3 %4").arg(1).arg(2*em).arg(3).arg(4*ex);
       
   946 
       
   947     QTest::newRow("crap") << "margin: crap" << "0 0 0 0";
       
   948 }
       
   949 
       
   950 void tst_QCssParser::marginValue()
       
   951 {
       
   952     QFETCH(QString, css);
       
   953     QFETCH(QString, expectedMargin);
       
   954 
       
   955     QDomDocument doc;
       
   956     QVERIFY(doc.setContent(QLatin1String("<!DOCTYPE test><test> <dummy/> </test>")));
       
   957 
       
   958     css.prepend("dummy {");
       
   959     css.append("}");
       
   960 
       
   961     QCss::Parser parser(css);
       
   962     QCss::StyleSheet sheet;
       
   963     QVERIFY(parser.parse(&sheet));
       
   964 
       
   965     DomStyleSelector testSelector(doc, sheet);
       
   966     QDomElement e = doc.documentElement().firstChildElement();
       
   967     QCss::StyleSelector::NodePtr n;
       
   968     n.ptr = &e;
       
   969     QVector<QCss::StyleRule> rules = testSelector.styleRulesForNode(n);
       
   970     QVector<QCss::Declaration> decls = rules.at(0).declarations;
       
   971     QCss::ValueExtractor v(decls);
       
   972 
       
   973     {
       
   974     int m[4];
       
   975     int p[4];
       
   976     int spacing;
       
   977     v.extractBox(m, p, &spacing);
       
   978     QString str = QString("%1 %2 %3 %4").arg(m[0]).arg(m[1]).arg(m[2]).arg(m[3]);
       
   979     QCOMPARE(str, expectedMargin);
       
   980     }
       
   981 }
       
   982 
       
   983 void tst_QCssParser::styleSelector_data()
       
   984 {
       
   985     QTest::addColumn<bool>("match");
       
   986     QTest::addColumn<QString>("selector");
       
   987     QTest::addColumn<QString>("xml");
       
   988     QTest::addColumn<QString>("elementToCheck");
       
   989 
       
   990     QTest::newRow("plain") << true << QString("p") << QString("<p />") << QString();
       
   991     QTest::newRow("noplain") << false << QString("bar") << QString("<p />") << QString();
       
   992 
       
   993     QTest::newRow("class") << true << QString(".foo") << QString("<p class=\"foo\" />") << QString();
       
   994     QTest::newRow("noclass") << false << QString(".bar") << QString("<p class=\"foo\" />") << QString();
       
   995 
       
   996     QTest::newRow("attrset") << true << QString("[justset]") << QString("<p justset=\"bar\" />") << QString();
       
   997     QTest::newRow("notattrset") << false << QString("[justset]") << QString("<p otherattribute=\"blub\" />") << QString();
       
   998 
       
   999     QTest::newRow("attrmatch") << true << QString("[foo=bar]") << QString("<p foo=\"bar\" />") << QString();
       
  1000     QTest::newRow("noattrmatch") << false << QString("[foo=bar]") << QString("<p foo=\"xyz\" />") << QString();
       
  1001 
       
  1002     QTest::newRow("contains") << true << QString("[foo~=bar]") << QString("<p foo=\"baz bleh bar\" />") << QString();
       
  1003     QTest::newRow("notcontains") << false << QString("[foo~=bar]") << QString("<p foo=\"test\" />") << QString();
       
  1004 
       
  1005     QTest::newRow("beingswith") << true << QString("[foo|=bar]") << QString("<p foo=\"bar-bleh\" />") << QString();
       
  1006     QTest::newRow("notbeingswith") << false << QString("[foo|=bar]") << QString("<p foo=\"bleh-bar\" />") << QString();
       
  1007 
       
  1008     QTest::newRow("attr2") << true << QString("[bar=foo]") << QString("<p bleh=\"bar\" bar=\"foo\" />") << QString();
       
  1009 
       
  1010     QTest::newRow("universal1") << true << QString("*") << QString("<p />") << QString();
       
  1011 
       
  1012     QTest::newRow("universal3") << false << QString("*[foo=bar]") << QString("<p foo=\"bleh\" />") << QString();
       
  1013     QTest::newRow("universal4") << true << QString("*[foo=bar]") << QString("<p foo=\"bar\" />") << QString();
       
  1014 
       
  1015     QTest::newRow("universal5") << false << QString("[foo=bar]") << QString("<p foo=\"bleh\" />") << QString();
       
  1016     QTest::newRow("universal6") << true << QString("[foo=bar]") << QString("<p foo=\"bar\" />") << QString();
       
  1017 
       
  1018     QTest::newRow("universal7") << true << QString(".charfmt1") << QString("<p class=\"charfmt1\" />") << QString();
       
  1019 
       
  1020     QTest::newRow("id") << true << QString("#blub") << QString("<p id=\"blub\" />") << QString();
       
  1021     QTest::newRow("noid") << false << QString("#blub") << QString("<p id=\"other\" />") << QString();
       
  1022 
       
  1023     QTest::newRow("childselector") << true << QString("parent > child")
       
  1024                                    << QString("<parent><child /></parent>")
       
  1025                                    << QString("parent/child");
       
  1026 
       
  1027     QTest::newRow("nochildselector2") << false << QString("parent > child")
       
  1028                                    << QString("<child><parent /></child>")
       
  1029                                    << QString("child/parent");
       
  1030 
       
  1031     QTest::newRow("nochildselector3") << false << QString("parent > child")
       
  1032                                    << QString("<parent><intermediate><child /></intermediate></parent>")
       
  1033                                    << QString("parent/intermediate/child");
       
  1034 
       
  1035     QTest::newRow("childselector2") << true << QString("parent[foo=bar] > child")
       
  1036                                    << QString("<parent foo=\"bar\"><child /></parent>")
       
  1037                                    << QString("parent/child");
       
  1038 
       
  1039     QTest::newRow("nochildselector4") << false << QString("parent[foo=bar] > child")
       
  1040                                    << QString("<parent><child /></parent>")
       
  1041                                    << QString("parent/child");
       
  1042 
       
  1043     QTest::newRow("nochildselector5") << false << QString("parent[foo=bar] > child")
       
  1044                                    << QString("<parent foo=\"bar\"><parent><child /></parent></parent>")
       
  1045                                    << QString("parent/parent/child");
       
  1046 
       
  1047     QTest::newRow("childselectors") << true << QString("grandparent > parent > child")
       
  1048                                    << QString("<grandparent><parent><child /></parent></grandparent>")
       
  1049                                    << QString("grandparent/parent/child");
       
  1050 
       
  1051     QTest::newRow("descendant") << true << QString("grandparent child")
       
  1052                                    << QString("<grandparent><parent><child /></parent></grandparent>")
       
  1053                                    << QString("grandparent/parent/child");
       
  1054 
       
  1055     QTest::newRow("nodescendant") << false << QString("grandparent child")
       
  1056                                    << QString("<other><parent><child /></parent></other>")
       
  1057                                    << QString("other/parent/child");
       
  1058 
       
  1059     QTest::newRow("descendant2") << true << QString("grandgrandparent grandparent child")
       
  1060                                    << QString("<grandgrandparent><inbetween><grandparent><parent><child /></parent></grandparent></inbetween></grandgrandparent>")
       
  1061                                    << QString("grandgrandparent/inbetween/grandparent/parent/child");
       
  1062 
       
  1063     QTest::newRow("combined") << true << QString("grandparent parent > child")
       
  1064                               << QString("<grandparent><inbetween><parent><child /></parent></inbetween></grandparent>")
       
  1065                               << QString("grandparent/inbetween/parent/child");
       
  1066 
       
  1067     QTest::newRow("combined2") << true << QString("grandparent > parent child")
       
  1068                               << QString("<grandparent><parent><inbetween><child /></inbetween></parent></grandparent>")
       
  1069                               << QString("grandparent/parent/inbetween/child");
       
  1070 
       
  1071     QTest::newRow("combined3") << true << QString("grandparent > parent child")
       
  1072                               << QString("<grandparent><parent><inbetween><child /></inbetween></parent></grandparent>")
       
  1073                               << QString("grandparent/parent/inbetween/child");
       
  1074 
       
  1075     QTest::newRow("nocombined") << false << QString("grandparent parent > child")
       
  1076                               << QString("<inbetween><parent><child /></parent></inbetween>")
       
  1077                               << QString("inbetween/parent/child");
       
  1078 
       
  1079     QTest::newRow("nocombined2") << false << QString("grandparent parent > child")
       
  1080                               << QString("<parent><child /></parent>")
       
  1081                               << QString("parent/child");
       
  1082 
       
  1083     QTest::newRow("previoussibling") << true << QString("p1 + p2")
       
  1084                                      << QString("<p1 /><p2 />")
       
  1085                                      << QString("p2");
       
  1086 
       
  1087     QTest::newRow("noprevioussibling") << false << QString("p2 + p1")
       
  1088                                      << QString("<p1 /><p2 />")
       
  1089                                      << QString("p2");
       
  1090 
       
  1091     QTest::newRow("ancestry_firstmismatch") << false << QString("parent child[foo=bar]")
       
  1092                                             << QString("<parent><child /></parent>")
       
  1093                                             << QString("parent/child");
       
  1094 
       
  1095     QTest::newRow("unknown-pseudo") << false << QString("p:enabled:foobar") << QString("<p/>") << QString();
       
  1096 }
       
  1097 
       
  1098 void tst_QCssParser::styleSelector()
       
  1099 {
       
  1100     QFETCH(bool, match);
       
  1101     QFETCH(QString, selector);
       
  1102     QFETCH(QString, xml);
       
  1103     QFETCH(QString, elementToCheck);
       
  1104 
       
  1105     QString css = QString("%1 { background-color: green }").arg(selector);
       
  1106     QCss::Parser parser(css);
       
  1107     QCss::StyleSheet sheet;
       
  1108     QVERIFY(parser.parse(&sheet));
       
  1109 
       
  1110     QDomDocument doc;
       
  1111     xml.prepend("<!DOCTYPE test><test>");
       
  1112     xml.append("</test>");
       
  1113     QVERIFY(doc.setContent(xml));
       
  1114 
       
  1115     DomStyleSelector testSelector(doc, sheet);
       
  1116 
       
  1117     QDomElement e = doc.documentElement();
       
  1118     if (elementToCheck.isEmpty()) {
       
  1119         e = e.firstChildElement();
       
  1120     } else {
       
  1121         QStringList path = elementToCheck.split(QLatin1Char('/'));
       
  1122         do {
       
  1123             e = e.namedItem(path.takeFirst()).toElement();
       
  1124         } while (!path.isEmpty());
       
  1125     }
       
  1126     QVERIFY(!e.isNull());
       
  1127     QCss::StyleSelector::NodePtr n;
       
  1128     n.ptr = &e;
       
  1129     QVector<QCss::Declaration> decls = testSelector.declarationsForNode(n);
       
  1130 
       
  1131     if (match) {
       
  1132         QCOMPARE(decls.count(), 1);
       
  1133         QCOMPARE(int(decls.at(0).d->propertyId), int(QCss::BackgroundColor));
       
  1134         QCOMPARE(decls.at(0).d->values.count(), 1);
       
  1135         QCOMPARE(int(decls.at(0).d->values.at(0).type), int(QCss::Value::Identifier));
       
  1136         QCOMPARE(decls.at(0).d->values.at(0).variant.toString(), QString("green"));
       
  1137     } else {
       
  1138         QVERIFY(decls.isEmpty());
       
  1139     }
       
  1140 }
       
  1141 
       
  1142 void tst_QCssParser::specificity_data()
       
  1143 {
       
  1144     QTest::addColumn<QString>("selector");
       
  1145     QTest::addColumn<int>("specificity");
       
  1146 
       
  1147     QTest::newRow("universal") << QString("*") << 0;
       
  1148 
       
  1149     QTest::newRow("elements+pseudos1") << QString("foo") << 1;
       
  1150     QTest::newRow("elements+pseudos2") << QString("foo *[blah]") << 1 + (1 * 0x10);
       
  1151 
       
  1152     // should strictly speaking be '2', but we don't support pseudo-elements yet,
       
  1153     // only pseudo-classes
       
  1154     QTest::newRow("elements+pseudos3") << QString("li:first-line") << 1 + (1 * 0x10);
       
  1155 
       
  1156     QTest::newRow("elements+pseudos4") << QString("ul li") << 2;
       
  1157     QTest::newRow("elements+pseudos5") << QString("ul ol+li") << 3;
       
  1158     QTest::newRow("elements+pseudos6") << QString("h1 + *[rel=up]") << 1 + (1 * 0x10);
       
  1159 
       
  1160     QTest::newRow("elements+pseudos7") << QString("ul ol li.red") << 3 + (1 * 0x10);
       
  1161     QTest::newRow("elements+pseudos8") << QString("li.red.level") << 1 + (2 * 0x10);
       
  1162     QTest::newRow("id") << QString("#x34y") << 1 * 0x100;
       
  1163 }
       
  1164 
       
  1165 void tst_QCssParser::specificity()
       
  1166 {
       
  1167     QFETCH(QString, selector);
       
  1168 
       
  1169     QString css = QString("%1 { }").arg(selector);
       
  1170     QCss::Parser parser(css);
       
  1171     QCss::StyleSheet sheet;
       
  1172     QVERIFY(parser.parse(&sheet));
       
  1173 
       
  1174     QCOMPARE(sheet.styleRules.count() + sheet.nameIndex.count() + sheet.idIndex.count() , 1);
       
  1175     QCss::StyleRule rule =  (!sheet.styleRules.isEmpty()) ? sheet.styleRules.at(0)
       
  1176                         :  (!sheet.nameIndex.isEmpty())  ? *sheet.nameIndex.begin()
       
  1177                         :  *sheet.idIndex.begin();
       
  1178     QCOMPARE(rule.selectors.count(), 1);
       
  1179     QTEST(rule.selectors.at(0).specificity(), "specificity");
       
  1180 }
       
  1181 
       
  1182 void tst_QCssParser::specificitySort_data()
       
  1183 {
       
  1184     QTest::addColumn<QString>("firstSelector");
       
  1185     QTest::addColumn<QString>("secondSelector");
       
  1186     QTest::addColumn<QString>("xml");
       
  1187 
       
  1188     QTest::newRow("universal1") << QString("*") << QString("p") << QString("<p />");
       
  1189     QTest::newRow("attr") << QString("p") << QString("p[foo=bar]") << QString("<p foo=\"bar\" />");
       
  1190     QTest::newRow("id") << QString("p") << QString("#hey") << QString("<p id=\"hey\" />");
       
  1191     QTest::newRow("id2") << QString("[id=hey]") << QString("#hey") << QString("<p id=\"hey\" />");
       
  1192     QTest::newRow("class") << QString("p") << QString(".hey") << QString("<p class=\"hey\" />");
       
  1193 }
       
  1194 
       
  1195 void tst_QCssParser::specificitySort()
       
  1196 {
       
  1197     QFETCH(QString, firstSelector);
       
  1198     QFETCH(QString, secondSelector);
       
  1199     QFETCH(QString, xml);
       
  1200 
       
  1201     firstSelector.append(" { color: green; }");
       
  1202     secondSelector.append(" { color: red; }");
       
  1203 
       
  1204     QDomDocument doc;
       
  1205     xml.prepend("<!DOCTYPE test><test>");
       
  1206     xml.append("</test>");
       
  1207     QVERIFY(doc.setContent(xml));
       
  1208 
       
  1209     for (int i = 0; i < 2; ++i) {
       
  1210         QString css;
       
  1211         if (i == 0)
       
  1212             css = firstSelector + secondSelector;
       
  1213         else
       
  1214             css = secondSelector + firstSelector;
       
  1215 
       
  1216         QCss::Parser parser(css);
       
  1217         QCss::StyleSheet sheet;
       
  1218         QVERIFY(parser.parse(&sheet));
       
  1219 
       
  1220         DomStyleSelector testSelector(doc, sheet);
       
  1221 
       
  1222         QDomElement e = doc.documentElement().firstChildElement();
       
  1223         QCss::StyleSelector::NodePtr n;
       
  1224         n.ptr = &e;
       
  1225         QVector<QCss::Declaration> decls = testSelector.declarationsForNode(n);
       
  1226 
       
  1227         QCOMPARE(decls.count(), 2);
       
  1228 
       
  1229         QCOMPARE(int(decls.at(0).d->propertyId), int(QCss::Color));
       
  1230         QCOMPARE(decls.at(0).d->values.count(), 1);
       
  1231         QCOMPARE(int(decls.at(0).d->values.at(0).type), int(QCss::Value::Identifier));
       
  1232         QCOMPARE(decls.at(0).d->values.at(0).variant.toString(), QString("green"));
       
  1233 
       
  1234         QCOMPARE(int(decls.at(1).d->propertyId), int(QCss::Color));
       
  1235         QCOMPARE(decls.at(1).d->values.count(), 1);
       
  1236         QCOMPARE(int(decls.at(1).d->values.at(0).type), int(QCss::Value::Identifier));
       
  1237         QCOMPARE(decls.at(1).d->values.at(0).variant.toString(), QString("red"));
       
  1238     }
       
  1239 }
       
  1240 
       
  1241 void tst_QCssParser::rulesForNode_data()
       
  1242 {
       
  1243     QTest::addColumn<QString>("xml");
       
  1244     QTest::addColumn<QString>("css");
       
  1245     QTest::addColumn<quint64>("pseudoClass");
       
  1246     QTest::addColumn<int>("declCount");
       
  1247     QTest::addColumn<QString>("value0");
       
  1248     QTest::addColumn<QString>("value1");
       
  1249 
       
  1250     QTest::newRow("universal1") << QString("<p/>") << QString("* { color: red }")
       
  1251                                 << (quint64)QCss::PseudoClass_Unspecified << 1 << "red" << "";
       
  1252 
       
  1253     QTest::newRow("basic") << QString("<p/>") << QString("p:enabled { color: red; bg:blue; }")
       
  1254         << (quint64)QCss::PseudoClass_Enabled << 2 << "red" << "blue";
       
  1255 
       
  1256     QTest::newRow("single") << QString("<p/>")
       
  1257         << QString("p:enabled { color: red; } *:hover { color: white }")
       
  1258         << (quint64)QCss::PseudoClass_Hover << 1 << "white" << "";
       
  1259 
       
  1260     QTest::newRow("multisel") << QString("<p/>")
       
  1261         << QString("p:enabled { color: red; } p:hover { color: gray } *:hover { color: white } ")
       
  1262         << (quint64)QCss::PseudoClass_Hover << 2 << "white" << "gray";
       
  1263 
       
  1264     QTest::newRow("multisel2") << QString("<p/>")
       
  1265         << QString("p:enabled { color: red; } p:hover:focus { color: gray } *:hover { color: white } ")
       
  1266         << quint64(QCss::PseudoClass_Hover|QCss::PseudoClass_Focus) << 2 << "white" << "gray";
       
  1267 
       
  1268     QTest::newRow("multisel3-diffspec") << QString("<p/>")
       
  1269         << QString("p:enabled { color: red; } p:hover:focus { color: gray } *:hover { color: white } ")
       
  1270         << quint64(QCss::PseudoClass_Hover) << 1 << "white" << "";
       
  1271 
       
  1272     QTest::newRow("!-1") << QString("<p/>")
       
  1273         << QString("p:checked:!hover { color: red; } p:checked:hover { color: gray } p:checked { color: white }")
       
  1274         << quint64(QCss::PseudoClass_Hover|QCss::PseudoClass_Checked) << 2 << "white" << "gray";
       
  1275 
       
  1276     QTest::newRow("!-2") << QString("<p/>")
       
  1277         << QString("p:checked:!hover:!pressed { color: red; } p:!checked:hover { color: gray } p:!focus { color: blue }")
       
  1278         << quint64(QCss::PseudoClass_Focus) << 0 << "" << "";
       
  1279 
       
  1280     QTest::newRow("!-3") << QString("<p/>")
       
  1281         << QString("p:checked:!hover:!pressed { color: red; } p:!checked:hover { color: gray } p:!focus { color: blue; }")
       
  1282         << quint64(QCss::PseudoClass_Pressed) << 1 << "blue" << "";
       
  1283 }
       
  1284 
       
  1285 void tst_QCssParser::rulesForNode()
       
  1286 {
       
  1287     QFETCH(QString, xml);
       
  1288     QFETCH(QString, css);
       
  1289     QFETCH(quint64, pseudoClass);
       
  1290     QFETCH(int, declCount);
       
  1291     QFETCH(QString, value0);
       
  1292     QFETCH(QString, value1);
       
  1293 
       
  1294     QDomDocument doc;
       
  1295     xml.prepend("<!DOCTYPE test><test>");
       
  1296     xml.append("</test>");
       
  1297     QVERIFY(doc.setContent(xml));
       
  1298 
       
  1299     QCss::Parser parser(css);
       
  1300     QCss::StyleSheet sheet;
       
  1301     QVERIFY(parser.parse(&sheet));
       
  1302 
       
  1303     DomStyleSelector testSelector(doc, sheet);
       
  1304     QDomElement e = doc.documentElement().firstChildElement();
       
  1305     QCss::StyleSelector::NodePtr n;
       
  1306     n.ptr = &e;
       
  1307     QVector<QCss::StyleRule> rules = testSelector.styleRulesForNode(n);
       
  1308 
       
  1309     QVector<QCss::Declaration> decls;
       
  1310     for (int i = 0; i < rules.count(); i++) {
       
  1311         const QCss::Selector &selector = rules.at(i).selectors.at(0);
       
  1312         quint64 negated = 0;
       
  1313         quint64 cssClass = selector.pseudoClass(&negated);
       
  1314         if ((cssClass == QCss::PseudoClass_Unspecified)
       
  1315             || ((((cssClass & pseudoClass) == cssClass)) && ((negated & pseudoClass) == 0)))
       
  1316             decls += rules.at(i).declarations;
       
  1317     }
       
  1318 
       
  1319     QVERIFY(decls.count() == declCount);
       
  1320 
       
  1321     if (declCount > 0)
       
  1322         QCOMPARE(decls.at(0).d->values.at(0).variant.toString(), value0);
       
  1323     if (declCount > 1)
       
  1324         QCOMPARE(decls.at(1).d->values.at(0).variant.toString(), value1);
       
  1325 }
       
  1326 
       
  1327 void tst_QCssParser::shorthandBackgroundProperty_data()
       
  1328 {
       
  1329     QTest::addColumn<QString>("css");
       
  1330     QTest::addColumn<QBrush>("expectedBrush");
       
  1331     QTest::addColumn<QString>("expectedImage");
       
  1332     QTest::addColumn<int>("expectedRepeatValue");
       
  1333     QTest::addColumn<int>("expectedAlignment");
       
  1334 
       
  1335     QTest::newRow("simple color") << "background: red" << QBrush(QColor("red")) << QString() << int(QCss::Repeat_XY) << int(Qt::AlignLeft | Qt::AlignTop);
       
  1336     QTest::newRow("plain color") << "background-color: red" << QBrush(QColor("red")) << QString() << int(QCss::Repeat_XY) << int(Qt::AlignLeft | Qt::AlignTop);
       
  1337     QTest::newRow("palette color") << "background-color: palette(mid)" << qApp->palette().mid() << QString() << int(QCss::Repeat_XY) << int(Qt::AlignLeft | Qt::AlignTop);
       
  1338     QTest::newRow("multiple") << "background: url(chess.png) blue repeat-y" << QBrush(QColor("blue")) << QString("chess.png") << int(QCss::Repeat_Y) << int(Qt::AlignLeft | Qt::AlignTop);
       
  1339     QTest::newRow("plain alignment") << "background-position: center" << QBrush() << QString() << int(QCss::Repeat_XY) << int(Qt::AlignCenter);
       
  1340     QTest::newRow("plain alignment2") << "background-position: left top" << QBrush() << QString() << int(QCss::Repeat_XY) << int(Qt::AlignLeft | Qt::AlignTop);
       
  1341     QTest::newRow("plain alignment3") << "background-position: left" << QBrush() << QString() << int(QCss::Repeat_XY) << int(Qt::AlignLeft | Qt::AlignVCenter);
       
  1342     QTest::newRow("multi") << "background: left url(blah.png) repeat-x" << QBrush() << QString("blah.png") << int(QCss::Repeat_X) << int(Qt::AlignLeft | Qt::AlignVCenter);
       
  1343     QTest::newRow("multi2") << "background: url(blah.png) repeat-x top" << QBrush() << QString("blah.png") << int(QCss::Repeat_X) << int(Qt::AlignTop | Qt::AlignHCenter);
       
  1344     QTest::newRow("multi3") << "background: url(blah.png) top right" << QBrush() << QString("blah.png") << int(QCss::Repeat_XY) << int(Qt::AlignTop | Qt::AlignRight);
       
  1345 }
       
  1346 
       
  1347 void tst_QCssParser::shorthandBackgroundProperty()
       
  1348 {
       
  1349     QFETCH(QString, css);
       
  1350 
       
  1351     QDomDocument doc;
       
  1352     QVERIFY(doc.setContent(QLatin1String("<!DOCTYPE test><test> <dummy/> </test>")));
       
  1353 
       
  1354     css.prepend("dummy {");
       
  1355     css.append("}");
       
  1356 
       
  1357     QCss::Parser parser(css);
       
  1358     QCss::StyleSheet sheet;
       
  1359     QVERIFY(parser.parse(&sheet));
       
  1360 
       
  1361     DomStyleSelector testSelector(doc, sheet);
       
  1362     QDomElement e = doc.documentElement().firstChildElement();
       
  1363     QCss::StyleSelector::NodePtr n;
       
  1364     n.ptr = &e;
       
  1365     QVector<QCss::StyleRule> rules = testSelector.styleRulesForNode(n);
       
  1366     QVector<QCss::Declaration> decls = rules.at(0).declarations;
       
  1367     QCss::ValueExtractor v(decls);
       
  1368 
       
  1369     QBrush brush;
       
  1370     QString image;
       
  1371     QCss::Repeat repeat = QCss::Repeat_XY;
       
  1372     Qt::Alignment alignment = Qt::AlignTop | Qt::AlignLeft;
       
  1373     QCss::Origin origin = QCss::Origin_Padding;
       
  1374     QCss::Attachment attachment;
       
  1375     QCss::Origin ignoredOrigin;
       
  1376     v.extractBackground(&brush, &image, &repeat, &alignment, &origin, &attachment, &ignoredOrigin);
       
  1377 
       
  1378     QFETCH(QBrush, expectedBrush);
       
  1379     QVERIFY(expectedBrush.color() == brush.color());
       
  1380 
       
  1381     QTEST(image, "expectedImage");
       
  1382     QTEST(int(repeat), "expectedRepeatValue");
       
  1383     QTEST(int(alignment), "expectedAlignment");
       
  1384 }
       
  1385 
       
  1386 void tst_QCssParser::pseudoElement_data()
       
  1387 {
       
  1388     QTest::addColumn<QString>("css");
       
  1389     QTest::addColumn<QString>("pseudoElement");
       
  1390     QTest::addColumn<int>("declCount");
       
  1391 
       
  1392     // QComboBox::dropDown { border-image: blah; }
       
  1393     QTest::newRow("no pseudo-elements") << QString("dummy:hover { color: red }") << "" << 1;
       
  1394     QTest::newRow("no pseudo-elements") << QString("dummy:hover { color: red }") << "pe" << 0;
       
  1395 
       
  1396     QTest::newRow("1 pseudo-element (1)") << QString("dummy::pe:hover { color: red }") << "pe" << 1;
       
  1397     QTest::newRow("1 pseudo-element (2)") << QString("dummy::pe:hover { color: red }") << "x" << 0;
       
  1398     QTest::newRow("1 pseudo-element (2)") << QString("whatever::pe:hover { color: red }") << "pe" << 0;
       
  1399 
       
  1400     QTest::newRow("1 pseudo-element (3)")
       
  1401         << QString("dummy { color: white; } dummy::pe:hover { color: red }") << "x" << 0;
       
  1402     QTest::newRow("1 pseudo-element (4)")
       
  1403         << QString("dummy { color: white; } dummy::pe:hover { color: red } dummy { x:y }") << "" << 2;
       
  1404     QTest::newRow("1 pseudo-element (5)")
       
  1405         << QString("dummy { color: white; } dummy::pe:hover { color: red }") << "pe" << 1;
       
  1406     QTest::newRow("1 pseudo-element (6)")
       
  1407       << QString("dummy { color: white; } dummy::pe:hover { color: red } dummy::pe:checked { x: y} ") << "pe" << 2;
       
  1408 
       
  1409     QTest::newRow("2 pseudo-elements (1)")
       
  1410       << QString("dummy { color: white; } dummy::pe1:hover { color: red } dummy::pe2:checked { x: y} ")
       
  1411       << "" << 1;
       
  1412     QTest::newRow("2 pseudo-elements (1)")
       
  1413       << QString("dummy { color: white; } dummy::pe1:hover { color: red } dummy::pe2:checked { x: y} ")
       
  1414       << "pe1" << 1;
       
  1415     QTest::newRow("2 pseudo-elements (2)")
       
  1416       << QString("dummy { color: white; } dummy::pe1:hover { color: red } dummy::pe2:checked { x: y} ")
       
  1417       << "pe2" << 1;
       
  1418 }
       
  1419 
       
  1420 void tst_QCssParser::pseudoElement()
       
  1421 {
       
  1422     QFETCH(QString, css);
       
  1423     QFETCH(QString, pseudoElement);
       
  1424     QFETCH(int, declCount);
       
  1425 
       
  1426     QDomDocument doc;
       
  1427     QVERIFY(doc.setContent(QLatin1String("<!DOCTYPE test><test> <dummy/> </test>")));
       
  1428 
       
  1429     QCss::Parser parser(css);
       
  1430     QCss::StyleSheet sheet;
       
  1431     QVERIFY(parser.parse(&sheet));
       
  1432 
       
  1433     DomStyleSelector testSelector(doc, sheet);
       
  1434     QDomElement e = doc.documentElement().firstChildElement();
       
  1435     QCss::StyleSelector::NodePtr n;
       
  1436     n.ptr = &e;
       
  1437     QVector<QCss::StyleRule> rules = testSelector.styleRulesForNode(n);
       
  1438     QVector<QCss::Declaration> decls;
       
  1439     for (int i = 0; i < rules.count(); i++) {
       
  1440         const QCss::Selector& selector = rules.at(i).selectors.at(0);
       
  1441         if (pseudoElement.compare(selector.pseudoElement(), Qt::CaseInsensitive) != 0)
       
  1442             continue;
       
  1443         decls += rules.at(i).declarations;
       
  1444 
       
  1445     }
       
  1446     QVERIFY(decls.count() == declCount);
       
  1447 }
       
  1448 
       
  1449 void tst_QCssParser::gradient_data()
       
  1450 {
       
  1451     QTest::addColumn<QString>("css");
       
  1452     QTest::addColumn<QString>("type");
       
  1453     QTest::addColumn<QPointF>("start");
       
  1454     QTest::addColumn<QPointF>("finalStop");
       
  1455     QTest::addColumn<int>("spread");
       
  1456     QTest::addColumn<qreal>("stop0");
       
  1457     QTest::addColumn<QColor>("color0");
       
  1458     QTest::addColumn<qreal>("stop1");
       
  1459     QTest::addColumn<QColor>("color1");
       
  1460 
       
  1461     QTest::newRow("color-string") <<
       
  1462      "selection-background-color: qlineargradient(x1:1, y1:2, x2:3, y2:4, "
       
  1463          "stop:0.2 red, stop:0.5 green)" << "linear" << QPointF(1, 2) << QPointF(3, 4)
       
  1464                                   << 0 << qreal(0.2) << QColor("red") << qreal(0.5) << QColor("green");
       
  1465 
       
  1466     QTest::newRow("color-#") <<
       
  1467      "selection-background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
       
  1468          "spread: reflect, stop:0.2 #123, stop:0.5 #456)" << "linear" << QPointF(0, 0) << QPointF(0, 1)
       
  1469                              << 1 << qreal(0.2) << QColor("#123") << qreal(0.5) << QColor("#456");
       
  1470 
       
  1471     QTest::newRow("color-rgb") <<
       
  1472      "selection-background-color: qlineargradient(x1:0, y1:0, x2:0, y2:1, "
       
  1473          "spread: reflect, stop:0.2 rgb(1, 2, 3), stop:0.5 rgba(1, 2, 3, 4))" << "linear" << QPointF(0, 0) << QPointF(0, 1)
       
  1474                              << 1 << qreal(0.2) << QColor(1, 2, 3) << qreal(0.5) << QColor(1, 2, 3, 4);
       
  1475 
       
  1476     QTest::newRow("color-spaces") <<
       
  1477      "selection-background-color: qlineargradient(x1: 0, y1 :0,x2:0, y2 : 1 , "
       
  1478          "spread: reflect, stop:0.2 rgb(1, 2, 3), stop: 0.5   rgba(1, 2, 3, 4))" << "linear" << QPointF(0, 0) << QPointF(0, 1)
       
  1479                              << 1 << qreal(0.2) << QColor(1, 2, 3) << qreal(0.5) << QColor(1, 2, 3, 4);
       
  1480 
       
  1481     QTest::newRow("conical gradient") <<
       
  1482      "selection-background-color: qconicalgradient(cx: 4, cy : 2, angle: 23, "
       
  1483          "spread: repeat, stop:0.2 rgb(1, 2, 3), stop:0.5 rgba(1, 2, 3, 4))" << "conical" << QPointF(4, 2) << QPointF()
       
  1484                              << 2 << qreal(0.2) << QColor(1, 2, 3) << qreal(0.5) << QColor(1, 2, 3, 4);
       
  1485 
       
  1486     /* wont pass: stop values are expected to be sorted
       
  1487      QTest::newRow("unsorted-stop") <<
       
  1488      "selection-background: lineargradient(x1:0, y1:0, x2:0, y2:1, "
       
  1489          "stop:0.5 green, stop:0.2 red)" << QPointF(0, 0) << QPointF(0, 1)
       
  1490          0 << 0.2 << QColor("red") << 0.5 << QColor("green");
       
  1491     */
       
  1492 }
       
  1493 
       
  1494 void tst_QCssParser::gradient()
       
  1495 {
       
  1496     QFETCH(QString, css);
       
  1497     QFETCH(QString, type);
       
  1498     QFETCH(QPointF, finalStop);
       
  1499     QFETCH(QPointF, start);
       
  1500     QFETCH(int, spread);
       
  1501     QFETCH(qreal, stop0); QFETCH(QColor, color0);
       
  1502     QFETCH(qreal, stop1); QFETCH(QColor, color1);
       
  1503 
       
  1504     QDomDocument doc;
       
  1505     QVERIFY(doc.setContent(QLatin1String("<!DOCTYPE test><test> <dummy/> </test>")));
       
  1506 
       
  1507     css.prepend("dummy {");
       
  1508     css.append("}");
       
  1509 
       
  1510     QCss::Parser parser(css);
       
  1511     QCss::StyleSheet sheet;
       
  1512     QVERIFY(parser.parse(&sheet));
       
  1513 
       
  1514     DomStyleSelector testSelector(doc, sheet);
       
  1515     QDomElement e = doc.documentElement().firstChildElement();
       
  1516     QCss::StyleSelector::NodePtr n;
       
  1517     n.ptr = &e;
       
  1518     QVector<QCss::StyleRule> rules = testSelector.styleRulesForNode(n);
       
  1519     QVector<QCss::Declaration> decls = rules.at(0).declarations;
       
  1520     QCss::ValueExtractor ve(decls);
       
  1521     QBrush fg, sfg;
       
  1522     QBrush sbg, abg;
       
  1523     QVERIFY(ve.extractPalette(&fg, &sfg, &sbg, &abg));
       
  1524     if (type == "linear") {
       
  1525         QVERIFY(sbg.style() == Qt::LinearGradientPattern);
       
  1526         const QLinearGradient *lg = static_cast<const QLinearGradient *>(sbg.gradient());
       
  1527         QCOMPARE(lg->start(), start);
       
  1528         QCOMPARE(lg->finalStop(), finalStop);
       
  1529     } else if (type == "conical") {
       
  1530         QVERIFY(sbg.style() == Qt::ConicalGradientPattern);
       
  1531         const QConicalGradient *cg = static_cast<const QConicalGradient *>(sbg.gradient());
       
  1532         QCOMPARE(cg->center(), start);
       
  1533     }
       
  1534     const QGradient *g = sbg.gradient();
       
  1535     QCOMPARE(g->spread(), QGradient::Spread(spread));
       
  1536     QVERIFY(g->stops().at(0).first == stop0);
       
  1537     QVERIFY(g->stops().at(0).second == color0);
       
  1538     QVERIFY(g->stops().at(1).first == stop1);
       
  1539     QVERIFY(g->stops().at(1).second == color1);
       
  1540 }
       
  1541 
       
  1542 void tst_QCssParser::extractFontFamily_data()
       
  1543 {
       
  1544     if (QFontInfo(QFont("Times New Roman")).family() != "Times New Roman")
       
  1545         QSKIP("'Times New Roman' font not found ", SkipAll);
       
  1546 
       
  1547     QTest::addColumn<QString>("css");
       
  1548     QTest::addColumn<QString>("expectedFamily");
       
  1549 
       
  1550     QTest::newRow("quoted-family-name") << "font-family: 'Times New Roman'" << QString("Times New Roman");
       
  1551     QTest::newRow("unquoted-family-name") << "font-family: Times New Roman" << QString("Times New Roman");
       
  1552     QTest::newRow("unquoted-family-name2") << "font-family: Times        New     Roman" << QString("Times New Roman");
       
  1553     QTest::newRow("multiple") << "font-family: Times New Roman  , foobar, 'baz'" << QString("Times New Roman");
       
  1554     QTest::newRow("multiple2") << "font-family: invalid,  Times New   Roman " << QString("Times New Roman");
       
  1555     QTest::newRow("invalid") << "font-family: invalid" << QFontInfo(QFont("invalid font")).family();
       
  1556     QTest::newRow("shorthand") << "font: 12pt Times New Roman" << QString("Times New Roman");
       
  1557     QTest::newRow("shorthand multiple quote") << "font: 12pt invalid, \"Times New Roman\" " << QString("Times New Roman");
       
  1558     QTest::newRow("shorthand multiple") << "font: 12pt invalid, Times New Roman " << QString("Times New Roman");
       
  1559 }
       
  1560 
       
  1561 void tst_QCssParser::extractFontFamily()
       
  1562 {
       
  1563     QFETCH(QString, css);
       
  1564     css.prepend("dummy {");
       
  1565     css.append("}");
       
  1566 
       
  1567     QCss::Parser parser(css);
       
  1568     QCss::StyleSheet sheet;
       
  1569     QVERIFY(parser.parse(&sheet));
       
  1570 
       
  1571     QCOMPARE(sheet.styleRules.count() + sheet.nameIndex.count(), 1);
       
  1572     QCss::StyleRule rule =  (!sheet.styleRules.isEmpty()) ?
       
  1573             sheet.styleRules.at(0) : *sheet.nameIndex.begin();
       
  1574 
       
  1575     const QVector<QCss::Declaration> decls = rule.declarations;
       
  1576     QVERIFY(!decls.isEmpty());
       
  1577     QCss::ValueExtractor extractor(decls);
       
  1578 
       
  1579     int adjustment = 0;
       
  1580     QFont fnt;
       
  1581     extractor.extractFont(&fnt, &adjustment);
       
  1582     QFontInfo info(fnt);
       
  1583     QTEST(info.family(), "expectedFamily");
       
  1584 }
       
  1585 
       
  1586 void tst_QCssParser::extractBorder_data()
       
  1587 {
       
  1588     QTest::addColumn<QString>("css");
       
  1589     QTest::addColumn<int>("expectedTopWidth");
       
  1590     QTest::addColumn<int>("expectedTopStyle");
       
  1591     QTest::addColumn<QColor>("expectedTopColor");
       
  1592 
       
  1593     QTest::newRow("all values") << "border: 2px solid green" << 2 << (int)QCss::BorderStyle_Solid << QColor("green");
       
  1594     QTest::newRow("palette") << "border: 2px solid palette(highlight)" << 2 << (int)QCss::BorderStyle_Solid << qApp->palette().color(QPalette::Highlight);
       
  1595     QTest::newRow("just width") << "border: 2px" << 2 << (int)QCss::BorderStyle_None << QColor();
       
  1596     QTest::newRow("just style") << "border: solid" << 0 << (int)QCss::BorderStyle_Solid << QColor();
       
  1597     QTest::newRow("just color") << "border: green" << 0 << (int)QCss::BorderStyle_None << QColor("green");
       
  1598     QTest::newRow("width+style") << "border: 2px solid" << 2 << (int)QCss::BorderStyle_Solid << QColor();
       
  1599     QTest::newRow("style+color") << "border: solid green" << 0 << (int)QCss::BorderStyle_Solid << QColor("green");
       
  1600     QTest::newRow("width+color") << "border: 3px green" << 3 << (int)QCss::BorderStyle_None << QColor("green");
       
  1601     QTest::newRow("groove style") << "border: groove" << 0 << (int)QCss::BorderStyle_Groove << QColor();
       
  1602     QTest::newRow("ridge style") << "border: ridge" << 0 << (int)QCss::BorderStyle_Ridge << QColor();
       
  1603     QTest::newRow("double style") << "border: double" << 0 << (int)QCss::BorderStyle_Double << QColor();
       
  1604     QTest::newRow("inset style") << "border: inset" << 0 << (int)QCss::BorderStyle_Inset << QColor();
       
  1605     QTest::newRow("outset style") << "border: outset" << 0 << (int)QCss::BorderStyle_Outset << QColor();
       
  1606     QTest::newRow("dashed style") << "border: dashed" << 0 << (int)QCss::BorderStyle_Dashed << QColor();
       
  1607     QTest::newRow("dotted style") << "border: dotted" << 0 << (int)QCss::BorderStyle_Dotted << QColor();
       
  1608     QTest::newRow("dot-dash style") << "border: dot-dash" << 0 << (int)QCss::BorderStyle_DotDash << QColor();
       
  1609     QTest::newRow("dot-dot-dash style") << "border: dot-dot-dash" << 0 << (int)QCss::BorderStyle_DotDotDash << QColor();
       
  1610 
       
  1611     QTest::newRow("top-width+color") << "border-top: 3px green" << 3 << (int)QCss::BorderStyle_None << QColor("green");
       
  1612 }
       
  1613 
       
  1614 void tst_QCssParser::extractBorder()
       
  1615 {
       
  1616     QFETCH(QString, css);
       
  1617     QFETCH(int, expectedTopWidth);
       
  1618     QFETCH(int, expectedTopStyle);
       
  1619     QFETCH(QColor, expectedTopColor);
       
  1620 
       
  1621     css.prepend("dummy {");
       
  1622     css.append("}");
       
  1623 
       
  1624     QCss::Parser parser(css);
       
  1625     QCss::StyleSheet sheet;
       
  1626     QVERIFY(parser.parse(&sheet));
       
  1627 
       
  1628     QCOMPARE(sheet.styleRules.count() + sheet.nameIndex.count(), 1);
       
  1629     QCss::StyleRule rule =  (!sheet.styleRules.isEmpty()) ?
       
  1630             sheet.styleRules.at(0) : *sheet.nameIndex.begin();
       
  1631     const QVector<QCss::Declaration> decls = rule.declarations;
       
  1632     QVERIFY(!decls.isEmpty());
       
  1633     QCss::ValueExtractor extractor(decls);
       
  1634 
       
  1635     int widths[4];
       
  1636     QBrush colors[4];
       
  1637     QCss::BorderStyle styles[4];
       
  1638     QSize radii[4];
       
  1639 
       
  1640     extractor.extractBorder(widths, colors, styles, radii);
       
  1641     QVERIFY(widths[QCss::TopEdge] == expectedTopWidth);
       
  1642     QVERIFY(styles[QCss::TopEdge] == expectedTopStyle);
       
  1643     QVERIFY(colors[QCss::TopEdge] == expectedTopColor);
       
  1644 }
       
  1645 
       
  1646 void tst_QCssParser::noTextDecoration()
       
  1647 {
       
  1648     QCss::Parser parser("dummy { text-decoration: none; }");
       
  1649     QCss::StyleSheet sheet;
       
  1650     QVERIFY(parser.parse(&sheet));
       
  1651 
       
  1652     QCOMPARE(sheet.styleRules.count() + sheet.nameIndex.count(), 1);
       
  1653     QCss::StyleRule rule =  (!sheet.styleRules.isEmpty()) ?
       
  1654             sheet.styleRules.at(0) : *sheet.nameIndex.begin();
       
  1655     const QVector<QCss::Declaration> decls = rule.declarations;
       
  1656     QVERIFY(!decls.isEmpty());
       
  1657     QCss::ValueExtractor extractor(decls);
       
  1658 
       
  1659     int adjustment = 0;
       
  1660     QFont f;
       
  1661     f.setUnderline(true);
       
  1662     f.setOverline(true);
       
  1663     f.setStrikeOut(true);
       
  1664     QVERIFY(extractor.extractFont(&f, &adjustment));
       
  1665 
       
  1666     QVERIFY(!f.underline());
       
  1667     QVERIFY(!f.overline());
       
  1668     QVERIFY(!f.strikeOut());
       
  1669 }
       
  1670 
       
  1671 void tst_QCssParser::quotedAndUnquotedIdentifiers()
       
  1672 {
       
  1673     QCss::Parser parser("foo { font-style: \"italic\"; font-weight: bold }");
       
  1674     QCss::StyleSheet sheet;
       
  1675     QVERIFY(parser.parse(&sheet));
       
  1676 
       
  1677     QCOMPARE(sheet.styleRules.count() + sheet.nameIndex.count(), 1);
       
  1678     QCss::StyleRule rule = (!sheet.styleRules.isEmpty()) ?
       
  1679            sheet.styleRules.at(0) : *sheet.nameIndex.begin();
       
  1680     const QVector<QCss::Declaration> decls = rule.declarations;
       
  1681     QCOMPARE(decls.size(), 2);
       
  1682 
       
  1683     QCOMPARE(decls.at(0).d->values.first().type, QCss::Value::String);
       
  1684     QCOMPARE(decls.at(0).d->property, QLatin1String("font-style"));
       
  1685     QCOMPARE(decls.at(0).d->values.first().toString(), QLatin1String("italic"));
       
  1686 
       
  1687     QCOMPARE(decls.at(1).d->values.first().type, QCss::Value::KnownIdentifier);
       
  1688     QCOMPARE(decls.at(1).d->property, QLatin1String("font-weight"));
       
  1689     QCOMPARE(decls.at(1).d->values.first().toString(), QLatin1String("bold"));
       
  1690 }
       
  1691 
       
  1692 QTEST_MAIN(tst_QCssParser)
       
  1693 #include "tst_qcssparser.moc"
       
  1694