From 7ba28a270c7254d5829cfb18710532ff15fb8bfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8C=83=E6=9C=8B=E7=A8=8B?= Date: Wed, 10 Mar 2021 10:05:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0Mock?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加Mock依赖 Log: Change-Id: I9625b67a9ac0155fafd60aca3ec27b996c6005e8 --- debian/control | 3 +- frame/item/appitem.cpp | 34 ++--- .../mockinterface/qgsettingsinterfacemock.cpp | 79 ----------- frame/mockinterface/qgsettingsinterfacemock.h | 45 ------ tests/CMakeLists.txt | 5 + tests/cmake/modules/FindGMock.cmake | 130 ++++++++++++++++++ tests/{ => controller}/ut_dockitemmanager.cpp | 0 tests/{ => item/components}/ut_appdrag.cpp | 14 +- .../components}/ut_appdragwidget.cpp | 21 +-- .../{ => item/components}/ut_appsnapshot.cpp | 0 .../components}/ut_floatingpreview.cpp | 2 +- .../components}/ut_hoverhighlighteffect.cpp | 0 tests/{ => item}/ut_appitem.cpp | 8 +- tests/{ => item}/ut_dockitem.cpp | 2 + tests/{ => item}/ut_launcheritem.cpp | 25 +++- tests/{ => item}/ut_placeholderitem.cpp | 2 +- tests/mock/QGsettingsMock.h | 45 ++++++ tests/util/ut_monitor.cpp | 35 ++--- tests/{ => util}/ut_multiscreenworker.cpp | 0 tests/{ => util}/ut_statebutton.cpp | 0 tests/util/ut_touchsignalmanager.cpp | 31 +++-- tests/util/ut_utils.cpp | 22 +-- 22 files changed, 285 insertions(+), 218 deletions(-) delete mode 100644 frame/mockinterface/qgsettingsinterfacemock.cpp delete mode 100644 frame/mockinterface/qgsettingsinterfacemock.h create mode 100644 tests/cmake/modules/FindGMock.cmake rename tests/{ => controller}/ut_dockitemmanager.cpp (100%) rename tests/{ => item/components}/ut_appdrag.cpp (86%) rename tests/{ => item/components}/ut_appdragwidget.cpp (89%) rename tests/{ => item/components}/ut_appsnapshot.cpp (100%) rename tests/{ => item/components}/ut_floatingpreview.cpp (96%) rename tests/{ => item/components}/ut_hoverhighlighteffect.cpp (100%) rename tests/{ => item}/ut_appitem.cpp (89%) rename tests/{ => item}/ut_dockitem.cpp (96%) rename tests/{ => item}/ut_launcheritem.cpp (68%) rename tests/{ => item}/ut_placeholderitem.cpp (96%) create mode 100644 tests/mock/QGsettingsMock.h rename tests/{ => util}/ut_multiscreenworker.cpp (100%) rename tests/{ => util}/ut_statebutton.cpp (100%) diff --git a/debian/control b/debian/control index d02f5779b..4d68ab940 100644 --- a/debian/control +++ b/debian/control @@ -24,7 +24,8 @@ Build-Depends: debhelper (>= 8.0.0), libdde-network-utils-dev, libdbusmenu-qt5-dev, libdtkgui-dev, - libgtest-dev + libgtest-dev, + libgmock-dev Standards-Version: 3.9.8 Homepage: http://www.deepin.org/ diff --git a/frame/item/appitem.cpp b/frame/item/appitem.cpp index 157b2ff49..d3ff7dbda 100644 --- a/frame/item/appitem.cpp +++ b/frame/item/appitem.cpp @@ -27,7 +27,6 @@ #include "appswingeffectbuilder.h" #include "appspreviewprovider.h" #include "qgsettingsinterfaceimpl.h" -#include "qgsettingsinterfacemock.h" #include #include @@ -55,14 +54,12 @@ static QGSettingsInterface *GSettingsByApp(QGSettingsInterface::Type type) static QGSettingsInterfaceImpl settings("com.deepin.dde.dock.module.app"); return &settings; } - case QGSettingsInterface::Type::MockType: - { - static QGSettingsInterfaceMock settings("com.deepin.dde.dock.module.app"); - return &settings; - } default: + { + qWarning("Unless you are doing unit testing, you should't see this message"); return nullptr; } + } } static QGSettingsInterface *GSettingsByActiveApp(QGSettingsInterface::Type type) @@ -73,14 +70,12 @@ static QGSettingsInterface *GSettingsByActiveApp(QGSettingsInterface::Type type) static QGSettingsInterfaceImpl settings("com.deepin.dde.dock.module.activeapp"); return &settings; } - case QGSettingsInterface::Type::MockType: - { - static QGSettingsInterfaceMock settings("com.deepin.dde.dock.module.activeapp"); - return &settings; - } default: + { + qWarning("Unless you are doing unit testing, you should't see this message"); return nullptr; } + } } static QGSettingsInterface *GSettingsByDockApp(QGSettingsInterface::Type type) @@ -91,14 +86,12 @@ static QGSettingsInterface *GSettingsByDockApp(QGSettingsInterface::Type type) static QGSettingsInterfaceImpl settings("com.deepin.dde.dock.module.dockapp"); return &settings; } - case QGSettingsInterface::Type::MockType: - { - static QGSettingsInterfaceMock settings("com.deepin.dde.dock.module.dockapp"); - return &settings; - } default: + { + qWarning("Unless you are doing unit testing, you should't see this message"); return nullptr; } + } } AppItem::AppItem(const QDBusObjectPath &entry, QGSettingsInterface::Type type, QWidget *parent) @@ -161,9 +154,12 @@ AppItem::AppItem(const QDBusObjectPath &entry, QGSettingsInterface::Type type, Q updateWindowInfos(m_itemEntryInter->windowInfos()); refreshIcon(); - connect(m_qgAppInterface->gsettings(), &QGSettings::changed, this, &AppItem::onGSettingsChanged); - connect(m_qgDockedAppInterface->gsettings(), &QGSettings::changed, this, &AppItem::onGSettingsChanged); - connect(m_qgActiveAppInterface->gsettings(), &QGSettings::changed, this, &AppItem::onGSettingsChanged); + if (m_qgAppInterface && m_qgAppInterface->gsettings()) + connect(m_qgAppInterface->gsettings(), &QGSettings::changed, this, &AppItem::onGSettingsChanged); + if (m_qgDockedAppInterface && m_qgDockedAppInterface->gsettings()) + connect(m_qgDockedAppInterface->gsettings(), &QGSettings::changed, this, &AppItem::onGSettingsChanged); + if (m_qgActiveAppInterface && m_qgActiveAppInterface->gsettings()) + connect(m_qgActiveAppInterface->gsettings(), &QGSettings::changed, this, &AppItem::onGSettingsChanged); connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::themeTypeChanged, this, &AppItem::onThemeTypeChanged); diff --git a/frame/mockinterface/qgsettingsinterfacemock.cpp b/frame/mockinterface/qgsettingsinterfacemock.cpp deleted file mode 100644 index 6f74360f9..000000000 --- a/frame/mockinterface/qgsettingsinterfacemock.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. - * - * Author: fanpengcheng - * - * Maintainer: fanpengcheng - * - * 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 - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#include -#include - -#include "qgsettingsinterfacemock.h" - -QGSettingsInterfaceMock::QGSettingsInterfaceMock(const QByteArray &schema_id, const QByteArray &path, QObject *parent) -{ - -} - -QGSettingsInterfaceMock::~QGSettingsInterfaceMock() -{ - -} - -QGSettingsInterface::Type QGSettingsInterfaceMock::type() -{ - return Type::MockType; -} - -QGSettings *QGSettingsInterfaceMock::gsettings() -{ - return nullptr; -} - -QVariant QGSettingsInterfaceMock::get(const QString &key) const -{ - return QVariant(); -} - -void QGSettingsInterfaceMock::set(const QString &key, const QVariant &value) -{ - -} - -bool QGSettingsInterfaceMock::trySet(const QString &key, const QVariant &value) -{ - return false; -} - -QStringList QGSettingsInterfaceMock::keys() const -{ - return QStringList(); -} - -QVariantList QGSettingsInterfaceMock::choices(const QString &key) const -{ - return QVariantList(); -} - -void QGSettingsInterfaceMock::reset(const QString &key) -{ - -} - -bool QGSettingsInterfaceMock::isSchemaInstalled(const QByteArray &schema_id) -{ - return false; -} diff --git a/frame/mockinterface/qgsettingsinterfacemock.h b/frame/mockinterface/qgsettingsinterfacemock.h deleted file mode 100644 index 6b61fd6c6..000000000 --- a/frame/mockinterface/qgsettingsinterfacemock.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. - * - * Author: fanpengcheng - * - * Maintainer: fanpengcheng - * - * 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 - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ -#ifndef QGSETTINGSINTERFACEMOCK_H -#define QGSETTINGSINTERFACEMOCK_H -#include - -#include "qgsettingsinterface.h" - -class QGSettings; -class QGSettingsInterfaceMock : public QGSettingsInterface -{ -public: - QGSettingsInterfaceMock(const QByteArray &schema_id, const QByteArray &path = QByteArray(), QObject *parent = nullptr); - ~QGSettingsInterfaceMock() override; - - virtual Type type() override; - virtual QGSettings *gsettings() override; - virtual QVariant get(const QString &key) const override; - virtual void set(const QString &key, const QVariant &value) override; - virtual bool trySet(const QString &key, const QVariant &value) override; - virtual QStringList keys() const override; - virtual QVariantList choices(const QString &key) const override; - virtual void reset(const QString &key) override; - static bool isSchemaInstalled(const QByteArray &schema_id); -}; - -#endif // QGSETTINGSINTERFACEMOCK_H diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 99e03ca55..5026d2a49 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -11,6 +11,9 @@ file(GLOB_RECURSE SRCS "*.h" "*.cpp" "../widgets/*.h" "../widgets/*.cpp") # 用于测试覆盖率的编译条件 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -lgcov") +# 查找gmock的cmake文件 +set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/tests/cmake/modules) + # 查找依赖库 find_package(PkgConfig REQUIRED) find_package(Qt5Widgets REQUIRED) @@ -21,6 +24,7 @@ find_package(DtkWidget REQUIRED) find_package(Qt5Svg REQUIRED) find_package(Qt5 COMPONENTS Test REQUIRED) find_package(GTest REQUIRED) +find_package(GMock REQUIRED) pkg_check_modules(QGSettings REQUIRED gsettings-qt) pkg_check_modules(DFrameworkDBus REQUIRED dframeworkdbus) @@ -53,6 +57,7 @@ target_link_libraries(${BIN_NAME} PRIVATE ${QGSettings_LIBRARIES} ${Qt5Svg_LIBRARIES} ${GTEST_LIBRARIES} + ${GMOCK_LIBRARIES} -lpthread -lm ) diff --git a/tests/cmake/modules/FindGMock.cmake b/tests/cmake/modules/FindGMock.cmake new file mode 100644 index 000000000..2ad922129 --- /dev/null +++ b/tests/cmake/modules/FindGMock.cmake @@ -0,0 +1,130 @@ +# Locate the Google C++ Mocking Framework. +# (This file is almost an identical copy of the original FindGTest.cmake file, +# feel free to use it as it is or modify it for your own needs.) +# +# +# Defines the following variables: +# +# GMOCK_FOUND - Found the Google Testing framework +# GMOCK_INCLUDE_DIRS - Include directories +# +# Also defines the library variables below as normal +# variables. These contain debug/optimized keywords when +# a debugging library is found. +# +# GMOCK_BOTH_LIBRARIES - Both libgmock & libgmock-main +# GMOCK_LIBRARIES - libgmock +# GMOCK_MAIN_LIBRARIES - libgmock-main +# +# Accepts the following variables as input: +# +# GMOCK_ROOT - (as a CMake or environment variable) +# The root directory of the gmock install prefix +# +# GMOCK_MSVC_SEARCH - If compiling with MSVC, this variable can be set to +# "MD" or "MT" to enable searching a gmock build tree +# (defaults: "MD") +# +#----------------------- +# Example Usage: +# +# find_package(GMock REQUIRED) +# include_directories(${GMOCK_INCLUDE_DIRS}) +# +# add_executable(foo foo.cc) +# target_link_libraries(foo ${GMOCK_BOTH_LIBRARIES}) +# +#============================================================================= +# This file is released under the MIT licence: +# +# Copyright (c) 2011 Matej Svec +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to +# deal in the Software without restriction, including without limitation the +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or +# sell copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS +# IN THE SOFTWARE. +#============================================================================= + + +function(_gmock_append_debugs _endvar _library) + if(${_library} AND ${_library}_DEBUG) + set(_output optimized ${${_library}} debug ${${_library}_DEBUG}) + else() + set(_output ${${_library}}) + endif() + set(${_endvar} ${_output} PARENT_SCOPE) +endfunction() + +function(_gmock_find_library _name) + find_library(${_name} + NAMES ${ARGN} + HINTS + $ENV{GMOCK_ROOT} + ${GMOCK_ROOT} + PATH_SUFFIXES ${_gmock_libpath_suffixes} + ) + mark_as_advanced(${_name}) +endfunction() + + +if(NOT DEFINED GMOCK_MSVC_SEARCH) + set(GMOCK_MSVC_SEARCH MD) +endif() + +set(_gmock_libpath_suffixes lib) +if(MSVC) + if(GMOCK_MSVC_SEARCH STREQUAL "MD") + list(APPEND _gmock_libpath_suffixes + msvc/gmock-md/Debug + msvc/gmock-md/Release) + elseif(GMOCK_MSVC_SEARCH STREQUAL "MT") + list(APPEND _gmock_libpath_suffixes + msvc/gmock/Debug + msvc/gmock/Release) + endif() +endif() + +find_path(GMOCK_INCLUDE_DIR gmock/gmock.h + HINTS + $ENV{GMOCK_ROOT}/include + ${GMOCK_ROOT}/include +) +mark_as_advanced(GMOCK_INCLUDE_DIR) + +if(MSVC AND GMOCK_MSVC_SEARCH STREQUAL "MD") + # The provided /MD project files for Google Mock add -md suffixes to the + # library names. + _gmock_find_library(GMOCK_LIBRARY gmock-md gmock) + _gmock_find_library(GMOCK_LIBRARY_DEBUG gmock-mdd gmockd) + _gmock_find_library(GMOCK_MAIN_LIBRARY gmock_main-md gmock_main) + _gmock_find_library(GMOCK_MAIN_LIBRARY_DEBUG gmock_main-mdd gmock_maind) +else() + _gmock_find_library(GMOCK_LIBRARY gmock) + _gmock_find_library(GMOCK_LIBRARY_DEBUG gmockd) + _gmock_find_library(GMOCK_MAIN_LIBRARY gmock_main) + _gmock_find_library(GMOCK_MAIN_LIBRARY_DEBUG gmock_maind) +endif() + +include(FindPackageHandleStandardArgs) +FIND_PACKAGE_HANDLE_STANDARD_ARGS(GMock DEFAULT_MSG GMOCK_LIBRARY GMOCK_INCLUDE_DIR GMOCK_MAIN_LIBRARY) + +if(GMOCK_FOUND) + set(GMOCK_INCLUDE_DIRS ${GMOCK_INCLUDE_DIR}) + _gmock_append_debugs(GMOCK_LIBRARIES GMOCK_LIBRARY) + _gmock_append_debugs(GMOCK_MAIN_LIBRARIES GMOCK_MAIN_LIBRARY) + set(GMOCK_BOTH_LIBRARIES ${GMOCK_LIBRARIES} ${GMOCK_MAIN_LIBRARIES}) +endif() + diff --git a/tests/ut_dockitemmanager.cpp b/tests/controller/ut_dockitemmanager.cpp similarity index 100% rename from tests/ut_dockitemmanager.cpp rename to tests/controller/ut_dockitemmanager.cpp diff --git a/tests/ut_appdrag.cpp b/tests/item/components/ut_appdrag.cpp similarity index 86% rename from tests/ut_appdrag.cpp rename to tests/item/components/ut_appdrag.cpp index ff82bdbdf..9a1612843 100644 --- a/tests/ut_appdrag.cpp +++ b/tests/item/components/ut_appdrag.cpp @@ -24,7 +24,7 @@ #include #include "appdrag.h" -#include "qgsettingsinterfacemock.h" +#include "mock/QGsettingsMock.h" class Test_AppDrag : public ::testing::Test { @@ -38,22 +38,26 @@ public: void Test_AppDrag::SetUp() { - QWidget *w = new QWidget; - drag = new AppDrag(new QGSettingsInterfaceMock("com.deepin.dde.dock.distancemultiple", "/com/deepin/dde/dock/distancemultiple/"),w); } void Test_AppDrag::TearDown() { - delete drag; - drag = nullptr; } TEST_F(Test_AppDrag, drag_test) { + QWidget *w = new QWidget; + QGSettingsMock mock; + ON_CALL(mock, get(::testing::_)) .WillByDefault(::testing::Invoke([](const QString& key){return 1.5; })); + + drag = new AppDrag(&mock, w); QPixmap pix(":/res/all_settings_on.png"); drag->setPixmap(pix); ASSERT_TRUE(drag->appDragWidget()); drag->exec(); + + delete drag; + drag = nullptr; } diff --git a/tests/ut_appdragwidget.cpp b/tests/item/components/ut_appdragwidget.cpp similarity index 89% rename from tests/ut_appdragwidget.cpp rename to tests/item/components/ut_appdragwidget.cpp index e0ea7637d..c7aed38b6 100644 --- a/tests/ut_appdragwidget.cpp +++ b/tests/item/components/ut_appdragwidget.cpp @@ -25,7 +25,7 @@ #define private public #include "appdragwidget.h" -#include "qgsettingsinterfacemock.h" +#include "mock/QGsettingsMock.h" #undef private class Test_AppDragWidget : public ::testing::Test @@ -33,24 +33,23 @@ class Test_AppDragWidget : public ::testing::Test public: virtual void SetUp() override; virtual void TearDown() override; - -public: - AppDragWidget *dragWidget = nullptr; }; void Test_AppDragWidget::SetUp() -{ - dragWidget = new AppDragWidget(new QGSettingsInterfaceMock("com.deepin.dde.dock.distancemultiple", "/com/deepin/dde/dock/distancemultiple/")); +{ } void Test_AppDragWidget::TearDown() { - delete dragWidget; - dragWidget = nullptr; } TEST_F(Test_AppDragWidget, cuntion_test) { + QGSettingsMock mock; + ON_CALL(mock, get(::testing::_)) .WillByDefault(::testing::Invoke([](const QString& key){return 1.5; })); + + AppDragWidget *dragWidget = new AppDragWidget(&mock); + QPixmap pix(":/res/all_settings_on.png"); dragWidget->setAppPixmap(pix); dragWidget->setOriginPos(QPoint(-1, -1)); @@ -62,18 +61,12 @@ TEST_F(Test_AppDragWidget, cuntion_test) dragWidget->showRemoveTips(); dragWidget->showGoBackAnimation(); -} -TEST_F(Test_AppDragWidget, event_test) -{ dragWidget->show(); dragWidget->hide(); QTest::mouseClick(dragWidget,Qt::LeftButton, Qt::NoModifier, QPoint(dragWidget->rect().center())); -} -TEST_F(Test_AppDragWidget, isRemoveAble_test) -{ // bottom const QRect &rect = QRect(QPoint(0, 1040), QPoint(1920, 1080)); dragWidget->setDockInfo(Dock::Position::Bottom, rect); diff --git a/tests/ut_appsnapshot.cpp b/tests/item/components/ut_appsnapshot.cpp similarity index 100% rename from tests/ut_appsnapshot.cpp rename to tests/item/components/ut_appsnapshot.cpp diff --git a/tests/ut_floatingpreview.cpp b/tests/item/components/ut_floatingpreview.cpp similarity index 96% rename from tests/ut_floatingpreview.cpp rename to tests/item/components/ut_floatingpreview.cpp index dc99bdf97..d1d472b5a 100644 --- a/tests/ut_floatingpreview.cpp +++ b/tests/item/components/ut_floatingpreview.cpp @@ -71,7 +71,7 @@ TEST_F(Test_FloatingPreview, view_test) // view->m_closeBtn3D->click(); view->hide(); - ASSERT_EQ(shot->contentsMargins(), QMargins(0, 0, 0, 0)); + ASSERT_TRUE(shot->contentsMargins() == QMargins(0, 0, 0, 0)); } TEST_F(Test_FloatingPreview, empty_test) diff --git a/tests/ut_hoverhighlighteffect.cpp b/tests/item/components/ut_hoverhighlighteffect.cpp similarity index 100% rename from tests/ut_hoverhighlighteffect.cpp rename to tests/item/components/ut_hoverhighlighteffect.cpp diff --git a/tests/ut_appitem.cpp b/tests/item/ut_appitem.cpp similarity index 89% rename from tests/ut_appitem.cpp rename to tests/item/ut_appitem.cpp index 4257f58cf..42808e88a 100644 --- a/tests/ut_appitem.cpp +++ b/tests/item/ut_appitem.cpp @@ -26,7 +26,7 @@ #define private public #include "appitem.h" -#include "qgsettingsinterfacemock.h" +#include "qgsettingsinterface.h" #undef private class Test_AppItem : public ::testing::Test @@ -59,7 +59,7 @@ TEST_F(Test_AppItem, coverage_test) appItem->setDockInfo(Dock::Position::Top, QRect(QPoint(0,0), QPoint(1920, 40))); - ASSERT_EQ(appItem->accessibleName(), appItem->m_itemEntryInter->name()); + ASSERT_TRUE(appItem->accessibleName() == appItem->m_itemEntryInter->name()); } TEST_F(Test_AppItem, AppItem_show_test) @@ -68,7 +68,7 @@ TEST_F(Test_AppItem, AppItem_show_test) QThread::msleep(450); - ASSERT_EQ(appItem->isVisible(), true); + ASSERT_TRUE(appItem->isVisible()); } TEST_F(Test_AppItem, AppItem_hide_test) @@ -77,5 +77,5 @@ TEST_F(Test_AppItem, AppItem_hide_test) QThread::msleep(450); - ASSERT_EQ(appItem->isVisible(), false); + ASSERT_TRUE(!appItem->isVisible()); } diff --git a/tests/ut_dockitem.cpp b/tests/item/ut_dockitem.cpp similarity index 96% rename from tests/ut_dockitem.cpp rename to tests/item/ut_dockitem.cpp index f22aefbf1..c0da75f90 100644 --- a/tests/ut_dockitem.cpp +++ b/tests/item/ut_dockitem.cpp @@ -23,8 +23,10 @@ #include #include +#include #include "dockitem.h" +#include "mock/QGsettingsMock.h" class Test_DockItem : public ::testing::Test { diff --git a/tests/ut_launcheritem.cpp b/tests/item/ut_launcheritem.cpp similarity index 68% rename from tests/ut_launcheritem.cpp rename to tests/item/ut_launcheritem.cpp index 7b85b135b..1b4ee86c5 100644 --- a/tests/ut_launcheritem.cpp +++ b/tests/item/ut_launcheritem.cpp @@ -23,34 +23,43 @@ #include #include +#include + +using namespace ::testing; #define private public #include "launcheritem.h" #undef private -#include "qgsettingsinterfacemock.h" + +#include "mock/QGsettingsMock.h" + class Test_LauncherItem : public ::testing::Test { public: virtual void SetUp() override; virtual void TearDown() override; - -public: - LauncherItem *launcherItem = nullptr; }; void Test_LauncherItem::SetUp() { - launcherItem = new LauncherItem(new QGSettingsInterfaceMock("com.deepin.dde.dock.module.launcher")); } void Test_LauncherItem::TearDown() { - delete launcherItem; - launcherItem = nullptr; } TEST_F(Test_LauncherItem, launcher_test) { + QGSettingsMock mock; +// EXPECT_CALL(mock, type()).WillRepeatedly(Return(QGSettingsMock::Type::MockType)); +// EXPECT_CALL(mock, keys()).WillRepeatedly(Return(QStringList() << "enable" << "control")); + + ON_CALL(mock, type()).WillByDefault(Return(QGSettingsMock::Type::MockType)); + ON_CALL(mock, keys()).WillByDefault(Return(QStringList() << "enable" << "control")); + ON_CALL(mock, get(_)) .WillByDefault(::testing::Invoke([](const QString& key){return true; })); + + LauncherItem *launcherItem = new LauncherItem(&mock); + ASSERT_EQ(launcherItem->itemType(), LauncherItem::Launcher); launcherItem->refreshIcon(); launcherItem->show(); @@ -60,6 +69,8 @@ TEST_F(Test_LauncherItem, launcher_test) launcherItem->update(); QThread::msleep(10); launcherItem->resize(100,100); + ASSERT_FALSE(launcherItem->popupTips()); + ON_CALL(mock, get(_)) .WillByDefault(::testing::Invoke([](const QString& key){ return false; })); ASSERT_TRUE(launcherItem->popupTips()); QTest::mouseClick(launcherItem, Qt::LeftButton, Qt::NoModifier, launcherItem->geometry().center()); diff --git a/tests/ut_placeholderitem.cpp b/tests/item/ut_placeholderitem.cpp similarity index 96% rename from tests/ut_placeholderitem.cpp rename to tests/item/ut_placeholderitem.cpp index b5e3c4bf7..ec3c508fc 100644 --- a/tests/ut_placeholderitem.cpp +++ b/tests/item/ut_placeholderitem.cpp @@ -47,7 +47,7 @@ void Test_PlaceholderItem::TearDown() placeholderitem = nullptr; } -TEST_F(Test_PlaceholderItem, launcher_test) +TEST_F(Test_PlaceholderItem, placeholder_test) { QCOMPARE(placeholderitem->itemType(), PlaceholderItem::Placeholder); } diff --git a/tests/mock/QGsettingsMock.h b/tests/mock/QGsettingsMock.h new file mode 100644 index 000000000..bbc75d38d --- /dev/null +++ b/tests/mock/QGsettingsMock.h @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2011 ~ 2018 Deepin Technology Co., Ltd. + * + * Author: fanpengcheng + * + * Maintainer: fanpengcheng + * + * 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 + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#ifndef QGSETTINGSMOCK_H +#define QGSETTINGSMOCK_H +#include "qgsettingsinterface.h" +#include + +#include +#include + +class QGSettingsMock : public QGSettingsInterface +{ +public: + virtual ~QGSettingsMock() {} + + MOCK_METHOD0(type, Type(void)); + MOCK_METHOD0(gsettings, QGSettings *(void)); + MOCK_CONST_METHOD1(get, QVariant(const QString &key)); + MOCK_METHOD2(set, void(const QString &key, const QVariant &value)); + MOCK_METHOD2(trySet, bool (const QString &key, const QVariant &value)); + MOCK_CONST_METHOD0(keys, QStringList(void)); + MOCK_CONST_METHOD1(choices, QVariantList(const QString &key)); + MOCK_METHOD1(reset, void(const QString &key)); + + static bool isSchemaInstalled(const QByteArray &schema_id) {Q_UNUSED(schema_id); return true;} +}; +#endif // QGSETTINGSMOCK_H diff --git a/tests/util/ut_monitor.cpp b/tests/util/ut_monitor.cpp index 6a80f25de..896521953 100644 --- a/tests/util/ut_monitor.cpp +++ b/tests/util/ut_monitor.cpp @@ -21,6 +21,7 @@ #include #include +#include #include @@ -73,43 +74,43 @@ TEST_F(Test_Monitor, monitor_test) int h = 100; monitor->setX(x); - ASSERT_EQ(monitor->x(), x); + QCOMPARE(monitor->x(), x); monitor->setX(x); monitor->setY(y); - ASSERT_EQ(monitor->y(), y); + QCOMPARE(monitor->y(), y); monitor->setY(y); monitor->setW(w); - ASSERT_EQ(monitor->w(), w); + QCOMPARE(monitor->w(), w); monitor->setW(w); monitor->setH(h); - ASSERT_EQ(monitor->h(), h); + QCOMPARE(monitor->h(), h); monitor->setH(h); - ASSERT_EQ(monitor->left(), x); - ASSERT_EQ(monitor->right(), x + w); - ASSERT_EQ(monitor->top(), y); - ASSERT_EQ(monitor->bottom(), y + h); + QCOMPARE(monitor->left(), x); + QCOMPARE(monitor->right(), x + w); + QCOMPARE(monitor->top(), y); + QCOMPARE(monitor->bottom(), y + h); - ASSERT_EQ(monitor->topLeft(), QPoint(x, y)); - ASSERT_EQ(monitor->topRight(), QPoint(x + w, y)); - ASSERT_EQ(monitor->bottomLeft(), QPoint(x, y + h)); - ASSERT_EQ(monitor->bottomRight(), QPoint(x + w, y + h)); - ASSERT_EQ(monitor->rect(), QRect(x, y, w, h)); + QCOMPARE(monitor->topLeft(), QPoint(x, y)); + QCOMPARE(monitor->topRight(), QPoint(x + w, y)); + QCOMPARE(monitor->bottomLeft(), QPoint(x, y + h)); + QCOMPARE(monitor->bottomRight(), QPoint(x + w, y + h)); + QCOMPARE(monitor->rect(), QRect(x, y, w, h)); QString name = "MonitorTestName"; monitor->setName(name); - ASSERT_EQ(monitor->name(), name); + QCOMPARE(monitor->name(), name); QString path = "testPath"; monitor->setPath(path); - ASSERT_EQ(monitor->path(), path); + QCOMPARE(monitor->path(), path); bool monitorEnable = true; monitor->setMonitorEnable(monitorEnable); - ASSERT_EQ(monitor->enable(), monitorEnable); + QCOMPARE(monitor->enable(), monitorEnable); monitor->setMonitorEnable(monitorEnable); Monitor::DockPosition dockPosition; @@ -118,7 +119,7 @@ TEST_F(Test_Monitor, monitor_test) dockPosition.topDock = true; dockPosition.bottomDock = true; monitor->setDockPosition(dockPosition); - ASSERT_EQ(monitor->dockPosition(), dockPosition); + QCOMPARE(monitor->dockPosition(), dockPosition); } TEST_F(Test_Monitor, dockPosition_test) diff --git a/tests/ut_multiscreenworker.cpp b/tests/util/ut_multiscreenworker.cpp similarity index 100% rename from tests/ut_multiscreenworker.cpp rename to tests/util/ut_multiscreenworker.cpp diff --git a/tests/ut_statebutton.cpp b/tests/util/ut_statebutton.cpp similarity index 100% rename from tests/ut_statebutton.cpp rename to tests/util/ut_statebutton.cpp diff --git a/tests/util/ut_touchsignalmanager.cpp b/tests/util/ut_touchsignalmanager.cpp index d19591956..334ae839b 100644 --- a/tests/util/ut_touchsignalmanager.cpp +++ b/tests/util/ut_touchsignalmanager.cpp @@ -2,6 +2,7 @@ #include "touchsignalmanager.h" #undef private +#include #include #include @@ -23,23 +24,23 @@ void Ut_TouchSignalManager::TearDown() TEST_F(Ut_TouchSignalManager, isDragIconPress_test) { - ASSERT_EQ(TouchSignalManager::instance()->isDragIconPress(), false); + QCOMPARE(TouchSignalManager::instance()->isDragIconPress(), false); TouchSignalManager::instance()->m_dragIconPressed = true; - ASSERT_EQ(TouchSignalManager::instance()->isDragIconPress(), true); + QCOMPARE(TouchSignalManager::instance()->isDragIconPress(), true); TouchSignalManager::instance()->m_dragIconPressed = false; - ASSERT_EQ(TouchSignalManager::instance()->isDragIconPress(), false); + QCOMPARE(TouchSignalManager::instance()->isDragIconPress(), false); } TEST_F(Ut_TouchSignalManager, dealShortTouchPress_test) { QSignalSpy spy(TouchSignalManager::instance(), SIGNAL(shortTouchPress(int, double, double))); TouchSignalManager::instance()->dealShortTouchPress(1, 0, 0); - ASSERT_EQ(spy.count(), 1); - ASSERT_EQ(TouchSignalManager::instance()->isDragIconPress(), true); + QCOMPARE(spy.count(), 1); + QCOMPARE(TouchSignalManager::instance()->isDragIconPress(), true); const QList &arguments = spy.takeFirst(); - ASSERT_EQ(arguments.size(), 3); - ASSERT_EQ(arguments.at(0), 1); + QCOMPARE(arguments.size(), 3); + QCOMPARE(arguments.at(0), 1); ASSERT_TRUE(qAbs(arguments.at(1).toDouble()) < 0.00001); ASSERT_TRUE(qAbs(arguments.at(2).toDouble()) < 0.00001); } @@ -48,11 +49,11 @@ TEST_F(Ut_TouchSignalManager, dealTouchRelease_test) { QSignalSpy spy(TouchSignalManager::instance(), SIGNAL(touchRelease(double, double))); TouchSignalManager::instance()->dealTouchRelease(0, 0); - ASSERT_EQ(spy.count(), 1); - ASSERT_EQ(TouchSignalManager::instance()->isDragIconPress(), false); + QCOMPARE(spy.count(), 1); + QCOMPARE(TouchSignalManager::instance()->isDragIconPress(), false); const QList &arguments = spy.takeFirst(); - ASSERT_EQ(arguments.size(), 2); + QCOMPARE(arguments.size(), 2); ASSERT_TRUE(qAbs(arguments.at(0).toDouble()) < 0.00001); ASSERT_TRUE(qAbs(arguments.at(1).toDouble()) < 0.00001); } @@ -61,16 +62,16 @@ TEST_F(Ut_TouchSignalManager, dealTouchPress_test) { QSignalSpy spy(TouchSignalManager::instance(), SIGNAL(middleTouchPress(double, double))); TouchSignalManager::instance()->dealTouchPress(1, 1000, 0, 0); - ASSERT_EQ(spy.count(), 1); + QCOMPARE(spy.count(), 1); const QList &arguments = spy.takeFirst(); - ASSERT_EQ(arguments.size(), 2); + QCOMPARE(arguments.size(), 2); ASSERT_TRUE(qAbs(arguments.at(0).toDouble()) < 0.00001); ASSERT_TRUE(qAbs(arguments.at(1).toDouble()) < 0.00001); TouchSignalManager::instance()->dealTouchPress(1, 2000, 0, 0); - ASSERT_EQ(spy.count(), 0); + QCOMPARE(spy.count(), 0); TouchSignalManager::instance()->dealTouchPress(1, 500, 0, 0); - ASSERT_EQ(spy.count(), 0); + QCOMPARE(spy.count(), 0); TouchSignalManager::instance()->dealTouchPress(2, 0000, 0, 0); - ASSERT_EQ(spy.count(), 0); + QCOMPARE(spy.count(), 0); } diff --git a/tests/util/ut_utils.cpp b/tests/util/ut_utils.cpp index 5c0b0e7d7..0c31cdfae 100644 --- a/tests/util/ut_utils.cpp +++ b/tests/util/ut_utils.cpp @@ -1,5 +1,7 @@ #include "utils.h" +#include + #include class Ut_Utils : public ::testing::Test @@ -23,11 +25,11 @@ TEST_F(Ut_Utils, comparePluginApi_test) QString v2("1.0.1"); QString v3("1.0.0.0"); - ASSERT_EQ(Utils::comparePluginApi(v1, v1), 0); - ASSERT_EQ(Utils::comparePluginApi(v1, v2), -1); - ASSERT_EQ(Utils::comparePluginApi(v2, v1), 1); - ASSERT_EQ(Utils::comparePluginApi(v1, v3), -1); - ASSERT_EQ(Utils::comparePluginApi(v3, v1), 1); + QCOMPARE(Utils::comparePluginApi(v1, v1), 0); + QCOMPARE(Utils::comparePluginApi(v1, v2), -1); + QCOMPARE(Utils::comparePluginApi(v2, v1), 1); + QCOMPARE(Utils::comparePluginApi(v1, v3), -1); + QCOMPARE(Utils::comparePluginApi(v3, v1), 1); } TEST_F(Ut_Utils, isSettingConfigured_test) @@ -39,13 +41,13 @@ TEST_F(Ut_Utils, isSettingConfigured_test) TEST_F(Ut_Utils, screenAt_test) { Utils::screenAt(QPoint(0, 0)); - ASSERT_EQ(Utils::screenAt(QPoint(-1, -1)), nullptr); + QCOMPARE(Utils::screenAt(QPoint(-1, -1)), nullptr); } TEST_F(Ut_Utils, screenAtByScaled_test) { Utils::screenAtByScaled(QPoint(0, 0)); - ASSERT_EQ(Utils::screenAtByScaled(QPoint(-1, -1)), nullptr); + QCOMPARE(Utils::screenAtByScaled(QPoint(-1, -1)), nullptr); } TEST_F(Ut_Utils, renderSVG_test) @@ -54,7 +56,7 @@ TEST_F(Ut_Utils, renderSVG_test) const QSize &size = pix.size(); ASSERT_TRUE(Utils::renderSVG("", size, 1.0).isNull()); - ASSERT_EQ(Utils::renderSVG(":/res/all_settings_on.png", size, 1.0).size(), size); - ASSERT_EQ(Utils::renderSVG(":/res/all_settings_on.png", QSize(50, 50), 1.0).size(), QSize(50, 50)); - ASSERT_EQ(Utils::renderSVG(":/res/all_settings_on.png", QSize(50, 50), 0.5).size(), QSize(25, 25)); + QCOMPARE(Utils::renderSVG(":/res/all_settings_on.png", size, 1.0).size(), size); + QCOMPARE(Utils::renderSVG(":/res/all_settings_on.png", QSize(50, 50), 1.0).size(), QSize(50, 50)); + QCOMPARE(Utils::renderSVG(":/res/all_settings_on.png", QSize(50, 50), 0.5).size(), QSize(25, 25)); }