Add testcase for Context::GetCallingQmlGlobal function
authorPeter Varga <pvarga@inf.u-szeged.hu>
Wed, 29 Feb 2012 14:19:18 +0000 (15:19 +0100)
committerQt by Nokia <qt-info@nokia.com>
Sat, 3 Mar 2012 16:08:53 +0000 (17:08 +0100)
Change-Id: I509b168b6923e1a9e92c6ab3505d51ea607e979c
Reviewed-by: Simon Hausmann <simon.hausmann@nokia.com>
tests/auto/v8/tst_v8.cpp
tests/auto/v8/v8test.cpp
tests/auto/v8/v8test.h

index ce87bac..1a309f7 100644 (file)
@@ -59,6 +59,7 @@ private slots:
     void userobjectcompare();
     void externalteardown();
     void globalcall();
+    void getcallingqmlglobal();
 };
 
 void tst_v8::eval()
@@ -86,6 +87,11 @@ void tst_v8::globalcall()
     QVERIFY(v8test_globalcall());
 }
 
+void tst_v8::getcallingqmlglobal()
+{
+    QVERIFY(v8test_getcallingqmlglobal());
+}
+
 int main(int argc, char *argv[])
 {
     V8::SetFlagsFromCommandLine(&argc, argv, true);
index 63f5956..db03883 100644 (file)
@@ -377,3 +377,60 @@ cleanup:
 
     ENDTEST();
 }
+
+#define VARNAME "tipli"
+#define VARVALUE 28
+
+Handle<Value> CheckQMLGlobal(const Arguments&)
+{
+  HandleScope handle_scope;
+
+  Local<String> key = String::New(VARNAME);
+  Local<Object> qmlglobal = Context::GetCallingQmlGlobal();
+
+  if (qmlglobal.IsEmpty()) return Integer::New(0);
+
+  int hash = qmlglobal->GetIdentityHash();
+  int value = qmlglobal->Get(key)->Int32Value();
+
+  return Integer::New(hash + value);
+}
+
+bool v8test_getcallingqmlglobal()
+{
+    BEGINTEST();
+
+    HandleScope handle_scope;
+
+    Local<ObjectTemplate> global = ObjectTemplate::New();
+    global->Set(String::New("checkQMLGlobal"), FunctionTemplate::New(CheckQMLGlobal));
+
+    Persistent<Context> context = Context::New(NULL, global);
+    Context::Scope global_scope(context);
+
+    Local<String> key = String::New(VARNAME);
+    Local<Object> qmlglobal = Object::New();
+
+    qmlglobal->Set(key, Integer::New(VARVALUE));
+    int hash1 = qmlglobal->GetIdentityHash();
+
+    Local<String> source = String::New("(function test() { return checkQMLGlobal(); })");
+    Local<Script> script = Script::Compile(source, NULL, NULL, v8::Handle<v8::String>(), v8::Script::QmlMode);
+    Local<Value> result = script->Run(qmlglobal);
+
+    Local<Function> v8function = Local<Function>::Cast(result);
+    int hash2 = v8function->Call(v8function, 0, 0)->Int32Value();
+    VERIFY(hash2);
+    VERIFY(hash1 == (hash2 - VARVALUE));
+
+    qmlglobal = Context::GetCallingQmlGlobal();
+    VERIFY(qmlglobal.IsEmpty());
+
+cleanup:
+    context.Dispose();
+
+    ENDTEST();
+}
+
+#undef VARNAME
+#undef VARVALUE
index 02cec34..b1989f9 100644 (file)
@@ -53,6 +53,7 @@ bool v8test_evalwithinwith();
 bool v8test_userobjectcompare();
 bool v8test_externalteardown();
 bool v8test_globalcall();
+bool v8test_getcallingqmlglobal();
 
 #endif // V8TEST_H