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

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

Issue 2295623002: DevTools: simplify http handler delegate as it is moving into content. (Closed)
Patch Set: lcean 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
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "components/devtools_http_handler/devtools_http_handler.h" 5 #include "components/devtools_http_handler/devtools_http_handler.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
11 11
12 #include "base/files/file_util.h" 12 #include "base/files/file_util.h"
13 #include "base/files/scoped_temp_dir.h" 13 #include "base/files/scoped_temp_dir.h"
14 #include "base/location.h" 14 #include "base/location.h"
15 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
16 #include "base/run_loop.h" 16 #include "base/run_loop.h"
17 #include "base/single_thread_task_runner.h" 17 #include "base/single_thread_task_runner.h"
18 #include "base/strings/string_number_conversions.h" 18 #include "base/strings/string_number_conversions.h"
19 #include "base/threading/thread_task_runner_handle.h" 19 #include "base/threading/thread_task_runner_handle.h"
20 #include "base/values.h" 20 #include "base/values.h"
21 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" 21 #include "components/devtools_http_handler/devtools_http_handler_delegate.h"
22 #include "content/public/browser/devtools_socket_factory.h"
22 #include "content/public/test/test_browser_thread_bundle.h" 23 #include "content/public/test/test_browser_thread_bundle.h"
23 #include "content/public/test/test_utils.h" 24 #include "content/public/test/test_utils.h"
24 #include "net/base/ip_address.h" 25 #include "net/base/ip_address.h"
25 #include "net/base/ip_endpoint.h" 26 #include "net/base/ip_endpoint.h"
26 #include "net/base/net_errors.h" 27 #include "net/base/net_errors.h"
27 #include "net/socket/server_socket.h" 28 #include "net/socket/server_socket.h"
28 #include "testing/gtest/include/gtest/gtest.h" 29 #include "testing/gtest/include/gtest/gtest.h"
29 30
30 using content::BrowserThread; 31 using content::BrowserThread;
31 32
(...skipping 21 matching lines...) Expand all
53 int Accept(std::unique_ptr<net::StreamSocket>* socket, 54 int Accept(std::unique_ptr<net::StreamSocket>* socket,
54 const net::CompletionCallback& callback) override { 55 const net::CompletionCallback& callback) override {
55 return net::ERR_IO_PENDING; 56 return net::ERR_IO_PENDING;
56 } 57 }
57 }; 58 };
58 59
59 void QuitFromHandlerThread(const base::Closure& quit_closure) { 60 void QuitFromHandlerThread(const base::Closure& quit_closure) {
60 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_closure); 61 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, quit_closure);
61 } 62 }
62 63
63 class DummyServerSocketFactory 64 class DummyServerSocketFactory : public content::DevToolsSocketFactory {
64 : public DevToolsHttpHandler::ServerSocketFactory {
65 public: 65 public:
66 DummyServerSocketFactory(base::Closure quit_closure_1, 66 DummyServerSocketFactory(base::Closure quit_closure_1,
67 base::Closure quit_closure_2) 67 base::Closure quit_closure_2)
68 : quit_closure_1_(quit_closure_1), 68 : quit_closure_1_(quit_closure_1),
69 quit_closure_2_(quit_closure_2) {} 69 quit_closure_2_(quit_closure_2) {}
70 70
71 ~DummyServerSocketFactory() override { 71 ~DummyServerSocketFactory() override {
72 BrowserThread::PostTask( 72 BrowserThread::PostTask(
73 BrowserThread::UI, FROM_HERE, quit_closure_2_); 73 BrowserThread::UI, FROM_HERE, quit_closure_2_);
74 } 74 }
75 75
76 protected: 76 protected:
77 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { 77 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
78 base::ThreadTaskRunnerHandle::Get()->PostTask( 78 base::ThreadTaskRunnerHandle::Get()->PostTask(
79 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_)); 79 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_));
80 return base::MakeUnique<DummyServerSocket>(); 80 return base::WrapUnique(new DummyServerSocket());
81 }
82
83 std::unique_ptr<net::ServerSocket> CreateForTethering(
84 std::string* out_name) override {
85 return nullptr;
81 } 86 }
82 87
83 base::Closure quit_closure_1_; 88 base::Closure quit_closure_1_;
84 base::Closure quit_closure_2_; 89 base::Closure quit_closure_2_;
85 }; 90 };
86 91
87 class FailingServerSocketFactory : public DummyServerSocketFactory { 92 class FailingServerSocketFactory : public DummyServerSocketFactory {
88 public: 93 public:
89 FailingServerSocketFactory(const base::Closure& quit_closure_1, 94 FailingServerSocketFactory(const base::Closure& quit_closure_1,
90 const base::Closure& quit_closure_2) 95 const base::Closure& quit_closure_2)
91 : DummyServerSocketFactory(quit_closure_1, quit_closure_2) { 96 : DummyServerSocketFactory(quit_closure_1, quit_closure_2) {
92 } 97 }
93 98
94 private: 99 private:
95 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override { 100 std::unique_ptr<net::ServerSocket> CreateForHttpServer() override {
96 base::ThreadTaskRunnerHandle::Get()->PostTask( 101 base::ThreadTaskRunnerHandle::Get()->PostTask(
97 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_)); 102 FROM_HERE, base::Bind(&QuitFromHandlerThread, quit_closure_1_));
98 return nullptr; 103 return nullptr;
99 } 104 }
100 }; 105 };
101 106
102 class DummyDelegate : public DevToolsHttpHandlerDelegate { 107 class DummyDelegate : public DevToolsHttpHandlerDelegate {
103 public: 108 public:
104 std::string GetDiscoveryPageHTML() override { return std::string(); } 109 std::string GetDiscoveryPageHTML() override { return std::string(); }
105 110
106 std::string GetFrontendResource(const std::string& path) override { 111 std::string GetFrontendResource(const std::string& path) override {
107 return std::string(); 112 return std::string();
108 } 113 }
109
110 std::string GetPageThumbnailData(const GURL& url) override {
111 return std::string();
112 }
113
114 content::DevToolsExternalAgentProxyDelegate*
115 HandleWebSocketConnection(const std::string& path) override {
116 return nullptr;
117 }
118 }; 114 };
119 115
120 } 116 }
121 117
122 class DevToolsHttpHandlerTest : public testing::Test { 118 class DevToolsHttpHandlerTest : public testing::Test {
123 public: 119 public:
124 DevToolsHttpHandlerTest() : testing::Test() { } 120 DevToolsHttpHandlerTest() : testing::Test() { }
125 121
126 private: 122 private:
127 content::TestBrowserThreadBundle thread_bundle_; 123 content::TestBrowserThreadBundle thread_bundle_;
128 }; 124 };
129 125
130 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { 126 TEST_F(DevToolsHttpHandlerTest, TestStartStop) {
131 base::RunLoop run_loop, run_loop_2; 127 base::RunLoop run_loop, run_loop_2;
132 std::unique_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( 128 std::unique_ptr<content::DevToolsSocketFactory> factory(
133 new DummyServerSocketFactory(run_loop.QuitClosure(), 129 new DummyServerSocketFactory(run_loop.QuitClosure(),
134 run_loop_2.QuitClosure())); 130 run_loop_2.QuitClosure()));
135 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( 131 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler(
136 new DevToolsHttpHandler(std::move(factory), std::string(), 132 new DevToolsHttpHandler(std::move(factory), std::string(),
137 new DummyDelegate(), base::FilePath(), 133 new DummyDelegate(), base::FilePath(),
138 base::FilePath(), std::string(), std::string())); 134 base::FilePath(), std::string(), std::string()));
139 // Our dummy socket factory will post a quit message once the server will 135 // Our dummy socket factory will post a quit message once the server will
140 // become ready. 136 // become ready.
141 run_loop.Run(); 137 run_loop.Run();
142 devtools_http_handler.reset(); 138 devtools_http_handler.reset();
143 // Make sure the handler actually stops. 139 // Make sure the handler actually stops.
144 run_loop_2.Run(); 140 run_loop_2.Run();
145 } 141 }
146 142
147 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) { 143 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) {
148 base::RunLoop run_loop, run_loop_2; 144 base::RunLoop run_loop, run_loop_2;
149 std::unique_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( 145 std::unique_ptr<content::DevToolsSocketFactory> factory(
150 new FailingServerSocketFactory(run_loop.QuitClosure(), 146 new FailingServerSocketFactory(run_loop.QuitClosure(),
151 run_loop_2.QuitClosure())); 147 run_loop_2.QuitClosure()));
152 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( 148 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler(
153 new DevToolsHttpHandler(std::move(factory), std::string(), 149 new DevToolsHttpHandler(std::move(factory), std::string(),
154 new DummyDelegate(), base::FilePath(), 150 new DummyDelegate(), base::FilePath(),
155 base::FilePath(), std::string(), std::string())); 151 base::FilePath(), std::string(), std::string()));
156 // Our dummy socket factory will post a quit message once the server will 152 // Our dummy socket factory will post a quit message once the server will
157 // become ready. 153 // become ready.
158 run_loop.Run(); 154 run_loop.Run();
159 for (int i = 0; i < 5; i++) { 155 for (int i = 0; i < 5; i++) {
160 RunAllPendingInMessageLoop(BrowserThread::UI); 156 RunAllPendingInMessageLoop(BrowserThread::UI);
161 RunAllPendingInMessageLoop(BrowserThread::FILE); 157 RunAllPendingInMessageLoop(BrowserThread::FILE);
162 } 158 }
163 devtools_http_handler.reset(); 159 devtools_http_handler.reset();
164 // Make sure the handler actually stops. 160 // Make sure the handler actually stops.
165 run_loop_2.Run(); 161 run_loop_2.Run();
166 } 162 }
167 163
168 164
169 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { 165 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) {
170 base::RunLoop run_loop, run_loop_2; 166 base::RunLoop run_loop, run_loop_2;
171 base::ScopedTempDir temp_dir; 167 base::ScopedTempDir temp_dir;
172 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); 168 EXPECT_TRUE(temp_dir.CreateUniqueTempDir());
173 std::unique_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( 169 std::unique_ptr<content::DevToolsSocketFactory> factory(
174 new DummyServerSocketFactory(run_loop.QuitClosure(), 170 new DummyServerSocketFactory(run_loop.QuitClosure(),
175 run_loop_2.QuitClosure())); 171 run_loop_2.QuitClosure()));
176 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler( 172 std::unique_ptr<DevToolsHttpHandler> devtools_http_handler(
177 new DevToolsHttpHandler(std::move(factory), std::string(), 173 new DevToolsHttpHandler(std::move(factory), std::string(),
178 new DummyDelegate(), temp_dir.path(), 174 new DummyDelegate(), temp_dir.path(),
179 base::FilePath(), std::string(), std::string())); 175 base::FilePath(), std::string(), std::string()));
180 // Our dummy socket factory will post a quit message once the server will 176 // Our dummy socket factory will post a quit message once the server will
181 // become ready. 177 // become ready.
182 run_loop.Run(); 178 run_loop.Run();
183 devtools_http_handler.reset(); 179 devtools_http_handler.reset();
184 // Make sure the handler actually stops. 180 // Make sure the handler actually stops.
185 run_loop_2.Run(); 181 run_loop_2.Run();
186 182
187 // Now make sure the DevToolsActivePort was written into the 183 // Now make sure the DevToolsActivePort was written into the
188 // temporary directory and its contents are as expected. 184 // temporary directory and its contents are as expected.
189 base::FilePath active_port_file = temp_dir.path().Append( 185 base::FilePath active_port_file = temp_dir.path().Append(
190 kDevToolsActivePortFileName); 186 kDevToolsActivePortFileName);
191 EXPECT_TRUE(base::PathExists(active_port_file)); 187 EXPECT_TRUE(base::PathExists(active_port_file));
192 std::string file_contents; 188 std::string file_contents;
193 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); 189 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents));
194 int port = 0; 190 int port = 0;
195 EXPECT_TRUE(base::StringToInt(file_contents, &port)); 191 EXPECT_TRUE(base::StringToInt(file_contents, &port));
196 EXPECT_EQ(static_cast<int>(kDummyPort), port); 192 EXPECT_EQ(static_cast<int>(kDummyPort), port);
197 } 193 }
198 194
199 } // namespace devtools_http_handler 195 } // namespace devtools_http_handler
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698