OLD | NEW |
---|---|
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 "base/files/file_util.h" | 5 #include "base/files/file_util.h" |
6 #include "base/files/scoped_temp_dir.h" | 6 #include "base/files/scoped_temp_dir.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
10 #include "content/browser/browser_thread_impl.h" | 10 #include "content/browser/browser_thread_impl.h" |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
44 *address = net::IPEndPoint(number, kDummyPort); | 44 *address = net::IPEndPoint(number, kDummyPort); |
45 return net::OK; | 45 return net::OK; |
46 } | 46 } |
47 | 47 |
48 int Accept(scoped_ptr<net::StreamSocket>* socket, | 48 int Accept(scoped_ptr<net::StreamSocket>* socket, |
49 const net::CompletionCallback& callback) override { | 49 const net::CompletionCallback& callback) override { |
50 return net::ERR_IO_PENDING; | 50 return net::ERR_IO_PENDING; |
51 } | 51 } |
52 }; | 52 }; |
53 | 53 |
54 void NoOp() { | |
55 } | |
56 | |
57 void QuitFromUI(const base::Closure& quit_closure) { | |
58 BrowserThread::PostTaskAndReply(BrowserThread::FILE, FROM_HERE, | |
59 base::Bind(&NoOp), | |
60 quit_closure); | |
61 } | |
62 | |
63 void QuitFromHandlerThread(const base::Closure& quit_closure) { | |
64 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
65 base::Bind(&QuitFromUI, quit_closure)); | |
66 } | |
67 | |
54 class DummyServerSocketFactory | 68 class DummyServerSocketFactory |
55 : public DevToolsHttpHandler::ServerSocketFactory { | 69 : public DevToolsHttpHandler::ServerSocketFactory { |
56 public: | 70 public: |
57 DummyServerSocketFactory(base::Closure quit_closure_1, | 71 DummyServerSocketFactory(base::Closure quit_closure_1, |
58 base::Closure quit_closure_2) | 72 base::Closure quit_closure_2) |
59 : DevToolsHttpHandler::ServerSocketFactory("", 0, 0), | 73 : DevToolsHttpHandler::ServerSocketFactory("", 0, 0), |
60 quit_closure_1_(quit_closure_1), | 74 quit_closure_1_(quit_closure_1), |
61 quit_closure_2_(quit_closure_2) {} | 75 quit_closure_2_(quit_closure_2) {} |
62 | 76 |
63 ~DummyServerSocketFactory() override { | 77 ~DummyServerSocketFactory() override { |
64 BrowserThread::PostTask( | 78 BrowserThread::PostTask( |
65 BrowserThread::UI, FROM_HERE, quit_closure_2_); | 79 BrowserThread::UI, FROM_HERE, quit_closure_2_); |
66 } | 80 } |
67 | 81 |
68 private: | 82 protected: |
69 scoped_ptr<net::ServerSocket> Create() const override { | 83 scoped_ptr<net::ServerSocket> Create() const override { |
70 BrowserThread::PostTask( | 84 base::MessageLoopProxy::current()->PostTask(FROM_HERE, |
71 BrowserThread::UI, FROM_HERE, quit_closure_1_); | 85 base::Bind(&QuitFromHandlerThread, quit_closure_1_)); |
72 return scoped_ptr<net::ServerSocket>(new DummyServerSocket()); | 86 return scoped_ptr<net::ServerSocket>(new DummyServerSocket()); |
73 } | 87 } |
74 | 88 |
75 base::Closure quit_closure_1_; | 89 base::Closure quit_closure_1_; |
76 base::Closure quit_closure_2_; | 90 base::Closure quit_closure_2_; |
77 }; | 91 }; |
78 | 92 |
93 class FailingServerSocketFactory : public DummyServerSocketFactory { | |
94 public: | |
95 FailingServerSocketFactory(const base::Closure& quit_closure_1, | |
96 const base::Closure& quit_closure_2) | |
97 : DummyServerSocketFactory(quit_closure_1, quit_closure_2) { | |
98 } | |
99 | |
100 private: | |
101 scoped_ptr<net::ServerSocket> Create() const override { | |
102 base::MessageLoopProxy::current()->PostTask(FROM_HERE, | |
dgozman
2014/11/06 14:46:22
Can we use RunAllPendingInMessageLoop for all the
vkuzkokov
2014/11/06 15:46:06
Done.
| |
103 base::Bind(&QuitFromHandlerThread, quit_closure_1_)); | |
104 return nullptr; | |
105 } | |
106 }; | |
107 | |
79 class DummyDelegate : public DevToolsHttpHandlerDelegate { | 108 class DummyDelegate : public DevToolsHttpHandlerDelegate { |
80 public: | 109 public: |
81 std::string GetDiscoveryPageHTML() override { return std::string(); } | 110 std::string GetDiscoveryPageHTML() override { return std::string(); } |
82 | 111 |
83 bool BundlesFrontendResources() override { return true; } | 112 bool BundlesFrontendResources() override { return true; } |
84 | 113 |
85 base::FilePath GetDebugFrontendDir() override { return base::FilePath(); } | 114 base::FilePath GetDebugFrontendDir() override { return base::FilePath(); } |
86 | 115 |
87 scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( | 116 scoped_ptr<net::StreamListenSocket> CreateSocketForTethering( |
88 net::StreamListenSocket::Delegate* delegate, | 117 net::StreamListenSocket::Delegate* delegate, |
(...skipping 22 matching lines...) Expand all Loading... | |
111 base::MessageLoopForIO message_loop_; | 140 base::MessageLoopForIO message_loop_; |
112 BrowserThreadImpl ui_thread_; | 141 BrowserThreadImpl ui_thread_; |
113 scoped_ptr<BrowserThreadImpl> file_thread_; | 142 scoped_ptr<BrowserThreadImpl> file_thread_; |
114 }; | 143 }; |
115 | 144 |
116 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { | 145 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { |
117 base::RunLoop run_loop, run_loop_2; | 146 base::RunLoop run_loop, run_loop_2; |
118 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | 147 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
119 new DummyServerSocketFactory(run_loop.QuitClosure(), | 148 new DummyServerSocketFactory(run_loop.QuitClosure(), |
120 run_loop_2.QuitClosure())); | 149 run_loop_2.QuitClosure())); |
121 content::DevToolsHttpHandler* devtools_http_handler_ = | 150 content::DevToolsHttpHandler* devtools_http_handler = |
122 content::DevToolsHttpHandler::Start(factory.Pass(), | 151 content::DevToolsHttpHandler::Start(factory.Pass(), |
123 std::string(), | 152 std::string(), |
124 new DummyDelegate(), | 153 new DummyDelegate(), |
125 base::FilePath()); | 154 base::FilePath()); |
126 // Our dummy socket factory will post a quit message once the server will | 155 // Our dummy socket factory will post a quit message once the server will |
127 // become ready. | 156 // become ready. |
128 run_loop.Run(); | 157 run_loop.Run(); |
129 devtools_http_handler_->Stop(); | 158 devtools_http_handler->Stop(); |
130 // Make sure the handler actually stops. | 159 // Make sure the handler actually stops. |
131 run_loop_2.Run(); | 160 run_loop_2.Run(); |
132 } | 161 } |
133 | 162 |
163 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) { | |
164 base::RunLoop run_loop, run_loop_2; | |
165 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | |
166 new FailingServerSocketFactory(run_loop.QuitClosure(), | |
167 run_loop_2.QuitClosure())); | |
168 content::DevToolsHttpHandler* devtools_http_handler = | |
169 content::DevToolsHttpHandler::Start(factory.Pass(), | |
170 std::string(), | |
171 new DummyDelegate(), | |
172 base::FilePath()); | |
173 // Our dummy socket factory will post a quit message once the server will | |
174 // become ready. | |
175 run_loop.Run(); | |
176 devtools_http_handler->Stop(); | |
177 // Make sure the handler actually stops. | |
178 run_loop_2.Run(); | |
179 } | |
180 | |
181 | |
134 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { | 182 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { |
135 base::RunLoop run_loop, run_loop_2; | 183 base::RunLoop run_loop, run_loop_2; |
136 base::ScopedTempDir temp_dir; | 184 base::ScopedTempDir temp_dir; |
137 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); | 185 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); |
138 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | 186 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
139 new DummyServerSocketFactory(run_loop.QuitClosure(), | 187 new DummyServerSocketFactory(run_loop.QuitClosure(), |
140 run_loop_2.QuitClosure())); | 188 run_loop_2.QuitClosure())); |
141 content::DevToolsHttpHandler* devtools_http_handler_ = | 189 content::DevToolsHttpHandler* devtools_http_handler = |
142 content::DevToolsHttpHandler::Start(factory.Pass(), | 190 content::DevToolsHttpHandler::Start(factory.Pass(), |
143 std::string(), | 191 std::string(), |
144 new DummyDelegate(), | 192 new DummyDelegate(), |
145 temp_dir.path()); | 193 temp_dir.path()); |
146 // Our dummy socket factory will post a quit message once the server will | 194 // Our dummy socket factory will post a quit message once the server will |
147 // become ready. | 195 // become ready. |
148 run_loop.Run(); | 196 run_loop.Run(); |
149 devtools_http_handler_->Stop(); | 197 devtools_http_handler->Stop(); |
150 // Make sure the handler actually stops. | 198 // Make sure the handler actually stops. |
151 run_loop_2.Run(); | 199 run_loop_2.Run(); |
152 | 200 |
153 // Now make sure the DevToolsActivePort was written into the | 201 // Now make sure the DevToolsActivePort was written into the |
154 // temporary directory and its contents are as expected. | 202 // temporary directory and its contents are as expected. |
155 base::FilePath active_port_file = temp_dir.path().Append( | 203 base::FilePath active_port_file = temp_dir.path().Append( |
156 kDevToolsActivePortFileName); | 204 kDevToolsActivePortFileName); |
157 EXPECT_TRUE(base::PathExists(active_port_file)); | 205 EXPECT_TRUE(base::PathExists(active_port_file)); |
158 std::string file_contents; | 206 std::string file_contents; |
159 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); | 207 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); |
160 int port = 0; | 208 int port = 0; |
161 EXPECT_TRUE(base::StringToInt(file_contents, &port)); | 209 EXPECT_TRUE(base::StringToInt(file_contents, &port)); |
162 EXPECT_EQ(kDummyPort, port); | 210 EXPECT_EQ(kDummyPort, port); |
163 } | 211 } |
164 | 212 |
165 } // namespace content | 213 } // namespace content |
OLD | NEW |