simpletest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 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 <QDateTime>
  30. #include <stdio.h>
  31. #include <iostream>
  32. #include <gtest/gtest.h>
  33. #include <google/protobuf/any.qpb.h>
  34. #include <google/protobuf/api.qpb.h>
  35. #include <google/protobuf/type.qpb.h>
  36. #include <google/protobuf/struct.qpb.h>
  37. #include <google/protobuf/empty.qpb.h>
  38. #include <google/protobuf/duration.qpb.h>
  39. #include <google/protobuf/wrappers.qpb.h>
  40. #include <google/protobuf/field_mask.qpb.h>
  41. #include "wellknowntypes.qpb.h"
  42. #include "../testscommon.h"
  43. using namespace google::protobuf;
  44. namespace QtProtobuf {
  45. namespace tests {
  46. class WellknowntypesTest : public ::testing::Test
  47. {
  48. public:
  49. // see simpletest.proto for property names and their field indices
  50. WellknowntypesTest() {
  51. }
  52. static void SetUpTestCase() {
  53. QtProtobuf::qRegisterProtobufTypes();
  54. serializer.reset(new QProtobufSerializer);
  55. }
  56. static std::unique_ptr<QProtobufSerializer> serializer;
  57. };
  58. std::unique_ptr<QProtobufSerializer> WellknowntypesTest::serializer;
  59. TEST_F(WellknowntypesTest, AnyTest)
  60. {
  61. ASSERT_GT(qMetaTypeId<Any>(), 0);
  62. assertMessagePropertyRegistered<Any, QString>(1, "QString", "typeUrl");
  63. assertMessagePropertyRegistered<Any, QByteArray>(2, "QByteArray", "value");
  64. }
  65. TEST_F(WellknowntypesTest, ApiTest)
  66. {
  67. ASSERT_GT(qMetaTypeId<Api>(), 0);
  68. assertMessagePropertyRegistered<Api, QString>(1, "QString", "name");
  69. assertMessagePropertyRegistered<Api, MethodRepeated>(2, "google::protobuf::MethodRepeated", "methodsData");
  70. assertMessagePropertyRegistered<Api, OptionRepeated>(3, "google::protobuf::OptionRepeated", "optionsData");
  71. assertMessagePropertyRegistered<Api, QString>(4, "QString", "version");
  72. assertMessagePropertyRegistered<Api, SourceContext>(5, "google::protobuf::SourceContext*", "sourceContext", true);
  73. assertMessagePropertyRegistered<Api, MixinRepeated>(6, "google::protobuf::MixinRepeated", "mixinsData");
  74. assertMessagePropertyRegistered<Api, SyntaxGadget::Syntax>(7, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  75. }
  76. TEST_F(WellknowntypesTest, MethodTest)
  77. {
  78. ASSERT_GT(qMetaTypeId<Method>(), 0);
  79. assertMessagePropertyRegistered<Method, QString>(1, "QString", "name");
  80. assertMessagePropertyRegistered<Method, QString>(2, "QString", "requestTypeUrl");
  81. assertMessagePropertyRegistered<Method, bool>(3, "bool", "requestStreaming");
  82. assertMessagePropertyRegistered<Method, QString>(4, "QString", "responseTypeUrl");
  83. assertMessagePropertyRegistered<Method, bool>(5, "bool", "responseStreaming");
  84. assertMessagePropertyRegistered<Method, OptionRepeated>(6, "google::protobuf::OptionRepeated", "optionsData");
  85. assertMessagePropertyRegistered<Method, SyntaxGadget::Syntax>(7, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  86. }
  87. TEST_F(WellknowntypesTest, MixinTest)
  88. {
  89. ASSERT_GT(qMetaTypeId<Mixin>(), 0);
  90. assertMessagePropertyRegistered<Mixin, QString>(1, "QString", "name");
  91. assertMessagePropertyRegistered<Mixin, QString>(2, "QString", "root");
  92. }
  93. TEST_F(WellknowntypesTest, DurationTest)
  94. {
  95. ASSERT_GT(qMetaTypeId<Duration>(), 0);
  96. assertMessagePropertyRegistered<Duration, QtProtobuf::int64>(1, "QtProtobuf::int64", "seconds");
  97. assertMessagePropertyRegistered<Duration, QtProtobuf::int32>(2, "QtProtobuf::int32", "nanos_p");
  98. }
  99. TEST_F(WellknowntypesTest, EmptyTest)
  100. {
  101. ASSERT_GT(qMetaTypeId<Empty>(), 0);
  102. }
  103. TEST_F(WellknowntypesTest, FieldMaskTest)
  104. {
  105. ASSERT_GT(qMetaTypeId<FieldMask>(), 0);
  106. assertMessagePropertyRegistered<FieldMask, QStringList>(1, "QStringList", "pathsData");
  107. }
  108. TEST_F(WellknowntypesTest, SourceContextTest)
  109. {
  110. ASSERT_GT(qMetaTypeId<SourceContext>(), 0);
  111. assertMessagePropertyRegistered<SourceContext, QString>(1, "QString", "fileName");
  112. }
  113. TEST_F(WellknowntypesTest, StructTest)
  114. {
  115. ASSERT_GT(qMetaTypeId<Struct>(), 0);
  116. assertMessagePropertyRegistered<Struct, Struct::FieldsEntry>(1, "google::protobuf::Struct::FieldsEntry", "fields");
  117. }
  118. TEST_F(WellknowntypesTest, ValueTest)
  119. {
  120. ASSERT_GT(qMetaTypeId<Value>(), 0);
  121. assertMessagePropertyRegistered<Value, NullValueGadget::NullValue>(1, "google::protobuf::NullValueGadget::NullValue", "nullValue");
  122. assertMessagePropertyRegistered<Value, double>(2, "double", "numberValue");
  123. assertMessagePropertyRegistered<Value, QString>(3, "QString", "stringValue");
  124. assertMessagePropertyRegistered<Value, bool>(4, "bool", "boolValue");
  125. assertMessagePropertyRegistered<Value, Struct *>(5, "google::protobuf::Struct*", "structValue");
  126. assertMessagePropertyRegistered<Value, ListValue *>(6, "google::protobuf::ListValue*", "listValue");
  127. }
  128. TEST_F(WellknowntypesTest, ListValueTest)
  129. {
  130. ASSERT_GT(qMetaTypeId<ListValue>(), 0);
  131. assertMessagePropertyRegistered<ListValue, ValueRepeated>(1, "google::protobuf::ValueRepeated", "valuesData");
  132. }
  133. TEST_F(WellknowntypesTest, TimestampTest)
  134. {
  135. ASSERT_GT(qMetaTypeId<Timestamp>(), 0);
  136. assertMessagePropertyRegistered<Timestamp, QtProtobuf::int64>(1, "QtProtobuf::int64", "seconds");
  137. assertMessagePropertyRegistered<Timestamp, QtProtobuf::int32>(2, "QtProtobuf::int32", "nanos_p");
  138. }
  139. TEST_F(WellknowntypesTest, TypeTest)
  140. {
  141. ASSERT_GT(qMetaTypeId<Type>(), 0);
  142. Q_PROPERTY(QStringList oneofs READ oneofs WRITE setOneofs NOTIFY oneofsChanged)
  143. assertMessagePropertyRegistered<Type, QString>(1, "QString", "name");
  144. assertMessagePropertyRegistered<Type, FieldRepeated>(2, "google::protobuf::FieldRepeated", "fieldsData");
  145. assertMessagePropertyRegistered<Type, QStringList>(3, "QStringList", "oneofsData");
  146. assertMessagePropertyRegistered<Type, OptionRepeated>(4, "google::protobuf::OptionRepeated", "optionsData");
  147. assertMessagePropertyRegistered<Type, SourceContext *>(5, "google::protobuf::SourceContext*", "sourceContext", true);
  148. assertMessagePropertyRegistered<Type, SyntaxGadget::Syntax>(6, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  149. }
  150. TEST_F(WellknowntypesTest, FieldTest)
  151. {
  152. ASSERT_GT(qMetaTypeId<Field>(), 0);
  153. assertMessagePropertyRegistered<Field, Field::Kind>(1, "Kind", "kind");
  154. assertMessagePropertyRegistered<Field, Field::Cardinality>(2, "Cardinality", "cardinality");
  155. assertMessagePropertyRegistered<Field, QtProtobuf::int32>(3, "QtProtobuf::int32", "number_p");
  156. assertMessagePropertyRegistered<Field, QString>(4, "QString", "name");
  157. assertMessagePropertyRegistered<Field, QString>(6, "QString", "typeUrl");
  158. assertMessagePropertyRegistered<Field, QtProtobuf::int32>(7, "QtProtobuf::int32", "oneofIndex_p");
  159. assertMessagePropertyRegistered<Field, bool>(8, "bool", "packed");
  160. assertMessagePropertyRegistered<Field, OptionRepeated>(9, "google::protobuf::OptionRepeated", "optionsData");
  161. assertMessagePropertyRegistered<Field, QString>(10, "QString", "jsonName");
  162. assertMessagePropertyRegistered<Field, QString>(11, "QString", "defaultValue");
  163. }
  164. TEST_F(WellknowntypesTest, EnumTest)
  165. {
  166. ASSERT_GT(qMetaTypeId<Enum>(), 0);
  167. assertMessagePropertyRegistered<Enum, QString>(1, "QString", "name");
  168. assertMessagePropertyRegistered<Enum, EnumValueRepeated>(2, "google::protobuf::EnumValueRepeated", "enumvalueData");
  169. assertMessagePropertyRegistered<Enum, OptionRepeated>(3, "google::protobuf::OptionRepeated", "optionsData");
  170. assertMessagePropertyRegistered<Enum, SourceContext *>(4, "google::protobuf::SourceContext*", "sourceContext", true);
  171. assertMessagePropertyRegistered<Enum, SyntaxGadget::Syntax>(5, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  172. }
  173. TEST_F(WellknowntypesTest, EnumValueTest)
  174. {
  175. ASSERT_GT(qMetaTypeId<EnumValue>(), 0);
  176. assertMessagePropertyRegistered<EnumValue, QString>(1, "QString", "name");
  177. assertMessagePropertyRegistered<EnumValue, QtProtobuf::int32>(2, "QtProtobuf::int32", "number_p");
  178. assertMessagePropertyRegistered<EnumValue, OptionRepeated>(3, "google::protobuf::OptionRepeated", "optionsData");
  179. }
  180. TEST_F(WellknowntypesTest, OptionTest)
  181. {
  182. ASSERT_GT(qMetaTypeId<Option>(), 0);
  183. assertMessagePropertyRegistered<Option, QString>(1, "QString", "name");
  184. assertMessagePropertyRegistered<Option, Any *>(2, "google::protobuf::Any*", "value", true);
  185. }
  186. TEST_F(WellknowntypesTest, DoubleValueTest)
  187. {
  188. ASSERT_GT(qMetaTypeId<DoubleValue>(), 0);
  189. assertMessagePropertyRegistered<DoubleValue, double>(1, "double", "value");
  190. }
  191. TEST_F(WellknowntypesTest, FloatValueTest)
  192. {
  193. ASSERT_GT(qMetaTypeId<FloatValue>(), 0);
  194. assertMessagePropertyRegistered<FloatValue, float>(1, "float", "value");
  195. }
  196. TEST_F(WellknowntypesTest, Int64ValueTest)
  197. {
  198. ASSERT_GT(qMetaTypeId<Int64Value>(), 0);
  199. assertMessagePropertyRegistered<Int64Value, QtProtobuf::int64>(1, "QtProtobuf::int64", "value");
  200. }
  201. TEST_F(WellknowntypesTest, UInt64ValueTest)
  202. {
  203. ASSERT_GT(qMetaTypeId<UInt64Value>(), 0);
  204. assertMessagePropertyRegistered<UInt64Value, QtProtobuf::uint64>(1, "QtProtobuf::uint64", "value");
  205. }
  206. TEST_F(WellknowntypesTest, Int32ValueTest)
  207. {
  208. ASSERT_GT(qMetaTypeId<Int32Value>(), 0);
  209. assertMessagePropertyRegistered<Int32Value, QtProtobuf::int32>(1, "QtProtobuf::int32", "value_p");
  210. }
  211. TEST_F(WellknowntypesTest, UInt32ValueTest)
  212. {
  213. ASSERT_GT(qMetaTypeId<UInt32Value>(), 0);
  214. assertMessagePropertyRegistered<UInt32Value, QtProtobuf::uint32>(1, "QtProtobuf::uint32", "value");
  215. }
  216. TEST_F(WellknowntypesTest, StringValueTest)
  217. {
  218. ASSERT_GT(qMetaTypeId<StringValue>(), 0);
  219. assertMessagePropertyRegistered<StringValue, QString>(1, "QString", "value");
  220. }
  221. TEST_F(WellknowntypesTest, BytesValueTest)
  222. {
  223. ASSERT_GT(qMetaTypeId<BytesValue>(), 0);
  224. assertMessagePropertyRegistered<BytesValue, QByteArray>(1, "QByteArray", "value");
  225. }
  226. TEST_F(WellknowntypesTest, TimestampMessageTest)
  227. {
  228. assertMessagePropertyRegistered<qtprotobufnamespace::wellknowntypes::tests::TimestampMessage, google::protobuf::Timestamp*>(1, "google::protobuf::Timestamp*", "testField");
  229. }
  230. TEST_F(WellknowntypesTest, TimestampMessageSerializationTest) {
  231. qtprotobufnamespace::wellknowntypes::tests::TimestampMessage msg;
  232. QDateTime deserializedDateTime;
  233. QDateTime originalDateTime = QDateTime::currentDateTime();
  234. msg.setTestField({originalDateTime, nullptr});
  235. ASSERT_EQ(msg.testField().seconds(), originalDateTime.toMSecsSinceEpoch() / 1000);
  236. ASSERT_EQ(msg.testField().nanos(), (originalDateTime.toMSecsSinceEpoch() % 1000) * 1000);
  237. QByteArray val = msg.serialize(serializer.get());
  238. msg.setTestField({0, 0, nullptr});
  239. ASSERT_EQ(msg.testField().seconds(), 0);
  240. ASSERT_EQ(msg.testField().nanos(), 0);
  241. msg.deserialize(serializer.get(), val);
  242. deserializedDateTime = msg.testField();
  243. ASSERT_EQ(deserializedDateTime.toMSecsSinceEpoch(), originalDateTime.toMSecsSinceEpoch());
  244. ASSERT_TRUE(deserializedDateTime == originalDateTime);
  245. }
  246. }
  247. }