Selaa lähdekoodia

Refactor types registration

- Migrate to manual registration approach
- Update tests
Alexey Edelev 5 vuotta sitten
vanhempi
commit
1990f3cacc

+ 18 - 0
README.md

@@ -119,6 +119,24 @@ generate_qtprotobuf(TARGET MyTarget
     GENERATED_HEADERS ${GENERATED_HEADERS})
 ...
 ```
+
+#### Usage
+
+To enable QtProtobuf project you need to register protobuf types. It's good practice to make it in 'main' function.
+
+```cpp
+...
+#include <QtProtobufTypes>
+...
+int main(int argc, char *argv[])
+{
+    QtProtobuf::registerProtoTypes();
+    ... //Qt application initialization and run
+}
+```
+
+**For each generated class you also need to call 'qRegisterProtobufType&lt;GeneratedClassName&gt;' to enable serialization and QML support**
+
 ## CMake functions reference
 
 ### generate_qtprotobuf

+ 8 - 0
examples/addressbook/addressbookengine.cpp

@@ -49,6 +49,14 @@ AddressBookEngine::AddressBookEngine() : QObject()
   , m_contacts(new ContactsListModel({}, this))
   , m_callStatus(CallStatus::Inactive)
 {
+    qRegisterProtobufType<Contact>();
+    qRegisterProtobufType<ListFrame>();
+    qRegisterProtobufType<CallStatus>();
+    qRegisterProtobufType<PhoneNumber>();
+    qRegisterProtobufType<None>();
+    qRegisterProtobufType<Address>();
+    qRegisterProtobufType<Job>();
+
     //Prepare ssl configuration
     QSslConfiguration conf = QSslConfiguration::defaultConfiguration();
     QFile certFile("cert.pem");

+ 1 - 0
examples/addressbook/main.cpp

@@ -40,6 +40,7 @@ using namespace qtprotobuf::examples;
 
 int main(int argc, char *argv[])
 {
+    QtProtobuf::registerProtoTypes();
     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
 
     qmlRegisterType<ContactsListModel>("examples.addressbook", 1, 0, "ContactsListModel");

+ 1 - 0
examples/simplechat/main.cpp

@@ -42,6 +42,7 @@ using namespace qtprotobuf::examples;
 
 int main(int argc, char *argv[])
 {
+    QtProtobuf::registerProtoTypes();
     QGuiApplication app(argc, argv);
 
     QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);

+ 3 - 0
examples/simplechat/simplechatengine.cpp

@@ -54,6 +54,9 @@ public:
 SimpleChatEngine::SimpleChatEngine(QObject *parent) : QObject(parent), m_client(new SimpleChatClient)
   , m_clipBoard(QGuiApplication::clipboard())
 {
+    qRegisterProtobufType<ChatMessage>();
+    qRegisterProtobufType<ChatMessages>();
+
     if (m_clipBoard) {
         connect(m_clipBoard, &QClipboard::dataChanged, this, &SimpleChatEngine::clipBoardContentTypeChanged);
     }

+ 3 - 0
src/generator/globalenumsgenerator.cpp

@@ -40,6 +40,9 @@ void GlobalEnumsGenerator::startEnum(const std::vector<std::string>& namespaces)
     printNamespaces(namespaces);
     printEnumClass();
     printPublic();
+    Indent();
+    mPrinter.Print({{"classname", mClassName}}, Templates::ManualRegistrationDeclaration);
+    Outdent();
     printPrivate();
     printConstructor();
 }

+ 2 - 9
src/generator/globalenumssourcegenerator.cpp

@@ -39,7 +39,7 @@ GlobalEnumsSourceGenerator::GlobalEnumsSourceGenerator(const PackagesList &packa
 void GlobalEnumsSourceGenerator::run() {
     mPrinter.Print("#include <globalenums.h>\n"
                    "#include <QProtobufObject>\n"
-                   "#include <QProtobufRegistrationHelper>\n\n"
+                   "\n"
                    "#include <QQmlEngine>");
 
     std::vector<std::string> namespaces;
@@ -74,7 +74,7 @@ void GlobalEnumsSourceGenerator::printRegisterBody(const std::list<const FileDes
                                                                        {"namespaces", fullNamespace},
                                                                        {"package", list.front()->package()}};
 
-    mPrinter.Print(registrationProperties, Templates::ComplexGlobalEnumRegistrationTemplate);
+    mPrinter.Print(registrationProperties, Templates::ManualRegistrationGlobalEnumDefinition);
     Indent();
     mPrinter.Print(registrationProperties, Templates::QmlRegisterTypeUncreatableTemplate);
 
@@ -92,11 +92,4 @@ void GlobalEnumsSourceGenerator::printRegisterBody(const std::list<const FileDes
     }
     Outdent();
     mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
-    printRegistrationHelperInvokation();
-    mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
-}
-
-void GlobalEnumsSourceGenerator::printRegistrationHelperInvokation()
-{
-    mPrinter.Print(Templates::RegistratorTemplate);
 }

+ 0 - 1
src/generator/globalenumssourcegenerator.h

@@ -41,7 +41,6 @@ public:
     void run() override;
     void printRegisterBody(const std::list<const ::google::protobuf::FileDescriptor *> &list,
                            const std::vector<std::string> &namespaces);
-    void printRegistrationHelperInvokation();
 };
 
 } //namespace generator

+ 4 - 0
src/generator/protobufclassgenerator.cpp

@@ -445,6 +445,10 @@ void ProtobufClassGenerator::printProperties()
     }
     Outdent();
 
+    Indent();
+    mPrinter.Print({{"classname", mClassName}}, Templates::ManualRegistrationDeclaration);
+    Outdent();
+
     mPrinter.Print(Templates::SignalsBlockTemplate);
 
     Indent();

+ 1 - 9
src/generator/protobufsourcegenerator.cpp

@@ -47,7 +47,7 @@ void ProtobufSourceGenerator::printRegisterBody()
                                                                        {"package", mMessage->file()->package()}
                                                                       };
     mPrinter.Print(registrationProperties,
-                   Templates::ComplexTypeRegistrationTemplate);
+                   Templates::ManualRegistrationComplexTypeDefinition);
     Indent();
     mPrinter.Print(registrationProperties, Templates::RegisterQmlListPropertyMetaTypeTemplate);
     mPrinter.Print(registrationProperties, Templates::QmlRegisterTypeTemplate);
@@ -80,11 +80,8 @@ void ProtobufSourceGenerator::printRegisterBody()
         }
     }
 
-    mPrinter.Print({{"classname", mClassName}}, Templates::RegisterSerializersTemplate);
     Outdent();
     mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
-    printRegistrationHelperInvokation();
-    mPrinter.Print(Templates::SimpleBlockEnclosureTemplate);
 }
 
 void ProtobufSourceGenerator::printFieldsOrdering() {
@@ -105,11 +102,6 @@ void ProtobufSourceGenerator::printFieldsOrdering() {
     mPrinter.Print("\n");
 }
 
-void ProtobufSourceGenerator::printRegistrationHelperInvokation()
-{
-    mPrinter.Print(Templates::RegistratorTemplate);
-}
-
 void ProtobufSourceGenerator::printConstructor()
 {
     std::string parameterList;

+ 0 - 2
src/generator/protobufsourcegenerator.h

@@ -37,12 +37,10 @@ public:
     ProtobufSourceGenerator(const google::protobuf::Descriptor *message, std::unique_ptr<google::protobuf::io::ZeroCopyOutputStream> out);
     void printRegisterBody();
     void printFieldsOrdering();
-    void printRegistrationHelperInvokation();
     void printConstructor();
 
     void run() override {
         printClassHeaderInclude();
-        mPrinter.Print({{"include", "qprotobufregistrationhelper.h"}}, Templates::ExternalIncludeTemplate);
         printNamespaces();
         printFieldsOrdering();
         printRegisterBody();

+ 9 - 9
src/generator/templates.cpp

@@ -46,15 +46,15 @@ const char *Templates::ExternalIncludeTemplate = "#include <$include$>\n";
 const char *Templates::GlobalEnumIncludeTemplate = "#include <globalenums.h>\n";
 
 const char *Templates::UsingQtProtobufNamespaceTemplate = "\nusing namespace QtProtobuf;\n";
-const char *Templates::ComplexTypeRegistrationTemplate = "namespace $classname$Private {\n"
-                                                         "void registerTypes()\n{\n"
-                                                         "    qRegisterMetaType<$classname$>(\"$classname$\");\n"
-                                                         "    qRegisterMetaType<$classname$Repeated>(\"$classname$Repeated\");\n"
-                                                         "    qRegisterMetaType<$classname$>(\"$namespaces$::$classname$\");\n"
-                                                         "    qRegisterMetaType<$classname$Repeated>(\"$namespaces$::$classname$Repeated\");\n"
-                                                         "";
-const char *Templates::ComplexGlobalEnumRegistrationTemplate = "namespace $classname$Private {\n"
-                                                               "void registerTypes()\n{\n";
+const char *Templates::ManualRegistrationDeclaration = "static void registerTypes();\n";
+const char *Templates::ManualRegistrationComplexTypeDefinition = "void $classname$::registerTypes()\n{\n"
+                                                                 "    qRegisterMetaType<$classname$>(\"$classname$\");\n"
+                                                                 "    qRegisterMetaType<$classname$Repeated>(\"$classname$Repeated\");\n"
+                                                                 "    qRegisterMetaType<$classname$>(\"$namespaces$::$classname$\");\n"
+                                                                 "    qRegisterMetaType<$classname$Repeated>(\"$namespaces$::$classname$Repeated\");\n"
+                                                                 "";
+const char *Templates::ManualRegistrationGlobalEnumDefinition = "void $classname$::registerTypes()\n{\n"
+                                                                 "";
 const char *Templates::ComplexGlobalEnumFieldRegistrationTemplate = "qRegisterMetaType<$classname$::$enum$>(\"$namespaces$::$classname$::$enum$\");\n";
 const char *Templates::ComplexListTypeUsingTemplate = "using $classname$Repeated = QList<QSharedPointer<$classname$>>;\n";
 const char *Templates::MapTypeUsingTemplate = "using $classname$ = QMap<$key$, $value$>;\n";

+ 3 - 2
src/generator/templates.h

@@ -42,8 +42,9 @@ public:
     static const char *ExternalIncludeTemplate;
     static const char *GlobalEnumIncludeTemplate;
     static const char *UsingQtProtobufNamespaceTemplate;
-    static const char *ComplexTypeRegistrationTemplate;
-    static const char *ComplexGlobalEnumRegistrationTemplate;
+    static const char *ManualRegistrationDeclaration;
+    static const char *ManualRegistrationComplexTypeDefinition;
+    static const char *ManualRegistrationGlobalEnumDefinition;
     static const char *ComplexGlobalEnumFieldRegistrationTemplate;
     static const char *ComplexListTypeUsingTemplate;
     static const char *MapTypeUsingTemplate;

+ 0 - 2
src/protobuf/CMakeLists.txt

@@ -39,7 +39,6 @@ file(GLOB HEADERS
     qprotobufserializer_p.h
     qprotobufjsonserializer.h
     qprotobufselfcheckiterator.h
-    qprotobufregistrationhelper.h
     qprotobufmetaproperty.h
     qprotobufmetaobject.h)
 
@@ -54,7 +53,6 @@ file(GLOB PUBLIC_HEADERS
     qprotobufserializer.h
     qprotobufjsonserializer.h
     qprotobufselfcheckiterator.h
-    qprotobufregistrationhelper.h
     qprotobufmetaproperty.h
     qprotobufmetaobject.h)
 

+ 4 - 4
src/protobuf/QtProtobufGen.cmake.in

@@ -10,7 +10,7 @@ function(generate_qtprotobuf)
     set(GEN_TARGET ${generate_qtprotobuf_TARGET}_qtprotobuf_generate)
 
     if(NOT DEFINED QTPROTOBUF_EXECUTABLE)
-        set(QTPROTOBUF_EXECUTABLE @QTPROTOBUF_EXECUTABLE_INSTALL@)
+        set(QTPROTOBUF_EXECUTABLE "@QTPROTOBUF_EXECUTABLE_INSTALL@")
     endif()
     find_program(GO_EXECUTABLE "go")
     foreach(PROTO_FILE IN LISTS generate_qtprotobuf_PROTO_FILES)
@@ -34,7 +34,7 @@ function(generate_qtprotobuf)
     endforeach()
 
     list(REMOVE_DUPLICATES GENERATED_HEADERS)
-    
+
     if(NOT DEFINED generate_qtprotobuf_OUT_DIR)
         set(OUT_DIR ${CMAKE_CURRENT_BINARY_DIR})
     else()
@@ -64,7 +64,7 @@ function(generate_qtprotobuf)
             DEPENDS ${PROTO_FILES} ${QTPROTOBUF_EXECUTABLE}
             COMMENT "Generating QtProtobuf ${generate_qtprotobuf_TARGET} sources..."
     )
-    
+
     add_custom_target(${GEN_TARGET} DEPENDS ${QTPROTOBUF_GENERATED_SOURCES} ${QTPROTOBUF_GENERATED_HEADERS} ${PROTO_FILES})
 
     qt5_wrap_cpp(MOC_SOURCES ${QTPROTOBUF_GENERATED_HEADERS})
@@ -76,4 +76,4 @@ function(generate_qtprotobuf)
         $<TARGET_PROPERTY:@QTPROTOBUF_COMMON_NAMESPACE@::QtProtobuf,INTERFACE_INCLUDE_DIRECTORIES>
         $<TARGET_PROPERTY:@QTPROTOBUF_COMMON_NAMESPACE@::QtGrpc,INTERFACE_INCLUDE_DIRECTORIES> ${OUT_DIR})
 endfunction()
- 
+

+ 1 - 0
src/protobuf/qabstractprotobufserializer.h

@@ -236,6 +236,7 @@ public:
  */
 template<typename T>
 static void qRegisterProtobufType() {
+    T::registerTypes();
     QtProtobufPrivate::registerHandler(qMetaTypeId<T *>(), { QtProtobufPrivate::serializeObject<T>,
             QtProtobufPrivate::deserializeObject<T>, QtProtobufPrivate::ObjectHandler });
     QtProtobufPrivate::registerHandler(qMetaTypeId<QList<QSharedPointer<T>>>(), { QtProtobufPrivate::serializeList<T>,

+ 0 - 43
src/protobuf/qprotobufregistrationhelper.h

@@ -1,43 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2019 Viktor Kopp <vifactor@gmail.com>, Alexey Edelev <semlanik@gmail.com>
- *
- * This file is part of QtProtobuf project https://git.semlanik.org/semlanik/qtprotobuf
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy of this
- * software and associated documentation files (the "Software"), to deal in the Software
- * without restriction, including without limitation the rights to use, copy, modify,
- * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
- * to permit persons to whom the Software is furnished to do so, subject to the following
- * conditions:
- *
- * The above copyright notice and this permission notice shall be included in all copies
- * or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
- * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
- * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
- * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-
-#pragma once //QProtobufRegistrationHelper
-
-#include <functional>
-
-/*!
- * \private
- * This class might be replaced by manual registration calls, because of issues related to static initialization in C++
- */
-namespace QtProtobuf {
-
-class QProtobufRegistrationHelper {
-public:
-    QProtobufRegistrationHelper(const std::function<void(void)> &registrator) {
-        registrator();
-    }
-};
-
-}

+ 9 - 6
src/protobuf/qtprotobuf.cpp

@@ -25,12 +25,11 @@
 
 #include "qtprotobuftypes.h"
 #include "qprotobufobject.h"
-#include "qprotobufregistrationhelper.h"
 
 #include <type_traits>
 
 #define registerProtobufType(X) qRegisterMetaType<X>(# X);\
-                                qRegisterMetaType<X>("QtProtobuf::"# X);
+                                qRegisterMetaType<X>("QtProtobuf::"# X)
 
 namespace QtProtobuf {
 
@@ -81,7 +80,14 @@ void registerBasicConverters() {
     QMetaType::registerConverter<T, QString>(T::toString);
 }
 
-void registerTypes() {
+}
+
+void registerProtoTypes() {
+    static bool registred = false;
+    if (registred) {
+        return;
+    }
+    registred = true;
     registerProtobufType(int32);
     registerProtobufType(int64);
     registerProtobufType(uint32);
@@ -115,6 +121,3 @@ void registerTypes() {
     registerBasicConverters<fixed64>();
 }
 }
-
-static QProtobufRegistrationHelper helper(registerTypes);
-}

+ 8 - 0
src/protobuf/qtprotobuftypes.h

@@ -25,6 +25,8 @@
 
 #pragma once //QtProtobufTypes
 
+#include "qtprotobufglobal.h"
+
 #include <QList>
 #include <QMetaType>
 
@@ -218,6 +220,12 @@ using FloatList = QList<float>;
  */
 using DoubleList = QList<double>;
 
+/*!
+ * \brief registerTypes
+ * This method should be called in all applications that supposed to use QtProtobuf
+ */
+Q_PROTOBUF_EXPORT extern void registerProtoTypes();
+
 /*! \} */
 }
 

+ 2 - 0
tests/test_grpc/clienttest.cpp

@@ -46,6 +46,8 @@ class ClientTest : public ::testing::Test
 {
 protected:
     static void SetUpTestCase() {
+        QtProtobuf::registerProtoTypes();
+        qRegisterProtobufType<SimpleStringMessage>();
     }
     static QCoreApplication m_app;
     static int m_argc;

+ 4 - 0
tests/test_grpc/sslclienttest.cpp

@@ -42,6 +42,10 @@ using namespace QtProtobuf;
 class ClientTest : public ::testing::Test
 {
 protected:
+    static void SetUpTestCase() {
+        QtProtobuf::registerProtoTypes();
+        qRegisterProtobufType<SimpleStringMessage>();
+    }
     static QCoreApplication m_app;
     static int m_argc;
     static QUrl m_echoServerAddress;

+ 3 - 0
tests/test_protobuf/converterstest.cpp

@@ -35,6 +35,9 @@ class CoverterTest : public ::testing::Test
 {
 public:
     CoverterTest() = default;
+    static void SetUpTestCase() {
+        QtProtobuf::registerProtoTypes();
+    }
 };
 
 TEST_F(CoverterTest, TestFromTypeConverters)

+ 64 - 0
tests/test_protobuf/deserializationtest.cpp

@@ -51,6 +51,7 @@
 #include "repeatedfixedint64message.h"
 #include "repeatedsfixedint64message.h"
 #include "simpleenummessage.h"
+#include "simplebytesmessage.h"
 #include "repeatedcomplexmessage.h"
 
 #include "simplefixed32stringmapmessage.h"
@@ -92,6 +93,69 @@ using namespace qtprotobufnamespace::tests;
 using namespace QtProtobuf::tests;
 using namespace QtProtobuf;
 
+void DeserializationTest::SetUpTestCase()
+{
+    //Register all types
+    QtProtobuf::registerProtoTypes();
+    qRegisterProtobufType<SimpleIntMessage>();
+    qRegisterProtobufType<SimpleUIntMessage>();
+    qRegisterProtobufType<SimpleSIntMessage>();
+//    qRegisterProtobufType<SimpleInt64Message>();
+//    qRegisterProtobufType<SimpleUInt64Message>();
+//    qRegisterProtobufType<SimpleSInt64Message>();
+    qRegisterProtobufType<SimpleFixedInt32Message>();
+    qRegisterProtobufType<SimpleFixedInt64Message>();
+    qRegisterProtobufType<SimpleSFixedInt32Message>();
+    qRegisterProtobufType<SimpleSFixedInt64Message>();
+    qRegisterProtobufType<SimpleFloatMessage>();
+    qRegisterProtobufType<SimpleDoubleMessage>();
+    qRegisterProtobufType<SimpleStringMessage>();
+    qRegisterProtobufType<ComplexMessage>();
+    qRegisterProtobufType<RepeatedIntMessage>();
+    qRegisterProtobufType<RepeatedSIntMessage>();
+    qRegisterProtobufType<RepeatedUIntMessage>();
+    qRegisterProtobufType<RepeatedInt64Message>();
+    qRegisterProtobufType<RepeatedSInt64Message>();
+    qRegisterProtobufType<RepeatedUInt64Message>();
+    qRegisterProtobufType<RepeatedFixedIntMessage>();
+    qRegisterProtobufType<RepeatedSFixedIntMessage>();
+    qRegisterProtobufType<RepeatedFixedInt64Message>();
+    qRegisterProtobufType<RepeatedStringMessage>();
+    qRegisterProtobufType<RepeatedDoubleMessage>();
+    qRegisterProtobufType<RepeatedBytesMessage>();
+    qRegisterProtobufType<RepeatedFloatMessage>();
+    qRegisterProtobufType<RepeatedComplexMessage>();
+    qRegisterProtobufType<SimpleFixed32StringMapMessage>();
+    qRegisterProtobufType<SimpleSFixed32StringMapMessage>();
+    qRegisterProtobufType<SimpleInt32StringMapMessage>();
+    qRegisterProtobufType<SimpleSInt32StringMapMessage>();
+    qRegisterProtobufType<SimpleUInt32StringMapMessage>();
+    qRegisterProtobufType<SimpleFixed64StringMapMessage>();
+    qRegisterProtobufType<SimpleSFixed64StringMapMessage>();
+    qRegisterProtobufType<SimpleInt64StringMapMessage>();
+    qRegisterProtobufType<SimpleSInt64StringMapMessage>();
+    qRegisterProtobufType<SimpleUInt64StringMapMessage>();
+    qRegisterProtobufType<SimpleStringStringMapMessage>();
+    qRegisterProtobufType<FieldIndexTest1Message>();
+    qRegisterProtobufType<FieldIndexTest2Message>();
+    qRegisterProtobufType<FieldIndexTest3Message>();
+    qRegisterProtobufType<FieldIndexTest4Message>();
+    qRegisterProtobufType<SimpleEnumListMessage>();
+    qRegisterProtobufType<SimpleBytesMessage>();
+    qRegisterProtobufType<SimpleFixed32ComplexMessageMapMessage>();
+    qRegisterProtobufType<SimpleSFixed32ComplexMessageMapMessage>();
+    qRegisterProtobufType<SimpleInt32ComplexMessageMapMessage>();
+    qRegisterProtobufType<SimpleSInt32ComplexMessageMapMessage>();
+    qRegisterProtobufType<SimpleUInt32ComplexMessageMapMessage>();
+    qRegisterProtobufType<SimpleFixed64ComplexMessageMapMessage>();
+    qRegisterProtobufType<SimpleSFixed64ComplexMessageMapMessage>();
+    qRegisterProtobufType<SimpleInt64ComplexMessageMapMessage>();
+    qRegisterProtobufType<SimpleSInt64ComplexMessageMapMessage>();
+    qRegisterProtobufType<SimpleUInt64ComplexMessageMapMessage>();
+    qRegisterProtobufType<SimpleStringComplexMessageMapMessage>();
+    qRegisterProtobufType<SimpleEnumMessage>();
+}
+
 void DeserializationTest::SetUp()
 {
     serializer.reset(new QProtobufSerializer);

+ 1 - 0
tests/test_protobuf/deserializationtest.h

@@ -36,6 +36,7 @@ class DeserializationTest : public ::testing::Test
 public:
     DeserializationTest() = default;
     void SetUp() override;
+    static void SetUpTestCase();
 protected:
     std::unique_ptr<QProtobufSerializer> serializer;
 };

+ 4 - 0
tests/test_protobuf/jsonserializationtest.cpp

@@ -39,6 +39,10 @@ class JsonSerializationTest : public ::testing::Test
 public:
     JsonSerializationTest() = default;
     void SetUp() override;
+    static void SetUpTestCase() {
+        QtProtobuf::registerProtoTypes();
+    }
+
 protected:
     std::unique_ptr<QProtobufJsonSerializer> serializer;
 };

+ 53 - 0
tests/test_protobuf/serializationtest.cpp

@@ -82,6 +82,59 @@ using namespace qtprotobufnamespace::tests;
 using namespace QtProtobuf::tests;
 using namespace QtProtobuf;
 
+
+void SerializationTest::SetUpTestCase()
+{
+    //Register all types
+    QtProtobuf::registerProtoTypes();
+    qRegisterProtobufType<SimpleIntMessage>();
+    qRegisterProtobufType<SimpleUIntMessage>();
+    qRegisterProtobufType<SimpleSIntMessage>();
+    qRegisterProtobufType<SimpleInt64Message>();
+    qRegisterProtobufType<SimpleUInt64Message>();
+    qRegisterProtobufType<SimpleSInt64Message>();
+    qRegisterProtobufType<SimpleFixedInt32Message>();
+    qRegisterProtobufType<SimpleFixedInt64Message>();
+    qRegisterProtobufType<SimpleSFixedInt32Message>();
+    qRegisterProtobufType<SimpleSFixedInt64Message>();
+    qRegisterProtobufType<SimpleFloatMessage>();
+    qRegisterProtobufType<SimpleDoubleMessage>();
+    qRegisterProtobufType<SimpleStringMessage>();
+    qRegisterProtobufType<ComplexMessage>();
+    qRegisterProtobufType<RepeatedIntMessage>();
+    qRegisterProtobufType<RepeatedSIntMessage>();
+    qRegisterProtobufType<RepeatedUIntMessage>();
+    qRegisterProtobufType<RepeatedInt64Message>();
+    qRegisterProtobufType<RepeatedSInt64Message>();
+    qRegisterProtobufType<RepeatedUInt64Message>();
+    qRegisterProtobufType<RepeatedFixedIntMessage>();
+    qRegisterProtobufType<RepeatedSFixedIntMessage>();
+    qRegisterProtobufType<RepeatedFixedInt64Message>();
+    qRegisterProtobufType<RepeatedStringMessage>();
+    qRegisterProtobufType<RepeatedDoubleMessage>();
+    qRegisterProtobufType<RepeatedBytesMessage>();
+    qRegisterProtobufType<RepeatedFloatMessage>();
+    qRegisterProtobufType<RepeatedComplexMessage>();
+    qRegisterProtobufType<SimpleFixed32StringMapMessage>();
+    qRegisterProtobufType<SimpleSFixed32StringMapMessage>();
+    qRegisterProtobufType<SimpleInt32StringMapMessage>();
+    qRegisterProtobufType<SimpleSInt32StringMapMessage>();
+    qRegisterProtobufType<SimpleUInt32StringMapMessage>();
+    qRegisterProtobufType<SimpleFixed64StringMapMessage>();
+    qRegisterProtobufType<SimpleSFixed64StringMapMessage>();
+    qRegisterProtobufType<SimpleInt64StringMapMessage>();
+    qRegisterProtobufType<SimpleSInt64StringMapMessage>();
+    qRegisterProtobufType<SimpleUInt64StringMapMessage>();
+    qRegisterProtobufType<SimpleStringStringMapMessage>();
+    qRegisterProtobufType<FieldIndexTest1Message>();
+    qRegisterProtobufType<FieldIndexTest2Message>();
+    qRegisterProtobufType<FieldIndexTest3Message>();
+    qRegisterProtobufType<FieldIndexTest4Message>();
+    qRegisterProtobufType<SimpleEnumListMessage>();
+    qRegisterProtobufType<SimpleBytesMessage>();
+    qRegisterProtobufType<SimpleEnumMessage>();
+}
+
 void SerializationTest::SetUp()
 {
     serializer.reset(new QProtobufSerializer);

+ 1 - 0
tests/test_protobuf/serializationtest.h

@@ -36,6 +36,7 @@ class SerializationTest : public ::testing::Test
 public:
     SerializationTest() = default;
     void SetUp() override;
+    static void SetUpTestCase();
 protected:
     std::unique_ptr<QProtobufSerializer> serializer;
 };

+ 60 - 0
tests/test_protobuf/simpletest.cpp

@@ -101,8 +101,68 @@ public:
         ASSERT_EQ(MessageType::staticMetaObject.property(propertyNumber).userType(), qMetaTypeId<PropertyType>());
         ASSERT_STREQ(MessageType::staticMetaObject.property(propertyNumber).name(), propertyName);
     }
+
+    static void SetUpTestCase();
 };
 
+void SimpleTest::SetUpTestCase()
+{
+    QtProtobuf::registerProtoTypes();
+    qRegisterProtobufType<SimpleIntMessage>();
+    qRegisterProtobufType<SimpleUIntMessage>();
+    qRegisterProtobufType<SimpleSIntMessage>();
+    qRegisterProtobufType<SimpleInt64Message>();
+    qRegisterProtobufType<SimpleUInt64Message>();
+    qRegisterProtobufType<SimpleSInt64Message>();
+    qRegisterProtobufType<SimpleFixedInt32Message>();
+    qRegisterProtobufType<SimpleFixedInt64Message>();
+    qRegisterProtobufType<SimpleSFixedInt32Message>();
+    qRegisterProtobufType<SimpleSFixedInt64Message>();
+    qRegisterProtobufType<SimpleFloatMessage>();
+    qRegisterProtobufType<SimpleDoubleMessage>();
+    qRegisterProtobufType<SimpleStringMessage>();
+    qRegisterProtobufType<ComplexMessage>();
+    qRegisterProtobufType<RepeatedIntMessage>();
+    qRegisterProtobufType<RepeatedSIntMessage>();
+    qRegisterProtobufType<RepeatedUIntMessage>();
+    qRegisterProtobufType<RepeatedInt64Message>();
+    qRegisterProtobufType<RepeatedSInt64Message>();
+    qRegisterProtobufType<RepeatedUInt64Message>();
+    qRegisterProtobufType<RepeatedFixedIntMessage>();
+    qRegisterProtobufType<RepeatedSFixedIntMessage>();
+    qRegisterProtobufType<RepeatedFixedInt64Message>();
+    qRegisterProtobufType<RepeatedStringMessage>();
+    qRegisterProtobufType<RepeatedDoubleMessage>();
+    qRegisterProtobufType<RepeatedBytesMessage>();
+    qRegisterProtobufType<RepeatedFloatMessage>();
+    qRegisterProtobufType<RepeatedComplexMessage>();
+//    qRegisterProtobufType<SimpleFixed32StringMapMessage>();
+//    qRegisterProtobufType<SimpleSFixed32StringMapMessage>();
+//    qRegisterProtobufType<SimpleInt32StringMapMessage>();
+    qRegisterProtobufType<SimpleSInt32StringMapMessage>();
+//    qRegisterProtobufType<SimpleUInt32StringMapMessage>();
+//    qRegisterProtobufType<SimpleFixed64StringMapMessage>();
+//    qRegisterProtobufType<SimpleSFixed64StringMapMessage>();
+//    qRegisterProtobufType<SimpleInt64StringMapMessage>();
+//    qRegisterProtobufType<SimpleSInt64StringMapMessage>();
+//    qRegisterProtobufType<SimpleUInt64StringMapMessage>();
+    qRegisterProtobufType<SimpleStringStringMapMessage>();
+//    qRegisterProtobufType<FieldIndexTest1Message>();
+//    qRegisterProtobufType<FieldIndexTest2Message>();
+//    qRegisterProtobufType<FieldIndexTest3Message>();
+//    qRegisterProtobufType<FieldIndexTest4Message>();
+    qRegisterProtobufType<SimpleEnumListMessage>();
+    qRegisterProtobufType<SimpleBytesMessage>();
+
+    qRegisterProtobufType<EmptyMessage>();
+    qRegisterProtobufType<Message_Uderscore_name>();
+    qRegisterProtobufType<MessageUderscorename>();
+    qRegisterProtobufType<MessageUnderscoreField>();
+    qRegisterProtobufType<PriorMessageUnderscoreField>();
+    qRegisterProtobufType<FollowingMessageUnderscoreField>();
+    qRegisterProtobufType<CombinedMessageUnderscoreField>();
+}
+
 TEST_F(SimpleTest, SimpleBoolMessageTest)
 {
     const char *propertyName = "testFieldBool";

+ 11 - 10
tests/test_qml/main.cpp

@@ -46,16 +46,17 @@ using namespace qtprotobufnamespace::tests;
 class TestSetup : public QObject {
 public:
     TestSetup() {
-        SimpleBoolMessage();
-        SimpleBytesMessage();
-        SimpleDoubleMessage();
-        SimpleFloatMessage();
-        SimpleFixedInt32Message();
-        SimpleIntMessage();
-        SimpleSIntMessage();
-        SimpleUIntMessage();
-        SimpleStringMessage();
-        SimpleSFixedInt32Message();
+        QtProtobuf::registerProtoTypes();
+        qRegisterProtobufType<SimpleBoolMessage>();
+        qRegisterProtobufType<SimpleBytesMessage>();
+        qRegisterProtobufType<SimpleDoubleMessage>();
+        qRegisterProtobufType<SimpleFloatMessage>();
+        qRegisterProtobufType<SimpleFixedInt32Message>();
+        qRegisterProtobufType<SimpleIntMessage>();
+        qRegisterProtobufType<SimpleSIntMessage>();
+        qRegisterProtobufType<SimpleUIntMessage>();
+        qRegisterProtobufType<SimpleStringMessage>();
+        qRegisterProtobufType<SimpleSFixedInt32Message>();
     }
     ~TestSetup() = default;
 };