- intervallo di lettura da 45 secondi a 30 minuti; parte solo con la schermata attiva e si ferma quando esce: nessun processo che resta vivo - il tempo rimanente a schermo continua a scendere: si sottrae il tempo passato dall'ultima lettura (stima locale, zero chiamate di rete), con 6 prove nuove - backoff a un'ora se LG segnala troppe richieste - post e descrizione allineati: versioni 0.3.0, impronte nuove, comportamento dichiarato (avviso di fine lavaggio fino a mezz'ora dopo)
214 lines
9.1 KiB
C++
214 lines
9.1 KiB
C++
// Banco di prova del nucleo: gira su desktop con Qt 5.15, senza kit Sailfish.
|
|
// Verifica il parsing sullo stato VERO di una lavasciuga LG e la coerenza dei
|
|
// comandi. Le asserzioni sulle stringhe sono scritte in inglese (lingua
|
|
// sorgente); con il catalogo italiano caricato si verifica anche la traduzione.
|
|
#include <QCoreApplication>
|
|
#include <QTranslator>
|
|
#include <QDebug>
|
|
#include <QJsonDocument>
|
|
#include <QJsonArray>
|
|
#include <QJsonObject>
|
|
#include <QString>
|
|
#include <QStringList>
|
|
|
|
#include "thinqjson.h"
|
|
|
|
static int failures = 0;
|
|
static int checks = 0;
|
|
|
|
static void check(bool condition, const QString &what, const QString &detail = QString())
|
|
{
|
|
++checks;
|
|
if (condition) {
|
|
qInfo().noquote() << " ok " << what;
|
|
} else {
|
|
++failures;
|
|
qWarning().noquote() << " FAIL " << what << (detail.isEmpty() ? QString() : "-> " + detail);
|
|
}
|
|
}
|
|
|
|
static void checkEq(const QString &got, const QString &expected, const QString &what)
|
|
{
|
|
check(got == expected, what, "got: " + got + " | atteso: " + expected);
|
|
}
|
|
|
|
// Stato reale letto dalla lavasciuga (response di GET /devices/{id}/state).
|
|
static QByteArray realState()
|
|
{
|
|
const QString json = QStringLiteral(
|
|
"[{\"runState\":{\"currentState\":\"DRYING\"},"
|
|
"\"remoteControlEnable\":{\"remoteControlEnabled\":true},"
|
|
"\"timer\":{\"remainHour\":1,\"remainMinute\":54,\"relativeHourToStop\":0,"
|
|
"\"relativeMinuteToStop\":0,\"totalHour\":4,\"totalMinute\":47},"
|
|
"\"cycle\":{\"cycleCount\":4},"
|
|
"\"location\":{\"locationName\":\"MAIN\"}}]");
|
|
return json.toUtf8();
|
|
}
|
|
|
|
static void testParsing()
|
|
{
|
|
qInfo() << "== parsing dello stato reale ==";
|
|
const WasherState s = ThinQJson::parseState(realState());
|
|
check(s.valid, "stato valido");
|
|
checkEq(s.phase, "DRYING", "fase");
|
|
check(s.remainMinutes == 114, "tempo rimanente 114 min", QString::number(s.remainMinutes));
|
|
check(s.totalMinutes == 287, "tempo totale 287 min", QString::number(s.totalMinutes));
|
|
check(s.cycleCount == 4, "quarto ciclo", QString::number(s.cycleCount));
|
|
check(s.remoteEnabled, "comando a distanza attivo");
|
|
check(s.running(), "considerata in corso");
|
|
|
|
qInfo() << "== casi limite ==";
|
|
const WasherState empty = ThinQJson::parseState("[]");
|
|
check(!empty.valid, "risposta vuota -> nessun dato");
|
|
const WasherState junk = ThinQJson::parseState("{\"errore\":true}");
|
|
check(!junk.valid, "json non riconosciuto -> nessun dato");
|
|
|
|
// La posizione MAIN va scelta anche quando non e' la prima della lista.
|
|
const QByteArray twoLocations = QStringLiteral(
|
|
"[{\"runState\":{\"currentState\":\"END\"},\"location\":{\"locationName\":\"MINI\"}},"
|
|
"{\"runState\":{\"currentState\":\"RUNNING\"},\"location\":{\"locationName\":\"MAIN\"}}]")
|
|
.toUtf8();
|
|
checkEq(ThinQJson::parseState(twoLocations).phase, "RUNNING", "sceglie la posizione MAIN");
|
|
}
|
|
|
|
static void testCommands()
|
|
{
|
|
qInfo() << "== comandi ==";
|
|
WasherState s;
|
|
s.valid = true;
|
|
|
|
s.phase = QStringLiteral("DRYING");
|
|
checkEq(ThinQJson::primaryAction(s), "STOP", "in asciugatura si ferma");
|
|
check(ThinQJson::statusLevel(s) == 1, "livello 1 (in corso)");
|
|
checkEq(ThinQJson::primaryActionLabel(s), "Stop", "etichetta Stop");
|
|
|
|
s.phase = QStringLiteral("END");
|
|
checkEq(ThinQJson::primaryAction(s), "START", "a fine ciclo si avvia");
|
|
check(ThinQJson::statusLevel(s) == 3, "livello 3 (finita)");
|
|
|
|
s.phase = QStringLiteral("PAUSE");
|
|
checkEq(ThinQJson::primaryActionLabel(s), "Resume", "in pausa si riprende");
|
|
check(ThinQJson::statusLevel(s) == 2, "livello 2 (in pausa)");
|
|
|
|
s.phase = QStringLiteral("ERROR");
|
|
checkEq(ThinQJson::primaryAction(s), "POWER_OFF", "in errore si spegne");
|
|
check(ThinQJson::statusLevel(s) == 4, "livello 4 (errore)");
|
|
|
|
s.phase = QStringLiteral("POWER_OFF");
|
|
check(ThinQJson::statusLevel(s) == 0, "livello 0 (spenta)");
|
|
|
|
checkEq(ThinQJson::operationPayload("START"),
|
|
"{\"operation\":{\"washerOperationMode\":\"START\"}}", "corpo del comando avvio");
|
|
checkEq(ThinQJson::operationPayload("POWER_OFF"),
|
|
"{\"operation\":{\"washerOperationMode\":\"POWER_OFF\"}}", "corpo dello spegnimento");
|
|
checkEq(ThinQJson::delayPayload(5), "{\"timer\":{\"relativeHourToStop\":5}}",
|
|
"corpo dello stop ritardato");
|
|
}
|
|
|
|
static void testCloud()
|
|
{
|
|
qInfo() << "== indirizzi del cloud ==";
|
|
checkEq(ThinQJson::regionFor("IT"), "eic", "Italia -> eic");
|
|
checkEq(ThinQJson::regionFor("it"), "eic", "paese minuscolo");
|
|
checkEq(ThinQJson::regionFor("US"), "aic", "Stati Uniti -> aic");
|
|
checkEq(ThinQJson::regionFor("KR"), "kic", "Corea -> kic");
|
|
checkEq(ThinQJson::regionFor("XX"), "kic", "paese sconosciuto -> kic");
|
|
checkEq(ThinQJson::regionFor(""), "eic", "paese vuoto -> eic (Europa)");
|
|
checkEq(ThinQJson::apiBase("IT"), "https://api-eic.lgthinq.com", "indirizzo per l'Italia");
|
|
|
|
const QString id = ThinQJson::messageId();
|
|
check(id.length() == 22, "message-id di 22 caratteri", QString::number(id.length()));
|
|
check(!id.contains(QLatin1Char('=')), "message-id senza riempimento");
|
|
check(id != ThinQJson::messageId(), "message-id diversi ad ogni chiamata");
|
|
|
|
// Senza queste due intestazioni il cloud LG risponde 401 con corpo vuoto:
|
|
// e' stato il difetto che impediva all'app di trovare le macchine.
|
|
checkEq(ThinQJson::servicePhase(), "OP", "x-service-phase");
|
|
check(ThinQJson::apiKey().length() == 40, "x-api-key di 40 caratteri",
|
|
QString::number(ThinQJson::apiKey().length()));
|
|
check(!ThinQJson::apiKey().contains(QLatin1Char(' ')), "x-api-key senza spazi");
|
|
}
|
|
|
|
static void testDurations()
|
|
{
|
|
qInfo() << "== durate ==";
|
|
checkEq(ThinQJson::durationText(114), "1 h 54 min", "1 h 54 min");
|
|
checkEq(ThinQJson::durationText(60), "1 h", "un'ora esatta");
|
|
checkEq(ThinQJson::durationText(45), "45 min", "solo minuti");
|
|
checkEq(ThinQJson::durationText(0), "", "zero -> vuoto");
|
|
}
|
|
|
|
static void testItalian(const QString &catalog)
|
|
{
|
|
qInfo() << "== catalogo italiano ==";
|
|
if (catalog.isEmpty()) {
|
|
qWarning() << " SALTO: nessun .qm passato come argomento";
|
|
return;
|
|
}
|
|
QTranslator tr;
|
|
if (!tr.load(catalog)) {
|
|
++failures;
|
|
qWarning() << " FAIL impossibile caricare" << catalog;
|
|
return;
|
|
}
|
|
QCoreApplication::installTranslator(&tr);
|
|
|
|
WasherState s;
|
|
s.valid = true;
|
|
s.phase = QStringLiteral("DRYING");
|
|
checkEq(ThinQJson::phaseLabel(s), "In asciugatura", "fase tradotta");
|
|
s.phase = QStringLiteral("END");
|
|
checkEq(ThinQJson::phaseLabel(s), "Fine", "fine tradotta");
|
|
checkEq(ThinQJson::primaryActionLabel(s), "Avvia", "tasto tradotto");
|
|
s.phase = QStringLiteral("PAUSE");
|
|
checkEq(ThinQJson::primaryActionLabel(s), "Riprendi", "riprendi tradotto");
|
|
|
|
// Le scritte delle schermate nuove (primo avvio, elenco, avviso): se una
|
|
// sfugge al catalogo, la si vede qui invece che sul telefono.
|
|
// ATTENZIONE al contesto: nelle pagine QML il contesto e' il NOME DEL FILE
|
|
// (MainPage, SettingsPage), non "ThinQ" come nel codice C++.
|
|
checkEq(QCoreApplication::translate("MainPage", "My machines"), "Le mie macchine",
|
|
"titolo tradotto");
|
|
checkEq(QCoreApplication::translate("MainPage", "First setup"), "Primo avvio",
|
|
"primo avvio tradotto");
|
|
checkEq(QCoreApplication::translate("MainPage", "Connect"), "Collega", "tasto collega tradotto");
|
|
checkEq(QCoreApplication::translate("MainPage", "Not connected"), "Non collegata",
|
|
"macchina non collegata tradotta");
|
|
checkEq(QCoreApplication::translate("SettingsPage", "Machines"), "Macchine",
|
|
"sezione macchine tradotta");
|
|
checkEq(QCoreApplication::translate("MainPage", "%1 found").arg(2), "2 trovate",
|
|
"conteggio tradotto");
|
|
checkEq(QCoreApplication::translate("harbour-thinq", "Washing finished"), "Lavaggio finito",
|
|
"avviso di fine lavaggio tradotto");
|
|
checkEq(QCoreApplication::translate("ThinQ", "No machine to command"),
|
|
"Nessuna macchina da comandare", "comando senza macchina tradotto");
|
|
}
|
|
|
|
static void testRemainEstimate()
|
|
{
|
|
qInfo().noquote() << "\n-- conto alla rovescia senza rete";
|
|
check(ThinQJson::remainingAfter(37, 0) == 37, "nessun tempo passato: invariato");
|
|
check(ThinQJson::remainingAfter(37, 10) == 27, "dieci minuti passati: scalati");
|
|
check(ThinQJson::remainingAfter(37, 50) == 0, "passato piu' del rimanente: zero, non negativo");
|
|
check(ThinQJson::remainingAfter(-1, 10) == -1, "rimanente sconosciuto: resta sconosciuto");
|
|
check(ThinQJson::remainingAfter(5, -3) == 5, "tempo negativo: non si somma");
|
|
checkEq(ThinQJson::durationText(ThinQJson::remainingAfter(74, 14)), "1 h",
|
|
"stima mostrata come durata");
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
QCoreApplication app(argc, argv);
|
|
const QString catalog = argc > 1 ? QString::fromLocal8Bit(argv[1]) : QString();
|
|
|
|
testParsing();
|
|
testCommands();
|
|
testCloud();
|
|
testDurations();
|
|
testRemainEstimate();
|
|
testItalian(catalog);
|
|
|
|
qInfo().noquote() << QString("\n%1 verifiche, %2 fallite").arg(checks).arg(failures);
|
|
return failures == 0 ? 0 : 1;
|
|
}
|