OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/renderer/mock_render_thread.h" | 5 #include "chrome/renderer/mock_render_thread.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "chrome/common/ipc_message_utils.h" | 8 #include "chrome/common/ipc_message_utils.h" |
9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
11 | 11 |
12 MockRenderThread::MockRenderThread() | 12 MockRenderThread::MockRenderThread() |
13 : routing_id_(0), | 13 : routing_id_(0), |
14 opener_id_(0), | 14 opener_id_(0), |
15 widget_(NULL), | 15 widget_(NULL), |
16 reply_deserializer_(NULL) { | 16 reply_deserializer_(NULL) { |
17 } | 17 } |
18 | 18 |
19 MockRenderThread::~MockRenderThread() { | 19 MockRenderThread::~MockRenderThread() { |
20 // Don't leak the allocated message bodies. | |
21 ClearMessages(); | |
22 } | 20 } |
23 | 21 |
24 // Called by the Widget. The routing_id must match the routing id assigned | 22 // Called by the Widget. The routing_id must match the routing id assigned |
25 // to the Widget in reply to ViewHostMsg_CreateWidget message. | 23 // to the Widget in reply to ViewHostMsg_CreateWidget message. |
26 void MockRenderThread::AddRoute(int32 routing_id, | 24 void MockRenderThread::AddRoute(int32 routing_id, |
27 IPC::Channel::Listener* listener) { | 25 IPC::Channel::Listener* listener) { |
28 EXPECT_EQ(routing_id_, routing_id); | 26 EXPECT_EQ(routing_id_, routing_id); |
29 widget_ = listener; | 27 widget_ = listener; |
30 } | 28 } |
31 | 29 |
(...skipping 21 matching lines...) Expand all Loading... |
53 // messages. | 51 // messages. |
54 reply_deserializer_.reset( | 52 reply_deserializer_.reset( |
55 static_cast<IPC::SyncMessage*>(msg)->GetReplyDeserializer()); | 53 static_cast<IPC::SyncMessage*>(msg)->GetReplyDeserializer()); |
56 } | 54 } |
57 OnMessageReceived(*msg); | 55 OnMessageReceived(*msg); |
58 } | 56 } |
59 delete msg; | 57 delete msg; |
60 return true; | 58 return true; |
61 } | 59 } |
62 | 60 |
63 void MockRenderThread::ClearMessages() { | |
64 for (size_t i = 0; i < messages_.size(); i++) | |
65 delete[] messages_[i].second; | |
66 messages_.clear(); | |
67 } | |
68 | |
69 const IPC::Message* MockRenderThread::GetMessageAt(size_t index) const { | |
70 if (index >= messages_.size()) | |
71 return NULL; | |
72 return &messages_[index].first; | |
73 } | |
74 | |
75 const IPC::Message* MockRenderThread::GetFirstMessageMatching(uint16 id) const { | |
76 for (size_t i = 0; i < messages_.size(); i++) { | |
77 if (messages_[i].first.type() == id) | |
78 return &messages_[i].first; | |
79 } | |
80 return NULL; | |
81 } | |
82 | |
83 const IPC::Message* MockRenderThread::GetUniqueMessageMatching( | |
84 uint16 id) const { | |
85 size_t found_index = 0; | |
86 size_t found_count = 0; | |
87 for (size_t i = 0; i < messages_.size(); i++) { | |
88 if (messages_[i].first.type() == id) { | |
89 found_count++; | |
90 found_index = i; | |
91 } | |
92 } | |
93 if (found_count != 1) | |
94 return NULL; // Didn't find a unique one. | |
95 return &messages_[found_index].first; | |
96 } | |
97 | |
98 void MockRenderThread::SendCloseMessage() { | 61 void MockRenderThread::SendCloseMessage() { |
99 ViewMsg_Close msg(routing_id_); | 62 ViewMsg_Close msg(routing_id_); |
100 widget_->OnMessageReceived(msg); | 63 widget_->OnMessageReceived(msg); |
101 } | 64 } |
102 | 65 |
103 void MockRenderThread::OnMessageReceived(const IPC::Message& msg) { | 66 void MockRenderThread::OnMessageReceived(const IPC::Message& msg) { |
104 // Copy the message into a pair. This is tricky since the message doesn't | 67 // Save the message in the sink. |
105 // manage its data itself. | 68 sink_.OnMessageReceived(msg); |
106 char* data_copy; | |
107 if (msg.size()) { | |
108 data_copy = new char[msg.size()]; | |
109 memcpy(data_copy, msg.data(), msg.size()); | |
110 } else { | |
111 // Dummy data so we can treat everything the same. | |
112 data_copy = new char[1]; | |
113 data_copy[0] = 0; | |
114 } | |
115 | |
116 // Save the message. | |
117 messages_.push_back( | |
118 std::make_pair(IPC::Message(data_copy, msg.size()), data_copy)); | |
119 | 69 |
120 // Some messages we do special handling. | 70 // Some messages we do special handling. |
121 bool handled = true; | 71 bool handled = true; |
122 bool msg_is_ok = true; | 72 bool msg_is_ok = true; |
123 IPC_BEGIN_MESSAGE_MAP_EX(MockRenderThread, msg, msg_is_ok) | 73 IPC_BEGIN_MESSAGE_MAP_EX(MockRenderThread, msg, msg_is_ok) |
124 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget, OnMsgCreateWidget); | 74 IPC_MESSAGE_HANDLER(ViewHostMsg_CreateWidget, OnMsgCreateWidget); |
125 IPC_MESSAGE_UNHANDLED(handled = false) | 75 IPC_MESSAGE_UNHANDLED(handled = false) |
126 IPC_END_MESSAGE_MAP_EX() | 76 IPC_END_MESSAGE_MAP_EX() |
127 } | 77 } |
128 | 78 |
129 // The Widget expects to be returned valid route_id. | 79 // The Widget expects to be returned valid route_id. |
130 void MockRenderThread::OnMsgCreateWidget(int opener_id, | 80 void MockRenderThread::OnMsgCreateWidget(int opener_id, |
131 bool activatable, | 81 bool activatable, |
132 int* route_id) { | 82 int* route_id) { |
133 opener_id_ = opener_id; | 83 opener_id_ = opener_id; |
134 *route_id = routing_id_; | 84 *route_id = routing_id_; |
135 } | 85 } |
136 | |
OLD | NEW |