Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
Weixuan Xiao
kdeconnect-kde
Commits
17d0fd49
Commit
17d0fd49
authored
Jul 04, 2013
by
Albert Vaca Cintora
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added unit tests for n tests for networkpackage serialization
parent
b1e4e2d8
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
67 additions
and
3 deletions
+67
-3
daemon/networkpackage.cpp
daemon/networkpackage.cpp
+8
-2
daemon/networkpackage.h
daemon/networkpackage.h
+1
-1
test/CMakeLists.txt
test/CMakeLists.txt
+2
-0
test/backendtests.cpp
test/backendtests.cpp
+52
-0
test/backendtests.h
test/backendtests.h
+4
-0
No files found.
daemon/networkpackage.cpp
View file @
17d0fd49
...
...
@@ -33,6 +33,7 @@ NetworkPackage::NetworkPackage(QString type)
{
mId
=
time
(
NULL
);
mType
=
type
;
mVersion
=
1
;
}
QByteArray
NetworkPackage
::
serialize
()
const
...
...
@@ -48,7 +49,7 @@ QByteArray NetworkPackage::serialize() const
bool
ok
;
QJson
::
Serializer
serializer
;
QByteArray
json
=
serializer
.
serialize
(
variant
,
&
ok
);
if
(
!
ok
)
qDebug
()
<<
"
D'oh!"
;
if
(
!
ok
)
qDebug
()
<<
"
Serialization error:"
<<
serializer
.
errorMessage
()
;
return
json
;
}
...
...
@@ -57,7 +58,12 @@ void NetworkPackage::unserialize(QByteArray a, NetworkPackage* np)
{
//Json -> QVariant
QJson
::
Parser
parser
;
QVariantMap
variant
=
parser
.
parse
(
a
).
toMap
();
bool
ok
;
QVariantMap
variant
=
parser
.
parse
(
a
,
&
ok
).
toMap
();
if
(
!
ok
)
{
qDebug
()
<<
"Unserialization error:"
<<
parser
.
errorLine
()
<<
parser
.
errorString
();
np
->
setVersion
(
-
1
);
}
//QVariant -> Object
//NetworkPackage np;
...
...
daemon/networkpackage.h
View file @
17d0fd49
...
...
@@ -52,7 +52,7 @@ public:
template
<
typename
T
>
T
get
(
const
QString
&
property
,
const
T
&
defaultValue
=
default_arg
<
T
>::
get
())
const
{
return
mBody
.
value
(
property
,
defaultValue
).
template
value
<
T
>();
//Important note: Awesome template syntax is awesome
}
template
<
typename
T
>
void
set
(
const
QString
&
property
,
const
T
&
value
)
const
{
return
mBody
[
property
]
.
setValue
(
value
)
;
}
template
<
typename
T
>
void
set
(
const
QString
&
property
,
const
T
&
value
)
{
mBody
[
property
]
=
value
;
}
private:
void
setId
(
long
id
)
{
mId
=
id
;
}
...
...
test/CMakeLists.txt
View file @
17d0fd49
set
(
kded_kdeconnect_tests_SRCS
../daemon/networkpackage.cpp
backendtests.cpp
)
...
...
@@ -8,6 +9,7 @@ target_link_libraries(kded_kdeconnect_tests
${
KDE4_KDECORE_LIBS
}
${
KDE4_KDEUI_LIBS
}
kdnssd
qjson
${
QT_QTTEST_LIBRARY
}
${
QT_QTNETWORK_LIBRARY
}
)
...
...
test/backendtests.cpp
View file @
17d0fd49
...
...
@@ -17,7 +17,10 @@
#include "backendtests.h"
#include "../daemon/networkpackage.h"
#include <qtest_kde.h>
#include <QtTest>
QTEST_KDEMAIN
(
BackendTests
,
NoGUI
);
...
...
@@ -26,6 +29,55 @@ void BackendTests::initTestCase()
// Called before the first testfunction is executed
}
void
BackendTests
::
dummyTest
()
{
QDate
date
;
date
.
setYMD
(
1967
,
3
,
11
);
QVERIFY
(
date
.
isValid
()
);
QCOMPARE
(
date
.
month
(),
3
);
QCOMPARE
(
QDate
::
longMonthName
(
date
.
month
()),
QString
(
"March"
)
);
}
void
BackendTests
::
networkPackageTest
()
{
NetworkPackage
np
(
"com.test"
);
np
.
set
(
"hello"
,
"hola"
);
QCOMPARE
(
(
np
.
get
<
QString
>
(
"hello"
,
"bye"
))
,
QString
(
"hola"
)
);
np
.
set
(
"hello"
,
""
);
QCOMPARE
(
(
np
.
get
<
QString
>
(
"hello"
,
"bye"
))
,
QString
(
""
)
);
np
.
body
().
remove
(
"hello"
);
QCOMPARE
(
(
np
.
get
<
QString
>
(
"hello"
,
"bye"
))
,
QString
(
"bye"
)
);
QByteArray
ba
=
np
.
serialize
();
//qDebug() << "Serialized package:" << ba;
NetworkPackage
np2
;
NetworkPackage
::
unserialize
(
ba
,
&
np2
);
QCOMPARE
(
np
.
id
(),
np2
.
id
()
);
QCOMPARE
(
np
.
type
(),
np2
.
type
()
);
QCOMPARE
(
np
.
version
(),
np2
.
version
()
);
QCOMPARE
(
np
.
body
(),
np2
.
body
()
);
QByteArray
json
(
"{
\"
id
\"
: 123,
\"
type
\"
:
\"
test
\"
,
\"
body
\"
: {
\"
testing
\"
: true },
\"
version
\"
: 3 }"
);
//qDebug() << json;
NetworkPackage
::
unserialize
(
json
,
&
np2
);
QCOMPARE
(
np2
.
id
(),
long
(
123
)
);
QCOMPARE
(
np2
.
version
(),
3
);
QCOMPARE
(
(
np2
.
get
<
bool
>
(
"testing"
)),
true
);
QCOMPARE
(
(
np2
.
get
<
bool
>
(
"not_testing"
)),
false
);
QCOMPARE
(
(
np2
.
get
<
bool
>
(
"not_testing"
,
true
)),
true
);
//NetworkPackage::unserialize("this is not json",&np2);
//QtTest::ignoreMessage(QtSystemMsg, "json_parser - syntax error found, forcing abort, Line 1 Column 0");
//QtTest::ignoreMessage(QtDebugMsg, "Unserialization error: 1 \"syntax error, unexpected string\"");
//QCOMPARE( np2.version(), -1 );
}
void
BackendTests
::
cleanupTestCase
()
{
// Called after the last testfunction was executed
...
...
test/backendtests.h
View file @
17d0fd49
...
...
@@ -26,6 +26,10 @@ class BackendTests : public QObject
private
Q_SLOTS
:
void
initTestCase
();
void
dummyTest
();
void
networkPackageTest
();
void
cleanupTestCase
();
void
init
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment