1 // Copyright (c) 2012 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.
5 #include "ipc/ipc_message.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/values.h"
11 #include "ipc/ipc_message_utils.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 // IPC messages for testing ----------------------------------------------------
16 #define IPC_MESSAGE_IMPL
17 #include "ipc/ipc_message_macros.h"
19 #define IPC_MESSAGE_START TestMsgStart
21 IPC_MESSAGE_CONTROL0(TestMsgClassEmpty
)
23 IPC_MESSAGE_CONTROL1(TestMsgClassI
, int)
25 IPC_SYNC_MESSAGE_CONTROL1_1(TestMsgClassIS
, int, std::string
)
29 TEST(IPCMessageTest
, ListValue
) {
30 base::ListValue input
;
31 input
.Set(0, new base::FundamentalValue(42.42));
32 input
.Set(1, new base::StringValue("forty"));
33 input
.Set(2, base::Value::CreateNullValue());
35 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
36 IPC::WriteParam(&msg
, input
);
38 base::ListValue output
;
39 base::PickleIterator
iter(msg
);
40 EXPECT_TRUE(IPC::ReadParam(&msg
, &iter
, &output
));
42 EXPECT_TRUE(input
.Equals(&output
));
44 // Also test the corrupt case.
45 IPC::Message
bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
47 iter
= base::PickleIterator(bad_msg
);
48 EXPECT_FALSE(IPC::ReadParam(&bad_msg
, &iter
, &output
));
51 TEST(IPCMessageTest
, DictionaryValue
) {
52 base::DictionaryValue input
;
53 input
.Set("null", base::Value::CreateNullValue());
54 input
.Set("bool", new base::FundamentalValue(true));
55 input
.Set("int", new base::FundamentalValue(42));
56 input
.SetWithoutPathExpansion("int.with.dot", new base::FundamentalValue(43));
58 scoped_ptr
<base::DictionaryValue
> subdict(new base::DictionaryValue());
59 subdict
->Set("str", new base::StringValue("forty two"));
60 subdict
->Set("bool", new base::FundamentalValue(false));
62 scoped_ptr
<base::ListValue
> sublist(new base::ListValue());
63 sublist
->Set(0, new base::FundamentalValue(42.42));
64 sublist
->Set(1, new base::StringValue("forty"));
65 sublist
->Set(2, new base::StringValue("two"));
66 subdict
->Set("list", sublist
.release());
68 input
.Set("dict", subdict
.release());
70 IPC::Message
msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
71 IPC::WriteParam(&msg
, input
);
73 base::DictionaryValue output
;
74 base::PickleIterator
iter(msg
);
75 EXPECT_TRUE(IPC::ReadParam(&msg
, &iter
, &output
));
77 EXPECT_TRUE(input
.Equals(&output
));
79 // Also test the corrupt case.
80 IPC::Message
bad_msg(1, 2, IPC::Message::PRIORITY_NORMAL
);
82 iter
= base::PickleIterator(bad_msg
);
83 EXPECT_FALSE(IPC::ReadParam(&bad_msg
, &iter
, &output
));
86 class IPCMessageParameterTest
: public testing::Test
{
88 IPCMessageParameterTest() : extra_param_("extra_param"), called_(false) {}
90 bool OnMessageReceived(const IPC::Message
& message
) {
92 IPC_BEGIN_MESSAGE_MAP_WITH_PARAM(IPCMessageParameterTest
, message
,
94 IPC_MESSAGE_HANDLER(TestMsgClassEmpty
, OnEmpty
)
95 IPC_MESSAGE_HANDLER(TestMsgClassI
, OnInt
)
96 //IPC_MESSAGE_HANDLER(TestMsgClassIS, OnSync)
97 IPC_MESSAGE_UNHANDLED(handled
= false)
103 void OnEmpty(std::string
* extra_param
) {
104 EXPECT_EQ(extra_param
, &extra_param_
);
108 void OnInt(std::string
* extra_param
, int foo
) {
109 EXPECT_EQ(extra_param
, &extra_param_
);
114 /* TODO: handle sync IPCs
115 void OnSync(std::string* extra_param, int foo, std::string* out) {
116 EXPECT_EQ(extra_param, &extra_param_);
119 *out = std::string("out");
122 bool Send(IPC::Message* reply) {
127 std::string extra_param_
;
131 TEST_F(IPCMessageParameterTest
, EmptyDispatcherWithParam
) {
132 TestMsgClassEmpty message
;
133 EXPECT_TRUE(OnMessageReceived(message
));
134 EXPECT_TRUE(called_
);
137 TEST_F(IPCMessageParameterTest
, OneIntegerWithParam
) {
138 TestMsgClassI
message(42);
139 EXPECT_TRUE(OnMessageReceived(message
));
140 EXPECT_TRUE(called_
);
143 /* TODO: handle sync IPCs
144 TEST_F(IPCMessageParameterTest, Sync) {
146 TestMsgClassIS message(42, &output);
147 EXPECT_TRUE(OnMessageReceived(message));
148 EXPECT_TRUE(called_);
149 EXPECT_EQ(output, std::string("out"));