summaryrefslogtreecommitdiff
path: root/tests/unit/qmap/test_decode_click_coalescer.cpp
blob: 284aa5e59cc3df9f31fa2d712d475cc8c0792886 (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
#include <QtTest>

#include "qmap/decode_click_coalescer.h"
#include "qmap/qmap_click_policy.h"

class TestDecodeClickCoalescer final : public QObject
{
  Q_OBJECT

private slots:
  void disarmsBeforeSingleClick();
  void coalescesUpdatedRecordByIdentity();
  void keepsDifferentRecordsSeparate();
  void protectsDoubleClickWhenDisarmIsOverwritten();
};

namespace
{
  struct DispatchEvent
  {
    QByteArray row;
    DecodeClickGesture gesture;
  };
}

void TestDecodeClickCoalescer::disarmsBeforeSingleClick()
{
  QList<DispatchEvent> dispatched;
  DecodeClickCoalescer coalescer {this, [&dispatched] (QByteArray const& row,
                                                        DecodeClickGesture gesture) {
    dispatched.append ({row, gesture});
  }, 20};

  coalescer.press ("K1ABC", "first");
  QCOMPARE (dispatched.size (), 1);
  QVERIFY (dispatched.first ().gesture == DecodeClickGesture::Press);
  QTRY_COMPARE_WITH_TIMEOUT (dispatched.size (), 2, 100);
  QCOMPARE (dispatched.last ().row, QByteArray {"first"});
  QVERIFY (dispatched.last ().gesture == DecodeClickGesture::SingleClick);
}

void TestDecodeClickCoalescer::coalescesUpdatedRecordByIdentity()
{
  QList<DispatchEvent> dispatched;
  DecodeClickCoalescer coalescer {this, [&dispatched] (QByteArray const& row,
                                                        DecodeClickGesture gesture) {
    dispatched.append ({row, gesture});
  }, 20};

  coalescer.press ("K1ABC", "old record");
  coalescer.doubleClick ("K1ABC", "new record");
  QCOMPARE (dispatched.size (), 2);
  QCOMPARE (dispatched.last ().row, QByteArray {"new record"});
  QVERIFY (dispatched.last ().gesture == DecodeClickGesture::DoubleClick);
  QTest::qWait (30);
  QCOMPARE (dispatched.size (), 2);
}

void TestDecodeClickCoalescer::keepsDifferentRecordsSeparate()
{
  QList<DispatchEvent> dispatched;
  DecodeClickCoalescer coalescer {this, [&dispatched] (QByteArray const& row,
                                                        DecodeClickGesture gesture) {
    dispatched.append ({row, gesture});
  }, 20};

  coalescer.press ("K1ABC", "first");
  coalescer.press ("K2XYZ", "second");
  QCOMPARE (dispatched.size (), 3);
  QCOMPARE (dispatched.at (1).row, QByteArray {"first"});
  QVERIFY (dispatched.at (1).gesture == DecodeClickGesture::SingleClick);
  QVERIFY (dispatched.last ().gesture == DecodeClickGesture::Press);
  QTRY_COMPARE_WITH_TIMEOUT (dispatched.size (), 4, 100);
  QCOMPARE (dispatched.last ().row, QByteArray {"second"});
  QVERIFY (dispatched.last ().gesture == DecodeClickGesture::SingleClick);
}

void TestDecodeClickCoalescer::protectsDoubleClickWhenDisarmIsOverwritten()
{
  auto const failedDoubleClick = qmapClickPolicy (true, false, true, true);
  QVERIFY (failedDoubleClick.disarmBeforeQsy);
  QVERIFY (!failedDoubleClick.enableAutoTx);
  QVERIFY (!failedDoubleClick.restartTransmission);

  auto const acceptedDoubleClick = qmapClickPolicy (true, true, false, true);
  QVERIFY (acceptedDoubleClick.enableAutoTx);
  QVERIFY (acceptedDoubleClick.restartTransmission);

  auto const failedSingleClick = qmapClickPolicy (false, false, true, false);
  QVERIFY (failedSingleClick.disableAutoTx);
}

QTEST_GUILESS_MAIN (TestDecodeClickCoalescer)

#include "test_decode_click_coalescer.moc"