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/run_loop.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "chromeos/dbus/services/service_provider_test_helper.h"
12 #include "dbus/message.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "third_party/cros_system_api/dbus/service_constants.h"
20 // ProxyResolutionServiceProvider will return the proxy info as a D-Bus
21 // signal, to the following signal interface and the signal name.
22 const char kReturnSignalInterface
[] = "org.chromium.TestInterface";
23 const char kReturnSignalName
[] = "TestSignal";
25 // Test ProxyResolverDelegate implementation.
26 class TestProxyResolverDelegate
: public ProxyResolverDelegate
{
28 explicit TestProxyResolverDelegate(
29 const scoped_refptr
<base::SingleThreadTaskRunner
>& network_task_runner
)
30 : request_context_getter_(
31 new net::TestURLRequestContextGetter(network_task_runner
)) {}
33 ~TestProxyResolverDelegate() override
{}
35 // ProxyResolverDelegate override.
36 scoped_refptr
<net::URLRequestContextGetter
> GetRequestContext() override
{
37 return request_context_getter_
;
41 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_getter_
;
43 DISALLOW_COPY_AND_ASSIGN(TestProxyResolverDelegate
);
48 class ProxyResolutionServiceProviderTest
: public testing::Test
{
50 void SetUp() override
{
51 // Create the proxy resolution service with the mock bus and the mock
53 service_provider_
.reset(ProxyResolutionServiceProvider::Create(
54 make_scoped_ptr(new TestProxyResolverDelegate(
55 base::ThreadTaskRunnerHandle::Get()))));
57 test_helper_
.SetUp(kResolveNetworkProxy
, service_provider_
.get());
59 // Connect to the signal that will be sent to kReturnSignalInterface and
60 // kReturnSignalName. ResolveNetworkProxy() will send the result as a
61 // signal. OnSignalReceived() will be called upon the delivery.
62 test_helper_
.SetUpReturnSignal(
63 kReturnSignalInterface
,
65 base::Bind(&ProxyResolutionServiceProviderTest::OnSignalReceived
,
66 base::Unretained(this)),
67 base::Bind(&ProxyResolutionServiceProviderTest::OnConnectedToSignal
,
68 base::Unretained(this)));
71 void TearDown() override
{
72 test_helper_
.TearDown();
73 service_provider_
.reset();
77 // Called when a signal is received.
78 void OnSignalReceived(dbus::Signal
* signal
) {
79 EXPECT_EQ(kReturnSignalInterface
, signal
->GetInterface());
80 EXPECT_EQ(kReturnSignalName
, signal
->GetMember());
82 // The signal should contain three strings.
83 dbus::MessageReader
reader(signal
);
84 EXPECT_TRUE(reader
.PopString(&source_url_
));
85 EXPECT_TRUE(reader
.PopString(&proxy_info_
));
86 EXPECT_TRUE(reader
.PopString(&error_message_
));
89 // Called when connected to a signal.
90 void OnConnectedToSignal(const std::string
& signal_interface
,
91 const std::string
& signal_name
,
93 EXPECT_EQ(kReturnSignalInterface
, signal_interface
);
94 EXPECT_EQ(kReturnSignalName
, signal_name
);
99 std::string source_url_
;
100 std::string proxy_info_
;
101 std::string error_message_
;
102 ServiceProviderTestHelper test_helper_
;
103 scoped_ptr
<CrosDBusService::ServiceProviderInterface
> service_provider_
;
106 TEST_F(ProxyResolutionServiceProviderTest
, ResolveProxy
) {
107 const char kSourceURL
[] = "http://www.gmail.com/";
109 // Create a method call to resolve proxy config for kSourceURL.
110 dbus::MethodCall
method_call(kLibCrosServiceInterface
, kResolveNetworkProxy
);
111 dbus::MessageWriter
writer(&method_call
);
112 writer
.AppendString(kSourceURL
);
113 writer
.AppendString(kReturnSignalInterface
);
114 writer
.AppendString(kReturnSignalName
);
116 // Call the ResolveNetworkProxy method.
117 scoped_ptr
<dbus::Response
> response(test_helper_
.CallMethod(&method_call
));
118 base::RunLoop().RunUntilIdle();
120 // An empty response should be returned.
121 ASSERT_TRUE(response
.get());
122 dbus::MessageReader
reader(response
.get());
123 EXPECT_FALSE(reader
.HasMoreData());
125 // Confirm that the signal is received successfully.
126 EXPECT_EQ(kSourceURL
, source_url_
);
127 EXPECT_EQ("DIRECT", proxy_info_
);
128 EXPECT_EQ("", error_message_
);
131 } // namespace chromeos