Add tests
authorIceyer <me@iceyer.net>
Thu, 3 Aug 2017 03:28:43 +0000 (11:28 +0800)
committerIceyer <me@iceyer.net>
Thu, 3 Aug 2017 03:54:58 +0000 (11:54 +0800)
Change-Id: Ibcd2de8c0346bb0ad31555de392eaac7965bf3bc

dtkcore.pro
tests/dutiltester.cpp [new file with mode: 0644]
tests/dutiltester.h [new file with mode: 0644]
tests/main.cpp [new file with mode: 0644]
tests/singletontester.cpp [new file with mode: 0644]
tests/singletontester.h [new file with mode: 0644]
tests/tests.pro [new file with mode: 0644]

index 39f7fbfd0bb1a3aca92ce700b5f622cb372c82f5..1e7088fdf3b51b4b05b9684043b78208f5097969 100644 (file)
@@ -4,4 +4,5 @@ CONFIG = ordered
 
 SUBDIRS +=   \
     src \
-    tool
+    tool \
+    tests
diff --git a/tests/dutiltester.cpp b/tests/dutiltester.cpp
new file mode 100644 (file)
index 0000000..eb0df58
--- /dev/null
@@ -0,0 +1,61 @@
+#include "dutiltester.h"
+
+#include <QCoreApplication>
+#include <QtTest/QtTest>
+#include <QStandardPaths>
+#include <QThread>
+
+#include "log/LogManager.h"
+#include "filesystem/dpathbuf.h"
+#include "singletontester.h"
+
+DCORE_USE_NAMESPACE
+
+TestDUtil::TestDUtil()
+{
+
+}
+
+void TestDUtil::testLogPath()
+{
+    qApp->setOrganizationName("deepin");
+    qApp->setApplicationName("deepin-test-dtk");
+
+    DPathBuf logPath(QStandardPaths::standardLocations(QStandardPaths::HomeLocation).first());
+    logPath = logPath / ".cache/deepin/deepin-test-dtk/deepin-test-dtk.log";
+
+    QCOMPARE(DLogManager::getlogFilePath(), logPath.toString());
+}
+
+void TestDUtil::testPathChange()
+{
+    DPathBuf root("/");
+
+    auto usr = root / "./usr";
+    QCOMPARE(QDir(usr.toString()).absolutePath(), QDir::toNativeSeparators("/usr"));
+
+    root /= "root";
+    QCOMPARE(root.toString(), QDir::toNativeSeparators("/root"));
+
+    root /= "../usr";
+    QCOMPARE(root.toString(), usr.toString());
+}
+
+void TestDUtil::testDSingleton()
+{
+    auto threadA = new QThread;
+    auto testerA = new MultiSingletonTester;
+    connect(threadA, &QThread::started, testerA, &MultiSingletonTester::run);
+    testerA->moveToThread(threadA);
+
+    auto threadB = new QThread;
+    auto testerB = new MultiSingletonTester;
+    testerB->moveToThread(threadB);
+    connect(threadB, &QThread::started, testerB, &MultiSingletonTester::run);
+
+    threadA->start();
+    threadB->start();
+
+    QThread::sleep(5);
+}
+
diff --git a/tests/dutiltester.h b/tests/dutiltester.h
new file mode 100644 (file)
index 0000000..c5e89a8
--- /dev/null
@@ -0,0 +1,18 @@
+#ifndef DUTILTESTER_H
+#define DUTILTESTER_H
+
+#include <QObject>
+
+class TestDUtil: public QObject
+{
+    Q_OBJECT
+public:
+    TestDUtil();
+
+private Q_SLOTS:
+    void testLogPath();
+    void testPathChange();
+    void testDSingleton();
+};
+
+#endif // DUTILTESTER_H
diff --git a/tests/main.cpp b/tests/main.cpp
new file mode 100644 (file)
index 0000000..28005f9
--- /dev/null
@@ -0,0 +1,5 @@
+#include <QtTest/QtTest>
+
+#include "dutiltester.h"
+
+QTEST_MAIN(TestDUtil);
diff --git a/tests/singletontester.cpp b/tests/singletontester.cpp
new file mode 100644 (file)
index 0000000..406aa82
--- /dev/null
@@ -0,0 +1,34 @@
+/**
+ * Copyright (C) 2016 Deepin Technology Co., Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ **/
+
+#include "singletontester.h"
+
+#include <QDebug>
+#include <QThread>
+
+Singleton::Singleton(QObject *parent) : QObject(parent)
+{
+    qDebug() << "Singleton Init Begin" << this;
+    QThread::sleep(3);
+    qDebug() << "Singleton Init End" << this;
+}
+
+void Singleton::test()
+{
+    qDebug() << "test" << this;
+}
+
+MultiSingletonTester::MultiSingletonTester(QObject *parent) : QObject(parent)
+{
+}
+
+void MultiSingletonTester::run()
+{
+    Singleton::instance()->test();
+}
diff --git a/tests/singletontester.h b/tests/singletontester.h
new file mode 100644 (file)
index 0000000..ab8ba0d
--- /dev/null
@@ -0,0 +1,35 @@
+/**
+ * Copyright (C) 2016 Deepin Technology Co., Ltd.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ **/
+
+#pragma once
+
+#include <QObject>
+
+#include "base/dsingleton.h"
+
+class Singleton : public QObject, public Dtk::Core::DSingleton<Singleton>
+{
+    Q_OBJECT
+    friend class Dtk::Core::DSingleton<Singleton>;
+public:
+    explicit Singleton(QObject *parent = 0);
+
+    void test();
+};
+
+class MultiSingletonTester : public QObject
+{
+    Q_OBJECT
+public:
+    explicit MultiSingletonTester(QObject *parent = 0);
+
+    void run();
+};
+
+
diff --git a/tests/tests.pro b/tests/tests.pro
new file mode 100644 (file)
index 0000000..f26a5bd
--- /dev/null
@@ -0,0 +1,21 @@
+TEMPLATE = app
+QT += testlib
+QT -= gui
+CONFIG += testcase c++11
+TARGET = tests
+
+SOURCES += \
+    main.cpp \
+    dutiltester.cpp \
+    singletontester.cpp
+
+HEADERS += \
+    dutiltester.h \
+    singletontester.h
+
+win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../src/release/ -ldtkcore
+else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../src/debug/ -ldtkcore
+else:unix: LIBS += -L$$OUT_PWD/../src/ -ldtkcore
+
+INCLUDEPATH += $$PWD/../src
+DEPENDPATH += $$PWD/../src