Index: net/proxy/mojo_proxy_resolver_factory_unittest.cc |
diff --git a/net/proxy/mojo_proxy_resolver_factory_unittest.cc b/net/proxy/mojo_proxy_resolver_factory_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..c4ed5a5ff188cc14e5102cca492d522283d31eba |
--- /dev/null |
+++ b/net/proxy/mojo_proxy_resolver_factory_unittest.cc |
@@ -0,0 +1,120 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "net/proxy/mojo_proxy_resolver_factory.h" |
+ |
+#include "base/run_loop.h" |
+#include "net/proxy/mock_proxy_resolver.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+#include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" |
+#include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" |
+ |
+namespace net { |
+namespace { |
+ |
+class FakeProxyResolver : public MockAsyncProxyResolverExpectsBytes { |
+ public: |
+ explicit FakeProxyResolver(const base::Closure& on_destruction) |
+ : on_destruction_(on_destruction) {} |
+ |
+ ~FakeProxyResolver() override { on_destruction_.Run(); } |
+ |
+ private: |
+ const base::Closure on_destruction_; |
+}; |
+ |
+} // namespace |
+ |
+class MojoProxyResolverFactoryTest : public testing::Test, |
+ public mojo::ErrorHandler { |
+ protected: |
+ enum Event { |
+ NONE, |
+ RESOLVER_CREATED, |
+ CONNECTION_ERROR, |
+ }; |
+ |
+ void SetUp() override { |
+ new MojoProxyResolverFactory( |
+ base::Bind(&MojoProxyResolverFactoryTest::CreateFakeProxyResolver, |
+ base::Unretained(this)), |
+ mojo::GetProxy(&factory_)); |
+ } |
+ |
+ void OnConnectionError() override { |
+ if (expected_event_ == CONNECTION_ERROR && |
+ !run_loop_quit_closure_.is_null()) { |
+ run_loop_quit_closure_.Run(); |
+ run_loop_quit_closure_.Reset(); |
+ } |
+ } |
+ |
+ void WaitForEvent(Event event) { |
Anand Mistry (off Chromium)
2015/02/16 06:46:15
Ok, I've seen this pattern a few times. I think it
Sam McNally
2015/02/17 00:00:57
Done in https://codereview.chromium.org/904313003/
|
+ expected_event_ = event; |
+ base::RunLoop run_loop; |
+ run_loop_quit_closure_ = run_loop.QuitClosure(); |
+ run_loop.Run(); |
+ } |
+ |
+ scoped_ptr<ProxyResolver> CreateFakeProxyResolver( |
+ HostResolver* host_resolver) { |
+ EXPECT_TRUE(host_resolver); |
+ instances_created_++; |
+ if (expected_event_ == RESOLVER_CREATED && |
+ !run_loop_quit_closure_.is_null()) { |
+ run_loop_quit_closure_.Run(); |
+ run_loop_quit_closure_.Reset(); |
+ } |
+ return make_scoped_ptr(new FakeProxyResolver( |
+ base::Bind(&MojoProxyResolverFactoryTest::OnFakeProxyInstanceDestroyed, |
+ base::Unretained(this)))); |
+ } |
+ |
+ void OnFakeProxyInstanceDestroyed() { instances_destroyed_++; } |
+ |
+ interfaces::ProxyResolverFactoryPtr factory_; |
+ |
+ int instances_created_ = 0; |
+ int instances_destroyed_ = 0; |
+ |
+ Event expected_event_; |
+ base::Closure run_loop_quit_closure_; |
+}; |
+ |
+TEST_F(MojoProxyResolverFactoryTest, DisconnectHostResolver) { |
+ interfaces::ProxyResolverPtr proxy_resolver; |
+ interfaces::HostResolverPtr host_resolver; |
+ mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request = |
+ mojo::GetProxy(&host_resolver); |
+ factory_->CreateResolver(mojo::GetProxy(&proxy_resolver), |
+ host_resolver.Pass()); |
+ proxy_resolver.set_error_handler(this); |
+ WaitForEvent(RESOLVER_CREATED); |
+ EXPECT_EQ(1, instances_created_); |
+ EXPECT_EQ(0, instances_destroyed_); |
+ host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>(); |
+ WaitForEvent(CONNECTION_ERROR); |
+ EXPECT_EQ(1, instances_created_); |
+ EXPECT_EQ(1, instances_destroyed_); |
+} |
+ |
+TEST_F(MojoProxyResolverFactoryTest, DisconnectProxyResolverClient) { |
+ interfaces::ProxyResolverPtr proxy_resolver; |
+ interfaces::HostResolverPtr host_resolver; |
+ mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request = |
+ mojo::GetProxy(&host_resolver); |
+ mojo::Binding<interfaces::HostResolver> binding(nullptr, &host_resolver); |
+ binding.set_error_handler(this); |
+ factory_->CreateResolver(mojo::GetProxy(&proxy_resolver), |
+ host_resolver.Pass()); |
+ WaitForEvent(RESOLVER_CREATED); |
+ EXPECT_EQ(1, instances_created_); |
+ EXPECT_EQ(0, instances_destroyed_); |
+ proxy_resolver.reset(); |
+ WaitForEvent(CONNECTION_ERROR); |
+ EXPECT_EQ(1, instances_created_); |
+ EXPECT_EQ(1, instances_destroyed_); |
+} |
+ |
+} // namespace net |