qtguitest.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 QtTypesQtGuiTest : public ::testing::Test
  39. {
  40. public:
  41. // see simpletest.proto for property names and their field indices
  42. QtTypesQtGuiTest() {
  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> QtTypesQtGuiTest::serializer;
  52. TEST_F(QtTypesQtGuiTest, QColorTest)
  53. {
  54. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QColorMessage, QColor>(1, "QColor", "testField");
  55. qtprotobufnamespace::qttypes::tests::QColorMessage msg;
  56. msg.setTestField(QColor("red"));
  57. auto result = msg.serialize(serializer.get());
  58. EXPECT_TRUE(QByteArray::fromHex("0a06088080fcff0f") == result);
  59. msg.setTestField({});
  60. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0608808082f80f"));
  61. EXPECT_EQ(QColor("green"), msg.testField());
  62. }
  63. TEST_F(QtTypesQtGuiTest, QMatrix4x4Test)
  64. {
  65. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QMatrix4x4Message, QMatrix4x4>(1, "QMatrix4x4", "testField");
  66. qtprotobufnamespace::qttypes::tests::QMatrix4x4Message msg;
  67. msg.setTestField({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
  68. auto result = msg.serialize(serializer.get());
  69. EXPECT_TRUE(QByteArray::fromHex("0a518501000070417d0000604175000050416d0000404165000030415d0000204155000010414d00000041450000e0403d0000c040350000a0402d0000804025000040401d00000040150000803f0d00000000") == result
  70. || QByteArray::fromHex("0a4c150000803f1d0000004025000040402d00008040350000a0403d0000c040450000e0404d0000004155000010415d0000204165000030416d0000404175000050417d00006041850100007041") == result
  71. || QByteArray::fromHex("0a514d000000410d00000000150000803f1d0000004025000040402d00008040350000a0403d0000c040450000e04055000010415d0000204165000030416d0000404175000050417d00006041850100007041") == result);
  72. msg.setTestField({});
  73. msg.deserialize(serializer.get(), QByteArray::fromHex("0a4c150000803f1d0000004025000040402d00008040350000a0403d0000c040450000e0404d0000004155000010415d0000204165000030416d0000404175000050417d00006041850100007041"));
  74. EXPECT_EQ(QMatrix4x4(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), msg.testField());
  75. }
  76. TEST_F(QtTypesQtGuiTest, QVector2DTest)
  77. {
  78. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QVector2DMessage, QVector2D>(1, "QVector2D", "testField");
  79. qtprotobufnamespace::qttypes::tests::QVector2DMessage msg;
  80. msg.setTestField(QVector2D(42, 24));
  81. auto result = msg.serialize(serializer.get());
  82. EXPECT_TRUE(QByteArray::fromHex("0a0a150000c0410d00002842") == result
  83. || QByteArray::fromHex("0a0a0d00002842150000c041") == result);
  84. msg.setTestField({});
  85. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0a0d0000c0411500002842"));
  86. EXPECT_EQ(QVector2D(24, 42), msg.testField());
  87. }
  88. TEST_F(QtTypesQtGuiTest, QVector3DTest)
  89. {
  90. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QVector3DMessage, QVector3D>(1, "QVector3D", "testField");
  91. qtprotobufnamespace::qttypes::tests::QVector3DMessage msg;
  92. msg.setTestField(QVector3D(42, 24, 11));
  93. auto result = msg.serialize(serializer.get());
  94. EXPECT_TRUE(QByteArray::fromHex("0a0f1d00003041150000c0410d00002842") == result
  95. || QByteArray::fromHex("0a0f0d00002842150000c0411d00003041") == result);
  96. msg.setTestField({});
  97. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0f0d0000c04115000030411d00002842"));
  98. EXPECT_EQ(QVector3D(24, 11, 42), msg.testField());
  99. }
  100. TEST_F(QtTypesQtGuiTest, QVector4DTest)
  101. {
  102. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QVector4DMessage, QVector4D>(1, "QVector4D", "testField");
  103. qtprotobufnamespace::qttypes::tests::QVector4DMessage msg;
  104. msg.setTestField({24, 11, 42, 0});
  105. auto result = msg.serialize(serializer.get());
  106. EXPECT_TRUE(QByteArray::fromHex("0a1425000000001d0000284215000030410d0000c041") == result
  107. || QByteArray::fromHex("0a0f0d0000c04115000030411d00002842") == result
  108. || QByteArray::fromHex("0a140d0000c04115000030411d000028422500000000") == result);
  109. msg.setTestField({});
  110. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0f0d0000c04115000030411d00002842"));
  111. EXPECT_EQ(QVector4D(24, 11, 42, 0), msg.testField());
  112. }
  113. TEST_F(QtTypesQtGuiTest, QTransformTest)
  114. {
  115. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QTransformMessage, QTransform>(1, "QTransform", "testField");
  116. qtprotobufnamespace::qttypes::tests::QTransformMessage msg;
  117. msg.setTestField(QTransform(0, 1, 2, 3, 4, 5, 6, 7, 8));
  118. auto result = msg.serialize(serializer.get());
  119. EXPECT_TRUE(QByteArray::fromHex("0a51490000000000002040410000000000001c4039000000000000184031000000000000144029000000000000104021000000000000084019000000000000004011000000000000f03f090000000000000000") == result
  120. || QByteArray::fromHex("0a4811000000000000f03f190000000000000040210000000000000840290000000000001040310000000000001440390000000000001840410000000000001c40490000000000002040") == result
  121. || QByteArray::fromHex("0a5149000000000000204009000000000000000011000000000000f03f190000000000000040210000000000000840290000000000001040310000000000001440390000000000001840410000000000001c40") == result);
  122. msg.setTestField({});
  123. msg.deserialize(serializer.get(), QByteArray::fromHex("0a48090000000000002040110000000000001c4019000000000000184021000000000000144029000000000000104031000000000000084039000000000000004041000000000000f03f"));
  124. EXPECT_EQ(QTransform(8, 7, 6, 5, 4, 3, 2, 1, 0), msg.testField());
  125. }
  126. TEST_F(QtTypesQtGuiTest, QQuaternionTest)
  127. {
  128. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QQuaternionMessage, QQuaternion>(1, "QQuaternion", "testField");
  129. qtprotobufnamespace::qttypes::tests::QQuaternionMessage msg;
  130. msg.setTestField(QQuaternion(14, 10, 24, 22));
  131. auto result = msg.serialize(serializer.get());
  132. EXPECT_TRUE(QByteArray::fromHex("0a16120f1d0000b041150000c0410d000020410d00006041") ==
  133. result || QByteArray::fromHex("0a160d00006041120f0d00002041150000c0411d0000b041") == result);
  134. msg.setTestField({});
  135. msg.deserialize(serializer.get(), QByteArray::fromHex("0a160d00006041120f0d00002041150000c0411d0000b041"));
  136. EXPECT_EQ(QQuaternion(14, 10, 24, 22), msg.testField());
  137. }
  138. TEST_F(QtTypesQtGuiTest, QImageTest)
  139. {
  140. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QImageMessage, QImage>(1, "QImage", "testField");
  141. qtprotobufnamespace::qttypes::tests::QImageMessage msg;
  142. QImage initialImage("./testimage.png");
  143. msg.setTestField(QImage("./testimage.png"));
  144. ASSERT_FALSE(msg.testField().isNull());
  145. auto result = msg.serialize(serializer.get());
  146. msg.setTestField({});
  147. msg.deserialize(serializer.get(), result);
  148. EXPECT_EQ(initialImage, msg.testField());
  149. }
  150. }
  151. }