This source file includes following definitions.
- SetUp
- TearDown
- OnSignalReceived
- OnConnectedToSignal
- MockResolveProxy
- TEST_F
#include "chrome/browser/chromeos/dbus/proxy_resolution_service_provider.h"
#include "base/bind.h"
#include "chrome/browser/chromeos/dbus/service_provider_test_helper.h"
#include "dbus/message.h"
#include "dbus/mock_exported_object.h"
#include "third_party/cros_system_api/dbus/service_constants.h"
using ::testing::_;
namespace chromeos {
const char kSourceURL[] = "http://www.gmail.com/";
const char kReturnSignalInterface[] = "org.chromium.TestInterface";
const char kReturnSignalName[] = "TestSignal";
const char kReturnProxyInfo[] = "PROXY cache.example.com:12345";
const char kReturnEmptyErrorMessage[] = "";
class MockProxyResolver : public ProxyResolverInterface {
public:
MOCK_METHOD4(ResolveProxy,
void(const std::string& source_url,
const std::string& signal_interface,
const std::string& signal_name,
scoped_refptr<dbus::ExportedObject> exported_object));
};
class ProxyResolutionServiceProviderTest : public testing::Test {
public:
ProxyResolutionServiceProviderTest()
: signal_received_successfully_(false) {
}
virtual void SetUp() OVERRIDE {
MockProxyResolver* mock_resolver = new MockProxyResolver;
EXPECT_CALL(*mock_resolver,
ResolveProxy(kSourceURL, kReturnSignalInterface,
kReturnSignalName, _))
.WillOnce(Invoke(
this,
&ProxyResolutionServiceProviderTest::MockResolveProxy));
service_provider_.reset(
ProxyResolutionServiceProvider::CreateForTesting(mock_resolver));
test_helper_.SetUp(kResolveNetworkProxy, service_provider_.get());
test_helper_.SetUpReturnSignal(
kReturnSignalInterface,
kReturnSignalName,
base::Bind(&ProxyResolutionServiceProviderTest::OnSignalReceived,
base::Unretained(this)),
base::Bind(&ProxyResolutionServiceProviderTest::OnConnectedToSignal,
base::Unretained(this)));
}
virtual void TearDown() OVERRIDE {
test_helper_.TearDown();
service_provider_.reset();
}
protected:
void OnSignalReceived(dbus::Signal* signal) {
ASSERT_EQ(kReturnSignalInterface, signal->GetInterface());
ASSERT_EQ(kReturnSignalName, signal->GetMember());
std::string source_url;
std::string proxy_info;
std::string error_message;
dbus::MessageReader reader(signal);
ASSERT_TRUE(reader.PopString(&source_url));
ASSERT_TRUE(reader.PopString(&proxy_info));
ASSERT_TRUE(reader.PopString(&error_message));
EXPECT_EQ(kSourceURL, source_url);
EXPECT_EQ(kReturnProxyInfo, proxy_info);
EXPECT_EQ(kReturnEmptyErrorMessage, error_message);
signal_received_successfully_ = true;
}
void OnConnectedToSignal(const std::string& signal_interface,
const std::string& signal_name,
bool success){
ASSERT_EQ(kReturnSignalInterface, signal_interface);
ASSERT_EQ(kReturnSignalName, signal_name);
ASSERT_TRUE(success);
}
void MockResolveProxy(const std::string& source_url,
const std::string& signal_interface,
const std::string& signal_name,
scoped_refptr<dbus::ExportedObject> exported_object) {
if (source_url == kSourceURL) {
dbus::Signal signal(signal_interface,
signal_name);
dbus::MessageWriter writer(&signal);
writer.AppendString(kSourceURL);
writer.AppendString(kReturnProxyInfo);
writer.AppendString(kReturnEmptyErrorMessage);
exported_object->SendSignal(&signal);
return;
}
LOG(ERROR) << "Unexpected source URL: " << source_url;
}
bool signal_received_successfully_;
ServiceProviderTestHelper test_helper_;
scoped_ptr<CrosDBusService::ServiceProviderInterface> service_provider_;
};
TEST_F(ProxyResolutionServiceProviderTest, ResolveProxy) {
ASSERT_FALSE(signal_received_successfully_);
dbus::MethodCall method_call(kLibCrosServiceInterface, kResolveNetworkProxy);
dbus::MessageWriter writer(&method_call);
writer.AppendString(kSourceURL);
writer.AppendString(kReturnSignalInterface);
writer.AppendString(kReturnSignalName);
scoped_ptr<dbus::Response> response(test_helper_.CallMethod(&method_call));
ASSERT_TRUE(response.get());
dbus::MessageReader reader(response.get());
ASSERT_FALSE(reader.HasMoreData());
ASSERT_TRUE(signal_received_successfully_);
}
}