1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef MOJO_PUBLIC_CPP_TEST_SUPPORT_TEST_UTILS_H_
6 #define MOJO_PUBLIC_CPP_TEST_SUPPORT_TEST_UTILS_H_
7
8 #include <string>
9
10 #include "mojo/public/cpp/system/core.h"
11
12 #include "mojo/public/cpp/bindings/message.h"
13
14 namespace mojo {
15 namespace test {
16
17 template <typename MojomType, typename UserType>
SerializeAndDeserialize(UserType * input,UserType * output)18 bool SerializeAndDeserialize(UserType* input, UserType* output) {
19 mojo::Message message = MojomType::SerializeAsMessage(input);
20
21 // This accurately simulates full serialization to ensure that all attached
22 // handles are serialized as well. Necessary for DeserializeFromMessage to
23 // work properly.
24 message = mojo::Message(message.TakeMojoMessage());
25
26 return MojomType::DeserializeFromMessage(std::move(message), output);
27 }
28
29 // Writes a message to |handle| with message data |text|. Returns true on
30 // success.
31 bool WriteTextMessage(const MessagePipeHandle& handle, const std::string& text);
32
33 // Reads a message from |handle|, putting its contents into |*text|. Returns
34 // true on success. (This blocks if necessary and will call |MojoReadMessage()|
35 // multiple times, e.g., to query the size of the message.)
36 bool ReadTextMessage(const MessagePipeHandle& handle, std::string* text);
37
38 // Discards a message from |handle|. Returns true on success. (This does not
39 // block. It will fail if no message is available to discard.)
40 bool DiscardMessage(const MessagePipeHandle& handle);
41
42 // Run |single_iteration| an appropriate number of times and report its
43 // performance appropriately. (This actually runs |single_iteration| for a fixed
44 // amount of time and reports the number of iterations per unit time.)
45 typedef void (*PerfTestSingleIteration)(void* closure);
46 void IterateAndReportPerf(const char* test_name,
47 const char* sub_test_name,
48 PerfTestSingleIteration single_iteration,
49 void* closure);
50
51 } // namespace test
52 } // namespace mojo
53
54 #endif // MOJO_PUBLIC_CPP_TEST_SUPPORT_TEST_UTILS_H_
55