simpletest.cpp 12 KB

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