1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
|
#include "gtest/gtest.h"
#include "mega/gfx/worker/command_serializer.h"
#include "mega/gfx/worker/commands.h"
#include "mega/gfx/worker/comms.h"
#include <chrono>
using mega::GfxDimension;
using mega::gfx::CommandHello;
using mega::gfx::CommandHelloResponse;
using mega::gfx::CommandNewGfx;
using mega::gfx::CommandNewGfxResponse;
using mega::gfx::CommandSerializer;
using mega::gfx::CommandShutDown;
using mega::gfx::CommandShutDownResponse;
using mega::gfx::CommandSupportFormats;
using mega::gfx::CommandSupportFormatsResponse;
using mega::gfx::IReader;
using std::chrono::milliseconds;
using namespace std::chrono_literals;
namespace mega
{
namespace gfx
{
bool operator==(const CommandNewGfx& lhs, const CommandNewGfx& rhs)
{
return lhs.Task.Path == rhs.Task.Path && lhs.Task.Dimensions == rhs.Task.Dimensions;
}
bool operator==(const CommandNewGfxResponse& lhs, const CommandNewGfxResponse& rhs)
{
return lhs.ErrorCode == rhs.ErrorCode && lhs.ErrorText == rhs.ErrorText && lhs.Images == rhs.Images;
}
bool operator==(const CommandShutDown& /*lhs*/, const CommandShutDown& /*rhs*/)
{
return true;
}
bool operator==(const CommandShutDownResponse& /*lhs*/, const CommandShutDownResponse& /*rhs*/)
{
return true;
}
bool operator==(const CommandHello& lhs, const CommandHello& rhs)
{
return lhs.Text == rhs.Text;
}
bool operator==(const CommandHelloResponse& lhs, const CommandHelloResponse& rhs)
{
return lhs.Text == rhs.Text;
}
bool operator==(const CommandSupportFormats& /*lhs*/, const CommandSupportFormats& /*rhs*/)
{
return true;
}
bool operator==(const CommandSupportFormatsResponse& lhs, const CommandSupportFormatsResponse& rhs)
{
return lhs.formats == rhs.formats && lhs.videoformats == rhs.videoformats;
}
}
}
class StringReader : public IReader
{
public:
StringReader(std::string&& value) : mValue(std::move(value)), mIndex{0} {}
private:
bool doRead(void* out, size_t n, milliseconds timeout) override;
std::string mValue;
size_t mIndex;
};
bool StringReader::doRead(void* out, size_t n, milliseconds /*timeout*/)
{
if (mIndex > mValue.size()) return false;
if (mIndex + n > mValue.size()) return false;
std::memcpy(out, mValue.data() + mIndex, n);
mIndex += n;
return true;
}
TEST(GfxCommandSerializer, CommandNewGfxSerializeAndUnserializeSuccessfully)
{
CommandNewGfx sourceCommand;
sourceCommand.Task.Path = "c:\\path\\image.png";
sourceCommand.Task.Dimensions = std::vector<GfxDimension>{ {250, 0} };
auto data = CommandSerializer::serialize(&sourceCommand);
ASSERT_NE(data, nullptr);
StringReader reader(std::move(*data));
auto command = CommandSerializer::unserialize(reader, 5000ms);
ASSERT_NE(command, nullptr);
auto targetCommand = dynamic_cast<CommandNewGfx*>(command.get());
ASSERT_NE(targetCommand, nullptr);
ASSERT_EQ(sourceCommand, *targetCommand);
}
TEST(GfxCommandSerializer, CommandNewGfxResponseSerializeAndUnserializeSuccessfully)
{
CommandNewGfxResponse sourceCommand;
sourceCommand.ErrorCode = 0;
sourceCommand.ErrorText = "OK";
sourceCommand.Images.push_back("imagedata");
auto data = CommandSerializer::serialize(&sourceCommand);
ASSERT_NE(data, nullptr);
StringReader reader(std::move(*data));
auto command = CommandSerializer::unserialize(reader, 5000ms);
ASSERT_NE(command, nullptr);
auto targetCommand = dynamic_cast<CommandNewGfxResponse*>(command.get());
ASSERT_NE(targetCommand, nullptr);
ASSERT_EQ(sourceCommand, *targetCommand);
}
TEST(GfxCommandSerializer, CommandShutdownSerializeAndUnserializeSuccessfully)
{
CommandShutDown sourceCommand;
auto data = CommandSerializer::serialize(&sourceCommand);
ASSERT_NE(data, nullptr);
StringReader reader(std::move(*data));
auto command = CommandSerializer::unserialize(reader, 5000ms);
ASSERT_NE(command, nullptr);
auto targetCommand = dynamic_cast<CommandShutDown*>(command.get());
ASSERT_NE(targetCommand, nullptr);
ASSERT_EQ(sourceCommand, *targetCommand);
}
TEST(GfxCommandSerializer, CommandShutdownResponseSerializeAndUnserializeSuccessfully)
{
CommandShutDownResponse sourceCommand;
auto data = CommandSerializer::serialize(&sourceCommand);
ASSERT_NE(data, nullptr);
StringReader reader(std::move(*data));
auto command = CommandSerializer::unserialize(reader, 5000ms);
ASSERT_NE(command, nullptr);
auto targetCommand = dynamic_cast<CommandShutDownResponse*>(command.get());
ASSERT_NE(targetCommand, nullptr);
ASSERT_EQ(sourceCommand, *targetCommand);
}
TEST(GfxCommandSerializer, CommandHelloSerializeAndUnserializeSuccessfully)
{
CommandHello sourceCommand;
auto data = CommandSerializer::serialize(&sourceCommand);
ASSERT_NE(data, nullptr);
StringReader reader(std::move(*data));
auto command = CommandSerializer::unserialize(reader, 5000ms);
ASSERT_NE(command, nullptr);
auto targetCommand = dynamic_cast<CommandHello*>(command.get());
ASSERT_NE(targetCommand, nullptr);
ASSERT_EQ(sourceCommand, *targetCommand);
}
TEST(GfxCommandSerializer, CommandHelloResponseSerializeAndUnserializeSuccessfully)
{
CommandHelloResponse sourceCommand;
auto data = CommandSerializer::serialize(&sourceCommand);
ASSERT_NE(data, nullptr);
StringReader reader(std::move(*data));
auto command = CommandSerializer::unserialize(reader, 5000ms);
ASSERT_NE(command, nullptr);
auto targetCommand = dynamic_cast<CommandHelloResponse*>(command.get());
ASSERT_NE(targetCommand, nullptr);
ASSERT_EQ(sourceCommand, *targetCommand);
}
TEST(GfxCommandSerializer, CommandSupportFormatsSerializeAndUnserializeSuccessfully)
{
CommandSupportFormats sourceCommand;
auto data = CommandSerializer::serialize(&sourceCommand);
ASSERT_NE(data, nullptr);
StringReader reader(std::move(*data));
auto command = CommandSerializer::unserialize(reader, 5000ms);
ASSERT_NE(command, nullptr);
auto targetCommand = dynamic_cast<CommandSupportFormats*>(command.get());
ASSERT_NE(targetCommand, nullptr);
ASSERT_EQ(sourceCommand, *targetCommand);
}
TEST(GfxCommandSerializer, CommandSupportFormatsResponseSerializeAndUnserializeSuccessfully)
{
CommandSupportFormatsResponse sourceCommand;
auto data = CommandSerializer::serialize(&sourceCommand);
ASSERT_NE(data, nullptr);
StringReader reader(std::move(*data));
auto command = CommandSerializer::unserialize(reader, 5000ms);
ASSERT_NE(command, nullptr);
auto targetCommand = dynamic_cast<CommandSupportFormatsResponse*>(command.get());
ASSERT_NE(targetCommand, nullptr);
ASSERT_EQ(sourceCommand, *targetCommand);
}
|