simpletest.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #include <QVariantList>
  26. #include <QMetaProperty>
  27. #include <QSignalSpy>
  28. #include <QProtobufSerializer>
  29. #include <QProtobufJsonSerializer>
  30. #include <QDateTime>
  31. #include <stdio.h>
  32. #include <iostream>
  33. #include <gtest/gtest.h>
  34. #include "../testscommon.h"
  35. #include "qttypes.qpb.h"
  36. namespace QtProtobuf {
  37. namespace tests {
  38. class QtTypesTest : public ::testing::Test
  39. {
  40. public:
  41. // see simpletest.proto for property names and their field indices
  42. QtTypesTest() {
  43. }
  44. static void SetUpTestCase() {
  45. QtProtobuf::qRegisterProtobufTypes();
  46. QtProtobuf::qRegisterProtobufQtTypes();
  47. serializer.reset(new QProtobufSerializer);
  48. }
  49. static std::unique_ptr<QProtobufSerializer> serializer;
  50. };
  51. std::unique_ptr<QProtobufSerializer> QtTypesTest::serializer;
  52. TEST_F(QtTypesTest, QUrlTest)
  53. {
  54. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QUrlMessage, QUrl>(1, "QUrl", "testField");
  55. qtprotobufnamespace::qttypes::tests::QUrlMessage msg;
  56. msg.setTestField(QUrl("https://github.com/semlanik/qtprotobuf"));
  57. EXPECT_TRUE(QByteArray::fromHex("0a280a2668747470733a2f2f6769746875622e636f6d2f73656d6c616e696b2f717470726f746f627566") ==
  58. msg.serialize(serializer.get()));
  59. msg.setTestField({});
  60. msg.deserialize(serializer.get(), QByteArray::fromHex("0a1d0a1b68747470733a2f2f6769746875622e636f6d2f73656d6c616e696b"));
  61. EXPECT_STREQ("https://github.com/semlanik", msg.testField().url().toStdString().c_str());
  62. }
  63. TEST_F(QtTypesTest, QCharTest)
  64. {
  65. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QCharMessage, QChar>(1, "QChar", "testField");
  66. qtprotobufnamespace::qttypes::tests::QCharMessage msg;
  67. msg.setTestField(QChar('q'));
  68. EXPECT_TRUE(QByteArray::fromHex("0a040a020071") ==
  69. msg.serialize(serializer.get()));
  70. msg.setTestField({});
  71. msg.deserialize(serializer.get(), QByteArray::fromHex("0a040a0220ac"));
  72. EXPECT_TRUE(QChar(8364) == msg.testField());
  73. }
  74. TEST_F(QtTypesTest, QUuidTest)
  75. {
  76. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QUuidMessage, QUuid>(1, "QUuid", "testField");
  77. qtprotobufnamespace::qttypes::tests::QUuidMessage msg;
  78. msg.setTestField(QUuid("{4bcbcdc3-c5b3-4d34-97fe-af78c825cc7d}"));
  79. EXPECT_TRUE(QByteArray::fromHex("0a280a267b34626362636463332d633562332d346433342d393766652d6166373863383235636337647d") ==
  80. msg.serialize(serializer.get()));
  81. msg.setTestField({});
  82. msg.deserialize(serializer.get(), QByteArray::fromHex("0a280a267b34626362636463332d633562332d346433342d393766652d6166373863383235636337647d"));
  83. EXPECT_TRUE(QUuid("{4bcbcdc3-c5b3-4d34-97fe-af78c825cc7d}") == msg.testField());
  84. }
  85. TEST_F(QtTypesTest, QTimeTest)
  86. {
  87. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QTimeMessage, QTime>(1, "QTime", "testField");
  88. qtprotobufnamespace::qttypes::tests::QTimeMessage msg;
  89. msg.setTestField(QTime(5, 30, 48, 123));
  90. QByteArray result = msg.serialize(serializer.get());
  91. EXPECT_TRUE(QByteArray::fromHex("0a09080a103c186020f601") ==
  92. result || QByteArray::fromHex("0a0920f6011860103c080a") == result);
  93. msg.setTestField({});
  94. msg.deserialize(serializer.get(), QByteArray::fromHex("0a09080e103c1824208205"));
  95. EXPECT_EQ(msg.testField().hour(), 7);
  96. EXPECT_EQ(msg.testField().minute(), 30);
  97. EXPECT_EQ(msg.testField().second(), 18);
  98. EXPECT_EQ(msg.testField().msec(), 321);
  99. }
  100. TEST_F(QtTypesTest, QDateTest)
  101. {
  102. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QDateMessage, QDate>(1, "QDate", "testField");
  103. qtprotobufnamespace::qttypes::tests::QDateMessage msg;
  104. msg.setTestField(QDate(1856, 6, 10));
  105. QByteArray result = msg.serialize(serializer.get());
  106. EXPECT_TRUE(QByteArray::fromHex("0a0708801d100c1814") ==
  107. result || QByteArray::fromHex("0a071814100c08801d") == result);
  108. msg.setTestField({});
  109. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0708d21d1006181c"));
  110. EXPECT_EQ(msg.testField().year(), 1897);
  111. EXPECT_EQ(msg.testField().month(), 3);
  112. EXPECT_EQ(msg.testField().day(), 14);
  113. }
  114. TEST_F(QtTypesTest, QDateTimeTest)
  115. {
  116. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QDateTimeMessage, QDateTime>(1, "QDateTime", "testField");
  117. qtprotobufnamespace::qttypes::tests::QDateTimeMessage msg;
  118. msg.setTestField({QDate(1897, 3, 14), QTime(7, 30, 12, 321)});
  119. QByteArray result = msg.serialize(serializer.get());
  120. EXPECT_TRUE(QByteArray::fromHex("0a1412092082051818103c080e0a07181c100608d21d") ==
  121. result || QByteArray::fromHex("0a140a0708d21d1006181c1209080e103c1818208205") == result);
  122. msg.setTestField({});
  123. msg.deserialize(serializer.get(), QByteArray::fromHex("0a140a0708d21d1006181c1209080e103c1818208205"));
  124. EXPECT_TRUE(msg.testField() == QDateTime(QDate(1897, 3, 14), QTime(7, 30, 12, 321)));
  125. msg.deserialize(serializer.get(), QByteArray::fromHex("0a140a0708801d100c18141209080a103c186020f601"));
  126. EXPECT_TRUE(msg.testField() == QDateTime(QDate(1856, 6, 10), QTime(5, 30, 48, 123)));
  127. }
  128. TEST_F(QtTypesTest, QSizeTest)
  129. {
  130. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QSizeMessage, QSize>(1, "QSize", "testField");
  131. qtprotobufnamespace::qttypes::tests::QSizeMessage msg;
  132. msg.setTestField({1024, 768});
  133. QByteArray result = msg.serialize(serializer.get());
  134. EXPECT_TRUE(QByteArray::fromHex("0a0610800c088010") ==
  135. result || QByteArray::fromHex("0a0608801010800c") == result);
  136. msg.setTestField({});
  137. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0608801010800c"));
  138. EXPECT_EQ(msg.testField().width(), 1024);
  139. EXPECT_EQ(msg.testField().height(), 768);
  140. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0608800c108010"));
  141. EXPECT_EQ(msg.testField().width(), 768);
  142. EXPECT_EQ(msg.testField().height(), 1024);
  143. }
  144. TEST_F(QtTypesTest, QSizeFTest)
  145. {
  146. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QSizeFMessage, QSizeF>(1, "QSizeF", "testField");
  147. qtprotobufnamespace::qttypes::tests::QSizeFMessage msg;
  148. msg.setTestField({1024.0, 768.0});
  149. QByteArray result = msg.serialize(serializer.get());
  150. EXPECT_TRUE(QByteArray::fromHex("0a12110000000000008840090000000000009040") ==
  151. result || QByteArray::fromHex("0a12090000000000009040110000000000008840") == result);
  152. msg.setTestField({});
  153. msg.deserialize(serializer.get(), QByteArray::fromHex("0a12090000000000009040110000000000008840"));
  154. EXPECT_EQ(msg.testField().width(), 1024.0);
  155. EXPECT_EQ(msg.testField().height(), 768.0);
  156. msg.deserialize(serializer.get(), QByteArray::fromHex("0a12090000000000008840110000000000009040"));
  157. EXPECT_EQ(msg.testField().width(), 768.0);
  158. EXPECT_EQ(msg.testField().height(), 1024.0);
  159. }
  160. TEST_F(QtTypesTest, QPointTest)
  161. {
  162. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QPointMessage, QPoint>(1, "QPoint", "testField");
  163. qtprotobufnamespace::qttypes::tests::QPointMessage msg;
  164. msg.setTestField({1024, 768});
  165. QByteArray result = msg.serialize(serializer.get());
  166. EXPECT_TRUE(QByteArray::fromHex("0a0610800c088010") ==
  167. result || QByteArray::fromHex("0a0608801010800c") == result);
  168. msg.setTestField({});
  169. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0608801010800c"));
  170. EXPECT_EQ(msg.testField().x(), 1024);
  171. EXPECT_EQ(msg.testField().y(), 768);
  172. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0608800c108010"));
  173. EXPECT_EQ(msg.testField().x(), 768);
  174. EXPECT_EQ(msg.testField().y(), 1024);
  175. }
  176. TEST_F(QtTypesTest, QPointFTest)
  177. {
  178. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QPointFMessage, QPointF>(1, "QPointF", "testField");
  179. qtprotobufnamespace::qttypes::tests::QPointFMessage msg;
  180. msg.setTestField({1024.0, 768.0});
  181. QByteArray result = msg.serialize(serializer.get());
  182. EXPECT_TRUE(QByteArray::fromHex("0a12110000000000008840090000000000009040") ==
  183. result || QByteArray::fromHex("0a12090000000000009040110000000000008840") == result);
  184. msg.setTestField({});
  185. msg.deserialize(serializer.get(), QByteArray::fromHex("0a12090000000000009040110000000000008840"));
  186. EXPECT_EQ(msg.testField().x(), 1024.0);
  187. EXPECT_EQ(msg.testField().y(), 768.0);
  188. msg.deserialize(serializer.get(), QByteArray::fromHex("0a12090000000000008840110000000000009040"));
  189. EXPECT_EQ(msg.testField().x(), 768.0);
  190. EXPECT_EQ(msg.testField().y(), 1024.0);
  191. }
  192. TEST_F(QtTypesTest, QRectTest)
  193. {
  194. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QRectMessage, QRect>(1, "QRect", "testField");
  195. qtprotobufnamespace::qttypes::tests::QRectMessage msg;
  196. msg.setTestField({QPoint(768, 768), QPoint(1024, 1024)});
  197. QByteArray result = msg.serialize(serializer.get());
  198. EXPECT_TRUE(QByteArray::fromHex("0a1012061080100880100a0610800c08800c") ==
  199. result || QByteArray::fromHex("0a100a0608800c10800c1206088010108010") == result);
  200. msg.setTestField({});
  201. msg.deserialize(serializer.get(), QByteArray::fromHex("0a100a0608800c10800c1206088010108010"));
  202. EXPECT_EQ(msg.testField().x(), 768);
  203. EXPECT_EQ(msg.testField().y(), 768);
  204. EXPECT_EQ(msg.testField().width(), 257); //WTF Qt, siriously why?
  205. EXPECT_EQ(msg.testField().height(), 257); //WTF Qt, siriously why?
  206. msg.setTestField({QPoint(0, 0), QPoint(1024, 768)});
  207. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0a0a00120608801010800c"));
  208. EXPECT_EQ(msg.testField().x(), 0);
  209. EXPECT_EQ(msg.testField().y(), 0);
  210. EXPECT_EQ(msg.testField().width(), 1025); //WTF Qt, siriously why?
  211. EXPECT_EQ(msg.testField().height(), 769); //WTF Qt, siriously why?
  212. }
  213. TEST_F(QtTypesTest, QRectFTest)
  214. {
  215. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QRectFMessage, QRectF>(1, "QRectF", "testField");
  216. qtprotobufnamespace::qttypes::tests::QRectFMessage msg;
  217. msg.setTestField({QPointF(768.0, 768.0), QPointF(1024.0, 1024.0)});
  218. QByteArray result = msg.serialize(serializer.get());
  219. EXPECT_TRUE(QByteArray::fromHex("0a2812121100000000000090400900000000000090400a12110000000000008840090000000000008840") ==
  220. result || QByteArray::fromHex("0a280a120900000000000088401100000000000088401212090000000000009040110000000000009040") == result);
  221. msg.setTestField({});
  222. msg.deserialize(serializer.get(), QByteArray::fromHex("0a280a120900000000000088401100000000000088401212090000000000009040110000000000009040"));
  223. EXPECT_EQ(msg.testField().x(), 768.0);
  224. EXPECT_EQ(msg.testField().y(), 768.0);
  225. EXPECT_EQ(msg.testField().width(), 256.0);
  226. EXPECT_EQ(msg.testField().height(), 256.0);
  227. msg.setTestField({QPointF(0, 0), QPointF(1024.0, 768.0)});
  228. msg.deserialize(serializer.get(), QByteArray::fromHex("0a160a001212090000000000009040110000000000008840"));
  229. EXPECT_EQ(msg.testField().x(), 0.0);
  230. EXPECT_EQ(msg.testField().y(), 0.0);
  231. EXPECT_EQ(msg.testField().width(), 1024.0);
  232. EXPECT_EQ(msg.testField().height(), 768.0);
  233. }
  234. TEST_F(QtTypesTest, QPolygonTest)
  235. {
  236. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QPolygonMessage, QPolygon>(1, "QPolygon", "testField");
  237. qtprotobufnamespace::qttypes::tests::QPolygonMessage msg;
  238. msg.setTestField(QPolygon({QPoint(10, 0), QPoint(20, 20), QPoint(0, 20)}));
  239. QByteArray result = msg.serialize(serializer.get());
  240. EXPECT_TRUE(QByteArray::fromHex("0a0e0a0208140a04102808280a021028") ==
  241. result || QByteArray::fromHex("0a0e0a0208140a04082810280a021028") == result);
  242. msg.setTestField({});
  243. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0e0a0208140a04082810280a021028"));
  244. EXPECT_EQ(msg.testField().size(), 3);
  245. EXPECT_EQ(msg.testField()[0], QPoint(10, 0));
  246. EXPECT_EQ(msg.testField()[1], QPoint(20, 20));
  247. EXPECT_EQ(msg.testField()[2], QPoint(0, 20));
  248. }
  249. TEST_F(QtTypesTest, QPolygonFTest)
  250. {
  251. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QPolygonFMessage, QPolygonF>(1, "QPolygonF", "testField");
  252. qtprotobufnamespace::qttypes::tests::QPolygonFMessage msg;
  253. msg.setTestField(QPolygonF({QPointF(10, 0), QPointF(20, 20), QPointF(0, 20)}));
  254. QByteArray result = msg.serialize(serializer.get());
  255. EXPECT_TRUE(QByteArray::fromHex("0a3c0a121100000000000000000900000000000024400a121100000000000034400900000000000034400a12110000000000003440090000000000000000") ==
  256. result || QByteArray::fromHex("0a3c0a090900000000000024400a120900000000000034401100000000000034400a09110000000000003440") == result);
  257. msg.setTestField({});
  258. msg.deserialize(serializer.get(), QByteArray::fromHex("0a2a0a090900000000000024400a120900000000000034401100000000000034400a09110000000000003440"));
  259. EXPECT_EQ(msg.testField().size(), 3);
  260. EXPECT_EQ(msg.testField()[0], QPointF(10, 0));
  261. EXPECT_EQ(msg.testField()[1], QPointF(20, 20));
  262. EXPECT_EQ(msg.testField()[2], QPointF(0, 20));
  263. }
  264. }
  265. }