From: Jari Jarvi Date: Mon, 14 Jun 2010 10:32:03 +0000 (+0300) Subject: Unit tests for RemoteList are now ready X-Git-Url: https://vcs.maemo.org/git/?p=irwi;a=commitdiff_plain;h=9d77c84418a946ce8f806b03e2db4d834122247a Unit tests for RemoteList are now ready --- diff --git a/src/remotelist.cpp b/src/remotelist.cpp index 7cfd34d..5d33a3f 100644 --- a/src/remotelist.cpp +++ b/src/remotelist.cpp @@ -1,4 +1,5 @@ #include "remotelist.h" + #include #include #include @@ -21,18 +22,6 @@ RemoteList::RemoteList(QDomDocument &doc) parse(doc); } -RemoteList::RemoteList(const QString &xmlFile) -{ - parse(xmlFile); -} - -RemoteList::RemoteList(QIODevice &in) -{ - QDomDocument doc; - if (doc.setContent(&in)) - parse(doc); -} - RemoteList::~RemoteList() { } @@ -44,13 +33,6 @@ void RemoteList::setContent(QDomDocument &doc) parse(doc); } -void RemoteList::setContent(const QString &xmlFile) -{ - mfgMap.clear(); - modelMap.clear(); - parse(xmlFile); -} - bool RemoteList::isValid() { return valid; @@ -71,28 +53,6 @@ QList RemoteList::models(const QString &manufacturer) return modelMap.value(manufacturer); } -void RemoteList::parse(const QString &xmlFile) -{ - QFile file(xmlFile); - QDomDocument doc; - - if (!file.open(QIODevice::ReadOnly)) - { - valid = false; - return; - } - - if (!doc.setContent(&file)) - { - file.close(); - valid = false; - return; - } - file.close(); - - parse(doc); -} - void RemoteList::parse(QDomDocument &doc) { QDomNodeList chars = doc.elementsByTagName("char"); diff --git a/src/remotelist.h b/src/remotelist.h index 4650c09..28481a3 100644 --- a/src/remotelist.h +++ b/src/remotelist.h @@ -13,12 +13,9 @@ public: RemoteList(); RemoteList(const RemoteList &); RemoteList(QDomDocument &); - RemoteList(const QString &xmlFile); - RemoteList(QIODevice &); ~RemoteList(); void setContent(QDomDocument &); - void setContent(const QString &xmlFile); // Returns false if document is not set or it's invalid bool isValid(); @@ -28,7 +25,6 @@ public: QList models(const QString &manufacturer); private: - void parse(const QString &xmlFile); void parse(QDomDocument &doc); QStringList parseMfgs(QDomElement &charEl); QList parseModels(QDomElement &mfgEl); diff --git a/ut/ut_remotelist/notxml b/ut/ut_remotelist/notxml deleted file mode 100644 index 9d1eb4c..0000000 --- a/ut/ut_remotelist/notxml +++ /dev/null @@ -1,2 +0,0 @@ -This is just a plain text file. - diff --git a/ut/ut_remotelist/remotes.xml b/ut/ut_remotelist/remotes.xml deleted file mode 100644 index ea236df..0000000 --- a/ut/ut_remotelist/remotes.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/ut/ut_remotelist/ut_remotelist.cpp b/ut/ut_remotelist/ut_remotelist.cpp index 3aed64c..8e949a0 100644 --- a/ut/ut_remotelist/ut_remotelist.cpp +++ b/ut/ut_remotelist/ut_remotelist.cpp @@ -1,7 +1,6 @@ #include "ut_remotelist.h" -#include "remotelist.h" + #include -#include void Ut_RemoteList::initTestCase() { @@ -38,42 +37,111 @@ void Ut_RemoteList::initTestCase() validModels.insert("Topfield", QList()); validModels["Topfield"].append(Model("PVR5000", "PVR_5000_00")); + + createDocument(); } void Ut_RemoteList::cleanupTestCase() { } -void Ut_RemoteList::init() +void Ut_RemoteList::createDocument() +{ + QDomElement root = document.createElement("db"); + document.appendChild(root); + + for (int i = 0; i < validMfgs.keys().size(); ++i) + { + QChar letter = validMfgs.keys().at(i).at(0); + QDomElement charEl = document.createElement("char"); + charEl.setAttribute("id", letter); + createMfgElements(letter, charEl); + root.appendChild(charEl); + } +} + +void Ut_RemoteList::createMfgElements(QChar &letter, QDomElement &parent) { + for (int i = 0; i < validMfgs[letter].size(); ++i) + { + QString mfg = validMfgs[letter].at(i); + QDomElement mfgEl = document.createElement("mfg"); + mfgEl.setAttribute("id", mfg); + createModelElements(mfg, mfgEl); + parent.appendChild(mfgEl); + } } -void Ut_RemoteList::testSetContent() +void Ut_RemoteList::createModelElements(const QString &mfg, QDomElement &parent) { + for (int i = 0; i < validModels[mfg].size(); ++i) + { + Model m = validModels[mfg].at(i); + QDomElement modelEl = document.createElement("model"); + modelEl.setAttribute("file", m.file); + modelEl.setAttribute("name", m.name); + parent.appendChild(modelEl); + } } void Ut_RemoteList::testIsValid() { - RemoteList subject("remotes.xml"); + RemoteList subject(document); QCOMPARE(subject.isValid(), true); } -void Ut_RemoteList::testIsValid_InvalidFile() +void Ut_RemoteList::testIsValid_setContent() { - RemoteList subject("notxml"); + RemoteList subject; QCOMPARE(subject.isValid(), false); + subject.setContent(document); + QCOMPARE(subject.isValid(), true); } -void Ut_RemoteList::testIsValid_FileNotFound() +void Ut_RemoteList::testLetters() { - RemoteList subject("thisdoesnotexist"); - QCOMPARE(subject.isValid(), false); + RemoteList subject(document); + compareLetters(subject); } - -void Ut_RemoteList::testLetters() + +void Ut_RemoteList::testManufacturers() +{ + RemoteList subject(document); + compareManufacturers(subject); +} + +void Ut_RemoteList::testModels() +{ + RemoteList subject(document); + compareModels(subject); +} + +void Ut_RemoteList::testLetters_setContent() +{ + RemoteList subject; + subject.setContent(document); + compareLetters(subject); +} + +void Ut_RemoteList::testManufacturers_setContent() +{ + RemoteList subject; + subject.setContent(document); + compareManufacturers(subject); +} + +void Ut_RemoteList::testModels_setContent() +{ + RemoteList subject; + subject.setContent(document); + compareModels(subject); +} + +void Ut_RemoteList::compareLetters(RemoteList &subject) { - QStringList letters = RemoteList("remotes.xml").letters(); + QStringList letters = subject.letters(); QStringList validLetters = validMfgs.keys(); + QCOMPARE(validLetters.size(), letters.size()); for (int i = 0; i < letters.size(); ++i) @@ -82,10 +150,8 @@ void Ut_RemoteList::testLetters() } } -void Ut_RemoteList::testManufacturers() +void Ut_RemoteList::compareManufacturers(RemoteList &subject) { - RemoteList subject("remotes.xml"); - for (int i = 0; i < validMfgs.keys().size(); ++i) { QString letter = validMfgs.keys().at(i); @@ -97,10 +163,8 @@ void Ut_RemoteList::testManufacturers() } } -void Ut_RemoteList::testModels() +void Ut_RemoteList::compareModels(RemoteList &subject) { - RemoteList subject("remotes.xml"); - for (int i = 0; i < validModels.keys().size(); ++i) { QString mfg = validModels.keys().at(i); diff --git a/ut/ut_remotelist/ut_remotelist.h b/ut/ut_remotelist/ut_remotelist.h index 478ca28..7db7764 100644 --- a/ut/ut_remotelist/ut_remotelist.h +++ b/ut/ut_remotelist/ut_remotelist.h @@ -5,7 +5,10 @@ #include #include #include +#include + #include "model.h" +#include "remotelist.h" class Ut_RemoteList: public QObject { @@ -14,19 +17,32 @@ class Ut_RemoteList: public QObject private slots: void initTestCase(); void cleanupTestCase(); - void init(); - - void testSetContent(); + void testIsValid(); - void testIsValid_InvalidFile(); - void testIsValid_FileNotFound(); + void testIsValid_setContent(); void testLetters(); void testManufacturers(); void testModels(); + void testLetters_setContent(); + void testManufacturers_setContent(); + void testModels_setContent(); + +private: + // Creates the xml document containing the test data + void createDocument(); + + // Creates element for each mfg which name starts with the given letter. + void createMfgElements(QChar &letter, QDomElement &parent); + void createModelElements(const QString &mfg, QDomElement &parent); + + void compareLetters(RemoteList &subject); + void compareManufacturers(RemoteList &subject); + void compareModels(RemoteList &subject); private: - QMap validMfgs; // letter -- mfgs - QMap > validModels; // mfg ----- models + QDomDocument document; + QMap validMfgs; + QMap > validModels; }; #endif // UT_REMOTELIST_H