Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 "net/proxy/mojo_proxy_resolver_factory.h" | |
| 6 | |
| 7 #include "base/run_loop.h" | |
| 8 #include "net/proxy/mock_proxy_resolver.h" | |
| 9 #include "testing/gtest/include/gtest/gtest.h" | |
| 10 #include "third_party/mojo/src/mojo/public/cpp/bindings/binding.h" | |
| 11 #include "third_party/mojo/src/mojo/public/cpp/bindings/error_handler.h" | |
| 12 | |
| 13 namespace net { | |
| 14 namespace { | |
| 15 | |
| 16 class FakeProxyResolver : public MockAsyncProxyResolverExpectsBytes { | |
| 17 public: | |
| 18 explicit FakeProxyResolver(const base::Closure& on_destruction) | |
| 19 : on_destruction_(on_destruction) {} | |
| 20 | |
| 21 ~FakeProxyResolver() override { on_destruction_.Run(); } | |
| 22 | |
| 23 private: | |
| 24 const base::Closure on_destruction_; | |
| 25 }; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 class MojoProxyResolverFactoryTest : public testing::Test, | |
| 30 public mojo::ErrorHandler { | |
| 31 protected: | |
| 32 enum Event { | |
| 33 NONE, | |
| 34 RESOLVER_CREATED, | |
| 35 CONNECTION_ERROR, | |
| 36 }; | |
| 37 | |
| 38 void SetUp() override { | |
| 39 new MojoProxyResolverFactory( | |
| 40 base::Bind(&MojoProxyResolverFactoryTest::CreateFakeProxyResolver, | |
| 41 base::Unretained(this)), | |
| 42 mojo::GetProxy(&factory_)); | |
| 43 } | |
| 44 | |
| 45 void OnConnectionError() override { | |
| 46 if (expected_event_ == CONNECTION_ERROR && | |
| 47 !run_loop_quit_closure_.is_null()) { | |
| 48 run_loop_quit_closure_.Run(); | |
| 49 run_loop_quit_closure_.Reset(); | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 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/
| |
| 54 expected_event_ = event; | |
| 55 base::RunLoop run_loop; | |
| 56 run_loop_quit_closure_ = run_loop.QuitClosure(); | |
| 57 run_loop.Run(); | |
| 58 } | |
| 59 | |
| 60 scoped_ptr<ProxyResolver> CreateFakeProxyResolver( | |
| 61 HostResolver* host_resolver) { | |
| 62 EXPECT_TRUE(host_resolver); | |
| 63 instances_created_++; | |
| 64 if (expected_event_ == RESOLVER_CREATED && | |
| 65 !run_loop_quit_closure_.is_null()) { | |
| 66 run_loop_quit_closure_.Run(); | |
| 67 run_loop_quit_closure_.Reset(); | |
| 68 } | |
| 69 return make_scoped_ptr(new FakeProxyResolver( | |
| 70 base::Bind(&MojoProxyResolverFactoryTest::OnFakeProxyInstanceDestroyed, | |
| 71 base::Unretained(this)))); | |
| 72 } | |
| 73 | |
| 74 void OnFakeProxyInstanceDestroyed() { instances_destroyed_++; } | |
| 75 | |
| 76 interfaces::ProxyResolverFactoryPtr factory_; | |
| 77 | |
| 78 int instances_created_ = 0; | |
| 79 int instances_destroyed_ = 0; | |
| 80 | |
| 81 Event expected_event_; | |
| 82 base::Closure run_loop_quit_closure_; | |
| 83 }; | |
| 84 | |
| 85 TEST_F(MojoProxyResolverFactoryTest, DisconnectHostResolver) { | |
| 86 interfaces::ProxyResolverPtr proxy_resolver; | |
| 87 interfaces::HostResolverPtr host_resolver; | |
| 88 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request = | |
| 89 mojo::GetProxy(&host_resolver); | |
| 90 factory_->CreateResolver(mojo::GetProxy(&proxy_resolver), | |
| 91 host_resolver.Pass()); | |
| 92 proxy_resolver.set_error_handler(this); | |
| 93 WaitForEvent(RESOLVER_CREATED); | |
| 94 EXPECT_EQ(1, instances_created_); | |
| 95 EXPECT_EQ(0, instances_destroyed_); | |
| 96 host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>(); | |
| 97 WaitForEvent(CONNECTION_ERROR); | |
| 98 EXPECT_EQ(1, instances_created_); | |
| 99 EXPECT_EQ(1, instances_destroyed_); | |
| 100 } | |
| 101 | |
| 102 TEST_F(MojoProxyResolverFactoryTest, DisconnectProxyResolverClient) { | |
| 103 interfaces::ProxyResolverPtr proxy_resolver; | |
| 104 interfaces::HostResolverPtr host_resolver; | |
| 105 mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request = | |
| 106 mojo::GetProxy(&host_resolver); | |
| 107 mojo::Binding<interfaces::HostResolver> binding(nullptr, &host_resolver); | |
| 108 binding.set_error_handler(this); | |
| 109 factory_->CreateResolver(mojo::GetProxy(&proxy_resolver), | |
| 110 host_resolver.Pass()); | |
| 111 WaitForEvent(RESOLVER_CREATED); | |
| 112 EXPECT_EQ(1, instances_created_); | |
| 113 EXPECT_EQ(0, instances_destroyed_); | |
| 114 proxy_resolver.reset(); | |
| 115 WaitForEvent(CONNECTION_ERROR); | |
| 116 EXPECT_EQ(1, instances_created_); | |
| 117 EXPECT_EQ(1, instances_destroyed_); | |
| 118 } | |
| 119 | |
| 120 } // namespace net | |
| OLD | NEW |