summaryrefslogtreecommitdiffstats
path: root/chrome
diff options
context:
space:
mode:
authorbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-17 05:41:48 +0000
committerbrettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-01-17 05:41:48 +0000
commitcde96e19f7142ab29d80d96ebf6719bbcd8f06fe (patch)
tree5ac81cc7a3a968f291338b5b46d6471665455ae2 /chrome
parente900bb3e43ce97892cdeb0c79ce503befbdaf4c8 (diff)
downloadchromium_src-cde96e19f7142ab29d80d96ebf6719bbcd8f06fe.zip
chromium_src-cde96e19f7142ab29d80d96ebf6719bbcd8f06fe.tar.gz
chromium_src-cde96e19f7142ab29d80d96ebf6719bbcd8f06fe.tar.bz2
Factor out the message sink from MockRenderThread to a separate class. I will be
using this in a RenderView test in the future. Review URL: http://codereview.chromium.org/18326 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8255 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome')
-rw-r--r--chrome/common/ipc_test_sink.cc51
-rw-r--r--chrome/common/ipc_test_sink.h76
-rw-r--r--chrome/renderer/mock_render_thread.cc55
-rw-r--r--chrome/renderer/mock_render_thread.h35
-rw-r--r--chrome/renderer/render_view_unittest.cc12
5 files changed, 141 insertions, 88 deletions
diff --git a/chrome/common/ipc_test_sink.cc b/chrome/common/ipc_test_sink.cc
new file mode 100644
index 0000000..7a461b6
--- /dev/null
+++ b/chrome/common/ipc_test_sink.cc
@@ -0,0 +1,51 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/common/ipc_test_sink.h"
+
+namespace IPC {
+
+TestSink::TestSink() {
+}
+
+TestSink::~TestSink() {
+}
+
+void TestSink::OnMessageReceived(const Message& msg) {
+ messages_.push_back(Message(msg));
+}
+
+void TestSink::ClearMessages() {
+ messages_.clear();
+}
+
+const Message* TestSink::GetMessageAt(size_t index) const {
+ if (index >= messages_.size())
+ return NULL;
+ return &messages_[index];
+}
+
+const Message* TestSink::GetFirstMessageMatching(uint16 id) const {
+ for (size_t i = 0; i < messages_.size(); i++) {
+ if (messages_[i].type() == id)
+ return &messages_[i];
+ }
+ return NULL;
+}
+
+const Message* TestSink::GetUniqueMessageMatching(uint16 id) const {
+ size_t found_index = 0;
+ size_t found_count = 0;
+ for (size_t i = 0; i < messages_.size(); i++) {
+ if (messages_[i].type() == id) {
+ found_count++;
+ found_index = i;
+ }
+ }
+ if (found_count != 1)
+ return NULL; // Didn't find a unique one.
+ return &messages_[found_index];
+}
+
+} // namespace IPC
diff --git a/chrome/common/ipc_test_sink.h b/chrome/common/ipc_test_sink.h
new file mode 100644
index 0000000..db4096d
--- /dev/null
+++ b/chrome/common/ipc_test_sink.h
@@ -0,0 +1,76 @@
+// Copyright (c) 2009 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_COMMON_IPC_TEST_SINK_H_
+#define CHROME_COMMON_IPC_TEST_SINK_H_
+
+#include <utility>
+#include <vector>
+
+#include "base/basictypes.h"
+#include "chrome/common/ipc_message.h"
+
+namespace IPC {
+
+// This test sink provides a "sink" for IPC messages that are sent. It allows
+// the caller to query messages received in various different ways. It is
+// designed for tests for objects that use the IPC system.
+//
+// Typical usage:
+//
+// test_sink.ClearMessages();
+// do_something();
+//
+// // We should have gotten exactly one update state message.
+// EXPECT_TRUE(test_sink.GetUniqeMessageMatching(ViewHostMsg_Update::ID));
+// // ...and no start load messages.
+// EXPECT_FALSE(test_sink.GetFirstMessageMatching(ViewHostMsg_Start::ID));
+//
+// // Go on to the next phase of the test.
+// test_sink.ClearMessages();
+//
+// To hook up the sink, all you need to do is call OnMessageReceived when a
+// message is recieved.
+class TestSink {
+ public:
+ TestSink();
+ ~TestSink();
+
+ // Used by the source of the messages to send the message to the sink. This
+ // will make a copy of the message and store it in the list.
+ void OnMessageReceived(const Message& msg);
+
+ // Returns the number of messages in the queue.
+ size_t message_count() const { return messages_.size(); }
+
+ // Clears the message queue of saved messages.
+ void ClearMessages();
+
+ // Returns the message at the given index in the queue. The index may be out
+ // of range, in which case the return value is NULL. The returned pointer will
+ // only be valid until another message is received or the list is cleared.
+ const Message* GetMessageAt(size_t index) const;
+
+ // Returns the first message with the given ID in the queue. If there is no
+ // message with the given ID, returns NULL. The returned pointer will only be
+ // valid until another message is received or the list is cleared.
+ const Message* GetFirstMessageMatching(uint16 id) const;
+
+ // Returns the message with the given ID in the queue. If there is no such
+ // message or there is more than one of that message, this will return NULL
+ // (with the expectation that you'll do an ASSERT_TRUE() on the result).
+ // The returned pointer will only be valid until another message is received
+ // or the list is cleared.
+ const Message* GetUniqueMessageMatching(uint16 id) const;
+
+ private:
+ // The actual list of received messages.
+ std::vector<Message> messages_;
+
+ DISALLOW_COPY_AND_ASSIGN(TestSink);
+};
+
+} // namespace IPC
+
+#endif // CHROME_COMMON_IPC_TEST_SINK_H_
diff --git a/chrome/renderer/mock_render_thread.cc b/chrome/renderer/mock_render_thread.cc
index a100a01..8bf07aa 100644
--- a/chrome/renderer/mock_render_thread.cc
+++ b/chrome/renderer/mock_render_thread.cc
@@ -17,8 +17,6 @@ MockRenderThread::MockRenderThread()
}
MockRenderThread::~MockRenderThread() {
- // Don't leak the allocated message bodies.
- ClearMessages();
}
// Called by the Widget. The routing_id must match the routing id assigned
@@ -60,62 +58,14 @@ bool MockRenderThread::Send(IPC::Message* msg) {
return true;
}
-void MockRenderThread::ClearMessages() {
- for (size_t i = 0; i < messages_.size(); i++)
- delete[] messages_[i].second;
- messages_.clear();
-}
-
-const IPC::Message* MockRenderThread::GetMessageAt(size_t index) const {
- if (index >= messages_.size())
- return NULL;
- return &messages_[index].first;
-}
-
-const IPC::Message* MockRenderThread::GetFirstMessageMatching(uint16 id) const {
- for (size_t i = 0; i < messages_.size(); i++) {
- if (messages_[i].first.type() == id)
- return &messages_[i].first;
- }
- return NULL;
-}
-
-const IPC::Message* MockRenderThread::GetUniqueMessageMatching(
- uint16 id) const {
- size_t found_index = 0;
- size_t found_count = 0;
- for (size_t i = 0; i < messages_.size(); i++) {
- if (messages_[i].first.type() == id) {
- found_count++;
- found_index = i;
- }
- }
- if (found_count != 1)
- return NULL; // Didn't find a unique one.
- return &messages_[found_index].first;
-}
-
void MockRenderThread::SendCloseMessage() {
ViewMsg_Close msg(routing_id_);
widget_->OnMessageReceived(msg);
}
void MockRenderThread::OnMessageReceived(const IPC::Message& msg) {
- // Copy the message into a pair. This is tricky since the message doesn't
- // manage its data itself.
- char* data_copy;
- if (msg.size()) {
- data_copy = new char[msg.size()];
- memcpy(data_copy, msg.data(), msg.size());
- } else {
- // Dummy data so we can treat everything the same.
- data_copy = new char[1];
- data_copy[0] = 0;
- }
-
- // Save the message.
- messages_.push_back(
- std::make_pair(IPC::Message(data_copy, msg.size()), data_copy));
+ // Save the message in the sink.
+ sink_.OnMessageReceived(msg);
// Some messages we do special handling.
bool handled = true;
@@ -133,4 +83,3 @@ void MockRenderThread::OnMsgCreateWidget(int opener_id,
opener_id_ = opener_id;
*route_id = routing_id_;
}
-
diff --git a/chrome/renderer/mock_render_thread.h b/chrome/renderer/mock_render_thread.h
index 5293461..4ed785f 100644
--- a/chrome/renderer/mock_render_thread.h
+++ b/chrome/renderer/mock_render_thread.h
@@ -7,6 +7,7 @@
#include <vector>
+#include "chrome/common/ipc_test_sink.h"
#include "chrome/renderer/render_thread.h"
// This class is very simple mock of RenderThread. It simulates an IPC channel
@@ -15,13 +16,12 @@
// ViewMsg_Close : async, send to the Widget.
class MockRenderThread : public RenderThreadBase {
public:
- // Encapusulates an IPC message and its associated data (which is not
- // otherwise bound to the lifetime of the message).
- typedef std::pair<IPC::Message, char*> MessagePair;
-
MockRenderThread();
virtual ~MockRenderThread();
+ // Provides access to the messages that have been received by this thread.
+ IPC::TestSink& sink() { return sink_; }
+
// Called by the Widget. Not used in the test.
virtual bool InSend() const {
return false;
@@ -60,29 +60,6 @@ class MockRenderThread : public RenderThreadBase {
return widget_ ? true : false;
}
- // Returns the number of messages in the queue.
- size_t message_count() const { return messages_.size(); }
-
- // Clears the message queue of saved messages.
- void ClearMessages();
-
- // Returns the message at the given index in the queue. The index may be out
- // of range, in which case the return value is NULL. The returned pointer will
- // only be valid until another message is received or the list is cleared.
- const IPC::Message* GetMessageAt(size_t index) const;
-
- // Returns the first message with the given ID in the queue. If there is no
- // message with the given ID, returns NULL. The returned pointer will only be
- // valid until another message is received or the list is cleared.
- const IPC::Message* GetFirstMessageMatching(uint16 id) const;
-
- // Returns the message with the given ID in the queue. If there is no such
- // message or there is more than one of that message, this will return NULL
- // (with the expectation that you'll do an ASSERT_TRUE() on the result).
- // The returned pointer will only be valid until another message is received
- // or the list is cleared.
- const IPC::Message* GetUniqueMessageMatching(uint16 id) const;
-
// Simulates the Widget receiving a close message. This should result
// on releasing the internal reference counts and destroying the internal
// state.
@@ -97,6 +74,8 @@ class MockRenderThread : public RenderThreadBase {
bool activatable,
int* route_id);
+ IPC::TestSink sink_;
+
// Routing id what will be assigned to the Widget.
int32 routing_id_;
@@ -109,8 +88,6 @@ class MockRenderThread : public RenderThreadBase {
// The last known good deserializer for sync messages.
scoped_ptr<IPC::MessageReplyDeserializer> reply_deserializer_;
-
- std::vector<MessagePair> messages_;
};
#endif // CHROME_RENDERER_MOCK_RENDER_THREAD_H_
diff --git a/chrome/renderer/render_view_unittest.cc b/chrome/renderer/render_view_unittest.cc
index 2477c93..ffb61ad 100644
--- a/chrome/renderer/render_view_unittest.cc
+++ b/chrome/renderer/render_view_unittest.cc
@@ -96,11 +96,11 @@ TEST_F(RenderViewTest, OnLoadAlternateHTMLText) {
// We should have gotten two different types of start messages in the
// following order.
- ASSERT_EQ(2, render_thread_.message_count());
- const IPC::Message* msg = render_thread_.GetMessageAt(0);
+ ASSERT_EQ(2, render_thread_.sink().message_count());
+ const IPC::Message* msg = render_thread_.sink().GetMessageAt(0);
EXPECT_EQ(ViewHostMsg_DidStartLoading::ID, msg->type());
- msg = render_thread_.GetMessageAt(1);
+ msg = render_thread_.sink().GetMessageAt(1);
EXPECT_EQ(ViewHostMsg_DidStartProvisionalLoadForFrame::ID, msg->type());
ViewHostMsg_DidStartProvisionalLoadForFrame::Param start_params;
ViewHostMsg_DidStartProvisionalLoadForFrame::Read(msg, &start_params);
@@ -117,14 +117,14 @@ TEST_F(RenderViewTest, OnNavStateChanged) {
LoadHTML("<input type=\"text\" id=\"elt_text\"></input>");
// We should NOT have gotten a form state change notification yet.
- EXPECT_FALSE(render_thread_.GetFirstMessageMatching(
+ EXPECT_FALSE(render_thread_.sink().GetFirstMessageMatching(
ViewHostMsg_UpdateState::ID));
- render_thread_.ClearMessages();
+ render_thread_.sink().ClearMessages();
// Change the value of the input. We should have gotten an update state
// notification. We need to spin the message loop to catch this update.
ExecuteJavaScript("document.getElementById('elt_text').value = 'foo';");
ProcessPendingMessages();
- EXPECT_TRUE(render_thread_.GetUniqueMessageMatching(
+ EXPECT_TRUE(render_thread_.sink().GetUniqueMessageMatching(
ViewHostMsg_UpdateState::ID));
}