Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: ipc/ipc_channel_proxy_unittest.cc

Issue 248133002: Make IPC::ChannelProxy test check filter events (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix windows build Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #include "base/message_loop/message_loop.h" 7 #include "base/message_loop/message_loop.h"
8 #include "base/pickle.h" 8 #include "base/pickle.h"
9 #include "base/threading/thread.h" 9 #include "base/threading/thread.h"
10 #include "ipc/ipc_message.h" 10 #include "ipc/ipc_message.h"
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after
76 channel_->Send(new IPC::Message(message)); 76 channel_->Send(new IPC::Message(message));
77 return true; 77 return true;
78 } 78 }
79 79
80 private: 80 private:
81 IPC::Channel* channel_; 81 IPC::Channel* channel_;
82 }; 82 };
83 83
84 class MessageCountFilter : public IPC::ChannelProxy::MessageFilter { 84 class MessageCountFilter : public IPC::ChannelProxy::MessageFilter {
85 public: 85 public:
86 enum FilterEvent {
87 NONE,
88 FILTER_ADDED,
89 CHANNEL_CONNECTED,
90 CHANNEL_ERROR,
91 CHANNEL_CLOSING,
92 FILTER_REMOVED
93 };
86 MessageCountFilter() 94 MessageCountFilter()
87 : messages_received_(0), 95 : messages_received_(0),
88 supported_message_class_(0), 96 supported_message_class_(0),
89 is_global_filter_(true), 97 is_global_filter_(true),
90 filter_removed_(false), 98 last_filter_event_(NONE),
91 message_filtering_enabled_(false) {} 99 message_filtering_enabled_(false) {}
92 100
93 MessageCountFilter(uint32 supported_message_class) 101 MessageCountFilter(uint32 supported_message_class)
94 : messages_received_(0), 102 : messages_received_(0),
95 supported_message_class_(supported_message_class), 103 supported_message_class_(supported_message_class),
96 is_global_filter_(false), 104 is_global_filter_(false),
97 filter_removed_(false), 105 last_filter_event_(NONE),
98 message_filtering_enabled_(false) {} 106 message_filtering_enabled_(false) {}
99 107
108 virtual void OnFilterAdded(IPC::Channel* channel) OVERRIDE {
109 EXPECT_TRUE(channel);
110 EXPECT_EQ(NONE, last_filter_event_);
111 last_filter_event_ = FILTER_ADDED;
112 }
113
114 virtual void OnChannelConnected(int32_t peer_pid) OVERRIDE {
115 EXPECT_EQ(FILTER_ADDED, last_filter_event_);
116 EXPECT_NE(static_cast<int32_t>(base::kNullProcessId), peer_pid);
117 last_filter_event_ = CHANNEL_CONNECTED;
118 }
119
120 virtual void OnChannelError() OVERRIDE {
121 EXPECT_EQ(CHANNEL_CONNECTED, last_filter_event_);
122 last_filter_event_ = CHANNEL_ERROR;
123 }
124
125 virtual void OnChannelClosing() OVERRIDE {
126 // We may or may not have gotten OnChannelError; if not, the last event has
127 // to be OnChannelConnected.
128 if (last_filter_event_ != CHANNEL_ERROR)
129 EXPECT_EQ(CHANNEL_CONNECTED, last_filter_event_);
130 last_filter_event_ = CHANNEL_CLOSING;
131 }
132
100 virtual void OnFilterRemoved() OVERRIDE { 133 virtual void OnFilterRemoved() OVERRIDE {
101 EXPECT_FALSE(filter_removed_); 134 // If the channel didn't get a chance to connect, we might see the
102 filter_removed_ = true; 135 // OnFilterRemoved event with no other events preceding it. We still want
136 // OnFilterRemoved to be called to allow for deleting the Filter.
137 if (last_filter_event_ != NONE)
138 EXPECT_EQ(CHANNEL_CLOSING, last_filter_event_);
139 last_filter_event_ = FILTER_REMOVED;
103 } 140 }
104 141
105 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE { 142 virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
143 // We should always get the OnFilterAdded and OnChannelConnected events
144 // prior to any messages.
145 EXPECT_EQ(CHANNEL_CONNECTED, last_filter_event_);
146
106 if (!is_global_filter_) { 147 if (!is_global_filter_) {
107 EXPECT_EQ(supported_message_class_, IPC_MESSAGE_CLASS(message)); 148 EXPECT_EQ(supported_message_class_, IPC_MESSAGE_CLASS(message));
108 } 149 }
109 ++messages_received_; 150 ++messages_received_;
110 return message_filtering_enabled_; 151 return message_filtering_enabled_;
111 } 152 }
112 153
113 virtual bool GetSupportedMessageClasses( 154 virtual bool GetSupportedMessageClasses(
114 std::vector<uint32>* supported_message_classes) const OVERRIDE { 155 std::vector<uint32>* supported_message_classes) const OVERRIDE {
115 if (is_global_filter_) 156 if (is_global_filter_)
116 return false; 157 return false;
117 supported_message_classes->push_back(supported_message_class_); 158 supported_message_classes->push_back(supported_message_class_);
118 return true; 159 return true;
119 } 160 }
120 161
121 void set_message_filtering_enabled(bool enabled) { 162 void set_message_filtering_enabled(bool enabled) {
122 message_filtering_enabled_ = enabled; 163 message_filtering_enabled_ = enabled;
123 } 164 }
124 165
125 size_t messages_received() const { return messages_received_; } 166 size_t messages_received() const { return messages_received_; }
126 bool filter_removed() const { return filter_removed_; } 167 FilterEvent last_filter_event() const { return last_filter_event_; }
127 168
128 private: 169 private:
129 virtual ~MessageCountFilter() {} 170 virtual ~MessageCountFilter() {}
130 171
131 size_t messages_received_; 172 size_t messages_received_;
132 uint32 supported_message_class_; 173 uint32 supported_message_class_;
133 bool is_global_filter_; 174 bool is_global_filter_;
134 bool filter_removed_; 175
176 FilterEvent last_filter_event_;
135 bool message_filtering_enabled_; 177 bool message_filtering_enabled_;
136 }; 178 };
137 179
138 class IPCChannelProxyTest : public IPCTestBase { 180 class IPCChannelProxyTest : public IPCTestBase {
139 public: 181 public:
140 IPCChannelProxyTest() {} 182 IPCChannelProxyTest() {}
141 virtual ~IPCChannelProxyTest() {} 183 virtual ~IPCChannelProxyTest() {}
142 184
143 virtual void SetUp() OVERRIDE { 185 virtual void SetUp() OVERRIDE {
144 IPCTestBase::SetUp(); 186 IPCTestBase::SetUp();
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 channel_proxy()->AddFilter(global_filter.get()); 283 channel_proxy()->AddFilter(global_filter.get());
242 channel_proxy()->RemoveFilter(global_filter.get()); 284 channel_proxy()->RemoveFilter(global_filter.get());
243 channel_proxy()->RemoveFilter(class_filter.get()); 285 channel_proxy()->RemoveFilter(class_filter.get());
244 286
245 // Send some messages; they should not be seen by either filter. 287 // Send some messages; they should not be seen by either filter.
246 Send(sender(), 0, SEND); 288 Send(sender(), 0, SEND);
247 Send(sender(), kMessageClass, SEND); 289 Send(sender(), kMessageClass, SEND);
248 290
249 // Ensure that the filters were removed and did not receive any messages. 291 // Ensure that the filters were removed and did not receive any messages.
250 SendQuitMessageAndWaitForIdle(); 292 SendQuitMessageAndWaitForIdle();
251 EXPECT_TRUE(global_filter->filter_removed()); 293 EXPECT_EQ(MessageCountFilter::FILTER_REMOVED,
252 EXPECT_TRUE(class_filter->filter_removed()); 294 global_filter->last_filter_event());
295 EXPECT_EQ(MessageCountFilter::FILTER_REMOVED,
296 class_filter->last_filter_event());
253 EXPECT_EQ(0U, class_filter->messages_received()); 297 EXPECT_EQ(0U, class_filter->messages_received());
254 EXPECT_EQ(0U, global_filter->messages_received()); 298 EXPECT_EQ(0U, global_filter->messages_received());
255 } 299 }
256 300
257 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ChannelProxyClient) { 301 MULTIPROCESS_IPC_TEST_CLIENT_MAIN(ChannelProxyClient) {
258 base::MessageLoopForIO main_message_loop; 302 base::MessageLoopForIO main_message_loop;
259 ChannelReflectorListener listener; 303 ChannelReflectorListener listener;
260 IPC::Channel channel(IPCTestBase::GetChannelName("ChannelProxyClient"), 304 IPC::Channel channel(IPCTestBase::GetChannelName("ChannelProxyClient"),
261 IPC::Channel::MODE_CLIENT, 305 IPC::Channel::MODE_CLIENT,
262 &listener); 306 &listener);
263 CHECK(channel.Connect()); 307 CHECK(channel.Connect());
264 listener.Init(&channel); 308 listener.Init(&channel);
265 309
266 base::MessageLoop::current()->Run(); 310 base::MessageLoop::current()->Run();
267 return 0; 311 return 0;
268 } 312 }
269 313
270 } // namespace 314 } // namespace
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698