|
| 1 | +#include "models/SliceModel.h" |
| 2 | + |
| 3 | +#include <QCoreApplication> |
| 4 | +#include <QMap> |
| 5 | +#include <QSignalSpy> |
| 6 | +#include <QString> |
| 7 | +#include <cstdio> |
| 8 | + |
| 9 | +using namespace AetherSDR; |
| 10 | + |
| 11 | +static int g_failures = 0; |
| 12 | + |
| 13 | +#define EXPECT_EQ(actual, expected) do { \ |
| 14 | + auto a_ = (actual); auto e_ = (expected); \ |
| 15 | + if (a_ != e_) { \ |
| 16 | + const QString a_str = QString("%1").arg(a_); \ |
| 17 | + const QString e_str = QString("%1").arg(e_); \ |
| 18 | + std::fprintf(stderr, "FAIL %s:%d expected %s, got %s\n", \ |
| 19 | + __FILE__, __LINE__, \ |
| 20 | + e_str.toUtf8().constData(), \ |
| 21 | + a_str.toUtf8().constData()); \ |
| 22 | + ++g_failures; \ |
| 23 | + } \ |
| 24 | +} while (0) |
| 25 | + |
| 26 | +static QMap<QString, QString> kv(std::initializer_list<std::pair<QString, QString>> pairs) |
| 27 | +{ |
| 28 | + QMap<QString, QString> m; |
| 29 | + for (const auto& p : pairs) m.insert(p.first, p.second); |
| 30 | + return m; |
| 31 | +} |
| 32 | + |
| 33 | +int main(int argc, char** argv) |
| 34 | +{ |
| 35 | + QCoreApplication app(argc, argv); |
| 36 | + |
| 37 | + // ── Fallback: letter() returns 'A' + sliceId when index_letter hasn't |
| 38 | + // arrived yet (older firmware, early in status sequence). |
| 39 | + { |
| 40 | + SliceModel s(2); |
| 41 | + EXPECT_EQ(s.letter(), QString("C")); // 'A' + 2 |
| 42 | + } |
| 43 | + |
| 44 | + // ── applyStatus("index_letter=A") populates the radio-given letter |
| 45 | + // and emits letterChanged with the resolved value. |
| 46 | + { |
| 47 | + SliceModel s(3); |
| 48 | + QSignalSpy spy(&s, &SliceModel::letterChanged); |
| 49 | + s.applyStatus(kv({{"index_letter", "A"}})); |
| 50 | + EXPECT_EQ(s.letter(), QString("A")); |
| 51 | + EXPECT_EQ(spy.count(), 1); |
| 52 | + EXPECT_EQ(spy.takeFirst().at(0).toString(), QString("A")); |
| 53 | + } |
| 54 | + |
| 55 | + // ── Re-applying the same letter does NOT re-emit letterChanged. |
| 56 | + { |
| 57 | + SliceModel s(1); |
| 58 | + s.applyStatus(kv({{"index_letter", "B"}})); |
| 59 | + QSignalSpy spy(&s, &SliceModel::letterChanged); |
| 60 | + s.applyStatus(kv({{"index_letter", "B"}})); |
| 61 | + EXPECT_EQ(spy.count(), 0); |
| 62 | + } |
| 63 | + |
| 64 | + // ── Letter change emits exactly once and the resolved value follows. |
| 65 | + { |
| 66 | + SliceModel s(2); |
| 67 | + s.applyStatus(kv({{"index_letter", "A"}})); |
| 68 | + QSignalSpy spy(&s, &SliceModel::letterChanged); |
| 69 | + s.applyStatus(kv({{"index_letter", "C"}})); |
| 70 | + EXPECT_EQ(s.letter(), QString("C")); |
| 71 | + EXPECT_EQ(spy.count(), 1); |
| 72 | + EXPECT_EQ(spy.takeFirst().at(0).toString(), QString("C")); |
| 73 | + } |
| 74 | + |
| 75 | + // ── emitLetterRefresh() forces a re-emission with the current value |
| 76 | + // (used when a display preference changes). |
| 77 | + { |
| 78 | + SliceModel s(0); |
| 79 | + s.applyStatus(kv({{"index_letter", "A"}})); |
| 80 | + QSignalSpy spy(&s, &SliceModel::letterChanged); |
| 81 | + s.emitLetterRefresh(); |
| 82 | + EXPECT_EQ(spy.count(), 1); |
| 83 | + EXPECT_EQ(spy.takeFirst().at(0).toString(), QString("A")); |
| 84 | + } |
| 85 | + |
| 86 | + // ── Status messages without index_letter don't disturb the stored |
| 87 | + // letter or emit on letterChanged. |
| 88 | + { |
| 89 | + SliceModel s(2); |
| 90 | + s.applyStatus(kv({{"index_letter", "A"}})); |
| 91 | + QSignalSpy spy(&s, &SliceModel::letterChanged); |
| 92 | + s.applyStatus(kv({{"in_use", "1"}, {"RF_frequency", "14.250"}})); |
| 93 | + EXPECT_EQ(s.letter(), QString("A")); |
| 94 | + EXPECT_EQ(spy.count(), 0); |
| 95 | + } |
| 96 | + |
| 97 | + if (g_failures == 0) { |
| 98 | + std::printf("slice_model_letter_test: all checks passed\n"); |
| 99 | + return 0; |
| 100 | + } |
| 101 | + std::printf("slice_model_letter_test: %d failure(s)\n", g_failures); |
| 102 | + return 1; |
| 103 | +} |
0 commit comments