summaryrefslogtreecommitdiff
path: root/tests/unit/common/test_mainwindow_compact_controls.cpp
blob: a3a99fa532b729546f8d806ff54b6dea1a5e5c69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// SPDX-License-Identifier: GPL-3.0-or-later
#include <QApplication>
#include <QImage>
#include <QPainter>
#include <QVBoxLayout>
#include <QTest>
#include "widgets/CompactButtonSize.hpp"
#include "widgets/CurrentPageHeightStackedWidget.hpp"
#include "widgets/TxDriveSlider.hpp"

class CompactControlsTest : public QObject
{
  Q_OBJECT
private Q_SLOTS:
  void currentStackPageHeight ()
  {
    CurrentPageHeightStackedWidget stack;
    auto makePage = [&stack] (int rows)
      {
        auto * page = new QWidget {&stack};
        auto * layout = new QVBoxLayout {page};
        for (int row = 0; row < rows; ++row)
          layout->addWidget (new QPushButton {QString::number (row), page});
        return page;
      };
    auto * shortPage = makePage (2);
    auto * tallPage = makePage (6);
    stack.addWidget (shortPage);
    stack.addWidget (tallPage);
    stack.ensurePolished ();

    stack.setCurrentWidget (shortPage);
    QCOMPARE (stack.sizeHint ().height (), shortPage->sizeHint ().height ());
    QVERIFY (stack.sizeHint ().height () < tallPage->sizeHint ().height ());

    stack.setCurrentWidget (tallPage);
    QCOMPARE (stack.sizeHint ().height (), tallPage->sizeHint ().height ());
  }

  void nativeSlider ()
  {
#ifndef Q_OS_WIN
    QSKIP ("Native Windows slider contract");
#else
    for (int width : {27, 44})
      for (int value : {0, 225, 450})
        {
          TxDriveSlider actual;
          QSlider reference;
          reference.setTickPosition (QSlider::TicksRight);
          reference.setTickInterval (50);
          for (auto * slider : {static_cast<QSlider *> (&actual), &reference})
            {
              slider->setOrientation (Qt::Vertical);
              slider->setRange (0, 450);
              slider->setInvertedAppearance (true);
              slider->setValue (value);
              slider->resize (width, 200);
              slider->ensurePolished ();
            }
          QCOMPARE (actual.tickPosition (), reference.tickPosition ());
          QCOMPARE (actual.sizeHint (), reference.sizeHint ());
          QCOMPARE (actual.minimumSizeHint (), reference.minimumSizeHint ());
          QCOMPARE (actual.grab ().toImage (), reference.grab ().toImage ());
        }
#endif
  }
  void compactButtons ()
  {
#if defined (Q_OS_WIN)
    auto const families = {QStringLiteral ("Arial"), QStringLiteral ("Segoe UI")};
#elif defined (Q_OS_LINUX)
    auto const families = {QStringLiteral ("DejaVu Sans"), QStringLiteral ("Liberation Sans")};
#else
    QSKIP ("Compact Windows and Linux button contract");
#endif
#if defined (Q_OS_WIN) || defined (Q_OS_LINUX)
    for (auto const& family : families)
      for (int points : {8, 10, 13, 16})
        {
          QPushButton button {QStringLiteral ("Tx &1")};
          button.setFont (QFont {family, points});
          button.ensurePolished ();
          auto shortSize = compactButtonSize (&button);
          QVERIFY (shortSize.width () < button.sizeHint ().width ());
          button.resize (shortSize);
          QStyleOptionButton option;
          option.initFrom (&button);
          auto contents = button.style ()->subElementRect (QStyle::SE_PushButtonContents, &option, &button);
          auto text = button.fontMetrics ().size (Qt::TextShowMnemonic, button.text ());
          QVERIFY (contents.width () >= text.width ());
          QVERIFY (contents.height () >= text.height ());
          for (auto const& label : {QStringLiteral ("&Lookup"),
                                    QStringLiteral ("Add"),
                                    QStringLiteral ("Ignore")})
            {
              button.setText (label);
              button.resize (compactButtonSize (&button));
              option.initFrom (&button);
              contents = button.style ()->subElementRect (
                QStyle::SE_PushButtonContents, &option, &button);
              text = button.fontMetrics ().size (
                Qt::TextShowMnemonic, button.text ());
              QVERIFY (contents.width () >= text.width ());
              QVERIFY (contents.height () >= text.height ());
            }
          button.setText (QStringLiteral ("Translated action label"));
          QVERIFY (compactButtonSize (&button).width () > shortSize.width ());
        }
#endif
  }
};
QTEST_MAIN (CompactControlsTest)
#include "test_mainwindow_compact_controls.moc"