| 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_impl.h" | 
|  | 6 | 
|  | 7 #include "net/proxy/mock_proxy_resolver.h" | 
|  | 8 #include "net/test/event_waiter.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 MojoProxyResolverFactoryImplTest : public testing::Test, | 
|  | 30                                          public mojo::ErrorHandler { | 
|  | 31  protected: | 
|  | 32   enum Event { | 
|  | 33     NONE, | 
|  | 34     RESOLVER_CREATED, | 
|  | 35     CONNECTION_ERROR, | 
|  | 36     RESOLVER_DESTROYED, | 
|  | 37   }; | 
|  | 38 | 
|  | 39   void SetUp() override { | 
|  | 40     new MojoProxyResolverFactoryImpl( | 
|  | 41         base::Bind(&MojoProxyResolverFactoryImplTest::CreateFakeProxyResolver, | 
|  | 42                    base::Unretained(this)), | 
|  | 43         mojo::GetProxy(&factory_)); | 
|  | 44   } | 
|  | 45 | 
|  | 46   void OnConnectionError() override { waiter_.NotifyEvent(CONNECTION_ERROR); } | 
|  | 47 | 
|  | 48   scoped_ptr<ProxyResolver> CreateFakeProxyResolver( | 
|  | 49       HostResolver* host_resolver) { | 
|  | 50     EXPECT_TRUE(host_resolver); | 
|  | 51     instances_created_++; | 
|  | 52     waiter_.NotifyEvent(RESOLVER_CREATED); | 
|  | 53     return make_scoped_ptr(new FakeProxyResolver(base::Bind( | 
|  | 54         &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed, | 
|  | 55         base::Unretained(this)))); | 
|  | 56   } | 
|  | 57 | 
|  | 58   void OnFakeProxyInstanceDestroyed() { | 
|  | 59     instances_destroyed_++; | 
|  | 60     waiter_.NotifyEvent(RESOLVER_DESTROYED); | 
|  | 61   } | 
|  | 62 | 
|  | 63   interfaces::ProxyResolverFactoryPtr factory_; | 
|  | 64 | 
|  | 65   int instances_created_ = 0; | 
|  | 66   int instances_destroyed_ = 0; | 
|  | 67 | 
|  | 68   EventWaiter<Event> waiter_; | 
|  | 69 }; | 
|  | 70 | 
|  | 71 TEST_F(MojoProxyResolverFactoryImplTest, DisconnectHostResolver) { | 
|  | 72   interfaces::ProxyResolverPtr proxy_resolver; | 
|  | 73   interfaces::HostResolverPtr host_resolver; | 
|  | 74   mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request = | 
|  | 75       mojo::GetProxy(&host_resolver); | 
|  | 76   factory_->CreateResolver(mojo::GetProxy(&proxy_resolver), | 
|  | 77                            host_resolver.Pass()); | 
|  | 78   proxy_resolver.set_error_handler(this); | 
|  | 79   waiter_.WaitForEvent(RESOLVER_CREATED); | 
|  | 80   EXPECT_EQ(1, instances_created_); | 
|  | 81   EXPECT_EQ(0, instances_destroyed_); | 
|  | 82   host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>(); | 
|  | 83   waiter_.WaitForEvent(CONNECTION_ERROR); | 
|  | 84   EXPECT_EQ(1, instances_created_); | 
|  | 85   EXPECT_EQ(1, instances_destroyed_); | 
|  | 86 } | 
|  | 87 | 
|  | 88 TEST_F(MojoProxyResolverFactoryImplTest, DisconnectProxyResolverClient) { | 
|  | 89   interfaces::ProxyResolverPtr proxy_resolver; | 
|  | 90   interfaces::HostResolverPtr host_resolver; | 
|  | 91   mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request = | 
|  | 92       mojo::GetProxy(&host_resolver); | 
|  | 93   mojo::Binding<interfaces::HostResolver> binding(nullptr, &host_resolver); | 
|  | 94   binding.set_error_handler(this); | 
|  | 95   factory_->CreateResolver(mojo::GetProxy(&proxy_resolver), | 
|  | 96                            host_resolver.Pass()); | 
|  | 97   waiter_.WaitForEvent(RESOLVER_CREATED); | 
|  | 98   EXPECT_EQ(1, instances_created_); | 
|  | 99   EXPECT_EQ(0, instances_destroyed_); | 
|  | 100   proxy_resolver.reset(); | 
|  | 101   waiter_.WaitForEvent(CONNECTION_ERROR); | 
|  | 102   EXPECT_EQ(1, instances_created_); | 
|  | 103   EXPECT_EQ(1, instances_destroyed_); | 
|  | 104 } | 
|  | 105 | 
|  | 106 TEST_F(MojoProxyResolverFactoryImplTest, DisconnectBoth) { | 
|  | 107   interfaces::ProxyResolverPtr proxy_resolver; | 
|  | 108   interfaces::HostResolverPtr host_resolver; | 
|  | 109   mojo::InterfaceRequest<interfaces::HostResolver> host_resolver_request = | 
|  | 110       mojo::GetProxy(&host_resolver); | 
|  | 111   factory_->CreateResolver(mojo::GetProxy(&proxy_resolver), | 
|  | 112                            host_resolver.Pass()); | 
|  | 113   waiter_.WaitForEvent(RESOLVER_CREATED); | 
|  | 114   EXPECT_EQ(1, instances_created_); | 
|  | 115   EXPECT_EQ(0, instances_destroyed_); | 
|  | 116   proxy_resolver.reset(); | 
|  | 117   host_resolver_request = mojo::InterfaceRequest<interfaces::HostResolver>(); | 
|  | 118   waiter_.WaitForEvent(RESOLVER_DESTROYED); | 
|  | 119   EXPECT_EQ(1, instances_created_); | 
|  | 120   EXPECT_EQ(1, instances_destroyed_); | 
|  | 121 } | 
|  | 122 | 
|  | 123 }  // namespace net | 
| OLD | NEW | 
|---|