File indexing completed on 2024-05-19 05:34:38

0001 /*
0002     SPDX-FileCopyrightText: 2014 Marco Martin <mart@kde.org>
0003     SPDX-FileCopyrightText: 2023 Harald Sitter <sitter@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #include "dialognativetest.h"
0009 
0010 #include "utils.h"
0011 
0012 #include <KWindowSystem>
0013 #include <KX11Extras>
0014 
0015 namespace
0016 {
0017 constexpr auto panelHeight = 50;
0018 constexpr auto panelWidth = panelHeight;
0019 constexpr qreal content1Width = 100;
0020 constexpr qreal content1Height = content1Width;
0021 constexpr qreal content2Width = 50;
0022 constexpr qreal content2Height = 25;
0023 } // namespace
0024 
0025 void DialogNativeTest::initTestCase()
0026 {
0027     QStandardPaths::setTestModeEnabled(true);
0028     Plasma::TestUtils::installPlasmaTheme();
0029 
0030     m_cacheDir = QDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
0031     m_cacheDir.removeRecursively();
0032 
0033     m_dialog = new PlasmaQuick::Dialog;
0034     m_dialog->setLocation(Plasma::Types::TopEdge);
0035 
0036     m_panel = new QQuickView;
0037     m_panel->setColor(Qt::red);
0038     m_panel->setGeometry(0, 0, panelHeight, panelWidth);
0039     m_panel->setFlags(Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);
0040 
0041     m_panel2 = new QQuickView;
0042     m_panel2->setColor(Qt::green);
0043     m_panel2->setGeometry(panelWidth * 2, 0, panelHeight, panelWidth);
0044     m_panel2->setFlags(Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);
0045 
0046     m_panel3 = new QQuickView;
0047     m_panel3->setColor(Qt::blue);
0048     m_panel3->setGeometry(panelWidth * 4, 0, panelHeight, panelWidth);
0049     m_panel3->setFlags(Qt::FramelessWindowHint | Qt::WindowDoesNotAcceptFocus);
0050 
0051     m_content = new QQuickItem;
0052     m_content->setWidth(content1Width);
0053     m_content->setHeight(content1Height);
0054     m_dialog->setMainItem(m_content);
0055 
0056     m_content2 = new QQuickItem(m_panel3->contentItem());
0057     m_content2->setWidth(content2Width);
0058     m_content2->setHeight(content2Height);
0059 
0060     m_panel->show();
0061     m_panel2->show();
0062     m_panel3->show();
0063     KX11Extras::setType(m_panel->winId(), NET::Dock);
0064     KX11Extras::setType(m_panel3->winId(), NET::Dock);
0065     m_dialog->setVisualParent(m_panel->contentItem());
0066     m_dialog->show();
0067 }
0068 
0069 void DialogNativeTest::cleanupTestCase()
0070 {
0071     delete m_dialog;
0072     delete m_panel;
0073     delete m_panel2;
0074     delete m_panel3;
0075     delete m_content;
0076 
0077     m_cacheDir.removeRecursively();
0078 }
0079 
0080 void DialogNativeTest::size()
0081 {
0082     QVERIFY(QTest::qWaitForWindowExposed(m_dialog));
0083 
0084     QCOMPARE(m_content->width(), content1Width);
0085     QCOMPARE(m_content->height(), content1Height);
0086 
0087     constexpr qreal themeFixedMargin = 4.0;
0088     QCOMPARE(m_dialog->margins()->property("left").value<qreal>(), themeFixedMargin);
0089     QCOMPARE(m_dialog->margins()->property("top").value<qreal>(), themeFixedMargin);
0090     QCOMPARE(m_dialog->margins()->property("right").value<qreal>(), themeFixedMargin);
0091     QCOMPARE(m_dialog->margins()->property("bottom").value<qreal>(), themeFixedMargin);
0092 
0093     QCOMPARE(m_dialog->width(), content1Width + themeFixedMargin * 2);
0094     QCOMPARE(m_dialog->height(), content1Height + themeFixedMargin * 2);
0095 
0096     QCOMPARE(m_content2->width(), content2Width);
0097     QCOMPARE(m_content2->height(), content2Height);
0098 }
0099 
0100 void DialogNativeTest::position()
0101 {
0102     QVERIFY(QTest::qWaitForWindowExposed(m_dialog));
0103 
0104     // Find where the outermost test-panel lives. Normally that would be
0105     // at x,y = (0,0) but if the test is run on a desktop with a
0106     // left-hand-edge panel, then the test-panel is placed next to it.
0107     const auto upper_left_x = m_panel->x();
0108     const auto upper_left_y = m_panel->y();
0109 
0110     constexpr auto offset = 1;
0111     constexpr auto anchorY = panelHeight - offset;
0112 
0113     QCOMPARE(m_dialog->x(), upper_left_x + 0);
0114     QCOMPARE(m_dialog->y(), upper_left_y + anchorY);
0115 
0116     m_dialog->setVisualParent(m_panel2->contentItem());
0117     // this derives from the center point of the current panel, I am too lazy to calculate this - sitter, 2023
0118     QCOMPARE(m_dialog->x(), 71);
0119     QCOMPARE(m_dialog->y(), anchorY);
0120 
0121     m_panel3->setMask(QRect(0, 0, panelWidth, panelHeight / 2));
0122     m_dialog->setVisualParent(m_content2);
0123     QCOMPARE(m_dialog->x(), 171);
0124     QCOMPARE(m_dialog->y(), panelHeight / 2 - offset);
0125 }
0126 
0127 QTEST_MAIN(DialogNativeTest)
0128 
0129 #include "moc_dialognativetest.cpp"