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
fuchsia_web / common / test / test_debug_listener.cc [blame]
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "fuchsia_web/common/test/test_debug_listener.h"
#include "base/auto_reset.h"
#include "base/containers/contains.h"
#include "base/run_loop.h"
#include "base/test/bind.h"
#include "testing/gtest/include/gtest/gtest.h"
TestDebugListener::TestDebugListener() = default;
TestDebugListener::~TestDebugListener() = default;
void TestDebugListener::DestroyListener(TestPerContextListener* listener) {
EXPECT_EQ(per_context_listeners_.erase(listener), 1u);
}
void TestDebugListener::AddPort(uint16_t port) {
EXPECT_FALSE(base::Contains(debug_ports_, port));
debug_ports_.insert(port);
if (on_debug_ports_changed_) {
on_debug_ports_changed_.Run();
}
}
void TestDebugListener::RemovePort(uint16_t port) {
EXPECT_EQ(debug_ports_.erase(port), 1u);
if (on_debug_ports_changed_) {
on_debug_ports_changed_.Run();
}
}
void TestDebugListener::RunUntilNumberOfPortsIs(size_t size) {
if (debug_ports_.size() == size) {
return;
}
base::RunLoop run_loop;
base::AutoReset<base::RepeatingClosure> set_on_debug_ports_changed(
&on_debug_ports_changed_,
base::BindLambdaForTesting([this, &run_loop, size]() {
if (debug_ports_.size() == size) {
run_loop.Quit();
}
}));
run_loop.Run();
ASSERT_EQ(debug_ports_.size(), size);
}
TestDebugListener::TestPerContextListener::TestPerContextListener(
TestDebugListener* test_debug_listener,
fidl::InterfaceRequest<fuchsia::web::DevToolsPerContextListener> listener)
: test_debug_listener_(test_debug_listener),
binding_(this, std::move(listener)) {
binding_.set_error_handler([this](zx_status_t) {
if (port_ != 0) {
test_debug_listener_->RemovePort(port_);
}
test_debug_listener_->DestroyListener(this);
});
}
TestDebugListener::TestPerContextListener::~TestPerContextListener() = default;
void TestDebugListener::TestPerContextListener::OnHttpPortOpen(uint16_t port) {
// If `port` is non-zero then the PerContextListener has created, or replaced,
// its DevTools port. If `port` is zero then the PerContextListener is
// reporting that it is not listening on DevTools.
// Remove the previously-reported `port_`, if any, from the TestDebugListener,
// before adding the new `port`, if set, to maintain the list of available
// DevTools ports.
if (port_ != 0) {
test_debug_listener_->RemovePort(port_);
}
port_ = port;
if (port != 0) {
test_debug_listener_->AddPort(port);
}
}
void TestDebugListener::OnContextDevToolsAvailable(
fidl::InterfaceRequest<fuchsia::web::DevToolsPerContextListener> listener) {
per_context_listeners_.insert(
std::make_unique<TestPerContextListener>(this, std::move(listener)));
}