1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/devtools/simple_devtools_protocol_client/simple_devtools_protocol_client.h"
#include <string>
#include <vector>
#include "base/containers/span.h"
#include "base/json/json_writer.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/notreached.h"
#include "base/values.h"
#include "content/public/browser/devtools_agent_host.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/mock_devtools_agent_host.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::ElementsAre;
namespace simple_devtools_protocol_client {
namespace {
class SimpleDevToolsProtocolClientTest : public SimpleDevToolsProtocolClient,
public testing::Test {
public:
SimpleDevToolsProtocolClientTest() {
AttachClient(new content::MockDevToolsAgentHost);
}
void RunUntilIdle() { task_environment_.RunUntilIdle(); }
private:
content::BrowserTaskEnvironment task_environment_;
};
class SimpleDevToolsProtocolClientSendCommandTest
: public SimpleDevToolsProtocolClientTest {
public:
SimpleDevToolsProtocolClientSendCommandTest() = default;
void SendCommand1() {
EXPECT_EQ(pending_response_map_.size(), 0ul);
SendCommand("command1",
base::BindOnce(&SimpleDevToolsProtocolClientSendCommandTest::
OnSendCommand1Response,
base::Unretained(this)));
RunUntilIdle();
}
void OnSendCommand1Response(base::Value::Dict params) {
EXPECT_EQ(*params.FindString("method"), "command1");
EXPECT_EQ(pending_response_map_.size(), 0ul);
SendCommand("command2",
base::BindOnce(&SimpleDevToolsProtocolClientSendCommandTest::
OnSendCommand2Response,
base::Unretained(this)));
RunUntilIdle();
}
void OnSendCommand2Response(base::Value::Dict params) {
EXPECT_EQ(*params.FindString("method"), "command2");
EXPECT_EQ(pending_response_map_.size(), 0ul);
SendCommand("command3",
base::BindOnce(
[](SimpleDevToolsProtocolClientSendCommandTest* self,
base::Value::Dict params) {
self->OnSendCommand3Response(std::move(params));
},
base::Unretained(this)));
RunUntilIdle();
}
void OnSendCommand3Response(base::Value::Dict params) {
EXPECT_EQ(*params.FindString("method"), "command3");
EXPECT_EQ(pending_response_map_.size(), 0ul);
}
};
TEST_F(SimpleDevToolsProtocolClientSendCommandTest, CallbackChain) {
// Verify command result dispatcher map is empty when calling
// chained commands.
SendCommand1();
}
class SimpleDevToolsProtocolClientEventHandlerTest
: public SimpleDevToolsProtocolClientTest {
public:
SimpleDevToolsProtocolClientEventHandlerTest() = default;
void SendEvent(const std::string& event_name) {
base::Value::Dict params;
params.Set("method", event_name);
std::string json;
base::JSONWriter::Write(base::Value(std::move(params)), &json);
DispatchProtocolMessage(agent_host_.get(), base::as_byte_span(json));
RunUntilIdle();
}
};
class SimpleDevToolsProtocolClientEventHandlerTraceTest
: public SimpleDevToolsProtocolClientEventHandlerTest {
public:
SimpleDevToolsProtocolClientEventHandlerTraceTest() = default;
void OnEvent1(const base::Value::Dict& params) {
received_events_.push_back(*params.FindString("method"));
}
void OnEvent2(const base::Value::Dict& params) {
received_events_.push_back(*params.FindString("method"));
}
void OnEventX(const base::Value::Dict& params) {
received_events_.push_back(*params.FindString("method"));
}
std::vector<std::string> received_events_;
};
TEST_F(SimpleDevToolsProtocolClientEventHandlerTraceTest,
AddRemoveEventHandler) {
EXPECT_EQ(event_handler_map_.size(), 0ul);
EventCallback event1_handler1 = base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerTraceTest::OnEvent1,
base::Unretained(this));
EventCallback event1_handler2 = base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerTraceTest::OnEvent1,
base::Unretained(this));
EventCallback event2_handler = base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerTraceTest::OnEvent2,
base::Unretained(this));
AddEventHandler("event1", event1_handler1);
EXPECT_EQ(event_handler_map_.size(), 1ul);
AddEventHandler("event1", event1_handler2);
EXPECT_EQ(event_handler_map_.size(), 1ul);
AddEventHandler("event2", event2_handler);
EXPECT_EQ(event_handler_map_.size(), 2ul);
// Event1 is received by two handlers, and event2 by one.
SendEvent("event1");
SendEvent("event2");
EXPECT_THAT(received_events_, ElementsAre("event1", "event1", "event2"));
received_events_.clear();
// Both events are received by their respective handlers once.
RemoveEventHandler("event1", event1_handler1);
SendEvent("event1");
SendEvent("event2");
EXPECT_EQ(event_handler_map_.size(), 2ul);
EXPECT_THAT(received_events_, ElementsAre("event1", "event2"));
received_events_.clear();
// Only the second event is received as the first one has no handlers.
RemoveEventHandler("event1", event1_handler2);
SendEvent("event1");
SendEvent("event2");
EXPECT_EQ(event_handler_map_.size(), 1ul);
EXPECT_THAT(received_events_, ElementsAre("event2"));
received_events_.clear();
// No events are received.
RemoveEventHandler("event2", event2_handler);
SendEvent("event1");
SendEvent("event2");
EXPECT_EQ(event_handler_map_.size(), 0ul);
EXPECT_TRUE(received_events_.empty());
}
TEST_F(SimpleDevToolsProtocolClientEventHandlerTraceTest,
AddRemoveAllEventHandlers) {
EXPECT_EQ(event_handler_map_.size(), 0ul);
EventCallback event1_handler1 = base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerTraceTest::OnEvent1,
base::Unretained(this));
EventCallback event1_handler2 = base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerTraceTest::OnEvent1,
base::Unretained(this));
EventCallback event2_handler = base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerTraceTest::OnEvent2,
base::Unretained(this));
AddEventHandler("event1", event1_handler1);
AddEventHandler("event1", event1_handler2);
AddEventHandler("event2", event2_handler);
SendEvent("event1");
SendEvent("event2");
EXPECT_THAT(received_events_, ElementsAre("event1", "event1", "event2"));
received_events_.clear();
RemoveEventHandler("event1", event1_handler1);
RemoveEventHandler("event1", event1_handler2);
SendEvent("event1");
SendEvent("event2");
EXPECT_THAT(received_events_, ElementsAre("event2"));
received_events_.clear();
RemoveEventHandler("event2", event2_handler);
SendEvent("event1");
SendEvent("event2");
EXPECT_TRUE(received_events_.empty());
received_events_.clear();
}
TEST_F(SimpleDevToolsProtocolClientEventHandlerTraceTest, EventsDispatching) {
AddEventHandler(
"event1",
base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerTraceTest::OnEvent1,
base::Unretained(this)));
AddEventHandler(
"event2",
base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerTraceTest::OnEvent2,
base::Unretained(this)));
SendEvent("event1");
SendEvent("event2");
SendEvent("event1");
SendEvent("event2");
SendEvent("event3");
// 'event3' is not expected and should be ignored.
EXPECT_THAT(received_events_,
ElementsAre("event1", "event2", "event1", "event2"));
}
class SimpleDevToolsProtocolClientEventHandlerNestedAddTest
: public SimpleDevToolsProtocolClientEventHandlerTest {
public:
SimpleDevToolsProtocolClientEventHandlerNestedAddTest() = default;
void OnEvent(const base::Value::Dict& params) {
received_events_.push_back(*params.FindString("method"));
AddEventHandler(
"event",
base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerNestedAddTest::OnEvent,
base::Unretained(this)));
AddEventHandler(
"event2",
base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerNestedAddTest::OnEvent2,
base::Unretained(this)));
}
void OnEvent2(const base::Value::Dict& params) {
received_events_.push_back(*params.FindString("method"));
AddEventHandler(
"event3",
base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerNestedAddTest::OnEvent3,
base::Unretained(this)));
}
void OnEvent3(const base::Value::Dict& params) {
received_events_.push_back(*params.FindString("method"));
}
std::vector<std::string> received_events_;
};
TEST_F(SimpleDevToolsProtocolClientEventHandlerNestedAddTest, ChainedAddEvent) {
AddEventHandler(
"event",
base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerNestedAddTest::OnEvent,
base::Unretained(this)));
SendEvent("event");
SendEvent("event2");
SendEvent("event3");
EXPECT_THAT(received_events_, ElementsAre("event", "event2", "event3"));
}
class SimpleDevToolsProtocolClientEventHandlerNestedRemoveTest
: public SimpleDevToolsProtocolClientEventHandlerTest {
public:
SimpleDevToolsProtocolClientEventHandlerNestedRemoveTest() = default;
void OnEvent(const base::Value::Dict& params) {
received_events_.push_back(*params.FindString("method"));
RemoveEventHandler("event", event_handler_);
RemoveEventHandler("event", event_handler1_);
RemoveEventHandler("event2", event_handler2_);
RemoveEventHandler("event3", event_handler3_);
}
void OnEvent1(const base::Value::Dict& params) {
received_events_.push_back(*params.FindString("method"));
}
void OnEvent2(const base::Value::Dict& params) {
received_events_.push_back(*params.FindString("method"));
}
void OnEvent3(const base::Value::Dict& params) {
received_events_.push_back(*params.FindString("method"));
}
std::vector<std::string> received_events_;
EventCallback event_handler_;
EventCallback event_handler1_;
EventCallback event_handler2_;
EventCallback event_handler3_;
};
TEST_F(SimpleDevToolsProtocolClientEventHandlerNestedRemoveTest,
NestedRemoveEvent) {
AddEventHandler(
"event",
event_handler_ = base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerNestedRemoveTest::OnEvent,
base::Unretained(this)));
AddEventHandler(
"event",
event_handler1_ = base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerNestedRemoveTest::OnEvent1,
base::Unretained(this)));
AddEventHandler(
"event2",
event_handler2_ = base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerNestedRemoveTest::OnEvent2,
base::Unretained(this)));
AddEventHandler(
"event3",
event_handler3_ = base::BindRepeating(
&SimpleDevToolsProtocolClientEventHandlerNestedRemoveTest::OnEvent3,
base::Unretained(this)));
SendEvent("event");
SendEvent("event2");
SendEvent("event3");
SendEvent("event");
// All event handlers were removed by the first event handler so we should
// only register the very first event.
EXPECT_THAT(received_events_, ElementsAre("event"));
}
class SelfDestructingSimpleDevToolsProtocolClient
: public SimpleDevToolsProtocolClient {
public:
void TryIt() {
std::string json_message = "{}";
SimpleDevToolsProtocolClient::DispatchProtocolMessage(
agent_host_.get(), base::as_byte_span(json_message));
// Delete self so that the task posted by the previous call has nowhere to
// go.
delete this;
}
void DispatchProtocolMessageTask(base::Value::Dict message) override {
NOTREACHED() << "use-after-free";
}
};
TEST(SimpleDevToolsProtocolClientTest, DestoroyClientInFlight) {
content::BrowserTaskEnvironment task_environment;
(new SelfDestructingSimpleDevToolsProtocolClient)->TryIt();
task_environment.RunUntilIdle();
}
} // namespace
} // namespace simple_devtools_protocol_client
|