simpletest.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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", "paths");
  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. assertMessagePropertyRegistered<Type, QString>(1, "QString", "name");
  143. assertMessagePropertyRegistered<Type, FieldRepeated>(2, "google::protobuf::FieldRepeated", "fieldsData");
  144. assertMessagePropertyRegistered<Type, QStringList>(3, "QStringList", "oneofs");
  145. assertMessagePropertyRegistered<Type, OptionRepeated>(4, "google::protobuf::OptionRepeated", "optionsData");
  146. assertMessagePropertyRegistered<Type, SourceContext *>(5, "google::protobuf::SourceContext*", "sourceContext", true);
  147. assertMessagePropertyRegistered<Type, SyntaxGadget::Syntax>(6, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  148. }
  149. TEST_F(WellknowntypesTest, FieldTest)
  150. {
  151. ASSERT_GT(qMetaTypeId<Field>(), 0);
  152. assertMessagePropertyRegistered<Field, Field::Kind>(1, "Kind", "kind");
  153. assertMessagePropertyRegistered<Field, Field::Cardinality>(2, "Cardinality", "cardinality");
  154. assertMessagePropertyRegistered<Field, QtProtobuf::int32>(3, "QtProtobuf::int32", "number_p");
  155. assertMessagePropertyRegistered<Field, QString>(4, "QString", "name");
  156. assertMessagePropertyRegistered<Field, QString>(6, "QString", "typeUrl");
  157. assertMessagePropertyRegistered<Field, QtProtobuf::int32>(7, "QtProtobuf::int32", "oneofIndex_p");
  158. assertMessagePropertyRegistered<Field, bool>(8, "bool", "packed");
  159. assertMessagePropertyRegistered<Field, OptionRepeated>(9, "google::protobuf::OptionRepeated", "optionsData");
  160. assertMessagePropertyRegistered<Field, QString>(10, "QString", "jsonName");
  161. assertMessagePropertyRegistered<Field, QString>(11, "QString", "defaultValue");
  162. }
  163. TEST_F(WellknowntypesTest, EnumTest)
  164. {
  165. ASSERT_GT(qMetaTypeId<Enum>(), 0);
  166. assertMessagePropertyRegistered<Enum, QString>(1, "QString", "name");
  167. assertMessagePropertyRegistered<Enum, EnumValueRepeated>(2, "google::protobuf::EnumValueRepeated", "enumvalueData");
  168. assertMessagePropertyRegistered<Enum, OptionRepeated>(3, "google::protobuf::OptionRepeated", "optionsData");
  169. assertMessagePropertyRegistered<Enum, SourceContext *>(4, "google::protobuf::SourceContext*", "sourceContext", true);
  170. assertMessagePropertyRegistered<Enum, SyntaxGadget::Syntax>(5, "google::protobuf::SyntaxGadget::Syntax", "syntax");
  171. }
  172. TEST_F(WellknowntypesTest, EnumValueTest)
  173. {
  174. ASSERT_GT(qMetaTypeId<EnumValue>(), 0);
  175. assertMessagePropertyRegistered<EnumValue, QString>(1, "QString", "name");
  176. assertMessagePropertyRegistered<EnumValue, QtProtobuf::int32>(2, "QtProtobuf::int32", "number_p");
  177. assertMessagePropertyRegistered<EnumValue, OptionRepeated>(3, "google::protobuf::OptionRepeated", "optionsData");
  178. }
  179. TEST_F(WellknowntypesTest, OptionTest)
  180. {
  181. ASSERT_GT(qMetaTypeId<Option>(), 0);
  182. assertMessagePropertyRegistered<Option, QString>(1, "QString", "name");
  183. assertMessagePropertyRegistered<Option, Any *>(2, "google::protobuf::Any*", "value", true);
  184. }
  185. TEST_F(WellknowntypesTest, DoubleValueTest)
  186. {
  187. ASSERT_GT(qMetaTypeId<DoubleValue>(), 0);
  188. assertMessagePropertyRegistered<DoubleValue, double>(1, "double", "value");
  189. }
  190. TEST_F(WellknowntypesTest, FloatValueTest)
  191. {
  192. ASSERT_GT(qMetaTypeId<FloatValue>(), 0);
  193. assertMessagePropertyRegistered<FloatValue, float>(1, "float", "value");
  194. }
  195. TEST_F(WellknowntypesTest, Int64ValueTest)
  196. {
  197. ASSERT_GT(qMetaTypeId<Int64Value>(), 0);
  198. assertMessagePropertyRegistered<Int64Value, QtProtobuf::int64>(1, "QtProtobuf::int64", "value");
  199. }
  200. TEST_F(WellknowntypesTest, UInt64ValueTest)
  201. {
  202. ASSERT_GT(qMetaTypeId<UInt64Value>(), 0);
  203. assertMessagePropertyRegistered<UInt64Value, QtProtobuf::uint64>(1, "QtProtobuf::uint64", "value");
  204. }
  205. TEST_F(WellknowntypesTest, Int32ValueTest)
  206. {
  207. ASSERT_GT(qMetaTypeId<Int32Value>(), 0);
  208. assertMessagePropertyRegistered<Int32Value, QtProtobuf::int32>(1, "QtProtobuf::int32", "value_p");
  209. }
  210. TEST_F(WellknowntypesTest, UInt32ValueTest)
  211. {
  212. ASSERT_GT(qMetaTypeId<UInt32Value>(), 0);
  213. assertMessagePropertyRegistered<UInt32Value, QtProtobuf::uint32>(1, "QtProtobuf::uint32", "value");
  214. }
  215. TEST_F(WellknowntypesTest, StringValueTest)
  216. {
  217. ASSERT_GT(qMetaTypeId<StringValue>(), 0);
  218. assertMessagePropertyRegistered<StringValue, QString>(1, "QString", "value");
  219. }
  220. TEST_F(WellknowntypesTest, BytesValueTest)
  221. {
  222. ASSERT_GT(qMetaTypeId<BytesValue>(), 0);
  223. assertMessagePropertyRegistered<BytesValue, QByteArray>(1, "QByteArray", "value");
  224. }
  225. TEST_F(WellknowntypesTest, TimestampMessageTest)
  226. {
  227. assertMessagePropertyRegistered<qtprotobufnamespace::wellknowntypes::tests::TimestampMessage, google::protobuf::Timestamp*>(1, "google::protobuf::Timestamp*", "testField");
  228. }
  229. TEST_F(WellknowntypesTest, TimestampMessageSerializationTest) {
  230. qtprotobufnamespace::wellknowntypes::tests::TimestampMessage msg;
  231. QDateTime deserializedDateTime;
  232. QDateTime originalDateTime = QDateTime::currentDateTime();
  233. msg.setTestField({originalDateTime, nullptr});
  234. ASSERT_EQ(msg.testField().seconds(), originalDateTime.toMSecsSinceEpoch() / 1000);
  235. ASSERT_EQ(msg.testField().nanos(), (originalDateTime.toMSecsSinceEpoch() % 1000) * 1000);
  236. QByteArray val = msg.serialize(serializer.get());
  237. msg.setTestField({0, 0, nullptr});
  238. ASSERT_EQ(msg.testField().seconds(), 0);
  239. ASSERT_EQ(msg.testField().nanos(), 0);
  240. msg.deserialize(serializer.get(), val);
  241. deserializedDateTime = msg.testField();
  242. ASSERT_EQ(deserializedDateTime.toMSecsSinceEpoch(), originalDateTime.toMSecsSinceEpoch());
  243. ASSERT_TRUE(deserializedDateTime == originalDateTime);
  244. }
  245. }
  246. }