Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / tests / auto / declarative / qdeclarativeecmascript / testtypes.h
index 8c7b838..d413209 100644 (file)
@@ -1,34 +1,34 @@
 /****************************************************************************
 **
-** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
-** All rights reserved.
-** Contact: Nokia Corporation (qt-info@nokia.com)
+** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
+** Contact: http://www.qt-project.org/
 **
 ** This file is part of the test suite of the Qt Toolkit.
 **
 ** $QT_BEGIN_LICENSE:LGPL$
-** No Commercial Usage
-** This file contains pre-release code and may not be distributed.
-** You may use this file in accordance with the terms and conditions
-** contained in the Technology Preview License Agreement accompanying
-** this package.
-**
 ** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 2.1 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL included in the
-** packaging of this file.  Please review the following information to
-** ensure the GNU Lesser General Public License version 2.1 requirements
-** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+** This file may be used under the terms of the GNU Lesser General Public
+** License version 2.1 as published by the Free Software Foundation and
+** appearing in the file LICENSE.LGPL included in the packaging of this
+** file. Please review the following information to ensure the GNU Lesser
+** General Public License version 2.1 requirements will be met:
+** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 **
 ** In addition, as a special exception, Nokia gives you certain additional
-** rights.  These rights are described in the Nokia Qt LGPL Exception
+** rights. These rights are described in the Nokia Qt LGPL Exception
 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
 **
-** If you have questions regarding the use of this file, please contact
-** Nokia at qt-info@nokia.com.
-**
+** GNU General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU General
+** Public License version 3.0 as published by the Free Software Foundation
+** and appearing in the file LICENSE.GPL included in the packaging of this
+** file. Please review the following information to ensure the GNU General
+** Public License version 3.0 requirements will be met:
+** http://www.gnu.org/copyleft/gpl.html.
 **
+** Other Usage
+** Alternatively, this file may be used in accordance with the terms and
+** conditions contained in a signed written agreement between you and Nokia.
 **
 **
 **
 #include <QtGui/qvector3d.h>
 #include <QtGui/QPixmap>
 #include <QtCore/qdatetime.h>
-#include <QtScript/qscriptvalue.h>
+#include <QtDeclarative/qjsvalue.h>
 #include <QtDeclarative/qdeclarativescriptstring.h>
 #include <QtDeclarative/qdeclarativecomponent.h>
 
+#include <private/qdeclarativeengine_p.h>
+#include <private/qv8engine_p.h>
+
 class MyQmlAttachedObject : public QObject
 {
     Q_OBJECT
@@ -90,14 +93,16 @@ class MyQmlObject : public QObject
     Q_PROPERTY(int value READ value WRITE setValue)
     Q_PROPERTY(int console READ console CONSTANT)
     Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty NOTIFY stringChanged)
+    Q_PROPERTY(QUrl urlProperty READ urlProperty WRITE setUrlProperty NOTIFY urlChanged)
     Q_PROPERTY(QObject *objectProperty READ objectProperty WRITE setObjectProperty NOTIFY objectChanged)
     Q_PROPERTY(QDeclarativeListProperty<QObject> objectListProperty READ objectListProperty CONSTANT)
     Q_PROPERTY(int resettableProperty READ resettableProperty WRITE setResettableProperty RESET resetProperty)
     Q_PROPERTY(QRegExp regExp READ regExp WRITE setRegExp)
     Q_PROPERTY(int nonscriptable READ nonscriptable WRITE setNonscriptable SCRIPTABLE false)
+    Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty NOTIFY intChanged)
 
 public:
-    MyQmlObject(): myinvokableObject(0), m_methodCalled(false), m_methodIntCalled(false), m_object(0), m_value(0), m_resetProperty(13) {}
+    MyQmlObject(): myinvokableObject(0), m_methodCalled(false), m_methodIntCalled(false), m_object(0), m_value(0), m_resetProperty(13), m_intProperty(0) {}
 
     enum MyEnum { EnumValue1 = 0, EnumValue2 = 1 };
     enum MyEnum2 { EnumValue3 = 2, EnumValue4 = 3 };
@@ -114,6 +119,15 @@ public:
         emit stringChanged();
     }
 
+    QUrl urlProperty() const { return m_url; }
+    void setUrlProperty(const QUrl &url)
+    {
+        if (url == m_url)
+            return;
+        m_url = url;
+        emit urlChanged();
+    }
+
     QObject *objectProperty() const { return m_object; }
     void setObjectProperty(QObject *obj) { 
         if (obj == m_object)
@@ -158,15 +172,23 @@ public:
         int value;
     };
     QVariant variant() const { return m_variant; }
+    QJSValue qjsvalue() const { return m_qjsvalue; }
+
+    int intProperty() const { return m_intProperty; }
+    void setIntProperty(int i) { m_intProperty = i; emit intChanged(); }
     
 signals:
     void basicSignal();
     void argumentSignal(int a, QString b, qreal c, MyEnum2 d, Qt::MouseButtons e);
     void stringChanged();
+    void urlChanged();
     void objectChanged();
     void anotherBasicSignal();
     void thirdBasicSignal();
     void signalWithUnknownType(const MyQmlObject::MyType &arg);
+    void signalWithVariant(const QVariant &arg);
+    void signalWithQJSValue(const QJSValue &arg);
+    void intChanged();
 
 public slots:
     void deleteMe() { delete this; }
@@ -175,6 +197,8 @@ public slots:
     void setString(const QString &s) { m_string = s; }
     void myinvokable(MyQmlObject *o) { myinvokableObject = o; }
     void variantMethod(const QVariant &v) { m_variant = v; }
+    void qjsvalueMethod(const QJSValue &v) { m_qjsvalue = v; }
+    void v8function(QDeclarativeV8Function*);
 
 private:
     friend class tst_qdeclarativeecmascript;
@@ -183,11 +207,14 @@ private:
 
     QObject *m_object;
     QString m_string;
+    QUrl m_url;
     QList<QObject *> m_objectQList;
     int m_value;
     int m_resetProperty;
     QRegExp m_regExp;
     QVariant m_variant;
+    QJSValue m_qjsvalue;
+    int m_intProperty;
 };
 
 QML_DECLARE_TYPEINFO(MyQmlObject, QML_HAS_ATTACHED_PROPERTIES)
@@ -581,7 +608,7 @@ public:
     }
 };
 
-Q_DECLARE_METATYPE(QScriptValue);
+Q_DECLARE_METATYPE(QJSValue);
 class MyInvokableBaseObject : public QObject
 {
     Q_OBJECT
@@ -614,7 +641,7 @@ public:
     Q_INVOKABLE QPointF method_NoArgs_QPointF() { invoke(3); return QPointF(123, 4.5); }
     Q_INVOKABLE QObject *method_NoArgs_QObject() { invoke(4); return this; }
     Q_INVOKABLE MyInvokableObject *method_NoArgs_unknown() { invoke(5); return this; }
-    Q_INVOKABLE QScriptValue method_NoArgs_QScriptValue() { invoke(6); return QScriptValue("Hello world"); }
+    Q_INVOKABLE QJSValue method_NoArgs_QScriptValue() { invoke(6); return QJSValue("Hello world"); }
     Q_INVOKABLE QVariant method_NoArgs_QVariant() { invoke(7); return QVariant("QML rocks"); }
 
     Q_INVOKABLE void method_int(int a) { invoke(8); m_actuals << a; }
@@ -623,8 +650,8 @@ public:
     Q_INVOKABLE void method_QString(QString a) { invoke(11); m_actuals << a; }
     Q_INVOKABLE void method_QPointF(QPointF a) { invoke(12); m_actuals << a; }
     Q_INVOKABLE void method_QObject(QObject *a) { invoke(13); m_actuals << qVariantFromValue(a); }
-    Q_INVOKABLE void method_QScriptValue(QScriptValue a) { invoke(14); m_actuals << qVariantFromValue(a); }
-    Q_INVOKABLE void method_intQScriptValue(int a, QScriptValue b) { invoke(15); m_actuals << a << qVariantFromValue(b); }
+    Q_INVOKABLE void method_QScriptValue(QJSValue a) { invoke(14); m_actuals << qVariantFromValue(a); }
+    Q_INVOKABLE void method_intQScriptValue(int a, QJSValue b) { invoke(15); m_actuals << a << qVariantFromValue(b); }
     
     Q_INVOKABLE void method_overload(int a) { invoke(16); m_actuals << a; }
     Q_INVOKABLE void method_overload(int a, int b) { invoke(17); m_actuals << a << b; }
@@ -924,16 +951,29 @@ public:
 
     bool scarceResourceIsDetached() const { return m_value.isDetached(); }
 
+    // this particular one returns a new one each time
+    // this means that every Scarce Resource Copy will
+    // consume resources (so that we can track disposal
+    // of v8 handles with circular references).
+    Q_INVOKABLE QPixmap newScarceResource() const
+    {
+        QPixmap retn(800, 600);
+        retn.fill(QColor(100, 110, 120, 45));
+        return retn;
+    }
+
 signals:
     void scarceResourceChanged();
 
 private:
     QPixmap m_value;
 };
+QML_DECLARE_TYPE(ScarceResourceObject)
 
 class testQObjectApi : public QObject
 {
     Q_OBJECT
+    Q_ENUMS(MyEnum)
     Q_PROPERTY (int qobjectTestProperty READ qobjectTestProperty NOTIFY qobjectTestPropertyChanged)
     Q_PROPERTY (int qobjectTestWritableProperty READ qobjectTestWritableProperty WRITE setQObjectTestWritableProperty NOTIFY qobjectTestWritablePropertyChanged)
 
@@ -945,7 +985,9 @@ public:
 
     ~testQObjectApi() {}
 
-    Q_INVOKABLE int qobjectTestMethod() { m_methodCallCount += 1; return m_methodCallCount; }
+    enum MyEnum { EnumValue1 = 25, EnumValue2 = 42 };
+    Q_INVOKABLE int qobjectEnumTestMethod(MyEnum val) { return (static_cast<int>(val) + 5); }
+    Q_INVOKABLE int qobjectTestMethod(int increment = 1) { m_methodCallCount += increment; return m_methodCallCount; }
 
     int qobjectTestProperty() const { return m_testProperty; }
     void setQObjectTestProperty(int tp) { m_testProperty = tp; emit qobjectTestPropertyChanged(tp); }
@@ -963,6 +1005,287 @@ private:
     int m_methodCallCount;
 };
 
+class CircularReferenceObject : public QObject,
+                                public QV8GCCallback::Node
+{
+    Q_OBJECT
+
+public:
+    CircularReferenceObject(QObject *parent = 0)
+        : QObject(parent), QV8GCCallback::Node(callback), m_referenced(0), m_dtorCount(0)
+    {
+        QV8GCCallback::addGcCallbackNode(this);
+    }
+
+    ~CircularReferenceObject()
+    {
+        if (m_dtorCount) *m_dtorCount = *m_dtorCount + 1;
+    }
+
+    Q_INVOKABLE void setDtorCount(int *dtorCount)
+    {
+        m_dtorCount = dtorCount;
+    }
+
+    Q_INVOKABLE CircularReferenceObject *generate(QObject *parent = 0)
+    {
+        CircularReferenceObject *retn = new CircularReferenceObject(parent);
+        retn->m_dtorCount = m_dtorCount;
+        retn->m_engine = m_engine;
+        return retn;
+    }
+
+    Q_INVOKABLE void addReference(QObject *other)
+    {
+        m_referenced = other;
+    }
+
+    static void callback(QV8GCCallback::Node *n)
+    {
+        CircularReferenceObject *cro = static_cast<CircularReferenceObject*>(n);
+        if (cro->m_referenced) {
+            cro->m_engine->addRelationshipForGC(cro, cro->m_referenced);
+        }
+    }
+
+    void setEngine(QDeclarativeEngine* declarativeEngine)
+    {
+        m_engine = QDeclarativeEnginePrivate::get(declarativeEngine)->v8engine();
+    }
+
+private:
+    QObject *m_referenced;
+    int *m_dtorCount;
+    QV8Engine* m_engine;
+};
+Q_DECLARE_METATYPE(CircularReferenceObject*)
+
+class CircularReferenceHandle : public QObject,
+                                public QV8GCCallback::Node
+{
+    Q_OBJECT
+
+public:
+    CircularReferenceHandle(QObject *parent = 0)
+        : QObject(parent), QV8GCCallback::Node(gccallback), m_dtorCount(0), m_engine(0)
+    {
+        QV8GCCallback::addGcCallbackNode(this);
+    }
+
+    ~CircularReferenceHandle()
+    {
+        if (m_dtorCount) *m_dtorCount = *m_dtorCount + 1;
+    }
+
+    Q_INVOKABLE void setDtorCount(int *dtorCount)
+    {
+        m_dtorCount = dtorCount;
+    }
+
+    Q_INVOKABLE CircularReferenceHandle *generate(QObject *parent = 0)
+    {
+        CircularReferenceHandle *retn = new CircularReferenceHandle(parent);
+        retn->m_dtorCount = m_dtorCount;
+        retn->m_engine = m_engine;
+        return retn;
+    }
+
+    Q_INVOKABLE void addReference(v8::Persistent<v8::Value> handle)
+    {
+        m_referenced = qPersistentNew(handle);
+        m_referenced.MakeWeak(static_cast<void*>(this), wrcallback);
+    }
+
+    static void wrcallback(v8::Persistent<v8::Value> handle, void *params)
+    {
+        CircularReferenceHandle *crh = static_cast<CircularReferenceHandle*>(params);
+        qPersistentDispose(handle);
+        crh->m_referenced.Clear();
+    }
+
+    static void gccallback(QV8GCCallback::Node *n)
+    {
+        CircularReferenceHandle *crh = static_cast<CircularReferenceHandle*>(n);
+        crh->m_engine->addRelationshipForGC(crh, crh->m_referenced);
+    }
+
+    void setEngine(QDeclarativeEngine* declarativeEngine)
+    {
+        m_engine = QDeclarativeEnginePrivate::get(declarativeEngine)->v8engine();
+    }
+
+private:
+    v8::Persistent<v8::Value> m_referenced;
+    int *m_dtorCount;
+    QV8Engine* m_engine;
+};
+Q_DECLARE_METATYPE(CircularReferenceHandle*)
+
+class MyDynamicCreationDestructionObject : public QObject
+{
+    Q_OBJECT
+    Q_PROPERTY (int intProperty READ intProperty WRITE setIntProperty NOTIFY intPropertyChanged)
+
+public:
+    MyDynamicCreationDestructionObject(QObject *parent = 0) : QObject(parent), m_intProperty(0), m_dtorCount(0)
+    {
+    }
+
+    ~MyDynamicCreationDestructionObject()
+    {
+        if (m_dtorCount) {
+            (*m_dtorCount)++;
+        }
+    }
+
+    int intProperty() const { return m_intProperty; }
+    void setIntProperty(int val) { m_intProperty = val; emit intPropertyChanged(); }
+
+    Q_INVOKABLE MyDynamicCreationDestructionObject *createNew()
+    {
+        // no parent == ownership transfers to JS; same dtor counter.
+        MyDynamicCreationDestructionObject *retn = new MyDynamicCreationDestructionObject;
+        retn->setDtorCount(m_dtorCount);
+        return retn;
+    }
+
+    void setDtorCount(int *dtorCount)
+    {
+        m_dtorCount = dtorCount;
+    }
+
+signals:
+    void intPropertyChanged();
+
+private:
+    int m_intProperty;
+    int *m_dtorCount;
+};
+
+class WriteCounter : public QObject
+{
+    Q_OBJECT
+    Q_PROPERTY(int value READ value WRITE setValue);
+public:
+    WriteCounter() : m_value(0), m_count(0) {}
+
+    int value() const { return m_value; }
+    void setValue(int v) { m_value = v; ++m_count; }
+
+    int count() const { return m_count; }
+
+private:
+    int m_value;
+    int m_count;
+};
+
+class MySequenceConversionObject : public QObject
+{
+    Q_OBJECT
+
+    Q_PROPERTY (QList<int> intListProperty READ intListProperty WRITE setIntListProperty NOTIFY intListPropertyChanged)
+    Q_PROPERTY (QList<int> intListProperty2 READ intListProperty2 WRITE setIntListProperty2 NOTIFY intListProperty2Changed)
+    Q_PROPERTY (QList<qreal> qrealListProperty READ qrealListProperty WRITE setQrealListProperty NOTIFY qrealListPropertyChanged)
+    Q_PROPERTY (QList<bool> boolListProperty READ boolListProperty WRITE setBoolListProperty NOTIFY boolListPropertyChanged)
+    Q_PROPERTY (QList<QString> stringListProperty READ stringListProperty WRITE setStringListProperty NOTIFY stringListPropertyChanged)
+    Q_PROPERTY (QList<QUrl> urlListProperty READ urlListProperty WRITE setUrlListProperty NOTIFY urlListPropertyChanged)
+    Q_PROPERTY (QStringList qstringListProperty READ qstringListProperty WRITE setQStringListProperty NOTIFY qstringListPropertyChanged)
+
+    Q_PROPERTY (QList<QPoint> pointListProperty READ pointListProperty WRITE setPointListProperty NOTIFY pointListPropertyChanged)
+    Q_PROPERTY (QList<QVariant> variantListProperty READ variantListProperty WRITE setVariantListProperty NOTIFY variantListPropertyChanged)
+
+public:
+    MySequenceConversionObject()
+    {
+        m_intList << 1 << 2 << 3 << 4;
+        m_intList2 << 1 << 2 << 3 << 4;
+        m_qrealList << 1.1 << 2.2 << 3.3 << 4.4;
+        m_boolList << true << false << true << false;
+        m_stringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
+        m_urlList << QUrl("http://www.example1.com") << QUrl("http://www.example2.com") << QUrl("http://www.example3.com");
+        m_qstringList << QLatin1String("first") << QLatin1String("second") << QLatin1String("third") << QLatin1String("fourth");
+
+        m_pointList << QPoint(1, 2) << QPoint(3, 4) << QPoint(5, 6);
+        m_variantList << QVariant(QLatin1String("one")) << QVariant(true) << QVariant(3);
+    }
+
+    ~MySequenceConversionObject() {}
+
+    QList<int> intListProperty() const { return m_intList; }
+    void setIntListProperty(const QList<int> &list) { m_intList = list; emit intListPropertyChanged(); }
+    QList<int> intListProperty2() const { return m_intList2; }
+    void setIntListProperty2(const QList<int> &list) { m_intList2 = list; emit intListProperty2Changed(); }
+    QList<qreal> qrealListProperty() const { return m_qrealList; }
+    void setQrealListProperty(const QList<qreal> &list) { m_qrealList = list; emit qrealListPropertyChanged(); }
+    QList<bool> boolListProperty() const { return m_boolList; }
+    void setBoolListProperty(const QList<bool> &list) { m_boolList = list; emit boolListPropertyChanged(); }
+    QList<QString> stringListProperty() const { return m_stringList; }
+    void setStringListProperty(const QList<QString> &list) { m_stringList = list; emit stringListPropertyChanged(); }
+    QList<QUrl> urlListProperty() const { return m_urlList; }
+    void setUrlListProperty(const QList<QUrl> &list) { m_urlList = list; emit urlListPropertyChanged(); }
+    QStringList qstringListProperty() const { return m_qstringList; }
+    void setQStringListProperty(const QStringList &list) { m_qstringList = list; emit qstringListPropertyChanged(); }
+    QList<QPoint> pointListProperty() const { return m_pointList; }
+    void setPointListProperty(const QList<QPoint> &list) { m_pointList = list; emit pointListPropertyChanged(); }
+    QList<QVariant> variantListProperty() const { return m_variantList; }
+    void setVariantListProperty(const QList<QVariant> &list) { m_variantList = list; emit variantListPropertyChanged(); }
+
+    // now for "copy resource" sequences:
+    Q_INVOKABLE QList<int> generateIntSequence() const { QList<int> retn; retn << 1 << 2 << 3; return retn; }
+    Q_INVOKABLE QList<qreal> generateQrealSequence() const { QList<qreal> retn; retn << 1.1 << 2.2 << 3.3; return retn; }
+    Q_INVOKABLE QList<bool> generateBoolSequence() const { QList<bool> retn; retn << true << false << true; return retn; }
+    Q_INVOKABLE QList<QString> generateStringSequence() const { QList<QString> retn; retn << "one" << "two" << "three"; return retn; }
+    Q_INVOKABLE QList<QUrl> generateUrlSequence() const { QList<QUrl> retn; retn << QUrl("http://www.example1.com") << QUrl("http://www.example2.com") << QUrl("http://www.example3.com"); return retn; }
+    Q_INVOKABLE QStringList generateQStringSequence() const { QStringList retn; retn << "one" << "two" << "three"; return retn; }
+
+    // "reference resource" underlying qobject deletion test:
+    Q_INVOKABLE MySequenceConversionObject *generateTestObject() const { return new MySequenceConversionObject; }
+    Q_INVOKABLE void deleteTestObject(QObject *object) const { delete object; }
+
+signals:
+    void intListPropertyChanged();
+    void intListProperty2Changed();
+    void qrealListPropertyChanged();
+    void boolListPropertyChanged();
+    void stringListPropertyChanged();
+    void urlListPropertyChanged();
+    void qstringListPropertyChanged();
+    void pointListPropertyChanged();
+    void variantListPropertyChanged();
+
+private:
+    QList<int> m_intList;
+    QList<int> m_intList2;
+    QList<qreal> m_qrealList;
+    QList<bool> m_boolList;
+    QList<QString> m_stringList;
+    QList<QUrl> m_urlList;
+    QStringList m_qstringList;
+
+    QList<QPoint> m_pointList; // not a supported sequence type
+    QList<QVariant> m_variantList; // not a supported sequence type, but QVariantList support is hardcoded.
+};
+
+class MyDeleteObject : public QObject
+{
+    Q_OBJECT
+    Q_PROPERTY(QObject *nestedObject READ nestedObject NOTIFY nestedObjectChanged);
+    Q_PROPERTY(int deleteNestedObject READ deleteNestedObject NOTIFY deleteNestedObjectChanged);
+
+public:
+    MyDeleteObject() : m_nestedObject(new MyQmlObject) {}
+
+    QObject *nestedObject() const { return m_nestedObject; }
+    int deleteNestedObject() { delete m_nestedObject; m_nestedObject = 0; return 1; }
+
+signals:
+    void nestedObjectChanged();
+    void deleteNestedObjectChanged();
+
+private:
+    MyQmlObject *m_nestedObject;
+};
+
 void registerTypes();
 
 #endif // TESTTYPES_H