#ifndef MOJO_PUBLIC_CPP_TEST_SUPPORT_TEST_UTILS_H_
#define MOJO_PUBLIC_CPP_TEST_SUPPORT_TEST_UTILS_H_
#include <string>
#include <utility>
#include "base/memory/raw_ref.h"
#include "base/run_loop.h"
#include "mojo/public/cpp/bindings/message.h"
#include "mojo/public/cpp/bindings/struct_ptr.h"
namespace mojo {
class MessagePipeHandle;
namespace test {
template <typename MojomType,
typename UserStructType,
std::enable_if_t<
!std::is_same<mojo::InlinedStructPtr<MojomType>,
std::remove_const_t<UserStructType>>::value &&
!std::is_same<mojo::StructPtr<MojomType>,
std::remove_const_t<UserStructType>>::value &&
!std::is_enum<UserStructType>::value,
int> = 0>
bool SerializeAndDeserialize(UserStructType& input,
std::remove_const_t<UserStructType>& output) {
mojo::Message message = MojomType::SerializeAsMessage(&input);
mojo::ScopedMessageHandle handle = message.TakeMojoMessage();
message = mojo::Message::CreateFromMessageHandle(&handle);
DCHECK(!message.IsNull());
return MojomType::DeserializeFromMessage(std::move(message), &output);
}
template <typename MojomType,
typename UserStructType,
typename MojomStructPtr,
std::enable_if_t<
(std::is_same<mojo::InlinedStructPtr<MojomType>,
std::remove_const_t<MojomStructPtr>>::value ||
std::is_same<mojo::StructPtr<MojomType>,
std::remove_const_t<MojomStructPtr>>::value) &&
!std::is_enum<UserStructType>::value,
int> = 0>
bool SerializeAndDeserialize(MojomStructPtr& input, UserStructType& output) {
mojo::Message message = MojomType::SerializeAsMessage(&input);
mojo::ScopedMessageHandle handle = message.TakeMojoMessage();
message = mojo::Message::CreateFromMessageHandle(&handle);
DCHECK(!message.IsNull());
return MojomType::DeserializeFromMessage(std::move(message), &output);
}
template <typename MojomType,
typename UserEnumType,
std::enable_if_t<std::is_enum<UserEnumType>::value, int> = 0>
bool SerializeAndDeserialize(UserEnumType input, UserEnumType& output) {
MojomType mode = mojo::EnumTraits<MojomType, UserEnumType>::ToMojom(input);
return mojo::EnumTraits<MojomType, UserEnumType>::FromMojom(mode, &output);
}
bool WriteTextMessage(const MessagePipeHandle& handle, const std::string& text);
bool ReadTextMessage(const MessagePipeHandle& handle, std::string* text);
bool DiscardMessage(const MessagePipeHandle& handle);
typedef void (*PerfTestSingleIteration)(void* closure);
void IterateAndReportPerf(const char* test_name,
const char* sub_test_name,
PerfTestSingleIteration single_iteration,
void* closure);
class BadMessageObserver {
public:
BadMessageObserver();
BadMessageObserver(const BadMessageObserver&) = delete;
BadMessageObserver& operator=(const BadMessageObserver&) = delete;
~BadMessageObserver();
std::string WaitForBadMessage();
bool got_bad_message() const { return got_bad_message_; }
private:
void OnReportBadMessage(const std::string& message);
std::string last_error_for_bad_message_;
bool got_bad_message_;
base::RunLoop run_loop_;
};
template <typename T>
class ScopedSwapImplForTesting {
public:
using ImplPointerType = typename T::ImplPointerType;
ScopedSwapImplForTesting(T& receiver, ImplPointerType new_impl)
: receiver_(receiver) {
old_impl_ = receiver_->SwapImplForTesting(new_impl);
}
~ScopedSwapImplForTesting() {
std::ignore = receiver_->SwapImplForTesting(old_impl_);
}
ImplPointerType old_impl() const { return old_impl_; }
ScopedSwapImplForTesting(const ScopedSwapImplForTesting&) = delete;
ScopedSwapImplForTesting& operator=(const ScopedSwapImplForTesting&) = delete;
private:
const raw_ref<T> receiver_;
ImplPointerType old_impl_;
};
}
}
#endif