qtguitest.cpp 9.1 KB

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