| 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 "base/values.h" |
| 10 #include "components/devtools_http_handler/devtools_http_handler.h" | 11 #include "components/devtools_http_handler/devtools_http_handler.h" |
| 11 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" | 12 #include "components/devtools_http_handler/devtools_http_handler_delegate.h" |
| 12 #include "content/public/browser/devtools_target.h" | |
| 13 #include "content/public/test/test_browser_thread_bundle.h" | 13 #include "content/public/test/test_browser_thread_bundle.h" |
| 14 #include "content/public/test/test_utils.h" | 14 #include "content/public/test/test_utils.h" |
| 15 #include "net/base/ip_endpoint.h" | 15 #include "net/base/ip_endpoint.h" |
| 16 #include "net/base/net_errors.h" | 16 #include "net/base/net_errors.h" |
| 17 #include "net/socket/server_socket.h" | 17 #include "net/socket/server_socket.h" |
| 18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 19 | 19 |
| 20 using content::BrowserThread; | 20 using content::BrowserThread; |
| 21 | 21 |
| 22 namespace devtools_http_handler { | 22 namespace devtools_http_handler { |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 } | 91 } |
| 92 }; | 92 }; |
| 93 | 93 |
| 94 class DummyDelegate : public DevToolsHttpHandlerDelegate { | 94 class DummyDelegate : public DevToolsHttpHandlerDelegate { |
| 95 public: | 95 public: |
| 96 std::string GetDiscoveryPageHTML() override { return std::string(); } | 96 std::string GetDiscoveryPageHTML() override { return std::string(); } |
| 97 | 97 |
| 98 std::string GetFrontendResource(const std::string& path) override { | 98 std::string GetFrontendResource(const std::string& path) override { |
| 99 return std::string(); | 99 return std::string(); |
| 100 } | 100 } |
| 101 }; | |
| 102 | |
| 103 class DummyManagerDelegate : public content::DevToolsManagerDelegate { | |
| 104 public: | |
| 105 ~DummyManagerDelegate() override {} | |
| 106 | |
| 107 void Inspect(content::BrowserContext* browser_context, | |
| 108 content::DevToolsAgentHost* agent_host) override {} | |
| 109 | |
| 110 void DevToolsAgentStateChanged(content::DevToolsAgentHost* agent_host, | |
| 111 bool attached) override {} | |
| 112 | |
| 113 base::DictionaryValue* HandleCommand( | |
| 114 content::DevToolsAgentHost* agent_host, | |
| 115 base::DictionaryValue* command) override { | |
| 116 return NULL; | |
| 117 } | |
| 118 | |
| 119 scoped_ptr<content::DevToolsTarget> CreateNewTarget( | |
| 120 const GURL& url) override { | |
| 121 return scoped_ptr<content::DevToolsTarget>(); | |
| 122 } | |
| 123 | |
| 124 void EnumerateTargets(TargetCallback callback) override { | |
| 125 TargetList result; | |
| 126 callback.Run(result); | |
| 127 } | |
| 128 | 101 |
| 129 std::string GetPageThumbnailData(const GURL& url) override { | 102 std::string GetPageThumbnailData(const GURL& url) override { |
| 130 return std::string(); | 103 return std::string(); |
| 131 } | 104 } |
| 132 }; | 105 }; |
| 133 | 106 |
| 134 } | 107 } |
| 135 | 108 |
| 136 class DevToolsHttpHandlerTest : public testing::Test { | 109 class DevToolsHttpHandlerTest : public testing::Test { |
| 137 public: | 110 public: |
| 138 DevToolsHttpHandlerTest() : testing::Test() { } | 111 DevToolsHttpHandlerTest() : testing::Test() { } |
| 139 | 112 |
| 140 protected: | |
| 141 DummyManagerDelegate manager_delegate_; | |
| 142 | |
| 143 private: | 113 private: |
| 144 content::TestBrowserThreadBundle thread_bundle_; | 114 content::TestBrowserThreadBundle thread_bundle_; |
| 145 }; | 115 }; |
| 146 | 116 |
| 147 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { | 117 TEST_F(DevToolsHttpHandlerTest, TestStartStop) { |
| 148 base::RunLoop run_loop, run_loop_2; | 118 base::RunLoop run_loop, run_loop_2; |
| 149 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | 119 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
| 150 new DummyServerSocketFactory(run_loop.QuitClosure(), | 120 new DummyServerSocketFactory(run_loop.QuitClosure(), |
| 151 run_loop_2.QuitClosure())); | 121 run_loop_2.QuitClosure())); |
| 152 scoped_ptr<DevToolsHttpHandler> devtools_http_handler( | 122 scoped_ptr<DevToolsHttpHandler> devtools_http_handler( |
| 153 new DevToolsHttpHandler(factory.Pass(), | 123 new DevToolsHttpHandler(factory.Pass(), |
| 154 std::string(), | 124 std::string(), |
| 155 new DummyDelegate(), | 125 new DummyDelegate(), |
| 156 &manager_delegate_, | |
| 157 base::FilePath(), | 126 base::FilePath(), |
| 158 base::FilePath(), | 127 base::FilePath(), |
| 159 std::string(), | 128 std::string(), |
| 160 std::string())); | 129 std::string())); |
| 161 // Our dummy socket factory will post a quit message once the server will | 130 // Our dummy socket factory will post a quit message once the server will |
| 162 // become ready. | 131 // become ready. |
| 163 run_loop.Run(); | 132 run_loop.Run(); |
| 164 devtools_http_handler.reset(); | 133 devtools_http_handler.reset(); |
| 165 // Make sure the handler actually stops. | 134 // Make sure the handler actually stops. |
| 166 run_loop_2.Run(); | 135 run_loop_2.Run(); |
| 167 } | 136 } |
| 168 | 137 |
| 169 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) { | 138 TEST_F(DevToolsHttpHandlerTest, TestServerSocketFailed) { |
| 170 base::RunLoop run_loop, run_loop_2; | 139 base::RunLoop run_loop, run_loop_2; |
| 171 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | 140 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
| 172 new FailingServerSocketFactory(run_loop.QuitClosure(), | 141 new FailingServerSocketFactory(run_loop.QuitClosure(), |
| 173 run_loop_2.QuitClosure())); | 142 run_loop_2.QuitClosure())); |
| 174 scoped_ptr<DevToolsHttpHandler> devtools_http_handler( | 143 scoped_ptr<DevToolsHttpHandler> devtools_http_handler( |
| 175 new DevToolsHttpHandler(factory.Pass(), | 144 new DevToolsHttpHandler(factory.Pass(), |
| 176 std::string(), | 145 std::string(), |
| 177 new DummyDelegate(), | 146 new DummyDelegate(), |
| 178 &manager_delegate_, | |
| 179 base::FilePath(), | 147 base::FilePath(), |
| 180 base::FilePath(), | 148 base::FilePath(), |
| 181 std::string(), | 149 std::string(), |
| 182 std::string())); | 150 std::string())); |
| 183 // Our dummy socket factory will post a quit message once the server will | 151 // Our dummy socket factory will post a quit message once the server will |
| 184 // become ready. | 152 // become ready. |
| 185 run_loop.Run(); | 153 run_loop.Run(); |
| 186 for (int i = 0; i < 5; i++) { | 154 for (int i = 0; i < 5; i++) { |
| 187 RunAllPendingInMessageLoop(BrowserThread::UI); | 155 RunAllPendingInMessageLoop(BrowserThread::UI); |
| 188 RunAllPendingInMessageLoop(BrowserThread::FILE); | 156 RunAllPendingInMessageLoop(BrowserThread::FILE); |
| 189 } | 157 } |
| 190 devtools_http_handler.reset(); | 158 devtools_http_handler.reset(); |
| 191 // Make sure the handler actually stops. | 159 // Make sure the handler actually stops. |
| 192 run_loop_2.Run(); | 160 run_loop_2.Run(); |
| 193 } | 161 } |
| 194 | 162 |
| 195 | 163 |
| 196 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { | 164 TEST_F(DevToolsHttpHandlerTest, TestDevToolsActivePort) { |
| 197 base::RunLoop run_loop, run_loop_2; | 165 base::RunLoop run_loop, run_loop_2; |
| 198 base::ScopedTempDir temp_dir; | 166 base::ScopedTempDir temp_dir; |
| 199 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); | 167 EXPECT_TRUE(temp_dir.CreateUniqueTempDir()); |
| 200 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( | 168 scoped_ptr<DevToolsHttpHandler::ServerSocketFactory> factory( |
| 201 new DummyServerSocketFactory(run_loop.QuitClosure(), | 169 new DummyServerSocketFactory(run_loop.QuitClosure(), |
| 202 run_loop_2.QuitClosure())); | 170 run_loop_2.QuitClosure())); |
| 203 scoped_ptr<DevToolsHttpHandler> devtools_http_handler( | 171 scoped_ptr<DevToolsHttpHandler> devtools_http_handler( |
| 204 new DevToolsHttpHandler(factory.Pass(), | 172 new DevToolsHttpHandler(factory.Pass(), |
| 205 std::string(), | 173 std::string(), |
| 206 new DummyDelegate(), | 174 new DummyDelegate(), |
| 207 &manager_delegate_, | |
| 208 temp_dir.path(), | 175 temp_dir.path(), |
| 209 base::FilePath(), | 176 base::FilePath(), |
| 210 std::string(), | 177 std::string(), |
| 211 std::string())); | 178 std::string())); |
| 212 // Our dummy socket factory will post a quit message once the server will | 179 // Our dummy socket factory will post a quit message once the server will |
| 213 // become ready. | 180 // become ready. |
| 214 run_loop.Run(); | 181 run_loop.Run(); |
| 215 devtools_http_handler.reset(); | 182 devtools_http_handler.reset(); |
| 216 // Make sure the handler actually stops. | 183 // Make sure the handler actually stops. |
| 217 run_loop_2.Run(); | 184 run_loop_2.Run(); |
| 218 | 185 |
| 219 // Now make sure the DevToolsActivePort was written into the | 186 // Now make sure the DevToolsActivePort was written into the |
| 220 // temporary directory and its contents are as expected. | 187 // temporary directory and its contents are as expected. |
| 221 base::FilePath active_port_file = temp_dir.path().Append( | 188 base::FilePath active_port_file = temp_dir.path().Append( |
| 222 kDevToolsActivePortFileName); | 189 kDevToolsActivePortFileName); |
| 223 EXPECT_TRUE(base::PathExists(active_port_file)); | 190 EXPECT_TRUE(base::PathExists(active_port_file)); |
| 224 std::string file_contents; | 191 std::string file_contents; |
| 225 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); | 192 EXPECT_TRUE(base::ReadFileToString(active_port_file, &file_contents)); |
| 226 int port = 0; | 193 int port = 0; |
| 227 EXPECT_TRUE(base::StringToInt(file_contents, &port)); | 194 EXPECT_TRUE(base::StringToInt(file_contents, &port)); |
| 228 EXPECT_EQ(static_cast<int>(kDummyPort), port); | 195 EXPECT_EQ(static_cast<int>(kDummyPort), port); |
| 229 } | 196 } |
| 230 | 197 |
| 231 } // namespace devtools_http_handler | 198 } // namespace devtools_http_handler |
| OLD | NEW |