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

Side by Side Diff: components/devtools_http_handler/devtools_http_handler_unittest.cc

Issue 2300703005: DevTools: merge devtools_http_handler into content - it is used in all the embedders anyways. (Closed)
Patch Set: for_landing! Created 4 years, 3 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
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/devtools_http_handler/devtools_http_handler.h"
6
7 #include <stdint.h>
8
9 #include <memory>
10 #include <utility>
11
12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h"
14 #include "base/location.h"
15 #include "base/memory/ptr_util.h"
16 #include "base/run_loop.h"
17 #include "base/single_thread_task_runner.h"
18 #include "base/strings/string_number_conversions.h"
19 #include "base/threading/thread_task_runner_handle.h"
20 #include "base/values.h"
21 #include "components/devtools_http_handler/devtools_http_handler_delegate.h"
22 #include "content/public/browser/devtools_socket_factory.h"
23 #include "content/public/test/test_browser_thread_bundle.h"
24 #include "content/public/test/test_utils.h"
25 #include "net/base/ip_address.h"
26 #include "net/base/ip_endpoint.h"
27 #include "net/base/net_errors.h"
28 #include "net/socket/server_socket.h"
29 #include "testing/gtest/include/gtest/gtest.h"
30
31 using content::BrowserThread;
32
33 namespace devtools_http_handler {
34 namespace {
35
36 const uint16_t kDummyPort = 4321;
37 const base::FilePath::CharType kDevToolsActivePortFileName[] =
38 FILE_PATH_LITERAL("DevToolsActivePort");
39
40 class DummyServerSocket : public net::ServerSocket {
41 public:
42 DummyServerSocket() {}
43
44 // net::ServerSocket "implementation"
45 int Listen(const net::IPEndPoint& address, int backlog) override {
46 return net::OK;
47 }
48
49 int GetLocalAddress(net::IPEndPoint* address) const override {
50 *address = net::IPEndPoint(net::IPAddress::IPv4Localhost(), kDummyPort);
51 return net::OK;
52 }
53
54 int Accept(std::unique_ptr<net::StreamSocket>* socket,
55 const net::CompletionCallback& callback) override {
56 return net::ERR_IO_PENDING;
57 }
58 };
59
60 void QuitFromHandlerThread(const base::Closure& quit_closure) {
61 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_closure);
62 }
63
64 class DummyServerSocketFactory : public content::DevToolsSocketFactory {
65 public:
66 DummyServerSocketFactory(base::Closure quit_closure_1,
67 base::Closure quit_closure_2)
68 : quit_closure_1_(quit_closure_1),
69 quit_closure_2_(quit_closure_2) {}
70
71 ~DummyServerSocketFactory() override {
72 BrowserThread::PostTask(
73 BrowserThread::UI, FROM_HERE, quit_closure_2_);
74 }
75
76 protected:
77 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
78 base::ThreadTaskRunnerHandle::Get()->PostTask(
79 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_));
80 return base::WrapUnique(new DummyServerSocket());
81 }
82
83 std::unique_ptr<net::ServerSocket> CreateForTethering(
84 std::string* out_name) override {
85 return nullptr;
86 }
87
88 base::Closure quit_closure_1_;
89 base::Closure quit_closure_2_;
90 };
91
92 class FailingServerSocketFactory : public DummyServerSocketFactory {
93 public:
94 FailingServerSocketFactory(const base::Closure& quit_closure_1,
95 const base::Closure& quit_closure_2)
96 : DummyServerSocketFactory(quit_closure_1, quit_closure_2) {
97 }
98
99 private:
100 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
101 base::ThreadTaskRunnerHandle::Get()->PostTask(
102 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_));
103 return nullptr;
104 }
105 };
106
107 class DummyDelegate : public DevToolsHttpHandlerDelegate {
108 public:
109 std::string GetDiscoveryPageHTML() override { return std::string(); }
110
111 std::string GetFrontendResource(const std::string& path) override {
112 return std::string();
113 }
114 };
115
116 }
117
118 class DevToolsHttpHandlerTest : public testing::Test {
119 public:
120 DevToolsHttpHandlerTest() : testing::Test() { }
121
122 private:
123 content::TestBrowserThreadBundle thread_bundle_;
124 };
125
126 TEST_F(DevToolsHttpHandlerTest, TestStartStop) {
127 base::RunLoop run_loop, run_loop_2;
128 std::unique_ptr<content::DevToolsSocketFactory> factory(
129 new DummyServerSocketFactory(run_loop.QuitClosure(),
130 run_loop_2.QuitClosure()));
131 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler(
132 new DevToolsHttpHandler(std::move(factory), std::string(),
133 new DummyDelegate(), base::FilePath(),
134 base::FilePath(), std::string(), std::string()));
135 // Our dummy socket factory will post a quit message once the server will
136 // become ready.
137 run_loop.Run();
138 devtools_http_handler.reset();
139 // Make sure the handler actually stops.
140 run_loop_2.Run();
141 }
142
143 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) {
144 base::RunLoop run_loop, run_loop_2;
145 std::unique_ptr<content::DevToolsSocketFactory> factory(
146 new FailingServerSocketFactory(run_loop.QuitClosure(),
147 run_loop_2.QuitClosure()));
148 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler(
149 new DevToolsHttpHandler(std::move(factory), std::string(),
150 new DummyDelegate(), base::FilePath(),
151 base::FilePath(), std::string(), std::string()));
152 // Our dummy socket factory will post a quit message once the server will
153 // become ready.
154 run_loop.Run();
155 for (int i = 0; i < 5; i++) {
156 RunAllPendingInMessageLoop(BrowserThread::UI);
157 RunAllPendingInMessageLoop(BrowserThread::FILE);
158 }
159 devtools_http_handler.reset();
160 // Make sure the handler actually stops.
161 run_loop_2.Run();
162 }
163
164
165 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) {
166 base::RunLoop run_loop, run_loop_2;
167 base::ScopedTempDir temp_dir;
168 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
169 std::unique_ptr<content::DevToolsSocketFactory> factory(
170 new DummyServerSocketFactory(run_loop.QuitClosure(),
171 run_loop_2.QuitClosure()));
172 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler(
173 new DevToolsHttpHandler(std::move(factory), std::string(),
174 new DummyDelegate(), temp_dir.path(),
175 base::FilePath(), std::string(), std::string()));
176 // Our dummy socket factory will post a quit message once the server will
177 // become ready.
178 run_loop.Run();
179 devtools_http_handler.reset();
180 // Make sure the handler actually stops.
181 run_loop_2.Run();
182
183 // Now make sure the DevToolsActivePort was written into the
184 // temporary directory and its contents are as expected.
185 base::FilePath active_port_file = temp_dir.path().Append(
186 kDevToolsActivePortFileName);
187 EXPECT_TRUE(base::PathExists(active_port_file));
188 std::string file_contents;
189 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents));
190 int port = 0;
191 EXPECT_TRUE(base::StringToInt(file_contents, &port));
192 EXPECT_EQ(static_cast<int>(kDummyPort), port);
193 }
194
195 } // namespace devtools_http_handler
OLDNEW
« no previous file with comments | « components/devtools_http_handler/devtools_http_handler_delegate.h ('k') | content/browser/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698