qtguitest.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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("0a0620fe0308fe03") ==
  59. result || QByteArray::fromHex("0a0608fe0320fe03") == result);
  60. msg.setTestField({});
  61. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0608fe0320fe03"));
  62. EXPECT_EQ(QColor("red"), msg.testField());
  63. }
  64. TEST_F(QtTypesQtGuiTest, QMatrix4x4Test)
  65. {
  66. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QMatrix4x4Message, QMatrix4x4>(1, "QMatrix4x4", "testField");
  67. qtprotobufnamespace::qttypes::tests::QMatrix4x4Message msg;
  68. msg.setTestField({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15});
  69. auto result = msg.serialize(serializer.get());
  70. EXPECT_TRUE(QByteArray::fromHex("0a518501000070417d0000604175000050416d0000404165000030415d0000204155000010414d00000041450000e0403d0000c040350000a0402d0000804025000040401d00000040150000803f0d00000000") ==
  71. result || QByteArray::fromHex("0a4c150000803f1d0000004025000040402d00008040350000a0403d0000c040450000e0404d0000004155000010415d0000204165000030416d0000404175000050417d00006041850100007041") == 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") ==
  83. 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") ==
  95. 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") ==
  107. result || QByteArray::fromHex("0a0f0d0000c04115000030411d00002842") == result);
  108. msg.setTestField({});
  109. msg.deserialize(serializer.get(), QByteArray::fromHex("0a0f0d0000c04115000030411d00002842"));
  110. EXPECT_EQ(QVector4D(24, 11, 42, 0), msg.testField());
  111. }
  112. TEST_F(QtTypesQtGuiTest, QTransformTest)
  113. {
  114. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QTransformMessage, QTransform>(1, "QTransform", "testField");
  115. qtprotobufnamespace::qttypes::tests::QTransformMessage msg;
  116. msg.setTestField(QTransform(0, 1, 2, 3, 4, 5, 6, 7, 8));
  117. auto result = msg.serialize(serializer.get());
  118. EXPECT_TRUE(QByteArray::fromHex("0a51490000000000002040410000000000001c4039000000000000184031000000000000144029000000000000104021000000000000084019000000000000004011000000000000f03f090000000000000000") ==
  119. result || QByteArray::fromHex("0a4811000000000000f03f190000000000000040210000000000000840290000000000001040310000000000001440390000000000001840410000000000001c40490000000000002040") == result);
  120. msg.setTestField({});
  121. msg.deserialize(serializer.get(), QByteArray::fromHex("0a48090000000000002040110000000000001c4019000000000000184021000000000000144029000000000000104031000000000000084039000000000000004041000000000000f03f"));
  122. EXPECT_EQ(QTransform(8, 7, 6, 5, 4, 3, 2, 1, 0), msg.testField());
  123. }
  124. TEST_F(QtTypesQtGuiTest, QQuaternionTest)
  125. {
  126. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QQuaternionMessage, QQuaternion>(1, "QQuaternion", "testField");
  127. qtprotobufnamespace::qttypes::tests::QQuaternionMessage msg;
  128. msg.setTestField(QQuaternion(14, 10, 24, 22));
  129. auto result = msg.serialize(serializer.get());
  130. EXPECT_TRUE(QByteArray::fromHex("0a16120f1d0000b041150000c0410d000020410d00006041") ==
  131. result || QByteArray::fromHex("0a160d00006041120f0d00002041150000c0411d0000b041") == result);
  132. msg.setTestField({});
  133. msg.deserialize(serializer.get(), QByteArray::fromHex("0a160d00006041120f0d00002041150000c0411d0000b041"));
  134. EXPECT_EQ(QQuaternion(14, 10, 24, 22), msg.testField());
  135. }
  136. TEST_F(QtTypesQtGuiTest, QImageTest)
  137. {
  138. assertMessagePropertyRegistered<qtprotobufnamespace::qttypes::tests::QImageMessage, QImage>(1, "QImage", "testField");
  139. qtprotobufnamespace::qttypes::tests::QImageMessage msg;
  140. QImage initialImage("./testimage.png");
  141. msg.setTestField(QImage("./testimage.png"));
  142. ASSERT_FALSE(msg.testField().isNull());
  143. auto result = msg.serialize(serializer.get());
  144. msg.setTestField({});
  145. msg.deserialize(serializer.get(), result);
  146. EXPECT_EQ(initialImage, msg.testField());
  147. }
  148. }
  149. }