tests/auto/qsimplexmlnodemodel/tst_qsimplexmlnodemodel.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
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 
       
    42 
       
    43 #include <QtTest/QtTest>
       
    44 
       
    45 #ifdef QTEST_XMLPATTERNS
       
    46 
       
    47 #include <QSimpleXmlNodeModel>
       
    48 #include <QXmlNamePool>
       
    49 #include <QXmlQuery>
       
    50 #include <QXmlSerializer>
       
    51 #include <QXmlStreamReader>
       
    52 
       
    53 #include "TestSimpleNodeModel.h"
       
    54 
       
    55 /*!
       
    56  \class tst_QSimpleXmlNodeModel
       
    57  \internal
       
    58  \since 4.4
       
    59  \brief Tests class QSimpleXmlNodeModel.
       
    60  */
       
    61 class tst_QSimpleXmlNodeModel : public QObject
       
    62 {
       
    63     Q_OBJECT
       
    64 
       
    65 private Q_SLOTS:
       
    66     void namePool() const;
       
    67     void namePoolIsReference() const;
       
    68     void defaultConstructor() const;
       
    69     void objectSize() const;
       
    70     void constCorrectness() const;
       
    71     void stringValue() const;
       
    72 };
       
    73 
       
    74 void tst_QSimpleXmlNodeModel::namePool() const
       
    75 {
       
    76     /* Check that the name pool we pass in, is what actually is returned. */
       
    77     QXmlNamePool np;
       
    78     const QXmlName name(np, QLatin1String("localName"),
       
    79                             QLatin1String("http://example.com/XYZ"),
       
    80                             QLatin1String("prefix432"));
       
    81     TestSimpleNodeModel model(np);
       
    82     const QXmlNamePool np2(model.namePool());
       
    83 
       
    84     /* If it's a bug, this will more or less crash. */
       
    85     QCOMPARE(name.namespaceUri(np2), QString::fromLatin1("http://example.com/XYZ"));
       
    86     QCOMPARE(name.localName(np2), QString::fromLatin1("localName"));
       
    87     QCOMPARE(name.prefix(np2), QString::fromLatin1("prefix432"));
       
    88 }
       
    89 
       
    90 void tst_QSimpleXmlNodeModel::namePoolIsReference() const
       
    91 {
       
    92     /* Test is placed in TestSimpleNodeModel.h:~50. */
       
    93 }
       
    94 
       
    95 void tst_QSimpleXmlNodeModel::defaultConstructor() const
       
    96 {
       
    97     QXmlNamePool np;
       
    98 }
       
    99 
       
   100 void tst_QSimpleXmlNodeModel::objectSize() const
       
   101 {
       
   102     /* We shouldn't have added any members. */
       
   103     QCOMPARE(sizeof(QSimpleXmlNodeModel), sizeof(QAbstractXmlNodeModel));
       
   104 }
       
   105 
       
   106 void tst_QSimpleXmlNodeModel::constCorrectness() const
       
   107 {
       
   108     QXmlNamePool np;
       
   109     const TestSimpleNodeModel instance(np);
       
   110 
       
   111     instance.nextFromSimpleAxis(QSimpleXmlNodeModel::Parent, QXmlNodeModelIndex());
       
   112     instance.attributes(QXmlNodeModelIndex());
       
   113 
       
   114     instance.namePool();
       
   115 }
       
   116 
       
   117 /*!
       
   118  Verify that if QAbstractXmlNodeModel::typedValue() return a null
       
   119  QVariant, it is treated as that the node has no typed value.
       
   120 
       
   121  This verifies the default implementation of QSimpleXmlNodeModel::stringValue().
       
   122  */
       
   123 void tst_QSimpleXmlNodeModel::stringValue() const
       
   124 {
       
   125     class TypedModel : public TestSimpleNodeModel
       
   126     {
       
   127     public:
       
   128         TypedModel(const QXmlNamePool &np) : TestSimpleNodeModel(np)
       
   129         {
       
   130         }
       
   131 
       
   132         virtual QXmlNodeModelIndex::NodeKind kind(const QXmlNodeModelIndex &) const
       
   133         {
       
   134             return QXmlNodeModelIndex::Element;
       
   135         }
       
   136 
       
   137         virtual QVariant typedValue(const QXmlNodeModelIndex &) const
       
   138         {
       
   139             return QVariant();
       
   140         }
       
   141 
       
   142         QXmlNodeModelIndex root() const
       
   143         {
       
   144             return createIndex(qint64(1));
       
   145         }
       
   146     };
       
   147 
       
   148     QXmlNamePool np;
       
   149     TypedModel model(np);
       
   150 
       
   151     QXmlQuery query(np);
       
   152     query.bindVariable(QLatin1String("node"), model.root());
       
   153     query.setQuery(QLatin1String("declare variable $node external;"
       
   154                                  "string($node), data($node)"));
       
   155 
       
   156     QByteArray output;
       
   157     QBuffer buffer(&output);
       
   158     QVERIFY(buffer.open(QIODevice::WriteOnly));
       
   159     QVERIFY(query.isValid());
       
   160 
       
   161     QXmlSerializer serializer(query, &buffer);
       
   162     QVERIFY(query.evaluateTo(&serializer));
       
   163 
       
   164     QVERIFY(output.isEmpty());
       
   165 }
       
   166 
       
   167 QTEST_MAIN(tst_QSimpleXmlNodeModel)
       
   168 
       
   169 #include "tst_qsimplexmlnodemodel.moc"
       
   170 #else //QTEST_XMLPATTERNS
       
   171 QTEST_NOOP_MAIN
       
   172 #endif