Fix locale test on Linux and skip it on Windows.
authorGlenn Watson <glenn.watson@nokia.com>
Wed, 20 Jun 2012 02:57:43 +0000 (12:57 +1000)
committerQt by Nokia <qt-info@nokia.com>
Thu, 21 Jun 2012 23:26:29 +0000 (01:26 +0200)
The timezone that V8 internally uses was not being updated at the
end of one test, causing failures in subsequent tests. This was
only showing up on Windows because the test was being skipped on
Linux due to a typo. Skip the test on Windows due to complexity
of changing the timezone (added comments in code with details).

Change-Id: I47c8542111e8ddfbdeff39815c50d98570b0c6c2
Reviewed-by: Matthew Vogt <matthew.vogt@nokia.com>
tests/auto/qml/qqmllocale/data/timeZoneUpdated.qml
tests/auto/qml/qqmllocale/tst_qqmllocale.cpp

index 92d1f5b..cc6e143 100644 (file)
@@ -13,11 +13,11 @@ Item {
 
         if (localDate.getTimezoneOffset() != -600) return
 
-        if (localDate.toLocaleString() != "Friday, June 1, 2012 2:15:30 AM AEST") return
+        if (localDate.toLocaleString() != getLocalizedForm('2012-06-01T02:15:30+10:00')) return
         if (localDate.toISOString() != "2012-05-31T16:15:30.000Z") return
 
         if (utcDate.toISOString() != "2012-06-01T02:15:30.000Z") return
-        if (utcDate.toLocaleString() != "Friday, June 1, 2012 12:15:30 PM AEST") return
+        if (utcDate.toLocaleString() != getLocalizedForm('2012-06-01T12:15:30+10:00')) return
 
         success = true
     }
@@ -30,22 +30,26 @@ Item {
 
         if (localDate.getTimezoneOffset() != -330) return
 
-        if (localDate.toLocaleString() != "Friday, June 1, 2012 2:15:30 AM IST") return
+        if (localDate.toLocaleString() != getLocalizedForm('2012-06-01T02:15:30+05:30')) return
         if (localDate.toISOString() != "2012-05-31T20:45:30.000Z") return
 
         if (utcDate.toISOString() != "2012-06-01T06:45:30.000Z") return
-        if (utcDate.toLocaleString() != "Friday, June 1, 2012 12:15:30 PM IST") return
+        if (utcDate.toLocaleString() != getLocalizedForm("2012-06-01T12:15:30+05:30")) return
 
         // Create new dates in this timezone
         localDate = new Date(2012, 6-1, 1, 2, 15, 30)
         utcDate = new Date(Date.UTC(2012, 6-1, 1, 2, 15, 30))
 
-        if (localDate.toLocaleString() != "Friday, June 1, 2012 2:15:30 AM IST") return
+        if (localDate.toLocaleString() != getLocalizedForm("2012-06-01T02:15:30+05:30")) return
         if (localDate.toISOString() != "2012-05-31T20:45:30.000Z") return
 
         if (utcDate.toISOString() != "2012-06-01T02:15:30.000Z") return
-        if (utcDate.toLocaleString() != "Friday, June 1, 2012 7:45:30 AM IST") return
+        if (utcDate.toLocaleString() != getLocalizedForm("2012-06-01T07:45:30+05:30")) return
 
         success = true
     }
+
+    function resetTimeZone() {
+        Date.timeZoneUpdated()
+    }
 }
index c394263..f9ced25 100644 (file)
@@ -43,6 +43,7 @@
 
 #include <QtQml/qqmlengine.h>
 #include <QtQml/qqmlcomponent.h>
+#include <QtQml/qqmlcontext.h>
 #include <QtCore/QDateTime>
 #include <qcolor.h>
 #include "../../shared/util.h"
@@ -1227,9 +1228,30 @@ static void setTimeZone(const QByteArray &tz)
 #endif
 }
 
+class DateFormatter : public QObject
+{
+    Q_OBJECT
+public:
+    DateFormatter() : QObject() {}
+
+    Q_INVOKABLE QString getLocalizedForm(const QString &isoTimestamp);
+};
+
+QString DateFormatter::getLocalizedForm(const QString &isoTimestamp)
+{
+    QDateTime input = QDateTime::fromString(isoTimestamp, Qt::ISODate);
+    QLocale locale;
+    return locale.toString(input);
+}
+
 void tst_qqmllocale::timeZoneUpdated()
 {
-#if !defined(Q_OS_WIN32) && !defined(Q_OS_UINX)
+#if !defined(Q_OS_UNIX)
+    // Currently disabled on Windows as adjusting the timezone
+    // requires additional privileges that aren't normally
+    // enabled for a process. This can be achieved by calling
+    // AdjustTokenPrivileges() and then SetTimeZoneInformation(),
+    // which will require linking to a different library to access that API.
     QSKIP("Timezone manipulation not available for this platform");
 #endif
 
@@ -1238,7 +1260,11 @@ void tst_qqmllocale::timeZoneUpdated()
     // Set the timezone to Brisbane time
     setTimeZone(QByteArray("AEST-10:00"));
 
+    DateFormatter formatter;
+
     QQmlEngine e;
+    e.rootContext()->setContextObject(&formatter);
+
     QQmlComponent c(&e, testFileUrl("timeZoneUpdated.qml"));
     QScopedPointer<QObject> obj(c.create());
     QVERIFY(obj);
@@ -1251,6 +1277,7 @@ void tst_qqmllocale::timeZoneUpdated()
 
     // Reset to original time
     setTimeZone(original);
+    QMetaObject::invokeMethod(obj.data(), "resetTimeZone");
 
     QCOMPARE(obj->property("success").toBool(), true);
 }