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.
5 #include "net/proxy/mojo_proxy_resolver_factory_impl.h"
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"
16 class FakeProxyResolver
: public MockAsyncProxyResolverExpectsBytes
{
18 explicit FakeProxyResolver(const base::Closure
& on_destruction
)
19 : on_destruction_(on_destruction
) {}
21 ~FakeProxyResolver() override
{ on_destruction_
.Run(); }
24 const base::Closure on_destruction_
;
29 class MojoProxyResolverFactoryImplTest
: public testing::Test
,
30 public mojo::ErrorHandler
{
39 void SetUp() override
{
40 new MojoProxyResolverFactoryImpl(
41 base::Bind(&MojoProxyResolverFactoryImplTest::CreateFakeProxyResolver
,
42 base::Unretained(this)),
43 mojo::GetProxy(&factory_
));
46 void OnConnectionError() override
{ waiter_
.NotifyEvent(CONNECTION_ERROR
); }
48 scoped_ptr
<ProxyResolver
> CreateFakeProxyResolver(
49 HostResolver
* host_resolver
,
50 const ProxyResolver::LoadStateChangedCallback
& callback
) {
51 EXPECT_TRUE(host_resolver
);
53 waiter_
.NotifyEvent(RESOLVER_CREATED
);
54 return make_scoped_ptr(new FakeProxyResolver(base::Bind(
55 &MojoProxyResolverFactoryImplTest::OnFakeProxyInstanceDestroyed
,
56 base::Unretained(this))));
59 void OnFakeProxyInstanceDestroyed() {
60 instances_destroyed_
++;
61 waiter_
.NotifyEvent(RESOLVER_DESTROYED
);
64 interfaces::ProxyResolverFactoryPtr factory_
;
66 int instances_created_
= 0;
67 int instances_destroyed_
= 0;
69 EventWaiter
<Event
> waiter_
;
72 TEST_F(MojoProxyResolverFactoryImplTest
, DisconnectHostResolver
) {
73 interfaces::ProxyResolverPtr proxy_resolver
;
74 interfaces::HostResolverPtr host_resolver
;
75 mojo::InterfaceRequest
<interfaces::HostResolver
> host_resolver_request
=
76 mojo::GetProxy(&host_resolver
);
77 factory_
->CreateResolver(mojo::GetProxy(&proxy_resolver
),
78 host_resolver
.Pass());
79 proxy_resolver
.set_error_handler(this);
80 waiter_
.WaitForEvent(RESOLVER_CREATED
);
81 EXPECT_EQ(1, instances_created_
);
82 EXPECT_EQ(0, instances_destroyed_
);
83 host_resolver_request
= mojo::InterfaceRequest
<interfaces::HostResolver
>();
84 waiter_
.WaitForEvent(CONNECTION_ERROR
);
85 EXPECT_EQ(1, instances_created_
);
86 EXPECT_EQ(1, instances_destroyed_
);
89 TEST_F(MojoProxyResolverFactoryImplTest
, DisconnectProxyResolverClient
) {
90 interfaces::ProxyResolverPtr proxy_resolver
;
91 interfaces::HostResolverPtr host_resolver
;
92 mojo::InterfaceRequest
<interfaces::HostResolver
> host_resolver_request
=
93 mojo::GetProxy(&host_resolver
);
94 mojo::Binding
<interfaces::HostResolver
> binding(nullptr, &host_resolver
);
95 binding
.set_error_handler(this);
96 factory_
->CreateResolver(mojo::GetProxy(&proxy_resolver
),
97 host_resolver
.Pass());
98 waiter_
.WaitForEvent(RESOLVER_CREATED
);
99 EXPECT_EQ(1, instances_created_
);
100 EXPECT_EQ(0, instances_destroyed_
);
101 proxy_resolver
.reset();
102 waiter_
.WaitForEvent(CONNECTION_ERROR
);
103 EXPECT_EQ(1, instances_created_
);
104 EXPECT_EQ(1, instances_destroyed_
);
107 TEST_F(MojoProxyResolverFactoryImplTest
, DisconnectBoth
) {
108 interfaces::ProxyResolverPtr proxy_resolver
;
109 interfaces::HostResolverPtr host_resolver
;
110 mojo::InterfaceRequest
<interfaces::HostResolver
> host_resolver_request
=
111 mojo::GetProxy(&host_resolver
);
112 factory_
->CreateResolver(mojo::GetProxy(&proxy_resolver
),
113 host_resolver
.Pass());
114 waiter_
.WaitForEvent(RESOLVER_CREATED
);
115 EXPECT_EQ(1, instances_created_
);
116 EXPECT_EQ(0, instances_destroyed_
);
117 proxy_resolver
.reset();
118 host_resolver_request
= mojo::InterfaceRequest
<interfaces::HostResolver
>();
119 waiter_
.WaitForEvent(RESOLVER_DESTROYED
);
120 EXPECT_EQ(1, instances_created_
);
121 EXPECT_EQ(1, instances_destroyed_
);