| 1 | /* |
| 2 | * Copyright (C) 2020 ~ 2021 Uniontech Software Technology Co., Ltd. |
| 3 | * |
| 4 | * Author: ZhangYong <[email protected]> |
| 5 | * |
| 6 | * Maintainer: ZhangYong <[email protected]> |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation, either version 3 of the License, or |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | */ |
| 21 | #include <gtest/gtest.h> |
| 22 | #include <QtConcurrent/QtConcurrent> |
| 23 | #include <gmock/gmock-matchers.h> |
| 24 | #include <QTestEventList> |
| 25 | |
| 26 | #define private public |
| 27 | #define protected public |
| 28 | |
| 29 | #include "application.h" |
| 30 | #include "dialogs/albumcreatedialog.h" |
| 31 | #include "dialogs/albumdeletedialog.h" |
| 32 | #include "widgets/dialogs/imgdeletedialog.h" |
| 33 | #include "controller/exporter.h" |
| 34 | #include "../test_qtestDefine.h" |
| 35 | #include "ac-desktop-define.h" |
| 36 | #include "testtoolkits.h" |
| 37 | #include "thumbnaillistview.h" |
| 38 | #include "QTestEventList" |
| 39 | |
| 40 | //输入:对话框对象、对话框运行时要执行的操作 |
| 41 | template<typename T, typename U> |
| 42 | void stubDialog(T &&activeFun, U &&processFun) |
| 43 | { |
| 44 | qDebug() << __FUNCTION__ << "---"; |
| 45 | activeFun();//启动函数 |
| 46 | |
| 47 | QEventLoop loop; |
| 48 | QtConcurrent::run([ =, &loop]() { |
| 49 | (void)QTest::qWaitFor([ =, &loop]() { |
| 50 | return (loop.isRunning()); |
| 51 | }); |
| 52 | (void)QTest::qWaitFor([ = ]() { |
| 53 | return (qApp->activeModalWidget() != nullptr && qApp->activeModalWidget() != dApp->getMainWindow()); |
| 54 | }); |
| 55 | if (qApp->activeModalWidget() != nullptr) { |
| 56 | QThread::msleep(200); |
| 57 | processFun(); //要执行的操作在这里 |
| 58 | QThread::msleep(200); |
| 59 | QMetaObject::invokeMethod(&loop, "quit"); |
| 60 | } else { |
| 61 | QMetaObject::invokeMethod(&loop, "quit"); |
| 62 | } |
| 63 | }); |
| 64 | loop.exec(); |
| 65 | } |
| 66 | |
| 67 | //三个界面的删除操作 |
| 68 | TEST(allPic, picdelete) |
| 69 | { |
| 70 | TEST_CASE_NAME("picdelete") |
| 71 | MainWindow *w = dApp->getMainWindow(); |
| 72 | QTestEventList e; |
| 73 | |
| 74 | clickToAllPictureView(); |
| 75 | |
| 76 | //------右键删除--------- |
| 77 | QPoint p1(60, 120); |
| 78 | e.addMouseMove(p1); |
| 79 | e.addMouseClick(Qt::MouseButton::LeftButton, Qt::NoModifier, p1, 50); |
| 80 | e.simulate(w->m_pAllPicView->m_pThumbnailListView->viewport()); |
| 81 | e.clear(); |
| 82 | |
| 83 | //所有照片 |
| 84 | auto menu = runContextMenu(w->m_pAllPicView->m_pThumbnailListView->viewport(), p1); |
| 85 | using TR_SUBORDINATE_t = PointerTypeGetter < decltype(w->m_pAllPicView->m_pThumbnailListView) >::type; |
| 86 | |
| 87 | asynchronousObject asynchronous; |
| 88 | stubDialog( |
| 89 | [ & ]() { |
| 90 | QMetaObject::invokeMethod(&asynchronous, "asynchronousRunActionFromMenu" |
| 91 | , Qt::QueuedConnection, Q_ARG(QMenu *, menu), Q_ARG(QString, TR_SUBORDINATE_t::tr("Delete"))); |
| 92 | }, |
| 93 | [ = ]() { |
| 94 | QMetaObject::invokeMethod(w, [ = ]() { |
| 95 | ImgDeleteDialog *dialog = qobject_cast<ImgDeleteDialog *>(qApp->activeModalWidget()); |
| 96 | QTestEventList e; |
| 97 | e.addKeyPress(Qt::Key::Key_Tab); |
| 98 | e.simulate(dialog->getButton(0)); |
| 99 | QTest::qWait(200); |
| 100 | |
| 101 | e.simulate(dialog->getButton(1)); |
| 102 | QTest::qWait(200); |
| 103 | |
| 104 | QWidget *closeButton = dialog->findChild<QWidget *>("DTitlebarDWindowCloseButton"); |
| 105 | e.simulate(closeButton); |
| 106 | QTest::qWait(200); |
| 107 | |
| 108 | e.simulate(dialog->getButton(0)); |
| 109 | QTest::qWait(200); |
| 110 | |
| 111 | e.clear(); |
| 112 | e.addKeyPress(Qt::Key::Key_Escape);//这个会让它退出去,不需要执行done |
| 113 | e.simulate(dialog); |
| 114 | }, Qt::QueuedConnection); |
| 115 | |
| 116 | }); |
| 117 | } |
| 118 | |
| 119 | TEST(albumcreatedialog, dia1) |
| 120 | { |
| 121 | TEST_CASE_NAME("dia1") |
| 122 | AlbumCreateDialog *a = new AlbumCreateDialog; |
| 123 | ASSERT_TRUE(a->getCreateAlbumName().isEmpty()); |
| 124 | |
| 125 | QTestEventList event; |
| 126 | event.addKeyClick(Qt::Key_Escape); |
| 127 | event.simulate(a); |
| 128 | |
| 129 | QTest::keyClicks(a->getEdit(), "test1"); |
| 130 | emit a->buttonClicked(0, ""); |
| 131 | } |
| 132 | |
| 133 | TEST(albumdeletedialog, deletdialog) |
| 134 | { |
| 135 | TEST_CASE_NAME("deletdialog") |
| 136 | AlbumDeleteDialog *d = new AlbumDeleteDialog; |
| 137 | QTestEventList e; |
| 138 | e.addKeyClick(Qt::Key_A); |
| 139 | e.simulate(d); |
| 140 | e.clear(); |
| 141 | } |
| 142 | |
| 143 | TEST(albumdeletedialog, exportdialog) |
| 144 | { |
| 145 | TEST_CASE_NAME("exportdialog") |
| 146 | CExportImageDialog *c = new CExportImageDialog; |
| 147 | c->deleteLater(); |
| 148 | } |