MockConnectionHandler.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #pragma once
  9. #include <native/utilities/AssetUtilEBusHelper.h>
  10. #include <native/utilities/ByteArrayStream.h>
  11. #include <AzFramework/Asset/AssetProcessorMessages.h>
  12. #include <AzCore/Serialization/Utils.h>
  13. #include <AzCore/IO/GenericStreams.h>
  14. #include <QByteArray>
  15. #include <QString>
  16. #include <AzCore/std/functional.h>
  17. namespace AssetProcessor
  18. {
  19. //! This class is mocks connection bus functionality in unit test mode
  20. class MockConnectionHandler
  21. : public AssetProcessor::ConnectionBus::Handler
  22. {
  23. public:
  24. ~MockConnectionHandler()
  25. {
  26. BusDisconnect();
  27. }
  28. size_t Send(unsigned int serial, const AzFramework::AssetSystem::BaseAssetProcessorMessage& message) override
  29. {
  30. QByteArray buffer;
  31. ByteArrayStream stream;
  32. bool wroteToStream = AZ::Utils::SaveObjectToStream(stream, AZ::DataStream::ST_BINARY, &message, message.RTTI_GetType());
  33. AZ_Assert(wroteToStream, "Connection::Send: Could not serialize to stream (type=%u)", message.GetMessageType());
  34. if (wroteToStream)
  35. {
  36. SendRaw(message.GetMessageType(), serial, stream.GetArray());
  37. return buffer.size();
  38. }
  39. return 0;
  40. }
  41. size_t SendRaw(unsigned int type, unsigned int serial, const QByteArray& payload) override
  42. {
  43. m_sent = true;
  44. if (m_callback)
  45. {
  46. //if callback is not null than calls it
  47. m_callback(type, serial, payload);
  48. }
  49. else
  50. {
  51. m_type = type;
  52. m_serial = serial;
  53. m_payload = payload;
  54. }
  55. return payload.size();
  56. }
  57. size_t SendPerPlatform(unsigned int serial, const AzFramework::AssetSystem::BaseAssetProcessorMessage& message, const QString& platform) override
  58. {
  59. if (QString::compare(platform, "pc", Qt::CaseInsensitive) == 0 || QString::compare(platform, "android", Qt::CaseInsensitive) == 0)
  60. {
  61. return Send(serial, message);
  62. }
  63. return 0;
  64. }
  65. size_t SendRawPerPlatform(unsigned int type, unsigned int serial, const QByteArray& data, const QString& platform) override
  66. {
  67. if (QString::compare(platform, "pc", Qt::CaseInsensitive) == 0 || QString::compare(platform, "android", Qt::CaseInsensitive) == 0)
  68. {
  69. return SendRaw(type, serial, data);
  70. }
  71. return 0;
  72. }
  73. unsigned int SendRequest(const AzFramework::AssetSystem::BaseAssetProcessorMessage& message, const AssetProcessor::ConnectionBusTraits::ResponseCallback& callback) override
  74. {
  75. Send(0, message);
  76. callback(message.GetMessageType(), QByteArray());
  77. return 0;
  78. }
  79. size_t SendResponse(unsigned int serial, const AzFramework::AssetSystem::BaseAssetProcessorMessage& message) override
  80. {
  81. return Send(serial, message);
  82. }
  83. void RemoveResponseHandler(unsigned int /*serial*/) override
  84. {
  85. // Nothing to do
  86. }
  87. typedef AZStd::function< void (unsigned int, unsigned int, const QByteArray&) > SendMessageCallBack;
  88. bool m_sent = false;
  89. unsigned int m_type = 0;
  90. unsigned int m_serial = 0;
  91. QByteArray m_payload;
  92. SendMessageCallBack m_callback;
  93. };
  94. }