qtguitest.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. || QByteArray::fromHex("0a510d00000000150000803f1d0000004025000040402d00008040350000a0403d0000c040450000e0404d0000004155000010415d0000204165000030416d0000404175000050417d00006041850100007041") == result);
  73. msg.setTestField({});
  74. msg.deserialize(serializer.get(), QByteArray::fromHex("0a4c150000803f1d0000004025000040402d00008040350000a0403d0000c040450000e0404d0000004155000010415d0000204165000030416d0000404175000050417d00006041850100007041"));
  75. EXPECT_EQ(QMatrix4x4(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15), msg.testField());
  76. }
  77. TEST_F(QtTypesQtGuiTest, QVector2DTest)
  78. {
  79. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QVector2DMessage, QVector2D>(1, "QVector2D", "testField");
  80. qtprotobufnamespace::qttypes::tests::QVector2DMessage msg;
  81. msg.setTestField(QVector2D(42, 24));
  82. auto result = msg.serialize(serializer.get());
  83. EXPECT_TRUE(QByteArray::fromHex("0a0a150000c0410d00002842") == result
  84. || QByteArray::fromHex("0a0a0d00002842150000c041") == result);
  85. msg.setTestField({});
  86. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0a0d0000c0411500002842"));
  87. EXPECT_EQ(QVector2D(24, 42), msg.testField());
  88. }
  89. TEST_F(QtTypesQtGuiTest, QVector3DTest)
  90. {
  91. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QVector3DMessage, QVector3D>(1, "QVector3D", "testField");
  92. qtprotobufnamespace::qttypes::tests::QVector3DMessage msg;
  93. msg.setTestField(QVector3D(42, 24, 11));
  94. auto result = msg.serialize(serializer.get());
  95. EXPECT_TRUE(QByteArray::fromHex("0a0f1d00003041150000c0410d00002842") == result
  96. || QByteArray::fromHex("0a0f0d00002842150000c0411d00003041") == result);
  97. msg.setTestField({});
  98. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0f0d0000c04115000030411d00002842"));
  99. EXPECT_EQ(QVector3D(24, 11, 42), msg.testField());
  100. }
  101. TEST_F(QtTypesQtGuiTest, QVector4DTest)
  102. {
  103. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QVector4DMessage, QVector4D>(1, "QVector4D", "testField");
  104. qtprotobufnamespace::qttypes::tests::QVector4DMessage msg;
  105. msg.setTestField({24, 11, 42, 0});
  106. auto result = msg.serialize(serializer.get());
  107. EXPECT_TRUE(QByteArray::fromHex("0a1425000000001d0000284215000030410d0000c041") == result
  108. || QByteArray::fromHex("0a0f0d0000c04115000030411d00002842") == result
  109. || QByteArray::fromHex("0a140d0000c04115000030411d000028422500000000") == result);
  110. msg.setTestField({});
  111. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0f0d0000c04115000030411d00002842"));
  112. EXPECT_EQ(QVector4D(24, 11, 42, 0), msg.testField());
  113. }
  114. TEST_F(QtTypesQtGuiTest, QTransformTest)
  115. {
  116. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QTransformMessage, QTransform>(1, "QTransform", "testField");
  117. qtprotobufnamespace::qttypes::tests::QTransformMessage msg;
  118. msg.setTestField(QTransform(0, 1, 2, 3, 4, 5, 6, 7, 8));
  119. auto result = msg.serialize(serializer.get());
  120. EXPECT_TRUE(QByteArray::fromHex("0a51490000000000002040410000000000001c4039000000000000184031000000000000144029000000000000104021000000000000084019000000000000004011000000000000f03f090000000000000000") == result
  121. || QByteArray::fromHex("0a4811000000000000f03f190000000000000040210000000000000840290000000000001040310000000000001440390000000000001840410000000000001c40490000000000002040") == result
  122. || QByteArray::fromHex("0a5149000000000000204009000000000000000011000000000000f03f190000000000000040210000000000000840290000000000001040310000000000001440390000000000001840410000000000001c40") == result
  123. || QByteArray::fromHex("0a5109000000000000000011000000000000f03f190000000000000040210000000000000840290000000000001040310000000000001440390000000000001840410000000000001c40490000000000002040") == result);
  124. msg.setTestField({});
  125. msg.deserialize(serializer.get(), QByteArray::fromHex("0a48090000000000002040110000000000001c4019000000000000184021000000000000144029000000000000104031000000000000084039000000000000004041000000000000f03f"));
  126. EXPECT_EQ(QTransform(8, 7, 6, 5, 4, 3, 2, 1, 0), msg.testField());
  127. }
  128. TEST_F(QtTypesQtGuiTest, QQuaternionTest)
  129. {
  130. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QQuaternionMessage, QQuaternion>(1, "QQuaternion", "testField");
  131. qtprotobufnamespace::qttypes::tests::QQuaternionMessage msg;
  132. msg.setTestField(QQuaternion(14, 10, 24, 22));
  133. auto result = msg.serialize(serializer.get());
  134. EXPECT_TRUE(QByteArray::fromHex("0a16120f1d0000b041150000c0410d000020410d00006041") ==
  135. result || QByteArray::fromHex("0a160d00006041120f0d00002041150000c0411d0000b041") == result);
  136. msg.setTestField({});
  137. msg.deserialize(serializer.get(), QByteArray::fromHex("0a160d00006041120f0d00002041150000c0411d0000b041"));
  138. EXPECT_EQ(QQuaternion(14, 10, 24, 22), msg.testField());
  139. }
  140. TEST_F(QtTypesQtGuiTest, QImageTest)
  141. {
  142. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QImageMessage, QImage>(1, "QImage", "testField");
  143. qtprotobufnamespace::qttypes::tests::QImageMessage msg;
  144. QImage initialImage("./testimage.png");
  145. msg.setTestField(QImage("./testimage.png"));
  146. ASSERT_FALSE(msg.testField().isNull());
  147. auto result = msg.serialize(serializer.get());
  148. msg.setTestField({});
  149. msg.deserialize(serializer.get(), result);
  150. EXPECT_EQ(initialImage, msg.testField());
  151. }
  152. }
  153. }