summaryrefslogtreecommitdiff
path: root/tests/unit/common/test_round_robin_selection.cpp
blob: 5bf32351970da229a78892cfcade85f69d1ae47b (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
#include <QtTest>
#include <QComboBox>
#include <QLineEdit>

#include "widgets/RoundRobinSelection.hpp"

namespace
{
  QString selectedValue (QComboBox const& combo)
  {
    return QString::fromStdString (
      BeaconTx::formatRoundRobinPolicy (RoundRobinSelection::policy (combo)));
  }
}

class TestRoundRobinSelection final : public QObject
{
  Q_OBJECT

private slots:
  void presetsHaveCanonicalIdentity ()
  {
    QComboBox combo;
    combo.setEditable (true);
    RoundRobinSelection::initialize (combo, "Localized random");
    QCOMPARE (combo.count (), 21);
    QCOMPARE (combo.currentText (), QString {"Localized random"});
    QCOMPARE (selectedValue (combo), QString {"random"});
    for (int index = 1; index < combo.count (); ++index)
      {
        combo.setCurrentIndex (index);
        QCOMPARE (selectedValue (combo), combo.currentData ().toString ());
        QVERIFY (RoundRobinSelection::policy (combo).kind == BeaconTx::RoundRobinPolicy::Kind::Fixed);
      }
    combo.setItemText (1, "Localized fixed preset");
    combo.setCurrentIndex (1);
    QCOMPARE (selectedValue (combo), QString {"1/2"});
  }

  void editorOverridesSelectedPreset ()
  {
    QComboBox combo;
    combo.setEditable (true);
    RoundRobinSelection::initialize (combo, "Random");
    QString observed;
    connect (&combo, &QComboBox::currentTextChanged, this,
             [&] {observed = selectedValue (combo);});
    combo.setEditText ("7/12");
    QCOMPARE (combo.currentIndex (), 0);
    QCOMPARE (observed, QString {"7/12"});
    combo.setCurrentIndex (1);
    combo.setEditText ("2/4");
    QCOMPARE (observed, QString {"2/4"});
    RoundRobinSelection::setPolicy (combo, BeaconTx::RoundRobinPolicy::fixed (0, 2));
    QCOMPARE (combo.currentText (), QString {"1/2"});
    QCOMPARE (observed, QString {"1/2"});
    combo.setEditText ("invalid");
    QCOMPARE (observed, QString {"random"});
    combo.setCurrentIndex (0);
    QCOMPARE (observed, QString {"random"});
  }

  void committedCustomEntryDoesNotNeedPresetData ()
  {
    QComboBox combo;
    combo.setEditable (true);
    RoundRobinSelection::initialize (combo, "Random");
    combo.setEditText ("7/12");
    QTest::keyClick (combo.lineEdit (), Qt::Key_Return);
    QVERIFY (combo.currentIndex () > 0);
    QVERIFY (!combo.currentData ().isValid ());
    QCOMPARE (selectedValue (combo), QString {"7/12"});
  }

  void selectionRestoration_data ()
  {
    QTest::addColumn<QString> ("stored");
    QTest::addColumn<QString> ("expected");
    QTest::newRow ("custom") << "7/12" << "7/12";
    QTest::newRow ("preset") << "2/4" << "2/4";
    QTest::newRow ("whitespace") << " 2 / 4 " << "2/4";
    QTest::newRow ("canonical-random") << "random" << "random";
    QTest::newRow ("legacy-random-label") << "Localized random" << "random";
    QTest::newRow ("invalid") << "13/12" << "random";
  }

  void selectionRestoration ()
  {
    QFETCH (QString, stored);
    QFETCH (QString, expected);
    QComboBox combo;
    combo.setEditable (true);
    RoundRobinSelection::initialize (combo, "Localized random");
    RoundRobinSelection::setPolicy (combo, BeaconTx::parseRoundRobinPolicy (
      stored.toStdString ()));
    QCOMPARE (selectedValue (combo), expected);
    QCOMPARE (combo.currentText (), expected == "random" ? QString {"Localized random"} : expected);
  }
};

QTEST_MAIN (TestRoundRobinSelection)

#include "test_round_robin_selection.moc"