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
3 // found in the LICENSE file.
5 #include "chromeos/dbus/services/proxy_resolution_service_provider.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "base/run_loop.h"
10 #include "chromeos/dbus/services/service_provider_test_helper.h"
11 #include "dbus/message.h"
12 #include "net/url_request/url_request_test_util.h"
13 #include "third_party/cros_system_api/dbus/service_constants.h"
19 // ProxyResolutionServiceProvider will return the proxy info as a D-Bus
20 // signal, to the following signal interface and the signal name.
21 const char kReturnSignalInterface
[] = "org.chromium.TestInterface";
22 const char kReturnSignalName
[] = "TestSignal";
24 // Test ProxyResolverDelegate implementation.
25 class TestProxyResolverDelegate
: public ProxyResolverDelegate
{
27 explicit TestProxyResolverDelegate(
28 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_task_runner
)
29 : request_context_getter_(
30 new net::TestURLRequestContextGetter(network_task_runner
)) {}
32 ~TestProxyResolverDelegate() override
{}
34 // ProxyResolverDelegate override.
35 scoped_refptr
<net::URLRequestContextGetter
> GetRequestContext() override
{
36 return request_context_getter_
;
40 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_getter_
;
42 DISALLOW_COPY_AND_ASSIGN(TestProxyResolverDelegate
);
47 class ProxyResolutionServiceProviderTest
: public testing::Test
{
49 void SetUp() override
{
50 // Create the proxy resolution service with the mock bus and the mock
52 service_provider_
.reset(ProxyResolutionServiceProvider::Create(
53 make_scoped_ptr(new TestProxyResolverDelegate(
54 base::MessageLoopProxy::current()))));
56 test_helper_
.SetUp(kResolveNetworkProxy
, service_provider_
.get());
58 // Connect to the signal that will be sent to kReturnSignalInterface and
59 // kReturnSignalName. ResolveNetworkProxy() will send the result as a
60 // signal. OnSignalReceived() will be called upon the delivery.
61 test_helper_
.SetUpReturnSignal(
62 kReturnSignalInterface
,
64 base::Bind(&ProxyResolutionServiceProviderTest::OnSignalReceived
,
65 base::Unretained(this)),
66 base::Bind(&ProxyResolutionServiceProviderTest::OnConnectedToSignal
,
67 base::Unretained(this)));
70 void TearDown() override
{
71 test_helper_
.TearDown();
72 service_provider_
.reset();
76 // Called when a signal is received.
77 void OnSignalReceived(dbus::Signal
* signal
) {
78 EXPECT_EQ(kReturnSignalInterface
, signal
->GetInterface());
79 EXPECT_EQ(kReturnSignalName
, signal
->GetMember());
81 // The signal should contain three strings.
82 dbus::MessageReader
reader(signal
);
83 EXPECT_TRUE(reader
.PopString(&source_url_
));
84 EXPECT_TRUE(reader
.PopString(&proxy_info_
));
85 EXPECT_TRUE(reader
.PopString(&error_message_
));
88 // Called when connected to a signal.
89 void OnConnectedToSignal(const std::string
& signal_interface
,
90 const std::string
& signal_name
,
92 EXPECT_EQ(kReturnSignalInterface
, signal_interface
);
93 EXPECT_EQ(kReturnSignalName
, signal_name
);
98 std::string source_url_
;
99 std::string proxy_info_
;
100 std::string error_message_
;
101 ServiceProviderTestHelper test_helper_
;
102 scoped_ptr
<CrosDBusService::ServiceProviderInterface
> service_provider_
;
105 TEST_F(ProxyResolutionServiceProviderTest
, ResolveProxy
) {
106 const char kSourceURL
[] = "http://www.gmail.com/";
108 // Create a method call to resolve proxy config for kSourceURL.
109 dbus::MethodCall
method_call(kLibCrosServiceInterface
, kResolveNetworkProxy
);
110 dbus::MessageWriter
writer(&method_call
);
111 writer
.AppendString(kSourceURL
);
112 writer
.AppendString(kReturnSignalInterface
);
113 writer
.AppendString(kReturnSignalName
);
115 // Call the ResolveNetworkProxy method.
116 scoped_ptr
<dbus::Response
> response(test_helper_
.CallMethod(&method_call
));
117 base::RunLoop().RunUntilIdle();
119 // An empty response should be returned.
120 ASSERT_TRUE(response
.get());
121 dbus::MessageReader
reader(response
.get());
122 EXPECT_FALSE(reader
.HasMoreData());
124 // Confirm that the signal is received successfully.
125 EXPECT_EQ(kSourceURL
, source_url_
);
126 EXPECT_EQ("DIRECT", proxy_info_
);
127 EXPECT_EQ("", error_message_
);
130 } // namespace chromeos